From d796c9dd933ab96ec83b9a634feedd5d32e1ba3f Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Tue, 8 Nov 2011 12:31:36 -0600 Subject: Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731 --- src/kernel/makepsheader.pl | 93 + src/kernel/q1xcompatibility.h | 49 + src/kernel/qabstractlayout.cpp | 1945 ++++++++ src/kernel/qabstractlayout.h | 54 + src/kernel/qaccel.cpp | 1089 +++++ src/kernel/qaccel.h | 110 + src/kernel/qaccessible.cpp | 719 +++ src/kernel/qaccessible.h | 295 ++ src/kernel/qapplication.cpp | 4602 +++++++++++++++++++ src/kernel/qapplication.h | 555 +++ src/kernel/qapplication_p.h | 95 + src/kernel/qapplication_x11.cpp | 6653 +++++++++++++++++++++++++++ src/kernel/qasyncimageio.cpp | 1309 ++++++ src/kernel/qasyncimageio.h | 111 + src/kernel/qasyncio.cpp | 360 ++ src/kernel/qasyncio.h | 120 + src/kernel/qbitmap.cpp | 304 ++ src/kernel/qbitmap.h | 73 + src/kernel/qbrush.h | 94 + src/kernel/qclipboard.cpp | 567 +++ src/kernel/qclipboard.h | 134 + src/kernel/qclipboard_x11.cpp | 1674 +++++++ src/kernel/qcolor.cpp | 1030 +++++ src/kernel/qcolor.h | 229 + src/kernel/qcolor_p.cpp | 797 ++++ src/kernel/qcolor_p.h | 66 + src/kernel/qcolor_x11.cpp | 835 ++++ src/kernel/qconnection.cpp | 94 + src/kernel/qconnection.h | 78 + src/kernel/qcursor.cpp | 287 ++ src/kernel/qcursor.h | 153 + src/kernel/qcursor_x11.cpp | 833 ++++ src/kernel/qdesktopwidget.h | 103 + src/kernel/qdesktopwidget_x11.cpp | 356 ++ src/kernel/qdnd_x11.cpp | 1859 ++++++++ src/kernel/qdragobject.cpp | 1811 ++++++++ src/kernel/qdragobject.h | 279 ++ src/kernel/qdrawutil.cpp | 957 ++++ src/kernel/qdrawutil.h | 128 + src/kernel/qdropsite.cpp | 81 + src/kernel/qdropsite.h | 59 + src/kernel/qevent.cpp | 2571 +++++++++++ src/kernel/qevent.h | 617 +++ src/kernel/qeventloop.cpp | 394 ++ src/kernel/qeventloop.h | 122 + src/kernel/qeventloop_p.h | 150 + src/kernel/qeventloop_unix.cpp | 587 +++ src/kernel/qeventloop_x11.cpp | 417 ++ src/kernel/qfocusdata.cpp | 141 + src/kernel/qfocusdata.h | 70 + src/kernel/qfont.cpp | 3356 ++++++++++++++ src/kernel/qfont.h | 372 ++ src/kernel/qfont_x11.cpp | 737 +++ src/kernel/qfontdata_p.h | 278 ++ src/kernel/qfontdatabase.cpp | 2491 ++++++++++ src/kernel/qfontdatabase.h | 227 + src/kernel/qfontdatabase_x11.cpp | 2014 +++++++++ src/kernel/qfontengine_p.h | 632 +++ src/kernel/qfontengine_x11.cpp | 2724 +++++++++++ src/kernel/qfontinfo.h | 91 + src/kernel/qfontmetrics.h | 117 + src/kernel/qgif.h | 61 + src/kernel/qgplugin.cpp | 72 + src/kernel/qgplugin.h | 146 + src/kernel/qguardedptr.cpp | 226 + src/kernel/qguardedptr.h | 144 + src/kernel/qiconset.cpp | 949 ++++ src/kernel/qiconset.h | 132 + src/kernel/qimage.cpp | 6497 ++++++++++++++++++++++++++ src/kernel/qimage.h | 425 ++ src/kernel/qimageformatinterface_p.h | 81 + src/kernel/qimageformatplugin.cpp | 184 + src/kernel/qimageformatplugin.h | 67 + src/kernel/qinputcontext.cpp | 856 ++++ src/kernel/qinputcontext.h | 143 + src/kernel/qinputcontext_p.h | 127 + src/kernel/qinputcontext_x11.cpp | 73 + src/kernel/qinternal.cpp | 787 ++++ src/kernel/qinternal_p.h | 213 + src/kernel/qjpegio.cpp | 599 +++ src/kernel/qjpegio.h | 52 + src/kernel/qkeycode.h | 50 + src/kernel/qkeysequence.cpp | 731 +++ src/kernel/qkeysequence.h | 109 + src/kernel/qlayout.cpp | 2605 +++++++++++ src/kernel/qlayout.h | 473 ++ src/kernel/qlayoutengine.cpp | 322 ++ src/kernel/qlayoutengine_p.h | 129 + src/kernel/qlocalfs.cpp | 407 ++ src/kernel/qlocalfs.h | 75 + src/kernel/qlock.cpp | 297 ++ src/kernel/qlock_p.h | 99 + src/kernel/qmetaobject.cpp | 1251 +++++ src/kernel/qmetaobject.h | 286 ++ src/kernel/qmime.cpp | 618 +++ src/kernel/qmime.h | 200 + src/kernel/qmngio.cpp | 464 ++ src/kernel/qmngio.h | 53 + src/kernel/qmotifdnd_x11.cpp | 978 ++++ src/kernel/qmovie.cpp | 1081 +++++ src/kernel/qmovie.h | 120 + src/kernel/qnamespace.h | 1008 +++++ src/kernel/qnetworkprotocol.cpp | 1265 ++++++ src/kernel/qnetworkprotocol.h | 244 + src/kernel/qobject.cpp | 2711 +++++++++++ src/kernel/qobject.h | 265 ++ src/kernel/qobjectcleanuphandler.cpp | 172 + src/kernel/qobjectcleanuphandler.h | 68 + src/kernel/qobjectdefs.h | 177 + src/kernel/qobjectdict.h | 66 + src/kernel/qobjectlist.h | 92 + src/kernel/qpaintdevice.h | 421 ++ src/kernel/qpaintdevice_x11.cpp | 1168 +++++ src/kernel/qpaintdevicedefs.h | 48 + src/kernel/qpaintdevicemetrics.cpp | 151 + src/kernel/qpaintdevicemetrics.h | 83 + src/kernel/qpainter.cpp | 3966 ++++++++++++++++ src/kernel/qpainter.h | 721 +++ src/kernel/qpainter_p.h | 66 + src/kernel/qpainter_x11.cpp | 3153 +++++++++++++ src/kernel/qpalette.cpp | 1224 +++++ src/kernel/qpalette.h | 189 + src/kernel/qpen.h | 102 + src/kernel/qpicture.cpp | 1229 +++++ src/kernel/qpicture.h | 127 + src/kernel/qpixmap.cpp | 1510 +++++++ src/kernel/qpixmap.h | 354 ++ src/kernel/qpixmap_x11.cpp | 2476 ++++++++++ src/kernel/qpixmapcache.cpp | 336 ++ src/kernel/qpixmapcache.h | 63 + src/kernel/qpngio.cpp | 1256 ++++++ src/kernel/qpngio.h | 107 + src/kernel/qpoint.cpp | 443 ++ src/kernel/qpoint.h | 219 + src/kernel/qpointarray.cpp | 1109 +++++ src/kernel/qpointarray.h | 118 + src/kernel/qpolygonscanner.cpp | 937 ++++ src/kernel/qpolygonscanner.h | 61 + src/kernel/qprinter.cpp | 1025 +++++ src/kernel/qprinter.h | 284 ++ src/kernel/qprinter_p.h | 59 + src/kernel/qprinter_unix.cpp | 672 +++ src/kernel/qprocess.cpp | 806 ++++ src/kernel/qprocess.h | 178 + src/kernel/qprocess_unix.cpp | 1409 ++++++ src/kernel/qpsprinter.cpp | 6582 +++++++++++++++++++++++++++ src/kernel/qpsprinter.ps | 805 ++++ src/kernel/qpsprinter_p.h | 92 + src/kernel/qrect.cpp | 960 ++++ src/kernel/qrect.h | 276 ++ src/kernel/qregion.cpp | 383 ++ src/kernel/qregion.h | 177 + src/kernel/qregion_x11.cpp | 2898 ++++++++++++ src/kernel/qrichtext.cpp | 8258 ++++++++++++++++++++++++++++++++++ src/kernel/qrichtext_p.cpp | 636 +++ src/kernel/qrichtext_p.h | 2141 +++++++++ src/kernel/qscriptengine.cpp | 1624 +++++++ src/kernel/qscriptengine_p.h | 83 + src/kernel/qscriptengine_x11.cpp | 3752 +++++++++++++++ src/kernel/qsession.h | 47 + src/kernel/qsessionmanager.h | 99 + src/kernel/qsharedmemory_p.cpp | 169 + src/kernel/qsharedmemory_p.h | 95 + src/kernel/qsignal.cpp | 257 ++ src/kernel/qsignal.h | 94 + src/kernel/qsignalmapper.cpp | 183 + src/kernel/qsignalmapper.h | 78 + src/kernel/qsignalslotimp.h | 97 + src/kernel/qsimplerichtext.cpp | 421 ++ src/kernel/qsimplerichtext.h | 104 + src/kernel/qsize.cpp | 432 ++ src/kernel/qsize.h | 237 + src/kernel/qsizegrip.cpp | 286 ++ src/kernel/qsizegrip.h | 74 + src/kernel/qsizepolicy.h | 128 + src/kernel/qsocketnotifier.cpp | 265 ++ src/kernel/qsocketnotifier.h | 93 + src/kernel/qsound.cpp | 316 ++ src/kernel/qsound.h | 125 + src/kernel/qsound_x11.cpp | 283 ++ src/kernel/qstyle.cpp | 1896 ++++++++ src/kernel/qstyle.h | 758 ++++ src/kernel/qstylesheet.cpp | 1623 +++++++ src/kernel/qstylesheet.h | 253 ++ src/kernel/qt.h | 375 ++ src/kernel/qt_compat.pri | 27 + src/kernel/qt_gfx.pri | 155 + src/kernel/qt_kernel.pri | 267 ++ src/kernel/qt_pch.h | 57 + src/kernel/qt_x11.pri | 23 + src/kernel/qt_x11_p.h | 280 ++ src/kernel/qtaddons_x11.cpp | 144 + src/kernel/qtextengine.cpp | 1180 +++++ src/kernel/qtextengine_p.h | 377 ++ src/kernel/qtextengine_unix.cpp | 127 + src/kernel/qtextlayout.cpp | 643 +++ src/kernel/qtextlayout_p.h | 184 + src/kernel/qthread.cpp | 241 + src/kernel/qthread.h | 130 + src/kernel/qthread_unix.cpp | 474 ++ src/kernel/qtimer.cpp | 338 ++ src/kernel/qtimer.h | 91 + src/kernel/qtkdeintegration_x11.cpp | 245 + src/kernel/qtkdeintegration_x11_p.h | 62 + src/kernel/qtranslator.cpp | 1478 ++++++ src/kernel/qtranslator.h | 167 + src/kernel/qucomextra.cpp | 177 + src/kernel/qucomextra_p.h | 111 + src/kernel/qurl.cpp | 1345 ++++++ src/kernel/qurl.h | 130 + src/kernel/qurlinfo.cpp | 761 ++++ src/kernel/qurlinfo.h | 144 + src/kernel/qurloperator.cpp | 1226 +++++ src/kernel/qurloperator.h | 129 + src/kernel/qvariant.cpp | 3496 ++++++++++++++ src/kernel/qvariant.h | 396 ++ src/kernel/qvfbhdr.h | 71 + src/kernel/qwidget.cpp | 6081 +++++++++++++++++++++++++ src/kernel/qwidget.h | 1074 +++++ src/kernel/qwidget_p.h | 64 + src/kernel/qwidget_x11.cpp | 3039 +++++++++++++ src/kernel/qwidgetcreate_x11.cpp | 74 + src/kernel/qwidgetintdict.h | 75 + src/kernel/qwidgetlist.h | 67 + src/kernel/qwindow.cpp | 39 + src/kernel/qwindow.h | 46 + src/kernel/qwindowdefs.h | 197 + src/kernel/qwmatrix.cpp | 1036 +++++ src/kernel/qwmatrix.h | 129 + 229 files changed, 167520 insertions(+) create mode 100755 src/kernel/makepsheader.pl create mode 100644 src/kernel/q1xcompatibility.h create mode 100644 src/kernel/qabstractlayout.cpp create mode 100644 src/kernel/qabstractlayout.h create mode 100644 src/kernel/qaccel.cpp create mode 100644 src/kernel/qaccel.h create mode 100644 src/kernel/qaccessible.cpp create mode 100644 src/kernel/qaccessible.h create mode 100644 src/kernel/qapplication.cpp create mode 100644 src/kernel/qapplication.h create mode 100644 src/kernel/qapplication_p.h create mode 100644 src/kernel/qapplication_x11.cpp create mode 100644 src/kernel/qasyncimageio.cpp create mode 100644 src/kernel/qasyncimageio.h create mode 100644 src/kernel/qasyncio.cpp create mode 100644 src/kernel/qasyncio.h create mode 100644 src/kernel/qbitmap.cpp create mode 100644 src/kernel/qbitmap.h create mode 100644 src/kernel/qbrush.h create mode 100644 src/kernel/qclipboard.cpp create mode 100644 src/kernel/qclipboard.h create mode 100644 src/kernel/qclipboard_x11.cpp create mode 100644 src/kernel/qcolor.cpp create mode 100644 src/kernel/qcolor.h create mode 100644 src/kernel/qcolor_p.cpp create mode 100644 src/kernel/qcolor_p.h create mode 100644 src/kernel/qcolor_x11.cpp create mode 100644 src/kernel/qconnection.cpp create mode 100644 src/kernel/qconnection.h create mode 100644 src/kernel/qcursor.cpp create mode 100644 src/kernel/qcursor.h create mode 100644 src/kernel/qcursor_x11.cpp create mode 100644 src/kernel/qdesktopwidget.h create mode 100644 src/kernel/qdesktopwidget_x11.cpp create mode 100644 src/kernel/qdnd_x11.cpp create mode 100644 src/kernel/qdragobject.cpp create mode 100644 src/kernel/qdragobject.h create mode 100644 src/kernel/qdrawutil.cpp create mode 100644 src/kernel/qdrawutil.h create mode 100644 src/kernel/qdropsite.cpp create mode 100644 src/kernel/qdropsite.h create mode 100644 src/kernel/qevent.cpp create mode 100644 src/kernel/qevent.h create mode 100644 src/kernel/qeventloop.cpp create mode 100644 src/kernel/qeventloop.h create mode 100644 src/kernel/qeventloop_p.h create mode 100644 src/kernel/qeventloop_unix.cpp create mode 100644 src/kernel/qeventloop_x11.cpp create mode 100644 src/kernel/qfocusdata.cpp create mode 100644 src/kernel/qfocusdata.h create mode 100644 src/kernel/qfont.cpp create mode 100644 src/kernel/qfont.h create mode 100644 src/kernel/qfont_x11.cpp create mode 100644 src/kernel/qfontdata_p.h create mode 100644 src/kernel/qfontdatabase.cpp create mode 100644 src/kernel/qfontdatabase.h create mode 100644 src/kernel/qfontdatabase_x11.cpp create mode 100644 src/kernel/qfontengine_p.h create mode 100644 src/kernel/qfontengine_x11.cpp create mode 100644 src/kernel/qfontinfo.h create mode 100644 src/kernel/qfontmetrics.h create mode 100644 src/kernel/qgif.h create mode 100644 src/kernel/qgplugin.cpp create mode 100644 src/kernel/qgplugin.h create mode 100644 src/kernel/qguardedptr.cpp create mode 100644 src/kernel/qguardedptr.h create mode 100644 src/kernel/qiconset.cpp create mode 100644 src/kernel/qiconset.h create mode 100644 src/kernel/qimage.cpp create mode 100644 src/kernel/qimage.h create mode 100644 src/kernel/qimageformatinterface_p.h create mode 100644 src/kernel/qimageformatplugin.cpp create mode 100644 src/kernel/qimageformatplugin.h create mode 100644 src/kernel/qinputcontext.cpp create mode 100644 src/kernel/qinputcontext.h create mode 100644 src/kernel/qinputcontext_p.h create mode 100644 src/kernel/qinputcontext_x11.cpp create mode 100644 src/kernel/qinternal.cpp create mode 100644 src/kernel/qinternal_p.h create mode 100644 src/kernel/qjpegio.cpp create mode 100644 src/kernel/qjpegio.h create mode 100644 src/kernel/qkeycode.h create mode 100644 src/kernel/qkeysequence.cpp create mode 100644 src/kernel/qkeysequence.h create mode 100644 src/kernel/qlayout.cpp create mode 100644 src/kernel/qlayout.h create mode 100644 src/kernel/qlayoutengine.cpp create mode 100644 src/kernel/qlayoutengine_p.h create mode 100644 src/kernel/qlocalfs.cpp create mode 100644 src/kernel/qlocalfs.h create mode 100644 src/kernel/qlock.cpp create mode 100644 src/kernel/qlock_p.h create mode 100644 src/kernel/qmetaobject.cpp create mode 100644 src/kernel/qmetaobject.h create mode 100644 src/kernel/qmime.cpp create mode 100644 src/kernel/qmime.h create mode 100644 src/kernel/qmngio.cpp create mode 100644 src/kernel/qmngio.h create mode 100644 src/kernel/qmotifdnd_x11.cpp create mode 100644 src/kernel/qmovie.cpp create mode 100644 src/kernel/qmovie.h create mode 100644 src/kernel/qnamespace.h create mode 100644 src/kernel/qnetworkprotocol.cpp create mode 100644 src/kernel/qnetworkprotocol.h create mode 100644 src/kernel/qobject.cpp create mode 100644 src/kernel/qobject.h create mode 100644 src/kernel/qobjectcleanuphandler.cpp create mode 100644 src/kernel/qobjectcleanuphandler.h create mode 100644 src/kernel/qobjectdefs.h create mode 100644 src/kernel/qobjectdict.h create mode 100644 src/kernel/qobjectlist.h create mode 100644 src/kernel/qpaintdevice.h create mode 100644 src/kernel/qpaintdevice_x11.cpp create mode 100644 src/kernel/qpaintdevicedefs.h create mode 100644 src/kernel/qpaintdevicemetrics.cpp create mode 100644 src/kernel/qpaintdevicemetrics.h create mode 100644 src/kernel/qpainter.cpp create mode 100644 src/kernel/qpainter.h create mode 100644 src/kernel/qpainter_p.h create mode 100644 src/kernel/qpainter_x11.cpp create mode 100644 src/kernel/qpalette.cpp create mode 100644 src/kernel/qpalette.h create mode 100644 src/kernel/qpen.h create mode 100644 src/kernel/qpicture.cpp create mode 100644 src/kernel/qpicture.h create mode 100644 src/kernel/qpixmap.cpp create mode 100644 src/kernel/qpixmap.h create mode 100644 src/kernel/qpixmap_x11.cpp create mode 100644 src/kernel/qpixmapcache.cpp create mode 100644 src/kernel/qpixmapcache.h create mode 100644 src/kernel/qpngio.cpp create mode 100644 src/kernel/qpngio.h create mode 100644 src/kernel/qpoint.cpp create mode 100644 src/kernel/qpoint.h create mode 100644 src/kernel/qpointarray.cpp create mode 100644 src/kernel/qpointarray.h create mode 100644 src/kernel/qpolygonscanner.cpp create mode 100644 src/kernel/qpolygonscanner.h create mode 100644 src/kernel/qprinter.cpp create mode 100644 src/kernel/qprinter.h create mode 100644 src/kernel/qprinter_p.h create mode 100644 src/kernel/qprinter_unix.cpp create mode 100644 src/kernel/qprocess.cpp create mode 100644 src/kernel/qprocess.h create mode 100644 src/kernel/qprocess_unix.cpp create mode 100644 src/kernel/qpsprinter.cpp create mode 100644 src/kernel/qpsprinter.ps create mode 100644 src/kernel/qpsprinter_p.h create mode 100644 src/kernel/qrect.cpp create mode 100644 src/kernel/qrect.h create mode 100644 src/kernel/qregion.cpp create mode 100644 src/kernel/qregion.h create mode 100644 src/kernel/qregion_x11.cpp create mode 100644 src/kernel/qrichtext.cpp create mode 100644 src/kernel/qrichtext_p.cpp create mode 100644 src/kernel/qrichtext_p.h create mode 100644 src/kernel/qscriptengine.cpp create mode 100644 src/kernel/qscriptengine_p.h create mode 100644 src/kernel/qscriptengine_x11.cpp create mode 100644 src/kernel/qsession.h create mode 100644 src/kernel/qsessionmanager.h create mode 100644 src/kernel/qsharedmemory_p.cpp create mode 100644 src/kernel/qsharedmemory_p.h create mode 100644 src/kernel/qsignal.cpp create mode 100644 src/kernel/qsignal.h create mode 100644 src/kernel/qsignalmapper.cpp create mode 100644 src/kernel/qsignalmapper.h create mode 100644 src/kernel/qsignalslotimp.h create mode 100644 src/kernel/qsimplerichtext.cpp create mode 100644 src/kernel/qsimplerichtext.h create mode 100644 src/kernel/qsize.cpp create mode 100644 src/kernel/qsize.h create mode 100644 src/kernel/qsizegrip.cpp create mode 100644 src/kernel/qsizegrip.h create mode 100644 src/kernel/qsizepolicy.h create mode 100644 src/kernel/qsocketnotifier.cpp create mode 100644 src/kernel/qsocketnotifier.h create mode 100644 src/kernel/qsound.cpp create mode 100644 src/kernel/qsound.h create mode 100644 src/kernel/qsound_x11.cpp create mode 100644 src/kernel/qstyle.cpp create mode 100644 src/kernel/qstyle.h create mode 100644 src/kernel/qstylesheet.cpp create mode 100644 src/kernel/qstylesheet.h create mode 100644 src/kernel/qt.h create mode 100644 src/kernel/qt_compat.pri create mode 100644 src/kernel/qt_gfx.pri create mode 100644 src/kernel/qt_kernel.pri create mode 100644 src/kernel/qt_pch.h create mode 100644 src/kernel/qt_x11.pri create mode 100644 src/kernel/qt_x11_p.h create mode 100644 src/kernel/qtaddons_x11.cpp create mode 100644 src/kernel/qtextengine.cpp create mode 100644 src/kernel/qtextengine_p.h create mode 100644 src/kernel/qtextengine_unix.cpp create mode 100644 src/kernel/qtextlayout.cpp create mode 100644 src/kernel/qtextlayout_p.h create mode 100644 src/kernel/qthread.cpp create mode 100644 src/kernel/qthread.h create mode 100644 src/kernel/qthread_unix.cpp create mode 100644 src/kernel/qtimer.cpp create mode 100644 src/kernel/qtimer.h create mode 100644 src/kernel/qtkdeintegration_x11.cpp create mode 100644 src/kernel/qtkdeintegration_x11_p.h create mode 100644 src/kernel/qtranslator.cpp create mode 100644 src/kernel/qtranslator.h create mode 100644 src/kernel/qucomextra.cpp create mode 100644 src/kernel/qucomextra_p.h create mode 100644 src/kernel/qurl.cpp create mode 100644 src/kernel/qurl.h create mode 100644 src/kernel/qurlinfo.cpp create mode 100644 src/kernel/qurlinfo.h create mode 100644 src/kernel/qurloperator.cpp create mode 100644 src/kernel/qurloperator.h create mode 100644 src/kernel/qvariant.cpp create mode 100644 src/kernel/qvariant.h create mode 100644 src/kernel/qvfbhdr.h create mode 100644 src/kernel/qwidget.cpp create mode 100644 src/kernel/qwidget.h create mode 100644 src/kernel/qwidget_p.h create mode 100644 src/kernel/qwidget_x11.cpp create mode 100644 src/kernel/qwidgetcreate_x11.cpp create mode 100644 src/kernel/qwidgetintdict.h create mode 100644 src/kernel/qwidgetlist.h create mode 100644 src/kernel/qwindow.cpp create mode 100644 src/kernel/qwindow.h create mode 100644 src/kernel/qwindowdefs.h create mode 100644 src/kernel/qwmatrix.cpp create mode 100644 src/kernel/qwmatrix.h (limited to 'src/kernel') diff --git a/src/kernel/makepsheader.pl b/src/kernel/makepsheader.pl new file mode 100755 index 000000000..b842e1465 --- /dev/null +++ b/src/kernel/makepsheader.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl + +open(INPUT, 'qpsprinter.ps') + or die "Can't open qpsprinter.ps"; + +$dontcompress = 1; +while() { + $line = $_; + chomp $line; + if ( /ENDUNCOMPRESS/ ) { + $dontcompress = 0; + } + $line =~ s/%.*$//; + $line = $line; + if ( $dontcompress eq 1 ) { + push(@uncompressed, $line); + } else { + push(@lines, $line); + } +# print "$line\n"; +} + +$uc = join(" ", @uncompressed); +$uc =~ s,\t+, ,g; +$uc=~ s, +, ,g; + +$h = join(" ", @lines); +$h =~ s,\t+, ,g; +$h =~ s, +, ,g; +$h = $h.' '; + +# now compress as much as possible +$h =~ s/ def / d /g; +$h =~ s/ bind def / D /g; +$h =~ s/ dup dup / d2 /g; +$h =~ s/ exch d / ED /g; +$h =~ s/ lineto / LT /g; +$h =~ s/ moveto / MT /g; +$h =~ s/ stroke / S /g; +$h =~ s/ setfont / F /g; +$h =~ s/ setlinewidth / SW /g; +$h =~ s/ closepath / CP /g; +$h =~ s/ rlineto / RL /g; +$h =~ s/ newpath / NP /g; +$h =~ s/ currentmatrix / CM /g; +$h =~ s/ setmatrix / SM /g; +$h =~ s/ translate / TR /g; +$h =~ s/ setdash / SD /g; +$h =~ s/ aload pop setrgbcolor / SC /g; +$h =~ s/ currentfile read pop / CR /g; +$h =~ s/ index / i /g; +$h =~ s/ bitshift / bs /g; +$h =~ s/ setcolorspace / scs /g; +$h =~ s/ dict dup begin / DB /g; +$h =~ s/ end d / DE /g; +$h =~ s/ ifelse / ie /g; +$h =~ s/ astore pop / sp /g; + +# add the uncompressed part of the header before +$h = $uc.' '.$h; + + + +#print $h; + +# wordwrap at col 76 +@head = split(' ', $h); +$line = shift @head; +while( @head ) { + $token = shift @head; + chomp $token; +# print "\nl=$l, len=$len, token=$token."; + $newline = $line.' '.$token; + $newline =~ s, /,/,g; + $newline =~ s, \{,\{,g; + $newline =~ s, \},\},g; + $newline =~ s, \[,\[,g; + $newline =~ s, \],\],g; + $newline =~ s,\{ ,\{,g; + $newline =~ s,\} ,\},g; + $newline =~ s,\[ ,\[,g; + $newline =~ s,\] ,\],g; + if ( length( $newline ) > 76 ) { +# print "\nline=$line\n"; + $header = $header."\n\"".$line."\\n\""; + $newline = $token; + } + $line = $newline; +} +$header = $header."\n\"".$line."\\n\""; + + +print $header; diff --git a/src/kernel/q1xcompatibility.h b/src/kernel/q1xcompatibility.h new file mode 100644 index 000000000..c3d6ce25b --- /dev/null +++ b/src/kernel/q1xcompatibility.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Various macros etc. to ease porting from TQt 1.x to 2.0. THIS FILE +** WILL CHANGE OR DISAPPEAR IN THE NEXT VERSION OF TQt. +** +** Created : 980824 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef Q1XCOMPATIBILITY_H +#define Q1XCOMPATIBILITY_H + +#error "Compatibility with TQt 1.x is no longer guaranteed. Please" +#error "update your code (for example using qt20fix script). We" +#error "apologize for any inconvenience." + +#endif // Q1XCOMPATIBILITY_H diff --git a/src/kernel/qabstractlayout.cpp b/src/kernel/qabstractlayout.cpp new file mode 100644 index 000000000..a0b4b8b51 --- /dev/null +++ b/src/kernel/qabstractlayout.cpp @@ -0,0 +1,1945 @@ +/**************************************************************************** +** +** Implementation of the abstract layout base class +** +** Created : 960416 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qlayout.h" + +#ifndef QT_NO_LAYOUT +#include "qapplication.h" +#include "qlayoutengine_p.h" +#include "qmenubar.h" +#include "qtoolbar.h" + +static int menuBarHeightForWidth( TQMenuBar *menubar, int w ) +{ +#ifndef QT_NO_MENUBAR + if ( menubar && !menubar->isHidden() && !menubar->isTopLevel() ) + return menubar->heightForWidth( TQMAX(w, menubar->minimumWidth()) ); + else +#endif + return 0; +} + +/*! + \class TQLayoutItem + \ingroup appearance + \ingroup geomanagement + \brief The TQLayoutItem class provides an abstract item that a + TQLayout manipulates. + + This is used by custom layouts. + + Pure virtual functions are provided to return information about + the layout, including, sizeHint(), minimumSize(), maximumSize() + and expanding(). + + The layout's geometry can be set and retrieved with setGeometry() + and geometry(), and its alignment with setAlignment() and + alignment(). + + isEmpty() returns whether the layout is empty. iterator() returns + an iterator for the layout's children. If the concrete item is a + TQWidget, it can be retrieved using widget(). Similarly for + layout() and spacerItem(). + + \sa TQLayout +*/ + +/*! + \class TQSpacerItem + \ingroup appearance + \ingroup geomanagement + \brief The TQSpacerItem class provides blank space in a layout. + + This class is used by custom layouts. + + \sa TQLayout TQLayout::spacerItem() +*/ + +/*! + \class TQWidgetItem + \ingroup appearance + \ingroup geomanagement + \brief The TQWidgetItem class is a layout item that represents a widget. + + This is used by custom layouts. + + \sa TQLayout TQLayout::widget() +*/ + +/*! + \fn TQLayoutItem::TQLayoutItem( int alignment ) + + Constructs a layout item with an \a alignment that is a bitwise OR + of the \l{TQt::AlignmentFlags}. Not all subclasses support + alignment. +*/ + +/*! + \fn int TQLayoutItem::alignment() const + + Returns the alignment of this item. +*/ + +/*! + Sets the alignment of this item to \a a, which is a bitwise OR of + the \l{TQt::AlignmentFlags}. Not all subclasses support alignment. +*/ +void TQLayoutItem::setAlignment( int a ) +{ + align = a; +} + +/*! + \fn TQSize TQLayoutItem::maximumSize() const + + Implemented in subclasses to return the maximum size of this item. +*/ + +/*! + \fn TQSize TQLayoutItem::minimumSize() const + + Implemented in subclasses to return the minimum size of this item. +*/ + +/*! + \fn TQSize TQLayoutItem::sizeHint() const + + Implemented in subclasses to return the preferred size of this item. +*/ + +/*! + \fn TQSizePolicy::ExpandData TQLayoutItem::expanding() const + + Implemented in subclasses to return the direction(s) this item + "wants" to expand in (if any). +*/ + +/*! + \fn void TQLayoutItem::setGeometry( const TQRect &r ) + + Implemented in subclasses to set this item's geometry to \a r. +*/ + +/*! + \fn TQRect TQLayoutItem::geometry() const + + Returns the rectangle covered by this layout item. +*/ + +/*! + \fn virtual bool TQLayoutItem::isEmpty() const + + Implemented in subclasses to return whether this item is empty, + i.e. whether it contains any widgets. +*/ + +/*! + \fn TQSpacerItem::TQSpacerItem( int w, int h, TQSizePolicy::SizeType hData, TQSizePolicy::SizeType vData ) + + Constructs a spacer item with preferred width \a w, preferred + height \a h, horizontal size policy \a hData and vertical size + policy \a vData. + + The default values provide a gap that is able to stretch if + nothing else wants the space. +*/ + +/*! + Changes this spacer item to have preferred width \a w, preferred + height \a h, horizontal size policy \a hData and vertical size + policy \a vData. + + The default values provide a gap that is able to stretch if + nothing else wants the space. +*/ +void TQSpacerItem::changeSize( int w, int h, TQSizePolicy::SizeType hData, + TQSizePolicy::SizeType vData ) +{ + width = w; + height = h; + sizeP = TQSizePolicy( hData, vData ); +} + +/*! + \fn TQWidgetItem::TQWidgetItem (TQWidget * w) + + Creates an item containing widget \a w. +*/ + +/*! + Destroys the TQLayoutItem. +*/ +TQLayoutItem::~TQLayoutItem() +{ +} + +/*! + Invalidates any cached information in this layout item. +*/ +void TQLayoutItem::invalidate() +{ +} + +/*! + If this item is a TQLayout, it is returned as a TQLayout; otherwise + 0 is returned. This function provides type-safe casting. +*/ +TQLayout * TQLayoutItem::layout() +{ + return 0; +} + +/*! + If this item is a TQSpacerItem, it is returned as a TQSpacerItem; + otherwise 0 is returned. This function provides type-safe casting. +*/ +TQSpacerItem * TQLayoutItem::spacerItem() +{ + return 0; +} + +/*! + \reimp +*/ +TQLayout * TQLayout::layout() +{ + return this; +} + +/*! + \reimp +*/ +TQSpacerItem * TQSpacerItem::spacerItem() +{ + return this; +} + +/*! + If this item is a TQWidget, it is returned as a TQWidget; otherwise + 0 is returned. This function provides type-safe casting. +*/ +TQWidget * TQLayoutItem::widget() +{ + return 0; +} + +/*! + Returns the widget managed by this item. +*/ +TQWidget * TQWidgetItem::widget() +{ + return wid; +} + +/*! + Returns TRUE if this layout's preferred height depends on its + width; otherwise returns FALSE. The default implementation returns + FALSE. + + Reimplement this function in layout managers that support height + for width. + + \sa heightForWidth(), TQWidget::heightForWidth() +*/ +bool TQLayoutItem::hasHeightForWidth() const +{ + return FALSE; +} + +/*! + Returns an iterator over this item's TQLayoutItem children. The + default implementation returns an empty iterator. + + Reimplement this function in subclasses that can have children. +*/ +TQLayoutIterator TQLayoutItem::iterator() +{ + return TQLayoutIterator( 0 ); +} + +/*! + Returns the preferred height for this layout item, given the width + \a w. + + The default implementation returns -1, indicating that the + preferred height is independent of the width of the item. Using + the function hasHeightForWidth() will typically be much faster + than calling this function and testing for -1. + + Reimplement this function in layout managers that support height + for width. A typical implementation will look like this: + \code + int MyLayout::heightForWidth( int w ) const + { + if ( cache_dirty || cached_width != w ) { + // not all C++ compilers support "mutable" + MyLayout *that = (MyLayout*)this; + int h = calculateHeightForWidth( w ); + that->cached_hfw = h; + return h; + } + return cached_hfw; + } + \endcode + + Caching is strongly recommended; without it layout will take + exponential time. + + \sa hasHeightForWidth() +*/ +int TQLayoutItem::heightForWidth( int /* w */ ) const +{ + return -1; +} + +/*! + Stores the spacer item's rect \a r so that it can be returned by + geometry(). +*/ +void TQSpacerItem::setGeometry( const TQRect &r ) +{ + rect = r; +} + +/*! + Sets the geometry of this item's widget to be contained within + rect \a r, taking alignment and maximum size into account. +*/ +void TQWidgetItem::setGeometry( const TQRect &r ) +{ + TQSize s = r.size().boundedTo( qSmartMaxSize( this ) ); + int x = r.x(); + int y = r.y(); + if ( align & (TQt::AlignHorizontal_Mask | TQt::AlignVertical_Mask) ) { + TQSize pref = wid->sizeHint().expandedTo( wid->minimumSize() ); //### + if ( align & TQt::AlignHorizontal_Mask ) + s.setWidth( TQMIN( s.width(), pref.width() ) ); + if ( align & TQt::AlignVertical_Mask ) { + if ( hasHeightForWidth() ) + s.setHeight( TQMIN( s.height(), heightForWidth(s.width()) ) ); + else + s.setHeight( TQMIN( s.height(), pref.height() ) ); + } + } + int alignHoriz = TQApplication::horizontalAlignment( align ); + if ( alignHoriz & TQt::AlignRight ) + x = x + ( r.width() - s.width() ); + else if ( !(alignHoriz & TQt::AlignLeft) ) + x = x + ( r.width() - s.width() ) / 2; + + if ( align & TQt::AlignBottom ) + y = y + ( r.height() - s.height() ); + else if ( !(align & TQt::AlignTop) ) + y = y + ( r.height() - s.height() ) / 2; + + if ( !isEmpty() ) + wid->setGeometry( x, y, s.width(), s.height() ); +} + +/*! + \reimp +*/ +TQRect TQSpacerItem::geometry() const +{ + return rect; +} + +/*! + \reimp +*/ +TQRect TQWidgetItem::geometry() const +{ + return wid->geometry(); +} + +/*! + \reimp +*/ +TQRect TQLayout::geometry() const +{ + return rect; +} + +/*! + \reimp +*/ +bool TQWidgetItem::hasHeightForWidth() const +{ + if ( isEmpty() ) + return FALSE; + if ( wid->layout() ) + return wid->layout()->hasHeightForWidth(); + return wid->sizePolicy().hasHeightForWidth(); +} + +/*! + \reimp +*/ +int TQWidgetItem::heightForWidth( int w ) const +{ + if ( isEmpty() ) + return -1; + int hfw; + if ( wid->layout() ) + hfw = wid->layout()->totalHeightForWidth( w ); + else + hfw = wid->heightForWidth( w ); + + if ( hfw > wid->maximumHeight() ) + hfw = wid->maximumHeight(); + if ( hfw < wid->minimumHeight() ) + hfw = wid->minimumHeight(); + if ( hfw < 1 ) + hfw = 1; + return hfw; +} + +/*! + Returns the direction in which this spacer item will expand. + + \sa TQSizePolicy::ExpandData +*/ +TQSizePolicy::ExpandData TQSpacerItem::expanding() const +{ + return sizeP.expanding(); +} + +/*! + Returns whether this item's widget can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions and \c NoDirection means that + it doesn't want to grow at all. +*/ +TQSizePolicy::ExpandData TQWidgetItem::expanding() const +{ + if ( isEmpty() ) + return TQSizePolicy::NoDirection; + + int e = wid->sizePolicy().expanding(); + /* + If the layout is expanding, we make the widget expanding, even if + its own size policy isn't expanding. This behavior should be + reconsidered in TQt 4.0. (###) + */ + if ( wid->layout() ) { + if ( wid->sizePolicy().mayGrowHorizontally() + && (wid->layout()->expanding() & TQSizePolicy::Horizontally) ) + e |= TQSizePolicy::Horizontally; + if ( wid->sizePolicy().mayGrowVertically() + && (wid->layout()->expanding() & TQSizePolicy::Vertically) ) + e |= TQSizePolicy::Vertically; + } + + if ( align & TQt::AlignHorizontal_Mask ) + e &= ~TQSizePolicy::Horizontally; + if ( align & TQt::AlignVertical_Mask) + e &= ~TQSizePolicy::Vertically; + return (TQSizePolicy::ExpandData)e; +} + +/*! + Returns the minimum size of this spacer item. +*/ +TQSize TQSpacerItem::minimumSize() const +{ + return TQSize( sizeP.mayShrinkHorizontally() ? 0 : width, + sizeP.mayShrinkVertically() ? 0 : height ); +} + +/*! + Returns the minimum size of this item. +*/ +TQSize TQWidgetItem::minimumSize() const +{ + if ( isEmpty() ) + return TQSize( 0, 0 ); + return qSmartMinSize( this ); +} + +/*! + Returns the maximum size of this spacer item. +*/ +TQSize TQSpacerItem::maximumSize() const +{ + return TQSize( sizeP.mayGrowHorizontally() ? TQLAYOUTSIZE_MAX : width, + sizeP.mayGrowVertically() ? TQLAYOUTSIZE_MAX : height ); +} + +/*! + Returns the maximum size of this item. +*/ +TQSize TQWidgetItem::maximumSize() const +{ + if ( isEmpty() ) { + return TQSize( 0, 0 ); + } else { + return qSmartMaxSize( this, align ); + } +} + +/*! + Returns the preferred size of this spacer item. +*/ +TQSize TQSpacerItem::sizeHint() const +{ + return TQSize( width, height ); +} + +/*! + Returns the preferred size of this item. +*/ +TQSize TQWidgetItem::sizeHint() const +{ + TQSize s; + if ( isEmpty() ) { + s = TQSize( 0, 0 ); + } else { + s = wid->sizeHint(); + if ( wid->sizePolicy().horData() == TQSizePolicy::Ignored ) + s.setWidth( 1 ); + if ( wid->sizePolicy().verData() == TQSizePolicy::Ignored ) + s.setHeight( 1 ); + s = s.boundedTo( wid->maximumSize() ) + .expandedTo( wid->minimumSize() ).expandedTo( TQSize(1, 1) ); + } + return s; +} + +/*! + Returns TRUE because a spacer item never contains widgets. +*/ +bool TQSpacerItem::isEmpty() const +{ + return TRUE; +} + +/*! + Returns TRUE if the widget has been hidden; otherwise returns + FALSE. +*/ +bool TQWidgetItem::isEmpty() const +{ + return wid->isHidden() || wid->isTopLevel(); +} + +/*! + \class TQLayout + \brief The TQLayout class is the base class of geometry managers. + + \ingroup appearance + \ingroup geomanagement + + This is an abstract base class inherited by the concrete classes, + TQBoxLayout and TQGridLayout. + + For users of TQLayout subclasses or of TQMainWindow there is seldom + any need to use the basic functions provided by TQLayout, such as + \l setResizeMode() or setMenuBar(). See the \link layout.html layout + overview page \endlink for more information. + + To make your own layout manager, subclass TQGLayoutIterator and + implement the functions addItem(), sizeHint(), setGeometry(), and + iterator(). You should also implement minimumSize() to ensure your + layout isn't resized to zero size if there is too little space. To + support children whose heights depend on their widths, implement + hasHeightForWidth() and heightForWidth(). See the \link + customlayout.html custom layout page \endlink for an in-depth + description. + + Geometry management stops when the layout manager is deleted. +*/ + +/*! + Constructs a new top-level TQLayout called \a name, with main + widget \a parent. \a parent may not be 0. + + The \a margin is the number of pixels between the edge of the + widget and the managed children. The \a spacing sets the value of + spacing(), which gives the spacing between the managed widgets. If + \a spacing is -1 (the default), spacing is set to the value of \a + margin. + + There can be only one top-level layout for a widget. It is + returned by TQWidget::layout() +*/ +TQLayout::TQLayout( TQWidget *parent, int margin, int spacing, const char *name ) + : TQObject( parent, name ) +{ + init(); + if ( parent ) { + if ( parent->layout() ) { + qWarning( "TQLayout \"%s\" added to %s \"%s\", which already has a" + " layout", TQObject::name(), parent->className(), + parent->name() ); + parent->removeChild( this ); + } else { + topLevel = TRUE; + parent->installEventFilter( this ); + setWidgetLayout( parent, this ); + } + } + outsideBorder = margin; + if ( spacing < 0 ) + insideSpacing = margin; + else + insideSpacing = spacing; +} + +void TQLayout::init() +{ + insideSpacing = 0; + outsideBorder = 0; + topLevel = FALSE; + enabled = TRUE; + autoNewChild = FALSE; + frozen = FALSE; + activated = FALSE; + marginImpl = FALSE; + autoMinimum = FALSE; + autoResizeMode = TRUE; + extraData = 0; +#ifndef QT_NO_MENUBAR + menubar = 0; +#endif +} + +/*! + Constructs a new child TQLayout called \a name, and places it + inside \a parentLayout by using the default placement defined by + addItem(). + + If \a spacing is -1, this TQLayout inherits \a parentLayout's + spacing(), otherwise the value of \a spacing is used. +*/ +TQLayout::TQLayout( TQLayout *parentLayout, int spacing, const char *name ) + : TQObject( parentLayout, name ) + +{ + init(); + insideSpacing = spacing < 0 ? parentLayout->insideSpacing : spacing; + parentLayout->addItem( this ); +} + +/*! + Constructs a new child TQLayout called \a name. If \a spacing is + -1, this TQLayout inherits its parent's spacing(); otherwise the + value of \a spacing is used. + + This layout has to be inserted into another layout before geometry + management will work. +*/ +TQLayout::TQLayout( int spacing, const char *name ) + : TQObject( 0, name ) +{ + init(); + insideSpacing = spacing; +} + +/*! + \fn void TQLayout::addItem( TQLayoutItem *item ) + + Implemented in subclasses to add an \a item. How it is added is + specific to each subclass. + + The ownership of \a item is transferred to the layout, and it's + the layout's responsibility to delete it. +*/ + +/*! + \fn TQLayoutIterator TQLayout::iterator() + + Implemented in subclasses to return an iterator that iterates over + this layout's children. + + A typical implementation will be: + \code + TQLayoutIterator MyLayout::iterator() + { + TQGLayoutIterator *i = new MyLayoutIterator( internal_data ); + return TQLayoutIterator( i ); + } + \endcode + where MyLayoutIterator is a subclass of TQGLayoutIterator. +*/ + +/*! + \fn void TQLayout::add( TQWidget *w ) + + Adds widget \a w to this layout in a manner specific to the + layout. This function uses addItem(). +*/ + +/*! + \fn TQMenuBar* TQLayout::menuBar () const + + Returns the menu bar set for this layout, or 0 if no menu bar is + set. +*/ + +/*! + \fn bool TQLayout::isTopLevel () const + + Returns TRUE if this layout is a top-level layout, i.e. not a + child of another layout; otherwise returns FALSE. +*/ + +/*! + \property TQLayout::margin + \brief the width of the outside border of the layout + + For some layout classes this property has an effect only on + top-level layouts; TQBoxLayout and TQGridLayout support margins for + child layouts. The default value is 0. + + \sa spacing +*/ + +/*! + \property TQLayout::spacing + \brief the spacing between widgets inside the layout + + The default value is -1, which signifies that the layout's spacing + should not override the widget's spacing. + + \sa margin +*/ +void TQLayout::setMargin( int margin ) +{ + outsideBorder = margin; + invalidate(); + if ( mainWidget() ) { + TQEvent *lh = new TQEvent( TQEvent::LayoutHint ); + TQApplication::postEvent( mainWidget(), lh ); + } +} + +void TQLayout::setSpacing( int spacing ) +{ + insideSpacing = spacing; + if ( spacing >= 0 ) + propagateSpacing( this ); + invalidate(); + if ( mainWidget() ) { + TQEvent *lh = new TQEvent( TQEvent::LayoutHint ); + TQApplication::postEvent( mainWidget(), lh ); + } +} + +/*! + Returns the main widget (parent widget) of this layout, or 0 if + this layout is a sub-layout that is not yet inserted. +*/ +TQWidget *TQLayout::mainWidget() +{ + if ( !topLevel ) { + if ( parent() ) { + TQLayout *parentLayout = ::qt_cast(parent()); + Q_ASSERT(parentLayout); + return parentLayout->mainWidget(); + } else { + return 0; + } + } else { + Q_ASSERT(parent() && parent()->isWidgetType()); + return (TQWidget*)parent(); + } +} + +/*! + Returns TRUE if this layout is empty. The default implementation + returns FALSE. +*/ +bool TQLayout::isEmpty() const +{ + return FALSE; //### should check +} + +/*! + Sets widget \a w's layout to layout \a l. +*/ +void TQLayout::setWidgetLayout( TQWidget *w, TQLayout *l ) +{ + w->setLayout( l ); +} + +/*! + This function is reimplemented in subclasses to perform layout. + + The default implementation maintains the geometry() information + given by rect \a r. Reimplementors must call this function. +*/ +void TQLayout::setGeometry( const TQRect &r ) +{ + rect = r; +} + +/*! + Invalidates cached information. Reimplementations must call this. +*/ +void TQLayout::invalidate() +{ + rect = TQRect(); +} + +static bool removeWidgetRecursively( TQLayoutItem *lay, TQWidget *w ) +{ + bool didSomething = FALSE; + TQLayoutIterator it = lay->iterator(); + TQLayoutItem *child; + while ( (child = it.current()) != 0 ) { + if ( child->widget() == w ) { + it.deleteCurrent(); + lay->invalidate(); // maybe redundant + didSomething = TRUE; + } else if ( removeWidgetRecursively(child, w) ) { + lay->invalidate(); // maybe redundant + didSomething = TRUE; + } else { + ++it; + } + } + return didSomething; +} + +/*! + \reimp + Performs child widget layout when the parent widget is resized. + Also handles removal of widgets and child layouts. \a e is the + event the occurred on object \a o. +*/ +bool TQLayout::eventFilter( TQObject *o, TQEvent *e ) +{ + if ( !enabled ) + return FALSE; + + if ( !o->isWidgetType() ) + return FALSE; + + switch ( e->type() ) { + case TQEvent::Resize: + if ( activated ) { + TQResizeEvent *r = (TQResizeEvent *)e; + int mbh = 0; +#ifndef QT_NO_MENUBAR + mbh = menuBarHeightForWidth( menubar, r->size().width() ); +#endif + int b = marginImpl ? 0 : outsideBorder; + setGeometry( TQRect( b, mbh + b, r->size().width() - 2 * b, + r->size().height() - mbh - 2 * b ) ); + } else { + activate(); + } + break; + case TQEvent::ChildRemoved: + { + TQChildEvent *c = (TQChildEvent *)e; + if ( c->child()->isWidgetType() ) { + TQWidget *w = (TQWidget *)c->child(); +#ifndef QT_NO_MENUBAR + if ( w == menubar ) + menubar = 0; +#endif + if ( removeWidgetRecursively( this, w ) ) { + TQEvent *lh = new TQEvent( TQEvent::LayoutHint ); + TQApplication::postEvent( o, lh ); + } + } + } + break; + case TQEvent::ChildInserted: + if ( topLevel && autoNewChild ) { + TQChildEvent *c = (TQChildEvent *)e; + if ( c->child()->isWidgetType() ) { + TQWidget *w = (TQWidget *)c->child(); + if ( !w->isTopLevel() ) { +#if !defined(QT_NO_MENUBAR) && !defined(QT_NO_TOOLBAR) + if ( ::qt_cast(w) && !::qt_cast(w->parentWidget()) ) + menubar = (TQMenuBar *)w; + else +#endif + addItem( new TQWidgetItem( w ) ); + TQEvent *lh = new TQEvent( TQEvent::LayoutHint ); + TQApplication::postEvent( o, lh ); + } + } + } + break; + case TQEvent::LayoutHint: + activate(); + break; + default: + break; + } + return TQObject::eventFilter( o, e ); +} + +/*! + \reimp +*/ +void TQLayout::childEvent( TQChildEvent *e ) +{ + if ( !enabled ) + return; + + if ( e->type() == TQEvent::ChildRemoved ) { + TQChildEvent *c = (TQChildEvent*)e; + TQLayoutIterator it = iterator(); + TQLayoutItem *item; + while ( (item = it.current() ) ) { + if ( item == (TQLayout*)c->child() ) { + it.takeCurrent(); + invalidate(); + break; + } else { + ++it; + } + } + } +} + +/*! + \internal + Also takes margin() and menu bar into account. +*/ +int TQLayout::totalHeightForWidth( int w ) const +{ + if ( topLevel ) { + TQWidget *mw = (TQWidget*)parent(); + if ( mw && !mw->testWState(WState_Polished) ) { + mw->polish(); + } + } + int b = ( topLevel && !marginImpl ) ? 2 * outsideBorder : 0; + int h = heightForWidth( w - b ) + b; +#ifndef QT_NO_MENUBAR + h += menuBarHeightForWidth( menubar, w ); +#endif + return h; +} + +/*! + \internal + Also takes margin() and menu bar into account. +*/ +TQSize TQLayout::totalMinimumSize() const +{ + if ( topLevel ) { + TQWidget *mw = (TQWidget*)parent(); + if ( mw && !mw->testWState(WState_Polished) ) + mw->polish(); + } + int b = ( topLevel && !marginImpl ) ? 2 * outsideBorder : 0; + + TQSize s = minimumSize(); + int h = b; +#ifndef QT_NO_MENUBAR + h += menuBarHeightForWidth( menubar, s.width() ); +#endif + return s + TQSize( b, h ); +} + +/*! + \internal + Also takes margin() and menu bar into account. +*/ +TQSize TQLayout::totalSizeHint() const +{ + if ( topLevel ) { + TQWidget *mw = (TQWidget*)parent(); + if ( mw && !mw->testWState(WState_Polished) ) + mw->polish(); + } + int b = ( topLevel && !marginImpl ) ? 2 * outsideBorder : 0; + + TQSize s = sizeHint(); + if ( hasHeightForWidth() ) + s.setHeight( heightForWidth(s.width()) ); + int h = b; +#ifndef QT_NO_MENUBAR + h += menuBarHeightForWidth( menubar, s.width() ); +#endif + return s + TQSize( b, h ); +} + +/*! + \internal + Also takes margin() and menu bar into account. +*/ +TQSize TQLayout::totalMaximumSize() const +{ + if ( topLevel ) { + TQWidget *mw = (TQWidget*)parent(); + if ( mw && !mw->testWState(WState_Polished) ) { + mw->polish(); + } + } + int b = ( topLevel && !marginImpl ) ? 2 * outsideBorder : 0; + + TQSize s = maximumSize(); + int h = b; +#ifndef QT_NO_MENUBAR + h += menuBarHeightForWidth( menubar, s.width() ); +#endif + + if ( isTopLevel() ) + s = TQSize( TQMIN( s.width() + b, TQLAYOUTSIZE_MAX ), + TQMIN( s.height() + h, TQLAYOUTSIZE_MAX ) ); + return s; +} + +/*! + \internal + Destroys the layout, deleting all child layouts. + Geometry management stops when a top-level layout is deleted. + + The layout classes will probably be fatally confused if you delete + a sublayout. +*/ +TQLayout::~TQLayout() +{ + /* + This function may be called during the TQObject destructor, + when the parent no longer is a TQWidget. + */ + if ( isTopLevel() && parent() && parent()->isWidgetType() && + ((TQWidget*)parent())->layout() == this ) + setWidgetLayout( (TQWidget*)parent(), 0 ); +} + +/*! + Removes and deletes all items in this layout. +*/ +void TQLayout::deleteAllItems() +{ + TQLayoutIterator it = iterator(); + TQLayoutItem *l; + while ( (l = it.takeCurrent()) ) + delete l; +} + +/*! + This function is called from addLayout() functions in subclasses + to add layout \a l as a sub-layout. +*/ +void TQLayout::addChildLayout( TQLayout *l ) +{ + if ( l->parent() ) { +#if defined(QT_CHECK_NULL) + qWarning( "TQLayout::addChildLayout: layout already has a parent" ); +#endif + return; + } + insertChild( l ); + if ( l->insideSpacing < 0 ) { + l->insideSpacing = insideSpacing; + propagateSpacing( l ); + } +} + +/*! \fn int TQLayout::defaultBorder() const + + \internal +*/ + +/*! \fn void TQLayout::freeze() + + \internal +*/ + +/*! + \internal + Fixes the size of the main widget and distributes the available + space to the child widgets. For widgets which should not be + resizable, but where a TQLayout subclass is used to set up the initial + geometry. + + As a special case, freeze(0, 0) is equivalent to setResizeMode(Fixed). +*/ +void TQLayout::freeze( int w, int h ) +{ + if ( w <= 0 || h <= 0 ) { + setResizeMode( Fixed ); + } else { + setResizeMode( FreeResize ); // layout will not change min/max size + mainWidget()->setFixedSize( w, h ); + } +} + +#ifndef QT_NO_MENUBAR + +/*! + Makes the geometry manager take account of the menu bar \a w. All + child widgets are placed below the bottom edge of the menu bar. + + A menu bar does its own geometry management: never do addWidget() + on a TQMenuBar. +*/ +void TQLayout::setMenuBar( TQMenuBar *w ) +{ + menubar = w; +} + +#endif + +/*! + Returns the minimum size of this layout. This is the smallest size + that the layout can have while still respecting the + specifications. Does not include what's needed by margin() or + menuBar(). + + The default implementation allows unlimited resizing. +*/ +TQSize TQLayout::minimumSize() const +{ + return TQSize( 0, 0 ); +} + +/*! + Returns the maximum size of this layout. This is the largest size + that the layout can have while still respecting the + specifications. Does not include what's needed by margin() or + menuBar(). + + The default implementation allows unlimited resizing. +*/ +TQSize TQLayout::maximumSize() const +{ + return TQSize( TQLAYOUTSIZE_MAX, TQLAYOUTSIZE_MAX ); +} + +/*! + Returns whether this layout can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions. + + The default implementation returns \c BothDirections. +*/ +TQSizePolicy::ExpandData TQLayout::expanding() const +{ + return TQSizePolicy::BothDirections; +} + +static void invalidateRecursive( TQLayoutItem *lay ) +{ + lay->invalidate(); + TQLayoutIterator it = lay->iterator(); + TQLayoutItem *child; + while ( (child = it.current()) != 0 ) { + invalidateRecursive( child ); + ++it; + } +} + +/*! + Redoes the layout for mainWidget(). You should generally not need + to call this because it is automatically called at the most + appropriate times. + + However, if you set up a TQLayout for a visible widget without + resizing that widget, you will need to call this function in order + to lay it out. + + \sa TQWidget::updateGeometry() +*/ +bool TQLayout::activate() +{ + invalidateRecursive( this ); + if ( !topLevel ) + return FALSE; + + TQWidget *mw = mainWidget(); + if (!mw) { +#if defined( QT_CHECK_NULL ) + qWarning( "TQLayout::activate: %s \"%s\" does not have a main widget", + TQObject::className(), TQObject::name() ); +#endif + return FALSE; + } + activated = TRUE; + TQSize s = mw->size(); + TQSize ms; + int mbh = 0; +#ifndef QT_NO_MENUBAR + mbh = menuBarHeightForWidth( menubar, s.width() ); +#endif + int b = marginImpl ? 0 : outsideBorder; + setGeometry(TQRect(b, mbh + b, s.width() - 2 * b, s.height() - mbh - 2 * b)); + if ( frozen ) { + // will trigger resize + mw->setFixedSize( totalSizeHint() ); + } else if ( autoMinimum ) { + ms = totalMinimumSize(); + } else if ( autoResizeMode && topLevel && mw->isTopLevel() ) { + ms = totalMinimumSize(); + if ( hasHeightForWidth() ) { + int h; + int mmbh = menuBarHeightForWidth( menubar, ms.width() ); + // ### 4.0: remove this 'if' when minimumHeightForWidth() is virtual + if ( inherits("TQBoxLayout") ) { + h = ((TQBoxLayout *) this)->minimumHeightForWidth( ms.width() ); + } else if ( inherits("TQGridLayout") ) { + h = ((TQGridLayout *) this)->minimumHeightForWidth( ms.width() ); + } else { + h = heightForWidth( ms.width() ); + } + if ( h + mmbh > ms.height() ) +#if 1 + //old behaviour: + ms = TQSize( 0, 0 ); +#else + //better, but too big a change for a patch release in a stable branch: + ms.setHeight( 0 ); +#endif + } + } + + if (ms.isValid()) + mw->setMinimumSize( ms ); + + // ideally only if sizeHint() or sizePolicy() has changed + mw->updateGeometry(); + return TRUE; +} + +/*! + \class TQSizePolicy + \brief The TQSizePolicy class is a layout attribute describing horizontal + and vertical resizing policy. + + \ingroup appearance + \ingroup geomanagement + + The size policy of a widget is an expression of its willingness to + be resized in various ways. + + Widgets that reimplement TQWidget::sizePolicy() return a TQSizePolicy + that describes the horizontal and vertical resizing policy they + prefer when being laid out. Only \link #interesting one of the + constructors\endlink is of interest in most applications. + + TQSizePolicy contains two independent SizeType objects; one describes + the widgets's horizontal size policy, and the other describes its + vertical size policy. It also contains a flag to indicate whether the + height and width of its preferred size are related. + + The horizontal and vertical \l{SizeType}s are set in the usual constructor + and can be queried using a variety of functions. + + The hasHeightForWidth() flag indicates whether the widget's sizeHint() + is width-dependent (such as a word-wrapping label) or not. + + \sa TQSizePolicy::SizeType +*/ + +/*! + \enum TQSizePolicy::SizeType + + The per-dimension sizing types used when constructing a + TQSizePolicy are: + + \value Fixed The TQWidget::sizeHint() is the only acceptable + alternative, so the widget can never grow or shrink (e.g. the + vertical direction of a push button). + + \value Minimum The sizeHint() is minimal, and sufficient. The + widget can be expanded, but there is no advantage to it being + larger (e.g. the horizontal direction of a push button). + It cannot be smaller than the size provided by sizeHint(). + + \value Maximum The sizeHint() is a maximum. The widget can be + shrunk any amount without detriment if other widgets need the + space (e.g. a separator line). + It cannot be larger than the size provided by sizeHint(). + + \value Preferred The sizeHint() is best, but the widget can be + shrunk and still be useful. The widget can be expanded, but there + is no advantage to it being larger than sizeHint() (the default + TQWidget policy). + + \value Expanding The sizeHint() is a sensible size, but the + widget can be shrunk and still be useful. The widget can make use + of extra space, so it should get as much space as possible (e.g. + the horizontal direction of a slider). + + \value MinimumExpanding The sizeHint() is minimal, and sufficient. + The widget can make use of extra space, so it should get as much + space as possible (e.g. the horizontal direction of a slider). + + \value Ignored the sizeHint() is ignored. The widget will get as + much space as possible. +*/ + +/*! + \enum TQSizePolicy::ExpandData + + This enum type describes in which directions a widget can make use + of extra space. There are four possible values: + + \value NoDirection the widget cannot make use of extra space in + any direction. + + \value Horizontally the widget can usefully be wider than the + sizeHint(). + + \value Vertically the widget can usefully be taller than the + sizeHint(). + + \value BothDirections the widget can usefully be both wider and + taller than the sizeHint(). +*/ + +/*! + \fn TQSizePolicy::TQSizePolicy() + + Constructs a minimally initialized TQSizePolicy. +*/ + +/*! + \fn TQSizePolicy::TQSizePolicy( SizeType hor, SizeType ver, bool hfw ) + + \target interesting + This is the constructor normally used to return a value in the + overridden \l TQWidget::sizePolicy() function of a TQWidget + subclass. + + It constructs a TQSizePolicy with independent horizontal and + vertical sizing types, \a hor and \a ver respectively. These \link + TQSizePolicy::SizeType sizing types\endlink affect how the widget + is treated by the \link TQLayout layout engine\endlink. + + If \a hfw is TRUE, the preferred height of the widget is dependent + on the width of the widget (for example, a TQLabel with line + wrapping). + + \sa horData() verData() hasHeightForWidth() +*/ + +/*! + \fn TQSizePolicy::TQSizePolicy( SizeType hor, SizeType ver, uchar horStretch, uchar verStretch, bool hfw ) + + Constructs a TQSizePolicy with independent horizontal and vertical + sizing types \a hor and \a ver, and stretch factors \a horStretch + and \a verStretch. + + If \a hfw is TRUE, the preferred height of the widget is dependent on the + width of the widget. + + \sa horStretch() verStretch() +*/ + +/*! + \fn TQSizePolicy::SizeType TQSizePolicy::horData() const + + Returns the horizontal component of the size policy. + + \sa setHorData() verData() horStretch() +*/ + +/*! + \fn TQSizePolicy::SizeType TQSizePolicy::verData() const + + Returns the vertical component of the size policy. + + \sa setVerData() horData() verStretch() +*/ + +/*! + \fn bool TQSizePolicy::mayShrinkHorizontally() const + + Returns TRUE if the widget can sensibly be narrower than its + sizeHint(); otherwise returns FALSE. + + \sa mayShrinkVertically() mayGrowHorizontally() +*/ + +/*! + \fn bool TQSizePolicy::mayShrinkVertically() const + + Returns TRUE if the widget can sensibly be shorter than its + sizeHint(); otherwise returns FALSE. + + \sa mayShrinkHorizontally() mayGrowVertically() +*/ + +/*! + \fn bool TQSizePolicy::mayGrowHorizontally() const + + Returns TRUE if the widget can sensibly be wider than its + sizeHint(); otherwise returns FALSE. + + \sa mayGrowVertically() mayShrinkHorizontally() +*/ + +/*! + \fn bool TQSizePolicy::mayGrowVertically() const + + Returns TRUE if the widget can sensibly be taller than its + sizeHint(); otherwise returns FALSE. + + \sa mayGrowHorizontally() mayShrinkVertically() +*/ + +/*! + \fn TQSizePolicy::ExpandData TQSizePolicy::expanding() const + + Returns whether this layout can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions. + + \sa mayShrinkHorizontally() mayGrowHorizontally() + mayShrinkVertically() mayGrowVertically() +*/ + +/*! + \fn void TQSizePolicy::setHorData( SizeType d ) + + Sets the horizontal component of the size policy to size type \a + d. + + \sa horData() setVerData() +*/ + +/*! + \fn void TQSizePolicy::setVerData( SizeType d ) + + Sets the vertical component of the size policy to size type \a d. + + \sa verData() setHorData() +*/ + +/*! + \fn bool TQSizePolicy::hasHeightForWidth() const + + Returns TRUE if the widget's preferred height depends on its + width; otherwise returns FALSE. + + \sa setHeightForWidth() +*/ + +/*! + \fn void TQSizePolicy::setHeightForWidth( bool b ) + + Sets the hasHeightForWidth() flag to \a b. + + \sa hasHeightForWidth() +*/ + +/*! + \fn uint TQSizePolicy::horStretch() const + + Returns the horizontal stretch factor of the size policy. + + \sa setHorStretch() verStretch() +*/ + +/*! + \fn uint TQSizePolicy::verStretch() const + + Returns the vertical stretch factor of the size policy. + + \sa setVerStretch() horStretch() +*/ + +/*! + \fn void TQSizePolicy::setHorStretch( uchar sf ) + + Sets the horizontal stretch factor of the size policy to \a sf. + + \sa horStretch() setVerStretch() +*/ + +/*! + \fn void TQSizePolicy::setVerStretch( uchar sf ) + + Sets the vertical stretch factor of the size policy to \a sf. + + \sa verStretch() setHorStretch() +*/ + +/*! + \fn void TQSizePolicy::transpose() + + Swaps the horizontal and vertical policies and stretches. +*/ + + +/*! + \fn bool TQSizePolicy::operator==( const TQSizePolicy &s ) const + + Returns TRUE if this policy is equal to \a s; otherwise returns + FALSE. + + \sa operator!=() +*/ + +/*! + \fn bool TQSizePolicy::operator!=( const TQSizePolicy &s ) const + + Returns TRUE if this policy is different from \a s; otherwise + returns FALSE. + + \sa operator==() +*/ + +/*! + \class TQGLayoutIterator + \brief The TQGLayoutIterator class is an abstract base class of internal layout iterators. + + \ingroup appearance + \ingroup geomanagement + + (This class is \e not OpenGL related, it just happens to start with + the letters TQGL...) + + Subclass this class to create a custom layout. The functions that + must be implemented are next(), current(), and takeCurrent(). + + The TQGLayoutIterator implements the functionality of + TQLayoutIterator. Each subclass of TQLayout needs a + TQGLayoutIterator subclass. +*/ + +/*! + \fn TQLayoutItem *TQGLayoutIterator::next() + + Implemented in subclasses to move the iterator to the next item + and return that item, or 0 if there is no next item. +*/ + +/*! + \fn TQLayoutItem *TQGLayoutIterator::current() + + Implemented in subclasses to return the current item, or 0 if + there is no current item. +*/ + +/*! + \fn TQLayoutItem *TQGLayoutIterator::takeCurrent() + + Implemented in subclasses. The function must remove the current + item from the layout without deleting it, move the iterator to the + next item and return the removed item, or 0 if no item was + removed. +*/ + +/*! + Destroys the iterator +*/ +TQGLayoutIterator::~TQGLayoutIterator() +{ +} + +/*! + \class TQLayoutIterator + \brief The TQLayoutIterator class provides iterators over TQLayoutItem. + + \ingroup appearance + \ingroup geomanagement + + Use TQLayoutItem::iterator() to create an iterator over a layout. + + TQLayoutIterator uses \e explicit sharing with a reference count. + If an iterator is copied and one of the copies is modified, both + iterators will be modified. + + A TQLayoutIterator is not protected against changes in its layout. If + the layout is modified or deleted the iterator will become invalid. + It is not possible to test for validity. It is safe to delete an + invalid layout; any other access may lead to an illegal memory + reference and the abnormal termination of the program. + + Calling takeCurrent() or deleteCurrent() leaves the iterator in a + valid state, but may invalidate any other iterators that access the + same layout. + + The following code will draw a rectangle for each layout item in + the layout structure of the widget. + \code + static void paintLayout( TQPainter *p, TQLayoutItem *lay ) + { + TQLayoutIterator it = lay->iterator(); + TQLayoutItem *child; + while ( (child = it.current()) != 0 ) { + paintLayout( p, child ); + ++it; + } + p->drawRect( lay->geometry() ); + } + void ExampleWidget::paintEvent( TQPaintEvent * ) + { + TQPainter p( this ); + if ( layout() ) + paintLayout( &p, layout() ); + } + \endcode + + All the functionality of TQLayoutIterator is implemented by + subclasses of \l TQGLayoutIterator. TQLayoutIterator itself is not + designed to be subclassed. +*/ + +/*! + \fn TQLayoutIterator::TQLayoutIterator( TQGLayoutIterator *gi ) + + Constructs an iterator based on \a gi. The constructed iterator + takes ownership of \a gi and will delete it. + + This constructor is provided for layout implementors. Application + programmers should use TQLayoutItem::iterator() to create an + iterator over a layout. +*/ + +/*! + \fn TQLayoutIterator::TQLayoutIterator( const TQLayoutIterator &i ) + + Creates a shallow copy of \a i, i.e. if the copy is modified, then + the original will also be modified. +*/ + +/*! + \fn TQLayoutIterator::~TQLayoutIterator() + + Destroys the iterator. +*/ + +/*! + \fn TQLayoutIterator &TQLayoutIterator::operator=( const TQLayoutIterator &i ) + + Assigns \a i to this iterator and returns a reference to this + iterator. +*/ + +/*! + \fn TQLayoutItem *TQLayoutIterator::operator++() + + Moves the iterator to the next child item and returns that item, + or 0 if there is no such item. +*/ + +/*! + \fn TQLayoutItem *TQLayoutIterator::current() + + Returns the current item, or 0 if there is no current item. +*/ + +/*! + \fn TQLayoutItem *TQLayoutIterator::takeCurrent() + + Removes the current child item from the layout without deleting + it, and moves the iterator to the next item. Returns the removed + item, or 0 if there was no item to be removed. This iterator will + still be valid, but any other iterator over the same layout may + become invalid. +*/ + +/*! + \fn void TQLayoutIterator::deleteCurrent() + + Removes and deletes the current child item from the layout and + moves the iterator to the next item. This iterator will still be + valid, but any other iterator over the same layout may become + invalid. +*/ + +/*! + \enum TQLayout::ResizeMode + + The possible values are: + + \value Auto If the main widget is a top-level widget with no + height-for-width (hasHeightForWidth()), this is + the same as \c Minimium; otherwise, this is the + same as \c FreeResize. + \value Fixed The main widget's size is set to sizeHint(); it + cannot be resized at all. + \value Minimum The main widget's minimum size is set to + minimumSize(); it cannot be smaller. + \value FreeResize The widget is not constrained. +*/ + +/*! + \property TQLayout::resizeMode + \brief the resize mode of the layout + + The default mode is \c Auto. + + \sa TQLayout::ResizeMode +*/ + +void TQLayout::setResizeMode( ResizeMode mode ) +{ + if ( mode == resizeMode() ) + return; + + switch ( mode ) { + case Auto: + frozen = FALSE; + autoMinimum = FALSE; + autoResizeMode = TRUE; + break; + case Fixed: + frozen = TRUE; + autoMinimum = FALSE; + autoResizeMode = FALSE; + break; + case FreeResize: + frozen = FALSE; + autoMinimum = FALSE; + autoResizeMode = FALSE; + break; + case Minimum: + frozen = FALSE; + autoMinimum = TRUE; + autoResizeMode = FALSE; + } + if ( mainWidget() && mainWidget()->isVisible() ) + activate(); +} + +TQLayout::ResizeMode TQLayout::resizeMode() const +{ + return ( autoResizeMode ? Auto : + (frozen ? Fixed : (autoMinimum ? Minimum : FreeResize)) ); +} + +/*! + \fn bool TQLayout::autoAdd() const + + Returns TRUE if this layout automatically grabs all new + mainWidget()'s new children and adds them as defined by addItem(); + otherwise returns FALSE. This has effect only for top-level + layouts, i.e. layouts that are direct children of their + mainWidget(). + + autoAdd() is disabled by default. + + Note that a top-level layout is not necessarily associated with + the top-level widget. + + \sa setAutoAdd() +*/ + +/*! + If \a b is TRUE, auto-add is enabled; otherwise auto-add is + disabled. + + \warning If auto-add is enabled, you cannot set stretch factors + on the child widgets until the widgets are actually inserted in + the layout (after control returned to the event loop). We + therefore recommend that you avoid the auto-add feature in new + programs. + + \sa autoAdd() +*/ +void TQLayout::setAutoAdd( bool b ) +{ + autoNewChild = b; +} + +/*! + \fn bool TQLayout::supportsMargin() const + + Returns TRUE if this layout supports \l TQLayout::margin on + non-top-level layouts; otherwise returns FALSE. + + \sa margin +*/ + +/*! + Sets the value returned by supportsMargin(). If \a b is TRUE, + margin() handling is implemented by the subclass. If \a b is + FALSE (the default), TQLayout will add margin() around top-level + layouts. + + If \a b is TRUE, margin handling needs to be implemented in + setGeometry(), maximumSize(), minimumSize(), sizeHint() and + heightForWidth(). + + \sa supportsMargin() +*/ +void TQLayout::setSupportsMargin( bool b ) +{ + marginImpl = b; +} + +/*! + Returns the rectangle that should be covered when the geometry of + this layout is set to \a r, provided that this layout supports + setAlignment(). + + The result is derived from sizeHint() and expanding(). It is never + larger than \a r. +*/ +TQRect TQLayout::alignmentRect( const TQRect &r ) const +{ + TQSize s = sizeHint(); + int a = alignment(); + + /* + This is a hack to obtain the real maximum size, not + TQSize(TQLAYOUTSIZE_MAX, TQLAYOUTSIZE_MAX), the value consistently + returned by TQLayoutItems that have an alignment. + */ + TQLayout *that = (TQLayout *) this; + that->setAlignment( 0 ); + TQSize ms = maximumSize(); + that->setAlignment( a ); + + if ( (expanding() & TQSizePolicy::Horizontally) || + !(a & TQt::AlignHorizontal_Mask ) ) { + s.setWidth( TQMIN(r.width(), ms.width()) ); + } + if ( (expanding() & TQSizePolicy::Vertically) || + !(a & TQt::AlignVertical_Mask) ) { + s.setHeight( TQMIN(r.height(), ms.height()) ); + } else if ( hasHeightForWidth() ) { + int hfw = heightForWidth( s.width() ); + if ( hfw < s.height() ) + s.setHeight( TQMIN(hfw, ms.height()) ); + } + + int x = r.x(); + int y = r.y(); + + if ( a & TQt::AlignBottom ) + y += ( r.height() - s.height() ); + else if ( !(a & TQt::AlignTop) ) + y += ( r.height() - s.height() ) / 2; + + a = TQApplication::horizontalAlignment( a ); + if ( a & TQt::AlignRight ) + x += ( r.width() - s.width() ); + else if ( !(a & TQt::AlignLeft) ) + x += ( r.width() - s.width() ) / 2; + + return TQRect( x, y, s.width(), s.height() ); +} + +/*! + Removes the widget \a widget from the layout. After this call, it + is the caller's responsibility to give the widget a reasonable + geometry or to put the widget back into a layout. + + \sa removeItem(), TQWidget::setGeometry(), add() +*/ +void TQLayout::remove( TQWidget *widget ) +{ + TQLayoutIterator it = iterator(); + TQLayoutItem *child; + while ( (child = it.current()) != 0 ) { + if ( child->widget() == widget ) { + it.deleteCurrent(); + invalidate(); // maybe redundant + TQApplication::postEvent( mainWidget(), + new TQEvent(TQEvent::LayoutHint) ); + } else { + ++it; + } + } +} + +/*! + Removes the layout item \a item from the layout. It is the + caller's responsibility to delete the item. + + Notice that \a item can be a layout (since TQLayout inherits + TQLayoutItem). + + \sa remove(), addItem() +*/ +void TQLayout::removeItem( TQLayoutItem *item ) +{ + TQLayoutIterator it = iterator(); + TQLayoutItem *child; + while ( (child = it.current()) != 0 ) { + if ( child == item ) { + it.takeCurrent(); + invalidate(); // maybe redundant + TQApplication::postEvent( mainWidget(), + new TQEvent(TQEvent::LayoutHint) ); + } else { + ++it; + } + } +} + +/*! + Enables this layout if \a enable is TRUE, otherwise disables it. + + An enabled layout adjusts dynamically to changes; a disabled + layout acts as if it did not exist. + + By default all layouts are enabled. + + \sa isEnabled() +*/ +void TQLayout::setEnabled( bool enable ) +{ + enabled = enable; +} + +/*! + Returns TRUE if the layout is enabled; otherwise returns FALSE. + + \sa setEnabled() +*/ +bool TQLayout::isEnabled() const +{ + return enabled; +} + +void TQLayout::propagateSpacing( TQLayout *parent ) +{ + TQLayoutIterator it = parent->iterator(); + TQLayoutItem *child; + while ( (child = it.current()) ) { + TQLayout *childLayout = child->layout(); + if ( childLayout && childLayout->insideSpacing < 0 ) { + childLayout->insideSpacing = parent->insideSpacing; + propagateSpacing( childLayout ); + } + ++it; + } +} + +#endif // QT_NO_LAYOUT diff --git a/src/kernel/qabstractlayout.h b/src/kernel/qabstractlayout.h new file mode 100644 index 000000000..5545f686b --- /dev/null +++ b/src/kernel/qabstractlayout.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Definition of layout classes +** +** Created : 960416 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQABSTRACTLAYOUT_H +#define TQABSTRACTLAYOUT_H + +/* + This header is provided for source compatibility only. +*/ + +#ifndef QT_H +#ifndef QT_NO_COMPAT +#include "qlayout.h" +#endif +#endif // QT_H + +#endif diff --git a/src/kernel/qaccel.cpp b/src/kernel/qaccel.cpp new file mode 100644 index 000000000..b761c7365 --- /dev/null +++ b/src/kernel/qaccel.cpp @@ -0,0 +1,1089 @@ +/**************************************************************************** +** +** Implementation of TQAccel class +** +** Created : 950419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qaccel.h" + +#ifndef QT_NO_ACCEL + +#include "qsignal.h" +#include "qapplication.h" +#include "qwidget.h" +#include "qptrlist.h" +#include "qwhatsthis.h" +#include "qguardedptr.h" +#include "qstatusbar.h" +#include "qdockwindow.h" +#include "qsignalslotimp.h" +/*! + \class TQAccel qaccel.h + \brief The TQAccel class handles keyboard accelerator and shortcut keys. + + \ingroup misc + + A keyboard accelerator triggers an action when a certain key + combination is pressed. The accelerator handles all keyboard + activity for all the children of one top-level widget, so it is + not affected by the keyboard focus. + + In most cases, you will not need to use this class directly. Use + the TQAction class to create actions with accelerators that can be + used in both menus and toolbars. If you're only interested in + menus use TQMenuData::insertItem() or TQMenuData::setAccel() to make + accelerators for operations that are also available on menus. Many + widgets automatically generate accelerators, such as TQButton, + TQGroupBox, TQLabel (with TQLabel::setBuddy()), TQMenuBar and TQTabBar. + Example: + \code + TQPushButton p( "&Exit", parent ); // automatic shortcut ALT+Key_E + TQPopupMenu *fileMenu = new fileMenu( parent ); + fileMenu->insertItem( "Undo", parent, SLOT(undo()), CTRL+Key_Z ); + \endcode + + A TQAccel contains a list of accelerator items that can be + manipulated using insertItem(), removeItem(), clear(), key() and + findKey(). + + Each accelerator item consists of an identifier and a \l + TQKeySequence. A single key sequence consists of a keyboard code + combined with modifiers (\c SHIFT, \c CTRL, \c ALT or \c + UNICODE_ACCEL). For example, \c{CTRL + Key_P} could be a shortcut + for printing a document. The key codes are listed in \c + qnamespace.h. As an alternative, use \c UNICODE_ACCEL with the + unicode code point of the character. For example, \c{UNICODE_ACCEL + + 'A'} gives the same accelerator as \c Key_A. + + When an accelerator key is pressed, the accelerator sends out the + signal activated() with a number that identifies this particular + accelerator item. Accelerator items can also be individually + connected, so that two different keys will activate two different + slots (see connectItem() and disconnectItem()). + + The activated() signal is \e not emitted when two or more + accelerators match the same key. Instead, the first matching + accelerator sends out the activatedAmbiguously() signal. By + pressing the key multiple times, users can navigate between all + matching accelerators. Some standard controls like TQPushButton and + TQCheckBox connect the activatedAmbiguously() signal to the + harmless setFocus() slot, whereas activated() is connected to a + slot invoking the button's action. Most controls, like TQLabel and + TQTabBar, treat activated() and activatedAmbiguously() as + equivalent. + + Use setEnabled() to enable or disable all the items in an + accelerator, or setItemEnabled() to enable or disable individual + items. An item is active only when both the TQAccel and the item + itself are enabled. + + The function setWhatsThis() specifies a help text that appears + when the user presses an accelerator key in What's This mode. + + The accelerator will be deleted when \e parent is deleted, + and will consume relevant key events until then. + + Please note that the accelerator + \code + accelerator->insertItem( TQKeySequence("M") ); + \endcode + can be triggered with both the 'M' key, and with Shift+M, + unless a second accelerator is defined for the Shift+M + combination. + + + Example: + \code + TQAccel *a = new TQAccel( myWindow ); // create accels for myWindow + a->connectItem( a->insertItem(Key_P+CTRL), // adds Ctrl+P accelerator + myWindow, // connected to myWindow's + SLOT(printDoc()) ); // printDoc() slot + \endcode + + \sa TQKeyEvent TQWidget::keyPressEvent() TQMenuData::setAccel() + TQButton::setAccel() TQLabel::setBuddy() TQKeySequence + \link guibooks.html#fowler GUI Design Handbook: Keyboard Shortcuts \endlink. +*/ + + +struct TQAccelItem { // internal accelerator item + TQAccelItem( const TQKeySequence &k, int i ) + { key=k; id=i; enabled=TRUE; signal=0; } + ~TQAccelItem() { delete signal; } + int id; + TQKeySequence key; + bool enabled; + TQSignal *signal; + TQString whatsthis; +}; + + +typedef TQPtrList TQAccelList; // internal accelerator list + +class TQAccelPrivate : public TQt { +public: + TQAccelPrivate( TQAccel* p ); + ~TQAccelPrivate(); + TQAccelList aitems; + bool enabled; + TQGuardedPtr watch; + bool ignorewhatsthis; + TQAccel* parent; + + void activate( TQAccelItem* item ); + void activateAmbiguously( TQAccelItem* item ); +}; + +class TQAccelManager : public TQt { +public: + static TQAccelManager* self() { return self_ptr ? self_ptr : new TQAccelManager; } + void registerAccel( TQAccelPrivate* a ) { accels.append( a ); } + void unregisterAccel( TQAccelPrivate* a ) { accels.removeRef( a ); if ( accels.isEmpty() ) delete this; } + bool tryAccelEvent( TQWidget* w, TQKeyEvent* e ); + bool dispatchAccelEvent( TQWidget* w, TQKeyEvent* e ); + bool tryComposeUnicode( TQWidget* w, TQKeyEvent* e ); + +private: + TQAccelManager():currentState(TQt::NoMatch), clash(-1) { self_ptr = this; } + ~TQAccelManager() { self_ptr = 0; } + + bool correctSubWindow( TQWidget *w, TQAccelPrivate* d ); + SequenceMatch match( TQKeyEvent* e, TQAccelItem* item, TQKeySequence& temp ); + int translateModifiers( ButtonState state ); + + TQPtrList accels; + static TQAccelManager* self_ptr; + TQt::SequenceMatch currentState; + TQKeySequence intermediate; + int clash; +}; +TQAccelManager* TQAccelManager::self_ptr = 0; + +bool Q_EXPORT qt_tryAccelEvent( TQWidget* w, TQKeyEvent* e){ + return TQAccelManager::self()->tryAccelEvent( w, e ); +} + +bool Q_EXPORT qt_dispatchAccelEvent( TQWidget* w, TQKeyEvent* e){ + return TQAccelManager::self()->dispatchAccelEvent( w, e ); +} + +bool Q_EXPORT qt_tryComposeUnicode( TQWidget* w, TQKeyEvent* e){ + return TQAccelManager::self()->tryComposeUnicode( w, e ); +} + +#ifdef Q_WS_MAC +static bool qt_accel_no_shortcuts = TRUE; +#else +static bool qt_accel_no_shortcuts = FALSE; +#endif +void Q_EXPORT qt_setAccelAutoShortcuts(bool b) { qt_accel_no_shortcuts = b; } + +/* + \internal + Returns TRUE if the accel is in the current subwindow, else FALSE. +*/ +bool TQAccelManager::correctSubWindow( TQWidget* w, TQAccelPrivate* d ) { +#if !defined ( Q_OS_MACX ) + if ( !d->watch || !d->watch->isVisible() || !d->watch->isEnabled() ) +#else + if ( !d->watch || (!d->watch->isVisible() && !d->watch->inherits( "TQMenuBar" )) || !d->watch->isEnabled() ) +#endif + return FALSE; + TQWidget* tlw = w->topLevelWidget(); + TQWidget* wtlw = d->watch->topLevelWidget(); + + /* if we live in a floating dock window, keep our parent's + * accelerators working */ +#ifndef QT_NO_MAINWINDOW + if ( tlw->isDialog() && tlw->parentWidget() && ::qt_cast(tlw) ) + return tlw->parentWidget()->topLevelWidget() == wtlw; + + if ( wtlw != tlw ) + return FALSE; +#endif + /* if we live in a MDI subwindow, ignore the event if we are + not the active document window */ + TQWidget* sw = d->watch; + while ( sw && !sw->testWFlags( WSubWindow ) ) + sw = sw->parentWidget( TRUE ); + if ( sw ) { // we are in a subwindow indeed + TQWidget* fw = w; + while ( fw && fw != sw ) + fw = fw->parentWidget( TRUE ); + if ( fw != sw ) // focus widget not in our subwindow + return FALSE; + } + return TRUE; +} + +inline int TQAccelManager::translateModifiers( ButtonState state ) +{ + int result = 0; + if ( state & ShiftButton ) + result |= SHIFT; + if ( state & ControlButton ) + result |= CTRL; + if ( state & MetaButton ) + result |= META; + if ( state & AltButton ) + result |= ALT; + return result; +} + +/* + \internal + Matches the current intermediate key sequence + the latest + keyevent, with and AccelItem. Returns Identical, + PartialMatch or NoMatch, and fills \a temp with the + resulting key sequence. +*/ +TQt::SequenceMatch TQAccelManager::match( TQKeyEvent *e, TQAccelItem* item, TQKeySequence& temp ) +{ + SequenceMatch result = TQt::NoMatch; + int index = intermediate.count(); + temp = intermediate; + + int modifier = translateModifiers( e->state() ); + + if ( e->key() && e->key() != Key_unknown) { + int key = e->key() | modifier; + if ( e->key() == Key_BackTab ) { + /* + In TQApplication, we map shift+tab to shift+backtab. + This code here reverts the mapping in a way that keeps + backtab and shift+tab accelerators working, in that + order, meaning backtab has priority.*/ + key &= ~SHIFT; + + temp.setKey( key, index ); + if ( TQt::NoMatch != (result = temp.matches( item->key )) ) + return result; + if ( e->state() & ShiftButton ) + key |= SHIFT; + key = Key_Tab | ( key & MODIFIER_MASK ); + temp.setKey( key, index ); + if ( TQt::NoMatch != (result = temp.matches( item->key )) ) + return result; + } else { + temp.setKey( key, index ); + if ( TQt::NoMatch != (result = temp.matches( item->key )) ) + return result; + } + + if ( key == Key_BackTab ) { + if ( e->state() & ShiftButton ) + key |= SHIFT; + temp.setKey( key, index ); + if ( TQt::NoMatch != (result = temp.matches( item->key )) ) + return result; + } + } + if ( !e->text().isEmpty() ) { + temp.setKey( (int)e->text()[0].unicode() | UNICODE_ACCEL | modifier, index ); + result = temp.matches( item->key ); + } + return result; +} + +bool TQAccelManager::tryAccelEvent( TQWidget* w, TQKeyEvent* e ) +{ + if ( TQt::NoMatch == currentState ) { + e->t = TQEvent::AccelOverride; + e->ignore(); + TQApplication::sendSpontaneousEvent( w, e ); + if ( e->isAccepted() ) + return FALSE; + } + e->t = TQEvent::Accel; + e->ignore(); + TQApplication::sendSpontaneousEvent( w, e ); + return e->isAccepted(); +} + +bool TQAccelManager::tryComposeUnicode( TQWidget* w, TQKeyEvent* e ) +{ + if ( TQApplication::metaComposeUnicode ) { + int value = e->key() - Key_0; + // Ignore acceloverrides so we don't trigger + // accels on keypad when Meta compose is on + if ( (e->type() == TQEvent::AccelOverride) && + (e->state() == TQt::Keypad + TQt::MetaButton) ) { + e->accept(); + // Meta compose start/continue + } else if ( (e->type() == TQEvent::KeyPress) && + (e->state() == TQt::Keypad + TQt::MetaButton) ) { + if ( value >= 0 && value <= 9 ) { + TQApplication::composedUnicode *= 10; + TQApplication::composedUnicode += value; + return TRUE; + } else { + // Composing interrupted, dispatch! + if ( TQApplication::composedUnicode ) { + TQChar ch( TQApplication::composedUnicode ); + TQString s( ch ); + TQKeyEvent kep( TQEvent::KeyPress, 0, ch.row() ? 0 : ch.cell(), 0, s ); + TQKeyEvent ker( TQEvent::KeyRelease, 0, ch.row() ? 0 : ch.cell(), 0, s ); + TQApplication::sendEvent( w, &kep ); + TQApplication::sendEvent( w, &ker ); + } + TQApplication::composedUnicode = 0; + return TRUE; + } + // Meta compose end, dispatch + } else if ( (e->type() == TQEvent::KeyRelease) && + (e->key() == Key_Meta) && + (TQApplication::composedUnicode != 0) ) { + if ( (TQApplication::composedUnicode > 0) && + (TQApplication::composedUnicode < 0xFFFE) ) { + TQChar ch( TQApplication::composedUnicode ); + TQString s( ch ); + TQKeyEvent kep( TQEvent::KeyPress, 0, ch.row() ? 0 : ch.cell(), 0, s ); + TQKeyEvent ker( TQEvent::KeyRelease, 0, ch.row() ? 0 : ch.cell(), 0, s ); + TQApplication::sendEvent( w, &kep ); + TQApplication::sendEvent( w, &ker ); + } + TQApplication::composedUnicode = 0; + return TRUE; + } + } + return FALSE; +} + +/* + \internal + Checks for possible accelerators, if no widget + ate the keypres, or we are in the middle of a + partial key sequence. +*/ +bool TQAccelManager::dispatchAccelEvent( TQWidget* w, TQKeyEvent* e ) +{ +#ifndef QT_NO_STATUSBAR + // Needs to be declared and used here because of "goto doclash" + TQStatusBar* mainStatusBar = 0; +#endif + + // Modifiers can NOT be accelerators... + if ( e->key() >= Key_Shift && + e->key() <= Key_Alt ) + return FALSE; + + SequenceMatch result = TQt::NoMatch; + TQKeySequence tocheck, partial; + TQAccelPrivate* accel = 0; + TQAccelItem* item = 0; + TQAccelPrivate* firstaccel = 0; + TQAccelItem* firstitem = 0; + TQAccelPrivate* lastaccel = 0; + TQAccelItem* lastitem = 0; + + TQKeyEvent pe = *e; + int n = -1; + int hasShift = (e->state()&TQt::ShiftButton)?1:0; + bool identicalDisabled = FALSE; + bool matchFound = FALSE; + do { + accel = accels.first(); + matchFound = FALSE; + while ( accel ) { + if ( correctSubWindow( w, accel ) ) { + if ( accel->enabled ) { + item = accel->aitems.last(); + while( item ) { + if ( TQt::Identical == (result = match( &pe, item, tocheck )) ) { + if ( item->enabled ) { + if ( !firstaccel ) { + firstaccel = accel; + firstitem = item; + } + lastaccel = accel; + lastitem = item; + n++; + matchFound = TRUE; + if ( n > TQMAX(clash,0) ) + goto doclash; + } else { + identicalDisabled = TRUE; + } + } + if ( item->enabled && TQt::PartialMatch == result ) { + partial = tocheck; + matchFound = TRUE; + } + item = accel->aitems.prev(); + } + } else { + item = accel->aitems.last(); + while( item ) { + if ( TQt::Identical == match( &pe, item, tocheck ) ) + identicalDisabled = TRUE; + item = accel->aitems.prev(); + } + } + } + accel = accels.next(); + } + pe = TQKeyEvent( TQEvent::Accel, pe.key(), pe.ascii(), pe.state()&~TQt::ShiftButton, pe.text() ); + } while ( hasShift-- && !matchFound && !identicalDisabled ); + +#ifndef QT_NO_STATUSBAR + mainStatusBar = (TQStatusBar*) w->topLevelWidget()->child( 0, "TQStatusBar" ); +#endif + if ( n < 0 ) { // no match found + currentState = partial.count() ? PartialMatch : NoMatch; +#ifndef QT_NO_STATUSBAR + // Only display message if we are, or were, in a partial match + if ( mainStatusBar && (PartialMatch == currentState || intermediate.count() ) ) { + if ( currentState == TQt::PartialMatch ) { + mainStatusBar->message( (TQString)partial + ", ...", 0 ); + } else if (!identicalDisabled) { + TQString message = TQAccel::tr("%1, %2 not defined"). + arg( (TQString)intermediate ). + arg( TQKeySequence::encodeString( e->key() | translateModifiers(e->state()) ) ); + mainStatusBar->message( message, 2000 ); + // Since we're a NoMatch, reset the clash count + clash = -1; + } else { + mainStatusBar->clear(); + } + } +#endif + + bool eatKey = (PartialMatch == currentState || intermediate.count() ); + intermediate = partial; + if ( eatKey ) + e->accept(); + return eatKey; + } else if ( n == 0 ) { // found exactly one match + clash = -1; // reset +#ifndef QT_NO_STATUSBAR + if ( currentState == TQt::PartialMatch && mainStatusBar ) + mainStatusBar->clear(); +#endif + currentState = TQt::NoMatch; // Free sequence keylock + intermediate = TQKeySequence(); + lastaccel->activate( lastitem ); + e->accept(); + return TRUE; + } + + doclash: // found more than one match +#ifndef QT_NO_STATUSBAR + if ( !mainStatusBar ) // if "goto doclash", we need to get statusbar again. + mainStatusBar = (TQStatusBar*) w->topLevelWidget()->child( 0, "TQStatusBar" ); +#endif + + TQString message = TQAccel::tr( "Ambiguous \"%1\" not handled" ).arg( (TQString)tocheck ); + if ( clash >= 0 && n > clash ) { // pick next match + intermediate = TQKeySequence(); + currentState = TQt::NoMatch; // Free sequence keylock + clash++; +#ifndef QT_NO_STATUSBAR + if ( mainStatusBar && + !lastitem->signal && + !(lastaccel->parent->receivers( "activatedAmbiguously(int)" )) ) + mainStatusBar->message( message, 2000 ); +#endif + lastaccel->activateAmbiguously( lastitem ); + } else { // start (or wrap) with the first matching + intermediate = TQKeySequence(); + currentState = TQt::NoMatch; // Free sequence keylock + clash = 0; +#ifndef QT_NO_STATUSBAR + if ( mainStatusBar && + !firstitem->signal && + !(firstaccel->parent->receivers( "activatedAmbiguously(int)" )) ) + mainStatusBar->message( message, 2000 ); +#endif + firstaccel->activateAmbiguously( firstitem ); + } + e->accept(); + return TRUE; +} + +TQAccelPrivate::TQAccelPrivate( TQAccel* p ) + : parent( p ) +{ + TQAccelManager::self()->registerAccel( this ); + aitems.setAutoDelete( TRUE ); + ignorewhatsthis = FALSE; +} + +TQAccelPrivate::~TQAccelPrivate() +{ + TQAccelManager::self()->unregisterAccel( this ); +} + +static TQAccelItem *find_id( TQAccelList &list, int id ) +{ + register TQAccelItem *item = list.first(); + while ( item && item->id != id ) + item = list.next(); + return item; +} + +static TQAccelItem *find_key( TQAccelList &list, const TQKeySequence &key ) +{ + register TQAccelItem *item = list.first(); + while ( item && !( item->key == key ) ) + item = list.next(); + return item; +} + +/*! + Constructs a TQAccel object called \a name, with parent \a parent. + The accelerator operates on \a parent. +*/ + +TQAccel::TQAccel( TQWidget *parent, const char *name ) + : TQObject( parent, name ) +{ + d = new TQAccelPrivate( this ); + d->enabled = TRUE; + d->watch = parent; +#if defined(QT_CHECK_NULL) + if ( !d->watch ) + qWarning( "TQAccel: An accelerator must have a parent or a watch widget" ); +#endif +} + +/*! + Constructs a TQAccel object called \a name, that operates on \a + watch, and is a child of \a parent. + + This constructor is not needed for normal application programming. +*/ +TQAccel::TQAccel( TQWidget* watch, TQObject *parent, const char *name ) + : TQObject( parent, name ) +{ + d = new TQAccelPrivate( this ); + d->enabled = TRUE; + d->watch = watch; +#if defined(QT_CHECK_NULL) + if ( !d->watch ) + qWarning( "TQAccel: An accelerator must have a parent or a watch widget" ); +#endif +} + +/*! + Destroys the accelerator object and frees all allocated resources. +*/ + +TQAccel::~TQAccel() +{ + delete d; +} + + +/*! + \fn void TQAccel::activated( int id ) + + This signal is emitted when an accelerator key is pressed. \a id + is a number that identifies this particular accelerator item. + + \sa activatedAmbiguously() +*/ + +/*! + \fn void TQAccel::activatedAmbiguously( int id ) + + This signal is emitted when an accelerator key is pressed. \a id + is a number that identifies this particular accelerator item. + + \sa activated() +*/ + + +/*! + Returns TRUE if the accelerator is enabled; otherwise returns + FALSE. + + \sa setEnabled(), isItemEnabled() +*/ + +bool TQAccel::isEnabled() const +{ + return d->enabled; +} + + +/*! + Enables the accelerator if \a enable is TRUE, or disables it if \a + enable is FALSE. + + Individual keys can also be enabled or disabled using + setItemEnabled(). To work, a key must be an enabled item in an + enabled TQAccel. + + \sa isEnabled(), setItemEnabled() +*/ + +void TQAccel::setEnabled( bool enable ) +{ + d->enabled = enable; +} + + +/*! + Returns the number of accelerator items in this accelerator. +*/ + +uint TQAccel::count() const +{ + return d->aitems.count(); +} + + +static int get_seq_id() +{ + static int seq_no = -2; // -1 is used as return value in findKey() + return seq_no--; +} + +/*! + Inserts an accelerator item and returns the item's identifier. + + \a key is a key code and an optional combination of SHIFT, CTRL + and ALT. \a id is the accelerator item id. + + If \a id is negative, then the item will be assigned a unique + negative identifier less than -1. + + \code + TQAccel *a = new TQAccel( myWindow ); // create accels for myWindow + a->insertItem( CTRL + Key_P, 200 ); // Ctrl+P, e.g. to print document + a->insertItem( ALT + Key_X, 201 ); // Alt+X, e.g. to tquit + a->insertItem( UNICODE_ACCEL + 'q', 202 ); // Unicode 'q', e.g. to tquit + a->insertItem( Key_D ); // gets a unique negative id < -1 + a->insertItem( CTRL + SHIFT + Key_P ); // gets a unique negative id < -1 + \endcode +*/ + +int TQAccel::insertItem( const TQKeySequence& key, int id ) +{ + if ( id == -1 ) + id = get_seq_id(); + d->aitems.insert( 0, new TQAccelItem(key,id) ); + return id; +} + +/*! + Removes the accelerator item with the identifier \a id. +*/ + +void TQAccel::removeItem( int id ) +{ + if ( find_id( d->aitems, id) ) + d->aitems.remove(); +} + + +/*! + Removes all accelerator items. +*/ + +void TQAccel::clear() +{ + d->aitems.clear(); +} + + +/*! + Returns the key sequence of the accelerator item with identifier + \a id, or an invalid key sequence (0) if the id cannot be found. +*/ + +TQKeySequence TQAccel::key( int id ) +{ + TQAccelItem *item = find_id( d->aitems, id); + return item ? item->key : TQKeySequence( 0 ); +} + + +/*! + Returns the identifier of the accelerator item with the key code + \a key, or -1 if the item cannot be found. +*/ + +int TQAccel::findKey( const TQKeySequence& key ) const +{ + TQAccelItem *item = find_key( d->aitems, key ); + return item ? item->id : -1; +} + + +/*! + Returns TRUE if the accelerator item with the identifier \a id is + enabled. Returns FALSE if the item is disabled or cannot be found. + + \sa setItemEnabled(), isEnabled() +*/ + +bool TQAccel::isItemEnabled( int id ) const +{ + TQAccelItem *item = find_id( d->aitems, id); + return item ? item->enabled : FALSE; +} + + +/*! + Enables the accelerator item with the identifier \a id if \a + enable is TRUE, and disables item \a id if \a enable is FALSE. + + To work, an item must be enabled and be in an enabled TQAccel. + + \sa isItemEnabled(), isEnabled() +*/ + +void TQAccel::setItemEnabled( int id, bool enable ) +{ + TQAccelItem *item = find_id( d->aitems, id); + if ( item ) + item->enabled = enable; +} + + +/*! + Connects the accelerator item \a id to the slot \a member of \a + receiver. + + \code + a->connectItem( 201, mainView, SLOT(tquit()) ); + \endcode + + Of course, you can also send a signal as \a member. + + Normally accelerators are connected to slots which then receive + the \c activated(int id) signal with the id of the accelerator + item that was activated. If you choose to connect a specific + accelerator item using this function, the \c activated() signal is + emitted if the associated key sequence is pressed but no \c + activated(int id) signal is emitted. + + \sa disconnectItem() +*/ + +bool TQAccel::connectItem( int id, const TQObject *receiver, const char *member ) +{ + TQAccelItem *item = find_id( d->aitems, id); + if ( item ) { + if ( !item->signal ) { + item->signal = new TQSignal; + Q_CHECK_PTR( item->signal ); + } + return item->signal->connect( receiver, member ); + } + return FALSE; +} + +/*! + Disconnects an accelerator item with id \a id from the function + called \a member in the \a receiver object. + + \sa connectItem() +*/ + +bool TQAccel::disconnectItem( int id, const TQObject *receiver, + const char *member ) +{ + TQAccelItem *item = find_id( d->aitems, id); + if ( item && item->signal ) + return item->signal->disconnect( receiver, member ); + return FALSE; +} + +void TQAccelPrivate::activate( TQAccelItem* item ) +{ +#ifndef QT_NO_WHATSTHIS + if ( TQWhatsThis::inWhatsThisMode() && !ignorewhatsthis ) { + TQWhatsThis::leaveWhatsThisMode( item->whatsthis ); + return; + } +#endif + if ( item->signal ) + item->signal->activate(); + else + emit parent->activated( item->id ); +} + +void TQAccelPrivate::activateAmbiguously( TQAccelItem* item ) +{ + if ( item->signal ) + item->signal->activate(); + else + emit parent->activatedAmbiguously( item->id ); +} + + +/*! + Returns the shortcut key sequence for \a str, or an invalid key + sequence (0) if \a str has no shortcut sequence. + + For example, shortcutKey("E&xit") returns ALT+Key_X, + shortcutKey("&Quit") returns ALT+Key_Q and shortcutKey("Quit") + returns 0. (In code that does not inherit the TQt namespace class, + you must write e.g. TQt::ALT+TQt::Key_Q.) + + We provide a \link accelerators.html list of common accelerators + \endlink in English. At the time of writing, Microsoft and Open + Group do not appear to have issued equivalent recommendations for + other languages. +*/ + +TQKeySequence TQAccel::shortcutKey( const TQString &str ) +{ + if(qt_accel_no_shortcuts) + return TQKeySequence(); + + int p = 0; + while ( p >= 0 ) { + p = str.find( '&', p ) + 1; + if ( p <= 0 || p >= (int)str.length() ) + return 0; + if ( str[p] != '&' ) { + TQChar c = str[p]; + if ( c.isPrint() ) { + char ltr = c.upper().latin1(); + if ( ltr >= (char)Key_A && ltr <= (char)Key_Z ) + c = ltr; + else + c = c.lower(); + return TQKeySequence( c.unicode() + ALT + UNICODE_ACCEL ); + } + } + p++; + } + return TQKeySequence(); +} + +/*! \obsolete + + Creates an accelerator string for the key \a k. + For instance CTRL+Key_O gives "Ctrl+O". The "Ctrl" etc. + are translated (using TQObject::tr()) in the "TQAccel" context. + + The function is superfluous. Cast the TQKeySequence \a k to a + TQString for the same effect. +*/ +TQString TQAccel::keyToString( TQKeySequence k ) +{ + return (TQString) k; +} + +/*!\obsolete + + Returns an accelerator code for the string \a s. For example + "Ctrl+O" gives CTRL+UNICODE_ACCEL+'O'. The strings "Ctrl", + "Shift", "Alt" are recognized, as well as their translated + equivalents in the "TQAccel" context (using TQObject::tr()). Returns 0 + if \a s is not recognized. + + This function is typically used with \link TQObject::tr() tr + \endlink(), so that accelerator keys can be replaced in + translations: + + \code + TQPopupMenu *file = new TQPopupMenu( this ); + file->insertItem( p1, tr("&Open..."), this, SLOT(open()), + TQAccel::stringToKey(tr("Ctrl+O", "File|Open")) ); + \endcode + + Notice the \c "File|Open" translator comment. It is by no means + necessary, but it provides some context for the human translator. + + + The function is superfluous. Construct a TQKeySequence from the + string \a s for the same effect. + + \sa TQObject::tr() + \link i18n.html Internationalization with TQt \endlink +*/ +TQKeySequence TQAccel::stringToKey( const TQString & s ) +{ + return TQKeySequence( s ); +} + + +/*! + Sets a What's This help text for the accelerator item \a id to \a + text. + + The text will be shown when the application is in What's This mode + and the user hits the accelerator key. + + To set What's This help on a menu item (with or without an + accelerator key), use TQMenuData::setWhatsThis(). + + \sa whatsThis(), TQWhatsThis::inWhatsThisMode(), + TQMenuData::setWhatsThis(), TQAction::setWhatsThis() +*/ +void TQAccel::setWhatsThis( int id, const TQString& text ) +{ + + TQAccelItem *item = find_id( d->aitems, id); + if ( item ) + item->whatsthis = text; +} + +/*! + Returns the What's This help text for the specified item \a id or + TQString::null if no text has been specified. + + \sa setWhatsThis() +*/ +TQString TQAccel::whatsThis( int id ) const +{ + + TQAccelItem *item = find_id( d->aitems, id); + return item? item->whatsthis : TQString::null; +} + +/*!\internal */ +void TQAccel::setIgnoreWhatsThis( bool b) +{ + d->ignorewhatsthis = b; +} + +/*!\internal */ +bool TQAccel::ignoreWhatsThis() const +{ + return d->ignorewhatsthis; +} + + +/*! + +\page accelerators.html + +\title Standard Accelerator Keys + +Applications invariably need to define accelerator keys for actions. +TQt fully supports accelerators, for example with \l TQAccel::shortcutKey(). + +Here are Microsoft's recommendations for accelerator keys, with +comments about the Open Group's recommendations where they exist +and differ. For most commands, the Open Group either has no advice or +agrees with Microsoft. + +The emboldened letter plus Alt is Microsoft's recommended choice, and +we recommend supporting it. For an Apply button, for example, we +recommend TQButton::setText( \link TQWidget::tr() tr \endlink("&Apply") ); + +If you have conflicting commands (e.g. About and Apply buttons in the +same dialog), you must decide for yourself. + +\list +\i About +\i Always on Top +\i Apply +\i Back +\i Browse +\i Close (CDE: Alt+F4; Alt+F4 is "close window" in Windows) +\i Copy (CDE: Ctrl+C, Ctrl+Insert) +\i Copy Here +\i Create Shortcut +\i Create Shortcut Here +\i Cut +\i Delete +\i Edit +\i Exit (CDE: Exit) +\i Explore +\i File +\i Find +\i Help +\i Help Topics +\i Hide +\i Insert +\i Insert Object +\i Link Here +\i Maximize +\i Minimize +\i Move +\i Move Here +\i New +\i Next +\i No +\i Open +\i Open With +\i Page Setup +\i Paste +\i Paste Link +\i Paste Shortcut +\i Paste Special +\i Pause +\i Play +\i Print +\i Print Here +\i Properties +\i Quick View +\i Redo (CDE: Ctrl+Y, Shift+Alt+Backspace) +\i Repeat +\i Restore +\i Resume +\i Retry +\i Run +\i Save +\i Save As +\i Select All +\i Send To +\i Show +\i Size +\i Split +\i Stop +\i Undo (CDE: Ctrl+Z or Alt+Backspace) +\i View +\i What's This? +\i Window +\i Yes +\endlist + +There are also a lot of other keys and actions (that use other +modifier keys than Alt). See the Microsoft and The Open Group +documentation for details. + +The \link http://www.amazon.com/exec/obidos/ASIN/0735605661/trolltech/t +Microsoft book \endlink has ISBN 0735605661. The corresponding Open Group +book is very hard to find, rather expensive and we cannot recommend +it. However, if you really want it, OGPubs@opengroup.org might be able +to help. Ask them for ISBN 1859121047. + +*/ + +/*! \obsolete serves no purpose anymore */ +void TQAccel::repairEventFilter() {} +/*! \obsolete serves no purpose anymore */ +bool TQAccel::eventFilter( TQObject *, TQEvent * ) { return FALSE; } +#endif // QT_NO_ACCEL diff --git a/src/kernel/qaccel.h b/src/kernel/qaccel.h new file mode 100644 index 000000000..1ada6ec05 --- /dev/null +++ b/src/kernel/qaccel.h @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Definition of TQAccel class +** +** Created : 950419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQACCEL_H +#define TQACCEL_H + +#ifndef QT_H +#include "qobject.h" +#include "qkeysequence.h" +#endif // QT_H + +#ifndef QT_NO_ACCEL + +class TQAccelPrivate; + +class Q_EXPORT TQAccel : public TQObject // accelerator class +{ + Q_OBJECT +public: + TQAccel( TQWidget *parent, const char *name=0 ); + TQAccel( TQWidget* watch, TQObject *parent, const char *name=0 ); + ~TQAccel(); + + bool isEnabled() const; + void setEnabled( bool ); + + uint count() const; + + int insertItem( const TQKeySequence& key, int id=-1); + void removeItem( int id ); + void clear(); + + TQKeySequence key( int id ); + int findKey( const TQKeySequence& key ) const; + + bool isItemEnabled( int id ) const; + void setItemEnabled( int id, bool enable ); + + bool connectItem( int id, const TQObject *receiver, const char* member ); + bool disconnectItem( int id, const TQObject *receiver, const char* member ); + + void repairEventFilter(); + + void setWhatsThis( int id, const TQString& ); + TQString whatsThis( int id ) const; + void setIgnoreWhatsThis( bool ); + bool ignoreWhatsThis() const; + + static TQKeySequence shortcutKey( const TQString & ); + static TQString keyToString(TQKeySequence k ); + static TQKeySequence stringToKey( const TQString & ); + +signals: + void activated( int id ); + void activatedAmbiguously( int id ); + +protected: + bool eventFilter( TQObject *, TQEvent * ); + +private: + TQAccelPrivate * d; + +private: +#if defined(Q_DISABLE_COPY) + TQAccel( const TQAccel & ); + TQAccel &operator=( const TQAccel & ); +#endif + friend class TQAccelPrivate; + friend class TQAccelManager; +}; + +#endif // QT_NO_ACCEL +#endif // TQACCEL_H diff --git a/src/kernel/qaccessible.cpp b/src/kernel/qaccessible.cpp new file mode 100644 index 000000000..4c6b5cc23 --- /dev/null +++ b/src/kernel/qaccessible.cpp @@ -0,0 +1,719 @@ +/**************************************************************************** +** +** Implementation of TQAccessible and TQAccessibleObject classes +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qaccessible.h" + +#if defined(QT_ACCESSIBILITY_SUPPORT) + +#include "qptrdict.h" +#include "qmetaobject.h" +#include +#include "qapplication.h" +#include + +/*! + \class TQAccessible qaccessible.h + \brief The TQAccessible class provides enums and static functions + relating to accessibility. + + \ingroup misc + + Accessibility clients use implementations of the + TQAccessibleInterface to read the information an accessible object + exposes, or to call functions to manipulate the accessible object. + +\omit + TQt provides implementations of the TQAccessibleInterface for most + widget classes in a plugin. This plugin is located in the \e + accessibility subdirectory of the plugins installation directory. + The default installation directory for plugins is \c INSTALL/plugins, + where \c INSTALL is the directory where TQt was installed. Calling + queryAccessibleInterface( TQObject *object, TQAccessibleInterface + **iface ) will ask all plugins located in this directory for an + implementation that exposes the information for objects of the + class of \e object. + + To make a TQt application accessible you have to distribute the + accessibility plugin provded with TQt together with your + application. Simply add the plugins created in + INSTALL/plugins/accessibility to your distribution process. Use \l + TQApplication::addLibraryPath() to specify a plugin directory for + your application, and copy the files into an \e accessibility + subdirectory of one of those plugin directories. TQt's + accessibility framework will load the plugins upon request and use + the implementations provided to expose an object's accessibility + information. +\endomit + + See the \link plugins-howto.html plugin documentation \endlink for + more details about how to redistribute TQt plugins. +*/ + +/*! + \enum TQAccessible::State + + This enum type defines bitflags that can be combined to indicate + the state of the accessible object. The values are: + + \value Normal + \value Unavailable + \value Selected + \value Focused + \value Pressed + \value Checked + \value Mixed + \value ReadOnly + \value HotTracked + \value Default + \value Expanded + \value Collapsed + \value Busy + \value Floating + \value Marqueed + \value Animated + \value Invisible + \value Offscreen + \value Sizeable + \value Moveable + \value SelfVoicing + \value Focusable + \value Selectable + \value Linked + \value Traversed + \value MultiSelectable + \value ExtSelectable + \value AlertLow + \value AlertMedium + \value AlertHigh + \value Protected + \value Valid +*/ + +/*! + \enum TQAccessible::Event + + This enum type defines event types when the state of the + accessible object has changed. The event types are: + + \value SoundPlayed + \value Alert + \value ForegroundChanged + \value MenuStart + \value MenuEnd + \value PopupMenuStart + \value PopupMenuEnd + \value ContextHelpStart + \value ContextHelpEnd + \value DragDropStart + \value DragDropEnd + \value DialogStart + \value DialogEnd + \value ScrollingStart + \value ScrollingEnd + \value ObjectCreated + \value ObjectDestroyed + \value ObjectShow + \value ObjectHide + \value ObjectReorder + \value Focus + \value Selection + \value SelectionAdd + \value SelectionRemove + \value SelectionWithin + \value StateChanged + \value LocationChanged + \value NameChanged + \value DescriptionChanged + \value ValueChanged + \value ParentChanged + \value HelpChanged + \value DefaultActionChanged + \value AcceleratorChanged + \value MenuCommand +*/ + +/*! + \enum TQAccessible::Role + + This enum defines a number of roles an accessible object can have. + The roles are: + + \value NoRole + \value TitleBar + \value MenuBar + \value ScrollBar + \value Grip + \value Sound + \value Cursor + \value Caret + \value AlertMessage + \value Window + \value Client + \value PopupMenu + \value MenuItem + \value ToolTip + \value Application + \value Document + \value Pane + \value Chart + \value Dialog + \value Border + \value Grouping + \value Separator + \value ToolBar + \value StatusBar + \value Table + \value ColumnHeader + \value RowHeader + \value Column + \value Row + \value Cell + \value Link + \value HelpBalloon + \value Character + \value List + \value ListItem + \value Outline + \value OutlineItem + \value PageTab + \value PropertyPage + \value Indicator + \value Graphic + \value StaticText + \value EditableText + \value PushButton + \value CheckBox + \value RadioButton + \value ComboBox + \value DropLest + \value ProgressBar + \value Dial + \value HotkeyField + \value Slider + \value SpinBox + \value Diagram + \value Animation + \value Equation + \value ButtonDropDown + \value ButtonMenu + \value ButtonDropGrid + \value Whitespace + \value PageTabList + \value Clock +*/ + +/*! + \enum TQAccessible::NavDirection + + This enum specifies which item to move to when navigating. + + \value NavUp sibling above + \value NavDown sibling below + \value NavLeft left sibling + \value NavRight right sibling + \value NavNext next sibling + \value NavPrevious previous sibling + \value NavFirstChild first child + \value NavLastChild last child + \value NavFocusChild child with focus +*/ + +/*! + \enum TQAccessible::Text + + This enum specifies string information that an accessible object + returns. + + \value Name The name of the object + \value Description A short text describing the object + \value Value The value of the object + \value Help A longer text giving information about how + to use the object + \value DefaultAction The default method to interact with the object + \value Accelerator The keyboard shortcut that executes the + default action +*/ + +/*! + \fn static void TQAccessible::updateAccessibility( TQObject *object, int control, Event reason ) + + Notifies accessibility clients about a change in \a object's + accessibility information. + + \a reason specifies the cause of the change, for example, + ValueChange when the position of a slider has been changed. \a + control is the ID of the child element that has changed. When \a + control is 0, the object itself has changed. + + Call this function whenever the state of your accessible object or + one of it's sub-elements has been changed either programmatically + (e.g. by calling TQLabel::setText()) or by user interaction. + + If there are no accessibility tools listening to this event, the + performance penalty for calling this function is minor, but if determining + the parameters of the call is expensive you can use isActive() to + avoid unnecessary performance penalties if no client is listening. +*/ + +static TQPluginManager *qAccessibleManager = 0; + +class AccessibleCache : public TQObject, public TQPtrDict +{ + Q_OBJECT +public: + AccessibleCache() + : TQPtrDict(73) + { + } + + void addObject(TQObject *object, TQAccessibleInterface *iface) + { + insert(object, iface); + connect(object, SIGNAL(destroyed(TQObject*)), this, SLOT(removeObject(TQObject*))); + } + +public slots: + void removeObject(TQObject *object); +}; + +#include "qaccessible.moc" + +static AccessibleCache *qAccessibleInterface = 0; +static bool cleanupAdded = FALSE; + +static void qAccessibleCleanup() +{ + if ( qAccessibleInterface && qAccessibleInterface->count() && qAccessibleManager ) + qAccessibleManager->setAutoUnload( FALSE ); + + delete qAccessibleInterface; + qAccessibleInterface = 0; + delete qAccessibleManager; + qAccessibleManager = 0; +} + +#ifdef Q_WS_MAC +TQObject *TQAccessible::queryAccessibleObject(TQAccessibleInterface *o) +{ + if(qAccessibleInterface) { + for(TQPtrDictIterator it(*qAccessibleInterface); it.current(); ++it) { + if(it.current() == o) + return (TQObject*)it.currentKey(); + } + } + return NULL; +} +#endif + +void AccessibleCache::removeObject(TQObject *object) +{ + if (!object) + return; + + remove(object); + if (!count()) { + delete this; + qAccessibleInterface = 0; + } +} + + +/*! + Sets \a iface to point to the implementation of the + TQAccessibleInterface for \a object, and returns \c TQS_OK if + successfull, or sets \a iface to 0 and returns \c TQE_NOCOMPONENT if + no accessibility implementation for \a object exists. + + The function uses the \link TQObject::className() classname + \endlink of \a object to find a suitable implementation. If no + implementation for the object's class is available the function + tries to find an implementation for the object's parent class. + + This function is called to answer an accessibility client's + request for object information. You should never need to call this + function yourself. +*/ +TQRESULT TQAccessible::queryAccessibleInterface( TQObject *object, TQAccessibleInterface **iface ) +{ + *iface = 0; + if ( !object ) + return TQE_INVALIDARG; + + if ( qAccessibleInterface ) { + *iface = qAccessibleInterface->find( object ); + if ( *iface ) { + (*iface)->addRef(); + return TQS_OK; + } + } + + if ( !qAccessibleManager ) { + qAccessibleManager = new TQPluginManager( IID_QAccessibleFactory, TQApplication::libraryPaths(), "/accessible" ); + if ( !cleanupAdded ) { + qAddPostRoutine( qAccessibleCleanup ); + cleanupAdded = TRUE; + } + } + + TQInterfacePtr factory = 0; + TQMetaObject *mo = object->metaObject(); + while ( mo ) { + qAccessibleManager->queryInterface( mo->className(), &factory ); + if ( factory ) + break; + mo = mo->superClass(); + } + if ( factory ) + return factory->createAccessibleInterface( mo->className(), object, iface ); + + return TQE_NOCOMPONENT; +} + +/*! + Returns TRUE if an accessibility implementation has been requested, + during the runtime of the application, otherwise returns FALSE. + + Use this function to prevent potentially expensive notifications via + updateAccessibility(). + + \omit + TQListView uses this function to prevent index-lookups for item based + notifications. + \endomit +*/ +bool TQAccessible::isActive() +{ + return qAccessibleManager != 0; +} + +/*! + \class TQAccessibleInterface qaccessible.h + \brief The TQAccessibleInterface class defines an interface that exposes information about accessible objects. + + \ingroup misc +*/ + +/*! + \fn bool TQAccessibleInterface::isValid() const + + Returns TRUE if all the data necessary to use this interface + implementation is valid (e.g. all pointers are non-null), + otherwise returns FALSE. +*/ + +/*! + \fn int TQAccessibleInterface::childCount() const + + Returns the number of children that belong to this object. A child + can provide accessibility information on it's own (e.g. a child + widget), or be a sub-element of this accessible object. + + All objects provide this information. + + \sa queryChild() +*/ + +/*! + \fn TQRESULT TQAccessibleInterface::queryChild( int control, TQAccessibleInterface **iface ) const + + Sets \a iface to point to the implementation of the + TQAccessibleInterface for the child specified with \a control. If + the child doesn't provide accessibility information on it's own, + the value of \a iface is set to 0. For those elements, this + object is responsible for exposing the child's properties. + + All objects provide this information. + + \sa childCount(), queryParent() +*/ + +/*! + \fn TQRESULT TQAccessibleInterface::queryParent( TQAccessibleInterface **iface ) const + + Sets \a iface to point to the implementation of the + TQAccessibleInterface for the parent object, or to 0 if there is + no such implementation or object. + + All objects provide this information. + + \sa queryChild() +*/ + +/*! + \fn int TQAccessibleInterface::controlAt( int x, int y ) const + + Returns the ID of the child that contains the screen coordinates + (\a x, \a y). This function returns 0 if the point is positioned + on the object itself. If the tested point is outside the + boundaries of the object this function returns -1. + + All visual objects provide this information. +*/ + +/*! + \fn TQRect TQAccessibleInterface::rect( int control ) const + + Returns the location of the child specified with \a control in + screen coordinates. This function returns the location of the + object itself if \a control is 0. + + All visual objects provide this information. +*/ + +/*! + \fn int TQAccessibleInterface::navigate( NavDirection direction, int startControl ) const + + This function traverses to another object, or to a sub-element of + the current object. \a direction specifies in which direction to + navigate, and \a startControl specifies the start point of the + navigation, which is either 0 if the navigation starts at the + object itself, or an ID of one of the object's sub-elements. + + The function returns the ID of the sub-element located in the \a + direction specified. If there is nothing in the navigated \a + direction, this function returns -1. + + All objects support navigation. +*/ + +/*! + \fn TQString TQAccessibleInterface::text( Text t, int control ) const + + Returns a string property \a t of the child object specified by \a + control, or the string property of the object itself if \a control + is 0. + + The \e Name is a string used by clients to identify, find or + announce an accessible object for the user. All objects must have + a name that is unique within their container. + + An accessible object's \e Description provides textual information + about an object's visual appearance. The description is primarily + used to provide greater context for low-vision or blind users, but + is also used for context searching or other applications. Not all + objects have a description. An "OK" button would not need a + description, but a toolbutton that shows a picture of a smiley + would. + + The \e Value of an accessible object represents visual information + contained by the object, e.g. the text in a line edit. Usually, + the value can be modified by the user. Not all objects have a + value, e.g. static text labels don't, and some objects have a + state that already is the value, e.g. toggle buttons. + + The \e Help text provides information about the function and + usage of an accessible object. Not all objects provide this + information. + + An accessible object's \e DefaultAction describes the object's + primary method of manipulation, and should be a verb or a short + phrase, e.g. "Press" for a button. + + The accelerator is a keyboard shortcut that activates the default + action of the object. A keyboard shortcut is the underlined + character in the text of a menu, menu item or control, and is + either the character itself, or a combination of this character + and a modifier key like ALT, CTRL or SHIFT. Command controls like + tool buttons also have shortcut keys and usually display them in + their tooltip. + + \sa role(), state(), selection() +*/ + +/*! + \fn void TQAccessibleInterface::setText( Text t, int control, const TQString &text ) + + Sets the text property \a t of the child object \a control to \a + text. If \a control is 0, the text property of the object itself + is set. +*/ + +/*! + \fn TQAccessible::Role TQAccessibleInterface::role( int control ) const + + Returns the role of the object if \a control is 0, or the role of + the object's sub-element with ID \a control. The role of an object + is usually static. All accessible objects have a role. + + \sa text(), state(), selection() +*/ + +/*! + \fn TQAccessible::State TQAccessibleInterface::state( int control ) const + + Returns the current state of the object if \a control is 0, or the + state of the object's sub-element element with ID \a control. All + objects have a state. + + \sa text(), role(), selection() +*/ + +/*! + \fn TQMemArray TQAccessibleInterface::selection() const + + Returns the list of all the element IDs that are selected. + + \sa text(), role(), state() +*/ + +/*! + \fn bool TQAccessibleInterface::doDefaultAction( int control ) + + Calling this function performs the default action of the child + object specified by \a control, or the default action of the + object itself if \a control is 0. +*/ + +/*! + \fn bool TQAccessibleInterface::setFocus( int control ) + + Gives the focus to the child object specified by \a control, or to + the object itself if \a control is 0. + + Returns TRUE if the focus could be set; otherwise returns FALSE. +*/ + +/*! + \fn bool TQAccessibleInterface::setSelected( int control, bool on, bool extend ) + + Sets the selection of the child object with ID \a control to \a + on. If \a extend is TRUE, all child elements between the focused + item and the specified child object have their selection set to \a + on. + + Returns TRUE if the selection could be set; otherwise returns + FALSE. + + \sa setFocus(), clearSelection() +*/ + +/*! + \fn void TQAccessibleInterface::clearSelection() + + Removes any selection from the object. + + \sa setSelected() +*/ + + + +/*! + \class TQAccessibleObject qaccessible.h + \brief The TQAccessibleObject class implements parts of the + TQAccessibleInterface for TQObjects. + + \ingroup misc + + This class is mainly provided for convenience. All subclasses of + the TQAccessibleInterface should use this class as the base class. +*/ + +/*! + Creates a TQAccessibleObject for \a object. +*/ +TQAccessibleObject::TQAccessibleObject( TQObject *object ) +: object_(object) +{ + if ( !qAccessibleInterface ) { + qAccessibleInterface = new AccessibleCache; + if ( !cleanupAdded ) { + qAddPostRoutine( qAccessibleCleanup ); + cleanupAdded = TRUE; + } + } + + qAccessibleInterface->addObject(object, this); +} + +/*! + Destroys the TQAccessibleObject. + + This only happens when a call to release() decrements the internal + reference counter to zero. +*/ +TQAccessibleObject::~TQAccessibleObject() +{ + if ( qAccessibleInterface ) { + qAccessibleInterface->removeObject(object_); + if ( !qAccessibleInterface->count() ) { + delete qAccessibleInterface; + qAccessibleInterface = 0; + } + } +} + +/*! + \reimp +*/ +TQRESULT TQAccessibleObject::queryInterface( const TQUuid &uuid, TQUnknownInterface **iface ) +{ + *iface = 0; + if ( uuid == IID_QAccessible ) + *iface = (TQAccessibleInterface*)this; + else if ( uuid == IID_QUnknown ) + *iface = (TQUnknownInterface*)this; + else + return TQE_NOINTERFACE; + + (*iface)->addRef(); + return TQS_OK; +} + +/*! + Returns the TQObject for which this TQAccessibleInterface + implementation provides information. Use isValid() to make sure + the object pointer is safe to use. +*/ +TQObject *TQAccessibleObject::object() const +{ +#if defined(QT_CHECK_RANGE) + if ( !isValid() ) + qWarning( "TQAccessibleInterface is invalid. Crash pending..." ); +#endif + return object_; +} + +/*! + \reimp +*/ +bool TQAccessibleObject::isValid() const +{ + return !object_.isNull(); +} + +#endif diff --git a/src/kernel/qaccessible.h b/src/kernel/qaccessible.h new file mode 100644 index 000000000..1b412c762 --- /dev/null +++ b/src/kernel/qaccessible.h @@ -0,0 +1,295 @@ +/**************************************************************************** +** +** Definition of TQAccessible and TQAccessibleObject classes +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQACCESSIBLE_H +#define TQACCESSIBLE_H + +#ifndef QT_H +#include "qobject.h" +#include +#include "qrect.h" +#include "qguardedptr.h" +#include "qmemarray.h" +#endif // QT_H + +#if defined(QT_ACCESSIBILITY_SUPPORT) + +struct TQAccessibleInterface; + +class Q_EXPORT TQAccessible +{ +private: +#ifdef Q_WS_MAC + static TQMAC_PASCAL OSStatus globalEventProcessor(EventHandlerCallRef, EventRef, void *); + static TQObject *queryAccessibleObject(TQAccessibleInterface *); +#endif +public: + enum Event { + SoundPlayed = 0x0001, + Alert = 0x0002, + ForegroundChanged = 0x0003, + MenuStart = 0x0004, + MenuEnd = 0x0005, + PopupMenuStart = 0x0006, + PopupMenuEnd = 0x0007, + ContextHelpStart = 0x000C, + ContextHelpEnd = 0x000D, + DragDropStart = 0x000E, + DragDropEnd = 0x000F, + DialogStart = 0x0010, + DialogEnd = 0x0011, + ScrollingStart = 0x0012, + ScrollingEnd = 0x0013, + + MenuCommand = 0x0018, + + ObjectCreated = 0x8000, + ObjectDestroyed = 0x8001, + ObjectShow = 0x8002, + ObjectHide = 0x8003, + ObjectReorder = 0x8004, + Focus = 0x8005, + Selection = 0x8006, + SelectionAdd = 0x8007, + SelectionRemove = 0x8008, + SelectionWithin = 0x8009, + StateChanged = 0x800A, + LocationChanged = 0x800B, + NameChanged = 0x800C, + DescriptionChanged = 0x800D, + ValueChanged = 0x800E, + ParentChanged = 0x800F, + HelpChanged = 0x80A0, + DefaultActionChanged= 0x80B0, + AcceleratorChanged = 0x80C0 + }; + + enum State { + Normal = 0x00000000, + Unavailable = 0x00000001, + Selected = 0x00000002, + Focused = 0x00000004, + Pressed = 0x00000008, + Checked = 0x00000010, + Mixed = 0x00000020, + ReadOnly = 0x00000040, + HotTracked = 0x00000080, + Default = 0x00000100, + Expanded = 0x00000200, + Collapsed = 0x00000400, + Busy = 0x00000800, + Floating = 0x00001000, + Marqueed = 0x00002000, + Animated = 0x00004000, + Invisible = 0x00008000, + Offscreen = 0x00010000, + Sizeable = 0x00020000, + Moveable = 0x00040000, + SelfVoicing = 0x00080000, + Focusable = 0x00100000, + Selectable = 0x00200000, + Linked = 0x00400000, + Traversed = 0x00800000, + MultiSelectable = 0x01000000, + ExtSelectable = 0x02000000, + AlertLow = 0x04000000, + AlertMedium = 0x08000000, + AlertHigh = 0x10000000, + Protected = 0x20000000, + Valid = 0x3fffffff + }; + + enum Role { + NoRole = 0x00000000, + TitleBar = 0x00000001, + MenuBar = 0x00000002, + ScrollBar = 0x00000003, + Grip = 0x00000004, + Sound = 0x00000005, + Cursor = 0x00000006, + Caret = 0x00000007, + AlertMessage = 0x00000008, + Window = 0x00000009, + Client = 0x0000000A, + PopupMenu = 0x0000000B, + MenuItem = 0x0000000C, + ToolTip = 0x0000000D, + Application = 0x0000000E, + Document = 0x0000000F, + Pane = 0x00000010, + Chart = 0x00000011, + Dialog = 0x00000012, + Border = 0x00000013, + Grouping = 0x00000014, + Separator = 0x00000015, + ToolBar = 0x00000016, + StatusBar = 0x00000017, + Table = 0x00000018, + ColumnHeader = 0x00000019, + RowHeader = 0x0000001A, + Column = 0x0000001B, + Row = 0x0000001C, + Cell = 0x0000001D, + Link = 0x0000001E, + HelpBalloon = 0x0000001F, + Character = 0x00000020, + List = 0x00000021, + ListItem = 0x00000022, + Outline = 0x00000023, + OutlineItem = 0x00000024, + PageTab = 0x00000025, + PropertyPage = 0x00000026, + Indicator = 0x00000027, + Graphic = 0x00000028, + StaticText = 0x00000029, + EditableText = 0x0000002A, // Editable, selectable, etc. + PushButton = 0x0000002B, + CheckBox = 0x0000002C, + RadioButton = 0x0000002D, + ComboBox = 0x0000002E, + DropLest = 0x0000002F, + ProgressBar = 0x00000030, + Dial = 0x00000031, + HotkeyField = 0x00000032, + Slider = 0x00000033, + SpinBox = 0x00000034, + Diagram = 0x00000035, + Animation = 0x00000036, + Equation = 0x00000037, + ButtonDropDown = 0x00000038, + ButtonMenu = 0x00000039, + ButtonDropGrid = 0x0000003A, + Whitespace = 0x0000003B, + PageTabList = 0x0000003C, + Clock = 0x0000003D + }; + + enum NavDirection { + NavUp = 0x00000001, + NavDown = 0x00000002, + NavLeft = 0x00000003, + NavRight = 0x00000004, + NavNext = 0x00000005, + NavPrevious = 0x00000006, + NavFirstChild = 0x00000007, + NavLastChild = 0x00000008, + NavFocusChild = 0x00000009 + }; + + enum Text { + Name = 0, + Description, + Value, + Help, + Accelerator, + DefaultAction + }; + + static TQRESULT queryAccessibleInterface( TQObject *, TQAccessibleInterface ** ); + static void updateAccessibility( TQObject *, int who, Event reason ); + static bool isActive(); + + static void initialize(); + static void cleanup(); +}; + +// {EC86CB9C-5DA0-4c43-A739-13EBDF1C6B14} +#define IID_QAccessible TQUuid( 0xec86cb9c, 0x5da0, 0x4c43, 0xa7, 0x39, 0x13, 0xeb, 0xdf, 0x1c, 0x6b, 0x14 ) + +struct Q_EXPORT TQAccessibleInterface : public TQAccessible, public TQUnknownInterface +{ + // check for valid pointers + virtual bool isValid() const = 0; + + // hierarchy + virtual int childCount() const = 0; + virtual TQRESULT queryChild( int control, TQAccessibleInterface** ) const = 0; + virtual TQRESULT queryParent( TQAccessibleInterface** ) const = 0; + + // navigation + virtual int controlAt( int x, int y ) const = 0; + virtual TQRect rect( int control ) const = 0; + virtual int navigate( NavDirection direction, int startControl ) const = 0; + + // properties and state + virtual TQString text( Text t, int control ) const = 0; + virtual void setText( Text t, int control, const TQString &text ) = 0; + virtual Role role( int control ) const = 0; + virtual State state( int control ) const = 0; + virtual TQMemArray selection() const = 0; + + // methods + virtual bool doDefaultAction( int control ) = 0; + virtual bool setFocus( int control ) = 0; + virtual bool setSelected( int control, bool on, bool extend ) = 0; + virtual void clearSelection() = 0; +}; + +// {49F4C6A7-412F-41DE-9E24-648843421FD3} +#ifndef IID_QAccessibleFactory +#define IID_QAccessibleFactory TQUuid( 0x49f4c6a7, 0x412f, 0x41de, 0x9e, 0x24, 0x64, 0x88, 0x43, 0x42, 0x1f, 0xd3 ) +#endif + +struct Q_EXPORT TQAccessibleFactoryInterface : public TQAccessible, public TQFeatureListInterface +{ + virtual TQRESULT createAccessibleInterface( const TQString &, TQObject *, TQAccessibleInterface** ) = 0; +}; + +class Q_EXPORT TQAccessibleObject : public TQObject, public TQAccessibleInterface +{ +public: + TQAccessibleObject( TQObject *object ); + virtual ~TQAccessibleObject(); + + TQRESULT queryInterface( const TQUuid &, TQUnknownInterface** ); + Q_REFCOUNT + + bool isValid() const; + +protected: + TQObject *object() const; + +private: + TQGuardedPtr object_; +}; + +#define Q_DEFINED_QACCESSIBLE_OBJECT +#include "qwinexport.h" +#endif //QT_ACCESSIBILITY_SUPPORT + +#endif //TQACCESSIBLE_H diff --git a/src/kernel/qapplication.cpp b/src/kernel/qapplication.cpp new file mode 100644 index 000000000..437361f88 --- /dev/null +++ b/src/kernel/qapplication.cpp @@ -0,0 +1,4602 @@ +/**************************************************************************** +** +** Implementation of TQApplication class +** +** Created : 931107 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qobjectlist.h" +#include "qapplication.h" +#include "qeventloop.h" +#include "qeventloop_p.h" +#include "qwidget.h" +#include "qwidgetlist.h" +#include "qwidgetintdict.h" +#include "qptrdict.h" +#include "qcleanuphandler.h" + +#include "qtranslator.h" +#include "qtextcodec.h" +#include "qsessionmanager.h" +#include "qdragobject.h" +#include "qclipboard.h" +#include "qcursor.h" +#include "qstyle.h" +#include "qstylefactory.h" +#include "qfile.h" +#include "qmessagebox.h" +#include "qdir.h" +#include "qfileinfo.h" +#ifdef Q_WS_WIN +#include "qinputcontext_p.h" +#endif +#include "qfontdata_p.h" + +#if defined(QT_THREAD_SUPPORT) +# include "qmutex.h" +# include "qthread.h" +#endif // QT_THREAD_SUPPORT + +#include + +#ifdef truncate +# undef truncate +#endif + +/*! + \class TQApplication qapplication.h + \brief The TQApplication class manages the GUI application's control + flow and main settings. + + \ingroup application + \mainclass + + It contains the main event loop, where all events from the window + system and other sources are processed and dispatched. It also + handles the application's initialization and finalization, and + provides session management. It also handles most system-wide and + application-wide settings. + + For any GUI application that uses TQt, there is precisely one + TQApplication object, no matter whether the application has 0, 1, 2 + or more windows at any time. + + The TQApplication object is accessible through the global pointer \c + qApp. Its main areas of responsibility are: + \list + + \i It initializes the application with the user's desktop settings + such as palette(), font() and doubleClickInterval(). It keeps track + of these properties in case the user changes the desktop globally, for + example through some kind of control panel. + + \i It performs event handling, meaning that it receives events + from the underlying window system and dispatches them to the relevant + widgets. By using sendEvent() and postEvent() you can send your own + events to widgets. + + \i It parses common command line arguments and sets its internal + state accordingly. See the \link TQApplication::TQApplication() + constructor documentation\endlink below for more details about this. + + \i It defines the application's look and feel, which is + encapsulated in a TQStyle object. This can be changed at runtime + with setStyle(). + + \i It specifies how the application is to allocate colors. + See setColorSpec() for details. + + \i It provides localization of strings that are visible to the user + via translate(). + + \i It provides some magical objects like the desktop() and the + clipboard(). + + \i It knows about the application's windows. You can ask which + widget is at a certain position using widgetAt(), get a list of + topLevelWidgets() and closeAllWindows(), etc. + + \i It manages the application's mouse cursor handling, + see setOverrideCursor() and setGlobalMouseTracking(). + + \i On the X window system, it provides functions to flush and sync + the communication stream, see flushX() and syncX(). + + \i It provides support for sophisticated \link + session.html session management \endlink. This makes it possible + for applications to terminate gracefully when the user logs out, to + cancel a shutdown process if termination isn't possible and even to + preserve the entire application's state for a future session. See + isSessionRestored(), sessionId() and commitData() and saveState() + for details. + + \endlist + + The Application walk-through + example contains a typical complete main() that does the usual + things with TQApplication. + + Since the TQApplication object does so much initialization, it + must be created before any other objects related to the user + interface are created. + + Since it also deals with common command line arguments, it is + usually a good idea to create it \e before any interpretation or + modification of \c argv is done in the application itself. (Note + also that for X11, setMainWidget() may change the main widget + according to the \c -geometry option. To preserve this + functionality, you must set your defaults before setMainWidget() and + any overrides after.) + + \table + \header \i21 Groups of functions + \row + \i System settings + \i + desktopSettingsAware(), + setDesktopSettingsAware(), + cursorFlashTime(), + setCursorFlashTime(), + doubleClickInterval(), + setDoubleClickInterval(), + wheelScrollLines(), + setWheelScrollLines(), + palette(), + setPalette(), + font(), + setFont(), + fontMetrics(). + + \row + \i Event handling + \i + exec(), + processEvents(), + enter_loop(), + exit_loop(), + exit(), + tquit(). + sendEvent(), + postEvent(), + sendPostedEvents(), + removePostedEvents(), + hasPendingEvents(), + notify(), + macEventFilter(), + qwsEventFilter(), + x11EventFilter(), + x11ProcessEvent(), + winEventFilter(). + + \row + \i GUI Styles + \i + style(), + setStyle(), + polish(). + + \row + \i Color usage + \i + colorSpec(), + setColorSpec(), + qwsSetCustomColors(). + + \row + \i Text handling + \i + installTranslator(), + removeTranslator() + translate(). + + \row + \i Widgets + \i + mainWidget(), + setMainWidget(), + allWidgets(), + topLevelWidgets(), + desktop(), + activePopupWidget(), + activeModalWidget(), + clipboard(), + focusWidget(), + winFocus(), + activeWindow(), + widgetAt(). + + \row + \i Advanced cursor handling + \i + hasGlobalMouseTracking(), + setGlobalMouseTracking(), + overrideCursor(), + setOverrideCursor(), + restoreOverrideCursor(). + + \row + \i X Window System synchronization + \i + flushX(), + syncX(). + + \row + \i Session management + \i + isSessionRestored(), + sessionId(), + commitData(), + saveState(). + + \row + \i Threading + \i + lock(), unlock(), locked(), tryLock(), + wakeUpGuiThread() + + \row + \i Miscellaneous + \i + closeAllWindows(), + startingUp(), + closingDown(), + type(). + \endtable + + \e {Non-GUI programs:} While TQt is not optimized or + designed for writing non-GUI programs, it's possible to use + \link tools.html some of its classes \endlink without creating a + TQApplication. This can be useful if you wish to share code between + a non-GUI server and a GUI client. + + \headerfile qnamespace.h + \headerfile qwindowdefs.h + \headerfile qglobal.h +*/ + +/*! \enum TQt::HANDLE + \internal +*/ + +/*! + \enum TQApplication::Type + + \value Tty a console application + \value GuiClient a GUI client application + \value GuiServer a GUI server application +*/ + +/*! + \enum TQApplication::ColorSpec + + \value NormalColor the default color allocation policy + \value CustomColor the same as NormalColor for X11; allocates colors + to a palette on demand under Windows + \value ManyColor the right choice for applications that use thousands of + colors + + See setColorSpec() for full details. +*/ + +/* + The qt_init() and qt_cleanup() functions are implemented in the + qapplication_xyz.cpp file. +*/ + +void qt_init( int *, char **, TQApplication::Type ); +void qt_cleanup(); +#if defined(Q_WS_X11) +void qt_init( Display* dpy, TQt::HANDLE, TQt::HANDLE ); +#endif +Q_EXPORT bool qt_tryModalHelper( TQWidget *widget, TQWidget **rettop ); + +TQApplication *qApp = 0; // global application object + +TQStyle *TQApplication::app_style = 0; // default application style +bool qt_explicit_app_style = FALSE; // style explicitly set by programmer + +int TQApplication::app_cspec = TQApplication::NormalColor; +#ifndef QT_NO_PALETTE +TQPalette *TQApplication::app_pal = 0; // default application palette +#endif +TQFont *TQApplication::app_font = 0; // default application font +bool qt_app_has_font = FALSE; +#ifndef QT_NO_CURSOR +TQCursor *TQApplication::app_cursor = 0; // default application cursor +#endif +int TQApplication::app_tracking = 0; // global mouse tracking +bool TQApplication::is_app_running = FALSE; // app starting up if FALSE +bool TQApplication::is_app_closing = FALSE; // app closing down if TRUE +int TQApplication::loop_level = 0; // event loop level +TQWidget *TQApplication::main_widget = 0; // main application widget +TQWidget *TQApplication::focus_widget = 0; // has keyboard input focus +TQWidget *TQApplication::active_window = 0; // toplevel with keyboard focus +bool TQApplication::obey_desktop_settings = TRUE; // use winsys resources +int TQApplication::cursor_flash_time = 1000; // text caret flash time +int TQApplication::mouse_double_click_time = 400; // mouse dbl click limit +#ifndef QT_NO_WHEELEVENT +int TQApplication::wheel_scroll_lines = 3; // number of lines to scroll +#endif +bool qt_is_gui_used; +bool Q_EXPORT qt_resolve_symlinks = TRUE; +bool Q_EXPORT qt_tab_all_widgets = TRUE; +TQRect qt_maxWindowRect; +static int drag_time = 500; +static int drag_distance = 4; +static bool reverse_layout = FALSE; +TQSize TQApplication::app_strut = TQSize( 0,0 ); // no default application strut +bool TQApplication::animate_ui = TRUE; +bool TQApplication::animate_menu = FALSE; +bool TQApplication::fade_menu = FALSE; +bool TQApplication::animate_combo = FALSE; +bool TQApplication::animate_tooltip = FALSE; +bool TQApplication::fade_tooltip = FALSE; +bool TQApplication::animate_toolbox = FALSE; +bool TQApplication::widgetCount = FALSE; +TQApplication::Type qt_appType=TQApplication::Tty; +#ifndef QT_NO_COMPONENT +TQStringList *TQApplication::app_libpaths = 0; +#endif +bool TQApplication::metaComposeUnicode = FALSE; +int TQApplication::composedUnicode = 0; + +#ifdef QT_THREAD_SUPPORT +TQMutex *TQApplication::qt_mutex = 0; +static TQMutex *postevent_mutex = 0; +static TQt::HANDLE qt_application_thread_id = 0; +Q_EXPORT TQt::HANDLE qt_get_application_thread_id() +{ + return qt_application_thread_id; +} +#endif // QT_THREAD_SUPPORT + +TQEventLoop *TQApplication::eventloop = 0; // application event loop + +#ifndef QT_NO_ACCEL +extern bool qt_dispatchAccelEvent( TQWidget*, TQKeyEvent* ); // def in qaccel.cpp +extern bool qt_tryComposeUnicode( TQWidget*, TQKeyEvent* ); // def in qaccel.cpp +#endif + +#if defined(QT_TABLET_SUPPORT) +bool chokeMouse = FALSE; +#endif + +void qt_setMaxWindowRect(const TQRect& r) +{ + qt_maxWindowRect = r; + // Re-resize any maximized windows + TQWidgetList* l = TQApplication::topLevelWidgets(); + if ( l ) { + TQWidget *w = l->first(); + while ( w ) { + if ( w->isVisible() && w->isMaximized() ) + { + w->showNormal(); //#### flicker + w->showMaximized(); + } + w = l->next(); + } + delete l; + } +} + +typedef void (*VFPTR)(); +typedef TQValueList TQVFuncList; +static TQVFuncList *postRList = 0; // list of post routines + +/*! + \relates TQApplication + + Adds a global routine that will be called from the TQApplication + destructor. This function is normally used to add cleanup routines + for program-wide functionality. + + The function given by \a p should take no arguments and return + nothing, like this: + \code + static int *global_ptr = 0; + + static void cleanup_ptr() + { + delete [] global_ptr; + global_ptr = 0; + } + + void init_ptr() + { + global_ptr = new int[100]; // allocate data + qAddPostRoutine( cleanup_ptr ); // delete later + } + \endcode + + Note that for an application- or module-wide cleanup, + qAddPostRoutine() is often not suitable. People have a tendency to + make such modules dynamically loaded, and then unload those modules + long before the TQApplication destructor is called, for example. + + For modules and libraries, using a reference-counted initialization + manager or TQt' parent-child delete mechanism may be better. Here is + an example of a private class which uses the parent-child mechanism + to call a cleanup function at the right time: + + \code + class MyPrivateInitStuff: public TQObject { + private: + MyPrivateInitStuff( TQObject * parent ): TQObject( parent) { + // initialization goes here + } + MyPrivateInitStuff * p; + + public: + static MyPrivateInitStuff * initStuff( TQObject * parent ) { + if ( !p ) + p = new MyPrivateInitStuff( parent ); + return p; + } + + ~MyPrivateInitStuff() { + // cleanup (the "post routine") goes here + } + } + \endcode + + By selecting the right parent widget/object, this can often be made + to clean up the module's data at the exact right moment. +*/ + +Q_EXPORT void qAddPostRoutine( TQtCleanUpFunction p) +{ + if ( !postRList ) { + postRList = new TQVFuncList; + Q_CHECK_PTR( postRList ); + } + postRList->prepend( p ); +} + + +Q_EXPORT void qRemovePostRoutine( TQtCleanUpFunction p ) +{ + if ( !postRList ) return; + TQVFuncList::Iterator it = postRList->begin(); + while ( it != postRList->end() ) { + if ( *it == p ) { + postRList->remove( it ); + it = postRList->begin(); + } else { + ++it; + } + } +} + +// Default application palettes and fonts (per widget type) +TQAsciiDict *TQApplication::app_palettes = 0; +TQAsciiDict *TQApplication::app_fonts = 0; + +#ifndef QT_NO_SESSIONMANAGER +TQString *TQApplication::session_key = 0; // ## session key. Should be a member in 4.0 +#endif +TQWidgetList *TQApplication::popupWidgets = 0; // has keyboard input focus + +TQDesktopWidget *qt_desktopWidget = 0; // root window widgets +#ifndef QT_NO_CLIPBOARD +TQClipboard *qt_clipboard = 0; // global clipboard object +#endif +TQWidgetList * qt_modal_stack=0; // stack of modal widgets + +// Definitions for posted events +struct TQPostEvent { + TQPostEvent( TQObject *r, TQEvent *e ): receiver( r ), event( e ) {} + ~TQPostEvent() { delete event; } + TQObject *receiver; + TQEvent *event; +}; + +class Q_EXPORT TQPostEventList : public TQPtrList +{ +public: + TQPostEventList() : TQPtrList() {} + TQPostEventList( const TQPostEventList &list ) : TQPtrList(list) {} + ~TQPostEventList() { clear(); } + TQPostEventList &operator=(const TQPostEventList &list) + { return (TQPostEventList&)TQPtrList::operator=(list); } +}; +class Q_EXPORT TQPostEventListIt : public TQPtrListIterator +{ +public: + TQPostEventListIt( const TQPostEventList &l ) : TQPtrListIterator(l) {} + TQPostEventListIt &operator=(const TQPostEventListIt &i) +{ return (TQPostEventListIt&)TQPtrListIterator::operator=(i); } +}; + +static TQPostEventList *globalPostedEvents = 0; // list of posted events + +uint qGlobalPostedEventsCount() +{ + if (!globalPostedEvents) + return 0; + return globalPostedEvents->count(); +} + +static TQSingleCleanupHandler qapp_cleanup_events; + +#ifndef QT_NO_PALETTE +TQPalette *qt_std_pal = 0; + +void qt_create_std_palette() +{ + if ( qt_std_pal ) + delete qt_std_pal; + + TQColor standardLightGray( 192, 192, 192 ); + TQColor light( 255, 255, 255 ); + TQColor dark( standardLightGray.dark( 150 ) ); + TQColorGroup std_act( TQt::black, standardLightGray, + light, dark, TQt::gray, + TQt::black, TQt::white ); + TQColorGroup std_dis( TQt::darkGray, standardLightGray, + light, dark, TQt::gray, + TQt::darkGray, std_act.background() ); + TQColorGroup std_inact( TQt::black, standardLightGray, + light, dark, TQt::gray, + TQt::black, TQt::white ); + qt_std_pal = new TQPalette( std_act, std_dis, std_inact ); +} + +static void qt_fix_tooltips() +{ + // No resources for this yet (unlike on Windows). + TQColorGroup cg( TQt::black, TQColor(255,255,220), + TQColor(96,96,96), TQt::black, TQt::black, + TQt::black, TQColor(255,255,220) ); + TQPalette pal( cg, cg, cg ); + TQApplication::setPalette( pal, TRUE, "TQTipLabel"); +} +#endif + +void TQApplication::process_cmdline( int* argcptr, char ** argv ) +{ + // process platform-indep command line + if ( !qt_is_gui_used || !*argcptr) + return; + + int argc = *argcptr; + int i, j; + + j = 1; + for ( i=1; i= 0 ) { + if ( !session_key ) + session_key = new TQString; + *session_key = session_id.mid( p +1 ); + session_id = session_id.left( p ); + } + is_session_restored = TRUE; + } +#endif + } else if ( qstrcmp(arg, "-reverse") == 0 ) { + setReverseLayout( TRUE ); + } else if ( qstrcmp(arg, "-widgetcount") == 0 ) { + widgetCount = TRUE;; + } else { + argv[j++] = argv[i]; + } +#ifndef QT_NO_STYLE + if ( !s.isEmpty() ) { + setStyle( s ); + } +#endif + } + + if(j < argc) { +#ifdef Q_WS_MACX + static char* empty = "\0"; + argv[j] = empty; +#else + argv[j] = 0; +#endif + *argcptr = j; + } +} + +/*! + Initializes the window system and constructs an application object + with \a argc command line arguments in \a argv. + + The global \c qApp pointer refers to this application object. Only + one application object should be created. + + This application object must be constructed before any \link + TQPaintDevice paint devices\endlink (including widgets, pixmaps, bitmaps + etc.). + + Note that \a argc and \a argv might be changed. TQt removes command + line arguments that it recognizes. The modified \a argc and \a argv + can also be accessed later with \c qApp->argc() and \c qApp->argv(). + The documentation for argv() contains a detailed description of how + to process command line arguments. + + TQt debugging options (not available if TQt was compiled with the + QT_NO_DEBUG flag defined): + \list + \i -nograb, tells TQt that it must never grab the mouse or the keyboard. + \i -dograb (only under X11), running under a debugger can cause + an implicit -nograb, use -dograb to override. + \i -sync (only under X11), switches to synchronous mode for + debugging. + \endlist + + See \link debug.html Debugging Techniques \endlink for a more + detailed explanation. + + All TQt programs automatically support the following command line options: + \list + \i -reverse causes text to be formatted for right-to-left languages + rather than in the usual left-to-right direction. + \i -style= \e style, sets the application GUI style. Possible values + are \c motif, \c windows, and \c platinum. If you compiled TQt + with additional styles or have additional styles as plugins these + will be available to the \c -style command line option. + \i -style \e style, is the same as listed above. + \i -session= \e session, restores the application from an earlier + \link session.html session \endlink. + \i -session \e session, is the same as listed above. + \i -widgetcount, prints debug message at the end about number of widgets left + undestroyed and maximum number of widgets existed at the same time + \endlist + + The X11 version of TQt also supports some traditional X11 + command line options: + \list + \i -display \e display, sets the X display (default is $DISPLAY). + \i -geometry \e geometry, sets the client geometry of the + \link setMainWidget() main widget\endlink. + \i -fn or \c -font \e font, defines the application font. The + font should be specified using an X logical font description. + \i -bg or \c -background \e color, sets the default background color + and an application palette (light and dark shades are calculated). + \i -fg or \c -foreground \e color, sets the default foreground color. + \i -btn or \c -button \e color, sets the default button color. + \i -name \e name, sets the application name. + \i -title \e title, sets the application title (caption). + \i -visual \c TrueColor, forces the application to use a TrueColor visual + on an 8-bit display. + \i -ncols \e count, limits the number of colors allocated in the + color cube on an 8-bit display, if the application is using the + \c TQApplication::ManyColor color specification. If \e count is + 216 then a 6x6x6 color cube is used (i.e. 6 levels of red, 6 of green, + and 6 of blue); for other values, a cube + approximately proportional to a 2x3x1 cube is used. + \i -cmap, causes the application to install a private color map + on an 8-bit display. + \endlist + + \sa argc(), argv() +*/ + +//######### BINARY COMPATIBILITY constructor +TQApplication::TQApplication( int &argc, char **argv ) +{ + construct( argc, argv, GuiClient ); +} + + +/*! + Constructs an application object with \a argc command line arguments + in \a argv. If \a GUIenabled is TRUE, a GUI application is + constructed, otherwise a non-GUI (console) application is created. + + Set \a GUIenabled to FALSE for programs without a graphical user + interface that should be able to run without a window system. + + On X11, the window system is initialized if \a GUIenabled is TRUE. + If \a GUIenabled is FALSE, the application does not connect to the + X-server. + On Windows and Macintosh, currently the window system is always + initialized, regardless of the value of GUIenabled. This may change in + future versions of TQt. + + The following example shows how to create an application that + uses a graphical interface when available. + \code + int main( int argc, char **argv ) + { +#ifdef Q_WS_X11 + bool useGUI = getenv( "DISPLAY" ) != 0; +#else + bool useGUI = TRUE; +#endif + TQApplication app(argc, argv, useGUI); + + if ( useGUI ) { + //start GUI version + ... + } else { + //start non-GUI version + ... + } + return app.exec(); + } +\endcode +*/ + +TQApplication::TQApplication( int &argc, char **argv, bool GUIenabled ) +{ + construct( argc, argv, GUIenabled ? GuiClient : Tty ); +} + +/*! + Constructs an application object with \a argc command line arguments + in \a argv. + + For TQt/Embedded, passing \c TQApplication::GuiServer for \a type + makes this application the server (equivalent to running with the + -qws option). +*/ +TQApplication::TQApplication( int &argc, char **argv, Type type ) +{ + construct( argc, argv, type ); +} + +Q_EXPORT void qt_ucm_initialize( TQApplication *theApp ) +{ + if ( qApp ) + return; + int argc = theApp->argc(); + char **argv = theApp->argv(); + theApp->construct( argc, argv, qApp->type() ); + + Q_ASSERT( qApp == theApp ); +} + +void TQApplication::construct( int &argc, char **argv, Type type ) +{ + qt_appType = type; + qt_is_gui_used = (type != Tty); + init_precmdline(); + static const char *empty = ""; + if ( argc == 0 || argv == 0 ) { + argc = 0; + argv = (char **)∅ // ouch! careful with TQApplication::argv()! + } + app_argc = argc; + app_argv = argv; + + qt_init( &argc, argv, type ); // Must be called before initialize() + process_cmdline( &argc, argv ); + initialize( argc, argv ); + if ( qt_is_gui_used ) + qt_maxWindowRect = desktop()->rect(); + if ( eventloop ) + eventloop->appStartingUp(); +} + +/*! + Returns the type of application, Tty, GuiClient or GuiServer. +*/ + +TQApplication::Type TQApplication::type() const +{ + return qt_appType; +} + +#if defined(Q_WS_X11) +/*! + Create an application, given an already open display \a dpy. If \a + visual and \a colormap are non-zero, the application will use those as + the default Visual and Colormap contexts. + + \warning TQt only supports TrueColor visuals at depths higher than 8 + bits-per-pixel. + + This is available only on X11. +*/ + +TQApplication::TQApplication( Display* dpy, HANDLE visual, HANDLE colormap ) +{ + static int aargc = 1; + // ### a string literal is a cont char* + // ### using it as a char* is wrong and could lead to segfaults + // ### if aargv is modified someday + static char *aargv[] = { (char*)"unknown", 0 }; + + app_argc = aargc; + app_argv = aargv; + + qt_appType = GuiClient; + qt_is_gui_used = TRUE; + qt_appType = GuiClient; + init_precmdline(); + // ... no command line. + + if ( ! dpy ) { +#ifdef QT_CHECK_STATE + qWarning( "TQApplication: invalid Display* argument." ); +#endif // QT_CHECK_STATE + + qt_init( &aargc, aargv, GuiClient ); + } else { + qt_init( dpy, visual, colormap ); + } + + initialize( aargc, aargv ); + + if ( qt_is_gui_used ) + qt_maxWindowRect = desktop()->rect(); + if ( eventloop ) + eventloop->appStartingUp(); +} + +/*! + Create an application, given an already open display \a dpy and using + \a argc command line arguments in \a argv. If \a + visual and \a colormap are non-zero, the application will use those as + the default Visual and Colormap contexts. + + \warning TQt only supports TrueColor visuals at depths higher than 8 + bits-per-pixel. + + This is available only on X11. + +*/ +TQApplication::TQApplication(Display *dpy, int argc, char **argv, + HANDLE visual, HANDLE colormap) +{ + qt_appType = GuiClient; + qt_is_gui_used = TRUE; + qt_appType = GuiClient; + init_precmdline(); + + app_argc = argc; + app_argv = argv; + + if ( ! dpy ) { +#ifdef QT_CHECK_STATE + qWarning( "TQApplication: invalid Display* argument." ); +#endif // QT_CHECK_STATE + + qt_init( &argc, argv, GuiClient ); + } else { + qt_init(dpy, visual, colormap); + } + + process_cmdline( &argc, argv ); + initialize(argc, argv); + + if ( qt_is_gui_used ) + qt_maxWindowRect = desktop()->rect(); + if ( eventloop ) + eventloop->appStartingUp(); +} + + +#endif // Q_WS_X11 + + +void TQApplication::init_precmdline() +{ + translators = 0; + is_app_closing = FALSE; +#ifndef QT_NO_SESSIONMANAGER + is_session_restored = FALSE; +#endif +#if defined(QT_CHECK_STATE) + if ( qApp ) + qWarning( "TQApplication: There should be max one application object" ); +#endif + qApp = (TQApplication*)this; +} + +/*! + Initializes the TQApplication object, called from the constructors. +*/ + +void TQApplication::initialize( int argc, char **argv ) +{ +#ifdef QT_THREAD_SUPPORT + qt_mutex = new TQMutex( TRUE ); + postevent_mutex = new TQMutex( TRUE ); + qt_application_thread_id = TQThread::currentThread(); +#endif // QT_THREAD_SUPPORT + + app_argc = argc; + app_argv = argv; + tquit_now = FALSE; + tquit_code = 0; + TQWidget::createMapper(); // create widget mapper +#ifndef QT_NO_PALETTE + (void) palette(); // trigger creation of application palette +#endif + is_app_running = TRUE; // no longer starting up + +#ifndef QT_NO_SESSIONMANAGER + // connect to the session manager + if ( !session_key ) + session_key = new TQString; + session_manager = new TQSessionManager( qApp, session_id, *session_key ); +#endif + +} + + +/***************************************************************************** + Functions returning the active popup and modal widgets. + *****************************************************************************/ + +/*! + Returns the active popup widget. + + A popup widget is a special top level widget that sets the \c + WType_Popup widget flag, e.g. the TQPopupMenu widget. When the + application opens a popup widget, all events are sent to the popup. + Normal widgets and modal widgets cannot be accessed before the popup + widget is closed. + + Only other popup widgets may be opened when a popup widget is shown. + The popup widgets are organized in a stack. This function returns + the active popup widget at the top of the stack. + + \sa activeModalWidget(), topLevelWidgets() +*/ + +TQWidget *TQApplication::activePopupWidget() +{ + return popupWidgets ? popupWidgets->getLast() : 0; +} + + +/*! + Returns the active modal widget. + + A modal widget is a special top level widget which is a subclass of + TQDialog that specifies the modal parameter of the constructor as + TRUE. A modal widget must be closed before the user can continue + with other parts of the program. + + Modal widgets are organized in a stack. This function returns + the active modal widget at the top of the stack. + + \sa activePopupWidget(), topLevelWidgets() +*/ + +TQWidget *TQApplication::activeModalWidget() +{ + return qt_modal_stack ? qt_modal_stack->getFirst() : 0; +} + +/*! + Cleans up any window system resources that were allocated by this + application. Sets the global variable \c qApp to 0. +*/ + +TQApplication::~TQApplication() +{ +#ifndef QT_NO_CLIPBOARD + // flush clipboard contents + if ( qt_clipboard ) { + TQCustomEvent event( TQEvent::Clipboard ); + TQApplication::sendEvent( qt_clipboard, &event ); + } +#endif + + if ( eventloop ) + eventloop->appClosingDown(); + if ( postRList ) { + TQVFuncList::Iterator it = postRList->begin(); + while ( it != postRList->end() ) { // call post routines + (**it)(); + postRList->remove( it ); + it = postRList->begin(); + } + delete postRList; + postRList = 0; + } + + TQObject *tipmanager = child( "toolTipManager", "TQTipManager", FALSE ); + delete tipmanager; + + delete qt_desktopWidget; + qt_desktopWidget = 0; + is_app_closing = TRUE; + +#ifndef QT_NO_CLIPBOARD + delete qt_clipboard; + qt_clipboard = 0; +#endif + TQWidget::destroyMapper(); +#ifndef QT_NO_PALETTE + delete qt_std_pal; + qt_std_pal = 0; + delete app_pal; + app_pal = 0; + delete app_palettes; + app_palettes = 0; +#endif + delete app_font; + app_font = 0; + delete app_fonts; + app_fonts = 0; +#ifndef QT_NO_STYLE + delete app_style; + app_style = 0; +#endif +#ifndef QT_NO_CURSOR + delete app_cursor; + app_cursor = 0; +#endif +#ifndef QT_NO_TRANSLATION + delete translators; +#endif + +#ifndef QT_NO_DRAGANDDROP + extern TQDragManager *qt_dnd_manager; + delete qt_dnd_manager; +#endif + + qt_cleanup(); + +#ifndef QT_NO_COMPONENT + delete app_libpaths; + app_libpaths = 0; +#endif + +#ifdef QT_THREAD_SUPPORT + delete qt_mutex; + qt_mutex = 0; + delete postevent_mutex; + postevent_mutex = 0; +#endif // QT_THREAD_SUPPORT + + if( qApp == this ) { + if ( postedEvents ) + removePostedEvents( this ); + qApp = 0; + } + is_app_running = FALSE; + + if ( widgetCount ) { + qDebug( "Widgets left: %i Max widgets: %i \n", TQWidget::instanceCounter, TQWidget::maxInstances ); + } +#ifndef QT_NO_SESSIONMANAGER + delete session_manager; + session_manager = 0; + delete session_key; + session_key = 0; +#endif //QT_NO_SESSIONMANAGER + + qt_explicit_app_style = FALSE; + qt_app_has_font = FALSE; + app_tracking = 0; + obey_desktop_settings = TRUE; + cursor_flash_time = 1000; + mouse_double_click_time = 400; +#ifndef QT_NO_WHEELEVENT + wheel_scroll_lines = 3; +#endif + drag_time = 500; + drag_distance = 4; + reverse_layout = FALSE; + app_strut = TQSize( 0, 0 ); + animate_ui = TRUE; + animate_menu = FALSE; + fade_menu = FALSE; + animate_combo = FALSE; + animate_tooltip = FALSE; + fade_tooltip = FALSE; + widgetCount = FALSE; +} + + +/*! + \fn int TQApplication::argc() const + + Returns the number of command line arguments. + + The documentation for argv() describes how to process command line + arguments. + + \sa argv(), TQApplication::TQApplication() +*/ + +/*! + \fn char **TQApplication::argv() const + + Returns the command line argument vector. + + \c argv()[0] is the program name, \c argv()[1] is the first + argument and \c argv()[argc()-1] is the last argument. + + A TQApplication object is constructed by passing \e argc and \e + argv from the \c main() function. Some of the arguments may be + recognized as TQt options and removed from the argument vector. For + example, the X11 version of TQt knows about \c -display, \c -font + and a few more options. + + Example: + \code + // showargs.cpp - displays program arguments in a list box + + #include + #include + + int main( int argc, char **argv ) + { + TQApplication a( argc, argv ); + TQListBox b; + a.setMainWidget( &b ); + for ( int i = 0; i < a.argc(); i++ ) // a.argc() == argc + b.insertItem( a.argv()[i] ); // a.argv()[i] == argv[i] + b.show(); + return a.exec(); + } + \endcode + + If you run \c{showargs -display unix:0 -font 9x15bold hello world} + under X11, the list box contains the three strings "showargs", + "hello" and "world". + + TQt provides a global pointer, \c qApp, that points to the + TQApplication object, and through which you can access argc() and + argv() in functions other than main(). + + \sa argc(), TQApplication::TQApplication() +*/ + +/*! + \fn void TQApplication::setArgs( int argc, char **argv ) + \internal +*/ + + +#ifndef QT_NO_STYLE + +static TQString *qt_style_override = 0; + +/*! + Returns the application's style object. + + \sa setStyle(), TQStyle +*/ +TQStyle& TQApplication::style() +{ +#ifndef QT_NO_STYLE + if ( app_style ) + return *app_style; + if ( !qt_is_gui_used ) + qFatal( "No style available in non-gui applications!" ); + +#if defined(Q_WS_X11) + if(!qt_style_override) + x11_initialize_style(); // run-time search for default style +#endif + if ( !app_style ) { + // Compile-time search for default style + // + TQString style; + if ( qt_style_override ) { + style = *qt_style_override; + delete qt_style_override; + qt_style_override = 0; + } else { +# if defined(Q_WS_WIN) && defined(Q_OS_TEMP) + style = "PocketPC"; +#elif defined(Q_WS_WIN) + if ( qWinVersion() >= TQt::WV_XP && qWinVersion() < TQt::WV_NT_based ) + style = "WindowsXP"; + else + style = "Windows"; // default styles for Windows +#elif defined(Q_WS_X11) && defined(Q_OS_SOLARIS) + style = "CDE"; // default style for X11 on Solaris +#elif defined(Q_WS_X11) && defined(Q_OS_IRIX) + style = "SGI"; // default style for X11 on IRIX +#elif defined(Q_WS_X11) + style = "Motif"; // default style for X11 +#elif defined(Q_WS_MAC) + style = "Macintosh"; // default style for all Mac's +#elif defined(Q_WS_QWS) + style = "Compact"; // default style for small devices +#endif + } + app_style = TQStyleFactory::create( style ); + if ( !app_style && // platform default style not available, try alternatives + !(app_style = TQStyleFactory::create( "Windows" ) ) && + !(app_style = TQStyleFactory::create( "Platinum" ) ) && + !(app_style = TQStyleFactory::create( "MotifPlus" ) ) && + !(app_style = TQStyleFactory::create( "Motif" ) ) && + !(app_style = TQStyleFactory::create( "CDE" ) ) && + !(app_style = TQStyleFactory::create( "Aqua" ) ) && + !(app_style = TQStyleFactory::create( "SGI" ) ) && + !(app_style = TQStyleFactory::create( "Compact" ) ) +#ifndef QT_NO_STRINGLIST + && !(app_style = TQStyleFactory::create( TQStyleFactory::keys()[0] ) ) +#endif + ) + qFatal( "No %s style available!", style.latin1() ); + } + + TQPalette app_pal_copy ( *app_pal ); + app_style->polish( *app_pal ); + + if ( is_app_running && !is_app_closing && (*app_pal != app_pal_copy) ) { + TQEvent e( TQEvent::ApplicationPaletteChange ); + TQWidgetIntDictIt it( *((TQWidgetIntDict*)TQWidget::mapper) ); + register TQWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + sendEvent( w, &e ); + } + } + + app_style->polish( qApp ); +#endif + return *app_style; +} + +/*! + Sets the application's GUI style to \a style. Ownership of the style + object is transferred to TQApplication, so TQApplication will delete + the style object on application exit or when a new style is set. + + Example usage: + \code + TQApplication::setStyle( new TQWindowsStyle ); + \endcode + + When switching application styles, the color palette is set back to + the initial colors or the system defaults. This is necessary since + certain styles have to adapt the color palette to be fully + style-guide compliant. + + \sa style(), TQStyle, setPalette(), desktopSettingsAware() +*/ +void TQApplication::setStyle( TQStyle *style ) +{ + TQStyle* old = app_style; + app_style = style; +#ifdef Q_WS_X11 + qt_explicit_app_style = TRUE; +#endif // Q_WS_X11 + + if ( startingUp() ) { + delete old; + return; + } + + // clean up the old style + if (old) { + if ( is_app_running && !is_app_closing ) { + TQWidgetIntDictIt it( *((TQWidgetIntDict*)TQWidget::mapper) ); + register TQWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + if ( !w->testWFlags(WType_Desktop) && // except desktop + w->testWState(WState_Polished) ) { // has been polished + old->unPolish(w); + } + } + } + old->unPolish( qApp ); + } + + // take care of possible palette retquirements of certain gui + // styles. Do it before polishing the application since the style + // might call TQApplication::setStyle() itself + if ( !qt_std_pal ) + qt_create_std_palette(); + TQPalette tmpPal = *qt_std_pal; + setPalette( tmpPal, TRUE ); + + // initialize the application with the new style + app_style->polish( qApp ); + + // re-polish existing widgets if necessary + if (old) { + if ( is_app_running && !is_app_closing ) { + TQWidgetIntDictIt it( *((TQWidgetIntDict*)TQWidget::mapper) ); + register TQWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + if ( !w->testWFlags(WType_Desktop) ) { // except desktop + if ( w->testWState(WState_Polished) ) + app_style->polish(w); // repolish + w->styleChange( *old ); + if ( w->isVisible() ){ + w->update(); + } + } + } + } + delete old; + } +} + +/*! + \overload + + Requests a TQStyle object for \a style from the TQStyleFactory. + + The string must be one of the TQStyleFactory::keys(), typically one + of "windows", "motif", "cde", "motifplus", "platinum", "sgi" and + "compact". Depending on the platform, "windowsxp", "aqua" or + "macintosh" may be available. + + A later call to the TQApplication constructor will override the + requested style when a "-style" option is passed in as a commandline + parameter. + + Returns 0 if an unknown \a style is passed, otherwise the TQStyle object + returned is set as the application's GUI style. +*/ +TQStyle* TQApplication::setStyle( const TQString& style ) +{ +#ifdef Q_WS_X11 + qt_explicit_app_style = TRUE; +#endif // Q_WS_X11 + + if ( startingUp() ) { + if(qt_style_override) + *qt_style_override = style; + else + qt_style_override = new TQString(style); + return 0; + } + TQStyle *s = TQStyleFactory::create( style ); + if ( !s ) + return 0; + + setStyle( s ); + return s; +} + +#endif + + +#if 1 /* OBSOLETE */ + +TQApplication::ColorMode TQApplication::colorMode() +{ + return (TQApplication::ColorMode)app_cspec; +} + +void TQApplication::setColorMode( TQApplication::ColorMode mode ) +{ + app_cspec = mode; +} +#endif + + +/*! + Returns the color specification. + \sa TQApplication::setColorSpec() + */ + +int TQApplication::colorSpec() +{ + return app_cspec; +} + +/*! + Sets the color specification for the application to \a spec. + + The color specification controls how the application allocates colors + when run on a display with a limited amount of colors, e.g. 8 bit / 256 + color displays. + + The color specification must be set before you create the TQApplication + object. + + The options are: + \list + \i TQApplication::NormalColor. + This is the default color allocation strategy. Use this option if + your application uses buttons, menus, texts and pixmaps with few + colors. With this option, the application uses system global + colors. This works fine for most applications under X11, but on + Windows machines it may cause dithering of non-standard colors. + \i TQApplication::CustomColor. + Use this option if your application needs a small number of custom + colors. On X11, this option is the same as NormalColor. On Windows, TQt + creates a Windows palette, and allocates colors to it on demand. + \i TQApplication::ManyColor. + Use this option if your application is very color hungry + (e.g. it retquires thousands of colors). + Under X11 the effect is: + \list + \i For 256-color displays which have at best a 256 color true color + visual, the default visual is used, and colors are allocated + from a color cube. The color cube is the 6x6x6 (216 color) "Web + palette"*, but the number of colors can be changed + by the \e -ncols option. The user can force the application to + use the true color visual with the \link + TQApplication::TQApplication() -visual \endlink option. + \i For 256-color displays which have a true color visual with more + than 256 colors, use that visual. Silicon Graphics X servers + have this feature, for example. They provide an 8 bit visual + by default but can deliver true color when asked. + \endlist + On Windows, TQt creates a Windows palette, and fills it with a color cube. + \endlist + + Be aware that the CustomColor and ManyColor choices may lead to colormap + flashing: The foreground application gets (most) of the available + colors, while the background windows will look less attractive. + + Example: + \code + int main( int argc, char **argv ) + { + TQApplication::setColorSpec( TQApplication::ManyColor ); + TQApplication a( argc, argv ); + ... + } + \endcode + + TQColor provides more functionality for controlling color allocation and + freeing up certain colors. See TQColor::enterAllocContext() for more + information. + + To check what mode you end up with, call TQColor::numBitPlanes() once + the TQApplication object exists. A value greater than 8 (typically + 16, 24 or 32) means true color. + + * The color cube used by TQt has 216 colors whose red, + green, and blue components always have one of the following values: + 0x00, 0x33, 0x66, 0x99, 0xCC, or 0xFF. + + \sa colorSpec(), TQColor::numBitPlanes(), TQColor::enterAllocContext() */ + +void TQApplication::setColorSpec( int spec ) +{ +#if defined(QT_CHECK_STATE) + if ( qApp ) { + qWarning( "TQApplication::setColorSpec: This function must be " + "called before the TQApplication object is created" ); + } +#endif + app_cspec = spec; +} + +/*! + \fn TQSize TQApplication::globalStrut() + + Returns the application's global strut. + + The strut is a size object whose dimensions are the minimum that any + GUI element that the user can interact with should have. For example + no button should be resized to be smaller than the global strut size. + + \sa setGlobalStrut() +*/ + +/*! + Sets the application's global strut to \a strut. + + The strut is a size object whose dimensions are the minimum that any + GUI element that the user can interact with should have. For example + no button should be resized to be smaller than the global strut size. + + The strut size should be considered when reimplementing GUI controls + that may be used on touch-screens or similar IO-devices. + + Example: + \code + TQSize& WidgetClass::sizeHint() const + { + return TQSize( 80, 25 ).expandedTo( TQApplication::globalStrut() ); + } + \endcode + + \sa globalStrut() +*/ + +void TQApplication::setGlobalStrut( const TQSize& strut ) +{ + app_strut = strut; +} + +#if defined( Q_WS_WIN ) || defined( Q_WS_MAC ) +extern const char *qAppFileName(); +#endif + +#ifndef QT_NO_DIR +#ifndef Q_WS_WIN +static TQString resolveSymlinks( const TQString& path, int depth = 0 ) +{ + bool foundLink = FALSE; + TQString linkTarget; + TQString part = path; + int slashPos = path.length(); + + // too deep; we give up + if ( depth == 128 ) + return TQString::null; + + do { + part = part.left( slashPos ); + TQFileInfo fileInfo( part ); + if ( fileInfo.isSymLink() ) { + foundLink = TRUE; + linkTarget = fileInfo.readLink(); + break; + } + } while ( (slashPos = part.findRev('/')) != -1 ); + + if ( foundLink ) { + TQString path2; + if ( linkTarget[0] == '/' ) { + path2 = linkTarget; + if ( slashPos < (int) path.length() ) + path2 += "/" + path.right( path.length() - slashPos - 1 ); + } else { + TQString relPath; + relPath = part.left( part.findRev('/') + 1 ) + linkTarget; + if ( slashPos < (int) path.length() ) { + if ( !linkTarget.endsWith( "/" ) ) + relPath += "/"; + relPath += path.right( path.length() - slashPos - 1 ); + } + path2 = TQDir::current().absFilePath( relPath ); + } + path2 = TQDir::cleanDirPath( path2 ); + return resolveSymlinks( path2, depth + 1 ); + } else { + return path; + } +} +#endif // Q_WS_WIN + +/*! + Returns the directory that contains the application executable. + + For example, if you have installed TQt in the \c{C:\Trolltech\TQt} + directory, and you run the \c{demo} example, this function will + return "C:/Trolltech/TQt/examples/demo". + + On Mac OS X this will point to the directory actually containing the + executable, which may be inside of an application bundle (if the + application is bundled). + + \warning On Unix, this function assumes that argv[0] contains the file + name of the executable (which it normally does). It also assumes that + the current directory hasn't been changed by the application. + + \sa applicationFilePath() +*/ +TQString TQApplication::applicationDirPath() +{ + return TQFileInfo( applicationFilePath() ).dirPath(); +} + +/*! + Returns the file path of the application executable. + + For example, if you have installed TQt in the \c{C:\Trolltech\TQt} + directory, and you run the \c{demo} example, this function will + return "C:/Trolltech/TQt/examples/demo/demo.exe". + + \warning On Unix, this function assumes that argv[0] contains the file + name of the executable (which it normally does). It also assumes that + the current directory hasn't been changed by the application. + + \sa applicationDirPath() +*/ +TQString TQApplication::applicationFilePath() +{ +#if defined( Q_WS_WIN ) + TQFileInfo filePath; + QT_WA({ + WCHAR module_name[256]; + GetModuleFileNameW(0, module_name, sizeof(module_name)); + filePath = TQString::fromUcs2((const unsigned short *)module_name); + }, { + char module_name[256]; + GetModuleFileNameA(0, module_name, sizeof(module_name)); + filePath = TQString::fromLocal8Bit(module_name); + }); + + return filePath.filePath(); +#elif defined( Q_WS_MAC ) + return TQDir::cleanDirPath( TQFile::decodeName( qAppFileName() ) ); +#else + TQString argv0 = TQFile::decodeName( argv()[0] ); + TQString absPath; + + if ( argv0[0] == '/' ) { + /* + If argv0 starts with a slash, it is already an absolute + file path. + */ + absPath = argv0; + } else if ( argv0.find('/') != -1 ) { + /* + If argv0 contains one or more slashes, it is a file path + relative to the current directory. + */ + absPath = TQDir::current().absFilePath( argv0 ); + } else { + /* + Otherwise, the file path has to be determined using the + PATH environment variable. + */ + char *pEnv = getenv( "PATH" ); + TQStringList paths( TQStringList::split(TQChar(':'), pEnv) ); + TQStringList::const_iterator p = paths.begin(); + while ( p != paths.end() ) { + TQString candidate = TQDir::current().absFilePath( *p + "/" + argv0 ); + if ( TQFile::exists(candidate) ) { + absPath = candidate; + break; + } + ++p; + } + } + + absPath = TQDir::cleanDirPath( absPath ); + if ( TQFile::exists(absPath) ) { + return resolveSymlinks( absPath ); + } else { + return TQString::null; + } +#endif +} +#endif // QT_NO_DIR + +#ifndef QT_NO_COMPONENT + +/*! + Returns a list of paths that the application will search when + dynamically loading libraries. + The installation directory for plugins is the only entry if no + paths have been set. The default installation directory for plugins + is \c INSTALL/plugins, where \c INSTALL is the directory where TQt was + installed. The directory of the application executable (NOT the + working directory) is also added to the plugin paths. + + If you want to iterate over the list, you should iterate over a + copy, e.g. + \code + TQStringList list = app.libraryPaths(); + TQStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + See the \link plugins-howto.html plugins documentation\endlink for a + description of how the library paths are used. + + \sa setLibraryPaths(), addLibraryPath(), removeLibraryPath(), TQLibrary +*/ +TQStringList TQApplication::libraryPaths() +{ + if ( !app_libpaths ) { + app_libpaths = new TQStringList; + TQString installPathPlugins = TQString::fromLocal8Bit(qInstallPathPlugins()); + if ( TQFile::exists(installPathPlugins) ) { +#ifdef Q_WS_WIN + installPathPlugins.replace('\\', '/'); +#endif + app_libpaths->append(installPathPlugins); + } + + TQString app_location; + if (qApp) + app_location = qApp->applicationFilePath(); +#ifdef Q_WS_WIN + else { + app_location = TQString(qAppFileName()); + app_location.replace('\\', '/'); + } +#endif + if (!app_location.isEmpty()) { + app_location.truncate( app_location.findRev( '/' ) ); + if ( app_location != qInstallPathPlugins() && TQFile::exists( app_location ) ) + app_libpaths->append( app_location ); + } + } + return *app_libpaths; +} + + +/*! + Sets the list of directories to search when loading libraries to \a paths. + All existing paths will be deleted and the path list will consist of the + paths given in \a paths. + + \sa libraryPaths(), addLibraryPath(), removeLibraryPath(), TQLibrary + */ +void TQApplication::setLibraryPaths( const TQStringList &paths ) +{ + delete app_libpaths; + app_libpaths = new TQStringList( paths ); +} + +/*! + Append \a path to the end of the library path list. If \a path is + empty or already in the path list, the path list is not changed. + + The default path list consists of a single entry, the installation + directory for plugins. The default installation directory for plugins + is \c INSTALL/plugins, where \c INSTALL is the directory where TQt was + installed. + + \sa removeLibraryPath(), libraryPaths(), setLibraryPaths() + */ +void TQApplication::addLibraryPath( const TQString &path ) +{ + if ( path.isEmpty() ) + return; + + // make sure that library paths is initialized + libraryPaths(); + + if ( !app_libpaths->contains( path ) ) + app_libpaths->prepend( path ); +} + +/*! + Removes \a path from the library path list. If \a path is empty or not + in the path list, the list is not changed. + + \sa addLibraryPath(), libraryPaths(), setLibraryPaths() +*/ +void TQApplication::removeLibraryPath( const TQString &path ) +{ + if ( path.isEmpty() ) + return; + + // make sure that library paths is initialized + libraryPaths(); + + if ( app_libpaths->contains( path ) ) + app_libpaths->remove( path ); +} +#endif //QT_NO_COMPONENT + +/*! + Returns the application palette. + + If a widget is passed in \a w, the default palette for the + widget's class is returned. This may or may not be the application + palette. In most cases there isn't a special palette for certain + types of widgets, but one notable exception is the popup menu under + Windows, if the user has defined a special background color for + menus in the display settings. + + \sa setPalette(), TQWidget::palette() +*/ +#ifndef QT_NO_PALETTE +TQPalette TQApplication::palette(const TQWidget* w) +{ +#if defined(QT_CHECK_STATE) + if ( !qApp ) + qWarning( "TQApplication::palette: This function can only be " + "called after the TQApplication object has been created" ); +#endif + if ( !app_pal ) { + if ( !qt_std_pal ) + qt_create_std_palette(); + app_pal = new TQPalette( *qt_std_pal ); + qt_fix_tooltips(); + } + + if ( w && app_palettes ) { + TQPalette* wp = app_palettes->find( w->className() ); + if ( wp ) + return *wp; + TQAsciiDictIterator it( *app_palettes ); + const char* name; + while ( (name=it.currentKey()) != 0 ) { + if ( w->inherits( name ) ) + return *it.current(); + ++it; + } + } + return *app_pal; +} + +/*! + Changes the default application palette to \a palette. If \a + informWidgets is TRUE, then existing widgets are informed about the + change and may adjust themselves to the new application + setting. If \a informWidgets is FALSE, the change only affects newly + created widgets. + + If \a className is passed, the change applies only to widgets that + inherit \a className (as reported by TQObject::inherits()). If + \a className is left 0, the change affects all widgets, thus overriding + any previously set class specific palettes. + + The palette may be changed according to the current GUI style in + TQStyle::polish(). + + \sa TQWidget::setPalette(), palette(), TQStyle::polish() +*/ + +void TQApplication::setPalette( const TQPalette &palette, bool informWidgets, + const char* className ) +{ + TQPalette pal = palette; + TQPalette *oldpal = 0; +#ifndef QT_NO_STYLE + if ( !startingUp() ) // on startup this has been done already + qApp->style().polish( pal ); // NB: non-const reference +#endif + bool all = FALSE; + if ( !className ) { + if ( !app_pal ) { + app_pal = new TQPalette( pal ); + Q_CHECK_PTR( app_pal ); + } else { + *app_pal = pal; + } + all = app_palettes != 0; + delete app_palettes; + app_palettes = 0; + qt_fix_tooltips(); + } else { + if ( !app_palettes ) { + app_palettes = new TQAsciiDict; + Q_CHECK_PTR( app_palettes ); + app_palettes->setAutoDelete( TRUE ); + } + oldpal = app_palettes->find( className ); + app_palettes->insert( className, new TQPalette( pal ) ); + } + if ( informWidgets && is_app_running && !is_app_closing ) { + if ( !oldpal || ( *oldpal != pal ) ) { + TQEvent e( TQEvent::ApplicationPaletteChange ); + TQWidgetIntDictIt it( *((TQWidgetIntDict*)TQWidget::mapper) ); + register TQWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + if ( all || (!className && w->isTopLevel() ) || w->inherits(className) ) // matching class + sendEvent( w, &e ); + } + } + } +} + +#endif // QT_NO_PALETTE + +/*! + Returns the default font for the widget \a w, or the default + application font if \a w is 0. + + \sa setFont(), fontMetrics(), TQWidget::font() +*/ + +TQFont TQApplication::font( const TQWidget *w ) +{ + if ( w && app_fonts ) { + TQFont* wf = app_fonts->find( w->className() ); + if ( wf ) + return *wf; + TQAsciiDictIterator it( *app_fonts ); + const char* name; + while ( (name=it.currentKey()) != 0 ) { + if ( w->inherits( name ) ) + return *it.current(); + ++it; + } + } + if ( !app_font ) { + app_font = new TQFont( "Helvetica" ); + Q_CHECK_PTR( app_font ); + } + return *app_font; +} + +/*! Changes the default application font to \a font. If \a + informWidgets is TRUE, then existing widgets are informed about the + change and may adjust themselves to the new application + setting. If \a informWidgets is FALSE, the change only affects newly + created widgets. If \a className is passed, the change applies only + to classes that inherit \a className (as reported by + TQObject::inherits()). + + On application start-up, the default font depends on the window + system. It can vary depending on both the window system version and + the locale. This function lets you override the default font; but + overriding may be a bad idea because, for example, some locales need + extra-large fonts to support their special characters. + + \sa font(), fontMetrics(), TQWidget::setFont() +*/ + +void TQApplication::setFont( const TQFont &font, bool informWidgets, + const char* className ) +{ + bool all = FALSE; + if ( !className ) { + qt_app_has_font = TRUE; + if ( !app_font ) { + app_font = new TQFont( font ); + Q_CHECK_PTR( app_font ); + } else { + *app_font = font; + } + + // make sure the application font is complete + app_font->detach(); + app_font->d->mask = TQFontPrivate::Complete; + + all = app_fonts != 0; + delete app_fonts; + app_fonts = 0; + } else { + if (!app_fonts){ + app_fonts = new TQAsciiDict; + Q_CHECK_PTR( app_fonts ); + app_fonts->setAutoDelete( TRUE ); + } + TQFont* fnt = new TQFont(font); + Q_CHECK_PTR( fnt ); + app_fonts->insert(className, fnt); + } + if ( informWidgets && is_app_running && !is_app_closing ) { + TQEvent e( TQEvent::ApplicationFontChange ); + TQWidgetIntDictIt it( *((TQWidgetIntDict*)TQWidget::mapper) ); + register TQWidget *w; + while ( (w=it.current()) ) { // for all widgets... + ++it; + if ( all || (!className && w->isTopLevel() ) || w->inherits(className) ) // matching class + sendEvent( w, &e ); + } + } +} + + +/*! + Initialization of the appearance of the widget \a w \e before it is first + shown. + + Usually widgets call this automatically when they are polished. It + may be used to do some style-based central customization of widgets. + + Note that you are not limited to the public functions of TQWidget. + Instead, based on meta information like TQObject::className() you are + able to customize any kind of widget. + + \sa TQStyle::polish(), TQWidget::polish(), setPalette(), setFont() +*/ + +void TQApplication::polish( TQWidget *w ) +{ +#ifndef QT_NO_STYLE + w->style().polish( w ); +#endif +} + + +/*! + Returns a list of the top level widgets in the application. + + The list is created using \c new and must be deleted by the caller. + + The list is empty (TQPtrList::isEmpty()) if there are no top level + widgets. + + Note that some of the top level widgets may be hidden, for example + the tooltip if no tooltip is currently shown. + + Example: + \code + // Show all hidden top level widgets. + TQWidgetList *list = TQApplication::topLevelWidgets(); + TQWidgetListIt it( *list ); // iterate over the widgets + TQWidget * w; + while ( (w=it.current()) != 0 ) { // for each top level widget... + ++it; + if ( !w->isVisible() ) + w->show(); + } + delete list; // delete the list, not the widgets + \endcode + + \warning Delete the list as soon you have finished using it. + The widgets in the list may be deleted by someone else at any time. + + \sa allWidgets(), TQWidget::isTopLevel(), TQWidget::isVisible(), + TQPtrList::isEmpty() +*/ + +TQWidgetList *TQApplication::topLevelWidgets() +{ + return TQWidget::tlwList(); +} + +/*! + Returns a list of all the widgets in the application. + + The list is created using \c new and must be deleted by the caller. + + The list is empty (TQPtrList::isEmpty()) if there are no widgets. + + Note that some of the widgets may be hidden. + + Example that updates all widgets: + \code + TQWidgetList *list = TQApplication::allWidgets(); + TQWidgetListIt it( *list ); // iterate over the widgets + TQWidget * w; + while ( (w=it.current()) != 0 ) { // for each widget... + ++it; + w->update(); + } + delete list; // delete the list, not the widgets + \endcode + + The TQWidgetList class is defined in the \c qwidgetlist.h header + file. + + \warning Delete the list as soon as you have finished using it. + The widgets in the list may be deleted by someone else at any time. + + \sa topLevelWidgets(), TQWidget::isVisible(), TQPtrList::isEmpty(), +*/ + +TQWidgetList *TQApplication::allWidgets() +{ + return TQWidget::wList(); +} + +/*! + \fn TQWidget *TQApplication::focusWidget() const + + Returns the application widget that has the keyboard input focus, or + 0 if no widget in this application has the focus. + + \sa TQWidget::setFocus(), TQWidget::hasFocus(), activeWindow() +*/ + +/*! + \fn TQWidget *TQApplication::activeWindow() const + + Returns the application top-level window that has the keyboard input + focus, or 0 if no application window has the focus. Note that + there might be an activeWindow() even if there is no focusWidget(), + for example if no widget in that window accepts key events. + + \sa TQWidget::setFocus(), TQWidget::hasFocus(), focusWidget() +*/ + +/*! + Returns display (screen) font metrics for the application font. + + \sa font(), setFont(), TQWidget::fontMetrics(), TQPainter::fontMetrics() +*/ + +TQFontMetrics TQApplication::fontMetrics() +{ + return desktop()->fontMetrics(); +} + + + +/*! + Tells the application to exit with return code 0 (success). + Equivalent to calling TQApplication::exit( 0 ). + + It's common to connect the lastWindowClosed() signal to tquit(), and + you also often connect e.g. TQButton::clicked() or signals in + TQAction, TQPopupMenu or TQMenuBar to it. + + Example: + \code + TQPushButton *tquitButton = new TQPushButton( "Quit" ); + connect( tquitButton, SIGNAL(clicked()), qApp, SLOT(tquit()) ); + \endcode + + \sa exit() aboutToQuit() lastWindowClosed() TQAction +*/ + +void TQApplication::tquit() +{ + TQApplication::exit( 0 ); +} + + +/*! + Closes all top-level windows. + + This function is particularly useful for applications with many + top-level windows. It could, for example, be connected to a "Quit" + entry in the file menu as shown in the following code example: + + \code + // the "Quit" menu entry should try to close all windows + TQPopupMenu* file = new TQPopupMenu( this ); + file->insertItem( "&Quit", qApp, SLOT(closeAllWindows()), CTRL+Key_Q ); + + // when the last window is closed, the application should tquit + connect( qApp, SIGNAL( lastWindowClosed() ), qApp, SLOT( tquit() ) ); + \endcode + + The windows are closed in random order, until one window does not + accept the close event. + + \sa TQWidget::close(), TQWidget::closeEvent(), lastWindowClosed(), + tquit(), topLevelWidgets(), TQWidget::isTopLevel() + + */ +void TQApplication::closeAllWindows() +{ + bool did_close = TRUE; + TQWidget *w; + while((w = activeModalWidget()) && did_close) { + if(w->isHidden()) + break; + did_close = w->close(); + } + TQWidgetList *list = TQApplication::topLevelWidgets(); + for ( w = list->first(); did_close && w; ) { + if ( !w->isHidden() ) { + did_close = w->close(); + delete list; + list = TQApplication::topLevelWidgets(); + w = list->first(); + } else { + w = list->next(); + } + } + delete list; +} + +/*! + Displays a simple message box about TQt. The message includes the + version number of TQt being used by the application. + + This is useful for inclusion in the Help menu of an application. + See the examples/menu/menu.cpp example. + + This function is a convenience slot for TQMessageBox::aboutTQt(). +*/ +void TQApplication::aboutTQt() +{ +#ifndef QT_NO_MESSAGEBOX + TQMessageBox::aboutTQt( mainWidget() ); +#endif // QT_NO_MESSAGEBOX +} + + +/*! + \fn void TQApplication::lastWindowClosed() + + This signal is emitted when the user has closed the last + top level window. + + The signal is very useful when your application has many top level + widgets but no main widget. You can then connect it to the tquit() + slot. + + For convenience, this signal is \e not emitted for transient top level + widgets such as popup menus and dialogs. + + \sa mainWidget(), topLevelWidgets(), TQWidget::isTopLevel(), TQWidget::close() +*/ + +/*! + \fn void TQApplication::aboutToQuit() + + This signal is emitted when the application is about to tquit the + main event loop, e.g. when the event loop level drops to zero. + This may happen either after a call to tquit() from inside the + application or when the users shuts down the entire desktop session. + + The signal is particularly useful if your application has to do some + last-second cleanup. Note that no user interaction is possible in + this state. + + \sa tquit() +*/ + + +/*! + \fn void TQApplication::guiThreadAwake() + + This signal is emitted after the event loop returns from a function + that could block. + + \sa wakeUpGuiThread() +*/ + + +/*! + \fn bool TQApplication::sendEvent( TQObject *receiver, TQEvent *event ) + + Sends event \a event directly to receiver \a receiver, using the + notify() function. Returns the value that was returned from the event + handler. + + The event is \e not deleted when the event has been sent. The normal + approach is to create the event on the stack, e.g. + \code + TQMouseEvent me( TQEvent::MouseButtonPress, pos, 0, 0 ); + TQApplication::sendEvent( mainWindow, &me ); + \endcode + If you create the event on the heap you must delete it. + + \sa postEvent(), notify() +*/ + +/*! + Sends event \a e to \a receiver: \a {receiver}->event(\a e). + Returns the value that is returned from the receiver's event handler. + + For certain types of events (e.g. mouse and key events), + the event will be propagated to the receiver's parent and so on up to + the top-level object if the receiver is not interested in the event + (i.e., it returns FALSE). + + There are five different ways that events can be processed; + reimplementing this virtual function is just one of them. All five + approaches are listed below: + \list 1 + \i Reimplementing this function. This is very powerful, providing + complete control; but only one subclass can be qApp. + + \i Installing an event filter on qApp. Such an event filter is able + to process all events for all widgets, so it's just as powerful as + reimplementing notify(); furthermore, it's possible to have more + than one application-global event filter. Global event filters even + see mouse events for \link TQWidget::isEnabled() disabled + widgets, \endlink and if \link setGlobalMouseTracking() global mouse + tracking \endlink is enabled, as well as mouse move events for all + widgets. + + \i Reimplementing TQObject::event() (as TQWidget does). If you do + this you get Tab key presses, and you get to see the events before + any widget-specific event filters. + + \i Installing an event filter on the object. Such an event filter + gets all the events except Tab and Shift-Tab key presses. + + \i Reimplementing paintEvent(), mousePressEvent() and so + on. This is the commonest, easiest and least powerful way. + \endlist + + \sa TQObject::event(), installEventFilter() +*/ + +bool TQApplication::notify( TQObject *receiver, TQEvent *e ) +{ + // no events are delivered after ~TQApplication() has started + if ( is_app_closing ) + return FALSE; + + if ( receiver == 0 ) { // serious error +#if defined(QT_CHECK_NULL) + qWarning( "TQApplication::notify: Unexpected null receiver" ); +#endif + return FALSE; + } + + if ( e->type() == TQEvent::ChildRemoved && receiver->postedEvents && globalPostedEvents) { + +#ifdef QT_THREAD_SUPPORT + TQMutexLocker locker( postevent_mutex ); +#endif // QT_THREAD_SUPPORT + + // the TQObject destructor calls TQObject::removeChild, which calls + // TQApplication::sendEvent() directly. this can happen while the event + // loop is in the middle of posting events, and when we get here, we may + // not have any more posted events for this object. + if ( receiver->postedEvents ) { + // if this is a child remove event and the child insert + // hasn't been dispatched yet, kill that insert + TQPostEventList * l = receiver->postedEvents; + TQObject * c = ((TQChildEvent*)e)->child(); + TQPostEvent * pe; + l->first(); + while( ( pe = l->current()) != 0 ) { + if ( pe->event && pe->receiver == receiver && + pe->event->type() == TQEvent::ChildInserted && + ((TQChildEvent*)pe->event)->child() == c ) { + pe->event->posted = FALSE; + delete pe->event; + pe->event = 0; + l->remove(); + continue; + } + l->next(); + } + } + } + + bool res = FALSE; + if ( !receiver->isWidgetType() ) + res = internalNotify( receiver, e ); + else switch ( e->type() ) { +#ifndef QT_NO_ACCEL + case TQEvent::Accel: + { + TQKeyEvent* key = (TQKeyEvent*) e; + res = internalNotify( receiver, e ); + + if ( !res && !key->isAccepted() ) + res = qt_dispatchAccelEvent( (TQWidget*)receiver, key ); + + // next lines are for compatibility with TQt <= 3.0.x: old + // TQAccel was listening on toplevel widgets + if ( !res && !key->isAccepted() && !((TQWidget*)receiver)->isTopLevel() ) + res = internalNotify( ((TQWidget*)receiver)->topLevelWidget(), e ); + } + break; +#endif //QT_NO_ACCEL + case TQEvent::KeyPress: + case TQEvent::KeyRelease: + case TQEvent::AccelOverride: + { + TQWidget* w = (TQWidget*)receiver; + TQKeyEvent* key = (TQKeyEvent*) e; +#ifndef QT_NO_ACCEL + if ( qt_tryComposeUnicode( w, key ) ) + break; +#endif + bool def = key->isAccepted(); + while ( w ) { + if ( def ) + key->accept(); + else + key->ignore(); + res = internalNotify( w, e ); + if ( res || key->isAccepted() ) + break; + w = w->parentWidget( TRUE ); + } + } + break; + case TQEvent::MouseButtonPress: + if ( e->spontaneous() ) { + TQWidget* fw = (TQWidget*)receiver; + while ( fw->focusProxy() ) + fw = fw->focusProxy(); + if ( fw->isEnabled() && fw->focusPolicy() & TQWidget::ClickFocus ) { + TQFocusEvent::setReason( TQFocusEvent::Mouse); + fw->setFocus(); + TQFocusEvent::resetReason(); + } + } + // fall through intended + case TQEvent::MouseButtonRelease: + case TQEvent::MouseButtonDblClick: + case TQEvent::MouseMove: + { + TQWidget* w = (TQWidget*)receiver; + TQMouseEvent* mouse = (TQMouseEvent*) e; + TQPoint relpos = mouse->pos(); + while ( w ) { + TQMouseEvent me(mouse->type(), relpos, mouse->globalPos(), mouse->button(), mouse->state()); + me.spont = mouse->spontaneous(); + res = internalNotify( w, w == receiver ? mouse : &me ); + e->spont = FALSE; + if (res || w->isTopLevel() || w->testWFlags(WNoMousePropagation)) + break; + + relpos += w->pos(); + w = w->parentWidget(); + } + if ( res ) + mouse->accept(); + else + mouse->ignore(); + } + break; +#ifndef QT_NO_WHEELEVENT + case TQEvent::Wheel: + { + if ( e->spontaneous() ) { + TQWidget* fw = (TQWidget*)receiver; + while ( fw->focusProxy() ) + fw = fw->focusProxy(); + if ( fw->isEnabled() && (fw->focusPolicy() & TQWidget::WheelFocus) == TQWidget::WheelFocus ) { + TQFocusEvent::setReason( TQFocusEvent::Mouse); + fw->setFocus(); + TQFocusEvent::resetReason(); + } + } + + TQWidget* w = (TQWidget*)receiver; + TQWheelEvent* wheel = (TQWheelEvent*) e; + TQPoint relpos = wheel->pos(); + while ( w ) { + TQWheelEvent we(relpos, wheel->globalPos(), wheel->delta(), wheel->state(), wheel->orientation()); + we.spont = wheel->spontaneous(); + res = internalNotify( w, w == receiver ? wheel : &we ); + e->spont = FALSE; + if (res || w->isTopLevel() || w->testWFlags(WNoMousePropagation)) + break; + + relpos += w->pos(); + w = w->parentWidget(); + } + if ( res ) + wheel->accept(); + else + wheel->ignore(); + } + break; +#endif + case TQEvent::ContextMenu: + { + TQWidget* w = (TQWidget*)receiver; + TQContextMenuEvent *context = (TQContextMenuEvent*) e; + TQPoint relpos = context->pos(); + while ( w ) { + TQContextMenuEvent ce(context->reason(), relpos, context->globalPos(), context->state()); + ce.spont = e->spontaneous(); + res = internalNotify( w, w == receiver ? context : &ce ); + e->spont = FALSE; + + if (res || w->isTopLevel() || w->testWFlags(WNoMousePropagation)) + break; + + relpos += w->pos(); + w = w->parentWidget(); + } + if ( res ) + context->accept(); + else + context->ignore(); + } + break; +#if defined (QT_TABLET_SUPPORT) + case TQEvent::TabletMove: + case TQEvent::TabletPress: + case TQEvent::TabletRelease: + { + TQWidget *w = (TQWidget*)receiver; + TQTabletEvent *tablet = (TQTabletEvent*)e; + TQPoint relpos = tablet->pos(); + while ( w ) { + TQTabletEvent te(tablet->pos(), tablet->globalPos(), tablet->device(), + tablet->pressure(), tablet->xTilt(), tablet->yTilt(), + tablet->uniqueId()); + te.spont = e->spontaneous(); + res = internalNotify( w, w == receiver ? tablet : &te ); + e->spont = FALSE; + if (res || w->isTopLevel() || w->testWFlags(WNoMousePropagation)) + break; + + relpos += w->pos(); + w = w->parentWidget(); + } + if ( res ) + tablet->accept(); + else + tablet->ignore(); + chokeMouse = tablet->isAccepted(); + } + break; +#endif + default: + res = internalNotify( receiver, e ); + break; + } + + return res; +} + +/*!\reimp + +*/ +bool TQApplication::event( TQEvent *e ) +{ + if(e->type() == TQEvent::Close) { + TQCloseEvent *ce = (TQCloseEvent*)e; + ce->accept(); + closeAllWindows(); + + TQWidgetList *list = topLevelWidgets(); + for(TQWidget *w = list->first(); w; w = list->next()) { + if ( !w->isHidden() && !w->isDesktop() && !w->isPopup() && + (!w->isDialog() || !w->parentWidget())) { + ce->ignore(); + break; + } + } + if(ce->isAccepted()) + return TRUE; + } else if (e->type() == TQEvent::Quit) { + tquit(); + return TRUE; + } + return TQObject::event(e); +} + +/*!\internal + + Helper function called by notify() + */ +bool TQApplication::internalNotify( TQObject *receiver, TQEvent * e) +{ + if ( eventFilters ) { + TQObjectListIt it( *eventFilters ); + register TQObject *obj; + while ( (obj=it.current()) != 0 ) { // send to all filters + ++it; // until one returns TRUE + if ( obj->eventFilter(receiver,e) ) + return TRUE; + } + } + + bool consumed = FALSE; + bool handled = FALSE; + if ( receiver->isWidgetType() ) { + TQWidget *widget = (TQWidget*)receiver; + + // toggle HasMouse widget state on enter and leave + if ( e->type() == TQEvent::Enter || e->type() == TQEvent::DragEnter ) + widget->setWState( WState_HasMouse ); + else if ( e->type() == TQEvent::Leave || e->type() == TQEvent::DragLeave ) + widget->clearWState( WState_HasMouse ); + + // throw away any mouse-tracking-only mouse events + if ( e->type() == TQEvent::MouseMove && + (((TQMouseEvent*)e)->state()&TQMouseEvent::MouseButtonMask) == 0 && + !widget->hasMouseTracking() ) { + handled = TRUE; + consumed = TRUE; + } else if ( !widget->isEnabled() ) { // throw away mouse events to disabled widgets + switch(e->type()) { + case TQEvent::MouseButtonPress: + case TQEvent::MouseButtonRelease: + case TQEvent::MouseButtonDblClick: + case TQEvent::MouseMove: + ( (TQMouseEvent*) e)->ignore(); + handled = TRUE; + consumed = TRUE; + break; +#ifndef QT_NO_DRAGANDDROP + case TQEvent::DragEnter: + case TQEvent::DragMove: + ( (TQDragMoveEvent*) e)->ignore(); + handled = TRUE; + break; + + case TQEvent::DragLeave: + case TQEvent::DragResponse: + handled = TRUE; + break; + + case TQEvent::Drop: + ( (TQDropEvent*) e)->ignore(); + handled = TRUE; + break; +#endif +#ifndef QT_NO_WHEELEVENT + case TQEvent::Wheel: + ( (TQWheelEvent*) e)->ignore(); + handled = TRUE; + break; +#endif + case TQEvent::ContextMenu: + ( (TQContextMenuEvent*) e)->ignore(); + handled = TRUE; + break; + default: + break; + } + } + + } + + if (!handled) + consumed = receiver->event( e ); + e->spont = FALSE; + return consumed; +} + +/*! + Returns TRUE if an application object has not been created yet; + otherwise returns FALSE. + + \sa closingDown() +*/ + +bool TQApplication::startingUp() +{ + return !is_app_running; +} + +/*! + Returns TRUE if the application objects are being destroyed; + otherwise returns FALSE. + + \sa startingUp() +*/ + +bool TQApplication::closingDown() +{ + return is_app_closing; +} + + +/*! + Processes pending events, for 3 seconds or until there are no more + events to process, whichever is shorter. + + You can call this function occasionally when your program is busy + performing a long operation (e.g. copying a file). + + \sa exec(), TQTimer, TQEventLoop::processEvents() +*/ + +void TQApplication::processEvents() +{ + processEvents( 3000 ); +} + +/*! + \overload + + Processes pending events for \a maxtime milliseconds or until + there are no more events to process, whichever is shorter. + + You can call this function occasionally when you program is busy + doing a long operation (e.g. copying a file). + + \sa exec(), TQTimer, TQEventLoop::processEvents() +*/ +void TQApplication::processEvents( int maxtime ) +{ + eventLoop()->processEvents( TQEventLoop::AllEvents, maxtime ); +} + +/*! \obsolete + Waits for an event to occur, processes it, then returns. + + This function is useful for adapting TQt to situations where the + event processing must be grafted onto existing program loops. + + Using this function in new applications may be an indication of design + problems. + + \sa processEvents(), exec(), TQTimer +*/ + +void TQApplication::processOneEvent() +{ + eventLoop()->processEvents( TQEventLoop::AllEvents | + TQEventLoop::WaitForMore ); +} + +/***************************************************************************** + Main event loop wrappers + *****************************************************************************/ + +/*! + Returns the application event loop. This function will return + zero if called during and after destroying TQApplication. + + To create your own instance of TQEventLoop or TQEventLoop subclass create + it before you create the TQApplication object. + + \sa TQEventLoop +*/ +TQEventLoop *TQApplication::eventLoop() +{ + if ( !eventloop && !is_app_closing ) + (void) new TQEventLoop( qApp, "default event loop" ); + return eventloop; +} + + +/*! + Enters the main event loop and waits until exit() is called or the + main widget is destroyed, and returns the value that was set to + exit() (which is 0 if exit() is called via tquit()). + + It is necessary to call this function to start event handling. The + main event loop receives events from the window system and + dispatches these to the application widgets. + + Generally speaking, no user interaction can take place before + calling exec(). As a special case, modal widgets like TQMessageBox + can be used before calling exec(), because modal widgets call + exec() to start a local event loop. + + To make your application perform idle processing, i.e. executing a + special function whenever there are no pending events, use a + TQTimer with 0 timeout. More advanced idle processing schemes can + be achieved using processEvents(). + + \sa tquit(), exit(), processEvents(), setMainWidget() +*/ +int TQApplication::exec() +{ + return eventLoop()->exec(); +} + +/*! + Tells the application to exit with a return code. + + After this function has been called, the application leaves the main + event loop and returns from the call to exec(). The exec() function + returns \a retcode. + + By convention, a \a retcode of 0 means success, and any non-zero + value indicates an error. + + Note that unlike the C library function of the same name, this + function \e does return to the caller -- it is event processing that + stops. + + \sa tquit(), exec() +*/ +void TQApplication::exit( int retcode ) +{ + qApp->eventLoop()->exit( retcode ); +} + +/*! + \obsolete + + This function enters the main event loop (recursively). Do not call + it unless you really know what you are doing. + + Use TQApplication::eventLoop()->enterLoop() instead. + +*/ +int TQApplication::enter_loop() +{ + return eventLoop()->enterLoop(); +} + +/*! + \obsolete + + This function exits from a recursive call to the main event loop. + Do not call it unless you are an expert. + + Use TQApplication::eventLoop()->exitLoop() instead. + +*/ +void TQApplication::exit_loop() +{ + eventLoop()->exitLoop(); +} + +/*! + \obsolete + + Returns the current loop level. + + Use TQApplication::eventLoop()->loopLevel() instead. + +*/ +int TQApplication::loopLevel() const +{ + return eventLoop()->loopLevel(); +} + +/*! + + Wakes up the GUI thread. + + \sa guiThreadAwake() \link threads.html Thread Support in TQt\endlink +*/ +void TQApplication::wakeUpGuiThread() +{ + eventLoop()->wakeUp(); +} + +/*! + This function returns TRUE if there are pending events; otherwise + returns FALSE. Pending events can be either from the window system + or posted events using TQApplication::postEvent(). +*/ +bool TQApplication::hasPendingEvents() +{ + return eventLoop()->hasPendingEvents(); +} + +#if !defined(Q_WS_X11) + +// The doc and X implementation of these functions is in qapplication_x11.cpp + +void TQApplication::flushX() {} // do nothing + +void TQApplication::syncX() {} // do nothing + +#endif + +/*! + \fn void TQApplication::setWinStyleHighlightColor( const TQColor & ) + \obsolete + + Sets the color used to mark selections in windows style for all widgets + in the application. Will repaint all widgets if the color is changed. + + The default color is \c darkBlue. + \sa winStyleHighlightColor() +*/ + +/*! + \fn const TQColor& TQApplication::winStyleHighlightColor() + \obsolete + + Returns the color used to mark selections in windows style. + + \sa setWinStyleHighlightColor() +*/ + +/*! + Returns the version of the Windows operating system that is running: + + \list + \i TQt::WV_95 - Windows 95 + \i TQt::WV_98 - Windows 98 + \i TQt::WV_Me - Windows Me + \i TQt::WV_NT - Windows NT 4.x + \i TQt::WV_2000 - Windows 2000 (NT5) + \i TQt::WV_XP - Windows XP + \i TQt::WV_2003 - Windows Server 2003 family + \i TQt::WV_CE - Windows CE + \i TQt::WV_CENET - Windows CE.NET + \endlist + + Note that this function is implemented for the Windows version + of TQt only. +*/ + +#if defined(Q_OS_CYGWIN) +TQt::WindowsVersion TQApplication::winVersion() +{ + return qt_winver; +} +#endif + +#ifndef QT_NO_TRANSLATION + +bool qt_detectRTLLanguage() +{ + return TQApplication::tr( "QT_LAYOUT_DIRECTION", + "Translate this string to the string 'LTR' in left-to-right" + " languages or to 'RTL' in right-to-left languages (such as Hebrew" + " and Arabic) to get proper widget layout." ) == "RTL"; +} + +/*! + Adds the message file \a mf to the list of message files to be used + for translations. + + Multiple message files can be installed. Translations are searched + for in the last installed message file, then the one from last, and + so on, back to the first installed message file. The search stops as + soon as a matching translation is found. + + \sa removeTranslator() translate() TQTranslator::load() +*/ + +void TQApplication::installTranslator( TQTranslator * mf ) +{ + if ( !mf ) + return; + if ( !translators ) + translators = new TQValueList; + + translators->prepend( mf ); + +#ifndef QT_NO_TRANSLATION_BUILDER + if ( mf->isEmpty() ) + return; +#endif + + // hook to set the layout direction of dialogs + setReverseLayout( qt_detectRTLLanguage() ); + + TQWidgetList *list = topLevelWidgets(); + TQWidgetListIt it( *list ); + TQWidget *w; + while ( ( w=it.current() ) != 0 ) { + ++it; + if (!w->isDesktop()) + postEvent( w, new TQEvent( TQEvent::LanguageChange ) ); + } + delete list; +} + +/*! + Removes the message file \a mf from the list of message files used by + this application. (It does not delete the message file from the file + system.) + + \sa installTranslator() translate(), TQObject::tr() +*/ + +void TQApplication::removeTranslator( TQTranslator * mf ) +{ + if ( !translators || !mf ) + return; + + if ( translators->remove( mf ) && ! qApp->closingDown() ) { + setReverseLayout( qt_detectRTLLanguage() ); + + TQWidgetList *list = topLevelWidgets(); + TQWidgetListIt it( *list ); + TQWidget *w; + while ( ( w=it.current() ) != 0 ) { + ++it; + postEvent( w, new TQEvent( TQEvent::LanguageChange ) ); + } + delete list; + } +} + +#ifndef QT_NO_TEXTCODEC +/*! \obsolete + This is the same as TQTextCodec::setCodecForTr(). +*/ +void TQApplication::setDefaultCodec( TQTextCodec* codec ) +{ + TQTextCodec::setCodecForTr( codec ); +} + +/*! \obsolete + Returns TQTextCodec::codecForTr(). +*/ +TQTextCodec* TQApplication::defaultCodec() const +{ + return TQTextCodec::codecForTr(); +} +#endif //QT_NO_TEXTCODEC + +/*! \enum TQApplication::Encoding + + This enum type defines the 8-bit encoding of character string + arguments to translate(): + + \value DefaultCodec - the encoding specified by + TQTextCodec::codecForTr() (Latin-1 if none has been set) + \value UnicodeUTF8 - UTF-8 + + \sa TQObject::tr(), TQObject::trUtf8(), TQString::fromUtf8() +*/ + +/*! \reentrant + Returns the translation text for \a sourceText, by querying the + installed messages files. The message files are searched from the most + recently installed message file back to the first installed message + file. + + TQObject::tr() and TQObject::trUtf8() provide this functionality more + conveniently. + + \a context is typically a class name (e.g., "MyDialog") and + \a sourceText is either English text or a short identifying text, if + the output text will be very long (as for help texts). + + \a comment is a disambiguating comment, for when the same \a + sourceText is used in different roles within the same context. By + default, it is null. \a encoding indicates the 8-bit encoding of + character stings + + See the \l TQTranslator documentation for more information about + contexts and comments. + + If none of the message files contain a translation for \a + sourceText in \a context, this function returns a TQString + equivalent of \a sourceText. The encoding of \a sourceText is + specified by \e encoding; it defaults to \c DefaultCodec. + + This function is not virtual. You can use alternative translation + techniques by subclassing \l TQTranslator. + + \warning This method is reentrant only if all translators are + installed \e before calling this method. Installing or removing + translators while performing translations is not supported. Doing + so will most likely result in crashes or other undesirable behavior. + + \sa TQObject::tr() installTranslator() defaultCodec() +*/ + +TQString TQApplication::translate( const char * context, const char * sourceText, + const char * comment, Encoding encoding ) const +{ + if ( !sourceText ) + return TQString::null; + + if ( translators ) { + TQValueList::iterator it; + TQTranslator * mf; + TQString result; + for ( it = translators->begin(); it != translators->end(); ++it ) { + mf = *it; + result = mf->findMessage( context, sourceText, comment ).translation(); + if ( !result.isNull() ) + return result; + } + } +#ifndef QT_NO_TEXTCODEC + if ( encoding == UnicodeUTF8 ) + return TQString::fromUtf8( sourceText ); + else if ( TQTextCodec::codecForTr() != 0 ) + return TQTextCodec::codecForTr()->toUnicode( sourceText ); + else +#endif + return TQString::fromLatin1( sourceText ); +} + +#endif + +/***************************************************************************** + TQApplication management of posted events + *****************************************************************************/ + +//see also notify(), which does the removal of ChildInserted when ChildRemoved. + +/*! + Adds the event \a event with the object \a receiver as the receiver of the + event, to an event queue and returns immediately. + + The event must be allocated on the heap since the post event queue + will take ownership of the event and delete it once it has been posted. + + When control returns to the main event loop, all events that are + stored in the queue will be sent using the notify() function. + + \threadsafe + + \sa sendEvent(), notify() +*/ + +void TQApplication::postEvent( TQObject *receiver, TQEvent *event ) +{ + if ( receiver == 0 ) { +#if defined(QT_CHECK_NULL) + qWarning( "TQApplication::postEvent: Unexpected null receiver" ); +#endif + delete event; + return; + } + +#ifdef QT_THREAD_SUPPORT + TQMutexLocker locker( postevent_mutex ); +#endif // QT_THREAD_SUPPORT + + if ( !globalPostedEvents ) { // create list + globalPostedEvents = new TQPostEventList; + Q_CHECK_PTR( globalPostedEvents ); + globalPostedEvents->setAutoDelete( TRUE ); + qapp_cleanup_events.set( &globalPostedEvents ); + } + + if ( !receiver->postedEvents ) + receiver->postedEvents = new TQPostEventList; + TQPostEventList * l = receiver->postedEvents; + + // if this is one of the compressible events, do compression + if ( event->type() == TQEvent::Paint || + event->type() == TQEvent::LayoutHint || + event->type() == TQEvent::Resize || + event->type() == TQEvent::Move || + event->type() == TQEvent::LanguageChange ) { + l->first(); + TQPostEvent * cur = 0; + for ( ;; ) { + while ( (cur=l->current()) != 0 && + ( cur->receiver != receiver || + cur->event == 0 || + cur->event->type() != event->type() ) ) + l->next(); + if ( l->current() != 0 ) { + if ( cur->event->type() == TQEvent::Paint ) { + TQPaintEvent * p = (TQPaintEvent*)(cur->event); + if ( p->erase != ((TQPaintEvent*)event)->erase ) { + l->next(); + continue; + } + p->reg = p->reg.unite( ((TQPaintEvent *)event)->reg ); + p->rec = p->rec.unite( ((TQPaintEvent *)event)->rec ); + delete event; + return; + } else if ( cur->event->type() == TQEvent::LayoutHint ) { + delete event; + return; + } else if ( cur->event->type() == TQEvent::Resize ) { + ((TQResizeEvent *)(cur->event))->s = ((TQResizeEvent *)event)->s; + delete event; + return; + } else if ( cur->event->type() == TQEvent::Move ) { + ((TQMoveEvent *)(cur->event))->p = ((TQMoveEvent *)event)->p; + delete event; + return; + } else if ( cur->event->type() == TQEvent::LanguageChange ) { + delete event; + return; + } + } + break; + }; + } + +#if !defined(QT_NO_IM) + // if this is one of the compressible IM events, do compression + else if ( event->type() == TQEvent::IMCompose ) { + l->last(); + TQPostEvent * cur = 0; + for ( ;; ) { + while ( (cur=l->current()) != 0 && + ( cur->receiver != receiver || + cur->event == 0 || + cur->event->type() != event->type() || + cur->event->type() != TQEvent::IMStart ) ) + l->prev(); + if ( l->current() != 0 ) { + // IMCompose must not be compressed with another one + // beyond its IMStart boundary + if ( cur->event->type() == TQEvent::IMStart ) { + break; + } else if ( cur->event->type() == TQEvent::IMCompose ) { + TQIMComposeEvent * e = (TQIMComposeEvent *)(cur->event); + *e = *(TQIMComposeEvent *)event; + delete event; + return; + } + } + break; + }; + } +#endif + + // if no compression could be done, just append something + event->posted = TRUE; + TQPostEvent * pe = new TQPostEvent( receiver, event ); + l->append( pe ); + globalPostedEvents->append( pe ); + + if (eventloop) + eventloop->wakeUp(); +} + + +/*! \overload + + Dispatches all posted events, i.e. empties the event queue. +*/ +void TQApplication::sendPostedEvents() +{ + sendPostedEvents( 0, 0 ); +} + + + +/*! + Immediately dispatches all events which have been previously queued + with TQApplication::postEvent() and which are for the object \a receiver + and have the event type \a event_type. + + Note that events from the window system are \e not dispatched by this + function, but by processEvents(). + + If \a receiver is null, the events of \a event_type are sent for all + objects. If \a event_type is 0, all the events are sent for \a receiver. +*/ + +void TQApplication::sendPostedEvents( TQObject *receiver, int event_type ) +{ + // Make sure the object hierarchy is stable before processing events + // to avoid endless loops + if ( receiver == 0 && event_type == 0 ) + sendPostedEvents( 0, TQEvent::ChildInserted ); + + if ( !globalPostedEvents || ( receiver && !receiver->postedEvents ) ) + return; + +#ifdef QT_THREAD_SUPPORT + TQMutexLocker locker( postevent_mutex ); +#endif + + bool sent = TRUE; + while ( sent ) { + sent = FALSE; + + if ( !globalPostedEvents || ( receiver && !receiver->postedEvents ) ) + return; + + // if we have a receiver, use the local list. Otherwise, use the + // global list + TQPostEventList * l = receiver ? receiver->postedEvents : globalPostedEvents; + + // okay. here is the tricky loop. be careful about optimizing + // this, it looks the way it does for good reasons. + TQPostEventListIt it( *l ); + TQPostEvent *pe; + while ( (pe=it.current()) != 0 ) { + ++it; + if ( pe->event // hasn't been sent yet + && ( receiver == 0 // we send to all receivers + || receiver == pe->receiver ) // we send to THAT receiver + && ( event_type == 0 // we send all types + || event_type == pe->event->type() ) ) { // we send THAT type + // first, we diddle the event so that we can deliver + // it, and that noone will try to touch it later. + pe->event->posted = FALSE; + TQEvent * e = pe->event; + TQObject * r = pe->receiver; + pe->event = 0; + + // next, update the data structure so that we're ready + // for the next event. + + // look for the local list, and take whatever we're + // delivering out of it. r->postedEvents maybe *l + if ( r->postedEvents ) { + r->postedEvents->removeRef( pe ); + // if possible, get rid of that list. this is not + // ideal - we will create and delete a list for + // each update() call. it would be better if we'd + // leave the list empty here, and delete it + // somewhere else if it isn't being used. + if ( r->postedEvents->isEmpty() ) { + delete r->postedEvents; + r->postedEvents = 0; + } + } + +#ifdef QT_THREAD_SUPPORT + if ( locker.mutex() ) locker.mutex()->unlock(); +#endif // QT_THREAD_SUPPORT + // after all that work, it's time to deliver the event. + if ( e->type() == TQEvent::Paint && r->isWidgetType() ) { + TQWidget * w = (TQWidget*)r; + TQPaintEvent * p = (TQPaintEvent*)e; + if ( w->isVisible() ) + w->repaint( p->reg, p->erase ); + } else { + sent = TRUE; + TQApplication::sendEvent( r, e ); + } +#ifdef QT_THREAD_SUPPORT + if ( locker.mutex() ) locker.mutex()->lock(); +#endif // QT_THREAD_SUPPORT + + delete e; + // careful when adding anything below this point - the + // sendEvent() call might invalidate any invariants this + // function depends on. + } + } + + // clear the global list, i.e. remove everything that was + // delivered. + if ( l == globalPostedEvents ) { + globalPostedEvents->first(); + while( (pe=globalPostedEvents->current()) != 0 ) { + if ( pe->event ) + globalPostedEvents->next(); + else + globalPostedEvents->remove(); + } + } + } +} + +/*! + Removes all events posted using postEvent() for \a receiver. + + The events are \e not dispatched, instead they are removed from the + queue. You should never need to call this function. If you do call it, + be aware that killing events may cause \a receiver to break one or + more invariants. + + \threadsafe +*/ + +void TQApplication::removePostedEvents( TQObject *receiver ) +{ + removePostedEvents( receiver, 0 ); +} + +/*! + Removes all events that have the event type \a event_type posted + using postEvent() for \a receiver. + + The events are \e not dispatched, instead they are removed from the + queue. + + If \a event_type is 0, all the events are removed from the queue. + + \threadsafe +*/ + +void TQApplication::removePostedEvents( TQObject *receiver, int event_type ) +{ + if ( !receiver ) + return; + +#ifdef QT_THREAD_SUPPORT + TQMutexLocker locker( postevent_mutex ); +#endif // QT_THREAD_SUPPORT + + // the TQObject destructor calls this function directly. this can + // happen while the event loop is in the middle of posting events, + // and when we get here, we may not have any more posted events + // for this object. + if ( !receiver->postedEvents ) + return; + + // iterate over the object-specifc list and delete the events. + // leave the TQPostEvent objects; they'll be deleted by + // sendPostedEvents(). + TQPostEventList * l = receiver->postedEvents; + l->first(); + TQPostEvent * pe; + while( (pe=l->current()) != 0 ) { + if ( !event_type || pe->event->type() == event_type ) { + if ( pe->event ) { + pe->event->posted = FALSE; + delete pe->event; + pe->event = 0; + } + l->remove(); + } else { + l->next(); + } + } + if ( !event_type || !l->count() ) { + receiver->postedEvents = 0; + delete l; + } +} + + +/*! + Removes \a event from the queue of posted events, and emits a + warning message if appropriate. + + \warning This function can be \e really slow. Avoid using it, if + possible. + + \threadsafe +*/ + +void TQApplication::removePostedEvent( TQEvent * event ) +{ + if ( !event || !event->posted ) + return; + + if ( !globalPostedEvents ) { +#if defined(QT_DEBUG) + qDebug( "TQApplication::removePostedEvent: %p %d is posted: impossible", + (void*)event, event->type() ); + return; +#endif + } + +#ifdef QT_THREAD_SUPPORT + TQMutexLocker locker( postevent_mutex ); +#endif // QT_THREAD_SUPPORT + + TQPostEventListIt it( *globalPostedEvents ); + TQPostEvent * pe; + while( (pe = it.current()) != 0 ) { + ++it; + if ( pe->event == event ) { +#if defined(QT_DEBUG) + const char *n; + switch ( event->type() ) { + case TQEvent::Timer: + n = "Timer"; + break; + case TQEvent::MouseButtonPress: + n = "MouseButtonPress"; + break; + case TQEvent::MouseButtonRelease: + n = "MouseButtonRelease"; + break; + case TQEvent::MouseButtonDblClick: + n = "MouseButtonDblClick"; + break; + case TQEvent::MouseMove: + n = "MouseMove"; + break; +#ifndef QT_NO_WHEELEVENT + case TQEvent::Wheel: + n = "Wheel"; + break; +#endif + case TQEvent::KeyPress: + n = "KeyPress"; + break; + case TQEvent::KeyRelease: + n = "KeyRelease"; + break; + case TQEvent::FocusIn: + n = "FocusIn"; + break; + case TQEvent::FocusOut: + n = "FocusOut"; + break; + case TQEvent::Enter: + n = "Enter"; + break; + case TQEvent::Leave: + n = "Leave"; + break; + case TQEvent::Paint: + n = "Paint"; + break; + case TQEvent::Move: + n = "Move"; + break; + case TQEvent::Resize: + n = "Resize"; + break; + case TQEvent::Create: + n = "Create"; + break; + case TQEvent::Destroy: + n = "Destroy"; + break; + case TQEvent::Close: + n = "Close"; + break; + case TQEvent::Quit: + n = "Quit"; + break; + default: + n = ""; + break; + } + qWarning("TQEvent: Warning: %s event deleted while posted to %s %s", + n, + pe->receiver ? pe->receiver->className() : "null", + pe->receiver ? pe->receiver->name() : "object" ); + // note the beautiful uglehack if !pe->receiver :) +#endif + event->posted = FALSE; + delete pe->event; + pe->event = 0; + return; + } + } +} + +/*!\internal + + Sets the active window in reaction to a system event. Call this + from the platform specific event handlers. + + It sets the activeWindow() and focusWidget() attributes and sends + proper WindowActivate/WindowDeactivate and FocusIn/FocusOut events + to all appropriate widgets. + + \sa activeWindow() + */ +void TQApplication::setActiveWindow( TQWidget* act ) +{ + TQWidget* window = act?act->topLevelWidget():0; + + if ( active_window == window ) + return; + + // first the activation/deactivation events + if ( active_window ) { + TQWidgetList deacts; +#ifndef QT_NO_STYLE + if ( style().styleHint(TQStyle::SH_Widget_ShareActivation, active_window ) ) { + TQWidgetList *list = topLevelWidgets(); + if ( list ) { + for ( TQWidget *w = list->first(); w; w = list->next() ) { + if ( w->isVisible() && w->isActiveWindow() ) + deacts.append(w); + } + delete list; + } + } else +#endif + deacts.append(active_window); + active_window = 0; + TQEvent e( TQEvent::WindowDeactivate ); + for(TQWidget *w = deacts.first(); w; w = deacts.next()) + TQApplication::sendSpontaneousEvent( w, &e ); + } + + active_window = window; + if ( active_window ) { + TQEvent e( TQEvent::WindowActivate ); + TQWidgetList acts; +#ifndef QT_NO_STYLE + if ( style().styleHint(TQStyle::SH_Widget_ShareActivation, active_window ) ) { + TQWidgetList *list = topLevelWidgets(); + if ( list ) { + for ( TQWidget *w = list->first(); w; w = list->next() ) { + if ( w->isVisible() && w->isActiveWindow() ) + acts.append(w); + } + delete list; + } + } else +#endif + acts.append(active_window); + for(TQWidget *w = acts.first(); w; w = acts.next()) + TQApplication::sendSpontaneousEvent( w, &e ); + } + + // then focus events + TQFocusEvent::setReason( TQFocusEvent::ActiveWindow ); + if ( !active_window && focus_widget ) { + TQFocusEvent out( TQEvent::FocusOut ); + TQWidget *tmp = focus_widget; + focus_widget = 0; +#ifdef Q_WS_WIN + TQInputContext::accept( tmp ); +#elif defined(Q_WS_X11) + tmp->unfocusInputContext(); +#endif + TQApplication::sendSpontaneousEvent( tmp, &out ); + } else if ( active_window ) { + TQWidget *w = active_window->focusWidget(); + if ( w && w->focusPolicy() != TQWidget::NoFocus ) + w->setFocus(); + else + active_window->focusNextPrevChild( TRUE ); + } + TQFocusEvent::resetReason(); +} + + +/*!\internal + + Creates the proper Enter/Leave event when widget \a enter is entered + and widget \a leave is left. + */ +Q_EXPORT void qt_dispatchEnterLeave( TQWidget* enter, TQWidget* leave ) { +#if 0 + if ( leave ) { + TQEvent e( TQEvent::Leave ); + TQApplication::sendEvent( leave, & e ); + } + if ( enter ) { + TQEvent e( TQEvent::Enter ); + TQApplication::sendEvent( enter, & e ); + } + return; +#endif + + TQWidget* w ; + if ( !enter && !leave ) + return; + TQWidgetList leaveList; + TQWidgetList enterList; + + bool sameWindow = leave && enter && leave->topLevelWidget() == enter->topLevelWidget(); + if ( leave && !sameWindow ) { + w = leave; + do { + leaveList.append( w ); + } while ( (w = w->parentWidget( TRUE ) ) ); + } + if ( enter && !sameWindow ) { + w = enter; + do { + enterList.prepend( w ); + } while ( (w = w->parentWidget(TRUE) ) ); + } + if ( sameWindow ) { + int enterDepth = 0; + int leaveDepth = 0; + w = enter; + while ( ( w = w->parentWidget( TRUE ) ) ) + enterDepth++; + w = leave; + while ( ( w = w->parentWidget( TRUE ) ) ) + leaveDepth++; + TQWidget* wenter = enter; + TQWidget* wleave = leave; + while ( enterDepth > leaveDepth ) { + wenter = wenter->parentWidget(); + enterDepth--; + } + while ( leaveDepth > enterDepth ) { + wleave = wleave->parentWidget(); + leaveDepth--; + } + while ( !wenter->isTopLevel() && wenter != wleave ) { + wenter = wenter->parentWidget(); + wleave = wleave->parentWidget(); + } + + w = leave; + while ( w != wleave ) { + leaveList.append( w ); + w = w->parentWidget(); + } + w = enter; + while ( w != wenter ) { + enterList.prepend( w ); + w = w->parentWidget(); + } + } + + TQEvent leaveEvent( TQEvent::Leave ); + for ( w = leaveList.first(); w; w = leaveList.next() ) { + if ( !qApp->activeModalWidget() || qt_tryModalHelper( w, 0 )) + TQApplication::sendEvent( w, &leaveEvent ); + } + TQEvent enterEvent( TQEvent::Enter ); + for ( w = enterList.first(); w; w = enterList.next() ) { + if ( !qApp->activeModalWidget() || qt_tryModalHelper( w, 0 )) + TQApplication::sendEvent( w, &enterEvent ); + } +} + + +#ifdef Q_WS_MACX +extern TQWidget *qt_tryModalHelperMac( TQWidget * top ); //qapplication_mac.cpp +#endif + + +/*!\internal + + Called from qapplication_.cpp, returns TRUE + if the widget should accept the event. + */ +Q_EXPORT bool qt_tryModalHelper( TQWidget *widget, TQWidget **rettop ) { + TQWidget *modal=0, *top=TQApplication::activeModalWidget(); + if ( rettop ) *rettop = top; + + if ( qApp->activePopupWidget() ) + return TRUE; + +#ifdef Q_WS_MACX + top = qt_tryModalHelperMac( top ); + if ( rettop ) *rettop = top; +#endif + + TQWidget* groupLeader = widget; + widget = widget->topLevelWidget(); + + if ( widget->testWFlags(TQt::WShowModal) ) // widget is modal + modal = widget; + if ( !top || modal == top ) // don't block event + return TRUE; + + TQWidget * p = widget->parentWidget(); // Check if the active modal widget is a parent of our widget + while ( p ) { + if ( p == top ) + return TRUE; + p = p->parentWidget(); + } + + while ( groupLeader && !groupLeader->testWFlags( TQt::WGroupLeader ) ) + groupLeader = groupLeader->parentWidget(); + + if ( groupLeader ) { + // Does groupLeader have a child in qt_modal_stack? + bool unrelated = TRUE; + modal = qt_modal_stack->first(); + while (modal && unrelated) { + TQWidget* p = modal->parentWidget(); + while ( p && p != groupLeader && !p->testWFlags( TQt::WGroupLeader) ) { + p = p->parentWidget(); + } + modal = qt_modal_stack->next(); + if ( p == groupLeader ) unrelated = FALSE; + } + + if ( unrelated ) + return TRUE; // don't block event + } + return FALSE; +} + + +/*! + Returns the desktop widget (also called the root window). + + The desktop widget is useful for obtaining the size of the screen. + It may also be possible to draw on the desktop. We recommend against + assuming that it's possible to draw on the desktop, since this does + not work on all operating systems. + + \code + TQDesktopWidget *d = TQApplication::desktop(); + int w = d->width(); // returns desktop width + int h = d->height(); // returns desktop height + \endcode +*/ + +TQDesktopWidget *TQApplication::desktop() +{ + if ( !qt_desktopWidget || // not created yet + !qt_desktopWidget->isDesktop() ) { // reparented away + qt_desktopWidget = new TQDesktopWidget(); + Q_CHECK_PTR( qt_desktopWidget ); + } + return qt_desktopWidget; +} + +#ifndef QT_NO_CLIPBOARD +/*! + Returns a pointer to the application global clipboard. +*/ +TQClipboard *TQApplication::clipboard() +{ + if ( qt_clipboard == 0 ) { + qt_clipboard = new TQClipboard; + Q_CHECK_PTR( qt_clipboard ); + } + return qt_clipboard; +} +#endif // QT_NO_CLIPBOARD + +/*! + By default, TQt will try to use the current standard colors, fonts + etc., from the underlying window system's desktop settings, + and use them for all relevant widgets. This behavior can be switched off + by calling this function with \a on set to FALSE. + + This static function must be called before creating the TQApplication + object, like this: + + \code + int main( int argc, char** argv ) { + TQApplication::setDesktopSettingsAware( FALSE ); // I know better than the user + TQApplication myApp( argc, argv ); // Use default fonts & colors + ... + } + \endcode + + \sa desktopSettingsAware() +*/ + +void TQApplication::setDesktopSettingsAware( bool on ) +{ + obey_desktop_settings = on; +} + +/*! + Returns the value set by setDesktopSettingsAware(); by default TRUE. + + \sa setDesktopSettingsAware() +*/ + +bool TQApplication::desktopSettingsAware() +{ + return obey_desktop_settings; +} + +/*! \fn void TQApplication::lock() + + Lock the TQt Library Mutex. If another thread has already locked the + mutex, the calling thread will block until the other thread has + unlocked the mutex. + + \sa unlock() locked() \link threads.html Thread Support in TQt\endlink +*/ + + +/*! \fn void TQApplication::unlock(bool wakeUpGui) + + Unlock the TQt Library Mutex. If \a wakeUpGui is TRUE (the default), + then the GUI thread will be woken with TQApplication::wakeUpGuiThread(). + + \sa lock(), locked() \link threads.html Thread Support in TQt\endlink +*/ + + +/*! \fn bool TQApplication::locked() + + Returns TRUE if the TQt Library Mutex is locked by a different thread; + otherwise returns FALSE. + + \warning Due to different implementations of recursive mutexes on + the supported platforms, calling this function from the same thread + that previously locked the mutex will give undefined results. + + \sa lock() unlock() \link threads.html Thread Support in TQt\endlink +*/ + +/*! \fn bool TQApplication::tryLock() + + Attempts to lock the TQt Library Mutex, and returns immediately. If + the lock was obtained, this function returns TRUE. If another thread + has locked the mutex, this function returns FALSE, instead of + waiting for the lock to become available. + + The mutex must be unlocked with unlock() before another thread can + successfully lock it. + + \sa lock(), unlock() \link threads.html Thread Support in TQt\endlink +*/ + +#if defined(QT_THREAD_SUPPORT) +void TQApplication::lock() +{ + qt_mutex->lock(); +} + +void TQApplication::unlock(bool wakeUpGui) +{ + qt_mutex->unlock(); + + if (wakeUpGui) + wakeUpGuiThread(); +} + +bool TQApplication::locked() +{ + return qt_mutex->locked(); +} + +bool TQApplication::tryLock() +{ + return qt_mutex->tryLock(); +} +#endif + + +/*! + \fn bool TQApplication::isSessionRestored() const + + Returns TRUE if the application has been restored from an earlier + \link session.html session\endlink; otherwise returns FALSE. + + \sa sessionId(), commitData(), saveState() +*/ + + +/*! + \fn TQString TQApplication::sessionId() const + + Returns the current \link session.html session's\endlink identifier. + + If the application has been restored from an earlier session, this + identifier is the same as it was in that previous session. + + The session identifier is guaranteed to be unique both for different + applications and for different instances of the same application. + + \sa isSessionRestored(), sessionKey(), commitData(), saveState() + */ + +/*! + \fn TQString TQApplication::sessionKey() const + + Returns the session key in the current \link session.html + session\endlink. + + If the application has been restored from an earlier session, this + key is the same as it was when the previous session ended. + + The session key changes with every call of commitData() or + saveState(). + + \sa isSessionRestored(), sessionId(), commitData(), saveState() + */ + + +/*! + \fn void TQApplication::commitData( TQSessionManager& sm ) + + This function deals with \link session.html session + management\endlink. It is invoked when the TQSessionManager wants the + application to commit all its data. + + Usually this means saving all open files, after getting + permission from the user. Furthermore you may want to provide a means + by which the user can cancel the shutdown. + + Note that you should not exit the application within this function. + Instead, the session manager may or may not do this afterwards, + depending on the context. + + \warning Within this function, no user interaction is possible, \e + unless you ask the session manager \a sm for explicit permission. + See TQSessionManager::allowsInteraction() and + TQSessionManager::allowsErrorInteraction() for details and example + usage. + + The default implementation requests interaction and sends a close + event to all visible top level widgets. If any event was + rejected, the shutdown is canceled. + + \sa isSessionRestored(), sessionId(), saveState(), \link session.html the Session Management overview\endlink +*/ +#ifndef QT_NO_SESSIONMANAGER +void TQApplication::commitData( TQSessionManager& sm ) +{ + + if ( sm.allowsInteraction() ) { + TQWidgetList done; + TQWidgetList *list = TQApplication::topLevelWidgets(); + bool cancelled = FALSE; + TQWidget* w = list->first(); + while ( !cancelled && w ) { + if ( !w->isHidden() ) { + TQCloseEvent e; + sendEvent( w, &e ); + cancelled = !e.isAccepted(); + if ( !cancelled ) + done.append( w ); + delete list; // one never knows... + list = TQApplication::topLevelWidgets(); + w = list->first(); + } else { + w = list->next(); + } + while ( w && done.containsRef( w ) ) + w = list->next(); + } + delete list; + if ( cancelled ) + sm.cancel(); + } +} + + +/*! + \fn void TQApplication::saveState( TQSessionManager& sm ) + + This function deals with \link session.html session + management\endlink. It is invoked when the + \link TQSessionManager session manager \endlink wants the application + to preserve its state for a future session. + + For example, a text editor would create a temporary file that + includes the current contents of its edit buffers, the location of + the cursor and other aspects of the current editing session. + + Note that you should never exit the application within this + function. Instead, the session manager may or may not do this + afterwards, depending on the context. Futhermore, most session + managers will very likely request a saved state immediately after + the application has been started. This permits the session manager + to learn about the application's restart policy. + + \warning Within this function, no user interaction is possible, \e + unless you ask the session manager \a sm for explicit permission. + See TQSessionManager::allowsInteraction() and + TQSessionManager::allowsErrorInteraction() for details. + + \sa isSessionRestored(), sessionId(), commitData(), \link session.html the Session Management overview\endlink +*/ + +void TQApplication::saveState( TQSessionManager& /* sm */ ) +{ +} +#endif //QT_NO_SESSIONMANAGER +/*! + Sets the time after which a drag should start to \a ms ms. + + \sa startDragTime() +*/ + +void TQApplication::setStartDragTime( int ms ) +{ + drag_time = ms; +} + +/*! + If you support drag and drop in you application and a drag should + start after a mouse click and after a certain time elapsed, you + should use the value which this method returns as the delay (in ms). + + TQt also uses this delay internally, e.g. in TQTextEdit and TQLineEdit, + for starting a drag. + + The default value is 500 ms. + + \sa setStartDragTime(), startDragDistance() +*/ + +int TQApplication::startDragTime() +{ + return drag_time; +} + +/*! + Sets the distance after which a drag should start to \a l pixels. + + \sa startDragDistance() +*/ + +void TQApplication::setStartDragDistance( int l ) +{ + drag_distance = l; +} + +/*! + If you support drag and drop in you application and a drag should + start after a mouse click and after moving the mouse a certain + distance, you should use the value which this method returns as the + distance. + + For example, if the mouse position of the click is stored in \c + startPos and the current position (e.g. in the mouse move event) is + \c currPos, you can find out if a drag should be started with code + like this: + \code + if ( ( startPos - currPos ).manhattanLength() > + TQApplication::startDragDistance() ) + startTheDrag(); + \endcode + + TQt uses this value internally, e.g. in TQFileDialog. + + The default value is 4 pixels. + + \sa setStartDragDistance(), startDragTime(), TQPoint::manhattanLength() +*/ + +int TQApplication::startDragDistance() +{ + return drag_distance; +} + +/*! + If \a b is TRUE, all dialogs and widgets will be laid out in a + mirrored fashion, as retquired by right to left languages such as + Arabic and Hebrew. If \a b is FALSE, dialogs and widgets are laid + out left to right. + + Changing this flag in runtime does not cause a relayout of already + instantiated widgets. + + \sa reverseLayout() +*/ +void TQApplication::setReverseLayout( bool b ) +{ + if ( reverse_layout == b ) + return; + + reverse_layout = b; + + TQWidgetList *list = topLevelWidgets(); + TQWidgetListIt it( *list ); + TQWidget *w; + while ( ( w=it.current() ) != 0 ) { + ++it; + postEvent( w, new TQEvent( TQEvent::LayoutDirectionChange ) ); + } + delete list; +} + +/*! + Returns TRUE if all dialogs and widgets will be laid out in a + mirrored (right to left) fashion. Returns FALSE if dialogs and + widgets will be laid out left to right. + + \sa setReverseLayout() +*/ +bool TQApplication::reverseLayout() +{ + return reverse_layout; +} + + +/*! + \class TQSessionManager qsessionmanager.h + \brief The TQSessionManager class provides access to the session manager. + + \ingroup application + \ingroup environment + + The session manager is responsible for session management, most + importantly for interruption and resumption. A "session" is a kind + of record of the state of the system, e.g. which applications were + run at start up and which applications are currently running. The + session manager is used to save the session, e.g. when the machine + is shut down; and to restore a session, e.g. when the machine is + started up. Use TQSettings to save and restore an individual + application's settings, e.g. window positions, recently used files, + etc. + + TQSessionManager provides an interface between the application and + the session manager so that the program can work well with the + session manager. In TQt, session management requests for action + are handled by the two virtual functions TQApplication::commitData() + and TQApplication::saveState(). Both provide a reference to + a session manager object as argument, to allow the application + to communicate with the session manager. + + During a session management action (i.e. within commitData() and + saveState()), no user interaction is possible \e unless the + application got explicit permission from the session manager. You + ask for permission by calling allowsInteraction() or, if it's really + urgent, allowsErrorInteraction(). TQt does not enforce this, but the + session manager may. + + You can try to abort the shutdown process by calling cancel(). The + default commitData() function does this if some top-level window + rejected its closeEvent(). + + For sophisticated session managers provided on Unix/X11, TQSessionManager + offers further possibilites to fine-tune an application's session + management behavior: setRestartCommand(), setDiscardCommand(), + setRestartHint(), setProperty(), requestPhase2(). See the respective + function descriptions for further details. +*/ + +/*! \enum TQSessionManager::RestartHint + + This enum type defines the circumstances under which this + application wants to be restarted by the session manager. The + current values are + + \value RestartIfRunning if the application is still running when + the session is shut down, it wants to be restarted at the start of + the next session. + + \value RestartAnyway the application wants to be started at the + start of the next session, no matter what. (This is useful for + utilities that run just after startup and then tquit.) + + \value RestartImmediately the application wants to be started + immediately whenever it is not running. + + \value RestartNever the application does not want to be restarted + automatically. + + The default hint is \c RestartIfRunning. +*/ + + +/*! + \fn TQString TQSessionManager::sessionId() const + + Returns the identifier of the current session. + + If the application has been restored from an earlier session, this + identifier is the same as it was in that earlier session. + + \sa sessionKey(), TQApplication::sessionId() + */ + +/*! + \fn TQString TQSessionManager::sessionKey() const + + Returns the session key in the current session. + + If the application has been restored from an earlier session, this + key is the same as it was when the previous session ended. + + The session key changes with every call of commitData() or + saveState(). + + \sa sessionId(), TQApplication::sessionKey() + */ + +// ### Note: This function is undocumented, since it is #ifdef'd. + +/*! + \fn void* TQSessionManager::handle() const + + X11 only: returns a handle to the current \c SmcConnection. +*/ + + +/*! + \fn bool TQSessionManager::allowsInteraction() + + Asks the session manager for permission to interact with the + user. Returns TRUE if interaction is permitted; otherwise + returns FALSE. + + The rationale behind this mechanism is to make it possible to + synchronize user interaction during a shutdown. Advanced session + managers may ask all applications simultaneously to commit their + data, resulting in a much faster shutdown. + + When the interaction is completed we strongly recommend releasing the + user interaction semaphore with a call to release(). This way, other + applications may get the chance to interact with the user while your + application is still busy saving data. (The semaphore is implicitly + released when the application exits.) + + If the user decides to cancel the shutdown process during the + interaction phase, you must tell the session manager that this has + happened by calling cancel(). + + Here's an example of how an application's TQApplication::commitData() + might be implemented: + +\code +void MyApplication::commitData( TQSessionManager& sm ) { + if ( sm.allowsInteraction() ) { + switch ( TQMessageBox::warning( + yourMainWindow, + tr("Application Name"), + tr("Save changes to document Foo?"), + tr("&Yes"), + tr("&No"), + tr("Cancel"), + 0, 2) ) { + case 0: // yes + sm.release(); + // save document here; if saving fails, call sm.cancel() + break; + case 1: // continue without saving + break; + default: // cancel + sm.cancel(); + break; + } + } else { + // we did not get permission to interact, then + // do something reasonable instead. + } +} +\endcode + + If an error occurred within the application while saving its data, + you may want to try allowsErrorInteraction() instead. + + \sa TQApplication::commitData(), release(), cancel() +*/ + + +/*! + \fn bool TQSessionManager::allowsErrorInteraction() + + This is similar to allowsInteraction(), but also tells the session + manager that an error occurred. Session managers may give error + interaction request higher priority, which means that it is more likely + that an error interaction is permitted. However, you are still not + guaranteed that the session manager will allow interaction. + + \sa allowsInteraction(), release(), cancel() +*/ + +/*! + \fn void TQSessionManager::release() + + Releases the session manager's interaction semaphore after an + interaction phase. + + \sa allowsInteraction(), allowsErrorInteraction() +*/ + +/*! + \fn void TQSessionManager::cancel() + + Tells the session manager to cancel the shutdown process. Applications + should not call this function without first asking the user. + + \sa allowsInteraction(), allowsErrorInteraction() + +*/ + +/*! + \fn void TQSessionManager::setRestartHint( RestartHint hint ) + + Sets the application's restart hint to \a hint. On application + startup the hint is set to \c RestartIfRunning. + + Note that these flags are only hints, a session manager may or may + not respect them. + + We recommend setting the restart hint in TQApplication::saveState() + because most session managers perform a checkpoint shortly after an + application's startup. + + \sa restartHint() +*/ + +/*! + \fn TQSessionManager::RestartHint TQSessionManager::restartHint() const + + Returns the application's current restart hint. The default is + \c RestartIfRunning. + + \sa setRestartHint() +*/ + +/*! + \fn void TQSessionManager::setRestartCommand( const TQStringList& command ) + + If the session manager is capable of restoring sessions it will + execute \a command in order to restore the application. The command + defaults to + + \code + appname -session id + \endcode + + The \c -session option is mandatory; otherwise TQApplication cannot + tell whether it has been restored or what the current session + identifier is. See TQApplication::isSessionRestored() and + TQApplication::sessionId() for details. + + If your application is very simple, it may be possible to store the + entire application state in additional command line options. This + is usually a very bad idea because command lines are often limited + to a few hundred bytes. Instead, use TQSettings, or temporary files + or a database for this purpose. By marking the data with the unique + sessionId(), you will be able to restore the application in a future + session. + + \sa restartCommand(), setDiscardCommand(), setRestartHint() +*/ + +/*! + \fn TQStringList TQSessionManager::restartCommand() const + + Returns the currently set restart command. + + Note that if you want to iterate over the list, you should + iterate over a copy, e.g. + \code + TQStringList list = mySession.restartCommand(); + TQStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa setRestartCommand(), restartHint() +*/ + +/*! + \fn void TQSessionManager::setDiscardCommand( const TQStringList& ) + + \sa discardCommand(), setRestartCommand() +*/ + + +/*! + \fn TQStringList TQSessionManager::discardCommand() const + + Returns the currently set discard command. + + Note that if you want to iterate over the list, you should + iterate over a copy, e.g. + \code + TQStringList list = mySession.discardCommand(); + TQStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa setDiscardCommand(), restartCommand(), setRestartCommand() +*/ + +/*! + \overload void TQSessionManager::setManagerProperty( const TQString& name, + const TQString& value ) + + Low-level write access to the application's identification and state + records are kept in the session manager. + + The property called \a name has its value set to the string \a value. +*/ + +/*! + \fn void TQSessionManager::setManagerProperty( const TQString& name, + const TQStringList& value ) + + Low-level write access to the application's identification and state + record are kept in the session manager. + + The property called \a name has its value set to the string list \a value. +*/ + +/*! + \fn bool TQSessionManager::isPhase2() const + + Returns TRUE if the session manager is currently performing a second + session management phase; otherwise returns FALSE. + + \sa requestPhase2() +*/ + +/*! + \fn void TQSessionManager::requestPhase2() + + Requests a second session management phase for the application. The + application may then return immediately from the + TQApplication::commitData() or TQApplication::saveState() function, + and they will be called again once most or all other applications have + finished their session management. + + The two phases are useful for applications such as the X11 window manager + that need to store information about another application's windows + and therefore have to wait until these applications have completed their + respective session management tasks. + + Note that if another application has requested a second phase it + may get called before, simultaneously with, or after your + application's second phase. + + \sa isPhase2() +*/ + +/*! + \fn int TQApplication::horizontalAlignment( int align ) + + Strips out vertical alignment flags and transforms an + alignment \a align of AlignAuto into AlignLeft or + AlignRight according to the language used. The other horizontal + alignment flags are left untouched. +*/ + + +/***************************************************************************** + Stubbed session management support + *****************************************************************************/ +#ifndef QT_NO_SESSIONMANAGER +#if defined( QT_NO_SM_SUPPORT ) || defined( Q_WS_WIN ) || defined( Q_WS_MAC ) || defined( Q_WS_QWS ) + +class TQSessionManagerData +{ +public: + TQStringList restartCommand; + TQStringList discardCommand; + TQString sessionId; + TQString sessionKey; + TQSessionManager::RestartHint restartHint; +}; + +TQSessionManager* qt_session_manager_self = 0; +TQSessionManager::TQSessionManager( TQApplication * app, TQString &id, TQString &key ) + : TQObject( app, "qt_sessionmanager" ) +{ + qt_session_manager_self = this; + d = new TQSessionManagerData; +#if defined(Q_WS_WIN) && !defined(Q_OS_TEMP) + wchar_t guidstr[40]; + GUID guid; + CoCreateGuid( &guid ); + StringFromGUID2(guid, guidstr, 40); + id = TQString::fromUcs2((ushort*)guidstr); + CoCreateGuid( &guid ); + StringFromGUID2(guid, guidstr, 40); + key = TQString::fromUcs2((ushort*)guidstr); +#endif + d->sessionId = id; + d->sessionKey = key; + d->restartHint = RestartIfRunning; +} + +TQSessionManager::~TQSessionManager() +{ + delete d; + qt_session_manager_self = 0; +} + +TQString TQSessionManager::sessionId() const +{ + return d->sessionId; +} + +TQString TQSessionManager::sessionKey() const +{ + return d->sessionKey; +} + + +#if defined(Q_WS_X11) || defined(Q_WS_MAC) +void* TQSessionManager::handle() const +{ + return 0; +} +#endif + +#if !defined(Q_WS_WIN) +bool TQSessionManager::allowsInteraction() +{ + return TRUE; +} + +bool TQSessionManager::allowsErrorInteraction() +{ + return TRUE; +} +void TQSessionManager::release() +{ +} + +void TQSessionManager::cancel() +{ +} +#endif + + +void TQSessionManager::setRestartHint( TQSessionManager::RestartHint hint) +{ + d->restartHint = hint; +} + +TQSessionManager::RestartHint TQSessionManager::restartHint() const +{ + return d->restartHint; +} + +void TQSessionManager::setRestartCommand( const TQStringList& command) +{ + d->restartCommand = command; +} + +TQStringList TQSessionManager::restartCommand() const +{ + return d->restartCommand; +} + +void TQSessionManager::setDiscardCommand( const TQStringList& command) +{ + d->discardCommand = command; +} + +TQStringList TQSessionManager::discardCommand() const +{ + return d->discardCommand; +} + +void TQSessionManager::setManagerProperty( const TQString&, const TQString&) +{ +} + +void TQSessionManager::setManagerProperty( const TQString&, const TQStringList& ) +{ +} + +bool TQSessionManager::isPhase2() const +{ + return FALSE; +} + +void TQSessionManager::requestPhase2() +{ +} + +#endif // QT_NO_SM_SUPPORT +#endif //QT_NO_SESSIONMANAGER diff --git a/src/kernel/qapplication.h b/src/kernel/qapplication.h new file mode 100644 index 000000000..7a50a164e --- /dev/null +++ b/src/kernel/qapplication.h @@ -0,0 +1,555 @@ +/**************************************************************************** +** +** Definition of TQApplication class +** +** Created : 931107 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQAPPLICATION_H +#define TQAPPLICATION_H + +#ifndef QT_H +#include "qdesktopwidget.h" +#include "qasciidict.h" +#include "qpalette.h" +#include "qtranslator.h" +#include "qstrlist.h" +#include "qstringlist.h" +#endif // QT_H + +class TQSessionManager; +class TQStyle; +class TQTranslator; +class TQEventLoop; +#if defined(Q_WS_X11) +class TQIMEvent; +#endif +#if defined(Q_WS_QWS) +class TQWSDecoration; +#endif + +#ifdef QT_THREAD_SUPPORT +class TQMutex; +#endif // QT_THREAD_SUPPORT + + +class TQApplication; +extern Q_EXPORT TQApplication *qApp; // global application object + + +class Q_EXPORT TQApplication : public TQObject +{ + Q_OBJECT +public: + TQApplication( int &argc, char **argv ); + TQApplication( int &argc, char **argv, bool GUIenabled ); + enum Type { Tty, GuiClient, GuiServer }; + TQApplication( int &argc, char **argv, Type ); +#if defined(Q_WS_X11) + TQApplication( Display* dpy, HANDLE visual = 0, HANDLE cmap = 0 ); + TQApplication( Display *dpy, int argc, char **argv, + HANDLE visual = 0, HANDLE cmap= 0 ); +#endif + virtual ~TQApplication(); + + int argc() const; + char **argv() const; + + Type type() const; + +#ifndef QT_NO_STYLE + static TQStyle &style(); + static void setStyle( TQStyle* ); + static TQStyle* setStyle( const TQString& ); +#endif +#ifndef Q_QDOC + enum ColorMode { NormalColors, CustomColors }; + static ColorMode colorMode(); + static void setColorMode( TQApplication::ColorMode ); +#endif + + enum ColorSpec { NormalColor=0, CustomColor=1, ManyColor=2 }; + static int colorSpec(); + static void setColorSpec( int ); +#ifndef QT_NO_CURSOR + static TQCursor *overrideCursor(); + static void setOverrideCursor( const TQCursor &, bool replace=FALSE ); + static void restoreOverrideCursor(); +#endif + static bool hasGlobalMouseTracking(); + static void setGlobalMouseTracking( bool enable ); +#ifndef QT_NO_PALETTE + static TQPalette palette( const TQWidget* = 0 ); + static void setPalette( const TQPalette &, bool informWidgets=FALSE, + const char* className = 0 ); +#endif + static TQFont font( const TQWidget* = 0 ); + static void setFont( const TQFont &, bool informWidgets=FALSE, + const char* className = 0 ); + static TQFontMetrics fontMetrics(); + + TQWidget *mainWidget() const; + virtual void setMainWidget( TQWidget * ); + virtual void polish( TQWidget * ); + + static TQWidgetList *allWidgets(); + static TQWidgetList *topLevelWidgets(); + + static TQDesktopWidget *desktop(); + + static TQWidget *activePopupWidget(); + static TQWidget *activeModalWidget(); +#ifndef QT_NO_CLIPBOARD + static TQClipboard *clipboard(); +#endif + TQWidget *focusWidget() const; + TQWidget *activeWindow() const; + + static TQWidget *widgetAt( int x, int y, bool child=FALSE ); + static TQWidget *widgetAt( const TQPoint &, bool child=FALSE ); + + static TQEventLoop *eventLoop(); + + int exec(); + void processEvents(); + void processEvents( int maxtime ); + void processOneEvent(); + bool hasPendingEvents(); + int enter_loop(); + void exit_loop(); + int loopLevel() const; + static void exit( int retcode=0 ); + + static bool sendEvent( TQObject *receiver, TQEvent *event ); + static void postEvent( TQObject *receiver, TQEvent *event ); + static void sendPostedEvents( TQObject *receiver, int event_type ); + static void sendPostedEvents(); + + static void removePostedEvents( TQObject *receiver ); + + virtual bool notify( TQObject *, TQEvent * ); + + static bool startingUp(); + static bool closingDown(); + + static void flushX(); + static void flush(); + static void syncX(); + + static void beep(); + +#ifndef QT_NO_TRANSLATION +# ifndef QT_NO_TEXTCODEC + void setDefaultCodec( TQTextCodec * ); + TQTextCodec* defaultCodec() const; +# endif + void installTranslator( TQTranslator * ); + void removeTranslator( TQTranslator * ); +#endif + enum Encoding { DefaultCodec, UnicodeUTF8 }; + TQString translate( const char * context, + const char * key, + const char * comment = 0, + Encoding encoding = DefaultCodec ) const; +#ifndef QT_NO_DIR + TQString applicationDirPath(); + TQString applicationFilePath(); +#endif +#ifndef QT_NO_PALETTE + // obsolete functions + static void setWinStyleHighlightColor( const TQColor &c ) { + TQPalette p( palette() ); + p.setColor( TQColorGroup::Highlight, c ); + setPalette( p, TRUE); + } + static const TQColor &winStyleHighlightColor() { + return palette().active().highlight(); + } +#endif + static void setDesktopSettingsAware( bool ); + static bool desktopSettingsAware(); + + static void setCursorFlashTime( int ); + static int cursorFlashTime(); + + static void setDoubleClickInterval( int ); + static int doubleClickInterval(); +#ifndef QT_NO_WHEELEVENT + static void setWheelScrollLines( int ); + static int wheelScrollLines(); +#endif + static void setGlobalStrut( const TQSize & ); + static TQSize globalStrut(); + +#ifndef QT_NO_COMPONENT + static void setLibraryPaths( const TQStringList & ); + static TQStringList libraryPaths(); + static void addLibraryPath( const TQString & ); + static void removeLibraryPath( const TQString & ); +#endif // QT_NO_COMPONENT + static void setStartDragTime( int ms ); + static int startDragTime(); + static void setStartDragDistance( int l ); + static int startDragDistance(); + + static void setReverseLayout( bool b ); + static bool reverseLayout(); + + static int horizontalAlignment( int align ); + + static bool isEffectEnabled( TQt::UIEffect ); + static void setEffectEnabled( TQt::UIEffect, bool enable = TRUE ); + +#if defined(Q_WS_MAC) + virtual bool macEventFilter( EventHandlerCallRef, EventRef ); +#endif +#if defined(Q_WS_WIN) + virtual bool winEventFilter( MSG * ); +#endif +#if defined(Q_WS_X11) + virtual bool x11EventFilter( XEvent * ); + virtual int x11ClientMessage( TQWidget*, XEvent*, bool passive_only); + int x11ProcessEvent( XEvent* ); +#endif +#if defined(Q_WS_QWS) + virtual bool qwsEventFilter( TQWSEvent * ); + int qwsProcessEvent( TQWSEvent* ); + void qwsSetCustomColors( TQRgb *colortable, int start, int numColors ); +/*! + \internal + Returns true if the process is GUI server +*/ + bool qwsIsGUIServer(); +#ifndef QT_NO_QWS_MANAGER + static TQWSDecoration &qwsDecoration(); + static void qwsSetDecoration( TQWSDecoration *); +#endif +#endif + +#if defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN) + static WindowsVersion winVersion(); +#elif defined(Q_OS_MAC) + static MacintoshVersion macVersion(); +#endif +#if defined(Q_WS_WIN) + void winFocus( TQWidget *, bool ); + static void winMouseButtonUp(); +#endif + +#ifndef QT_NO_SESSIONMANAGER + // session management + bool isSessionRestored() const; + TQString sessionId() const; + TQString sessionKey() const; + virtual void commitData( TQSessionManager& sm ); + virtual void saveState( TQSessionManager& sm ); +#endif +#if defined(Q_WS_X11) +#if !defined(QT_NO_IM_EXTENSIONS) + virtual TQWidget *locateICHolderWidget( TQWidget *w ); + virtual TQWidgetList *icHolderWidgets(); + static void create_im(); + static void close_im(); +#else + TQWidget *locateICHolderWidget( TQWidget *w ); + TQWidgetList *icHolderWidgets(); + static void create_xim(); + static void close_xim(); +#endif + static TQString defaultInputMethod(); + void changeAllInputContext( const TQString & ); + static bool x11_apply_settings(); +#endif + void wakeUpGuiThread(); +#if defined(QT_THREAD_SUPPORT) + void lock(); + void unlock(bool wakeUpGui = TRUE); + bool locked(); + bool tryLock(); +#endif + +signals: + void lastWindowClosed(); + void aboutToQuit(); + void guiThreadAwake(); + +public slots: + void tquit(); + void closeAllWindows(); + void aboutTQt(); + +#if defined(Q_WS_QWS) +protected: + void setArgs(int, char **); +#endif + +protected: + bool event(TQEvent *); + +private: + void construct( int &argc, char **argv, Type ); + void initialize( int, char ** ); + void init_precmdline(); + void process_cmdline( int* argcptr, char ** argv ); + bool internalNotify( TQObject *, TQEvent * ); +#if defined(Q_WS_QWS) + static TQWidget *findChildWidget( const TQWidget *p, const TQPoint &pos ); + static TQWidget *findWidget( const TQObjectList&, const TQPoint &, bool rec ); +#endif + +#if defined(Q_WS_MAC) + bool do_mouse_down(Point *, bool *); + static TQMAC_PASCAL OSStatus globalEventProcessor(EventHandlerCallRef, EventRef, void *); + static TQMAC_PASCAL OSStatus globalAppleEventProcessor(const AppleEvent *, AppleEvent *, long); + static TQMAC_PASCAL void qt_context_timer_callbk(EventLoopTimerRef, void *); + static TQMAC_PASCAL void qt_select_timer_callbk(EventLoopTimerRef, void *); + static bool qt_mac_apply_settings(); + friend class TQMacInputMethod; + friend TQMAC_PASCAL OSStatus qt_window_event(EventHandlerCallRef, EventRef, void *); + friend void qt_mac_update_os_settings(); + friend bool qt_set_socket_handler( int, int, TQObject *, bool); + friend void qt_mac_destroy_widget(TQWidget *); + friend void qt_init(int *, char **, TQApplication::Type); +#endif + +#if defined(Q_WS_X11) +private slots: + void postIMEvent( TQObject *receiver, TQIMEvent *event ); +#endif + +private: +#ifdef QT_THREAD_SUPPORT + static TQMutex *qt_mutex; +#endif // QT_THREAD_SUPPORT + + int app_argc; + char **app_argv; + bool tquit_now; + int tquit_code; + static TQStyle *app_style; + static int app_cspec; +#ifndef QT_NO_PALETTE + static TQPalette *app_pal; +#endif + static TQFont *app_font; +#ifndef QT_NO_CURSOR + static TQCursor *app_cursor; +#endif + static TQEventLoop* eventloop; + static int app_tracking; + static bool is_app_running; + static bool is_app_closing; + static bool app_exit_loop; + static int loop_level; + static TQWidget *main_widget; + static TQWidget *focus_widget; + static TQWidget *active_window; + static bool obey_desktop_settings; + static int cursor_flash_time; + static int mouse_double_click_time; + static int wheel_scroll_lines; + static int composedUnicode; // Value, meta-composed character + + static bool animate_ui; + static bool animate_menu; + static bool animate_tooltip; + static bool animate_combo; + static bool fade_menu; + static bool fade_tooltip; + static bool animate_toolbox; + static bool widgetCount; // Coupled with -widgetcount switch + static bool metaComposeUnicode; + + TQValueList *translators; +#ifndef QT_NO_SESSIONMANAGER + TQSessionManager *session_manager; + TQString session_id; + static TQString* session_key; + bool is_session_restored; +#endif +#if defined(Q_WS_X11) +#if !defined (QT_NO_STYLE) + static void x11_initialize_style(); +#endif + static TQString defaultIM; // default input method's name in this application. +#endif + + static TQSize app_strut; +#ifndef QT_NO_COMPONENT + static TQStringList *app_libpaths; +#endif + static TQAsciiDict *app_palettes; + static TQAsciiDict *app_fonts; + + static TQWidgetList *popupWidgets; + bool inPopupMode() const; + void closePopup( TQWidget *popup ); + void openPopup( TQWidget *popup ); + void setActiveWindow( TQWidget* act ); + + static bool sendSpontaneousEvent( TQObject *receiver, TQEvent *event ); + static void removePostedEvent( TQEvent * ); + static void removePostedEvents( TQObject *receiver, int event_type ); + + friend class TQWidget; + friend class TQETWidget; + friend class TQDialog; + friend class TQAccelManager; + friend class TQEvent; + friend class TQTranslator; + friend class TQEventLoop; + friend Q_EXPORT void qt_ucm_initialize( TQApplication * ); +#if defined(Q_WS_WIN) + friend bool qt_sendSpontaneousEvent( TQObject*, TQEvent* ); +#endif +#if defined(Q_WS_QWS) + friend class TQInputContext; +#endif +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQApplication( const TQApplication & ); + TQApplication &operator=( const TQApplication & ); +#endif +}; + +inline int TQApplication::argc() const +{ + return app_argc; +} + +inline char **TQApplication::argv() const +{ + return app_argv; +} + +#if defined(Q_WS_QWS) +inline void TQApplication::setArgs(int c, char **v) +{ + app_argc = c; + app_argv = v; +} +#endif + +#ifndef QT_NO_CURSOR +inline TQCursor *TQApplication::overrideCursor() +{ + return app_cursor; +} +#endif +inline bool TQApplication::hasGlobalMouseTracking() +{ + return app_tracking > 0; +} + +inline TQWidget *TQApplication::mainWidget() const +{ + return main_widget; +} + +inline TQWidget *TQApplication::focusWidget() const +{ + return focus_widget; +} + +inline TQWidget *TQApplication::activeWindow() const +{ + return active_window; +} + +inline TQWidget *TQApplication::widgetAt( const TQPoint &p, bool child ) +{ + return widgetAt( p.x(), p.y(), child ); +} + +inline bool TQApplication::inPopupMode() const +{ + return popupWidgets != 0; +} +#ifndef QT_NO_SESSIONMANAGER +inline bool TQApplication::isSessionRestored() const +{ + return is_session_restored; +} + +inline TQString TQApplication::sessionId() const +{ + return session_id; +} + +inline TQString TQApplication::sessionKey() const +{ + return session_key ? *session_key : TQString::null; +} +#endif +inline TQSize TQApplication::globalStrut() +{ + return app_strut; +} + +inline bool TQApplication::sendEvent( TQObject *receiver, TQEvent *event ) +{ if ( event ) event->spont = FALSE; return qApp ? qApp->notify( receiver, event ) : FALSE; } + +inline bool TQApplication::sendSpontaneousEvent( TQObject *receiver, TQEvent *event ) +{ if ( event ) event->spont = TRUE; return qApp ? qApp->notify( receiver, event ) : FALSE; } + +#ifdef QT_NO_TRANSLATION +// Simple versions +inline TQString TQApplication::translate( const char *, const char *sourceText, + const char *, Encoding encoding ) const +{ +#ifndef QT_NO_TEXTCODEC + if ( encoding == UnicodeUTF8 ) + return TQString::fromUtf8( sourceText ); + else +#endif + return TQString::fromLatin1( sourceText ); +} +#endif + +inline int TQApplication::horizontalAlignment( int align ) +{ + align &= AlignHorizontal_Mask; + if ( align == AlignAuto ) { + if ( reverseLayout() ) + align = AlignRight; + else + align = AlignLeft; + } + return align; +} + +#endif // TQAPPLICATION_H + diff --git a/src/kernel/qapplication_p.h b/src/kernel/qapplication_p.h new file mode 100644 index 000000000..e3981274d --- /dev/null +++ b/src/kernel/qapplication_p.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Definition of some TQt private functions. +** +** Created : 000228 +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQAPPLICATION_P_H +#define TQAPPLICATION_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of qapplication_*.cpp, qwidget*.cpp, qcolor_x11.cpp, qfiledialog.cpp +// and many other. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#endif // QT_H + +class TQWidget; +class TQObject; +class TQClipboard; +class TQKeyEvent; +class TQMouseEvent; +class TQWheelEvent; + +extern Q_EXPORT bool qt_modal_state(); +extern Q_EXPORT void qt_enter_modal( TQWidget* ); +extern Q_EXPORT void qt_leave_modal( TQWidget* ); + +extern bool qt_is_gui_used; +#ifndef QT_NO_CLIPBOARD +extern TQClipboard *qt_clipboard; +#endif + +#if defined (Q_OS_WIN32) || defined (Q_OS_CYGWIN) +extern TQt::WindowsVersion qt_winver; +const int QT_TABLET_NPACKETQSIZE = 128; +# ifdef Q_OS_TEMP + extern DWORD qt_cever; +# endif +#elif defined (Q_OS_MAC) +extern TQt::MacintoshVersion qt_macver; +#endif + +#if defined (Q_WS_X11) +extern int qt_ncols_option; +#endif + + +extern void qt_dispatchEnterLeave( TQWidget*, TQWidget* ); +extern bool qt_tryModalHelper( TQWidget *, TQWidget ** = 0 ); + +#endif diff --git a/src/kernel/qapplication_x11.cpp b/src/kernel/qapplication_x11.cpp new file mode 100644 index 000000000..1e2b6c8e3 --- /dev/null +++ b/src/kernel/qapplication_x11.cpp @@ -0,0 +1,6653 @@ +/**************************************************************************** +** +** Implementation of X11 startup routines and event handling +** +** Created : 931029 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +// ### 4.0: examine Q_EXPORT's below. The respective symbols had all +// been in use (e.g. in the KDE wm ) before the introduction of a version +// map. One might want to turn some of them into propert public API and +// provide a proper alternative for others. See also the exports in +// qapplication_win.cpp which suggest a unification. + +// ### needed for solaris-g++ in beta5 +#define QT_CLEAN_NAMESPACE + +#include "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +// Solaris redefines connect -> __xnet_connect with _XOPEN_SOURCE_EXTENDED. +#if defined(connect) +# undef connect +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +#include "qapplication.h" +#include "qapplication_p.h" +#include "qcolor_p.h" +#include "qcursor.h" +#include "qwidget.h" +#include "qwidget_p.h" +#include "qobjectlist.h" +#include "qwidgetlist.h" +#include "qwidgetintdict.h" +#include "qbitarray.h" +#include "qpainter.h" +#include "qpixmapcache.h" +#include "qdatetime.h" +#include "qtextcodec.h" +#include "qdatastream.h" +#include "qbuffer.h" +#include "qsocketnotifier.h" +#include "qsessionmanager.h" +#include "qvaluelist.h" +#include "qdict.h" +#include "qguardedptr.h" +#include "qclipboard.h" +#include "qwhatsthis.h" // ######## dependency +#include "qsettings.h" +#include "qstylefactory.h" +#include "qfileinfo.h" + +// Input method stuff - UNFINISHED +#ifndef QT_NO_IM +#include "qinputcontext.h" +#endif // QT_NO_IM +#include "qinternal_p.h" // shared double buffer cleanup + +#if defined(QT_THREAD_SUPPORT) +# include "qthread.h" +#endif + +#if defined(QT_DEBUG) && defined(Q_OS_LINUX) +# include "qfile.h" +#endif + +#include "qt_x11_p.h" + +#if !defined(QT_NO_XFTFREETYPE) +// XFree86 4.0.3 implementation is missing XftInitFtLibrary forward +extern "C" Bool XftInitFtLibrary(void); +#endif + +#include +#include +#include +#include +#include + +//#define X_NOT_BROKEN +#ifdef X_NOT_BROKEN +// Some X libraries are built with setlocale #defined to _Xsetlocale, +// even though library users are then built WITHOUT such a definition. +// This creates a problem - TQt might setlocale() one value, but then +// X looks and doesn't see the value TQt set. The solution here is to +// implement _Xsetlocale just in case X calls it - redirecting it to +// the real libC version. +// +# ifndef setlocale +extern "C" char *_Xsetlocale(int category, const char *locale); +char *_Xsetlocale(int category, const char *locale) +{ + //qDebug("_Xsetlocale(%d,%s),category,locale"); + return setlocale(category,locale); +} +# endif // setlocale +#endif // X_NOT_BROKEN + + +// resolve the conflict between X11's FocusIn and TQEvent::FocusIn +const int XFocusOut = FocusOut; +const int XFocusIn = FocusIn; +#undef FocusOut +#undef FocusIn + +const int XKeyPress = KeyPress; +const int XKeyRelease = KeyRelease; +#undef KeyPress +#undef KeyRelease + + +// Fix old X libraries +#ifndef XK_KP_Home +#define XK_KP_Home 0xFF95 +#endif +#ifndef XK_KP_Left +#define XK_KP_Left 0xFF96 +#endif +#ifndef XK_KP_Up +#define XK_KP_Up 0xFF97 +#endif +#ifndef XK_KP_Right +#define XK_KP_Right 0xFF98 +#endif +#ifndef XK_KP_Down +#define XK_KP_Down 0xFF99 +#endif +#ifndef XK_KP_Prior +#define XK_KP_Prior 0xFF9A +#endif +#ifndef XK_KP_Next +#define XK_KP_Next 0xFF9B +#endif +#ifndef XK_KP_End +#define XK_KP_End 0xFF9C +#endif +#ifndef XK_KP_Insert +#define XK_KP_Insert 0xFF9E +#endif +#ifndef XK_KP_Delete +#define XK_KP_Delete 0xFF9F +#endif + + +/***************************************************************************** + Internal variables and functions + *****************************************************************************/ +static const char *appName; // application name +static const char *appClass; // application class +static const char *appFont = 0; // application font +static const char *appBGCol = 0; // application bg color +static const char *appFGCol = 0; // application fg color +static const char *appBTNCol = 0; // application btn color +static const char *mwGeometry = 0; // main widget geometry +static const char *mwTitle = 0; // main widget title +//Ming-Che 10/10 +char *qt_ximServer = 0; // XIM Server will connect to +static bool mwIconic = FALSE; // main widget iconified +//Ming-Che 10/10 +static Display *appDpy = 0; // X11 application display +static char *appDpyName = 0; // X11 display name +static bool appForeignDpy = FALSE; // we didn't create display +static bool appSync = FALSE; // X11 synchronization +#if defined(QT_DEBUG) +static bool appNoGrab = FALSE; // X11 grabbing enabled +static bool appDoGrab = FALSE; // X11 grabbing override (gdb) +#endif +static int appScreen; // X11 screen number +static int appScreenCount; // X11 screen count +static bool app_save_rootinfo = FALSE; // save root info +static bool app_do_modal = FALSE; // modal mode +static Window curWin = 0; // current window + +static GC* app_gc_ro = 0; // read-only GC +static GC* app_gc_tmp = 0; // temporary GC +static GC* app_gc_ro_m = 0; // read-only GC (monochrome) +static GC* app_gc_tmp_m = 0; // temporary GC (monochrome) +// symbols needed by extern TQXEmbed class +Q_EXPORT Atom qt_wm_protocols = 0; // window manager protocols +Q_EXPORT Atom qt_wm_delete_window = 0; // delete window protocol +Q_EXPORT Atom qt_wm_take_focus = 0; // take focus window protocol + +Atom qt_qt_scrolldone = 0; // scroll synchronization +Atom qt_net_wm_context_help = 0; // context help +Atom qt_net_wm_ping = 0; // _NET_WM_PING protocol + +static Atom qt_xsetroot_id = 0; +Atom qt_xa_clipboard = 0; +Atom qt_selection_property = 0; +Atom qt_clipboard_sentinel = 0; +Atom qt_selection_sentinel = 0; +Q_EXPORT Atom qt_wm_state = 0; +Atom qt_wm_change_state = 0; +static Atom qt_settings_timestamp = 0; // TQt >=3 settings timestamp +static Atom qt_input_encoding = 0; // TQt desktop properties +static Atom qt_resource_manager = 0; // X11 Resource manager +Atom qt_sizegrip = 0; // sizegrip +Atom qt_wm_client_leader = 0; +Q_EXPORT Atom qt_window_role = 0; +Q_EXPORT Atom qt_sm_client_id = 0; +Atom qt_xa_motif_wm_hints = 0; +Atom qt_cde_running = 0; +Atom qt_twin_running = 0; +Atom qt_kwm_running = 0; +Atom qt_gbackground_properties = 0; +Atom qt_x_incr = 0; +Atom qt_utf8_string = 0; + +// detect broken window managers +Atom qt_sgi_desks_manager = 0; +bool qt_broken_wm = FALSE; +static void qt_detect_broken_window_manager(); + +// NET WM support +Atom qt_net_supported = 0; +Atom qt_net_wm_name = 0; +Atom qt_net_wm_icon_name = 0; +Atom qt_net_virtual_roots = 0; +Atom qt_net_workarea = 0; +Atom qt_net_wm_state = 0; +Atom qt_net_wm_state_modal = 0; +Atom qt_net_wm_state_max_v = 0; +Atom qt_net_wm_state_max_h = 0; +Atom qt_net_wm_state_fullscreen = 0; +Atom qt_net_wm_state_above = 0; +Atom qt_net_wm_window_type = 0; +Atom qt_net_wm_window_type_normal = 0; +Atom qt_net_wm_window_type_dialog = 0; +Atom qt_net_wm_window_type_toolbar = 0; +Atom qt_net_wm_window_type_menu = 0; +Atom qt_net_wm_window_type_utility = 0; +Atom qt_net_wm_window_type_splash = 0; +Atom qt_net_wm_window_type_override = 0; // KDE extension +Atom qt_net_wm_window_type_dropdown_menu = 0; +Atom qt_net_wm_window_type_popup_menu = 0; +Atom qt_net_wm_window_type_tooltip = 0; +Atom qt_net_wm_window_type_combo = 0; +Atom qt_net_wm_window_type_dnd = 0; +Atom qt_net_wm_frame_strut = 0; // KDE extension +Atom qt_net_wm_state_stays_on_top = 0; // KDE extension +Atom qt_net_wm_pid = 0; +Atom qt_net_wm_user_time = 0; +Atom qt_net_wm_full_placement = 0; // KDE extension +// Enlightenment support +Atom qt_enlightenment_desktop = 0; + +// window managers list of supported "stuff" +Atom *qt_net_supported_list = 0; +// list of virtual root windows +Window *qt_net_virtual_root_list = 0; + + +// X11 SYNC support +#ifndef QT_NO_XSYNC +Atom qt_net_wm_sync_request_counter = 0; +Atom qt_net_wm_sync_request = 0; +#endif + +// client leader window +Window qt_x11_wm_client_leader = 0; + +// function to update the workarea of the screen - in qdesktopwidget_x11.cpp +extern void qt_desktopwidget_update_workarea(); + +// current focus model +static const int FocusModel_Unknown = -1; +static const int FocusModel_Other = 0; +static const int FocusModel_PointerRoot = 1; +static int qt_focus_model = -1; + +#ifndef QT_NO_XRANDR +// TRUE if TQt is compiled w/ XRandR support and XRandR exists on the connected +// Display +bool qt_use_xrandr = FALSE; +static int xrandr_eventbase; +#endif + +// TRUE if TQt is compiled w/ XRender support and XRender exists on the connected +// Display +Q_EXPORT bool qt_use_xrender = FALSE; + +#ifndef QT_NO_XSYNC +// True if SYNC extension exists on the connected display +bool qt_use_xsync = FALSE; +static int xsync_eventbase; +static int xsync_errorbase; +#endif + +// modifier masks for alt/meta - detected when the application starts +static long qt_alt_mask = 0; +static long qt_meta_mask = 0; +// modifier mask to remove mode switch from modifiers that have alt/meta set +// this problem manifests itself on HP/UX 10.20 at least, and without it +// modifiers do not work at all... +static long qt_mode_switch_remove_mask = 0; + +// flags for extensions for special Languages, currently only for RTL languages +static bool qt_use_rtl_extensions = FALSE; +bool qt_hebrew_keyboard_hack = FALSE; + +static Window mouseActWindow = 0; // window where mouse is +static int mouseButtonPressed = 0; // last mouse button pressed +static int mouseButtonState = 0; // mouse button state +static Time mouseButtonPressTime = 0; // when was a button pressed +static short mouseXPos, mouseYPos; // mouse pres position in act window +static short mouseGlobalXPos, mouseGlobalYPos; // global mouse press position + +extern TQWidgetList *qt_modal_stack; // stack of modal widgets +static bool ignoreNextMouseReleaseEvent = FALSE; // ignore the next mouse release + // event if return from a modal + // widget + +static TQWidget *popupButtonFocus = 0; +static TQWidget *popupOfPopupButtonFocus = 0; +static bool popupCloseDownMode = FALSE; +static bool popupGrabOk; + +static bool sm_blockUserInput = FALSE; // session management + +int qt_xfocusout_grab_counter = 0; + +#if defined (QT_TABLET_SUPPORT) +// since XInput event classes aren't created until we actually open an XInput +// device, here is a static list that we will use later on... +const int INVALID_EVENT = -1; +const int TOTAL_XINPUT_EVENTS = 7; + +XDevice *devStylus = NULL; +XDevice *devEraser = NULL; +XEventClass event_list_stylus[TOTAL_XINPUT_EVENTS]; +XEventClass event_list_eraser[TOTAL_XINPUT_EVENTS]; + +int qt_curr_events_stylus = 0; +int qt_curr_events_eraser = 0; + +// well, luckily we only need to do this once. +static int xinput_motion = INVALID_EVENT; +static int xinput_key_press = INVALID_EVENT; +static int xinput_key_release = INVALID_EVENT; +static int xinput_button_press = INVALID_EVENT; +static int xinput_button_release = INVALID_EVENT; + +// making this assumption on XFree86, since we can only use 1 device, +// the pressure for the eraser and the stylus should be the same, if they aren't +// well, they certainly have a strange pen then... +static int max_pressure; +extern bool chokeMouse; +#endif + +// last timestamp read from TQSettings +static uint appliedstamp = 0; + + +typedef int (*QX11EventFilter) (XEvent*); +QX11EventFilter qt_set_x11_event_filter(QX11EventFilter filter); + +static QX11EventFilter qt_x11_event_filter = 0; +Q_EXPORT QX11EventFilter qt_set_x11_event_filter(QX11EventFilter filter) +{ + QX11EventFilter old_filter = qt_x11_event_filter; + qt_x11_event_filter = filter; + return old_filter; +} +static bool qt_x11EventFilter( XEvent* ev ) +{ + if ( qt_x11_event_filter && qt_x11_event_filter( ev ) ) + return TRUE; + return qApp->x11EventFilter( ev ); +} + + + + + +#if !defined(QT_NO_XIM) +//XIM qt_xim = 0; +XIMStyle qt_xim_style = 0; +XIMStyle qt_xim_preferred_style = 0; +static XIMStyle xim_default_style = XIMPreeditCallbacks | XIMStatusNothing; +#endif + +int qt_ximComposingKeycode=0; +TQTextCodec * qt_input_mapper = 0; + +Q_EXPORT Time qt_x_time = CurrentTime; +Q_EXPORT Time qt_x_user_time = CurrentTime; +extern bool qt_check_clipboard_sentinel(); //def in qclipboard_x11.cpp +extern bool qt_check_selection_sentinel(); //def in qclipboard_x11.cpp + +static void qt_save_rootinfo(); +bool qt_try_modal( TQWidget *, XEvent * ); + +int qt_ncols_option = 216; // used in qcolor_x11.cpp +int qt_visual_option = -1; +bool qt_cmap_option = FALSE; +TQWidget *qt_button_down = 0; // widget got last button-down + +extern bool qt_tryAccelEvent( TQWidget*, TQKeyEvent* ); // def in qaccel.cpp + +struct TQScrollInProgress { + static long serial; + TQScrollInProgress( TQWidget* w, int x, int y ) : + id( serial++ ), scrolled_widget( w ), dx( x ), dy( y ) {} + long id; + TQWidget* scrolled_widget; + int dx, dy; +}; +long TQScrollInProgress::serial=0; +static TQPtrList *sip_list = 0; + + +// stuff in qt_xdnd.cpp +// setup +extern void qt_xdnd_setup(); +// x event handling +extern void qt_handle_xdnd_enter( TQWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_position( TQWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_status( TQWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_leave( TQWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_drop( TQWidget *, const XEvent *, bool ); +extern void qt_handle_xdnd_finished( TQWidget *, const XEvent *, bool ); +extern void qt_xdnd_handle_selection_request( const XSelectionRequestEvent * ); +extern bool qt_xdnd_handle_badwindow(); + +extern void qt_motifdnd_handle_msg( TQWidget *, const XEvent *, bool ); +extern void qt_x11_motifdnd_init(); + +// client message atoms +extern Atom qt_xdnd_enter; +extern Atom qt_xdnd_position; +extern Atom qt_xdnd_status; +extern Atom qt_xdnd_leave; +extern Atom qt_xdnd_drop; +extern Atom qt_xdnd_finished; +// xdnd selection atom +extern Atom qt_xdnd_selection; +extern bool qt_xdnd_dragging; + +// gui or non-gui from qapplication.cpp +extern bool qt_is_gui_used; +extern bool qt_app_has_font; + +static bool qt_x11_cmdline_font = false; + + +extern bool qt_resolve_symlinks; // from qapplication.cpp + +// Paint event clipping magic +extern void qt_set_paintevent_clipping( TQPaintDevice* dev, const TQRegion& region); +extern void qt_clear_paintevent_clipping(); + + +// Palette handling +extern TQPalette *qt_std_pal; +extern void qt_create_std_palette(); + +void qt_x11_intern_atom( const char *, Atom * ); + +static TQPtrList* deferred_map_list = 0; +static void qt_deferred_map_cleanup() +{ + delete deferred_map_list; + deferred_map_list = 0; +} +void qt_deferred_map_add( TQWidget* w) +{ + if ( !deferred_map_list ) { + deferred_map_list = new TQPtrList; + qAddPostRoutine( qt_deferred_map_cleanup ); + } + deferred_map_list->append( w ); +} +void qt_deferred_map_take( TQWidget* w ) +{ + if (deferred_map_list ) { + deferred_map_list->remove( w ); + } +} +bool qt_deferred_map_contains( TQWidget* w ) +{ + if (!deferred_map_list) + return FALSE; + else + return deferred_map_list->contains( w ); +} + + +class TQETWidget : public TQWidget // event translator widget +{ +public: + void setWState( WFlags f ) { TQWidget::setWState(f); } + void clearWState( WFlags f ) { TQWidget::clearWState(f); } + void setWFlags( WFlags f ) { TQWidget::setWFlags(f); } + void clearWFlags( WFlags f ) { TQWidget::clearWFlags(f); } + bool translateMouseEvent( const XEvent * ); + bool translateKeyEventInternal( const XEvent *, int& count, TQString& text, int& state, char& ascii, int &code, TQEvent::Type &type, bool willRepeat=FALSE, bool statefulTranslation=TRUE ); + bool translateKeyEvent( const XEvent *, bool grab ); + bool translatePaintEvent( const XEvent * ); + bool translateConfigEvent( const XEvent * ); + bool translateCloseEvent( const XEvent * ); + bool translateScrollDoneEvent( const XEvent * ); + bool translateWheelEvent( int global_x, int global_y, int delta, int state, Orientation orient ); +#if defined (QT_TABLET_SUPPORT) + bool translateXinputEvent( const XEvent* ); +#endif + bool translatePropertyEvent(const XEvent *); +}; + + + + +// ************************************************************************ +// Input Method support +// ************************************************************************ + +/*! + An identifier name of the default input method. +*/ +TQString TQApplication::defaultIM = "imsw-multi"; + + +/*! + This function handles the query about location of the widget + holding the TQInputContext instance for widget \a w. + + The input context is used for text input to widget \a w. By + default, it returns the top-level widget of \a w. + + If you want to change the mapping of widget \w to TQInputContext + instance, reimplement both this function and + TQApplication::icHolderWidgets(). For example, suppose a tabbed web + browser. The browser should allocate a input context per tab + widget because users may switch the tabs and input a new text + during previous input contexts live. + + See also 'Sharing input context between text widgets' and 'Preedit + preservation' section of the class description of TQInputContext. + + \sa TQInputContext, icHolderWidgets() +*/ +TQWidget *TQApplication::locateICHolderWidget( TQWidget *w ) +{ + return w->topLevelWidget(); +} + + +/*! + This function returns all widgets holding TQInputContext. + + By default, This function returns top-level widgets. So if you + want to change the mapping of a widget to TQInputContext instance, + you must override this function and locateICHolderWidget(). + + \sa locateICHolderWidget() +*/ +TQWidgetList *TQApplication::icHolderWidgets() +{ + return TQApplication::topLevelWidgets(); +} + + +/*! + This function replaces all TQInputContext instances in the + application. The function's argument is the identifier name of + the newly selected input method. +*/ +void TQApplication::changeAllInputContext( const TQString &identifierName ) +{ + TQWidgetList *list = qApp->icHolderWidgets(); + TQWidgetListIt it(*list); + while(it.current()) { + it.current()->changeInputContext( identifierName ); + ++it; + } + delete list; + + // defaultIM = identifierName ; // Change of defaultIM -- default input method -- may be enabled. +} + + +/*! + \internal + This is an internal function, you should never call this. + + \sa TQInputContext::imEventGenerated() +*/ +void TQApplication::postIMEvent( TQObject *receiver, TQIMEvent *event ) +{ + if ( event->type() == TQEvent::IMCompose ) { + // enable event compression to reduce preedit flicker on fast + // typing + postEvent( receiver, event ); + } else { + // cancel queued preedit update + if ( event->type() == TQEvent::IMEnd ) + removePostedEvents( receiver, TQEvent::IMCompose ); + + // to avoid event receiving order inversion between TQKeyEvent + // and TQIMEvent, we must send IMStart and IMEnd via + // sendEvent(). + sendEvent( receiver, event ); + delete event; + } +} + + +/*! + This function returns the identifier name of the default input + method in this Application. The value is identical to the value of + TQApplication::defaultIM. +*/ +TQString TQApplication::defaultInputMethod() +{ + return TQApplication::defaultIM; +} + + +#if !defined(QT_NO_IM_EXTENSIONS) +/*! \internal + Creates the application input method. +*/ +void TQApplication::create_im() +{ +#ifndef QT_NO_XIM + if ( ! qt_xim_preferred_style ) // no configured input style, use the default + qt_xim_preferred_style = xim_default_style; +#endif // QT_NO_XIM +} + + +/*! \internal + Closes the application input method. +*/ +void TQApplication::close_im() +{ + TQWidgetList *list = qApp->icHolderWidgets(); + TQWidgetListIt it(*list); + while(it.current()) { + it.current()->destroyInputContext(); + ++it; + } + delete list; +} + +#else + +/*! \internal + Creates the application input method. +*/ +void TQApplication::create_xim() +{ +#ifndef QT_NO_XIM + if ( ! qt_xim_preferred_style ) // no configured input style, use the default + qt_xim_preferred_style = xim_default_style; +#endif // QT_NO_XIM + + TQWidgetList *list= qApp->topLevelWidgets(); + TQWidgetListIt it(*list); + TQWidget * w; + while( (w=it.current()) != 0 ) { + ++it; + w->createTLSysExtra(); + } + delete list; +} + + + /*! \internal + Closes the application input method. + */ +void TQApplication::close_xim() +{ +#ifndef QT_NO_XIM + // Calling XCloseIM gives a Purify FMR error + // XCloseIM( qt_xim ); + // We prefer a less serious memory leak + + // if ( qt_xim ) + // qt_xim = 0; + +#endif // QT_NO_XIM + TQWidgetList *list = qApp->topLevelWidgets(); + TQWidgetListIt it(*list); + while(it.current()) { + it.current()->destroyInputContext(); + ++it; + } + delete list; +} +#endif + +/***************************************************************************** + Default X error handlers + *****************************************************************************/ + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static bool x11_ignore_badwindow; +static bool x11_badwindow; + + // starts to ignore bad window errors from X +void qt_ignore_badwindow() +{ + x11_ignore_badwindow = TRUE; + x11_badwindow = FALSE; +} + + // ends ignoring bad window errors and returns whether an error + // had happen. +bool qt_badwindow() +{ + x11_ignore_badwindow = FALSE; + return x11_badwindow; +} + +static int (*original_x_errhandler)( Display *dpy, XErrorEvent * ); +static int (*original_xio_errhandler)( Display *dpy ); + +static int qt_x_errhandler( Display *dpy, XErrorEvent *err ) +{ + if ( err->error_code == BadWindow ) { + x11_badwindow = TRUE; + if ( err->request_code == 25 /* X_SendEvent */ && + qt_xdnd_handle_badwindow() ) + return 0; + if ( x11_ignore_badwindow ) + return 0; + } else if ( err->error_code == BadMatch && + err->request_code == 42 /* X_SetInputFocus */ ) { + return 0; + } + + char errstr[256]; + XGetErrorText( dpy, err->error_code, errstr, 256 ); + qWarning( "X Error: %s %d\n" + " Major opcode: %d\n" + " Minor opcode: %d\n" + " Resource id: 0x%lx", + errstr, err->error_code, + err->request_code, + err->minor_code, + err->resourceid ); + + // ### we really should distinguish between severe, non-severe and + // ### application specific errors + + return 0; +} + + +static int qt_xio_errhandler( Display * ) +{ + qWarning( "%s: Fatal IO error: client killed", appName ); + qApp = 0; + exit( 1 ); + //### give the application a chance for a proper shutdown instead, + //### exit(1) doesn't help. + return 0; +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +// Memory leak: if the app exits before qt_init_internal(), this dict +// isn't released correctly. +static TQAsciiDict *atoms_to_be_created = 0; +static bool create_atoms_now = 0; + +/***************************************************************************** + qt_x11_intern_atom() - efficiently interns an atom, now or later. + + If the application is being initialized, this function stores the + adddress of the atom and qt_init_internal will do the actual work + tquickly. If the application is running, the atom is created here. + + Neither argument may point to temporary variables. + *****************************************************************************/ + +void qt_x11_intern_atom( const char *name, Atom *result) +{ + if ( !name || !result || *result ) + return; + + if ( create_atoms_now ) { + *result = XInternAtom( appDpy, name, False ); + } else { + if ( !atoms_to_be_created ) { + atoms_to_be_created = new TQAsciiDict; + atoms_to_be_created->setAutoDelete( FALSE ); + } + atoms_to_be_created->insert( name, result ); + *result = 0; + } +} + + +static void qt_x11_process_intern_atoms() +{ + if ( atoms_to_be_created ) { +#if defined(XlibSpecificationRelease) && (XlibSpecificationRelease >= 6) + int i = atoms_to_be_created->count(); + Atom * res = (Atom *)malloc( i * sizeof( Atom ) ); + Atom ** resp = (Atom **)malloc( i * sizeof( Atom* ) ); + char ** names = (char **)malloc( i * sizeof(const char*)); + + i = 0; + TQAsciiDictIterator it( *atoms_to_be_created ); + while( it.current() ) { + res[i] = 0; + resp[i] = it.current(); + names[i] = qstrdup(it.currentKey()); + i++; + ++it; + } + XInternAtoms( appDpy, names, i, False, res ); + while( i ) { + i--; + delete [] names[i]; + if ( res[i] && resp[i] ) + *(resp[i]) = res[i]; + } + free( res ); + free( resp ); + free( names ); +#else + TQAsciiDictIterator it( *atoms_to_be_created ); + Atom * result; + const char * name; + while( (result = it.current()) != 0 ) { + name = it.currentKey(); + ++it; + *result = XInternAtom( appDpy, name, False ); + } +#endif + delete atoms_to_be_created; + atoms_to_be_created = 0; + create_atoms_now = TRUE; + } +} + + +/*! \internal + apply the settings to the application +*/ +bool TQApplication::x11_apply_settings() +{ + if (! qt_std_pal) + qt_create_std_palette(); + + Atom type; + int format; + long offset = 0; + unsigned long nitems, after = 1; + unsigned char *data = 0; + TQDateTime timestamp, settingsstamp; + bool update_timestamp = FALSE; + + if (XGetWindowProperty(appDpy, TQPaintDevice::x11AppRootWindow( 0 ), + qt_settings_timestamp, 0, 0, + False, AnyPropertyType, &type, &format, &nitems, + &after, &data) == Success && format == 8) { + if (data) + XFree(data); + + TQBuffer ts; + ts.open(IO_WriteOnly); + + while (after > 0) { + XGetWindowProperty(appDpy, TQPaintDevice::x11AppRootWindow( 0 ), + qt_settings_timestamp, + offset, 1024, False, AnyPropertyType, + &type, &format, &nitems, &after, &data); + if (format == 8) { + ts.writeBlock((const char *) data, nitems); + offset += nitems / 4; + } + + XFree(data); + } + + TQDataStream d(ts.buffer(), IO_ReadOnly); + d >> timestamp; + } + + TQSettings settings; + settingsstamp = settings.lastModificationTime( "/qt/font" ); + if (! settingsstamp.isValid()) + return FALSE; + + if ( appliedstamp && appliedstamp == settingsstamp.toTime_t() ) + return TRUE; + appliedstamp = settingsstamp.toTime_t(); + + if (! timestamp.isValid() || settingsstamp > timestamp) + update_timestamp = TRUE; + + /* + TQt settings. This is now they are written into the datastream. + + /qt/Palette/ * - TQPalette + /qt/font - TQFont + /qt/libraryPath - TQStringList + /qt/style - TQString + /qt/doubleClickInterval - int + /qt/cursorFlashTime - int + /qt/wheelScrollLines - int + /qt/colorSpec - TQString + /qt/defaultCodec - TQString + /qt/globalStrut - TQSize + /qt/GUIEffects - TQStringList + /qt/Font Substitutions/ * - TQStringList + /qt/Font Substitutions/... - TQStringList + */ + + TQString str; + TQStringList strlist; + int i, num; + TQPalette pal(TQApplication::palette()); + strlist = settings.readListEntry("/qt/Palette/active"); + if (strlist.count() == TQColorGroup::NColorRoles) { + for (i = 0; i < TQColorGroup::NColorRoles; i++) + pal.setColor(TQPalette::Active, (TQColorGroup::ColorRole) i, + TQColor(strlist[i])); + } + strlist = settings.readListEntry("/qt/Palette/inactive"); + if (strlist.count() == TQColorGroup::NColorRoles) { + for (i = 0; i < TQColorGroup::NColorRoles; i++) + pal.setColor(TQPalette::Inactive, (TQColorGroup::ColorRole) i, + TQColor(strlist[i])); + } + strlist = settings.readListEntry("/qt/Palette/disabled"); + if (strlist.count() == TQColorGroup::NColorRoles) { + for (i = 0; i < TQColorGroup::NColorRoles; i++) + pal.setColor(TQPalette::Disabled, (TQColorGroup::ColorRole) i, + TQColor(strlist[i])); + } + + // workaround for KDE 3.0, which messes up the buttonText value of + // the disabled palette in TQSettings + if ( pal.disabled().buttonText() == pal.active().buttonText() ) { + pal.setColor( TQPalette::Disabled, TQColorGroup::ButtonText, + pal.disabled().foreground() ); + } + + if (pal != *qt_std_pal && pal != TQApplication::palette()) { + TQApplication::setPalette(pal, TRUE); + *qt_std_pal = pal; + } + + TQFont font(TQApplication::font()); + if ( !qt_app_has_font && !qt_x11_cmdline_font ) { + // read new font + str = settings.readEntry("/qt/font"); + if (! str.isNull() && ! str.isEmpty()) { + font.fromString(str); + + if (font != TQApplication::font()) + TQApplication::setFont(font, TRUE); + } + } + + // read library (ie. plugin) path list + TQString libpathkey = + TQString("/qt/%1.%2/libraryPath").arg( QT_VERSION >> 16 ).arg( (QT_VERSION & 0xff00 ) >> 8 ); + TQStringList pathlist = settings.readListEntry(libpathkey, ':'); + if (! pathlist.isEmpty()) { + TQStringList::ConstIterator it = pathlist.begin(); + while (it != pathlist.end()) + TQApplication::addLibraryPath(*it++); + } + + // read new TQStyle + extern bool qt_explicit_app_style; // defined in qapplication.cpp + TQString stylename = settings.readEntry( "/qt/style" ); + if ( !stylename.isEmpty() && !qt_explicit_app_style ) { + TQApplication::setStyle( stylename ); + // took the style from the user settings, so mark the explicit flag FALSE + qt_explicit_app_style = FALSE; + } + + num = + settings.readNumEntry("/qt/doubleClickInterval", + TQApplication::doubleClickInterval()); + TQApplication::setDoubleClickInterval(num); + + num = + settings.readNumEntry("/qt/cursorFlashTime", + TQApplication::cursorFlashTime()); + TQApplication::setCursorFlashTime(num); + + num = + settings.readNumEntry("/qt/wheelScrollLines", + TQApplication::wheelScrollLines()); + TQApplication::setWheelScrollLines(num); + + TQString colorspec = settings.readEntry("/qt/colorSpec", "default"); + if (colorspec == "normal") + TQApplication::setColorSpec(TQApplication::NormalColor); + else if (colorspec == "custom") + TQApplication::setColorSpec(TQApplication::CustomColor); + else if (colorspec == "many") + TQApplication::setColorSpec(TQApplication::ManyColor); + else if (colorspec != "default") + colorspec = "default"; + + TQString defaultcodec = settings.readEntry("/qt/defaultCodec", "none"); + if (defaultcodec != "none") { + TQTextCodec *codec = TQTextCodec::codecForName(defaultcodec); + if (codec) + qApp->setDefaultCodec(codec); + } + + TQStringList strut = settings.readListEntry("/qt/globalStrut"); + if (! strut.isEmpty()) { + if (strut.count() == 2) { + TQSize sz(strut[0].toUInt(), strut[1].toUInt()); + + if (sz.isValid()) + TQApplication::setGlobalStrut(sz); + } + } + + TQStringList effects = settings.readListEntry("/qt/GUIEffects"); + + TQApplication::setEffectEnabled( TQt::UI_General, effects.contains("general") ); + TQApplication::setEffectEnabled( TQt::UI_AnimateMenu, effects.contains("animatemenu") ); + TQApplication::setEffectEnabled( TQt::UI_FadeMenu, effects.contains("fademenu") ); + TQApplication::setEffectEnabled( TQt::UI_AnimateCombo, effects.contains("animatecombo") ); + TQApplication::setEffectEnabled( TQt::UI_AnimateTooltip, effects.contains("animatetooltip") ); + TQApplication::setEffectEnabled( TQt::UI_FadeTooltip, effects.contains("fadetooltip") ); + TQApplication::setEffectEnabled( TQt::UI_AnimateToolBox, effects.contains("animatetoolbox") ); + + TQStringList fontsubs = + settings.entryList("/qt/Font Substitutions"); + if (!fontsubs.isEmpty()) { + TQStringList subs; + TQString fam, skey; + TQStringList::Iterator it = fontsubs.begin(); + while (it != fontsubs.end()) { + fam = (*it++); + skey = "/qt/Font Substitutions/" + fam; + subs = settings.readListEntry(skey); + TQFont::insertSubstitutions(fam, subs); + } + } + + qt_broken_wm = + settings.readBoolEntry("/qt/brokenWindowManager", qt_broken_wm); + + qt_resolve_symlinks = + settings.readBoolEntry("/qt/resolveSymlinks", TRUE); + + qt_use_rtl_extensions = + settings.readBoolEntry("/qt/useRtlExtensions", FALSE); + +#ifndef QT_NO_XIM + if (qt_xim_preferred_style == 0) { + TQString ximInputStyle = + settings.readEntry( "/qt/XIMInputStyle", + TQObject::trUtf8( "On The Spot" ) ).lower(); + if ( ximInputStyle == "on the spot" ) + qt_xim_preferred_style = XIMPreeditCallbacks | XIMStatusNothing; + else if ( ximInputStyle == "over the spot" ) + qt_xim_preferred_style = XIMPreeditPosition | XIMStatusNothing; + else if ( ximInputStyle == "off the spot" ) + qt_xim_preferred_style = XIMPreeditArea | XIMStatusArea; + else if ( ximInputStyle == "root" ) + qt_xim_preferred_style = XIMPreeditNothing | XIMStatusNothing; + } +#endif + +#ifndef QT_NO_IM + /* + The identifier name of an input method is actquired from the + configuration file as a default. If a environment variable + "QT_IM_SWITCHER" is not empty it will overwrite the + configuration file. The "imsw-multi" becomes the default if the entry + is not configured. + */ + if ( getenv( "QT_IM_SWITCHER" ) ) + defaultIM = getenv( "QT_IM_SWITCHER" ); +#ifndef QT_NO_IM_EXTENSIONS + else + defaultIM = settings.readEntry( "/qt/DefaultInputMethodSwitcher", "imsw-multi" ); +#endif + + // defaultIM is restricted to be an IM-switcher. An IM-switcher + // has a 'imsw-' prefix + if ( ! defaultIM.startsWith( "imsw-" ) ) { + defaultIM = "imsw-multi"; + } +#endif + + if (update_timestamp) { + TQBuffer stamp; + TQDataStream s(stamp.buffer(), IO_WriteOnly); + s << settingsstamp; + + XChangeProperty(appDpy, TQPaintDevice::x11AppRootWindow( 0 ), + qt_settings_timestamp, qt_settings_timestamp, 8, + PropModeReplace, (unsigned char *) stamp.buffer().data(), + stamp.buffer().size()); + } + + return TRUE; +} + + +// read the _QT_INPUT_ENCODING property and apply the settings to +// the application +static void qt_set_input_encoding() +{ + Atom type; + int format; + ulong nitems, after = 1; + const char *data; + + int e = XGetWindowProperty( appDpy, TQPaintDevice::x11AppRootWindow(), + qt_input_encoding, 0, 1024, + False, XA_STRING, &type, &format, &nitems, + &after, (unsigned char**)&data ); + if ( e != Success || !nitems || type == None ) { + // Always use the locale codec, since we have no examples of non-local + // XIMs, and since we cannot get a sensible answer about the encoding + // from the XIM. + qt_input_mapper = TQTextCodec::codecForLocale(); + + } else { + if ( !qstricmp( data, "locale" ) ) + qt_input_mapper = TQTextCodec::codecForLocale(); + else + qt_input_mapper = TQTextCodec::codecForName( data ); + // make sure we have an input codec + if( !qt_input_mapper ) + qt_input_mapper = TQTextCodec::codecForName( "ISO 8859-1" ); + } + if ( qt_input_mapper->mibEnum() == 11 ) // 8859-8 + qt_input_mapper = TQTextCodec::codecForName( "ISO 8859-8-I"); + if( data ) + XFree( (char *)data ); +} + +// set font, foreground and background from x11 resources. The +// arguments may override the resource settings. +static void qt_set_x11_resources( const char* font = 0, const char* fg = 0, + const char* bg = 0, const char* button = 0 ) +{ + if ( !qt_std_pal ) + qt_create_std_palette(); + + TQCString resFont, resFG, resBG, resEF, sysFont; + + TQApplication::setEffectEnabled( TQt::UI_General, FALSE); + TQApplication::setEffectEnabled( TQt::UI_AnimateMenu, FALSE); + TQApplication::setEffectEnabled( TQt::UI_FadeMenu, FALSE); + TQApplication::setEffectEnabled( TQt::UI_AnimateCombo, FALSE ); + TQApplication::setEffectEnabled( TQt::UI_AnimateTooltip, FALSE ); + TQApplication::setEffectEnabled( TQt::UI_FadeTooltip, FALSE ); + TQApplication::setEffectEnabled( TQt::UI_AnimateToolBox, FALSE ); + + if ( TQApplication::desktopSettingsAware() && !TQApplication::x11_apply_settings() ) { + int format; + ulong nitems, after = 1; + TQCString res; + long offset = 0; + Atom type = None; + + while (after > 0) { + uchar *data; + XGetWindowProperty( appDpy, TQPaintDevice::x11AppRootWindow( 0 ), + qt_resource_manager, + offset, 8192, False, AnyPropertyType, + &type, &format, &nitems, &after, + &data ); + res += (char*)data; + offset += 2048; // offset is in 32bit quantities... 8192/4 == 2048 + if ( data ) + XFree( (char *)data ); + } + + TQCString key, value; + int l = 0, r; + TQCString apn = appName; + TQCString apc = appClass; + int apnl = apn.length(); + int apcl = apc.length(); + int resl = res.length(); + + while (l < resl) { + r = res.find( '\n', l ); + if ( r < 0 ) + r = resl; + while ( isspace((uchar) res[l]) ) + l++; + bool mine = FALSE; + if ( res[l] == '*' && + (res[l+1] == 'f' || res[l+1] == 'b' || res[l+1] == 'g' || + res[l+1] == 'F' || res[l+1] == 'B' || res[l+1] == 'G' || + res[l+1] == 's' || res[l+1] == 'S' ) ) { + // OPTIMIZED, since we only want "*[fbgs].." + + TQCString item = res.mid( l, r - l ).simplifyWhiteSpace(); + int i = item.find( ":" ); + key = item.left( i ).stripWhiteSpace().mid(1).lower(); + value = item.right( item.length() - i - 1 ).stripWhiteSpace(); + mine = TRUE; + } else if ( res[l] == appName[0] || (appClass && res[l] == appClass[0]) ) { + if (res.mid(l,apnl) == apn && (res[l+apnl] == '.' || res[l+apnl] == '*')) { + TQCString item = res.mid( l, r - l ).simplifyWhiteSpace(); + int i = item.find( ":" ); + key = item.left( i ).stripWhiteSpace().mid(apnl+1).lower(); + value = item.right( item.length() - i - 1 ).stripWhiteSpace(); + mine = TRUE; + } else if (res.mid(l,apcl) == apc && (res[l+apcl] == '.' || res[l+apcl] == '*')) { + TQCString item = res.mid( l, r - l ).simplifyWhiteSpace(); + int i = item.find( ":" ); + key = item.left( i ).stripWhiteSpace().mid(apcl+1).lower(); + value = item.right( item.length() - i - 1 ).stripWhiteSpace(); + mine = TRUE; + } + } + + if ( mine ) { + if ( !font && key == "systemfont") + sysFont = value.left( value.findRev(':') ).copy(); + if ( !font && key == "font") + resFont = value.copy(); + else if ( !fg && key == "foreground" ) + resFG = value.copy(); + else if ( !bg && key == "background") + resBG = value.copy(); + else if ( key == "guieffects") + resEF = value.copy(); + // NOTE: if you add more, change the [fbg] stuff above + } + + l = r + 1; + } + } + if ( !sysFont.isEmpty() ) + resFont = sysFont; + if ( resFont.isEmpty() ) + resFont = font; + if ( resFG.isEmpty() ) + resFG = fg; + if ( resBG.isEmpty() ) + resBG = bg; + if ( (!qt_app_has_font || qt_x11_cmdline_font) && !resFont.isEmpty() ) { // set application font + TQFont fnt; + fnt.setRawName( resFont ); + + // the font we get may actually be an alias for another font, + // so we reset the application font to the real font info. + if ( ! fnt.exactMatch() ) { + TQFontInfo fontinfo( fnt ); + fnt.setFamily( fontinfo.family() ); + fnt.setRawMode( fontinfo.rawMode() ); + + if ( ! fnt.rawMode() ) { + fnt.setItalic( fontinfo.italic() ); + fnt.setWeight( fontinfo.weight() ); + fnt.setUnderline( fontinfo.underline() ); + fnt.setStrikeOut( fontinfo.strikeOut() ); + fnt.setStyleHint( fontinfo.styleHint() ); + + if ( fnt.pointSize() <= 0 && fnt.pixelSize() <= 0 ) + // size is all wrong... fix it + fnt.setPointSize( (int) ( ( fontinfo.pixelSize() * 72. / + (float) TQPaintDevice::x11AppDpiY() ) + + 0.5 ) ); + } + } + + if ( fnt != TQApplication::font() ) { + TQApplication::setFont( fnt, TRUE ); + } + } + + if ( button || !resBG.isEmpty() || !resFG.isEmpty() ) {// set app colors + TQColor btn; + TQColor bg; + TQColor fg; + if ( !resBG.isEmpty() ) + bg = TQColor(TQString(resBG)); + else + bg = qt_std_pal->active().background(); + if ( !resFG.isEmpty() ) + fg = TQColor(TQString(resFG)); + else + fg = qt_std_pal->active().foreground(); + if ( button ) + btn = TQColor( button ); + else if ( !resBG.isEmpty() ) + btn = bg; + else + btn = qt_std_pal->active().button(); + + int h,s,v; + fg.hsv(&h,&s,&v); + TQColor base = TQt::white; + bool bright_mode = FALSE; + if (v >= 255-50) { + base = btn.dark(150); + bright_mode = TRUE; + } + + TQColorGroup cg( fg, btn, btn.light(), + btn.dark(), btn.dark(150), fg, TQt::white, base, bg ); + if (bright_mode) { + cg.setColor( TQColorGroup::HighlightedText, base ); + cg.setColor( TQColorGroup::Highlight, TQt::white ); + } else { + cg.setColor( TQColorGroup::HighlightedText, TQt::white ); + cg.setColor( TQColorGroup::Highlight, TQt::darkBlue ); + } + TQColor disabled( (fg.red()+btn.red())/2, + (fg.green()+btn.green())/2, + (fg.blue()+btn.blue())/2); + TQColorGroup dcg( disabled, btn, btn.light( 125 ), btn.dark(), btn.dark(150), + disabled, TQt::white, TQt::white, bg ); + if (bright_mode) { + dcg.setColor( TQColorGroup::HighlightedText, base ); + dcg.setColor( TQColorGroup::Highlight, TQt::white ); + } else { + dcg.setColor( TQColorGroup::HighlightedText, TQt::white ); + dcg.setColor( TQColorGroup::Highlight, TQt::darkBlue ); + } + TQPalette pal( cg, dcg, cg ); + if ( pal != *qt_std_pal && pal != TQApplication::palette() ) + TQApplication::setPalette( pal, TRUE ); + *qt_std_pal = pal; + } + + if ( !resEF.isEmpty() ) { + TQStringList effects = TQStringList::split(" ",resEF); + TQApplication::setEffectEnabled( TQt::UI_General, effects.contains("general") ); + TQApplication::setEffectEnabled( TQt::UI_AnimateMenu, effects.contains("animatemenu") ); + TQApplication::setEffectEnabled( TQt::UI_FadeMenu, effects.contains("fademenu") ); + TQApplication::setEffectEnabled( TQt::UI_AnimateCombo, effects.contains("animatecombo") ); + TQApplication::setEffectEnabled( TQt::UI_AnimateTooltip, effects.contains("animatetooltip") ); + TQApplication::setEffectEnabled( TQt::UI_FadeTooltip, effects.contains("fadetooltip") ); + TQApplication::setEffectEnabled( TQt::UI_AnimateToolBox, effects.contains("animatetoolbox") ); + } +} + + +static void qt_detect_broken_window_manager() +{ + Atom type; + int format; + ulong nitems, after; + uchar *data = 0; + + // look for SGI's 4Dwm + int e = XGetWindowProperty(appDpy, TQPaintDevice::x11AppRootWindow(), + qt_sgi_desks_manager, 0, 1, False, XA_WINDOW, + &type, &format, &nitems, &after, &data); + if (data) + XFree(data); + + if (e == Success && type == XA_WINDOW && format == 32 && nitems == 1 && after == 0) { + // detected SGI 4Dwm + qt_broken_wm = TRUE; + } +} + + +// update the supported array +void qt_get_net_supported() +{ + Atom type; + int format; + long offset = 0; + unsigned long nitems, after; + unsigned char *data = 0; + + int e = XGetWindowProperty(appDpy, TQPaintDevice::x11AppRootWindow(), + qt_net_supported, 0, 0, + False, XA_ATOM, &type, &format, &nitems, &after, &data); + if (data) + XFree(data); + + if (qt_net_supported_list) + delete [] qt_net_supported_list; + qt_net_supported_list = 0; + + if (e == Success && type == XA_ATOM && format == 32) { + TQBuffer ts; + ts.open(IO_WriteOnly); + + while (after > 0) { + XGetWindowProperty(appDpy, TQPaintDevice::x11AppRootWindow(), + qt_net_supported, offset, 1024, + False, XA_ATOM, &type, &format, &nitems, &after, &data); + + if (type == XA_ATOM && format == 32) { + ts.writeBlock((const char *) data, nitems * sizeof(long)); + offset += nitems; + } else + after = 0; + if (data) + XFree(data); + } + + // compute nitems + TQByteArray buffer(ts.buffer()); + nitems = buffer.size() / sizeof(Atom); + qt_net_supported_list = new Atom[nitems + 1]; + Atom *a = (Atom *) buffer.data(); + uint i; + for (i = 0; i < nitems; i++) + qt_net_supported_list[i] = a[i]; + qt_net_supported_list[nitems] = 0; + } +} + + +bool qt_net_supports(Atom atom) +{ + if (! qt_net_supported_list) + return FALSE; + + bool supported = FALSE; + int i = 0; + while (qt_net_supported_list[i] != 0) { + if (qt_net_supported_list[i++] == atom) { + supported = TRUE; + break; + } + } + + return supported; +} + + +// update the virtual roots array +void qt_get_net_virtual_roots() +{ + if (qt_net_virtual_root_list) + delete [] qt_net_virtual_root_list; + qt_net_virtual_root_list = 0; + + if (! qt_net_supports(qt_net_virtual_roots)) + return; + + Atom type; + int format; + long offset = 0; + unsigned long nitems, after; + unsigned char *data; + + int e = XGetWindowProperty(appDpy, TQPaintDevice::x11AppRootWindow(), + qt_net_virtual_roots, 0, 0, + False, XA_ATOM, &type, &format, &nitems, &after, &data); + if (data) + XFree(data); + + if (e == Success && type == XA_ATOM && format == 32) { + TQBuffer ts; + ts.open(IO_WriteOnly); + + while (after > 0) { + XGetWindowProperty(appDpy, TQPaintDevice::x11AppRootWindow(), + qt_net_virtual_roots, offset, 1024, + False, XA_ATOM, &type, &format, &nitems, &after, &data); + + if (type == XA_ATOM && format == 32) { + ts.writeBlock((const char *) data, nitems * 4); + offset += nitems; + } else + after = 0; + if (data) + XFree(data); + } + + // compute nitems + TQByteArray buffer(ts.buffer()); + nitems = buffer.size() / sizeof(Window); + qt_net_virtual_root_list = new Window[nitems + 1]; + Window *a = (Window *) buffer.data(); + uint i; + for (i = 0; i < nitems; i++) + qt_net_virtual_root_list[i] = a[i]; + qt_net_virtual_root_list[nitems] = 0; + } +} + +void qt_x11_create_wm_client_leader() +{ + if ( qt_x11_wm_client_leader ) return; + + qt_x11_wm_client_leader = + XCreateSimpleWindow( TQPaintDevice::x11AppDisplay(), + TQPaintDevice::x11AppRootWindow(), + 0, 0, 1, 1, 0, 0, 0 ); + + // set client leader property to itself + XChangeProperty( TQPaintDevice::x11AppDisplay(), + qt_x11_wm_client_leader, qt_wm_client_leader, + XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&qt_x11_wm_client_leader, 1 ); + + // If we are session managed, inform the window manager about it + TQCString session = qApp->sessionId().latin1(); + if ( !session.isEmpty() ) { + XChangeProperty( TQPaintDevice::x11AppDisplay(), + qt_x11_wm_client_leader, qt_sm_client_id, + XA_STRING, 8, PropModeReplace, + (unsigned char *)session.data(), session.length() ); + } +} + +static void qt_net_update_user_time(TQWidget *tlw) +{ + XChangeProperty(TQPaintDevice::x11AppDisplay(), tlw->winId(), qt_net_wm_user_time, XA_CARDINAL, + 32, PropModeReplace, (unsigned char *) &qt_x_user_time, 1); +} + +static void qt_check_focus_model() +{ + Window fw = None; + int unused; + XGetInputFocus( appDpy, &fw, &unused ); + if ( fw == PointerRoot ) + qt_focus_model = FocusModel_PointerRoot; + else + qt_focus_model = FocusModel_Other; +} + + +/* + Returns a truecolor visual (if there is one). 8-bit TrueColor visuals + are ignored, unless the user has explicitly requested -visual TrueColor. + The SGI X server usually has an 8 bit default visual, but the application + can also ask for a truecolor visual. This is what we do if + TQApplication::colorSpec() is TQApplication::ManyColor. +*/ + +static Visual *find_truecolor_visual( Display *dpy, int scr, int *depth, int *ncols ) +{ + XVisualInfo *vi, rvi; + int best=0, n, i; + rvi.c_class = TrueColor; + rvi.screen = scr; + vi = XGetVisualInfo( dpy, VisualClassMask | VisualScreenMask, + &rvi, &n ); + if ( vi ) { + for ( i=0; i vi[best].depth ) + best = i; + } + } + Visual *v = DefaultVisual(dpy,scr); + if ( !vi || (vi[best].visualid == XVisualIDFromVisual(v)) || + (vi[best].depth <= 8 && qt_visual_option != TrueColor) ) + { + *depth = DefaultDepth(dpy,scr); + *ncols = DisplayCells(dpy,scr); + } else { + v = vi[best].visual; + *depth = vi[best].depth; + *ncols = vi[best].colormap_size; + } + if ( vi ) + XFree( (char *)vi ); + return v; +} + + +/***************************************************************************** + qt_init() - initializes TQt for X11 + *****************************************************************************/ + +#define XK_MISCELLANY +#define XK_LATIN1 +#define XK_KOREAN +#define XK_XKB_KEYS +#include + +// ### This should be static but it isn't because of the friend declaration +// ### in qpaintdevice.h which then should have a static too but can't have +// ### it because "storage class specifiers invalid in friend function +// ### declarations" :-) Ideas anyone? +void qt_init_internal( int *argcptr, char **argv, + Display *display, TQt::HANDLE visual, TQt::HANDLE colormap ) +{ + setlocale( LC_ALL, "" ); // use correct char set mapping + setlocale( LC_NUMERIC, "C" ); // make sprintf()/scanf() work + + if ( display ) { + // TQt part of other application + + appForeignDpy = TRUE; + appDpy = display; + + // Set application name and class + appName = qstrdup( "TQt-subapplication" ); + char *app_class = 0; + if (argv) { + const char* p = strrchr( argv[0], '/' ); + app_class = qstrdup(p ? p + 1 : argv[0]); + if (app_class[0]) + app_class[0] = toupper(app_class[0]); + } + appClass = app_class; + + // Install default error handlers + original_x_errhandler = XSetErrorHandler( qt_x_errhandler ); + original_xio_errhandler = XSetIOErrorHandler( qt_xio_errhandler ); + } else { + // TQt controls everything (default) + + int argc = *argcptr; + int j; + + // Install default error handlers + original_x_errhandler = XSetErrorHandler( qt_x_errhandler ); + original_xio_errhandler = XSetIOErrorHandler( qt_xio_errhandler ); + + // Set application name and class + char *app_class = 0; + if (argv) { + const char *p = strrchr( argv[0], '/' ); + appName = p ? p + 1 : argv[0]; + app_class = qstrdup(appName); + if (app_class[0]) + app_class[0] = toupper(app_class[0]); + } + appClass = app_class; + + // Get command line params + j = argc ? 1 : 0; + for ( int i=1; i 0 ) { + if ( c == '/' ) + s.truncate( 0 ); + else + s += (char)c; + } + if ( s == "gdb" ) { + appNoGrab = TRUE; + qDebug( "TQt: gdb: -nograb added to command-line options.\n" + "\t Use the -dograb option to enforce grabbing." ); + } + f.close(); + } + } +#endif + // Connect to X server + + if( qt_is_gui_used ) { + if ( ( appDpy = XOpenDisplay(appDpyName) ) == 0 ) { + qWarning( "%s: cannot connect to X server %s", appName, + XDisplayName(appDpyName) ); + qApp = 0; + exit( 1 ); + } + + if ( appSync ) // if "-sync" argument + XSynchronize( appDpy, TRUE ); + } + } + // Common code, regardless of whether display is foreign. + + // Get X parameters + + if( qt_is_gui_used ) { + appScreen = DefaultScreen(appDpy); + appScreenCount = ScreenCount(appDpy); + + TQPaintDevice::x_appdisplay = appDpy; + TQPaintDevice::x_appscreen = appScreen; + + // allocate the arrays for the TQPaintDevice data + TQPaintDevice::x_appdepth_arr = new int[ appScreenCount ]; + TQPaintDevice::x_appcells_arr = new int[ appScreenCount ]; + TQPaintDevice::x_approotwindow_arr = new TQt::HANDLE[ appScreenCount ]; + TQPaintDevice::x_appcolormap_arr = new TQt::HANDLE[ appScreenCount ]; + TQPaintDevice::x_appdefcolormap_arr = new bool[ appScreenCount ]; + TQPaintDevice::x_appvisual_arr = new void*[ appScreenCount ]; + TQPaintDevice::x_appdefvisual_arr = new bool[ appScreenCount ]; + Q_CHECK_PTR( TQPaintDevice::x_appdepth_arr ); + Q_CHECK_PTR( TQPaintDevice::x_appcells_arr ); + Q_CHECK_PTR( TQPaintDevice::x_approotwindow_arr ); + Q_CHECK_PTR( TQPaintDevice::x_appcolormap_arr ); + Q_CHECK_PTR( TQPaintDevice::x_appdefcolormap_arr ); + Q_CHECK_PTR( TQPaintDevice::x_appvisual_arr ); + Q_CHECK_PTR( TQPaintDevice::x_appdefvisual_arr ); + + int screen; + TQString serverVendor( ServerVendor( appDpy) ); + if (serverVendor.contains("XFree86") && VendorRelease(appDpy) < 40300000) + qt_hebrew_keyboard_hack = TRUE; + + for ( screen = 0; screen < appScreenCount; ++screen ) { + TQPaintDevice::x_appdepth_arr[ screen ] = DefaultDepth(appDpy, screen); + TQPaintDevice::x_appcells_arr[ screen ] = DisplayCells(appDpy, screen); + TQPaintDevice::x_approotwindow_arr[ screen ] = RootWindow(appDpy, screen); + + // setup the visual and colormap for each screen + Visual *vis = 0; + if ( visual && screen == appScreen ) { + // use the provided visual on the default screen only + vis = (Visual *) visual; + + // figure out the depth of the visual we are using + XVisualInfo *vi, rvi; + int n; + rvi.visualid = XVisualIDFromVisual(vis); + rvi.screen = screen; + vi = XGetVisualInfo( appDpy, VisualIDMask | VisualScreenMask, &rvi, &n ); + if (vi) { + TQPaintDevice::x_appdepth_arr[ screen ] = vi->depth; + TQPaintDevice::x_appcells_arr[ screen ] = vi->visual->map_entries; + TQPaintDevice::x_appvisual_arr[ screen ] = vi->visual; + TQPaintDevice::x_appdefvisual_arr[ screen ] = FALSE; + XFree(vi); + } else { + // couldn't get info about the visual, use the default instead + vis = 0; + } + } + + if (!vis) { + // use the default visual + vis = DefaultVisual(appDpy, screen); + TQPaintDevice::x_appdefvisual_arr[ screen ] = TRUE; + + if ( qt_visual_option == TrueColor || + TQApplication::colorSpec() == TQApplication::ManyColor ) { + // find custom visual + + int d, c; + vis = find_truecolor_visual( appDpy, screen, &d, &c ); + TQPaintDevice::x_appdepth_arr[ screen ] = d; + TQPaintDevice::x_appcells_arr[ screen ] = c; + + TQPaintDevice::x_appvisual_arr[ screen ] = vis; + TQPaintDevice::x_appdefvisual_arr[ screen ] = + (XVisualIDFromVisual(vis) == + XVisualIDFromVisual(DefaultVisual(appDpy, screen))); + } + + TQPaintDevice::x_appvisual_arr[ screen ] = vis; + } + + // we assume that 8bpp == pseudocolor, but this is not + // always the case (according to the X server), so we need + // to make sure that our internal data is setup in a way + // that is compatible with our assumptions + if ( vis->c_class == TrueColor && + TQPaintDevice::x_appdepth_arr[ screen ] == 8 && + TQPaintDevice::x_appcells_arr[ screen ] == 8 ) + TQPaintDevice::x_appcells_arr[ screen ] = 256; + + if ( colormap && screen == appScreen ) { + // use the provided colormap for the default screen only + TQPaintDevice::x_appcolormap_arr[ screen ] = colormap; + TQPaintDevice::x_appdefcolormap_arr[ screen ] = FALSE; + } else { + if ( vis->c_class == TrueColor ) { + TQPaintDevice::x_appdefcolormap_arr[ screen ] = + TQPaintDevice::x_appdefvisual_arr[ screen ]; + } else { + TQPaintDevice::x_appdefcolormap_arr[ screen ] = + !qt_cmap_option && TQPaintDevice::x_appdefvisual_arr[ screen ]; + } + + if ( TQPaintDevice::x_appdefcolormap_arr[ screen ] ) { + // use default colormap + XStandardColormap *stdcmap; + VisualID vid = + XVisualIDFromVisual((Visual *) + TQPaintDevice::x_appvisual_arr[ screen ]); + int i, count; + + TQPaintDevice::x_appcolormap_arr[ screen ] = 0; + + if ( ! serverVendor.contains( "Hewlett-Packard" ) ) { + // on HPUX 10.20 local displays, the RGB_DEFAULT_MAP colormap + // doesn't give us correct colors. Why this happens, I have + // no clue, so we disable this for HPUX + if (XGetRGBColormaps(appDpy, + TQPaintDevice::x11AppRootWindow( screen ), + &stdcmap, &count, XA_RGB_DEFAULT_MAP)) { + i = 0; + while (i < count && + TQPaintDevice::x_appcolormap_arr[ screen ] == 0) { + if (stdcmap[i].visualid == vid) { + TQPaintDevice::x_appcolormap_arr[ screen ] = + stdcmap[i].colormap; + } + i++; + } + + XFree( (char *)stdcmap ); + } + } + + if (TQPaintDevice::x_appcolormap_arr[ screen ] == 0) { + TQPaintDevice::x_appcolormap_arr[ screen ] = + DefaultColormap(appDpy, screen); + } + } else { + // create a custom colormap + TQPaintDevice::x_appcolormap_arr[ screen ] = + XCreateColormap(appDpy, TQPaintDevice::x11AppRootWindow( screen ), + vis, AllocNone); + } + } + } + + // Set X paintdevice parameters for the default screen + TQPaintDevice::x_appdepth = TQPaintDevice::x_appdepth_arr[ appScreen ]; + TQPaintDevice::x_appcells = TQPaintDevice::x_appcells_arr[ appScreen ]; + TQPaintDevice::x_approotwindow = TQPaintDevice::x_approotwindow_arr[ appScreen ]; + TQPaintDevice::x_appcolormap = TQPaintDevice::x_appcolormap_arr[ appScreen ]; + TQPaintDevice::x_appdefcolormap = TQPaintDevice::x_appdefcolormap_arr[ appScreen ]; + TQPaintDevice::x_appvisual = TQPaintDevice::x_appvisual_arr[ appScreen ]; + TQPaintDevice::x_appdefvisual = TQPaintDevice::x_appdefvisual_arr[ appScreen ]; + + // Support protocols + + qt_x11_intern_atom( "WM_PROTOCOLS", &qt_wm_protocols ); + qt_x11_intern_atom( "WM_DELETE_WINDOW", &qt_wm_delete_window ); + qt_x11_intern_atom( "WM_STATE", &qt_wm_state ); + qt_x11_intern_atom( "WM_CHANGE_STATE", &qt_wm_change_state ); + qt_x11_intern_atom( "WM_TAKE_FOCUS", &qt_wm_take_focus ); + qt_x11_intern_atom( "WM_CLIENT_LEADER", &qt_wm_client_leader); + qt_x11_intern_atom( "WM_WINDOW_ROLE", &qt_window_role); + qt_x11_intern_atom( "SM_CLIENT_ID", &qt_sm_client_id); + qt_x11_intern_atom( "CLIPBOARD", &qt_xa_clipboard ); + qt_x11_intern_atom( "RESOURCE_MANAGER", &qt_resource_manager ); + qt_x11_intern_atom( "INCR", &qt_x_incr ); + qt_x11_intern_atom( "_XSETROOT_ID", &qt_xsetroot_id ); + qt_x11_intern_atom( "_QT_SELECTION", &qt_selection_property ); + qt_x11_intern_atom( "_QT_CLIPBOARD_SENTINEL", &qt_clipboard_sentinel ); + qt_x11_intern_atom( "_QT_SELECTION_SENTINEL", &qt_selection_sentinel ); + qt_x11_intern_atom( "_QT_SCROLL_DONE", &qt_qt_scrolldone ); + qt_x11_intern_atom( "_QT_INPUT_ENCODING", &qt_input_encoding ); + qt_x11_intern_atom( "_QT_SIZEGRIP", &qt_sizegrip ); + qt_x11_intern_atom( "_NET_WM_CONTEXT_HELP", &qt_net_wm_context_help ); + qt_x11_intern_atom( "_NET_WM_PING", &qt_net_wm_ping ); + qt_x11_intern_atom( "_MOTIF_WM_HINTS", &qt_xa_motif_wm_hints ); + qt_x11_intern_atom( "DTWM_IS_RUNNING", &qt_cde_running ); + qt_x11_intern_atom( "KWIN_RUNNING", &qt_twin_running ); + qt_x11_intern_atom( "KWM_RUNNING", &qt_kwm_running ); + qt_x11_intern_atom( "GNOME_BACKGROUND_PROPERTIES", &qt_gbackground_properties ); + + TQString atomname("_QT_SETTINGS_TIMESTAMP_"); + atomname += XDisplayName(appDpyName); + qt_x11_intern_atom( atomname.latin1(), &qt_settings_timestamp ); + + qt_x11_intern_atom( "_NET_SUPPORTED", &qt_net_supported ); + qt_x11_intern_atom( "_NET_VIRTUAL_ROOTS", &qt_net_virtual_roots ); + qt_x11_intern_atom( "_NET_WORKAREA", &qt_net_workarea ); + qt_x11_intern_atom( "_NET_WM_STATE", &qt_net_wm_state ); + qt_x11_intern_atom( "_NET_WM_STATE_MODAL", &qt_net_wm_state_modal ); + qt_x11_intern_atom( "_NET_WM_STATE_MAXIMIZED_VERT", &qt_net_wm_state_max_v ); + qt_x11_intern_atom( "_NET_WM_STATE_MAXIMIZED_HORZ", &qt_net_wm_state_max_h ); + qt_x11_intern_atom( "_NET_WM_STATE_FULLSCREEN", &qt_net_wm_state_fullscreen ); + qt_x11_intern_atom( "_NET_WM_STATE_ABOVE", &qt_net_wm_state_above ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE", &qt_net_wm_window_type ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_NORMAL", &qt_net_wm_window_type_normal ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_DIALOG", &qt_net_wm_window_type_dialog ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_TOOLBAR", &qt_net_wm_window_type_toolbar ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_MENU", &qt_net_wm_window_type_menu ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_UTILITY", &qt_net_wm_window_type_utility ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_SPLASH", &qt_net_wm_window_type_splash ); + qt_x11_intern_atom( "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE", &qt_net_wm_window_type_override ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", &qt_net_wm_window_type_dropdown_menu ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_POPUP_MENU", &qt_net_wm_window_type_popup_menu ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_TOOLTIP", &qt_net_wm_window_type_tooltip ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_COMBO", &qt_net_wm_window_type_combo ); + qt_x11_intern_atom( "_NET_WM_WINDOW_TYPE_DND", &qt_net_wm_window_type_dnd ); + qt_x11_intern_atom( "_KDE_NET_WM_FRAME_STRUT", &qt_net_wm_frame_strut ); + qt_x11_intern_atom( "_NET_WM_STATE_STAYS_ON_TOP", + &qt_net_wm_state_stays_on_top ); + qt_x11_intern_atom( "_NET_WM_PID", &qt_net_wm_pid ); + qt_x11_intern_atom( "_NET_WM_USER_TIME", &qt_net_wm_user_time ); + qt_x11_intern_atom( "_NET_WM_FULL_PLACEMENT", &qt_net_wm_full_placement ); + qt_x11_intern_atom( "ENLIGHTENMENT_DESKTOP", &qt_enlightenment_desktop ); + qt_x11_intern_atom( "_NET_WM_NAME", &qt_net_wm_name ); + qt_x11_intern_atom( "_NET_WM_ICON_NAME", &qt_net_wm_icon_name ); + qt_x11_intern_atom( "UTF8_STRING", &qt_utf8_string ); + qt_x11_intern_atom( "_SGI_DESKS_MANAGER", &qt_sgi_desks_manager ); + +#ifndef QT_NO_XSYNC + qt_x11_intern_atom( "_NET_WM_SYNC_RETQUEST_COUNTER", &qt_net_wm_sync_request_counter ); + qt_x11_intern_atom( "_NET_WM_SYNC_RETQUEST", &qt_net_wm_sync_request ); +#endif + + qt_xdnd_setup(); + qt_x11_motifdnd_init(); + + // Finally create all atoms + qt_x11_process_intern_atoms(); + + // look for broken window managers + qt_detect_broken_window_manager(); + + // initialize NET lists + qt_get_net_supported(); + qt_get_net_virtual_roots(); + +#ifndef QT_NO_XRANDR + // See if XRandR is supported on the connected display + int xrandr_errorbase; + Q_UNUSED( xrandr_eventbase ); + if ( XRRQueryExtension( appDpy, &xrandr_eventbase, &xrandr_errorbase ) ) { + // XRandR is supported + qt_use_xrandr = TRUE; + } +#endif // QT_NO_XRANDR + +#ifndef QT_NO_XRENDER + // See if XRender is supported on the connected display + int xrender_eventbase, xrender_errorbase; + if (XRenderQueryExtension(appDpy, &xrender_eventbase, &xrender_errorbase)) { + // XRender is supported, let's see if we have a PictFormat for the + // default visual + XRenderPictFormat *format = + XRenderFindVisualFormat(appDpy, + (Visual *) TQPaintDevice::x_appvisual); + qt_use_xrender = (format != 0) && (TQPaintDevice::x_appdepth != 8); + } +#endif // QT_NO_XRENDER + +#ifndef QT_NO_XSYNC + // Try to initialize SYNC extension on the connected display + int xsync_major, xsync_minor; + if ( XSyncQueryExtension( appDpy, &xsync_eventbase, &xsync_errorbase ) && + XSyncInitialize( appDpy, &xsync_major, &xsync_minor ) ) { + qt_use_xsync = TRUE; + } +#endif + +#ifndef QT_NO_XKB + // If XKB is detected, set the GrabsUseXKBState option so input method + // compositions continue to work (ie. deadkeys) + unsigned int state = XkbPCF_GrabsUseXKBStateMask; + (void) XkbSetPerClientControls(appDpy, state, &state); +#endif + +#if !defined(QT_NO_XFTFREETYPE) + // defined in qfont_x11.cpp + extern bool qt_has_xft; +#ifndef QT_XFT2 + if (!qt_use_xrender) + qt_has_xft = FALSE; + else +#endif + qt_has_xft = XftInit(0) && XftInitFtLibrary(); + + if (qt_has_xft) { + char *dpi_str = XGetDefault(appDpy, "Xft", "dpi"); + if (dpi_str) { + // use a custom DPI + char *end = 0; + int dpi = strtol(dpi_str, &end, 0); + if (dpi_str != end) { + for (int s = 0; s < ScreenCount(appDpy); ++s) { + TQPaintDevice::x11SetAppDpiX(dpi, s); + TQPaintDevice::x11SetAppDpiY(dpi, s); + } + } + } + } +#endif // QT_NO_XFTFREETYPE + + // look at the modifier mapping, and get the correct masks for alt/meta + // find the alt/meta masks + XModifierKeymap *map = XGetModifierMapping(appDpy); + if (map) { + int i, maskIndex = 0, mapIndex = 0; + for (maskIndex = 0; maskIndex < 8; maskIndex++) { + for (i = 0; i < map->max_keypermod; i++) { + if (map->modifiermap[mapIndex]) { + KeySym sym = + XKeycodeToKeysym(appDpy, map->modifiermap[mapIndex], 0); + if ( qt_alt_mask == 0 && + ( sym == XK_Alt_L || sym == XK_Alt_R ) ) { + qt_alt_mask = 1 << maskIndex; + } + if ( qt_meta_mask == 0 && + (sym == XK_Meta_L || sym == XK_Meta_R ) ) { + qt_meta_mask = 1 << maskIndex; + } + } + mapIndex++; + } + } + + // not look for mode_switch in qt_alt_mask and qt_meta_mask - if it is + // present in one or both, then we set qt_mode_switch_remove_mask. + // see TQETWidget::translateKeyEventInternal for an explanation + // of why this is needed + mapIndex = 0; + for ( maskIndex = 0; maskIndex < 8; maskIndex++ ) { + if ( qt_alt_mask != ( 1 << maskIndex ) && + qt_meta_mask != ( 1 << maskIndex ) ) { + for ( i = 0; i < map->max_keypermod; i++ ) + mapIndex++; + continue; + } + + for ( i = 0; i < map->max_keypermod; i++ ) { + if ( map->modifiermap[ mapIndex ] ) { + KeySym sym = + XKeycodeToKeysym( appDpy, map->modifiermap[ mapIndex ], 0 ); + if ( sym == XK_Mode_switch ) { + qt_mode_switch_remove_mask |= 1 << maskIndex; + } + } + mapIndex++; + } + } + + XFreeModifiermap(map); + } else { + // assume defaults + qt_alt_mask = Mod1Mask; + qt_meta_mask = Mod4Mask; + qt_mode_switch_remove_mask = 0; + } + + // Misc. initialization + + TQColor::initialize(); + TQFont::initialize(); + TQCursor::initialize(); + TQPainter::initialize(); + } + +#if defined(QT_THREAD_SUPPORT) + TQThread::initialize(); +#endif + + if( qt_is_gui_used ) { + qApp->setName( appName ); + + int screen; + for ( screen = 0; screen < appScreenCount; ++screen ) { + XSelectInput( appDpy, TQPaintDevice::x11AppRootWindow( screen ), + KeymapStateMask | EnterWindowMask | LeaveWindowMask | + PropertyChangeMask ); + +#ifndef QT_NO_XRANDR + if (qt_use_xrandr) + XRRSelectInput( appDpy, TQPaintDevice::x11AppRootWindow( screen ), True ); +#endif // QT_NO_XRANDR + } + } + + if ( qt_is_gui_used ) { + qt_set_input_encoding(); + + qt_set_x11_resources( appFont, appFGCol, appBGCol, appBTNCol); + + // be smart about the size of the default font. most X servers have helvetica + // 12 point available at 2 resolutions: + // 75dpi (12 pixels) and 100dpi (17 pixels). + // At 95 DPI, a 12 point font should be 16 pixels tall - in which case a 17 + // pixel font is a closer match than a 12 pixel font + int ptsz = + (int) ( ( ( TQPaintDevice::x11AppDpiY() >= 95 ? 17. : 12. ) * + 72. / (float) TQPaintDevice::x11AppDpiY() ) + 0.5 ); + + if ( !qt_app_has_font && !qt_x11_cmdline_font ) { + TQFont f( "Helvetica", ptsz ); + TQApplication::setFont( f ); + } + +#if !defined(QT_NO_IM) +#if !defined(QT_NO_IM_EXTENSIONS) + TQApplication::create_im(); +#else + TQApplication::create_xim(); +#endif +#endif + +#if defined (QT_TABLET_SUPPORT) + int ndev, + i, + j; + bool gotStylus, + gotEraser; + XDeviceInfo *devices, *devs; + XInputClassInfo *ip; + XAnyClassPtr any; + XValuatorInfoPtr v; + XAxisInfoPtr a; + XDevice *dev; + XEventClass *ev_class; + int curr_event_count; + +#if !defined(Q_OS_IRIX) + // XFree86 divides a stylus and eraser into 2 devices, so we must do for both... + const TQString XFREENAMESTYLUS = "stylus"; + const TQString XFREENAMEPEN = "pen"; + const TQString XFREENAMEERASER = "eraser"; +#endif + + devices = XListInputDevices( appDpy, &ndev); + if ( devices == NULL ) { + qWarning( "Failed to get list of devices" ); + ndev = -1; + } + dev = NULL; + for ( devs = devices, i = 0; i < ndev; i++, devs++ ) { + gotEraser = FALSE; +#if defined(Q_OS_IRIX) + + gotStylus = ( !strncmp(devs->name, + WACOM_NAME, sizeof(WACOM_NAME) - 1) ); +#else + TQString devName = devs->name; + devName = devName.lower(); + gotStylus = ( devName.startsWith(XFREENAMEPEN) + || devName.startsWith(XFREENAMESTYLUS) ); + if ( !gotStylus ) + gotEraser = devName.startsWith( XFREENAMEERASER ); + +#endif + if ( gotStylus || gotEraser ) { + // I only wanted to do this once, so wrap pointers around these + curr_event_count = 0; + + if ( gotStylus ) { + devStylus = XOpenDevice( appDpy, devs->id ); + dev = devStylus; + ev_class = event_list_stylus; + } else if ( gotEraser ) { + devEraser = XOpenDevice( appDpy, devs->id ); + dev = devEraser; + ev_class = event_list_eraser; + } + if ( dev == NULL ) { + qWarning( "Failed to open device" ); + } else { + if ( dev->num_classes > 0 ) { + for ( ip = dev->classes, j = 0; j < devs->num_classes; + ip++, j++ ) { + switch ( ip->input_class ) { + case KeyClass: + DeviceKeyPress( dev, xinput_key_press, + ev_class[curr_event_count] ); + curr_event_count++; + DeviceKeyRelease( dev, xinput_key_release, + ev_class[curr_event_count] ); + curr_event_count++; + break; + case ButtonClass: + DeviceButtonPress( dev, xinput_button_press, + ev_class[curr_event_count] ); + curr_event_count++; + DeviceButtonRelease( dev, xinput_button_release, + ev_class[curr_event_count] ); + curr_event_count++; + break; + case ValuatorClass: + // I'm only going to be interested in motion when the + // stylus is already down anyway! + DeviceMotionNotify( dev, xinput_motion, + ev_class[curr_event_count] ); + curr_event_count++; + break; + default: + break; + } + } + } + } + // get the min/max value for pressure! + any = (XAnyClassPtr) ( devs->inputclassinfo ); + if ( dev == devStylus ) { + qt_curr_events_stylus = curr_event_count; + for (j = 0; j < devs->num_classes; j++) { + if ( any->c_class == ValuatorClass ) { + v = (XValuatorInfoPtr) any; + a = (XAxisInfoPtr) ((char *) v + + sizeof (XValuatorInfo)); +#if defined (Q_OS_IRIX) + max_pressure = a[WAC_PRESSURE_I].max_value; +#else + max_pressure = a[2].max_value; +#endif + // got the max pressure no need to go further... + break; + } + any = (XAnyClassPtr) ((char *) any + any->length); + } + } else { + qt_curr_events_eraser = curr_event_count; + } + // at this point we are assuming there is only one + // wacom device... +#if defined (Q_OS_IRIX) + if ( devStylus != NULL ) { +#else + if ( devStylus != NULL && devEraser != NULL ) { +#endif + break; + } + } + } // end for loop + XFreeDeviceList( devices ); +#endif // QT_TABLET_SUPPORT + + } else { + // read some non-GUI settings when not using the X server... + + if ( TQApplication::desktopSettingsAware() ) { + TQSettings settings; + + // read library (ie. plugin) path list + TQString libpathkey = TQString("/qt/%1.%2/libraryPath") + .arg( QT_VERSION >> 16 ) + .arg( (QT_VERSION & 0xff00 ) >> 8 ); + TQStringList pathlist = + settings.readListEntry(libpathkey, ':'); + if (! pathlist.isEmpty()) { + TQStringList::ConstIterator it = pathlist.begin(); + while (it != pathlist.end()) + TQApplication::addLibraryPath(*it++); + } + + TQString defaultcodec = settings.readEntry("/qt/defaultCodec", "none"); + if (defaultcodec != "none") { + TQTextCodec *codec = TQTextCodec::codecForName(defaultcodec); + if (codec) + qApp->setDefaultCodec(codec); + } + + qt_resolve_symlinks = + settings.readBoolEntry("/qt/resolveSymlinks", TRUE); + } + } + } + + +#ifndef QT_NO_STYLE + // run-time search for default style +void TQApplication::x11_initialize_style() +{ + Atom type; + int format; + unsigned long length, after; + uchar *data; + if ( !app_style && + XGetWindowProperty( appDpy, TQPaintDevice::x11AppRootWindow(), qt_twin_running, + 0, 1, False, AnyPropertyType, &type, &format, &length, + &after, &data ) == Success && length ) { + if ( data ) XFree( (char *)data ); + // twin is there. check if KDE's styles are available, + // otherwise use windows style + if ( (app_style = TQStyleFactory::create("highcolor") ) == 0 ) + app_style = TQStyleFactory::create("windows"); + } + if ( !app_style && + XGetWindowProperty( appDpy, TQPaintDevice::x11AppRootWindow(), qt_kwm_running, + 0, 1, False, AnyPropertyType, &type, &format, &length, + &after, &data ) == Success && length ) { + if ( data ) XFree( (char *)data ); + app_style = TQStyleFactory::create("windows"); + } + if ( !app_style && + XGetWindowProperty( appDpy, TQPaintDevice::x11AppRootWindow(), qt_cde_running, + 0, 1, False, AnyPropertyType, &type, &format, &length, + &after, &data ) == Success && length ) { + // DTWM is running, meaning most likely CDE is running... + if ( data ) XFree( (char *) data ); + app_style = TQStyleFactory::create( "cde" ); + } + // maybe another desktop? + if ( !app_style && + XGetWindowProperty( appDpy, TQPaintDevice::x11AppRootWindow(), + qt_gbackground_properties, 0, 1, False, AnyPropertyType, + &type, &format, &length, &after, &data ) == Success && + length ) { + if ( data ) XFree( (char *)data ); + // default to MotifPlus with hovering + app_style = TQStyleFactory::create("motifplus" ); + } +} +#endif + +void qt_init( int *argcptr, char **argv, TQApplication::Type ) +{ + qt_init_internal( argcptr, argv, 0, 0, 0 ); +} + +void qt_init( Display *display, TQt::HANDLE visual, TQt::HANDLE colormap ) +{ + qt_init_internal( 0, 0, display, visual, colormap ); +} + + +/***************************************************************************** + qt_cleanup() - cleans up when the application is finished + *****************************************************************************/ + +void qt_cleanup() +{ + appliedstamp = 0; + + if ( app_save_rootinfo ) // root window must keep state + qt_save_rootinfo(); + + if ( qt_is_gui_used ) { + TQPixmapCache::clear(); + TQPainter::cleanup(); + TQCursor::cleanup(); + TQFont::cleanup(); + TQColor::cleanup(); + TQSharedDoubleBuffer::cleanup(); + } +#if defined(QT_THREAD_SUPPORT) + TQThread::cleanup(); +#endif + +#if defined (QT_TABLET_SUPPORT) + if ( devStylus != NULL ) + XCloseDevice( appDpy, devStylus ); + if ( devEraser != NULL ) + XCloseDevice( appDpy, devEraser ); +#endif + +#if !defined(QT_NO_IM) +#if !defined(QT_NO_IM_EXTENSIONS) + TQApplication::close_im(); +#else + TQApplication::close_xim(); +#endif +#endif + + if ( qt_is_gui_used ) { + int screen; + for ( screen = 0; screen < appScreenCount; screen++ ) { + if ( ! TQPaintDevice::x11AppDefaultColormap( screen ) ) + XFreeColormap( TQPaintDevice::x11AppDisplay(), + TQPaintDevice::x11AppColormap( screen ) ); + } + } + +#define QT_CLEANUP_GC(g) if (g) { for (int i=0;i= appScreenCount ) { + qDebug("invalid screen %d %d", scrn, appScreenCount ); + TQWidget* bla = 0; + bla->setName("hello"); + } + GC gc; + if ( monochrome ) { + if ( !app_gc_ro_m ) // create GC for bitmap + memset( (app_gc_ro_m = new GC[appScreenCount]), 0, appScreenCount * sizeof( GC ) ); + if ( !app_gc_ro_m[scrn] ) + app_gc_ro_m[scrn] = create_gc( scrn, TRUE ); + gc = app_gc_ro_m[scrn]; + } else { // create standard GC + if ( !app_gc_ro ) + memset( (app_gc_ro = new GC[appScreenCount]), 0, appScreenCount * sizeof( GC ) ); + if ( !app_gc_ro[scrn] ) + app_gc_ro[scrn] = create_gc( scrn, FALSE ); + gc = app_gc_ro[scrn]; + } + return gc; +} + +GC qt_xget_temp_gc( int scrn, bool monochrome ) // get temporary GC +{ + if ( scrn < 0 || scrn >= appScreenCount ) { + qDebug("invalid screen (tmp) %d %d", scrn, appScreenCount ); + TQWidget* bla = 0; + bla->setName("hello"); + } + GC gc; + if ( monochrome ) { + if ( !app_gc_tmp_m ) // create GC for bitmap + memset( (app_gc_tmp_m = new GC[appScreenCount]), 0, appScreenCount * sizeof( GC ) ); + if ( !app_gc_tmp_m[scrn] ) + app_gc_tmp_m[scrn] = create_gc( scrn, TRUE ); + gc = app_gc_tmp_m[scrn]; + } else { // create standard GC + if ( !app_gc_tmp ) + memset( (app_gc_tmp = new GC[appScreenCount]), 0, appScreenCount * sizeof( GC ) ); + if ( !app_gc_tmp[scrn] ) + app_gc_tmp[scrn] = create_gc( scrn, FALSE ); + gc = app_gc_tmp[scrn]; + } + return gc; +} + + +/***************************************************************************** + Platform specific TQApplication members + *****************************************************************************/ + +/*! + \fn TQWidget *TQApplication::mainWidget() const + + Returns the main application widget, or 0 if there is no main + widget. + + \sa setMainWidget() +*/ + +/*! + Sets the application's main widget to \a mainWidget. + + In most respects the main widget is like any other widget, except + that if it is closed, the application exits. Note that + TQApplication does \e not take ownership of the \a mainWidget, so + if you create your main widget on the heap you must delete it + yourself. + + You need not have a main widget; connecting lastWindowClosed() to + tquit() is an alternative. + + For X11, this function also resizes and moves the main widget + according to the \e -geometry command-line option, so you should + set the default geometry (using \l TQWidget::setGeometry()) before + calling setMainWidget(). + + \sa mainWidget(), exec(), tquit() +*/ + +void TQApplication::setMainWidget( TQWidget *mainWidget ) +{ +#if defined(QT_CHECK_STATE) + if ( mainWidget && mainWidget->parentWidget() && + ! mainWidget->parentWidget()->isDesktop() ) + qWarning( "TQApplication::setMainWidget(): New main widget (%s/%s) " + "has a parent!", + mainWidget->className(), mainWidget->name() ); +#endif + main_widget = mainWidget; + if ( main_widget ) { // give WM command line + XSetWMProperties( main_widget->x11Display(), main_widget->winId(), + 0, 0, app_argv, app_argc, 0, 0, 0 ); + if ( mwTitle ) + XStoreName( main_widget->x11Display(), main_widget->winId(), (char*)mwTitle ); + if ( mwGeometry ) { // parse geometry + int x, y; + int w, h; + int m = XParseGeometry( (char*)mwGeometry, &x, &y, (uint*)&w, (uint*)&h ); + TQSize minSize = main_widget->minimumSize(); + TQSize maxSize = main_widget->maximumSize(); + if ( (m & XValue) == 0 ) + x = main_widget->geometry().x(); + if ( (m & YValue) == 0 ) + y = main_widget->geometry().y(); + if ( (m & WidthValue) == 0 ) + w = main_widget->width(); + if ( (m & HeightValue) == 0 ) + h = main_widget->height(); + w = TQMIN(w,maxSize.width()); + h = TQMIN(h,maxSize.height()); + w = TQMAX(w,minSize.width()); + h = TQMAX(h,minSize.height()); + if ( (m & XNegative) ) { + x = desktop()->width() + x - w; + qt_widget_tlw_gravity = NorthEastGravity; + } + if ( (m & YNegative) ) { + y = desktop()->height() + y - h; + qt_widget_tlw_gravity = (m & XNegative) ? SouthEastGravity : SouthWestGravity; + } + main_widget->setGeometry( x, y, w, h ); + } + } +} + +#ifndef QT_NO_CURSOR + +/***************************************************************************** + TQApplication cursor stack + *****************************************************************************/ + +extern void qt_x11_enforce_cursor( TQWidget * w ); + +typedef TQPtrList TQCursorList; + +static TQCursorList *cursorStack = 0; + +/*! + \fn TQCursor *TQApplication::overrideCursor() + + Returns the active application override cursor. + + This function returns 0 if no application cursor has been defined + (i.e. the internal cursor stack is empty). + + \sa setOverrideCursor(), restoreOverrideCursor() +*/ + +/*! + Sets the application override cursor to \a cursor. + + Application override cursors are intended for showing the user + that the application is in a special state, for example during an + operation that might take some time. + + This cursor will be displayed in all the application's widgets + until restoreOverrideCursor() or another setOverrideCursor() is + called. + + Application cursors are stored on an internal stack. + setOverrideCursor() pushes the cursor onto the stack, and + restoreOverrideCursor() pops the active cursor off the stack. + Every setOverrideCursor() must eventually be followed by a + corresponding restoreOverrideCursor(), otherwise the stack will + never be emptied. + + If \a replace is TRUE, the new cursor will replace the last + override cursor (the stack keeps its depth). If \a replace is + FALSE, the new stack is pushed onto the top of the stack. + + Example: + \code + TQApplication::setOverrideCursor( TQCursor(TQt::WaitCursor) ); + calculateHugeMandelbrot(); // lunch time... + TQApplication::restoreOverrideCursor(); + \endcode + + \sa overrideCursor(), restoreOverrideCursor(), TQWidget::setCursor() +*/ + +void TQApplication::setOverrideCursor( const TQCursor &cursor, bool replace ) +{ + if ( !cursorStack ) { + cursorStack = new TQCursorList; + Q_CHECK_PTR( cursorStack ); + cursorStack->setAutoDelete( TRUE ); + } + app_cursor = new TQCursor( cursor ); + Q_CHECK_PTR( app_cursor ); + if ( replace ) + cursorStack->removeLast(); + cursorStack->append( app_cursor ); + + TQWidgetIntDictIt it( *((TQWidgetIntDict*)TQWidget::mapper) ); + register TQWidget *w; + while ( (w=it.current()) ) { // for all widgets that have + if ( w->testWState( WState_OwnCursor ) ) + qt_x11_enforce_cursor( w ); + ++it; + } + XFlush( appDpy ); // make X execute it NOW +} + +/*! + Undoes the last setOverrideCursor(). + + If setOverrideCursor() has been called twice, calling + restoreOverrideCursor() will activate the first cursor set. + Calling this function a second time restores the original widgets' + cursors. + + \sa setOverrideCursor(), overrideCursor(). +*/ + +void TQApplication::restoreOverrideCursor() +{ + if ( !cursorStack ) // no cursor stack + return; + cursorStack->removeLast(); + app_cursor = cursorStack->last(); + if ( TQWidget::mapper != 0 && !closingDown() ) { + TQWidgetIntDictIt it( *((TQWidgetIntDict*)TQWidget::mapper) ); + register TQWidget *w; + while ( (w=it.current()) ) { // set back to original cursors + if ( w->testWState( WState_OwnCursor ) ) + qt_x11_enforce_cursor( w ); + ++it; + } + XFlush( appDpy ); + } + if ( !app_cursor ) { + delete cursorStack; + cursorStack = 0; + } +} + +#endif + +/*! + \fn bool TQApplication::hasGlobalMouseTracking() + + Returns TRUE if global mouse tracking is enabled; otherwise + returns FALSE. + + \sa setGlobalMouseTracking() +*/ + +/*! + Enables global mouse tracking if \a enable is TRUE, or disables it + if \a enable is FALSE. + + Enabling global mouse tracking makes it possible for widget event + filters or application event filters to get all mouse move events, + even when no button is depressed. This is useful for special GUI + elements, e.g. tooltips. + + Global mouse tracking does not affect widgets and their + mouseMoveEvent(). For a widget to get mouse move events when no + button is depressed, it must do TQWidget::setMouseTracking(TRUE). + + This function uses an internal counter. Each + setGlobalMouseTracking(TRUE) must have a corresponding + setGlobalMouseTracking(FALSE): + \code + // at this point global mouse tracking is off + TQApplication::setGlobalMouseTracking( TRUE ); + TQApplication::setGlobalMouseTracking( TRUE ); + TQApplication::setGlobalMouseTracking( FALSE ); + // at this point it's still on + TQApplication::setGlobalMouseTracking( FALSE ); + // but now it's off + \endcode + + \sa hasGlobalMouseTracking(), TQWidget::hasMouseTracking() +*/ + +void TQApplication::setGlobalMouseTracking( bool enable ) +{ + bool tellAllWidgets; + if ( enable ) { + tellAllWidgets = (++app_tracking == 1); + } else { + tellAllWidgets = (--app_tracking == 0); + } + if ( tellAllWidgets ) { + TQWidgetIntDictIt it( *((TQWidgetIntDict*)TQWidget::mapper) ); + register TQWidget *w; + while ( (w=it.current()) ) { + if ( app_tracking > 0 ) { // switch on + if ( !w->testWState(WState_MouseTracking) ) { + w->setMouseTracking( TRUE ); + w->clearWState( WState_MouseTracking ); + } + } else { // switch off + if ( !w->testWState(WState_MouseTracking) ) { + w->setWState( WState_MouseTracking ); + w->setMouseTracking( FALSE ); + } + } + ++it; + } + } +} + + +/***************************************************************************** + Routines to find a TQt widget from a screen position + *****************************************************************************/ + +Window qt_x11_findClientWindow( Window win, Atom property, bool leaf ) +{ + Atom type = None; + int format, i; + ulong nitems, after; + uchar *data; + Window root, parent, target=0, *children=0; + uint nchildren; + if ( XGetWindowProperty( appDpy, win, property, 0, 0, FALSE, AnyPropertyType, + &type, &format, &nitems, &after, &data ) == Success ) { + if ( data ) + XFree( (char *)data ); + if ( type ) + return win; + } + if ( !XQueryTree(appDpy,win,&root,&parent,&children,&nchildren) ) { + if ( children ) + XFree( (char *)children ); + return 0; + } + for ( i=nchildren-1; !target && i >= 0; i-- ) + target = qt_x11_findClientWindow( children[i], property, leaf ); + if ( children ) + XFree( (char *)children ); + return target; +} + + +/*! + Returns a pointer to the widget at global screen position \a + (x, y), or 0 if there is no TQt widget there. + + If \a child is FALSE and there is a child widget at position \a + (x, y), the top-level widget containing it is returned. If \a child + is TRUE the child widget at position \a (x, y) is returned. + + This function is normally rather slow. + + \sa TQCursor::pos(), TQWidget::grabMouse(), TQWidget::grabKeyboard() +*/ + +TQWidget *TQApplication::widgetAt( int x, int y, bool child ) +{ + int screen = TQCursor::x11Screen(); + int lx, ly; + + Window target; + if ( !XTranslateCoordinates(appDpy, + TQPaintDevice::x11AppRootWindow(screen), + TQPaintDevice::x11AppRootWindow(screen), + x, y, &lx, &ly, &target) ) { + return 0; + } + if ( !target || target == TQPaintDevice::x11AppRootWindow(screen) ) + return 0; + TQWidget *w, *c; + w = TQWidget::find( (WId)target ); + + if ( !w ) { + qt_ignore_badwindow(); + target = qt_x11_findClientWindow( target, qt_wm_state, TRUE ); + if (qt_badwindow() ) + return 0; + w = TQWidget::find( (WId)target ); +#if 0 + if ( !w ) { + // Perhaps the widgets at (x,y) is inside a foreign application? + // Search all toplevel widgets to see if one is within target + TQWidgetList *list = topLevelWidgets(); + TQWidget *widget = list->first(); + while ( widget && !w ) { + Window ctarget = target; + if ( widget->isVisible() && !widget->isDesktop() ) { + Window wid = widget->winId(); + while ( ctarget && !w ) { + XTranslateCoordinates(appDpy, + TQPaintDevice::x11AppRootWindow(screen), + ctarget, x, y, &lx, &ly, &ctarget); + if ( ctarget == wid ) { + // Found + w = widget; + XTranslateCoordinates(appDpy, + TQPaintDevice::x11AppRootWindow(screen), + ctarget, x, y, &lx, &ly, &ctarget); + } + } + } + widget = list->next(); + } + delete list; + } +#endif + } + if ( child && w ) { + if ( (c = w->childAt( w->mapFromGlobal(TQPoint(x, y ) ) ) ) ) + return c; + } + return w; +} + +/*! + \overload TQWidget *TQApplication::widgetAt( const TQPoint &pos, bool child ) + + Returns a pointer to the widget at global screen position \a pos, + or 0 if there is no TQt widget there. + + If \a child is FALSE and there is a child widget at position \a + pos, the top-level widget containing it is returned. If \a child + is TRUE the child widget at position \a pos is returned. +*/ + + +/*! + Flushes the X event queue in the X11 implementation. This normally + returns almost immediately. Does nothing on other platforms. + + \sa syncX() +*/ + +void TQApplication::flushX() +{ + if ( appDpy ) + XFlush( appDpy ); +} + +/*! + Flushes the window system specific event queues. + + If you are doing graphical changes inside a loop that does not + return to the event loop on asynchronous window systems like X11 + or double buffered window systems like MacOS X, and you want to + visualize these changes immediately (e.g. Splash Screens), call + this function. + + \sa flushX() sendPostedEvents() TQPainter::flush() +*/ + +void TQApplication::flush() +{ + flushX(); +} + +/*! + Synchronizes with the X server in the X11 implementation. This + normally takes some time. Does nothing on other platforms. + + \sa flushX() +*/ + +void TQApplication::syncX() +{ + if ( appDpy ) + XSync( appDpy, False ); // don't discard events +} + + +/*! + Sounds the bell, using the default volume and sound. +*/ + +void TQApplication::beep() +{ + if ( appDpy ) + XBell( appDpy, 0 ); +} + + + +/***************************************************************************** + Special lookup functions for windows that have been reparented recently + *****************************************************************************/ + +static TQWidgetIntDict *wPRmapper = 0; // alternative widget mapper + +void qPRCreate( const TQWidget *widget, Window oldwin ) +{ // TQWidget::reparent mechanism + if ( !wPRmapper ) { + wPRmapper = new TQWidgetIntDict; + Q_CHECK_PTR( wPRmapper ); + } + wPRmapper->insert( (long)oldwin, widget ); // add old window to mapper + TQETWidget *w = (TQETWidget *)widget; + w->setWState( TQt::WState_Reparented ); // set reparented flag +} + +void qPRCleanup( TQWidget *widget ) +{ + TQETWidget *etw = (TQETWidget *)widget; + if ( !(wPRmapper && etw->testWState(TQt::WState_Reparented)) ) + return; // not a reparented widget + TQWidgetIntDictIt it(*wPRmapper); + TQWidget *w; + while ( (w=it.current()) ) { + int key = it.currentKey(); + ++it; + if ( w == etw ) { // found widget + etw->clearWState( TQt::WState_Reparented ); // clear flag + wPRmapper->remove( key );// old window no longer needed + if ( wPRmapper->count() == 0 ) { // became empty + delete wPRmapper; // then reset alt mapper + wPRmapper = 0; + return; + } + } + } +} + +static TQETWidget *qPRFindWidget( Window oldwin ) +{ + return wPRmapper ? (TQETWidget*)wPRmapper->find((long)oldwin) : 0; +} + +/*! + \internal +*/ +int TQApplication::x11ClientMessage(TQWidget* w, XEvent* event, bool passive_only) +{ + TQETWidget *widget = (TQETWidget*)w; + if ( event->xclient.format == 32 && event->xclient.message_type ) { + if ( event->xclient.message_type == qt_wm_protocols ) { + Atom a = event->xclient.data.l[0]; + if ( a == qt_wm_delete_window ) { + if ( passive_only ) return 0; + widget->translateCloseEvent(event); + } + else if ( a == qt_wm_take_focus ) { + TQWidget * amw = activeModalWidget(); + if ( (ulong) event->xclient.data.l[1] > qt_x_time ) + qt_x_time = event->xclient.data.l[1]; + if ( amw && amw != widget ) { + TQWidget* groupLeader = widget; + while ( groupLeader && !groupLeader->testWFlags( TQt::WGroupLeader ) + && groupLeader != amw ) + groupLeader = groupLeader->parentWidget(); + if ( !groupLeader ) { + TQWidget *p = amw->parentWidget(); + while (p && p != widget) + p = p->parentWidget(); + if (!p || !qt_net_supported_list) + amw->raise(); // help broken window managers + amw->setActiveWindow(); + } + } +#ifndef QT_NO_WHATSTHIS + } else if ( a == qt_net_wm_context_help ) { + TQWhatsThis::enterWhatsThisMode(); +#endif // QT_NO_WHATSTHIS + } else if ( a == qt_net_wm_ping ) { + // avoid send/reply loops + Window root = TQPaintDevice::x11AppRootWindow( w->x11Screen() ); + if (event->xclient.window != root) { + event->xclient.window = root; + XSendEvent( event->xclient.display, event->xclient.window, + False, SubstructureNotifyMask|SubstructureRedirectMask, event ); + } +#ifndef QT_NO_XSYNC + } else if (a == qt_net_wm_sync_request ) { + widget->handleSyncRequest( event ); +#endif + } + } else if ( event->xclient.message_type == qt_qt_scrolldone ) { + widget->translateScrollDoneEvent(event); + } else if ( event->xclient.message_type == qt_xdnd_position ) { + qt_handle_xdnd_position( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_enter ) { + qt_handle_xdnd_enter( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_status ) { + qt_handle_xdnd_status( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_leave ) { + qt_handle_xdnd_leave( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_drop ) { + qt_handle_xdnd_drop( widget, event, passive_only ); + } else if ( event->xclient.message_type == qt_xdnd_finished ) { + qt_handle_xdnd_finished( widget, event, passive_only ); + } else { + if ( passive_only ) return 0; + // All other are interactions + } + } else { + qt_motifdnd_handle_msg( widget, event, passive_only ); + } + + return 0; +} + +/*! + This function does the core processing of individual X + \a{event}s, normally by dispatching TQt events to the right + destination. + + It returns 1 if the event was consumed by special handling, 0 if + the \a event was consumed by normal handling, and -1 if the \a + event was for an unrecognized widget. + + \sa x11EventFilter() +*/ +int TQApplication::x11ProcessEvent( XEvent* event ) +{ + switch ( event->type ) { + case ButtonPress: + ignoreNextMouseReleaseEvent = FALSE; + qt_x_user_time = event->xbutton.time; + // fallthrough intended + case ButtonRelease: + qt_x_time = event->xbutton.time; + break; + case MotionNotify: + qt_x_time = event->xmotion.time; + break; + case XKeyPress: + qt_x_user_time = event->xkey.time; + // fallthrough intended + case XKeyRelease: + qt_x_time = event->xkey.time; + break; + case PropertyNotify: + qt_x_time = event->xproperty.time; + break; + case EnterNotify: + case LeaveNotify: + qt_x_time = event->xcrossing.time; + break; + case SelectionClear: + qt_x_time = event->xselectionclear.time; + break; + default: + break; + } + + TQETWidget *widget = (TQETWidget*)TQWidget::find( (WId)event->xany.window ); + + if ( wPRmapper ) { // just did a widget reparent? + if ( widget == 0 ) { // not in std widget mapper + switch ( event->type ) { // only for mouse/key events + case ButtonPress: + case ButtonRelease: + case MotionNotify: + case XKeyPress: + case XKeyRelease: + widget = qPRFindWidget( event->xany.window ); + break; + } + } + else if ( widget->testWState(WState_Reparented) ) + qPRCleanup( widget ); // remove from alt mapper + } + + TQETWidget *keywidget=0; + bool grabbed=FALSE; + if ( event->type==XKeyPress || event->type==XKeyRelease ) { + keywidget = (TQETWidget*)TQWidget::keyboardGrabber(); + if ( keywidget ) { + grabbed = TRUE; + } else { + if ( focus_widget ) + keywidget = (TQETWidget*)focus_widget; + if ( !keywidget ) { + if ( inPopupMode() ) // no focus widget, see if we have a popup + keywidget = (TQETWidget*) activePopupWidget(); + else if ( widget ) + keywidget = (TQETWidget*)widget->topLevelWidget(); + } + } + } + +#ifndef QT_NO_IM + // Filtering input events by the input context. It has to be taken + // place before any other key event consumers such as eventfilters + // and accelerators because some input methods retquire tquite + // various key combination and sequences. It often conflicts with + // accelerators and so on, so we must give the input context the + // filtering opportunity first to ensure all input methods work + // properly regardless of application design. + +// #ifndef QT_NO_IM_EXTENSIONS + if( keywidget && keywidget->isEnabled() && keywidget->isInputMethodEnabled() ) { +// #else +// if( keywidget && keywidget->isEnabled() ) { +// #endif + if( ( event->type==XKeyPress || event->type==XKeyRelease ) && + sm_blockUserInput ) // block user interaction during session management + return TRUE; + + // for XIM handling + TQInputContext *qic = keywidget->getInputContext(); + if( qic && qic->x11FilterEvent( keywidget, event ) ) + return TRUE; + + // filterEvent() accepts TQEvent *event rather than preexpanded key + // event attribute values. This is intended to pass other IM-related + // events in future. The IM-related events are supposed as + // TQWheelEvent, TQTabletEvent and so on. Other non IM-related events + // should not be forwarded to input contexts to prevent weird event + // handling. + if ( ( event->type == XKeyPress || event->type == XKeyRelease ) ) { + int code = -1; + int count = 0; + int state; + char ascii = 0; + TQEvent::Type type; + TQString text; + + keywidget->translateKeyEventInternal( event, count, text, + state, ascii, code, type, + FALSE, FALSE ); + + // both key press/release is retquired for some complex + // input methods. don't eliminate anything. + TQKeyEvent keyevent( type, code, ascii, state, text, FALSE, count ); + + if( qic && qic->filterEvent( &keyevent ) ) + return TRUE; + } + } else +#endif // QT_NO_IM + { + if ( XFilterEvent( event, None ) ) + return TRUE; + } + + if ( qt_x11EventFilter(event) ) // send through app filter + return 1; + + if ( event->type == MappingNotify ) { // keyboard mapping changed + XRefreshKeyboardMapping( &event->xmapping ); + return 0; + } + + if ( event->type == PropertyNotify ) { // some properties changed + if ( event->xproperty.window == TQPaintDevice::x11AppRootWindow( 0 ) ) { + // root properties for the first screen + if ( event->xproperty.atom == qt_clipboard_sentinel ) { + if (qt_check_clipboard_sentinel() ) + emit clipboard()->dataChanged(); + } else if ( event->xproperty.atom == qt_selection_sentinel ) { + if (qt_check_selection_sentinel() ) + emit clipboard()->selectionChanged(); + } else if ( obey_desktop_settings ) { + if ( event->xproperty.atom == qt_resource_manager ) + qt_set_x11_resources(); + else if ( event->xproperty.atom == qt_settings_timestamp ) + TQApplication::x11_apply_settings(); + } + } + if ( event->xproperty.window == TQPaintDevice::x11AppRootWindow() ) { + // root properties for the default screen + if ( event->xproperty.atom == qt_input_encoding ) { + qt_set_input_encoding(); + } else if ( event->xproperty.atom == qt_net_supported ) { + qt_get_net_supported(); + } else if ( event->xproperty.atom == qt_net_virtual_roots ) { + qt_get_net_virtual_roots(); + } else if ( event->xproperty.atom == qt_net_workarea ) { + qt_desktopwidget_update_workarea(); + } + } else if ( widget ) { + widget->translatePropertyEvent(event); + } else { + return -1; // don't know this window + } + return 0; + } + + if ( !widget ) { // don't know this windows + TQWidget* popup = TQApplication::activePopupWidget(); + if ( popup ) { + + /* + That is more than suboptimal. The real solution should + do some keyevent and buttonevent translation, so that + the popup still continues to work as the user expects. + Unfortunately this translation is currently only + possible with a known widget. I'll change that soon + (Matthias). + */ + + // Danger - make sure we don't lock the server + switch ( event->type ) { + case ButtonPress: + case ButtonRelease: + case XKeyPress: + case XKeyRelease: + do { + popup->close(); + } while ( (popup = qApp->activePopupWidget()) ); + return 1; + } + } + return -1; + } + + if ( event->type == XKeyPress || event->type == XKeyRelease ) + widget = keywidget; // send XKeyEvents through keywidget->x11Event() + + if ( app_do_modal ) // modal event handling + if ( !qt_try_modal(widget, event) ) { + if ( event->type == ClientMessage ) + x11ClientMessage( widget, event, TRUE ); + return 1; + } + + + if ( widget->x11Event(event) ) // send through widget filter + return 1; +#if defined (QT_TABLET_SUPPORT) + if ( event->type == xinput_motion || + event->type == xinput_button_release || + event->type == xinput_button_press ) { + widget->translateXinputEvent( event ); + return 0; + } +#endif + +#ifndef QT_NO_XRANDR + if (event->type == xrandr_eventbase + RRScreenChangeNotify + || ( event->type == ConfigureNotify && event->xconfigure.window == TQPaintDevice::x11AppRootWindow())) { + // update Xlib internals with the latest screen configuration + XRRUpdateConfiguration(event); + + // update the size for desktop widget + int scr = XRRRootToScreen( appDpy, event->xany.window ); + TQWidget *w = desktop()->screen( scr ); + TQSize oldSize( w->size() ); + w->crect.setWidth( DisplayWidth( appDpy, scr ) ); + w->crect.setHeight( DisplayHeight( appDpy, scr ) ); + if ( w->size() != oldSize ) { + TQResizeEvent e( w->size(), oldSize ); + TQApplication::sendEvent( w, &e ); + emit desktop()->resized( scr ); + } + } +#endif // QT_NO_XRANDR + + switch ( event->type ) { + + case ButtonRelease: // mouse event + if ( ignoreNextMouseReleaseEvent ) { + ignoreNextMouseReleaseEvent = FALSE; + break; + } + // fall through intended + case ButtonPress: + if (event->xbutton.root != RootWindow(widget->x11Display(), widget->x11Screen()) + && ! qt_xdnd_dragging) { + while ( activePopupWidget() ) + activePopupWidget()->close(); + return 1; + } + if (event->type == ButtonPress) + qt_net_update_user_time(widget->topLevelWidget()); + // fall through intended + case MotionNotify: +#if defined(QT_TABLET_SUPPORT) + if ( !chokeMouse ) { +#endif + widget->translateMouseEvent( event ); +#if defined(QT_TABLET_SUPPORT) + } else { + chokeMouse = FALSE; + } +#endif + break; + + case XKeyPress: // keyboard event + qt_net_update_user_time(widget->topLevelWidget()); + // fallthrough intended + case XKeyRelease: + { + if ( keywidget && keywidget->isEnabled() ) { // should always exist + // qDebug( "sending key event" ); + keywidget->translateKeyEvent( event, grabbed ); + } + break; + } + + case GraphicsExpose: + case Expose: // paint event + widget->translatePaintEvent( event ); + break; + + case ConfigureNotify: // window move/resize event + if ( event->xconfigure.event == event->xconfigure.window ) + widget->translateConfigEvent( event ); + break; + + case XFocusIn: { // got focus + if ( widget->isDesktop() ) + break; + if ( inPopupMode() ) // some delayed focus event to ignore + break; + if ( !widget->isTopLevel() ) + break; + if ( event->xfocus.detail != NotifyAncestor && + event->xfocus.detail != NotifyInferior && + event->xfocus.detail != NotifyNonlinear ) + break; + widget->createInputContext(); + setActiveWindow( widget ); + if ( qt_focus_model == FocusModel_PointerRoot ) { + // We got real input focus from somewhere, but we were in PointerRoot + // mode, so we don't trust this event. Check the focus model to make + // sure we know what focus mode we are using... + qt_check_focus_model(); + } + } + break; + + case XFocusOut: // lost focus + if ( widget->isDesktop() ) + break; + if ( !widget->isTopLevel() ) + break; + if ( event->xfocus.mode == NotifyGrab ) + qt_xfocusout_grab_counter++; + if ( event->xfocus.mode != NotifyNormal ) + break; + if ( event->xfocus.detail != NotifyAncestor && + event->xfocus.detail != NotifyNonlinearVirtual && + event->xfocus.detail != NotifyNonlinear ) + break; + if ( !inPopupMode() && widget == active_window ) + setActiveWindow( 0 ); + break; + + case EnterNotify: { // enter window + if ( TQWidget::mouseGrabber() && widget != TQWidget::mouseGrabber() ) + break; + if ( inPopupMode() && widget->topLevelWidget() != activePopupWidget() ) + break; + if ( event->xcrossing.mode != NotifyNormal || + event->xcrossing.detail == NotifyVirtual || + event->xcrossing.detail == NotifyNonlinearVirtual ) + break; + if ( event->xcrossing.focus && + !widget->isDesktop() && !widget->isActiveWindow() ) { + if ( qt_focus_model == FocusModel_Unknown ) // check focus model + qt_check_focus_model(); + if ( qt_focus_model == FocusModel_PointerRoot ) // PointerRoot mode + setActiveWindow( widget ); + } + qt_dispatchEnterLeave( widget, TQWidget::find( curWin ) ); + curWin = widget->winId(); + widget->translateMouseEvent( event ); //we don't get MotionNotify, emulate it + } + break; + + case LeaveNotify: { // leave window + if ( TQWidget::mouseGrabber() && widget != TQWidget::mouseGrabber() ) + break; + if ( curWin && widget->winId() != curWin ) + break; + if ( event->xcrossing.mode != NotifyNormal ) + break; + if ( !widget->isDesktop() ) + widget->translateMouseEvent( event ); //we don't get MotionNotify, emulate it + + TQWidget* enter = 0; + XEvent ev; + while ( XCheckMaskEvent( widget->x11Display(), EnterWindowMask | LeaveWindowMask , &ev ) + && !qt_x11EventFilter( &ev )) { + TQWidget* event_widget = TQWidget::find( ev.xcrossing.window ); + if( event_widget && event_widget->x11Event( &ev ) ) + break; + if ( ev.type == LeaveNotify && ev.xcrossing.mode == NotifyNormal ){ + enter = event_widget; + XPutBackEvent( widget->x11Display(), &ev ); + break; + } + if ( ev.xcrossing.mode != NotifyNormal || + ev.xcrossing.detail == NotifyVirtual || + ev.xcrossing.detail == NotifyNonlinearVirtual ) + continue; + enter = event_widget; + if ( ev.xcrossing.focus && + enter && !enter->isDesktop() && !enter->isActiveWindow() ) { + if ( qt_focus_model == FocusModel_Unknown ) // check focus model + qt_check_focus_model(); + if ( qt_focus_model == FocusModel_PointerRoot ) // PointerRoot mode + setActiveWindow( enter ); + } + break; + } + + if ( ( ! enter || enter->isDesktop() ) && + event->xcrossing.focus && widget == active_window && + qt_focus_model == FocusModel_PointerRoot // PointerRoot mode + ) { + setActiveWindow( 0 ); + } + + if ( !curWin ) + qt_dispatchEnterLeave( widget, 0 ); + + qt_dispatchEnterLeave( enter, widget ); + curWin = enter ? enter->winId() : 0; + } + break; + + case UnmapNotify: // window hidden + if ( widget->isTopLevel() && widget->isShown() ) { + widget->topData()->spont_unmapped = 1; + TQHideEvent e; + TQApplication::sendSpontaneousEvent( widget, &e ); + widget->hideChildren( TRUE ); + } + break; + + case MapNotify: // window shown + if ( widget->isTopLevel() && + widget->topData()->spont_unmapped ) { + widget->topData()->spont_unmapped = 0; + widget->showChildren( TRUE ); + TQShowEvent e; + TQApplication::sendSpontaneousEvent( widget, &e ); + } + break; + + case ClientMessage: // client message + return x11ClientMessage(widget,event,False); + + case ReparentNotify: // window manager reparents + while ( XCheckTypedWindowEvent( widget->x11Display(), + widget->winId(), + ReparentNotify, + event ) ) + ; // skip old reparent events + if ( event->xreparent.parent == TQPaintDevice::x11AppRootWindow() ) { + if ( widget->isTopLevel() ) { + widget->topData()->parentWinId = event->xreparent.parent; + if ( qt_deferred_map_contains( widget ) ) { + qt_deferred_map_take( widget ); + XMapWindow( appDpy, widget->winId() ); + } + } + } else + // store the parent. Useful for many things, embedding for instance. + widget->topData()->parentWinId = event->xreparent.parent; + if ( widget->isTopLevel() ) { + // the widget frame strut should also be invalidated + widget->topData()->fleft = widget->topData()->fright = + widget->topData()->ftop = widget->topData()->fbottom = 0; + + if ( qt_focus_model != FocusModel_Unknown ) { + // toplevel reparented... + TQWidget *newparent = TQWidget::find( event->xreparent.parent ); + if ( ! newparent || newparent->isDesktop() ) { + // we dont' know about the new parent (or we've been + // reparented to root), perhaps a window manager + // has been (re)started? reset the focus model to unknown + qt_focus_model = FocusModel_Unknown; + } + } + } + break; + + case SelectionRequest: { + XSelectionRequestEvent *req = &event->xselectionrequest; + if (! req) + break; + + if ( qt_xdnd_selection && req->selection == qt_xdnd_selection ) { + qt_xdnd_handle_selection_request( req ); + + } else if (qt_clipboard) { + TQCustomEvent e( TQEvent::Clipboard, event ); + TQApplication::sendSpontaneousEvent( qt_clipboard, &e ); + } + break; + } + case SelectionClear: { + XSelectionClearEvent *req = &event->xselectionclear; + // don't deliver dnd events to the clipboard, it gets confused + if (! req || qt_xdnd_selection && req->selection == qt_xdnd_selection) + break; + + if (qt_clipboard) { + TQCustomEvent e( TQEvent::Clipboard, event ); + TQApplication::sendSpontaneousEvent( qt_clipboard, &e ); + } + break; + } + + case SelectionNotify: { + XSelectionEvent *req = &event->xselection; + // don't deliver dnd events to the clipboard, it gets confused + if (! req || qt_xdnd_selection && req->selection == qt_xdnd_selection) + break; + + if (qt_clipboard) { + TQCustomEvent e( TQEvent::Clipboard, event ); + TQApplication::sendSpontaneousEvent( qt_clipboard, &e ); + } + break; + } + + default: + break; + } + + return 0; +} + +/*! + This virtual function is only implemented under X11. + + If you create an application that inherits TQApplication and + reimplement this function, you get direct access to all X events + that the are received from the X server. + + Return TRUE if you want to stop the event from being processed. + Return FALSE for normal event dispatching. + + \sa x11ProcessEvent() +*/ + +bool TQApplication::x11EventFilter( XEvent * ) +{ + return FALSE; +} + + + +/***************************************************************************** + Modal widgets; Since Xlib has little support for this we roll our own + modal widget mechanism. + A modal widget without a parent becomes application-modal. + A modal widget with a parent becomes modal to its parent and grandparents.. + + qt_enter_modal() + Enters modal state + Arguments: + TQWidget *widget A modal widget + + qt_leave_modal() + Leaves modal state for a widget + Arguments: + TQWidget *widget A modal widget + *****************************************************************************/ + +bool qt_modal_state() +{ + return app_do_modal; +} + +void qt_enter_modal( TQWidget *widget ) +{ + if ( !qt_modal_stack ) { // create modal stack + qt_modal_stack = new TQWidgetList; + Q_CHECK_PTR( qt_modal_stack ); + } + if (widget->parentWidget()) { + TQEvent e(TQEvent::WindowBlocked); + TQApplication::sendEvent(widget->parentWidget(), &e); + } + + qt_dispatchEnterLeave( 0, TQWidget::find((WId)curWin) ); + qt_modal_stack->insert( 0, widget ); + app_do_modal = TRUE; + curWin = 0; + ignoreNextMouseReleaseEvent = FALSE; +} + + +void qt_leave_modal( TQWidget *widget ) +{ + if ( qt_modal_stack && qt_modal_stack->removeRef(widget) ) { + if ( qt_modal_stack->isEmpty() ) { + delete qt_modal_stack; + qt_modal_stack = 0; + TQPoint p( TQCursor::pos() ); + TQWidget* w = TQApplication::widgetAt( p.x(), p.y(), TRUE ); + qt_dispatchEnterLeave( w, TQWidget::find( curWin ) ); // send synthetic enter event + curWin = w? w->winId() : 0; + } + } + app_do_modal = qt_modal_stack != 0; + ignoreNextMouseReleaseEvent = TRUE; + + if (widget->parentWidget()) { + TQEvent e(TQEvent::WindowUnblocked); + TQApplication::sendEvent(widget->parentWidget(), &e); + } +} + + +bool qt_try_modal( TQWidget *widget, XEvent *event ) +{ + if (qt_xdnd_dragging) { + // allow mouse events while DnD is active + switch (event->type) { + case ButtonPress: + case ButtonRelease: + case MotionNotify: + return TRUE; + default: + break; + } + } + + if ( qt_tryModalHelper( widget ) ) + return TRUE; + + bool block_event = FALSE; + switch ( event->type ) { + case ButtonPress: // disallow mouse/key events + case ButtonRelease: + case MotionNotify: + case XKeyPress: + case XKeyRelease: + case EnterNotify: + case LeaveNotify: + case ClientMessage: + block_event = TRUE; + break; + default: + break; + } + + return !block_event; +} + + +/***************************************************************************** + Popup widget mechanism + + openPopup() + Adds a widget to the list of popup widgets + Arguments: + TQWidget *widget The popup widget to be added + + closePopup() + Removes a widget from the list of popup widgets + Arguments: + TQWidget *widget The popup widget to be removed + *****************************************************************************/ + + +static int openPopupCount = 0; +void TQApplication::openPopup( TQWidget *popup ) +{ + openPopupCount++; + if ( !popupWidgets ) { // create list + popupWidgets = new TQWidgetList; + Q_CHECK_PTR( popupWidgets ); + } + popupWidgets->append( popup ); // add to end of list + + if ( popupWidgets->count() == 1 && !qt_nograb() ){ // grab mouse/keyboard + int r = XGrabKeyboard( popup->x11Display(), popup->winId(), FALSE, + GrabModeSync, GrabModeAsync, CurrentTime ); + if ( (popupGrabOk = (r == GrabSuccess)) ) { + r = XGrabPointer( popup->x11Display(), popup->winId(), TRUE, + (uint)(ButtonPressMask | ButtonReleaseMask | + ButtonMotionMask | EnterWindowMask | + LeaveWindowMask | PointerMotionMask), + GrabModeSync, GrabModeAsync, + None, None, CurrentTime ); + + if ( (popupGrabOk = (r == GrabSuccess)) ) + XAllowEvents( popup->x11Display(), SyncPointer, CurrentTime ); + else + XUngrabKeyboard( popup->x11Display(), CurrentTime ); + } + } else if ( popupGrabOk ) { + XAllowEvents( popup->x11Display(), SyncPointer, CurrentTime ); + } + + // popups are not focus-handled by the window system (the first + // popup grabbed the keyboard), so we have to do that manually: A + // new popup gets the focus + TQFocusEvent::setReason( TQFocusEvent::Popup ); + if ( popup->focusWidget()) + popup->focusWidget()->setFocus(); + else + popup->setFocus(); + TQFocusEvent::resetReason(); +} + +void TQApplication::closePopup( TQWidget *popup ) +{ + if ( !popupWidgets ) + return; + popupWidgets->removeRef( popup ); + if (popup == popupOfPopupButtonFocus) { + popupButtonFocus = 0; + popupOfPopupButtonFocus = 0; + } + if ( popupWidgets->count() == 0 ) { // this was the last popup + popupCloseDownMode = TRUE; // control mouse events + delete popupWidgets; + popupWidgets = 0; + if ( !qt_nograb() && popupGrabOk ) { // grabbing not disabled + if ( mouseButtonState != 0 + || popup->geometry(). contains(TQPoint(mouseGlobalXPos, mouseGlobalYPos) ) ) + { // mouse release event or inside + XAllowEvents( popup->x11Display(), AsyncPointer, + CurrentTime ); + } else { // mouse press event + mouseButtonPressTime -= 10000; // avoid double click + XAllowEvents( popup->x11Display(), ReplayPointer,CurrentTime ); + } + XUngrabPointer( popup->x11Display(), CurrentTime ); + XFlush( popup->x11Display() ); + } + if ( active_window ) { + TQFocusEvent::setReason( TQFocusEvent::Popup ); + if ( active_window->focusWidget() ) + active_window->focusWidget()->setFocus(); + else + active_window->setFocus(); + TQFocusEvent::resetReason(); + } + } else { + // popups are not focus-handled by the window system (the + // first popup grabbed the keyboard), so we have to do that + // manually: A popup was closed, so the previous popup gets + // the focus. + TQFocusEvent::setReason( TQFocusEvent::Popup ); + TQWidget* aw = popupWidgets->getLast(); + if (aw->focusWidget()) + aw->focusWidget()->setFocus(); + else + aw->setFocus(); + TQFocusEvent::resetReason(); + if ( popupWidgets->count() == 1 && !qt_nograb() ){ // grab mouse/keyboard + int r = XGrabKeyboard( aw->x11Display(), aw->winId(), FALSE, + GrabModeSync, GrabModeAsync, CurrentTime ); + if ( (popupGrabOk = (r == GrabSuccess)) ) { + r = XGrabPointer( aw->x11Display(), aw->winId(), TRUE, + (uint)(ButtonPressMask | ButtonReleaseMask | + ButtonMotionMask | EnterWindowMask | + LeaveWindowMask | PointerMotionMask), + GrabModeSync, GrabModeAsync, + None, None, CurrentTime ); + + if ( (popupGrabOk = (r == GrabSuccess)) ) + XAllowEvents( aw->x11Display(), SyncPointer, CurrentTime ); + } + } + } +} + +/***************************************************************************** + Event translation; translates X11 events to TQt events + *****************************************************************************/ + +// +// Mouse event translation +// +// Xlib doesn't give mouse double click events, so we generate them by +// comparing window, time and position between two mouse press events. +// + +// +// Keyboard event translation +// + +int qt_x11_translateButtonState( int s ) +{ + int bst = 0; + if ( s & Button1Mask ) + bst |= TQt::LeftButton; + if ( s & Button2Mask ) + bst |= TQt::MidButton; + if ( s & Button3Mask ) + bst |= TQt::RightButton; + if ( s & ShiftMask ) + bst |= TQt::ShiftButton; + if ( s & ControlMask ) + bst |= TQt::ControlButton; + if ( s & qt_alt_mask ) + bst |= TQt::AltButton; + if ( s & qt_meta_mask ) + bst |= TQt::MetaButton; + return bst; +} + +bool TQETWidget::translateMouseEvent( const XEvent *event ) +{ + static bool manualGrab = FALSE; + TQEvent::Type type; // event parameters + TQPoint pos; + TQPoint globalPos; + int button = 0; + int state; + XEvent nextEvent; + + if ( sm_blockUserInput ) // block user interaction during session management + return TRUE; + + static int x_root_save = -1, y_root_save = -1; + + if ( event->type == MotionNotify ) { // mouse move + if (event->xmotion.root != RootWindow(appDpy, x11Screen()) && + ! qt_xdnd_dragging ) + return FALSE; + + XMotionEvent lastMotion = event->xmotion; + while( XPending( appDpy ) ) { // compres mouse moves + XNextEvent( appDpy, &nextEvent ); + if ( nextEvent.type == ConfigureNotify + || nextEvent.type == PropertyNotify + || nextEvent.type == Expose + || nextEvent.type == NoExpose ) { + qApp->x11ProcessEvent( &nextEvent ); + continue; + } else if ( nextEvent.type != MotionNotify || + nextEvent.xmotion.window != event->xmotion.window || + nextEvent.xmotion.state != event->xmotion.state ) { + XPutBackEvent( appDpy, &nextEvent ); + break; + } + if ( !qt_x11EventFilter(&nextEvent) + && !x11Event( &nextEvent ) ) // send event through filter + lastMotion = nextEvent.xmotion; + else + break; + } + type = TQEvent::MouseMove; + pos.rx() = lastMotion.x; + pos.ry() = lastMotion.y; + globalPos.rx() = lastMotion.x_root; + globalPos.ry() = lastMotion.y_root; + state = qt_x11_translateButtonState( lastMotion.state ); + if ( qt_button_down && (state & (LeftButton | + MidButton | + RightButton ) ) == 0 ) + qt_button_down = 0; + + // throw away mouse move events that are sent multiple times to the same + // position + bool throw_away = FALSE; + if ( x_root_save == globalPos.x() && + y_root_save == globalPos.y() ) + throw_away = TRUE; + x_root_save = globalPos.x(); + y_root_save = globalPos.y(); + if ( throw_away ) + return TRUE; + } else if ( event->type == EnterNotify || event->type == LeaveNotify) { + XEvent *xevent = (XEvent *)event; + //unsigned int xstate = event->xcrossing.state; + type = TQEvent::MouseMove; + pos.rx() = xevent->xcrossing.x; + pos.ry() = xevent->xcrossing.y; + globalPos.rx() = xevent->xcrossing.x_root; + globalPos.ry() = xevent->xcrossing.y_root; + state = qt_x11_translateButtonState( xevent->xcrossing.state ); + if ( qt_button_down && (state & (LeftButton | + MidButton | + RightButton ) ) == 0 ) + qt_button_down = 0; + if ( !qt_button_down ) + state = state & ~(LeftButton | MidButton | RightButton ); + } else { // button press or release + pos.rx() = event->xbutton.x; + pos.ry() = event->xbutton.y; + globalPos.rx() = event->xbutton.x_root; + globalPos.ry() = event->xbutton.y_root; + state = qt_x11_translateButtonState( event->xbutton.state ); + switch ( event->xbutton.button ) { + case Button1: button = LeftButton; break; + case Button2: button = MidButton; break; + case Button3: button = RightButton; break; + case Button4: + case Button5: + case 6: + case 7: + // the fancy mouse wheel. + + // take care about grabbing. We do this here since it + // is clear that we return anyway + if ( qApp->inPopupMode() && popupGrabOk ) + XAllowEvents( x11Display(), SyncPointer, CurrentTime ); + + // We are only interested in ButtonPress. + if (event->type == ButtonPress ){ + + // compress wheel events (the X Server will simply + // send a button press for each single notch, + // regardless whether the application can catch up + // or not) + int delta = 1; + XEvent xevent; + while ( XCheckTypedWindowEvent(x11Display(),winId(), + ButtonPress,&xevent) ){ + if (xevent.xbutton.button != event->xbutton.button){ + XPutBackEvent(x11Display(), &xevent); + break; + } + delta++; + } + + // the delta is defined as multiples of + // WHEEL_DELTA, which is set to 120. Future wheels + // may offer a finer-resolution. A positive delta + // indicates forward rotation, a negative one + // backward rotation respectively. + int btn = event->xbutton.button; + delta *= 120 * ( (btn == Button4 || btn == 6) ? 1 : -1 ); + bool hor = ( (btn == Button4 || btn == Button5) && (state&AltButton) || + (btn == 6 || btn == 7) ); + translateWheelEvent( globalPos.x(), globalPos.y(), delta, state, (hor)?Horizontal:Vertical ); + } + return TRUE; + } + if ( event->type == ButtonPress ) { // mouse button pressed +#if defined(Q_OS_IRIX) && defined(QT_TABLET_SUPPORT) + XEvent myEv; + if ( XCheckTypedEvent( appDpy, xinput_button_press, &myEv ) ) { + if ( translateXinputEvent( &myEv ) ) { + //Spontaneous event sent. Check if we need to continue. + if ( chokeMouse ) { + chokeMouse = FALSE; + return FALSE; + } + } + } +#endif + qt_button_down = childAt( pos ); //magic for masked widgets + if ( !qt_button_down || !qt_button_down->testWFlags(WMouseNoMask) ) + qt_button_down = this; + if ( mouseActWindow == event->xbutton.window && + mouseButtonPressed == button && + (long)event->xbutton.time -(long)mouseButtonPressTime + < TQApplication::doubleClickInterval() && + TQABS(event->xbutton.x - mouseXPos) < 5 && + TQABS(event->xbutton.y - mouseYPos) < 5 ) { + type = TQEvent::MouseButtonDblClick; + mouseButtonPressTime -= 2000; // no double-click next time + } else { + type = TQEvent::MouseButtonPress; + mouseButtonPressTime = event->xbutton.time; + } + mouseButtonPressed = button; // save event params for + mouseXPos = pos.x(); // future double click tests + mouseYPos = pos.y(); + mouseGlobalXPos = globalPos.x(); + mouseGlobalYPos = globalPos.y(); + } else { // mouse button released +#if defined(Q_OS_IRIX) && defined(QT_TABLET_SUPPORT) + XEvent myEv; + if ( XCheckTypedEvent( appDpy, xinput_button_release, &myEv ) ) { + if ( translateXinputEvent( &myEv ) ) { + //Spontaneous event sent. Check if we need to continue. + if ( chokeMouse ) { + chokeMouse = FALSE; + return FALSE; + } + } + } +#endif + if ( manualGrab ) { // release manual grab + manualGrab = FALSE; + XUngrabPointer( x11Display(), CurrentTime ); + XFlush( x11Display() ); + } + + type = TQEvent::MouseButtonRelease; + } + } + mouseActWindow = winId(); // save some event params + mouseButtonState = state; + if ( type == 0 ) // don't send event + return FALSE; + + if ( qApp->inPopupMode() ) { // in popup mode + TQWidget *popup = qApp->activePopupWidget(); + if ( popup != this ) { + if ( testWFlags(WType_Popup) && rect().contains(pos) ) + popup = this; + else // send to last popup + pos = popup->mapFromGlobal( globalPos ); + } + bool releaseAfter = FALSE; + TQWidget *popupChild = popup->childAt( pos ); + TQWidget *popupTarget = popupChild ? popupChild : popup; + + if (popup != popupOfPopupButtonFocus){ + popupButtonFocus = 0; + popupOfPopupButtonFocus = 0; + } + + if ( !popupTarget->isEnabled() ) { + if ( popupGrabOk ) + XAllowEvents( x11Display(), SyncPointer, CurrentTime ); + } + + switch ( type ) { + case TQEvent::MouseButtonPress: + case TQEvent::MouseButtonDblClick: + popupButtonFocus = popupChild; + popupOfPopupButtonFocus = popup; + break; + case TQEvent::MouseButtonRelease: + releaseAfter = TRUE; + break; + default: + break; // nothing for mouse move + } + + Display* dpy = x11Display(); // store display, send() may destroy us + + + int oldOpenPopupCount = openPopupCount; + + if ( popupButtonFocus ) { + TQMouseEvent e( type, popupButtonFocus->mapFromGlobal(globalPos), + globalPos, button, state ); + TQApplication::sendSpontaneousEvent( popupButtonFocus, &e ); + if ( releaseAfter ) { + popupButtonFocus = 0; + popupOfPopupButtonFocus = 0; + } + } else if ( popupChild ) { + TQMouseEvent e( type, popupChild->mapFromGlobal(globalPos), + globalPos, button, state ); + TQApplication::sendSpontaneousEvent( popupChild, &e ); + } else { + TQMouseEvent e( type, pos, globalPos, button, state ); + TQApplication::sendSpontaneousEvent( popup, &e ); + } + + if ( type == TQEvent::MouseButtonPress && button == RightButton && ( openPopupCount == oldOpenPopupCount ) ) { + TQWidget *popupEvent = popup; + if(popupButtonFocus) + popupEvent = popupButtonFocus; + else if(popupChild) + popupEvent = popupChild; + TQContextMenuEvent e( TQContextMenuEvent::Mouse, pos, globalPos, state ); + TQApplication::sendSpontaneousEvent( popupEvent, &e ); + } + + if ( releaseAfter ) + qt_button_down = 0; + + if ( qApp->inPopupMode() ) { // still in popup mode + if ( popupGrabOk ) + XAllowEvents( dpy, SyncPointer, CurrentTime ); + } else { + if ( type != TQEvent::MouseButtonRelease && state != 0 && + TQWidget::find((WId)mouseActWindow) ) { + manualGrab = TRUE; // need to manually grab + XGrabPointer( dpy, mouseActWindow, False, + (uint)(ButtonPressMask | ButtonReleaseMask | + ButtonMotionMask | + EnterWindowMask | LeaveWindowMask), + GrabModeAsync, GrabModeAsync, + None, None, CurrentTime ); + } + } + + } else { + TQWidget *widget = this; + TQWidget *w = TQWidget::mouseGrabber(); + if ( !w ) + w = qt_button_down; + if ( w && w != this ) { + widget = w; + pos = w->mapFromGlobal( globalPos ); + } + + if ( popupCloseDownMode ) { + popupCloseDownMode = FALSE; + if ( testWFlags(WType_Popup) ) // ignore replayed event + return TRUE; + } + + if ( type == TQEvent::MouseButtonRelease && + (state & (~button) & ( LeftButton | + MidButton | + RightButton)) == 0 ) { + qt_button_down = 0; + } + + int oldOpenPopupCount = openPopupCount; + + TQMouseEvent e( type, pos, globalPos, button, state ); + TQApplication::sendSpontaneousEvent( widget, &e ); + + if ( type == TQEvent::MouseButtonPress && button == RightButton && ( openPopupCount == oldOpenPopupCount ) ) { + TQContextMenuEvent e( TQContextMenuEvent::Mouse, pos, globalPos, state ); + TQApplication::sendSpontaneousEvent( widget, &e ); + } + } + return TRUE; +} + + +// +// Wheel event translation +// +bool TQETWidget::translateWheelEvent( int global_x, int global_y, int delta, int state, Orientation orient ) +{ + // send the event to the widget or its ancestors + { + TQWidget* popup = qApp->activePopupWidget(); + if ( popup && topLevelWidget() != popup ) + popup->close(); + TQWheelEvent e( mapFromGlobal(TQPoint( global_x, global_y)), + TQPoint(global_x, global_y), delta, state, orient ); + if ( TQApplication::sendSpontaneousEvent( this, &e ) ) + return TRUE; + } + + // send the event to the widget that has the focus or its ancestors, if different + TQWidget *w = this; + if ( w != qApp->focusWidget() && ( w = qApp->focusWidget() ) ) { + TQWidget* popup = qApp->activePopupWidget(); + if ( popup && w != popup ) + popup->hide(); + TQWheelEvent e( mapFromGlobal(TQPoint( global_x, global_y)), + TQPoint(global_x, global_y), delta, state, orient ); + if ( TQApplication::sendSpontaneousEvent( w, &e ) ) + return TRUE; + } + return FALSE; +} + + +// +// XInput Translation Event +// +#if defined (QT_TABLET_SUPPORT) +bool TQETWidget::translateXinputEvent( const XEvent *ev ) +{ +#if defined (Q_OS_IRIX) + // Wacom has put defines in their wacom.h file so it would be tquite wise + // to use them, need to think of a decent way of not using + // it when it doesn't exist... + XDeviceState *s; + XInputClass *iClass; + XValuatorState *vs; + int j; +#endif + TQWidget *w = this; + TQPoint global, + curr; + static int pressure = 0; + static int xTilt = 0, + yTilt = 0; + int deviceType = TQTabletEvent::NoDevice; + TQPair tId; + XEvent xinputMotionEvent; + XEvent mouseMotionEvent; + XDevice *dev; + const XDeviceMotionEvent *motion = 0; + XDeviceButtonEvent *button = 0; + TQEvent::Type t; + + if ( ev->type == xinput_motion ) { + motion = (const XDeviceMotionEvent*)ev; + for (;;) { + if (!XCheckTypedWindowEvent(x11Display(), winId(), MotionNotify, &mouseMotionEvent)) + break; + if (!XCheckTypedWindowEvent(x11Display(), winId(), xinput_motion, &xinputMotionEvent)) { + XPutBackEvent(x11Display(), &mouseMotionEvent); + break; + } + if (mouseMotionEvent.xmotion.time != motion->time) { + XPutBackEvent(x11Display(), &mouseMotionEvent); + XPutBackEvent(x11Display(), &xinputMotionEvent); + break; + } + motion = ((const XDeviceMotionEvent*)&xinputMotionEvent); + } + t = TQEvent::TabletMove; + curr = TQPoint( motion->x, motion->y ); + } else { + if ( ev->type == xinput_button_press ) { + t = TQEvent::TabletPress; + } else { + t = TQEvent::TabletRelease; + } + button = (XDeviceButtonEvent*)ev; +/* + qDebug( "\n\nXInput Button Event" ); + qDebug( "serial:\t%d", button->serial ); + qDebug( "send_event:\t%d", button->send_event ); + qDebug( "display:\t%p", button->display ); + qDebug( "window:\t%d", button->window ); + qDebug( "deviceID:\t%d", button->deviceid ); + qDebug( "root:\t%d", button->root ); + qDebug( "subwindot:\t%d", button->subwindow ); + qDebug( "x:\t%d", button->x ); + qDebug( "y:\t%d", button->y ); + qDebug( "x_root:\t%d", button->x_root ); + qDebug( "y_root:\t%d", button->y_root ); + qDebug( "state:\t%d", button->state ); + qDebug( "button:\t%d", button->button ); + qDebug( "same_screen:\t%d", button->same_screen ); + qDebug( "time:\t%d", button->time ); +*/ + curr = TQPoint( button->x, button->y ); + } +#if defined(Q_OS_IRIX) + // default... + dev = devStylus; +#else + if ( ev->type == xinput_motion ) { + if ( motion->deviceid == devStylus->device_id ) { + dev = devStylus; + deviceType = TQTabletEvent::Stylus; + } else if ( motion->deviceid == devEraser->device_id ) { + dev = devEraser; + deviceType = TQTabletEvent::Eraser; + } + } else { + if ( button->deviceid == devStylus->device_id ) { + dev = devStylus; + deviceType = TQTabletEvent::Stylus; + } else if ( button->deviceid == devEraser->device_id ) { + dev = devEraser; + deviceType = TQTabletEvent::Eraser; + } + } +#endif + + const int PRESSURE_LEVELS = 255; + // we got the maximum pressure at start time, since various tablets have + // varying levels of distinguishing pressure changes, let's standardize and + // scale everything to 256 different levels... + static int scaleFactor = -1; + if ( scaleFactor == -1 ) { + if ( max_pressure > PRESSURE_LEVELS ) + scaleFactor = max_pressure / PRESSURE_LEVELS; + else + scaleFactor = PRESSURE_LEVELS / max_pressure; + } +#if defined (Q_OS_IRIX) + s = XQueryDeviceState( appDpy, dev ); + if ( s == NULL ) + return FALSE; + iClass = s->data; + for ( j = 0; j < s->num_classes; j++ ) { + if ( iClass->c_class == ValuatorClass ) { + vs = (XValuatorState *)iClass; + // figure out what device we have, based on bitmasking... + if ( vs->valuators[WAC_TRANSDUCER_I] + & WAC_TRANSDUCER_PROX_MSK ) { + switch ( vs->valuators[WAC_TRANSDUCER_I] + & WAC_TRANSDUCER_MSK ) { + case WAC_PUCK_ID: + deviceType = TQTabletEvent::Puck; + break; + case WAC_STYLUS_ID: + deviceType = TQTabletEvent::Stylus; + break; + case WAC_ERASER_ID: + deviceType = TQTabletEvent::Eraser; + break; + } + // Get a Unique Id for the device, Wacom gives us this ability + tId.first = vs->valuators[WAC_TRANSDUCER_I] & WAC_TRANSDUCER_ID_MSK; + tId.second = vs->valuators[WAC_SERIAL_NUM_I]; + } else + deviceType = TQTabletEvent::NoDevice; + // apparently Wacom needs a cast for the +/- values to make sense + xTilt = short(vs->valuators[WAC_XTILT_I]); + yTilt = short(vs->valuators[WAC_YTILT_I]); + if ( max_pressure > PRESSURE_LEVELS ) + pressure = vs->valuators[WAC_PRESSURE_I] / scaleFactor; + else + pressure = vs->valuators[WAC_PRESSURE_I] * scaleFactor; + global = TQPoint( vs->valuators[WAC_XCOORD_I], + vs->valuators[WAC_YCOORD_I] ); + break; + } + iClass = (XInputClass*)((char*)iClass + iClass->length); + } + XFreeDeviceState( s ); +#else + if ( motion ) { + xTilt = short(motion->axis_data[3]); + yTilt = short(motion->axis_data[4]); + if ( max_pressure > PRESSURE_LEVELS ) + pressure = motion->axis_data[2] / scaleFactor; + else + pressure = motion->axis_data[2] * scaleFactor; + global = TQPoint( motion->axis_data[0], motion->axis_data[1] ); + } else { + xTilt = short(button->axis_data[3]); + yTilt = short(button->axis_data[4]); + if ( max_pressure > PRESSURE_LEVELS ) + pressure = button->axis_data[2] / scaleFactor; + else + pressure = button->axis_data[2] * scaleFactor; + global = TQPoint( button->axis_data[0], button->axis_data[1] ); + } + // The only way to get these Ids is to scan the XFree86 log, which I'm not going to do. + tId.first = tId.second = -1; +#endif + + TQTabletEvent e( t, curr, global, deviceType, pressure, xTilt, yTilt, tId ); + TQApplication::sendSpontaneousEvent( w, &e ); + return TRUE; +} +#endif + +bool TQETWidget::translatePropertyEvent(const XEvent *event) +{ + if (!isTopLevel()) return TRUE; + + Atom ret; + int format, e; + unsigned char *data = 0; + unsigned long nitems, after; + + if (event->xproperty.atom == qt_net_wm_frame_strut) { + topData()->fleft = topData()->fright = topData()->ftop = topData()->fbottom = 0; + fstrut_dirty = 1; + + if (event->xproperty.state == PropertyNewValue) { + e = XGetWindowProperty(appDpy, event->xproperty.window, qt_net_wm_frame_strut, + 0, 4, // struts are 4 longs + False, XA_CARDINAL, &ret, &format, &nitems, &after, &data); + + if (e == Success && ret == XA_CARDINAL && + format == 32 && nitems == 4) { + long *strut = (long *) data; + topData()->fleft = strut[0]; + topData()->fright = strut[1]; + topData()->ftop = strut[2]; + topData()->fbottom = strut[3]; + fstrut_dirty = 0; + } + } + } else if (event->xproperty.atom == qt_net_wm_state) { + bool max = FALSE; + bool full = FALSE; + + if (event->xproperty.state == PropertyNewValue) { + // using length of 1024 should be safe for all current and + // possible NET states... + e = XGetWindowProperty(appDpy, event->xproperty.window, qt_net_wm_state, 0, 1024, + False, XA_ATOM, &ret, &format, &nitems, &after, &data); + + if (e == Success && ret == XA_ATOM && format == 32 && nitems > 0) { + Atom *states = (Atom *) data; + + unsigned long i; + for (i = 0; i < nitems; i++) { + if (states[i] == qt_net_wm_state_max_v || states[i] == qt_net_wm_state_max_h) + max = TRUE; + else if (states[i] == qt_net_wm_state_fullscreen) + full = TRUE; + } + } + } + + bool send_event = FALSE; + + if (qt_net_supports(qt_net_wm_state_max_v) + && qt_net_supports(qt_net_wm_state_max_h)) { + if (max && !isMaximized()) { + setWState(WState_Maximized); + send_event = TRUE; + } else if (!max && isMaximized()) { + clearWState(WState_Maximized); + send_event = TRUE; + } + } + + if (qt_net_supports(qt_net_wm_state_fullscreen)) { + if (full && !isFullScreen()) { + setWState(WState_FullScreen); + send_event = TRUE; + } else if (!full && isFullScreen()) { + clearWState(WState_FullScreen); + send_event = TRUE; + } + } + + if (send_event) { + TQEvent e(TQEvent::WindowStateChange); + TQApplication::sendSpontaneousEvent(this, &e); + } + } else if (event->xproperty.atom == qt_wm_state) { + // the widget frame strut should also be invalidated + topData()->fleft = topData()->fright = topData()->ftop = topData()->fbottom = 0; + fstrut_dirty = 1; + + if (event->xproperty.state == PropertyDelete) { + // the window manager has removed the WM State property, + // so it is now in the withdrawn state (ICCCM 4.1.3.1) and + // we are free to reuse this window + topData()->parentWinId = 0; + // map the window if we were waiting for a transition to + // withdrawn + if ( qt_deferred_map_contains( this ) ) { + qt_deferred_map_take( this ); + XMapWindow( appDpy, winId() ); + } + } else if (topData()->parentWinId != TQPaintDevice::x11AppRootWindow(x11Screen())) { + // the window manager has changed the WM State property... + // we are wanting to see if we are withdrawn so that we + // can reuse this window... we only do this check *IF* we + // haven't been reparented to root - (the parentWinId != + // TQPaintDevice::x11AppRootWindow(x11Screen())) check + // above + + e = XGetWindowProperty(appDpy, winId(), qt_wm_state, 0, 2, False, qt_wm_state, + &ret, &format, &nitems, &after, &data ); + + if (e == Success && ret == qt_wm_state && format == 32 && nitems > 0) { + long *state = (long *) data; + switch (state[0]) { + case WithdrawnState: + // if we are in the withdrawn state, we are free + // to reuse this window provided we remove the + // WM_STATE property (ICCCM 4.1.3.1) + XDeleteProperty(appDpy, winId(), qt_wm_state); + + // set the parent id to zero, so that show() will + // work again + topData()->parentWinId = 0; + // map the window if we were waiting for a + // transition to withdrawn + if ( qt_deferred_map_contains( this ) ) { + qt_deferred_map_take( this ); + XMapWindow( appDpy, winId() ); + } + break; + + case IconicState: + if (!isMinimized()) { + // window was minimized + setWState(WState_Minimized); + TQEvent e(TQEvent::WindowStateChange); + TQApplication::sendSpontaneousEvent(this, &e); + } + break; + + default: + if (isMinimized()) { + // window was un-minimized + clearWState(WState_Minimized); + TQEvent e(TQEvent::WindowStateChange); + TQApplication::sendSpontaneousEvent(this, &e); + } + break; + } + } + } + } + + if (data) + XFree(data); + + return TRUE; +} + +#ifndef XK_ISO_Left_Tab +#define XK_ISO_Left_Tab 0xFE20 +#endif + +// the next lines are taken from XFree > 4.0 (X11/XF86keysyms.h), defining some special +// multimedia keys. They are included here as not every system has them. +#define XF86XK_Standby 0x1008FF10 +#define XF86XK_AudioLowerVolume 0x1008FF11 +#define XF86XK_AudioMute 0x1008FF12 +#define XF86XK_AudioRaiseVolume 0x1008FF13 +#define XF86XK_AudioPlay 0x1008FF14 +#define XF86XK_AudioStop 0x1008FF15 +#define XF86XK_AudioPrev 0x1008FF16 +#define XF86XK_AudioNext 0x1008FF17 +#define XF86XK_HomePage 0x1008FF18 +#define XF86XK_Calculator 0x1008FF1D +#define XF86XK_Mail 0x1008FF19 +#define XF86XK_Start 0x1008FF1A +#define XF86XK_Search 0x1008FF1B +#define XF86XK_AudioRecord 0x1008FF1C +#define XF86XK_Back 0x1008FF26 +#define XF86XK_Forward 0x1008FF27 +#define XF86XK_Stop 0x1008FF28 +#define XF86XK_Refresh 0x1008FF29 +#define XF86XK_Favorites 0x1008FF30 +#define XF86XK_AudioPause 0x1008FF31 +#define XF86XK_AudioMedia 0x1008FF32 +#define XF86XK_MyComputer 0x1008FF33 +#define XF86XK_OpenURL 0x1008FF38 +#define XF86XK_Launch0 0x1008FF40 +#define XF86XK_Launch1 0x1008FF41 +#define XF86XK_Launch2 0x1008FF42 +#define XF86XK_Launch3 0x1008FF43 +#define XF86XK_Launch4 0x1008FF44 +#define XF86XK_Launch5 0x1008FF45 +#define XF86XK_Launch6 0x1008FF46 +#define XF86XK_Launch7 0x1008FF47 +#define XF86XK_Launch8 0x1008FF48 +#define XF86XK_Launch9 0x1008FF49 +#define XF86XK_LaunchA 0x1008FF4A +#define XF86XK_LaunchB 0x1008FF4B +#define XF86XK_LaunchC 0x1008FF4C +#define XF86XK_LaunchD 0x1008FF4D +#define XF86XK_LaunchE 0x1008FF4E +#define XF86XK_LaunchF 0x1008FF4F +// end of XF86keysyms.h + + + +static const KeySym KeyTbl[] = { // keyboard mapping table + XK_Escape, TQt::Key_Escape, // misc keys + XK_Tab, TQt::Key_Tab, + XK_ISO_Left_Tab, TQt::Key_Backtab, + XK_BackSpace, TQt::Key_Backspace, + XK_Return, TQt::Key_Return, + XK_Insert, TQt::Key_Insert, + XK_KP_Insert, TQt::Key_Insert, + XK_Delete, TQt::Key_Delete, + XK_KP_Delete, TQt::Key_Delete, + XK_Clear, TQt::Key_Delete, + XK_Pause, TQt::Key_Pause, + XK_Print, TQt::Key_Print, + XK_KP_Begin, TQt::Key_Clear, + 0x1005FF60, TQt::Key_SysReq, // hardcoded Sun SysReq + 0x1007ff00, TQt::Key_SysReq, // hardcoded X386 SysReq + XK_Home, TQt::Key_Home, // cursor movement + XK_End, TQt::Key_End, + XK_Left, TQt::Key_Left, + XK_Up, TQt::Key_Up, + XK_Right, TQt::Key_Right, + XK_Down, TQt::Key_Down, + XK_Prior, TQt::Key_Prior, + XK_Next, TQt::Key_Next, + XK_KP_Home, TQt::Key_Home, + XK_KP_End, TQt::Key_End, + XK_KP_Left, TQt::Key_Left, + XK_KP_Up, TQt::Key_Up, + XK_KP_Right, TQt::Key_Right, + XK_KP_Down, TQt::Key_Down, + XK_KP_Prior, TQt::Key_Prior, + XK_KP_Next, TQt::Key_Next, + XK_Shift_L, TQt::Key_Shift, // modifiers + XK_Shift_R, TQt::Key_Shift, + XK_Shift_Lock, TQt::Key_Shift, + XK_Control_L, TQt::Key_Control, + XK_Control_R, TQt::Key_Control, + XK_Meta_L, TQt::Key_Meta, + XK_Meta_R, TQt::Key_Meta, + XK_Alt_L, TQt::Key_Alt, + XK_Alt_R, TQt::Key_Alt, + XK_Caps_Lock, TQt::Key_CapsLock, + XK_Num_Lock, TQt::Key_NumLock, + XK_Scroll_Lock, TQt::Key_ScrollLock, + XK_KP_Space, TQt::Key_Space, // numeric keypad + XK_KP_Tab, TQt::Key_Tab, + XK_KP_Enter, TQt::Key_Enter, + XK_KP_Equal, TQt::Key_Equal, + XK_KP_Multiply, TQt::Key_Asterisk, + XK_KP_Add, TQt::Key_Plus, + XK_KP_Separator, TQt::Key_Comma, + XK_KP_Subtract, TQt::Key_Minus, + XK_KP_Decimal, TQt::Key_Period, + XK_KP_Divide, TQt::Key_Slash, + XK_Super_L, TQt::Key_Super_L, + XK_Super_R, TQt::Key_Super_R, + XK_Menu, TQt::Key_Menu, + XK_Hyper_L, TQt::Key_Hyper_L, + XK_Hyper_R, TQt::Key_Hyper_R, + XK_Help, TQt::Key_Help, + 0x1000FF74, TQt::Key_BackTab, // hardcoded HP backtab + 0x1005FF10, TQt::Key_F11, // hardcoded Sun F36 (labeled F11) + 0x1005FF11, TQt::Key_F12, // hardcoded Sun F37 (labeled F12) + + // International input method support keys + + // International & multi-key character composition + XK_Multi_key, TQt::Key_Multi_key, + XK_Codeinput, TQt::Key_Codeinput, + XK_SingleCandidate, TQt::Key_SingleCandidate, + XK_MultipleCandidate, TQt::Key_MultipleCandidate, + XK_PreviousCandidate, TQt::Key_PreviousCandidate, + + // Misc Functions + XK_Mode_switch, TQt::Key_Mode_switch, + //XK_script_switch, TQt::Key_script_switch, + XK_script_switch, TQt::Key_Mode_switch, + + // Japanese keyboard support + XK_Kanji, TQt::Key_Kanji, + XK_Muhenkan, TQt::Key_Muhenkan, + //XK_Henkan_Mode, TQt::Key_Henkan_Mode, + XK_Henkan_Mode, TQt::Key_Henkan, + XK_Henkan, TQt::Key_Henkan, + XK_Romaji, TQt::Key_Romaji, + XK_Hiragana, TQt::Key_Hiragana, + XK_Katakana, TQt::Key_Katakana, + XK_Hiragana_Katakana, TQt::Key_Hiragana_Katakana, + XK_Zenkaku, TQt::Key_Zenkaku, + XK_Hankaku, TQt::Key_Hankaku, + XK_Zenkaku_Hankaku, TQt::Key_Zenkaku_Hankaku, + XK_Touroku, TQt::Key_Touroku, + XK_Massyo, TQt::Key_Massyo, + XK_Kana_Lock, TQt::Key_Kana_Lock, + XK_Kana_Shift, TQt::Key_Kana_Shift, + XK_Eisu_Shift, TQt::Key_Eisu_Shift, + XK_Eisu_toggle, TQt::Key_Eisu_toggle, + //XK_Kanji_Bangou, TQt::Key_Kanji_Bangou, + //XK_Zen_Koho, TQt::Key_Zen_Koho, + //XK_Mae_Koho, TQt::Key_Mae_Koho, + XK_Kanji_Bangou, TQt::Key_Codeinput, + XK_Zen_Koho, TQt::Key_MultipleCandidate, + XK_Mae_Koho, TQt::Key_PreviousCandidate, + +#ifdef XK_KOREAN + // Korean keyboard support + XK_Hangul, TQt::Key_Hangul, + XK_Hangul_Start, TQt::Key_Hangul_Start, + XK_Hangul_End, TQt::Key_Hangul_End, + XK_Hangul_Hanja, TQt::Key_Hangul_Hanja, + XK_Hangul_Jamo, TQt::Key_Hangul_Jamo, + XK_Hangul_Romaja, TQt::Key_Hangul_Romaja, + //XK_Hangul_Codeinput, TQt::Key_Hangul_Codeinput, + XK_Hangul_Codeinput, TQt::Key_Codeinput, + XK_Hangul_Jeonja, TQt::Key_Hangul_Jeonja, + XK_Hangul_Banja, TQt::Key_Hangul_Banja, + XK_Hangul_PreHanja, TQt::Key_Hangul_PreHanja, + XK_Hangul_PostHanja, TQt::Key_Hangul_PostHanja, + //XK_Hangul_SingleCandidate, TQt::Key_Hangul_SingleCandidate, + //XK_Hangul_MultipleCandidate, TQt::Key_Hangul_MultipleCandidate, + //XK_Hangul_PreviousCandidate, TQt::Key_Hangul_PreviousCandidate, + XK_Hangul_SingleCandidate, TQt::Key_SingleCandidate, + XK_Hangul_MultipleCandidate, TQt::Key_MultipleCandidate, + XK_Hangul_PreviousCandidate, TQt::Key_PreviousCandidate, + XK_Hangul_Special, TQt::Key_Hangul_Special, + //XK_Hangul_switch, TQt::Key_Hangul_switch, + XK_Hangul_switch, TQt::Key_Mode_switch, +#endif // XK_KOREAN + + // dead keys + XK_dead_grave, TQt::Key_Dead_Grave, + XK_dead_acute, TQt::Key_Dead_Acute, + XK_dead_circumflex, TQt::Key_Dead_Circumflex, + XK_dead_tilde, TQt::Key_Dead_Tilde, + XK_dead_macron, TQt::Key_Dead_Macron, + XK_dead_breve, TQt::Key_Dead_Breve, + XK_dead_abovedot, TQt::Key_Dead_Abovedot, + XK_dead_diaeresis, TQt::Key_Dead_Diaeresis, + XK_dead_abovering, TQt::Key_Dead_Abovering, + XK_dead_doubleacute, TQt::Key_Dead_Doubleacute, + XK_dead_caron, TQt::Key_Dead_Caron, + XK_dead_cedilla, TQt::Key_Dead_Cedilla, + XK_dead_ogonek, TQt::Key_Dead_Ogonek, + XK_dead_iota, TQt::Key_Dead_Iota, + XK_dead_voiced_sound, TQt::Key_Dead_Voiced_Sound, + XK_dead_semivoiced_sound, TQt::Key_Dead_Semivoiced_Sound, + XK_dead_belowdot, TQt::Key_Dead_Belowdot, + XK_dead_hook, TQt::Key_Dead_Hook, + XK_dead_horn, TQt::Key_Dead_Horn, + + // Special multimedia keys + // currently only tested with MS internet keyboard + + // browsing keys + XF86XK_Back, TQt::Key_Back, + XF86XK_Forward, TQt::Key_Forward, + XF86XK_Stop, TQt::Key_Stop, + XF86XK_Refresh, TQt::Key_Refresh, + XF86XK_Favorites, TQt::Key_Favorites, + XF86XK_AudioMedia, TQt::Key_LaunchMedia, + XF86XK_OpenURL, TQt::Key_OpenUrl, + XF86XK_HomePage, TQt::Key_HomePage, + XF86XK_Search, TQt::Key_Search, + + // media keys + XF86XK_AudioLowerVolume, TQt::Key_VolumeDown, + XF86XK_AudioMute, TQt::Key_VolumeMute, + XF86XK_AudioRaiseVolume, TQt::Key_VolumeUp, + XF86XK_AudioPlay, TQt::Key_MediaPlay, + XF86XK_AudioStop, TQt::Key_MediaStop, + XF86XK_AudioPrev, TQt::Key_MediaPrev, + XF86XK_AudioNext, TQt::Key_MediaNext, + XF86XK_AudioRecord, TQt::Key_MediaRecord, + + // launch keys + XF86XK_Mail, TQt::Key_LaunchMail, + XF86XK_MyComputer, TQt::Key_Launch0, + XF86XK_Calculator, TQt::Key_Launch1, + XF86XK_Standby, TQt::Key_Standby, + + XF86XK_Launch0, TQt::Key_Launch2, + XF86XK_Launch1, TQt::Key_Launch3, + XF86XK_Launch2, TQt::Key_Launch4, + XF86XK_Launch3, TQt::Key_Launch5, + XF86XK_Launch4, TQt::Key_Launch6, + XF86XK_Launch5, TQt::Key_Launch7, + XF86XK_Launch6, TQt::Key_Launch8, + XF86XK_Launch7, TQt::Key_Launch9, + XF86XK_Launch8, TQt::Key_LaunchA, + XF86XK_Launch9, TQt::Key_LaunchB, + XF86XK_LaunchA, TQt::Key_LaunchC, + XF86XK_LaunchB, TQt::Key_LaunchD, + XF86XK_LaunchC, TQt::Key_LaunchE, + XF86XK_LaunchD, TQt::Key_LaunchF, + + 0, 0 +}; + + +static TQIntDict *keyDict = 0; +static TQIntDict *textDict = 0; + +static void deleteKeyDicts() +{ + if ( keyDict ) + delete keyDict; + keyDict = 0; + if ( textDict ) + delete textDict; + textDict = 0; +} + +#if !defined(QT_NO_XIM) +static const unsigned short katakanaKeysymsToUnicode[] = { + 0x0000, 0x3002, 0x300C, 0x300D, 0x3001, 0x30FB, 0x30F2, 0x30A1, + 0x30A3, 0x30A5, 0x30A7, 0x30A9, 0x30E3, 0x30E5, 0x30E7, 0x30C3, + 0x30FC, 0x30A2, 0x30A4, 0x30A6, 0x30A8, 0x30AA, 0x30AB, 0x30AD, + 0x30AF, 0x30B1, 0x30B3, 0x30B5, 0x30B7, 0x30B9, 0x30BB, 0x30BD, + 0x30BF, 0x30C1, 0x30C4, 0x30C6, 0x30C8, 0x30CA, 0x30CB, 0x30CC, + 0x30CD, 0x30CE, 0x30CF, 0x30D2, 0x30D5, 0x30D8, 0x30DB, 0x30DE, + 0x30DF, 0x30E0, 0x30E1, 0x30E2, 0x30E4, 0x30E6, 0x30E8, 0x30E9, + 0x30EA, 0x30EB, 0x30EC, 0x30ED, 0x30EF, 0x30F3, 0x309B, 0x309C +}; + +static const unsigned short cyrillicKeysymsToUnicode[] = { + 0x0000, 0x0452, 0x0453, 0x0451, 0x0454, 0x0455, 0x0456, 0x0457, + 0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x0000, 0x045e, 0x045f, + 0x2116, 0x0402, 0x0403, 0x0401, 0x0404, 0x0405, 0x0406, 0x0407, + 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x0000, 0x040e, 0x040f, + 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, + 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, + 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, + 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, + 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, + 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, + 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, + 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a +}; + +static const unsigned short greekKeysymsToUnicode[] = { + 0x0000, 0x0386, 0x0388, 0x0389, 0x038a, 0x03aa, 0x0000, 0x038c, + 0x038e, 0x03ab, 0x0000, 0x038f, 0x0000, 0x0000, 0x0385, 0x2015, + 0x0000, 0x03ac, 0x03ad, 0x03ae, 0x03af, 0x03ca, 0x0390, 0x03cc, + 0x03cd, 0x03cb, 0x03b0, 0x03ce, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, + 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, + 0x03a0, 0x03a1, 0x03a3, 0x0000, 0x03a4, 0x03a5, 0x03a6, 0x03a7, + 0x03a8, 0x03a9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, + 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, + 0x03c0, 0x03c1, 0x03c3, 0x03c2, 0x03c4, 0x03c5, 0x03c6, 0x03c7, + 0x03c8, 0x03c9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 +}; + +static const unsigned short technicalKeysymsToUnicode[] = { + 0x0000, 0x23B7, 0x250C, 0x2500, 0x2320, 0x2321, 0x2502, 0x23A1, + 0x23A3, 0x23A4, 0x23A6, 0x239B, 0x239D, 0x239E, 0x23A0, 0x23A8, + 0x23AC, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x2264, 0x2260, 0x2265, 0x222B, + 0x2234, 0x221D, 0x221E, 0x0000, 0x0000, 0x2207, 0x0000, 0x0000, + 0x223C, 0x2243, 0x0000, 0x0000, 0x0000, 0x21D4, 0x21D2, 0x2261, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x221A, 0x0000, + 0x0000, 0x0000, 0x2282, 0x2283, 0x2229, 0x222A, 0x2227, 0x2228, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x2202, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0192, 0x0000, + 0x0000, 0x0000, 0x0000, 0x2190, 0x2191, 0x2192, 0x2193, 0x0000 +}; + +static const unsigned short specialKeysymsToUnicode[] = { + 0x25C6, 0x2592, 0x2409, 0x240C, 0x240D, 0x240A, 0x0000, 0x0000, + 0x2424, 0x240B, 0x2518, 0x2510, 0x250C, 0x2514, 0x253C, 0x23BA, + 0x23BB, 0x2500, 0x23BC, 0x23BD, 0x251C, 0x2524, 0x2534, 0x252C, + 0x2502, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 +}; + +static const unsigned short publishingKeysymsToUnicode[] = { + 0x0000, 0x2003, 0x2002, 0x2004, 0x2005, 0x2007, 0x2008, 0x2009, + 0x200a, 0x2014, 0x2013, 0x0000, 0x0000, 0x0000, 0x2026, 0x2025, + 0x2153, 0x2154, 0x2155, 0x2156, 0x2157, 0x2158, 0x2159, 0x215a, + 0x2105, 0x0000, 0x0000, 0x2012, 0x2329, 0x0000, 0x232a, 0x0000, + 0x0000, 0x0000, 0x0000, 0x215b, 0x215c, 0x215d, 0x215e, 0x0000, + 0x0000, 0x2122, 0x2613, 0x0000, 0x25c1, 0x25b7, 0x25cb, 0x25af, + 0x2018, 0x2019, 0x201c, 0x201d, 0x211e, 0x0000, 0x2032, 0x2033, + 0x0000, 0x271d, 0x0000, 0x25ac, 0x25c0, 0x25b6, 0x25cf, 0x25ae, + 0x25e6, 0x25ab, 0x25ad, 0x25b3, 0x25bd, 0x2606, 0x2022, 0x25aa, + 0x25b2, 0x25bc, 0x261c, 0x261e, 0x2663, 0x2666, 0x2665, 0x0000, + 0x2720, 0x2020, 0x2021, 0x2713, 0x2717, 0x266f, 0x266d, 0x2642, + 0x2640, 0x260e, 0x2315, 0x2117, 0x2038, 0x201a, 0x201e, 0x0000 +}; + +static const unsigned short aplKeysymsToUnicode[] = { + 0x0000, 0x0000, 0x0000, 0x003c, 0x0000, 0x0000, 0x003e, 0x0000, + 0x2228, 0x2227, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x00af, 0x0000, 0x22a5, 0x2229, 0x230a, 0x0000, 0x005f, 0x0000, + 0x0000, 0x0000, 0x2218, 0x0000, 0x2395, 0x0000, 0x22a4, 0x25cb, + 0x0000, 0x0000, 0x0000, 0x2308, 0x0000, 0x0000, 0x222a, 0x0000, + 0x2283, 0x0000, 0x2282, 0x0000, 0x22a2, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x22a3, 0x0000, 0x0000, 0x0000 +}; + +static const unsigned short koreanKeysymsToUnicode[] = { + 0x0000, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, + 0x3138, 0x3139, 0x313a, 0x313b, 0x313c, 0x313d, 0x313e, 0x313f, + 0x3140, 0x3141, 0x3142, 0x3143, 0x3144, 0x3145, 0x3146, 0x3147, + 0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e, 0x314f, + 0x3150, 0x3151, 0x3152, 0x3153, 0x3154, 0x3155, 0x3156, 0x3157, + 0x3158, 0x3159, 0x315a, 0x315b, 0x315c, 0x315d, 0x315e, 0x315f, + 0x3160, 0x3161, 0x3162, 0x3163, 0x11a8, 0x11a9, 0x11aa, 0x11ab, + 0x11ac, 0x11ad, 0x11ae, 0x11af, 0x11b0, 0x11b1, 0x11b2, 0x11b3, + 0x11b4, 0x11b5, 0x11b6, 0x11b7, 0x11b8, 0x11b9, 0x11ba, 0x11bb, + 0x11bc, 0x11bd, 0x11be, 0x11bf, 0x11c0, 0x11c1, 0x11c2, 0x316d, + 0x3171, 0x3178, 0x317f, 0x3181, 0x3184, 0x3186, 0x318d, 0x318e, + 0x11eb, 0x11f0, 0x11f9, 0x0000, 0x0000, 0x0000, 0x0000, 0x20a9 +}; + + +static TQChar keysymToUnicode(unsigned char byte3, unsigned char byte4) +{ + if ( byte3 == 0x04 ) { + // katakana + if ( byte4 > 0xa0 && byte4 < 0xe0 ) + return TQChar( katakanaKeysymsToUnicode[byte4 - 0xa0] ); + else if ( byte4 == 0x7e ) + return TQChar( 0x203e ); // Overline + } else if ( byte3 == 0x06 ) { + // russian, use lookup table + if ( byte4 > 0xa0 ) + return TQChar( cyrillicKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x07 ) { + // greek + if ( byte4 > 0xa0 ) + return TQChar( greekKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x08 ) { + // technical + if ( byte4 > 0xa0 ) + return TQChar( technicalKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x09 ) { + // special + if ( byte4 >= 0xe0 ) + return TQChar( specialKeysymsToUnicode[byte4 - 0xe0] ); + } else if ( byte3 == 0x0a ) { + // publishing + if ( byte4 > 0xa0 ) + return TQChar( publishingKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x0b ) { + // APL + if ( byte4 > 0xa0 ) + return TQChar( aplKeysymsToUnicode[byte4 - 0xa0] ); + } else if ( byte3 == 0x0e ) { + // Korean + if ( byte4 > 0xa0 ) + return TQChar( koreanKeysymsToUnicode[byte4 - 0xa0] ); + } + return TQChar(0x0); +} +#endif + + +bool TQETWidget::translateKeyEventInternal( const XEvent *event, int& count, + TQString& text, + int& state, + char& ascii, int& code, TQEvent::Type &type, bool willRepeat, bool statefulTranslation ) +{ + TQTextCodec *mapper = qt_input_mapper; + // some XmbLookupString implementations don't return buffer overflow correctly, + // so we increase the input buffer to allow for long strings... + // 256 chars * 2 bytes + 1 null-term == 513 bytes + TQCString chars(513); + TQChar converted; + KeySym key = 0; + + if ( !keyDict ) { + keyDict = new TQIntDict( 13 ); + keyDict->setAutoDelete( FALSE ); + textDict = new TQIntDict( 13 ); + textDict->setAutoDelete( FALSE ); + qAddPostRoutine( deleteKeyDicts ); + } + + TQWidget* tlw = topLevelWidget(); + + XKeyEvent xkeyevent = event->xkey; + + // save the modifier state, we will use the keystate uint later by passing + // it to qt_x11_translateButtonState + uint keystate = event->xkey.state; + // remove the modifiers where mode_switch exists... HPUX machines seem + // to have alt *AND* mode_switch both in Mod1Mask, which causes + // XLookupString to return things like 'å' (aring) for ALT-A. This + // completely breaks modifiers. If we remove the modifier for Mode_switch, + // then things work correctly... + xkeyevent.state &= ~qt_mode_switch_remove_mask; + + type = (event->type == XKeyPress) + ? TQEvent::KeyPress : TQEvent::KeyRelease; +#if defined(QT_NO_XIM) + + count = XLookupString( &xkeyevent, chars.data(), chars.size(), &key, 0 ); + + if ( count == 1 ) + ascii = chars[0]; + +#else + // Implementation for X11R5 and newer, using XIM + + int keycode = event->xkey.keycode; + Status status; + + if ( type == TQEvent::KeyPress ) { + bool mb=FALSE; + // commit string handling is done by + // TQXIMInputContext::x11FilterEvent() and are passed to + // widgets via TQIMEvent regardless of XIM style, so the + // following code is commented out. +#if 0 + if ( qt_xim ) { + TQTLWExtra* xd = tlw->topData(); + TQInputContext *qic = (TQInputContext *) xd->xic; + if ( qic ) { + mb=TRUE; + count = qic->lookupString(&xkeyevent, chars, &key, &status); + } + } +#endif + if ( !mb ) { + count = XLookupString( &xkeyevent, + chars.data(), chars.size(), &key, 0 ); + } + if ( count && !keycode ) { + keycode = qt_ximComposingKeycode; + qt_ximComposingKeycode = 0; + } + if ( key ) + keyDict->replace( keycode, (void*)key ); + // all keysyms smaller than that are actally keys that can be mapped + // to unicode chars + if ( count == 0 && key < 0xff00 ) { + unsigned char byte3 = (unsigned char )(key >> 8); + int mib = -1; + switch( byte3 ) { + case 0: // Latin 1 + case 1: // Latin 2 + case 2: //latin 3 + case 3: // latin4 + mib = byte3 + 4; break; + case 5: // arabic + mib = 82; break; + case 12: // Hebrew + mib = 85; break; + case 13: // Thai + mib = 2259; break; + case 4: // kana + case 6: // cyrillic + case 7: // greek + case 8: // technical, no mapping here at the moment + case 9: // Special + case 10: // Publishing + case 11: // APL + case 14: // Korean, no mapping + mib = -1; // manual conversion + mapper = 0; + converted = keysymToUnicode( byte3, key & 0xff ); + case 0x20: + // currency symbols + if ( key >= 0x20a0 && key <= 0x20ac ) { + mib = -1; // manual conversion + mapper = 0; + converted = (uint)key; + } + break; + default: + break; + } + if ( mib != -1 ) { + mapper = TQTextCodec::codecForMib( mib ); + chars[0] = (unsigned char) (key & 0xff); // get only the fourth bit for conversion later + count++; + } + } else if ( key >= 0x1000000 && key <= 0x100ffff ) { + converted = (ushort) (key - 0x1000000); + mapper = 0; + } + if ( count < (int)chars.size()-1 ) + chars[count] = '\0'; + if ( count == 1 ) { + ascii = chars[0]; + // +256 so we can store all eight-bit codes, including ascii 0, + // and independent of whether char is signed or not. + textDict->replace( keycode, (void*)(long)(256+ascii) ); + } + tlw = 0; + } else { + key = (int)(long)keyDict->find( keycode ); + if ( key ) + if( !willRepeat && statefulTranslation ) // Take out key of dictionary only if this call. + keyDict->take( keycode ); + long s = (long)textDict->find( keycode ); + if ( s ) { + if( statefulTranslation ) + textDict->take( keycode ); + ascii = (char)(s-256); + } + } +#endif // !QT_NO_XIM + + state = qt_x11_translateButtonState( keystate ); + + static int directionKeyEvent = 0; + static unsigned int lastWinId = 0; + if ( qt_use_rtl_extensions && type == TQEvent::KeyRelease && statefulTranslation ) { + if (directionKeyEvent == Key_Direction_R || directionKeyEvent == Key_Direction_L ) { + type = TQEvent::KeyPress; + code = directionKeyEvent; + chars[0] = 0; + directionKeyEvent = 0; + lastWinId = 0; + return TRUE; + } else { + directionKeyEvent = 0; + lastWinId = 0; + } + } + + // Watch for keypresses and if its a key belonging to the Ctrl-Shift + // direction-changing accel, remember it. + // We keep track of those keys instead of using the event's state + // (to figure out whether the Ctrl modifier is held while Shift is pressed, + // or Shift is held while Ctrl is pressed) since the 'state' doesn't tell + // us whether the modifier held is Left or Right. + if ( qt_use_rtl_extensions && type == TQEvent::KeyPress && statefulTranslation ) + if (key == XK_Control_L || key == XK_Control_R || key == XK_Shift_L || key == XK_Shift_R) { + if (!directionKeyEvent) { + directionKeyEvent = key; + // This code exists in order to check that + // the event is occurred in the same widget. + lastWinId = winId(); + } + } else { + // this can no longer be a direction-changing accel. + // if any other key was pressed. + directionKeyEvent = Key_Space; + } + + // Commentary in X11/keysymdef says that X codes match ASCII, so it + // is safe to use the locale functions to process X codes in ISO8859-1. + // + // This is mainly for compatibility - applications should not use the + // TQt keycodes between 128 and 255, but should rather use the + // TQKeyEvent::text(). + // + if ( key < 128 || (key < 256 && (!qt_input_mapper || qt_input_mapper->mibEnum()==4)) ) { + code = isprint((int)key) ? toupper((int)key) : 0; // upper-case key, if known + } else if ( key >= XK_F1 && key <= XK_F35 ) { + code = Key_F1 + ((int)key - XK_F1); // function keys + } else if ( key >= XK_KP_0 && key <= XK_KP_9) { + code = Key_0 + ((int)key - XK_KP_0); // numeric keypad keys + state |= Keypad; + } else { + int i = 0; // any other keys + while ( KeyTbl[i] ) { + if ( key == KeyTbl[i] ) { + code = (int)KeyTbl[i+1]; + break; + } + i += 2; + } + switch ( key ) { + case XK_KP_Insert: + case XK_KP_Delete: + case XK_KP_Home: + case XK_KP_End: + case XK_KP_Left: + case XK_KP_Up: + case XK_KP_Right: + case XK_KP_Down: + case XK_KP_Prior: + case XK_KP_Next: + case XK_KP_Space: + case XK_KP_Tab: + case XK_KP_Enter: + case XK_KP_Equal: + case XK_KP_Multiply: + case XK_KP_Add: + case XK_KP_Separator: + case XK_KP_Subtract: + case XK_KP_Decimal: + case XK_KP_Divide: + state |= Keypad; + break; + default: + break; + } + + if ( code == Key_Tab && + (state & ShiftButton) == ShiftButton ) { + // map shift+tab to shift+backtab, TQAccel knows about it + // and will handle it. + code = Key_Backtab; + chars[0] = 0; + } + + if ( qt_use_rtl_extensions && type == TQEvent::KeyPress && statefulTranslation ) { + if ( directionKeyEvent && lastWinId == winId() ) { + if ( key == XK_Shift_L && directionKeyEvent == XK_Control_L || + key == XK_Control_L && directionKeyEvent == XK_Shift_L ) { + directionKeyEvent = Key_Direction_L; + } else if ( key == XK_Shift_R && directionKeyEvent == XK_Control_R || + key == XK_Control_R && directionKeyEvent == XK_Shift_R ) { + directionKeyEvent = Key_Direction_R; + } + } + else if ( directionKeyEvent == Key_Direction_L || directionKeyEvent == Key_Direction_R ) { + directionKeyEvent = Key_Space; // invalid + } + } + } + +#if 0 +#ifndef Q_EE + static int c = 0; + extern void qt_dialog_default_key(); +#define Q_EE(x) c = (c == x || (!c && x == 0x1000) )? x+1 : 0 + if ( tlw && state == '0' ) { + switch ( code ) { + case 0x4f: Q_EE(Key_Backtab); break; + case 0x52: Q_EE(Key_Tab); break; + case 0x54: Q_EE(Key_Escape); break; + case 0x4c: + if (c == Key_Return ) + qt_dialog_default_key(); + else + Q_EE(Key_Backspace); + break; + } + } +#undef Q_EE +#endif +#endif + + // convert chars (8bit) to text (unicode). + if ( mapper ) + text = mapper->toUnicode(chars,count); + else if ( !mapper && converted.unicode() != 0x0 ) + text = converted; + else + text = chars; + return TRUE; +} + + +struct qt_auto_repeat_data +{ + // match the window and keycode with timestamp delta of 10ms + Window window; + KeyCode keycode; + Time timestamp; + + // queue scanner state + bool release; + bool error; +}; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static Bool qt_keypress_scanner(Display *, XEvent *event, XPointer arg) +{ + if (event->type != XKeyPress && event->type != XKeyRelease) + return FALSE; + + qt_auto_repeat_data *d = (qt_auto_repeat_data *) arg; + if (d->error || + event->xkey.window != d->window || + event->xkey.keycode != d->keycode) { + d->error = TRUE; + return FALSE; + } + + if (event->type == XKeyPress) { + d->error = (! d->release || event->xkey.time - d->timestamp > 10); + return (! d->error); + } + + // must be XKeyRelease event + if (d->release) { + // found a second release + d->error = TRUE; + return FALSE; + } + + // found a single release + d->release = TRUE; + d->timestamp = event->xkey.time; + + return FALSE; +} + +static Bool qt_keyrelease_scanner(Display *, XEvent *event, XPointer arg) +{ + const qt_auto_repeat_data *d = (const qt_auto_repeat_data *) arg; + return (event->type == XKeyRelease && + event->xkey.window == d->window && + event->xkey.keycode == d->keycode); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + +bool TQETWidget::translateKeyEvent( const XEvent *event, bool grab ) +{ + int code = -1; + int count = 0; + int state; + char ascii = 0; + + if ( sm_blockUserInput ) // block user interaction during session management + return TRUE; + + Display *dpy = x11Display(); + + if ( !isEnabled() ) + return TRUE; + + TQEvent::Type type; + bool autor = FALSE; + TQString text; + + translateKeyEventInternal( event, count, text, state, ascii, code, type, + qt_mode_switch_remove_mask != 0 ); + + static uint curr_autorep = 0; + // was this the last auto-repeater? + qt_auto_repeat_data auto_repeat_data; + auto_repeat_data.window = event->xkey.window; + auto_repeat_data.keycode = event->xkey.keycode; + auto_repeat_data.timestamp = event->xkey.time; + + if ( event->type == XKeyPress ) { + if ( curr_autorep == event->xkey.keycode ) { + autor = TRUE; + curr_autorep = 0; + } + } else { + // look ahead for auto-repeat + XEvent nextpress; + + auto_repeat_data.release = TRUE; + auto_repeat_data.error = FALSE; + if (XCheckIfEvent(dpy, &nextpress, &qt_keypress_scanner, + (XPointer) &auto_repeat_data)) { + autor = TRUE; + + // Put it back... we COULD send the event now and not need + // the static curr_autorep variable. + XPutBackEvent(dpy,&nextpress); + } + curr_autorep = autor ? event->xkey.keycode : 0; + } + + // process accelerators before doing key compression + if ( type == TQEvent::KeyPress && !grab ) { + // send accel events if the keyboard is not grabbed + TQKeyEvent a( type, code, ascii, state, text, autor, + TQMAX( TQMAX(count,1), int(text.length())) ); + if ( qt_tryAccelEvent( this, &a ) ) + return TRUE; + } + + long save = 0; + if ( qt_mode_switch_remove_mask != 0 ) { + save = qt_mode_switch_remove_mask; + qt_mode_switch_remove_mask = 0; + + // translate the key event again, but this time apply any Mode_switch + // modifiers + translateKeyEventInternal( event, count, text, state, ascii, code, type ); + } + +#ifndef QT_NO_IM + TQInputContext *qic = getInputContext(); +#endif + + // compress keys + if ( !text.isEmpty() && testWState(WState_CompressKeys) && +#ifndef QT_NO_IM + // Ordinary input methods retquire discrete key events to work + // properly, so key compression has to be disabled when input + // context exists. + // + // And further consideration, some complex input method + // retquire all key press/release events discretely even if + // the input method awares of key compression and compressed + // keys are ordinary alphabets. For example, the uim project + // is planning to implement "combinational shift" feature for + // a Japanese input method, uim-skk. It will work as follows. + // + // 1. press "r" + // 2. press "u" + // 3. release both "r" and "u" in arbitrary order + // 4. above key sequence generates "Ru" + // + // Of course further consideration about other participants + // such as key repeat mechanism is retquired to implement such + // feature. + ! qic && +#endif // QT_NO_IM + // do not compress keys if the key event we just got above matches + // one of the key ranges used to compute stopCompression + ! ( ( code >= Key_Escape && code <= Key_SysReq ) || + ( code >= Key_Home && code <= Key_Next ) || + ( code >= Key_Super_L && code <= Key_Direction_R ) || + ( ( code == 0 ) && ( ascii == '\n' ) ) ) ) { + // the widget wants key compression so it gets it + int codeIntern = -1; + int countIntern = 0; + int stateIntern; + char asciiIntern = 0; + XEvent evRelease; + XEvent evPress; + + // sync the event queue, this makes key compress work better + XSync( dpy, FALSE ); + + for (;;) { + TQString textIntern; + if ( !XCheckTypedWindowEvent(dpy,event->xkey.window, + XKeyRelease,&evRelease) ) + break; + if ( !XCheckTypedWindowEvent(dpy,event->xkey.window, + XKeyPress,&evPress) ) { + XPutBackEvent(dpy, &evRelease); + break; + } + TQEvent::Type t; + translateKeyEventInternal( &evPress, countIntern, textIntern, + stateIntern, asciiIntern, codeIntern, t ); + // use stopCompression to stop key compression for the following + // key event ranges: + bool stopCompression = + // 1) misc keys + ( codeIntern >= Key_Escape && codeIntern <= Key_SysReq ) || + // 2) cursor movement + ( codeIntern >= Key_Home && codeIntern <= Key_Next ) || + // 3) extra keys + ( codeIntern >= Key_Super_L && codeIntern <= Key_Direction_R ) || + // 4) something that a) doesn't translate to text or b) translates + // to newline text + ((codeIntern == 0) && (asciiIntern == '\n')); + if (stateIntern == state && !textIntern.isEmpty() && !stopCompression) { + text += textIntern; + count += countIntern; + } else { + XPutBackEvent(dpy, &evPress); + XPutBackEvent(dpy, &evRelease); + break; + } + } + } + + if ( save != 0 ) + qt_mode_switch_remove_mask = save; + + // autorepeat compression makes sense for all widgets (Windows + // does it automatically .... ) + if ( event->type == XKeyPress && text.length() <= 1 +#ifndef QT_NO_IM + // input methods need discrete key events + && ! qic +#endif// QT_NO_IM + ) { + XEvent dummy; + + for (;;) { + auto_repeat_data.release = FALSE; + auto_repeat_data.error = FALSE; + if (! XCheckIfEvent(dpy, &dummy, &qt_keypress_scanner, + (XPointer) &auto_repeat_data)) + break; + if (! XCheckIfEvent(dpy, &dummy, &qt_keyrelease_scanner, + (XPointer) &auto_repeat_data)) + break; + + count++; + if (!text.isEmpty()) + text += text[0]; + } + } + + if (code == 0 && ascii == '\n') { + code = Key_Return; + ascii = '\r'; + text = "\r"; + } + + // try the menukey first + if ( type == TQEvent::KeyPress && code == TQt::Key_Menu ) { + TQContextMenuEvent e( TQContextMenuEvent::Keyboard, TQPoint( 5, 5 ), mapToGlobal( TQPoint( 5, 5 ) ), 0 ); + TQApplication::sendSpontaneousEvent( this, &e ); + if( e.isAccepted() ) + return TRUE; + } + + TQKeyEvent e( type, code, ascii, state, text, autor, + TQMAX(TQMAX(count,1), int(text.length())) ); + return TQApplication::sendSpontaneousEvent( this, &e ); +} + + +// +// Paint event translation +// +// When receiving many expose events, we compress them (union of all expose +// rectangles) into one event which is sent to the widget. + +struct PaintEventInfo { + Window window; +}; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static Bool isPaintOrScrollDoneEvent( Display *, XEvent *ev, XPointer a ) +{ + PaintEventInfo *info = (PaintEventInfo *)a; + if ( ev->type == Expose || ev->type == GraphicsExpose + || ev->type == ClientMessage + && ev->xclient.message_type == qt_qt_scrolldone ) + { + if ( ev->xexpose.window == info->window ) + return True; + } + return False; +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +// declared above: static TQPtrList *sip_list = 0; + +void qt_insert_sip( TQWidget* scrolled_widget, int dx, int dy ) +{ + if ( !sip_list ) { + sip_list = new TQPtrList; + sip_list->setAutoDelete( TRUE ); + } + + TQScrollInProgress* sip = new TQScrollInProgress( scrolled_widget, dx, dy ); + sip_list->append( sip ); + + XClientMessageEvent client_message; + client_message.type = ClientMessage; + client_message.window = scrolled_widget->winId(); + client_message.format = 32; + client_message.message_type = qt_qt_scrolldone; + client_message.data.l[0] = sip->id; + + XSendEvent( appDpy, scrolled_widget->winId(), False, NoEventMask, + (XEvent*)&client_message ); +} + +int qt_sip_count( TQWidget* scrolled_widget ) +{ + if ( !sip_list ) + return 0; + + int sips=0; + + for (TQScrollInProgress* sip = sip_list->first(); + sip; sip=sip_list->next()) + { + if ( sip->scrolled_widget == scrolled_widget ) + sips++; + } + + return sips; +} + +static +bool translateBySips( TQWidget* that, TQRect& paintRect ) +{ + if ( sip_list ) { + int dx=0, dy=0; + int sips=0; + for (TQScrollInProgress* sip = sip_list->first(); + sip; sip=sip_list->next()) + { + if ( sip->scrolled_widget == that ) { + if ( sips ) { + dx += sip->dx; + dy += sip->dy; + } + sips++; + } + } + if ( sips > 1 ) { + paintRect.moveBy( dx, dy ); + return TRUE; + } + } + return FALSE; +} + +bool TQETWidget::translatePaintEvent( const XEvent *event ) +{ + setWState( WState_Exposed ); + TQRect paintRect( event->xexpose.x, event->xexpose.y, + event->xexpose.width, event->xexpose.height ); + bool merging_okay = !testWFlags(WPaintClever); + XEvent xevent; + PaintEventInfo info; + info.window = winId(); + bool should_clip = translateBySips( this, paintRect ); + + TQRegion paintRegion( paintRect ); + + if ( merging_okay ) { + // WARNING: this is O(number_of_events * number_of_matching_events) + while ( XCheckIfEvent(x11Display(),&xevent,isPaintOrScrollDoneEvent, + (XPointer)&info) && + !qt_x11EventFilter(&xevent) && + !x11Event( &xevent ) ) // send event through filter + { + if ( xevent.type == Expose || xevent.type == GraphicsExpose ) { + TQRect exposure(xevent.xexpose.x, + xevent.xexpose.y, + xevent.xexpose.width, + xevent.xexpose.height); + if ( translateBySips( this, exposure ) ) + should_clip = TRUE; + paintRegion = paintRegion.unite( exposure ); + } else { + translateScrollDoneEvent( &xevent ); + } + } + } + + if ( should_clip ) { + paintRegion = paintRegion.intersect( rect() ); + if ( paintRegion.isEmpty() ) + return TRUE; + } + + TQPaintEvent e( paintRegion ); + setWState( WState_InPaintEvent ); + if ( !isTopLevel() && backgroundOrigin() != WidgetOrigin ) + erase( paintRegion ); + qt_set_paintevent_clipping( this, paintRegion ); + TQApplication::sendSpontaneousEvent( this, &e ); + qt_clear_paintevent_clipping(); + clearWState( WState_InPaintEvent ); + return TRUE; +} + +// +// Scroll-done event translation. +// + +bool TQETWidget::translateScrollDoneEvent( const XEvent *event ) +{ + if ( !sip_list ) return FALSE; + + long id = event->xclient.data.l[0]; + + // Remove any scroll-in-progress record for the given id. + for (TQScrollInProgress* sip = sip_list->first(); sip; sip=sip_list->next()) { + if ( sip->id == id ) { + sip_list->remove( sip_list->current() ); + return TRUE; + } + } + + return FALSE; +} + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif +#ifndef QT_NO_XSYNC +static Bool qt_net_wm_sync_request_scanner(Display*, XEvent* event, XPointer arg) +{ + return (event->type == ClientMessage && event->xclient.window == *(Window*)arg + && event->xclient.message_type == qt_wm_protocols + && event->xclient.data.l[ 0 ] == qt_net_wm_sync_request ); +} +#endif + +#if defined(Q_C_CALLBACKS) +} +#endif + +// +// ConfigureNotify (window move and resize) event translation + +bool TQETWidget::translateConfigEvent( const XEvent *event ) +{ + // config pending is only set on resize, see qwidget_x11.cpp, internalSetGeometry() + bool was_resize = testWState( WState_ConfigPending ); + + clearWState(WState_ConfigPending); + + if ( isTopLevel() ) { + TQPoint newCPos( geometry().topLeft() ); + TQSize newSize( event->xconfigure.width, event->xconfigure.height ); + + bool trust = (topData()->parentWinId == None || + topData()->parentWinId == TQPaintDevice::x11AppRootWindow()); + + if (event->xconfigure.send_event || trust ) { + // if a ConfigureNotify comes from a real sendevent request, we can + // trust its values. + newCPos.rx() = event->xconfigure.x + event->xconfigure.border_width; + newCPos.ry() = event->xconfigure.y + event->xconfigure.border_width; + } + + if ( isVisible() ) + TQApplication::syncX(); + + if (! extra || extra->compress_events) { + // ConfigureNotify compression for faster opaque resizing + XEvent otherEvent; + int compressed_configs = 0; + while ( XCheckTypedWindowEvent( x11Display(), winId(), ConfigureNotify, + &otherEvent ) ) { + if ( qt_x11EventFilter( &otherEvent ) ) + continue; + + if (x11Event( &otherEvent ) ) + continue; + + if ( otherEvent.xconfigure.event != otherEvent.xconfigure.window ) + continue; + + newSize.setWidth( otherEvent.xconfigure.width ); + newSize.setHeight( otherEvent.xconfigure.height ); + + if ( otherEvent.xconfigure.send_event || trust ) { + newCPos.rx() = otherEvent.xconfigure.x + + otherEvent.xconfigure.border_width; + newCPos.ry() = otherEvent.xconfigure.y + + otherEvent.xconfigure.border_width; + } + ++compressed_configs; + } +#ifndef QT_NO_XSYNC + // _NET_WM_SYNC_RETQUEST compression + Window wid = winId(); + while ( compressed_configs && + XCheckIfEvent( x11Display(), &otherEvent, + qt_net_wm_sync_request_scanner, (XPointer)&wid ) ) { + handleSyncRequest( (void*)&otherEvent ); + --compressed_configs; + } +#endif + } + + TQRect cr ( geometry() ); + if ( newSize != cr.size() ) { // size changed + was_resize = TRUE; + TQSize oldSize = size(); + cr.setSize( newSize ); + crect = cr; + + if ( isVisible() ) { + TQResizeEvent e( newSize, oldSize ); + TQApplication::sendSpontaneousEvent( this, &e ); + } else { + TQResizeEvent * e = new TQResizeEvent( newSize, oldSize ); + TQApplication::postEvent( this, e ); + } + } + + if ( newCPos != cr.topLeft() ) { // compare with cpos (exluding frame) + TQPoint oldPos = geometry().topLeft(); + cr.moveTopLeft( newCPos ); + crect = cr; + if ( isVisible() ) { + TQMoveEvent e( newCPos, oldPos ); // pos (including frame), not cpos + TQApplication::sendSpontaneousEvent( this, &e ); + } else { + TQMoveEvent * e = new TQMoveEvent( newCPos, oldPos ); + TQApplication::postEvent( this, e ); + } + } + } else { + XEvent xevent; + while ( XCheckTypedWindowEvent(x11Display(),winId(), ConfigureNotify,&xevent) && + !qt_x11EventFilter(&xevent) && + !x11Event( &xevent ) ) // send event through filter + ; + } + + bool transbg = backgroundOrigin() != WidgetOrigin; + // we ignore NorthWestGravity at the moment for reversed layout + if ( transbg || + (!testWFlags( WStaticContents ) && + testWState( WState_Exposed ) && was_resize ) || + TQApplication::reverseLayout() ) { + // remove unnecessary paint events from the queue + XEvent xevent; + while ( XCheckTypedWindowEvent( x11Display(), winId(), Expose, &xevent ) && + ! qt_x11EventFilter( &xevent ) && + ! x11Event( &xevent ) ) // send event through filter + ; + repaint( !testWFlags(WResizeNoErase) || transbg ); + } + + incrementSyncCounter(); + + return TRUE; +} + + +// +// Close window event translation. +// +bool TQETWidget::translateCloseEvent( const XEvent * ) +{ + return close(FALSE); +} + + +/*! + Sets the text cursor's flash (blink) time to \a msecs + milliseconds. The flash time is the time retquired to display, + invert and restore the caret display. Usually the text cursor is + displayed for \a msecs/2 milliseconds, then hidden for \a msecs/2 + milliseconds, but this may vary. + + Note that on Microsoft Windows, calling this function sets the + cursor flash time for all windows. + + \sa cursorFlashTime() +*/ +void TQApplication::setCursorFlashTime( int msecs ) +{ + cursor_flash_time = msecs; +} + + +/*! + Returns the text cursor's flash (blink) time in milliseconds. The + flash time is the time retquired to display, invert and restore the + caret display. + + The default value on X11 is 1000 milliseconds. On Windows, the + control panel value is used. + + Widgets should not cache this value since it may be changed at any + time by the user changing the global desktop settings. + + \sa setCursorFlashTime() +*/ +int TQApplication::cursorFlashTime() +{ + return cursor_flash_time; +} + +/*! + Sets the time limit that distinguishes a double click from two + consecutive mouse clicks to \a ms milliseconds. + + Note that on Microsoft Windows, calling this function sets the + double click interval for all windows. + + \sa doubleClickInterval() +*/ + +void TQApplication::setDoubleClickInterval( int ms ) +{ + mouse_double_click_time = ms; +} + + +/*! + Returns the maximum duration for a double click. + + The default value on X11 is 400 milliseconds. On Windows, the + control panel value is used. + + \sa setDoubleClickInterval() +*/ + +int TQApplication::doubleClickInterval() +{ + return mouse_double_click_time; +} + + +/*! + Sets the number of lines to scroll when the mouse wheel is rotated + to \a n. + + If this number exceeds the number of visible lines in a certain + widget, the widget should interpret the scroll operation as a + single page up / page down operation instead. + + \sa wheelScrollLines() +*/ +void TQApplication::setWheelScrollLines( int n ) +{ + wheel_scroll_lines = n; +} + +/*! + Returns the number of lines to scroll when the mouse wheel is + rotated. + + \sa setWheelScrollLines() +*/ +int TQApplication::wheelScrollLines() +{ + return wheel_scroll_lines; +} + +/*! + Enables the UI effect \a effect if \a enable is TRUE, otherwise + the effect will not be used. + + Note: All effects are disabled on screens running at less than + 16-bit color depth. + + \sa isEffectEnabled(), TQt::UIEffect, setDesktopSettingsAware() +*/ +void TQApplication::setEffectEnabled( TQt::UIEffect effect, bool enable ) +{ + switch (effect) { + case UI_AnimateMenu: + if ( enable ) fade_menu = FALSE; + animate_menu = enable; + break; + case UI_FadeMenu: + if ( enable ) + animate_menu = TRUE; + fade_menu = enable; + break; + case UI_AnimateCombo: + animate_combo = enable; + break; + case UI_AnimateTooltip: + if ( enable ) fade_tooltip = FALSE; + animate_tooltip = enable; + break; + case UI_FadeTooltip: + if ( enable ) + animate_tooltip = TRUE; + fade_tooltip = enable; + break; + case UI_AnimateToolBox: + animate_toolbox = enable; + break; + default: + animate_ui = enable; + break; + } +} + +/*! + Returns TRUE if \a effect is enabled; otherwise returns FALSE. + + By default, TQt will try to use the desktop settings. Call + setDesktopSettingsAware(FALSE) to prevent this. + + Note: All effects are disabled on screens running at less than + 16-bit color depth. + + \sa setEffectEnabled(), TQt::UIEffect +*/ +bool TQApplication::isEffectEnabled( TQt::UIEffect effect ) +{ + if ( TQColor::numBitPlanes() < 16 || !animate_ui ) + return FALSE; + + switch( effect ) { + case UI_AnimateMenu: + return animate_menu; + case UI_FadeMenu: + return fade_menu; + case UI_AnimateCombo: + return animate_combo; + case UI_AnimateTooltip: + return animate_tooltip; + case UI_FadeTooltip: + return fade_tooltip; + case UI_AnimateToolBox: + return animate_toolbox; + default: + return animate_ui; + } +} + +/***************************************************************************** + Session management support + *****************************************************************************/ + +#ifndef QT_NO_SM_SUPPORT + +#include + +class TQSessionManagerData +{ +public: + TQSessionManagerData( TQSessionManager* mgr, TQString& id, TQString& key ) + : sm( mgr ), sessionId( id ), sessionKey( key ) {} + TQSessionManager* sm; + TQStringList restartCommand; + TQStringList discardCommand; + TQString& sessionId; + TQString& sessionKey; + TQSessionManager::RestartHint restartHint; +}; + +class TQSmSocketReceiver : public TQObject +{ + Q_OBJECT +public: + TQSmSocketReceiver( int socket ) + : TQObject(0,0) + { + TQSocketNotifier* sn = new TQSocketNotifier( socket, TQSocketNotifier::Read, this ); + connect( sn, SIGNAL( activated(int) ), this, SLOT( socketActivated(int) ) ); + } + +public slots: + void socketActivated(int); +}; + + +static SmcConn smcConnection = 0; +static bool sm_interactionActive; +static bool sm_smActive; +static int sm_interactStyle; +static int sm_saveType; +static bool sm_cancel; +// static bool sm_waitingForPhase2; ### never used?!? +static bool sm_waitingForInteraction; +static bool sm_isshutdown; +// static bool sm_shouldbefast; ### never used?!? +static bool sm_phase2; +static bool sm_in_phase2; + +static TQSmSocketReceiver* sm_receiver = 0; + +static void resetSmState(); +static void sm_setProperty( const char* name, const char* type, + int num_vals, SmPropValue* vals); +static void sm_saveYourselfCallback( SmcConn smcConn, SmPointer clientData, + int saveType, Bool shutdown , int interactStyle, Bool fast); +static void sm_saveYourselfPhase2Callback( SmcConn smcConn, SmPointer clientData ) ; +static void sm_dieCallback( SmcConn smcConn, SmPointer clientData ) ; +static void sm_shutdownCancelledCallback( SmcConn smcConn, SmPointer clientData ); +static void sm_saveCompleteCallback( SmcConn smcConn, SmPointer clientData ); +static void sm_interactCallback( SmcConn smcConn, SmPointer clientData ); +static void sm_performSaveYourself( TQSessionManagerData* ); + +static void resetSmState() +{ +// sm_waitingForPhase2 = FALSE; ### never used?!? + sm_waitingForInteraction = FALSE; + sm_interactionActive = FALSE; + sm_interactStyle = SmInteractStyleNone; + sm_smActive = FALSE; + sm_blockUserInput = FALSE; + sm_isshutdown = FALSE; +// sm_shouldbefast = FALSE; ### never used?!? + sm_phase2 = FALSE; + sm_in_phase2 = FALSE; +} + + +// theoretically it's possible to set several properties at once. For +// simplicity, however, we do just one property at a time +static void sm_setProperty( const char* name, const char* type, + int num_vals, SmPropValue* vals) +{ + if (num_vals ) { + SmProp prop; + prop.name = (char*)name; + prop.type = (char*)type; + prop.num_vals = num_vals; + prop.vals = vals; + + SmProp* props[1]; + props[0] = ∝ + SmcSetProperties( smcConnection, 1, props ); + } + else { + char* names[1]; + names[0] = (char*) name; + SmcDeleteProperties( smcConnection, 1, names ); + } +} + +static void sm_setProperty( const TQString& name, const TQString& value) +{ + SmPropValue prop; + prop.length = value.length(); + prop.value = (SmPointer) value.latin1(); + sm_setProperty( name.latin1(), SmARRAY8, 1, &prop ); +} + +static void sm_setProperty( const TQString& name, const TQStringList& value) +{ + SmPropValue *prop = new SmPropValue[ value.count() ]; + int count = 0; + for ( TQStringList::ConstIterator it = value.begin(); it != value.end(); ++it ) { + prop[ count ].length = (*it).length(); + prop[ count ].value = (char*)(*it).latin1(); + ++count; + } + sm_setProperty( name.latin1(), SmLISTofARRAY8, count, prop ); + delete [] prop; +} + + +// workaround for broken libsm, see below +struct QT_smcConn { + unsigned int save_yourself_in_progress : 1; + unsigned int shutdown_in_progress : 1; +}; + +static void sm_saveYourselfCallback( SmcConn smcConn, SmPointer clientData, + int saveType, Bool shutdown , int interactStyle, Bool /*fast*/) +{ + if (smcConn != smcConnection ) + return; + sm_cancel = FALSE; + sm_smActive = TRUE; + sm_isshutdown = shutdown; + sm_saveType = saveType; + sm_interactStyle = interactStyle; +// sm_shouldbefast = fast; ### never used?!? + + // ugly workaround for broken libSM. libSM should do that _before_ + // actually invoking the callback in sm_process.c + ( (QT_smcConn*)smcConn )->save_yourself_in_progress = TRUE; + if ( sm_isshutdown ) + ( (QT_smcConn*)smcConn )->shutdown_in_progress = TRUE; + + sm_performSaveYourself( (TQSessionManagerData*) clientData ); + if ( !sm_isshutdown ) // we cannot expect a confirmation message in that case + resetSmState(); +} + +static void sm_performSaveYourself( TQSessionManagerData* smd ) +{ + if ( sm_isshutdown ) + sm_blockUserInput = TRUE; + + TQSessionManager* sm = smd->sm; + + // generate a new session key + timeval tv; + gettimeofday( &tv, 0 ); + smd->sessionKey = TQString::number( tv.tv_sec ) + "_" + TQString::number(tv.tv_usec); + + // tell the session manager about our program in best POSIX style + sm_setProperty( SmProgram, TQString( qApp->argv()[0] ) ); + // tell the session manager about our user as well. + struct passwd* entry = getpwuid( geteuid() ); + if ( entry ) + sm_setProperty( SmUserID, TQString::fromLatin1( entry->pw_name ) ); + + // generate a restart and discard command that makes sense + TQStringList restart; + restart << qApp->argv()[0] << "-session" << smd->sessionId + "_" + smd->sessionKey; + if (qstricmp(qAppName(), qAppClass()) != 0) + restart << "-name" << qAppName(); + sm->setRestartCommand( restart ); + TQStringList discard; + sm->setDiscardCommand( discard ); + + switch ( sm_saveType ) { + case SmSaveBoth: + qApp->commitData( *sm ); + if ( sm_isshutdown && sm_cancel) + break; // we cancelled the shutdown, no need to save state + // fall through + case SmSaveLocal: + qApp->saveState( *sm ); + break; + case SmSaveGlobal: + qApp->commitData( *sm ); + break; + default: + break; + } + + if ( sm_phase2 && !sm_in_phase2 ) { + SmcRequestSaveYourselfPhase2( smcConnection, sm_saveYourselfPhase2Callback, (SmPointer*) smd ); + sm_blockUserInput = FALSE; + } + else { + // close eventual interaction monitors and cancel the + // shutdown, if retquired. Note that we can only cancel when + // performing a shutdown, it does not work for checkpoints + if ( sm_interactionActive ) { + SmcInteractDone( smcConnection, sm_isshutdown && sm_cancel); + sm_interactionActive = FALSE; + } + else if ( sm_cancel && sm_isshutdown ) { + if ( sm->allowsErrorInteraction() ) { + SmcInteractDone( smcConnection, True ); + sm_interactionActive = FALSE; + } + } + + // set restart and discard command in session manager + sm_setProperty( SmRestartCommand, sm->restartCommand() ); + sm_setProperty( SmDiscardCommand, sm->discardCommand() ); + + // set the restart hint + SmPropValue prop; + prop.length = sizeof( int ); + int value = sm->restartHint(); + prop.value = (SmPointer) &value; + sm_setProperty( SmRestartStyleHint, SmCARD8, 1, &prop ); + + // we are done + SmcSaveYourselfDone( smcConnection, !sm_cancel ); + } +} + +static void sm_dieCallback( SmcConn smcConn, SmPointer /* clientData */) +{ + if (smcConn != smcConnection ) + return; + resetSmState(); + TQEvent tquitEvent(TQEvent::Quit); + TQApplication::sendEvent(qApp, &tquitEvent); +} + +static void sm_shutdownCancelledCallback( SmcConn smcConn, SmPointer /* clientData */) +{ + if (smcConn != smcConnection ) + return; + if ( sm_waitingForInteraction ) + qApp->exit_loop(); + resetSmState(); +} + +static void sm_saveCompleteCallback( SmcConn smcConn, SmPointer /*clientData */) +{ + if (smcConn != smcConnection ) + return; + resetSmState(); +} + +static void sm_interactCallback( SmcConn smcConn, SmPointer /* clientData */ ) +{ + if (smcConn != smcConnection ) + return; + if ( sm_waitingForInteraction ) + qApp->exit_loop(); +} + +static void sm_saveYourselfPhase2Callback( SmcConn smcConn, SmPointer clientData ) +{ + if (smcConn != smcConnection ) + return; + sm_in_phase2 = TRUE; + sm_performSaveYourself( (TQSessionManagerData*) clientData ); +} + + +void TQSmSocketReceiver::socketActivated(int) +{ + IceProcessMessages( SmcGetIceConnection( smcConnection ), 0, 0); +} + + +#undef Bool +#include "qapplication_x11.moc" + +TQSessionManager::TQSessionManager( TQApplication * app, TQString &id, TQString& key ) + : TQObject( app, "session manager" ) +{ + d = new TQSessionManagerData( this, id, key ); + d->restartHint = RestartIfRunning; + + resetSmState(); + char cerror[256]; + char* myId = 0; + char* prevId = (char*)id.latin1(); // we know what we are doing + + SmcCallbacks cb; + cb.save_yourself.callback = sm_saveYourselfCallback; + cb.save_yourself.client_data = (SmPointer) d; + cb.die.callback = sm_dieCallback; + cb.die.client_data = (SmPointer) d; + cb.save_complete.callback = sm_saveCompleteCallback; + cb.save_complete.client_data = (SmPointer) d; + cb.shutdown_cancelled.callback = sm_shutdownCancelledCallback; + cb.shutdown_cancelled.client_data = (SmPointer) d; + + // avoid showing a warning message below + const char* session_manager = getenv("SESSION_MANAGER"); + if ( !session_manager || !session_manager[0] ) + return; + + smcConnection = SmcOpenConnection( 0, 0, 1, 0, + SmcSaveYourselfProcMask | + SmcDieProcMask | + SmcSaveCompleteProcMask | + SmcShutdownCancelledProcMask, + &cb, + prevId, + &myId, + 256, cerror ); + + id = TQString::fromLatin1( myId ); + ::free( myId ); // it was allocated by C + + TQString error = cerror; + if (!smcConnection ) { + qWarning("Session management error: %s", error.latin1() ); + } + else { + sm_receiver = new TQSmSocketReceiver( IceConnectionNumber( SmcGetIceConnection( smcConnection ) ) ); + } +} + +TQSessionManager::~TQSessionManager() +{ + if ( smcConnection ) + SmcCloseConnection( smcConnection, 0, 0 ); + smcConnection = 0; + delete sm_receiver; + delete d; +} + +TQString TQSessionManager::sessionId() const +{ + return d->sessionId; +} + +TQString TQSessionManager::sessionKey() const +{ + return d->sessionKey; +} + + +void* TQSessionManager::handle() const +{ + return (void*) smcConnection; +} + + +bool TQSessionManager::allowsInteraction() +{ + if ( sm_interactionActive ) + return TRUE; + + if ( sm_waitingForInteraction ) + return FALSE; + + if ( sm_interactStyle == SmInteractStyleAny ) { + sm_waitingForInteraction = SmcInteractRequest( smcConnection, SmDialogNormal, + sm_interactCallback, (SmPointer*) this ); + } + if ( sm_waitingForInteraction ) { + qApp->enter_loop(); + sm_waitingForInteraction = FALSE; + if ( sm_smActive ) { // not cancelled + sm_interactionActive = TRUE; + sm_blockUserInput = FALSE; + return TRUE; + } + } + return FALSE; +} + +bool TQSessionManager::allowsErrorInteraction() +{ + if ( sm_interactionActive ) + return TRUE; + + if ( sm_waitingForInteraction ) + return FALSE; + + if ( sm_interactStyle == SmInteractStyleAny || sm_interactStyle == SmInteractStyleErrors ) { + sm_waitingForInteraction = SmcInteractRequest( smcConnection, SmDialogError, + sm_interactCallback, (SmPointer*) this ); + } + if ( sm_waitingForInteraction ) { + qApp->enter_loop(); + sm_waitingForInteraction = FALSE; + if ( sm_smActive ) { // not cancelled + sm_interactionActive = TRUE; + sm_blockUserInput = FALSE; + return TRUE; + } + } + return FALSE; +} + +void TQSessionManager::release() +{ + if ( sm_interactionActive ) { + SmcInteractDone( smcConnection, False ); + sm_interactionActive = FALSE; + if ( sm_smActive && sm_isshutdown ) + sm_blockUserInput = TRUE; + } +} + +void TQSessionManager::cancel() +{ + sm_cancel = TRUE; +} + +void TQSessionManager::setRestartHint( TQSessionManager::RestartHint hint) +{ + d->restartHint = hint; +} + +TQSessionManager::RestartHint TQSessionManager::restartHint() const +{ + return d->restartHint; +} + +void TQSessionManager::setRestartCommand( const TQStringList& command) +{ + d->restartCommand = command; +} + +TQStringList TQSessionManager::restartCommand() const +{ + return d->restartCommand; +} + +void TQSessionManager::setDiscardCommand( const TQStringList& command) +{ + d->discardCommand = command; +} + +TQStringList TQSessionManager::discardCommand() const +{ + return d->discardCommand; +} + +void TQSessionManager::setManagerProperty( const TQString& name, const TQString& value) +{ + SmPropValue prop; + prop.length = value.length(); + prop.value = (SmPointer) value.utf8().data(); + sm_setProperty( name.latin1(), SmARRAY8, 1, &prop ); +} + +void TQSessionManager::setManagerProperty( const TQString& name, const TQStringList& value) +{ + SmPropValue *prop = new SmPropValue[ value.count() ]; + int count = 0; + for ( TQStringList::ConstIterator it = value.begin(); it != value.end(); ++it ) { + prop[ count ].length = (*it).length(); + prop[ count ].value = (char*)(*it).utf8().data(); + ++count; + } + sm_setProperty( name.latin1(), SmLISTofARRAY8, count, prop ); + delete [] prop; +} + +bool TQSessionManager::isPhase2() const +{ + return sm_in_phase2; +} + +void TQSessionManager::requestPhase2() +{ + sm_phase2 = TRUE; +} + + +#endif // QT_NO_SM_SUPPORT diff --git a/src/kernel/qasyncimageio.cpp b/src/kernel/qasyncimageio.cpp new file mode 100644 index 000000000..1b199e94f --- /dev/null +++ b/src/kernel/qasyncimageio.cpp @@ -0,0 +1,1309 @@ +/**************************************************************************** +** +** Implementation of asynchronous image/movie loading classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qasyncimageio.h" + +#ifndef QT_NO_ASYNC_IMAGE_IO + +#include "qptrlist.h" +#include "qgif.h" +#include + +extern void qt_init_image_handlers(); +extern void qt_init_image_plugins(); + +#define Q_TRANSPARENT 0x00ffffff + +/*! + \class TQImageConsumer qasyncimageio.h + \brief The TQImageConsumer class is an abstraction used by TQImageDecoder. + + \ingroup images + \ingroup graphics + \ingroup multimedia + + The TQMovie class, or TQLabel::setMovie(), are easy to use and for + most situations do what you want with regards animated images. + + A TQImageConsumer consumes information about changes to the TQImage + maintained by a TQImageDecoder. Think of the TQImage as the model or + source of the image data, with the TQImageConsumer as a view of + that data and the TQImageDecoder being the controller that + orchestrates the relationship between the model and the view. + + You'd use the TQImageConsumer class, for example, if you were + implementing a web browser with your own image loaders. + + \sa TQImageDecoder +*/ + +/*! + \fn void TQImageConsumer::changed(const TQRect&) + + Called when the given area of the image has changed. +*/ + +/*! + \fn void TQImageConsumer::end() + + Called when all the data from all the frames has been decoded and + revealed as changed(). +*/ + +/*! + \fn void TQImageConsumer::frameDone() + + One of the two frameDone() functions will be called when a frame + of an animated image has ended and been revealed as changed(). + + When this function is called, the current image should be + displayed. + + The decoder will not make any further changes to the image until + the next call to TQImageFormat::decode(). +*/ + +/*! + \overload void TQImageConsumer::frameDone( const TQPoint& offset, const TQRect& rect ) + + One of the two frameDone() functions will be called when a frame + of an animated image has ended and been revealed as changed(). + + When this function is called, the area \a rect in the current + image should be moved by \a offset and displayed. + + The decoder will not make any further changes to the image until + the next call to TQImageFormat::decode(). +*/ + +/*! + \fn void TQImageConsumer::setLooping(int n) + + Called to indicate that the sequence of frames in the image + should be repeated \a n times, including the sequence during + decoding. + + \list + \i 0 = Forever + \i 1 = Only display frames the first time through + \i 2 = Repeat once after first pass through images + \i etc. + \endlist + + To make the TQImageDecoder do this, just delete it and pass the + information to it again for decoding (setLooping() will be called + again, of course, but that can be ignored), or keep copies of the + changed areas at the ends of frames. +*/ + +/*! + \fn void TQImageConsumer::setFramePeriod(int milliseconds) + + Notes that the frame about to be decoded should not be displayed + until the given number of \a milliseconds after the time that this + function is called. Of course, the image may not have been + decoded by then, in which case the frame should not be displayed + until it is complete. A value of -1 (the assumed default) + indicates that the image should be displayed even while it is only + partially loaded. +*/ + +/*! + \fn void TQImageConsumer::setSize(int, int) + + This function is called as soon as the size of the image has been + determined. +*/ + + +/*! + \class TQImageDecoder qasyncimageio.h + \brief The TQImageDecoder class is an incremental image decoder for all supported image formats. + + \ingroup images + \ingroup graphics + \ingroup multimedia + + New formats are installed by creating objects of class + TQImageFormatType; the TQMovie class can be used for all installed + incremental image formats. TQImageDecoder is only useful for + creating new ways of feeding data to an TQImageConsumer. + + A TQImageDecoder is a machine that decodes images. It takes encoded + image data via its decode() method and expresses its decoding by + supplying information to a TQImageConsumer. It implements its + decoding by using a TQImageFormat created by one of the + currently-existing TQImageFormatType factory objects. + + TQImageFormatType and TQImageFormat are the classes that you might + need to implement support for additional image formats. + + \legalese + + TQt supports GIF reading if it is configured that way during + installation (see qgif.h). If it is, we are retquired to state that + "The Graphics Interchange Format(c) is the Copyright property of + CompuServe Incorporated. GIF(sm) is a Service Mark property of + CompuServe Incorporated." + + \warning If you are in a country that recognizes software patents + and in which Unisys holds a patent on LZW compression and/or + decompression and you want to use GIF, Unisys may retquire you to + license that technology. Such countries include Canada, Japan, + the USA, France, Germany, Italy and the UK. + + GIF support may be removed completely in a future version of TQt. + We recommend using the MNG or PNG format. +*/ + +static const int max_header = 32; + + + + + +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 +class TQGIFFormat : public TQImageFormat { +public: + TQGIFFormat(); + virtual ~TQGIFFormat(); + + int decode(TQImage& img, TQImageConsumer* consumer, + const uchar* buffer, int length); + +private: + void fillRect(TQImage&, int x, int y, int w, int h, TQRgb col); + TQRgb color( uchar index ) const; + + // GIF specific stuff + TQRgb* globalcmap; + TQRgb* localcmap; + TQImage backingstore; + unsigned char hold[16]; + bool gif89; + int count; + int ccount; + int expectcount; + enum State { + Header, + LogicalScreenDescriptor, + GlobalColorMap, + LocalColorMap, + Introducer, + ImageDescriptor, + TableImageLZWSize, + ImageDataBlockSize, + ImageDataBlock, + ExtensionLabel, + GraphicControlExtension, + ApplicationExtension, + NetscapeExtensionBlockSize, + NetscapeExtensionBlock, + SkipBlockSize, + SkipBlock, + Done, + Error + } state; + int gncols; + int lncols; + int ncols; + int lzwsize; + bool lcmap; + int swidth, sheight; + int width, height; + int left, top, right, bottom; + enum Disposal { NoDisposal, DoNotChange, RestoreBackground, RestoreImage }; + Disposal disposal; + bool disposed; + int trans_index; + bool gcmap; + int bgcol; + int interlace; + int accum; + int bitcount; + + enum { max_lzw_bits=12 }; // (poor-compiler's static const int) + + int code_size, clear_code, end_code, max_code_size, max_code; + int firstcode, oldcode, incode; + short table[2][1<< max_lzw_bits]; + short stack[(1<<(max_lzw_bits))*2]; + short *sp; + bool needfirst; + int x, y; + int frame; + bool out_of_bounds; + bool digress; + void nextY(TQImage& img, TQImageConsumer* consumer); + void disposePrevious( TQImage& img, TQImageConsumer* consumer ); +}; + +class TQGIFFormatType : public TQImageFormatType +{ + TQImageFormat* decoderFor(const uchar* buffer, int length); + const char* formatName() const; +}; + +#endif + + +class TQImageDecoderPrivate +{ +public: + TQImageDecoderPrivate() + { + count = 0; + } + + static void cleanup(); + + static void ensureFactories() + { + if ( !factories ) { + factories = new TQPtrList; +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 + gif_decoder_factory = new TQGIFFormatType; +#endif + qt_init_image_handlers(); + qAddPostRoutine( cleanup ); + } + } + + static TQPtrList * factories; + +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 + static TQGIFFormatType * gif_decoder_factory; +#endif + + uchar header[max_header]; + int count; +}; + +TQPtrList * TQImageDecoderPrivate::factories = 0; +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 +TQGIFFormatType * TQImageDecoderPrivate::gif_decoder_factory = 0; +#endif + + +void TQImageDecoderPrivate::cleanup() +{ + delete factories; + factories = 0; +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 + delete gif_decoder_factory; + gif_decoder_factory = 0; +#endif +} + + +/*! + Constructs a TQImageDecoder that will send change information to + the TQImageConsumer \a c. +*/ +TQImageDecoder::TQImageDecoder(TQImageConsumer* c) +{ + qt_init_image_handlers(); + d = new TQImageDecoderPrivate; + Q_CHECK_PTR(d); + consumer = c; + actual_decoder = 0; +} + +/*! + Destroys a TQImageDecoder. The image it built is destroyed. The + decoder built by the factory for the file format is destroyed. The + consumer for which it decoded the image is \e not destroyed. +*/ +TQImageDecoder::~TQImageDecoder() +{ + delete d; + delete actual_decoder; +} + +/*! + \fn const TQImage& TQImageDecoder::image() + + Returns the image currently being decoded. +*/ + +static bool plugins_loaded = FALSE; + +/*! + Call this function to decode some data into image changes. The + data in \a buffer will be decoded, sending change information to + the TQImageConsumer of this TQImageDecoder until one of the change + functions of the consumer returns FALSE. The length of the data is + given in \a length. + + Returns the number of bytes consumed: 0 if consumption is + complete, and -1 if decoding fails due to invalid data. +*/ +int TQImageDecoder::decode(const uchar* buffer, int length) +{ + if (!actual_decoder) { + int i=0; + + while (i < length && d->count < max_header) + d->header[d->count++] = buffer[i++]; + + TQImageDecoderPrivate::ensureFactories(); + + for (TQImageFormatType* f = TQImageDecoderPrivate::factories->first(); + f && !actual_decoder; + f = TQImageDecoderPrivate::factories->next()) + { + actual_decoder = f->decoderFor(d->header, d->count); + } + if ( !actual_decoder && !plugins_loaded) { + qt_init_image_plugins(); + plugins_loaded = TRUE; + + for (TQImageFormatType* f = TQImageDecoderPrivate::factories->first(); + f && !actual_decoder; + f = TQImageDecoderPrivate::factories->next()) + { + actual_decoder = f->decoderFor(d->header, d->count); + } + } + + if (!actual_decoder) { + if ( d->count < max_header ) { + // not enough info yet + return i; + } else { + // failure - nothing matches max_header bytes + return -1; + } + } + } + return actual_decoder->decode(img, consumer, buffer, length); +} + +/*! + Returns a TQImageFormatType by name. This might be used when the + user needs to force data to be interpreted as being in a certain + format. \a name is one of the formats listed by + TQImageDecoder::inputFormats(). Note that you will still need to + supply decodable data to result->decoderFor() before you can begin + decoding the data. +*/ +TQImageFormatType* TQImageDecoder::format( const char* name ) +{ + TQImageDecoderPrivate::ensureFactories(); + qt_init_image_plugins(); + + for (TQImageFormatType* f = TQImageDecoderPrivate::factories->first(); + f; + f = TQImageDecoderPrivate::factories->next()) + { + if ( qstricmp(name,f->formatName())==0 ) + return f; + } + return 0; +} + +/*! + Call this function to find the name of the format of the given + header. The returned string is statically allocated. The function + will look at the first \a length characters in the \a buffer. + + Returns 0 if the format is not recognized. +*/ +const char* TQImageDecoder::formatName(const uchar* buffer, int length) +{ + TQImageDecoderPrivate::ensureFactories(); + + const char* name = 0; + for (TQImageFormatType* f = TQImageDecoderPrivate::factories->first(); + f && !name; + f = TQImageDecoderPrivate::factories->next()) + { + TQImageFormat *decoder = f->decoderFor(buffer, length); + if (decoder) { + name = f->formatName(); + delete decoder; + } + } + if ( !name && !plugins_loaded) { + qt_init_image_plugins(); + plugins_loaded = TRUE; + for (TQImageFormatType* f = TQImageDecoderPrivate::factories->first(); + f && !name; + f = TQImageDecoderPrivate::factories->next()) + { + TQImageFormat *decoder = f->decoderFor(buffer, length); + if (decoder) { + name = f->formatName(); + delete decoder; + } + } + } + + return name; +} + +/*! + Returns a sorted list of formats for which asynchronous loading is + supported. +*/ +TQStrList TQImageDecoder::inputFormats() +{ + TQImageDecoderPrivate::ensureFactories(); + qt_init_image_plugins(); + + TQStrList result; + + for (TQImageFormatType* f = TQImageDecoderPrivate::factories->first(); + f; + f = TQImageDecoderPrivate::factories->next()) + { + if ( !result.contains( f->formatName() ) ) { + result.inSort( f->formatName() ); + } + } + + return result; +} + +/*! + Registers the new TQImageFormatType \a f. This is not needed in + application code because factories call this themselves. +*/ +void TQImageDecoder::registerDecoderFactory(TQImageFormatType* f) +{ + TQImageDecoderPrivate::ensureFactories(); + + TQImageDecoderPrivate::factories->insert(0,f); +} + +/*! + Unregisters the TQImageFormatType \a f. This is not needed in + application code because factories call this themselves. +*/ +void TQImageDecoder::unregisterDecoderFactory(TQImageFormatType* f) +{ + if ( !TQImageDecoderPrivate::factories ) + return; + + TQImageDecoderPrivate::factories->remove(f); +} + +/*! + \class TQImageFormat qasyncimageio.h + \brief The TQImageFormat class is an incremental image decoder for a specific image format. + + \ingroup images + \ingroup graphics + \ingroup multimedia + + By making a derived class of TQImageFormatType, which in turn + creates objects that are a subclass of TQImageFormat, you can add + support for more incremental image formats, allowing such formats + to be sources for a TQMovie or for the first frame of the image + stream to be loaded as a TQImage or TQPixmap. + + Your new subclass must reimplement the decode() function in order + to process your new format. + + New TQImageFormat objects are generated by new TQImageFormatType + factories. +*/ + +/*! + Destroys the object. + + \internal + More importantly, destroys derived classes. +*/ +TQImageFormat::~TQImageFormat() +{ +} + +/*! + \fn int TQImageFormat::decode(TQImage& img, TQImageConsumer* consumer, const uchar* buffer, int length) + + New subclasses must reimplement this method. + + It should decode some or all of the bytes from \a buffer into \a + img, calling the methods of \a consumer as the decoding proceeds + to inform that consumer of changes to the image. The length of the + data is given in \a length. The consumer may be 0, in which case + the function should just process the data into \a img without + telling any consumer about the changes. Note that the decoder must + store enough state to be able to continue in subsequent calls to + this method - this is the essence of the incremental image + loading. + + The function should return without processing all the data if it + reaches the end of a frame in the input. + + The function must return the number of bytes it has processed. +*/ + +/*! + \class TQImageFormatType qasyncimageio.h + \brief The TQImageFormatType class is a factory that makes TQImageFormat objects. + + \ingroup images + \ingroup graphics + \ingroup multimedia + + Whereas the TQImageIO class allows for \e complete loading of + images, TQImageFormatType allows for \e incremental loading of + images. + + New image file formats are installed by creating objects of + derived classes of TQImageFormatType. They must implement + decoderFor() and formatName(). + + TQImageFormatType is a very simple class. Its only task is to + recognize image data in some format and make a new object, + subclassed from TQImageFormat, which can decode that format. + + The factories for formats built into TQt are automatically defined + before any other factory is initialized. If two factories would + recognize an image format, the factory created last will override + the earlier one; you can thus override current and future built-in + formats. +*/ + +/*! + \fn virtual TQImageFormat* TQImageFormatType::decoderFor(const uchar* buffer, int length) + + Returns a decoder for decoding an image that starts with the bytes + in \a buffer. The length of the data is given in \a length. This + function should only return a decoder if it is certain that the + decoder applies to data with the given header. Returns 0 if there + is insufficient data in the header to make a positive + identification or if the data is not recognized. +*/ + +/*! + \fn virtual const char* TQImageFormatType::formatName() const + + Returns the name of the format supported by decoders from this + factory. The string is statically allocated. +*/ + +/*! + Constructs a factory. It automatically registers itself with + TQImageDecoder. +*/ +TQImageFormatType::TQImageFormatType() +{ + TQImageDecoder::registerDecoderFactory(this); +} + +/*! + Destroys a factory. It automatically unregisters itself from + TQImageDecoder. +*/ +TQImageFormatType::~TQImageFormatType() +{ + TQImageDecoder::unregisterDecoderFactory(this); +} + + +/*! + Returns TRUE if TQt was compiled with built-in GIF reading support; + otherwise returns FALSE. +*/ +bool qt_builtin_gif_reader() +{ +#if defined(QT_BUILTIN_GIF_READER) + return QT_BUILTIN_GIF_READER == 1; +#else + return 0; +#endif +} + +// See qgif.h for important information regarding this option +#if defined(QT_BUILTIN_GIF_READER) && QT_BUILTIN_GIF_READER == 1 + +/* -- NOTDOC + \class TQGIFFormat qasyncimageio.h + \brief Incremental image decoder for GIF image format. + + \ingroup images + \ingroup graphics + + This subclass of TQImageFormat decodes GIF format images, + including animated GIFs. Internally in +*/ + +/*! + Constructs a TQGIFFormat. +*/ +TQGIFFormat::TQGIFFormat() +{ + globalcmap = 0; + localcmap = 0; + lncols = 0; + gncols = 0; + disposal = NoDisposal; + out_of_bounds = FALSE; + disposed = TRUE; + frame = -1; + state = Header; + count = 0; + lcmap = FALSE; +} + +/*! + Destroys a TQGIFFormat. +*/ +TQGIFFormat::~TQGIFFormat() +{ + if (globalcmap) delete[] globalcmap; + if ( localcmap ) delete[] localcmap; +} + + +/* -- NOTDOC + \class TQGIFFormatType qasyncimageio.h + \brief Incremental image decoder for GIF image format. + + \ingroup images + \ingroup graphics + + This subclass of TQImageFormatType recognizes GIF + format images, creating a TQGIFFormat when retquired. An instance + of this class is created automatically before any other factories, + so you should have no need for such objects. +*/ + +TQImageFormat* TQGIFFormatType::decoderFor( + const uchar* buffer, int length) +{ + if (length < 6) return 0; + if (buffer[0]=='G' + && buffer[1]=='I' + && buffer[2]=='F' + && buffer[3]=='8' + && (buffer[4]=='9' || buffer[4]=='7') + && buffer[5]=='a') + return new TQGIFFormat; + return 0; +} + +const char* TQGIFFormatType::formatName() const +{ + return "GIF"; +} + + +void TQGIFFormat::disposePrevious( TQImage& img, TQImageConsumer* consumer ) +{ + if ( out_of_bounds ) // flush anything that survived + consumer->changed(TQRect(0,0,swidth,sheight)); + + // Handle disposal of previous image before processing next one + + if ( disposed ) return; + + int l = TQMIN(swidth-1,left); + int r = TQMIN(swidth-1,right); + int t = TQMIN(sheight-1,top); + int b = TQMIN(sheight-1,bottom); + + switch (disposal) { + case NoDisposal: + break; + case DoNotChange: + break; + case RestoreBackground: + if (trans_index>=0) { + // Easy: we use the transparent color + fillRect(img, l, t, r-l+1, b-t+1, Q_TRANSPARENT); + } else if (bgcol>=0) { + // Easy: we use the bgcol given + fillRect(img, l, t, r-l+1, b-t+1, color(bgcol)); + } else { + // Impossible: We don't know of a bgcol - use pixel 0 + TQRgb** line = (TQRgb **)img.jumpTable(); + fillRect(img, l, t, r-l+1, b-t+1, line[0][0]); + } + if (consumer) + consumer->changed(TQRect(l, t, r-l+1, b-t+1)); + break; + case RestoreImage: { + if ( frame >= 0 ) { + TQRgb** line = (TQRgb **)img.jumpTable(); + for (int ln=t; ln<=b; ln++) { + memcpy(line[ln]+l, + backingstore.scanLine(ln-t), + (r-l+1)*sizeof(TQRgb) ); + } + consumer->changed(TQRect(l, t, r-l+1, b-t+1)); + } + } + } + disposal = NoDisposal; // Until an extension says otherwise. + + disposed = TRUE; +} + +/*! + This function decodes some data into image changes. + + Returns the number of bytes consumed. +*/ +int TQGIFFormat::decode(TQImage& img, TQImageConsumer* consumer, + const uchar* buffer, int length) +{ + // We are retquired to state that + // "The Graphics Interchange Format(c) is the Copyright property of + // CompuServe Incorporated. GIF(sm) is a Service Mark property of + // CompuServe Incorporated." + +#define LM(l, m) (((m)<<8)|l) + digress = FALSE; + int initial = length; + TQRgb** line = (TQRgb **)img.jumpTable(); + while (!digress && length) { + length--; + unsigned char ch=*buffer++; + switch (state) { + case Header: + hold[count++]=ch; + if (count==6) { + // Header + gif89=(hold[3]!='8' || hold[4]!='7'); + state=LogicalScreenDescriptor; + count=0; + } + break; + case LogicalScreenDescriptor: + hold[count++]=ch; + if (count==7) { + // Logical Screen Descriptor + swidth=LM(hold[0], hold[1]); + sheight=LM(hold[2], hold[3]); + gcmap=!!(hold[4]&0x80); + //UNUSED: bpchan=(((hold[4]&0x70)>>3)+1); + //UNUSED: gcmsortflag=!!(hold[4]&0x08); + gncols=2<<(hold[4]&0x7); + bgcol=(gcmap) ? hold[5] : -1; + //aspect=hold[6] ? double(hold[6]+15)/64.0 : 1.0; + + trans_index = -1; + count=0; + ncols=gncols; + if (gcmap) { + ccount=0; + state=GlobalColorMap; + globalcmap = new TQRgb[gncols+1]; // +1 for trans_index + globalcmap[gncols] = Q_TRANSPARENT; + } else { + state=Introducer; + } + } + break; + case GlobalColorMap: case LocalColorMap: + hold[count++]=ch; + if (count==3) { + TQRgb rgb = qRgb(hold[0], hold[1], hold[2]); + if ( state == LocalColorMap ) { + if ( ccount < lncols ) + localcmap[ccount] = rgb; + } else { + globalcmap[ccount] = rgb; + } + if (++ccount >= ncols) { + if ( state == LocalColorMap ) + state=TableImageLZWSize; + else + state=Introducer; + } + count=0; + } + break; + case Introducer: + hold[count++]=ch; + switch (ch) { + case ',': + state=ImageDescriptor; + break; + case '!': + state=ExtensionLabel; + break; + case ';': + if (consumer) { + if ( out_of_bounds ) // flush anything that survived + consumer->changed(TQRect(0,0,swidth,sheight)); + consumer->end(); + } + state=Done; + break; + default: + digress=TRUE; + // Unexpected Introducer - ignore block + state=Error; + } + break; + case ImageDescriptor: + hold[count++]=ch; + if (count==10) { + int newleft=LM(hold[1], hold[2]); + int newtop=LM(hold[3], hold[4]); + int newwidth=LM(hold[5], hold[6]); + int newheight=LM(hold[7], hold[8]); + + // disbelieve ridiculous logical screen sizes, + // unless the image frames are also large. + if ( swidth/10 > TQMAX(newwidth,200) ) + swidth = -1; + if ( sheight/10 > TQMAX(newheight,200) ) + sheight = -1; + + if ( swidth <= 0 ) + swidth = newleft + newwidth; + if ( sheight <= 0 ) + sheight = newtop + newheight; + + if (img.isNull()) { + img.create(swidth, sheight, 32); + memset( img.bits(), 0, img.numBytes() ); + if (consumer) consumer->setSize(swidth, sheight); + } + img.setAlphaBuffer(trans_index >= 0); + line = (TQRgb **)img.jumpTable(); + + disposePrevious( img, consumer ); + disposed = FALSE; + + left = newleft; + top = newtop; + width = newwidth; + height = newheight; + + right=TQMAX( 0, TQMIN(left+width, swidth)-1); + bottom=TQMAX(0, TQMIN(top+height, sheight)-1); + lcmap=!!(hold[9]&0x80); + interlace=!!(hold[9]&0x40); + //bool lcmsortflag=!!(hold[9]&0x20); + lncols=lcmap ? (2<<(hold[9]&0x7)) : 0; + if (lncols) { + if ( localcmap ) + delete [] localcmap; + localcmap = new TQRgb[lncols+1]; + localcmap[lncols] = Q_TRANSPARENT; + ncols = lncols; + } else { + ncols = gncols; + } + frame++; + if ( frame == 0 ) { + if ( left || top || width= 0 ) { + fillRect(img, 0, 0, swidth, sheight, color(trans_index)); + if (consumer) consumer->changed(TQRect(0,0,swidth,sheight)); + } else if ( bgcol>=0 ) { + fillRect(img, 0, 0, swidth, sheight, color(bgcol)); + if (consumer) consumer->changed(TQRect(0,0,swidth,sheight)); + } + } + } + + if ( disposal == RestoreImage ) { + int l = TQMIN(swidth-1,left); + int r = TQMIN(swidth-1,right); + int t = TQMIN(sheight-1,top); + int b = TQMIN(sheight-1,bottom); + int w = r-l+1; + int h = b-t+1; + + if (backingstore.width() < w + || backingstore.height() < h) { + // We just use the backing store as a byte array + backingstore.create( TQMAX(backingstore.width(), w), + TQMAX(backingstore.height(), h), + 32); + memset( img.bits(), 0, img.numBytes() ); + } + for (int ln=0; ln=swidth || y>=sheight; + } + break; + case TableImageLZWSize: { + lzwsize=ch; + if ( lzwsize > max_lzw_bits ) { + state=Error; + } else { + code_size=lzwsize+1; + clear_code=1<frameDone(); + digress = TRUE; + } + + state=Introducer; + } + break; + case ImageDataBlock: + count++; + accum|=(ch<=code_size && state==ImageDataBlock) { + int code=accum&((1<>=code_size; + + if (code==clear_code) { + if (!needfirst) { + int i; + code_size=lzwsize+1; + max_code_size=2*clear_code; + max_code=clear_code+2; + for (i=0; i=swidth) out_of_bounds = TRUE; + needfirst=FALSE; + if (x>=left+width) { + x=left; + out_of_bounds = left>=swidth || y>=sheight; + nextY(img,consumer); + } + } else { + incode=code; + if (code>=max_code) { + *sp++=firstcode; + code=oldcode; + } + while (code>=clear_code) { + *sp++=table[1][code]; + if (code==table[0][code]) { + state=Error; + break; + } + if (sp-stack>=(1<<(max_lzw_bits))*2) { + state=Error; + break; + } + code=table[0][code]; + } + *sp++=firstcode=table[1][code]; + code=max_code; + if (code<(1<=max_code_size) + && (max_code_size<(1<stack) { + --sp; + if (!out_of_bounds && line && *sp!=trans_index) + line[y][x] = color(*sp); + x++; + if (x>=swidth) out_of_bounds = TRUE; + if (x>=left+width) { + x=left; + out_of_bounds = left>=swidth || y>=sheight; + nextY(img,consumer); + } + } + } + } + } + if (count==expectcount) { + count=0; + state=ImageDataBlockSize; + } + break; + case ExtensionLabel: + switch (ch) { + case 0xf9: + state=GraphicControlExtension; + break; + case 0xff: + state=ApplicationExtension; + break; +#if 0 + case 0xfe: + state=CommentExtension; + break; + case 0x01: + break; +#endif + default: + state=SkipBlockSize; + } + count=0; + break; + case ApplicationExtension: + if (count<11) hold[count]=ch; + count++; + if (count==hold[0]+1) { + if (qstrncmp((char*)(hold+1), "NETSCAPE", 8)==0) { + // Looping extension + state=NetscapeExtensionBlockSize; + } else { + state=SkipBlockSize; + } + count=0; + } + break; + case NetscapeExtensionBlockSize: + expectcount=ch; + count=0; + if (expectcount) state=NetscapeExtensionBlock; + else state=Introducer; + break; + case NetscapeExtensionBlock: + if (count<3) hold[count]=ch; + count++; + if (count==expectcount) { + int loop = hold[0]+hold[1]*256; + if (consumer) consumer->setLooping(loop); + state=SkipBlockSize; // Ignore further blocks + } + break; + case GraphicControlExtension: + if (count<5) hold[count]=ch; + count++; + if (count==hold[0]+1) { + disposePrevious( img, consumer ); + disposal=Disposal((hold[1]>>2)&0x7); + //UNUSED: waitforuser=!!((hold[1]>>1)&0x1); + int delay=count>3 ? LM(hold[2], hold[3]) : 1; + // IE and mozilla use a minimum delay of 10. With the minumum delay of 10 + // we are compatible to them and avoid huge loads on the app and xserver. + if ( delay < 10 ) + delay = 10; + + bool havetrans=hold[1]&0x1; + trans_index = havetrans ? hold[4] : -1; + + if (consumer) consumer->setFramePeriod(delay*10); + count=0; + state=SkipBlockSize; + } + break; + case SkipBlockSize: + expectcount=ch; + count=0; + if (expectcount) state=SkipBlock; + else state=Introducer; + break; + case SkipBlock: + count++; + if (count==expectcount) state=SkipBlockSize; + break; + case Done: + digress=TRUE; + /* Netscape ignores the junk, so we do too. + length++; // Unget + state=Error; // More calls to this is an error + */ + break; + case Error: + return -1; // Called again after done. + } + } + return initial-length; +} + +void TQGIFFormat::fillRect(TQImage& img, int col, int row, int w, int h, TQRgb color) +{ + if (w>0) { + TQRgb** line = (TQRgb **)img.jumpTable() + row; + for (int j=0; jchanged(TQRect(left, y, right-left+1, 1)); + y++; + break; + case 1: + { + int i; + my = TQMIN(7, bottom-y); + if ( trans_index < 0 ) // Don't dup with transparency + for (i=1; i<=my; i++) + memcpy(img.scanLine(y+i)+left, img.scanLine(y)+left, + (right-left+1)*sizeof(TQRgb)); + if (consumer && !out_of_bounds) + consumer->changed(TQRect(left, y, right-left+1, my+1)); + y+=8; + if (y>bottom) { + interlace++; y=top+4; + if (y > bottom) { // for really broken GIFs with bottom < 5 + interlace=2; + y = top + 2; + if (y > bottom) { // for really broken GIF with bottom < 3 + interlace = 0; + y = top + 1; + } + } + } + } break; + case 2: + { + int i; + my = TQMIN(3, bottom-y); + if ( trans_index < 0 ) // Don't dup with transparency + for (i=1; i<=my; i++) + memcpy(img.scanLine(y+i)+left, img.scanLine(y)+left, + (right-left+1)*sizeof(TQRgb)); + if (consumer && !out_of_bounds) + consumer->changed(TQRect(left, y, right-left+1, my+1)); + y+=8; + if (y>bottom) { + interlace++; y=top+2; + if (y > bottom) { // for really broken GIF with bottom < 3 + interlace = 3; + y = top + 1; + } + } + } break; + case 3: + { + int i; + my = TQMIN(1, bottom-y); + if ( trans_index < 0 ) // Don't dup with transparency + for (i=1; i<=my; i++) + memcpy(img.scanLine(y+i)+left, img.scanLine(y)+left, + (right-left+1)*sizeof(TQRgb)); + if (consumer && !out_of_bounds) + consumer->changed(TQRect(left, y, right-left+1, my+1)); + y+=4; + if (y>bottom) { interlace++; y=top+1; } + } break; + case 4: + if (consumer && !out_of_bounds) + consumer->changed(TQRect(left, y, right-left+1, 1)); + y+=2; + } + + // Consume bogus extra lines + if (y >= sheight) out_of_bounds=TRUE; //y=bottom; +} + +TQRgb TQGIFFormat::color( uchar index ) const +{ + if ( index == trans_index || index > ncols ) + return Q_TRANSPARENT; + TQRgb *map = lcmap ? localcmap : globalcmap; + return map ? map[index] : 0; +} + + + +#endif // QT_BUILTIN_GIF_READER + +#endif // QT_NO_ASYNC_IMAGE_IO diff --git a/src/kernel/qasyncimageio.h b/src/kernel/qasyncimageio.h new file mode 100644 index 000000000..ba33177a8 --- /dev/null +++ b/src/kernel/qasyncimageio.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Definition of asynchronous image/movie loading classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQASYNCIMAGEIO_H +#define TQASYNCIMAGEIO_H + +#ifndef QT_H +#include "qimage.h" +#endif // QT_H + +#ifndef QT_NO_ASYNC_IMAGE_IO + +#if __GNUC__ - 0 > 3 +#pragma GCC system_header +#endif + +class Q_EXPORT TQImageConsumer { +public: + virtual void end()=0; + + // Change transfer type 1. + virtual void changed( const TQRect& ) = 0; + virtual void frameDone() = 0; + + // Change transfer type 2. + virtual void frameDone( const TQPoint&, const TQRect& ) = 0; + + virtual void setLooping( int ) = 0; + virtual void setFramePeriod( int ) = 0; + virtual void setSize( int, int ) = 0; +}; + +class Q_EXPORT TQImageFormat { +public: + virtual ~TQImageFormat(); + virtual int decode( TQImage& img, TQImageConsumer* consumer, + const uchar* buffer, int length ) = 0; +}; + +class Q_EXPORT TQImageFormatType { +public: + virtual ~TQImageFormatType(); + virtual TQImageFormat* decoderFor( const uchar* buffer, int length ) = 0; + virtual const char* formatName() const = 0; +protected: + TQImageFormatType(); +}; + +class TQImageDecoderPrivate; +class Q_EXPORT TQImageDecoder { +public: + TQImageDecoder( TQImageConsumer* c ); + ~TQImageDecoder(); + + const TQImage& image() { return img; } + int decode( const uchar* buffer, int length ); + + static const char* formatName( const uchar* buffer, int length ); + static TQImageFormatType* format( const char* name ); // direct use - no decode() + + static TQStrList inputFormats(); + static void registerDecoderFactory( TQImageFormatType* ); + static void unregisterDecoderFactory( TQImageFormatType* ); + +private: + TQImageFormat* actual_decoder; + TQImageConsumer* consumer; + TQImage img; + TQImageDecoderPrivate *d; +}; + +#endif // QT_NO_ASYNC_IMAGE_IO + +#endif // TQASYNCIMAGEIO_H diff --git a/src/kernel/qasyncio.cpp b/src/kernel/qasyncio.cpp new file mode 100644 index 000000000..e8c12748a --- /dev/null +++ b/src/kernel/qasyncio.cpp @@ -0,0 +1,360 @@ +/**************************************************************************** +** +** Implementation of asynchronous I/O classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qasyncio.h" +#include "qiodevice.h" +#include + +#ifndef QT_NO_ASYNC_IO + +/*! + \class TQAsyncIO qasyncio.h + \obsolete + \brief The TQAsyncIO class encapsulates I/O asynchronicity. + + The TQt classes for asynchronous input/output provide a simple + mechanism to allow large files or slow data sources to be processed + without using large amounts of memory or blocking the user interface. + + This facility is used in TQt to drive animated images. See TQImageConsumer. +*/ + + +/*! + Destroys the async IO object. +*/ +TQAsyncIO::~TQAsyncIO() +{ +} + +/*! + Ensures that only one object, \a obj and function, \a member, can + respond to changes in readiness. +*/ +void TQAsyncIO::connect(TQObject* obj, const char *member) +{ + signal.disconnect(0, 0); + signal.connect(obj, member); +} + +/*! + Derived classes should call this when they change from being + unready to ready. +*/ +void TQAsyncIO::ready() +{ + signal.activate(); +} + + + +/*! + \class TQDataSink qasyncio.h + \obsolete + \brief The TQDataSink class is an asynchronous consumer of data. + + A data sink is an object which receives data from some source in an + asynchronous manner. This means that at some time not determined by + the data sink, blocks of data are given to it from processing. The + data sink is able to limit the maximum size of such blocks which it + is currently able to process. + + \sa TQAsyncIO, TQDataSource, TQDataPump +*/ + +/*! + \fn int TQDataSink::readyToReceive() + + The data sink should return a value indicating how much data it is ready + to consume. This may be 0. +*/ + +/*! + This should be called whenever readyToReceive() might have become non-zero. + It is merely calls TQAsyncIO::ready() if readyToReceive() is non-zero. +*/ +void TQDataSink::maybeReady() +{ + if (readyToReceive()) ready(); +} + +/*! + \fn void TQDataSink::receive(const uchar*, int count) + + This function is called to provide data for the data sink. The \a count + will be no more than the amount indicated by the most recent call to + readyToReceive(). The sink must use all the provided data. +*/ + +/*! + \fn void TQDataSink::eof() + + This function will be called when no more data is available for + processing. +*/ + + +/*! + \class TQDataSource qasyncio.h + \obsolete + \brief The TQDataSource class is an asynchronous producer of data. + + A data source is an object which provides data from some source in an + asynchronous manner. This means that at some time not determined by + the data source, blocks of data will be taken from it for processing. + The data source is able to limit the maximum size of such blocks which + it is currently able to provide. + + \sa TQAsyncIO, TQDataSink, TQDataPump +*/ + +/*! + \fn int TQDataSource::readyToSend() + + The data source should return a value indicating how much data it is ready + to provide. This may be 0. If the data source knows it will never be + able to provide any more data (until after a rewind()), it may return -1. +*/ + +/*! + This should be called whenever readyToSend() might have become non-zero. + It is merely calls TQAsyncIO::ready() if readyToSend() is non-zero. +*/ +void TQDataSource::maybeReady() +{ + if (readyToSend()) ready(); +} + +/*! + \fn void TQDataSource::sendTo(TQDataSink*, int count) + + This function is called to extract data from the source, by sending + it to the given data sink. The \a count will be no more than the amount + indicated by the most recent call to readyToSend(). The source must + use all the provided data, and the sink will be prepared to accept at + least this much data. +*/ + +/*! + This function should return TRUE if the data source can be rewound. + + The default returns FALSE. +*/ +bool TQDataSource::rewindable() const +{ + return FALSE; +} + +/*! + If this function is called with \a on set to TRUE, and rewindable() + is TRUE, then the data source must take measures to allow the rewind() + function to subsequently operate as described. If rewindable() is FALSE, + the function should call TQDataSource::enableRewind(), which aborts with + a qFatal() error. + + For example, a network connection may choose to use a disk cache + of input only if rewinding is enabled before the first buffer-full of + data is discarded, returning FALSE in rewindable() if that first buffer + is discarded. +*/ +void TQDataSource::enableRewind( bool /* on */ ) +{ + qFatal( "Attempted to make unrewindable TQDataSource rewindable" ); +} + +/*! + This function rewinds the data source. This may only be called if + enableRewind(TRUE) has been previously called. +*/ +void TQDataSource::rewind() +{ + qFatal("Attempted to rewind unrewindable TQDataSource"); +} + +/*! + \class TQIODeviceSource qasyncio.h + \obsolete + \brief The TQIODeviceSource class is a TQDataSource that draws data from a TQIODevice. + + This class encapsulates retrieving data from a TQIODevice (such as a TQFile). +*/ + +/*! + Constructs a TQIODeviceSource from the TQIODevice \a device. The TQIODevice + \e must be dynamically allocated, becomes owned by the TQIODeviceSource, + and will be deleted when the TQIODeviceSource is destroyed. \a buffer_size + determines the size of buffering to use between asynchronous operations. + The higher the \a buffer_size, the more efficient, but the less interleaved + the operation will be with other processing. +*/ +TQIODeviceSource::TQIODeviceSource(TQIODevice* device, int buffer_size) : + buf_size(buffer_size), + buffer(new uchar[buf_size]), + iod(device), + rew(FALSE) +{ +} + +/*! + Destroys the TQIODeviceSource, deleting the TQIODevice from which it was + constructed. +*/ +TQIODeviceSource::~TQIODeviceSource() +{ + delete iod; + delete [] buffer; +} + +/*! + Ready until end-of-file. +*/ +int TQIODeviceSource::readyToSend() +{ + if ( iod->status() != IO_Ok || !(iod->state() & IO_Open) ) + return -1; + + int n = TQMIN((uint)buf_size, iod->size()-iod->at()); // ### not 64-bit safe + // ### not large file safe + return n ? n : -1; +} + +/*! + Reads a block of data and sends up to \a n bytes to the \a sink. +*/ +void TQIODeviceSource::sendTo(TQDataSink* sink, int n) +{ + iod->readBlock((char*)buffer, n); + sink->receive(buffer, n); +} + +/*! + All TQIODeviceSource's are rewindable. +*/ +bool TQIODeviceSource::rewindable() const +{ + return TRUE; +} + +/*! + If \a on is set to TRUE then rewinding is enabled. + No special action is taken. If \a on is set to + FALSE then rewinding is disabled. +*/ +void TQIODeviceSource::enableRewind(bool on) +{ + rew = on; +} + +/*! + Calls reset() on the TQIODevice. +*/ +void TQIODeviceSource::rewind() +{ + if (!rew) { + TQDataSource::rewind(); + } else { + iod->reset(); + ready(); + } +} + + +/*! + \class TQDataPump qasyncio.h + \obsolete + \brief The TQDataPump class moves data from a TQDataSource to a TQDataSink during event processing. + + For a TQDataSource to provide data to a TQDataSink, a controller must exist + to examine the TQDataSource::readyToSend() and TQDataSink::readyToReceive() + methods and respond to the TQASyncIO::activate() signal of the source and + sink. One very useful way to do this is interleaved with other event + processing. TQDataPump provides this - create a pipe between a source + and a sink, and data will be moved during subsequent event processing. + + Note that each source can only provide data to one sink and each sink + can only receive data from one source (although it is tquite possible + to write a multiplexing sink that is multiple sources). +*/ + +/*! + Constructs a TQDataPump to move data from a given \a data_source + to a given \a data_sink. +*/ +TQDataPump::TQDataPump(TQDataSource* data_source, TQDataSink* data_sink) : + source(data_source), sink(data_sink) +{ + source->connect(this, SLOT(kickStart())); + sink->connect(this, SLOT(kickStart())); + connect(&timer, SIGNAL(timeout()), this, SLOT(tryToPump())); + timer.start(0, TRUE); +} + +void TQDataPump::kickStart() +{ + if (!timer.isActive()) { + interval = 0; + timer.start(0, TRUE); + } +} + +void TQDataPump::tryToPump() +{ + int supply, demand; + + supply = source->readyToSend(); + demand = sink->readyToReceive(); + if (demand <= 0) { + return; + } + interval = 0; + if (supply < 0) { + // All done (until source signals change in readiness) + sink->eof(); + return; + } + if (!supply) + return; + source->sendTo(sink, TQMIN(supply, demand)); + + timer.start(0, TRUE); +} + +#endif // QT_NO_ASYNC_IO + diff --git a/src/kernel/qasyncio.h b/src/kernel/qasyncio.h new file mode 100644 index 000000000..463f1ffd7 --- /dev/null +++ b/src/kernel/qasyncio.h @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Definition of asynchronous I/O classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQASYNCIO_H +#define TQASYNCIO_H + +#ifndef QT_H +#include "qobject.h" +#include "qsignal.h" +#include "qtimer.h" +#endif // QT_H + +#ifndef QT_NO_ASYNC_IO + +class TQIODevice; + +class Q_EXPORT TQAsyncIO { +public: + virtual ~TQAsyncIO(); + void connect(TQObject*, const char *member); + +protected: + void ready(); + +private: + TQSignal signal; +}; + +class Q_EXPORT TQDataSink : public TQAsyncIO { +public: + // Call this to know how much I can take. + virtual int readyToReceive()=0; + virtual void receive(const uchar*, int count)=0; + virtual void eof()=0; + void maybeReady(); +}; + +class Q_EXPORT TQDataSource : public TQAsyncIO { +public: + virtual int readyToSend()=0; // returns -1 when never any more ready + virtual void sendTo(TQDataSink*, int count)=0; + void maybeReady(); + + virtual bool rewindable() const; + virtual void enableRewind(bool); + virtual void rewind(); +}; + +class Q_EXPORT TQIODeviceSource : public TQDataSource { + const int buf_size; + uchar *buffer; + TQIODevice* iod; + bool rew; + +public: + TQIODeviceSource(TQIODevice*, int bufsize=4096); + ~TQIODeviceSource(); + + int readyToSend(); + void sendTo(TQDataSink* sink, int n); + bool rewindable() const; + void enableRewind(bool on); + void rewind(); +}; + +class Q_EXPORT TQDataPump : public TQObject { + Q_OBJECT + int interval; + TQTimer timer; + TQDataSource* source; + TQDataSink* sink; + +public: + TQDataPump(TQDataSource*, TQDataSink*); + +private slots: + void kickStart(); + void tryToPump(); +}; + +#endif // QT_NO_ASYNC_IO + +#endif diff --git a/src/kernel/qbitmap.cpp b/src/kernel/qbitmap.cpp new file mode 100644 index 000000000..92f4b637e --- /dev/null +++ b/src/kernel/qbitmap.cpp @@ -0,0 +1,304 @@ +/**************************************************************************** +** +** Implementation of TQBitmap class +** +** Created : 941020 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qbitmap.h" +#include "qimage.h" + + +/*! + \class TQBitmap qbitmap.h + \brief The TQBitmap class provides monochrome (1-bit depth) pixmaps. + + \ingroup graphics + \ingroup images + \ingroup shared + + The TQBitmap class is a monochrome off-screen paint device used + mainly for creating custom TQCursor and TQBrush objects, in + TQPixmap::setMask() and for TQRegion. + + A TQBitmap is a TQPixmap with a \link TQPixmap::depth() depth\endlink + of 1. If a pixmap with a depth greater than 1 is assigned to a + bitmap, the bitmap will be dithered automatically. A TQBitmap is + guaranteed to always have the depth 1, unless it is + TQPixmap::isNull() which has depth 0. + + When drawing in a TQBitmap (or TQPixmap with depth 1), we recommend + using the TQColor objects \c TQt::color0 and \c TQt::color1. + Painting with \c color0 sets the bitmap bits to 0, and painting + with \c color1 sets the bits to 1. For a bitmap, 0-bits indicate + background (or transparent) and 1-bits indicate foreground (or + opaque). Using the \c black and \c white TQColor objects make no + sense because the TQColor::pixel() value is not necessarily 0 for + black and 1 for white. + + The TQBitmap can be transformed (translated, scaled, sheared or + rotated) using xForm(). + + Just like the TQPixmap class, TQBitmap is optimized by the use of + \link shclass.html implicit sharing\endlink, so it is very + efficient to pass TQBitmap objects as arguments. + + \sa TQPixmap, TQPainter::drawPixmap(), bitBlt(), \link shclass.html Shared Classes\endlink +*/ + + +/*! + Constructs a null bitmap. + + \sa TQPixmap::isNull() +*/ + +TQBitmap::TQBitmap() +{ + data->bitmap = TRUE; +} + + +/*! + Constructs a bitmap with width \a w and height \a h. + + The contents of the bitmap is uninitialized if \a clear is FALSE; + otherwise it is filled with pixel value 0 (the TQColor \c + TQt::color0). + + The optional \a optimization argument specifies the optimization + setting for the bitmap. The default optimization should be used in + most cases. Games and other pixmap-intensive applications may + benefit from setting this argument; see \l{TQPixmap::Optimization}. + + \sa TQPixmap::setOptimization(), TQPixmap::setDefaultOptimization() +*/ + +TQBitmap::TQBitmap( int w, int h, bool clear, + TQPixmap::Optimization optimization ) + : TQPixmap( w, h, 1, optimization ) +{ + data->bitmap = TRUE; + if ( clear ) + fill( TQt::color0 ); +} + + +/*! + \overload + + Constructs a bitmap with the size \a size. + + The contents of the bitmap is uninitialized if \a clear is FALSE; + otherwise it is filled with pixel value 0 (the TQColor \c + TQt::color0). + + The optional \a optimization argument specifies the optimization + setting for the bitmap. The default optimization should be used in + most cases. Games and other pixmap-intensive applications may + benefit from setting this argument; see \l{TQPixmap::Optimization}. +*/ + +TQBitmap::TQBitmap( const TQSize &size, bool clear, + TQPixmap::Optimization optimization ) + : TQPixmap( size, 1, optimization ) +{ + data->bitmap = TRUE; + if ( clear ) + fill( TQt::color0 ); +} + + +/*! + Constructs a bitmap with width \a w and height \a h and sets the + contents to \a bits. + + The \a isXbitmap flag should be TRUE if \a bits was generated by + the X11 bitmap program. The X bitmap bit order is little endian. + The TQImage documentation discusses bit order of monochrome images. + + Example (creates an arrow bitmap): + \code + uchar arrow_bits[] = { 0x3f, 0x1f, 0x0f, 0x1f, 0x3b, 0x71, 0xe0, 0xc0 }; + TQBitmap bm( 8, 8, arrow_bits, TRUE ); + \endcode +*/ + +TQBitmap::TQBitmap( int w, int h, const uchar *bits, bool isXbitmap ) + : TQPixmap( w, h, bits, isXbitmap ) +{ + data->bitmap = TRUE; +} + + +/*! + \overload + + Constructs a bitmap with the size \a size and sets the contents to + \a bits. + + The \a isXbitmap flag should be TRUE if \a bits was generated by + the X11 bitmap program. The X bitmap bit order is little endian. + The TQImage documentation discusses bit order of monochrome images. +*/ + +TQBitmap::TQBitmap( const TQSize &size, const uchar *bits, bool isXbitmap ) + : TQPixmap( size.width(), size.height(), bits, isXbitmap ) +{ + data->bitmap = TRUE; +} + + +/*! + Constructs a bitmap that is a copy of \a bitmap. +*/ + +TQBitmap::TQBitmap( const TQBitmap &bitmap ) + : TQPixmap( bitmap ) +{ +} + +#ifndef QT_NO_IMAGEIO +/*! + Constructs a bitmap from the file \a fileName. If the file does + not exist or is of an unknown format, the bitmap becomes a null + bitmap. + + The parameters \a fileName and \a format are passed on to + TQPixmap::load(). Dithering will be performed if the file format + uses more than 1 bit per pixel. + + \sa TQPixmap::isNull(), TQPixmap::load(), TQPixmap::loadFromData(), + TQPixmap::save(), TQPixmap::imageFormat() +*/ + +TQBitmap::TQBitmap( const TQString& fileName, const char *format ) + : TQPixmap() // Will set bitmap to null bitmap, explicit call for clarity +{ + data->bitmap = TRUE; + load( fileName, format, Mono ); +} +#endif + +/*! + Assigns the bitmap \a bitmap to this bitmap and returns a + reference to this bitmap. +*/ + +TQBitmap &TQBitmap::operator=( const TQBitmap &bitmap ) +{ + TQPixmap::operator=(bitmap); +#if defined(QT_CHECK_STATE) + Q_ASSERT( data->bitmap ); +#endif + return *this; +} + + +/*! + \overload + + Assigns the pixmap \a pixmap to this bitmap and returns a + reference to this bitmap. + + Dithering will be performed if the pixmap has a TQPixmap::depth() + greater than 1. +*/ + +TQBitmap &TQBitmap::operator=( const TQPixmap &pixmap ) +{ + if ( pixmap.isNull() ) { // a null pixmap + TQBitmap bm( 0, 0, FALSE, pixmap.optimization() ); + TQBitmap::operator=(bm); + } else if ( pixmap.depth() == 1 ) { // 1-bit pixmap + if ( pixmap.isTQBitmap() ) { // another TQBitmap + TQPixmap::operator=(pixmap); // shallow assignment + } else { // not a TQBitmap, but 1-bit + TQBitmap bm( pixmap.size(), FALSE, pixmap.optimization() ); + bitBlt( &bm, 0,0, &pixmap, 0,0,pixmap.width(),pixmap.height() ); + TQBitmap::operator=(bm); + } + } else { // n-bit depth pixmap + TQImage image; + image = pixmap; // convert pixmap to image + *this = image; // will dither image + } + return *this; +} + + +/*! + \overload + + Converts the image \a image to a bitmap and assigns the result to + this bitmap. Returns a reference to the bitmap. + + Dithering will be performed if the image has a TQImage::depth() + greater than 1. +*/ + +TQBitmap &TQBitmap::operator=( const TQImage &image ) +{ + convertFromImage( image ); + return *this; +} + + +#ifndef QT_NO_PIXMAP_TRANSFORMATION +/*! + Returns a transformed copy of this bitmap by using \a matrix. + + This function does exactly the same as TQPixmap::xForm(), except + that it returns a TQBitmap instead of a TQPixmap. + + \sa TQPixmap::xForm() +*/ + +TQBitmap TQBitmap::xForm( const TQWMatrix &matrix ) const +{ + TQPixmap pm = TQPixmap::xForm( matrix ); + TQBitmap bm; + // Here we fake the pixmap to think it's a TQBitmap. With this trick, + // the TQBitmap::operator=(const TQPixmap&) will just refer the + // pm.data and we do not need to perform a bitBlt. + pm.data->bitmap = TRUE; + bm = pm; + return bm; +} +#endif // QT_NO_TRANSFORMATIONS + + + diff --git a/src/kernel/qbitmap.h b/src/kernel/qbitmap.h new file mode 100644 index 000000000..8c597c9bc --- /dev/null +++ b/src/kernel/qbitmap.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Definition of TQBitmap class +** +** Created : 941020 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQBITMAP_H +#define TQBITMAP_H + +#ifndef QT_H +#include "qpixmap.h" +#endif // QT_H + + +class Q_EXPORT TQBitmap : public TQPixmap +{ +public: + TQBitmap(); + TQBitmap( int w, int h, bool clear = FALSE, + TQPixmap::Optimization = TQPixmap::DefaultOptim ); + TQBitmap( const TQSize &, bool clear = FALSE, + TQPixmap::Optimization = TQPixmap::DefaultOptim ); + TQBitmap( int w, int h, const uchar *bits, bool isXbitmap=FALSE ); + TQBitmap( const TQSize &, const uchar *bits, bool isXbitmap=FALSE ); + TQBitmap( const TQBitmap & ); +#ifndef QT_NO_IMAGEIO + TQBitmap( const TQString &fileName, const char *format=0 ); +#endif + TQBitmap &operator=( const TQBitmap & ); + TQBitmap &operator=( const TQPixmap & ); + TQBitmap &operator=( const TQImage & ); + +#ifndef QT_NO_PIXMAP_TRANSFORMATION + TQBitmap xForm( const TQWMatrix & ) const; +#endif +}; + + +#endif // TQBITMAP_H diff --git a/src/kernel/qbrush.h b/src/kernel/qbrush.h new file mode 100644 index 000000000..6e014d7fd --- /dev/null +++ b/src/kernel/qbrush.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Definition of TQBrush class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQBRUSH_H +#define TQBRUSH_H + +#ifndef QT_H +#include "qcolor.h" +#include "qshared.h" +#endif // QT_H + + +class Q_EXPORT TQBrush: public TQt +{ +friend class TQPainter; +public: + TQBrush(); + TQBrush( BrushStyle ); + TQBrush( const TQColor &, BrushStyle=SolidPattern ); + TQBrush( const TQColor &, const TQPixmap & ); + TQBrush( const TQBrush & ); + ~TQBrush(); + TQBrush &operator=( const TQBrush & ); + + BrushStyle style() const { return data->style; } + void setStyle( BrushStyle ); + const TQColor &color()const { return data->color; } + void setColor( const TQColor & ); + TQPixmap *pixmap() const { return data->pixmap; } + void setPixmap( const TQPixmap & ); + + bool operator==( const TQBrush &p ) const; + bool operator!=( const TQBrush &b ) const + { return !(operator==(b)); } + +private: + TQBrush copy() const; + void detach(); + void init( const TQColor &, BrushStyle ); + struct TQBrushData : public TQShared { // brush data + BrushStyle style; + TQColor color; + TQPixmap *pixmap; + } *data; +}; + + +/***************************************************************************** + TQBrush stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQBrush & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQBrush & ); +#endif + +#endif // TQBRUSH_H diff --git a/src/kernel/qclipboard.cpp b/src/kernel/qclipboard.cpp new file mode 100644 index 000000000..1f96edc2d --- /dev/null +++ b/src/kernel/qclipboard.cpp @@ -0,0 +1,567 @@ +/**************************************************************************** +** +** Implementation of TQClipboard class +** +** Created : 960430 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qclipboard.h" + +#ifndef QT_NO_CLIPBOARD + +#include "qapplication.h" +#include "qapplication_p.h" +#include "qdragobject.h" +#include "qpixmap.h" + +/*! + \class TQClipboard qclipboard.h + \brief The TQClipboard class provides access to the window system clipboard. + + \ingroup io + \ingroup environment + \mainclass + + The clipboard offers a simple mechanism to copy and paste data + between applications. + + TQClipboard supports the same data types that TQDragObject does, and + uses similar mechanisms. For advanced clipboard usage + read \link dnd.html the drag-and-drop documentation\endlink. + + There is a single TQClipboard object in an application, and you can + access it using TQApplication::clipboard(). + + Example: + \code + TQClipboard *cb = TQApplication::clipboard(); + + // Copy text from the clipboard (paste) + TQString text = cb->text(TQClipboard::Clipboard); + if ( !text.isNull() ) + qDebug( "The clipboard contains: " + text ); + + // Copy text into the clipboard + cb->setText( "This text can be pasted by other programs", + TQClipboard::Clipboard ); + \endcode + + TQClipboard features some convenience functions to access common data + types: setText() allows the exchange of Unicode text and + setPixmap() and setImage() allows the exchange of TQPixmaps + and TQImages between applications. The setData() function is the + ultimate in flexibility: it allows you to add any TQMimeSource into the + clipboard. There are corresponding getters for each of these, e.g. + text(), image() and pixmap(). + + You can clear the clipboard by calling clear(). + + + \section1 Platform Specific Information + + \section2 X11 + + \list + + \i The X11 Window System has the concept of a separate selection + and clipboard. When text is selected, it is immediately available + as the global mouse selection. The global mouse selection may + later be copied to the clipboard. By convention, the middle mouse + button is used to paste the global mouse selection. + + \i X11 also has the concept of ownership; if you change the + selection within a window, X11 will only notify the owner and the + previous owner of the change, i.e. it will not notify all + applications that the selection or clipboard data changed. + + \i Lastly, the X11 clipboard is event driven, i.e. the clipboard + will not function properly if the event loop is not running. + Similarly, it is recommended that the contents of the clipboard + are stored or retrieved in direct response to user-input events, + e.g. mouse button or key presses and releases. You should not + store or retrieve the clipboard contents in response to timer or + non-user-input events. + + \endlist + + \section2 Windows + + \list + + \i Microsoft Windows does not support the global mouse selection; + it only supports the global clipboard, e.g. Windows only adds text + to the clipboard when an explicit copy or cut is made. + + \i Windows does not have the concept of ownership; the clipboard + is a fully global resource so all applications are notified of + changes. + + \endlist + + See the multiclip example in the \e{TQt Designer} examples + directory for an example of a multiplatform clipboard application + that also demonstrates selection handling. +*/ + + +/*! + \internal + + Constructs a clipboard object. + + Do not call this function. + + Call TQApplication::clipboard() instead to get a pointer to the + application's global clipboard object. + + There is only one clipboard in the window system, and creating + more than one object to represent it is almost certainly an error. +*/ + +TQClipboard::TQClipboard( TQObject *parent, const char *name ) + : TQObject( parent, name ) +{ + // nothing +} + +#ifndef Q_WS_WIN32 +/*! + \internal + + Destroys the clipboard. + + You should never delete the clipboard. TQApplication will do this + when the application terminates. +*/ +TQClipboard::~TQClipboard() +{ +} +#endif + +/*! + \fn void TQClipboard::dataChanged() + + This signal is emitted when the clipboard data is changed. +*/ + +/*! + \fn void TQClipboard::selectionChanged() + + This signal is emitted when the selection is changed. This only + applies to windowing systems that support selections, e.g. X11. + Windows doesn't support selections. +*/ + +/*! \enum TQClipboard::Mode + \keyword clipboard mode + + This enum type is used to control which part of the system clipboard is + used by TQClipboard::data(), TQClipboard::setData() and related functions. + + \value Clipboard indicates that data should be stored and retrieved from + the global clipboard. + + \value Selection indicates that data should be stored and retrieved from + the global mouse selection. + + \e Note: Support for \c Selection is provided only on systems with a + global mouse selection (e.g. X11). + + \sa TQClipboard::supportsSelection() +*/ + + +/***************************************************************************** + TQApplication member functions related to TQClipboard. + *****************************************************************************/ + +#ifndef QT_NO_MIMECLIPBOARD +// text handling is done directly in qclipboard_qws, for now + +/*! + \overload + + Returns the clipboard text in subtype \a subtype, or a null string + if the clipboard does not contain any text. If \a subtype is null, + any subtype is acceptable, and \a subtype is set to the chosen + subtype. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + text is retrieved from the global clipboard. If \a mode is + TQClipboard::Selection, the text is retrieved from the global + mouse selection. + + Common values for \a subtype are "plain" and "html". + + \sa setText(), data(), TQString::operator!() +*/ +TQString TQClipboard::text( TQCString &subtype, Mode mode ) const +{ + TQString r; + TQTextDrag::decode( data( mode ) ,r, subtype ); + return r; +} + +/*! + \overload + + Returns the clipboard text in subtype \a subtype, or a null string + if the clipboard does not contain any text. This function uses the + TQClipboard::text() function which takes a TQClipboard::Mode + argument. The value of the mode argument is determined by the + return value of selectionModeEnabled(). If selectionModeEnabled() + returns TRUE, the mode argument is TQClipboard::Selection, + otherwise the mode argument is TQClipboard::Clipboard. +*/ +// ### remove 4.0 +TQString TQClipboard::text( TQCString& subtype ) const +{ + return text( subtype, selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Returns the clipboard text as plain text, or a null string if the + clipboard does not contain any text. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + text is retrieved from the global clipboard. If \a mode is + TQClipboard::Selection, the text is retrieved from the global + mouse selection. + + \sa setText(), data(), TQString::operator!() +*/ +TQString TQClipboard::text( Mode mode ) const +{ + TQCString subtype = "plain"; + return text( subtype, mode ); +} + +/*! + \overload + + This function uses the TQClipboard::text() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is + TQClipboard::Clipboard. +*/ + +TQString TQClipboard::text() const +{ + return text( selectionModeEnabled() ? Selection : Clipboard ); +} + + /*! + Copies \a text into the clipboard as plain text. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + text is stored in the global clipboard. If \a mode is + TQClipboard::Selection, the text is stored in the global + mouse selection. + + \sa text(), setData() +*/ + +void TQClipboard::setText( const TQString &text, Mode mode ) +{ + setData( new TQTextDrag(text), mode ); +} + +/*! + \overload + + This function uses the TQClipboard::setText() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is + TQClipboard::Clipboard. +*/ +// ### remove 4.0 +void TQClipboard::setText( const TQString &text ) +{ + setText( text, selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Returns the clipboard image, or returns a null image if the + clipboard does not contain an image or if it contains an image in + an unsupported image format. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + image is retrieved from the global clipboard. If \a mode is + TQClipboard::Selection, the image is retrieved from the global + mouse selection. + + \sa setImage() pixmap() data(), TQImage::isNull() +*/ +TQImage TQClipboard::image( Mode mode ) const +{ + TQImage r; + TQImageDrag::decode( data( mode ), r ); + return r; +} + +/*! + \overload + + This function uses the TQClipboard::image() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is + TQClipboard::Clipboard. +*/ +// ### remove 4.0 +TQImage TQClipboard::image() const +{ + return image( selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Copies \a image into the clipboard. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + image is stored in the global clipboard. If \a mode is + TQClipboard::Selection, the data is stored in the global + mouse selection. + + This is shorthand for: + \code + setData( new TQImageDrag(image), mode ) + \endcode + + \sa image(), setPixmap() setData() +*/ +void TQClipboard::setImage( const TQImage &image, Mode mode ) +{ + setData( new TQImageDrag( image ), mode ); +} + +/*! + \overload + + This function uses the TQClipboard::setImage() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is + TQClipboard::Clipboard. +*/ +// ### remove 4.0 +void TQClipboard::setImage( const TQImage &image ) +{ + setImage( image, selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Returns the clipboard pixmap, or null if the clipboard does not + contain a pixmap. Note that this can lose information. For + example, if the image is 24-bit and the display is 8-bit, the + result is converted to 8 bits, and if the image has an alpha + channel, the result just has a mask. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + pixmap is retrieved from the global clipboard. If \a mode is + TQClipboard::Selection, the pixmap is retrieved from the global + mouse selection. + + \sa setPixmap() image() data() TQPixmap::convertFromImage(). +*/ +TQPixmap TQClipboard::pixmap( Mode mode ) const +{ + TQPixmap r; + TQImageDrag::decode( data( mode ), r ); + return r; +} + +/*! + \overload + + This function uses the TQClipboard::pixmap() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is + TQClipboard::Clipboard. +*/ +// ### remove 4.0 +TQPixmap TQClipboard::pixmap() const +{ + return pixmap( selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! + Copies \a pixmap into the clipboard. Note that this is slower + than setImage() because it needs to convert the TQPixmap to a + TQImage first. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + pixmap is stored in the global clipboard. If \a mode is + TQClipboard::Selection, the pixmap is stored in the global + mouse selection. + + \sa pixmap() setImage() setData() +*/ +void TQClipboard::setPixmap( const TQPixmap &pixmap, Mode mode ) +{ + // *could* just use the handle, but that is X hackery, MIME is better. + setData( new TQImageDrag( pixmap.convertToImage() ), mode ); +} + +/*! + \overload + + This function uses the TQClipboard::setPixmap() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is + TQClipboard::Clipboard. +*/ +// ### remove 4.0 +void TQClipboard::setPixmap( const TQPixmap &pixmap ) +{ + setPixmap( pixmap, selectionModeEnabled() ? Selection : Clipboard ); +} + + +/*! \fn TQMimeSource *TQClipboard::data( Mode mode ) const + Returns a reference to a TQMimeSource representation of the current + clipboard data. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + data is retrieved from the global clipboard. If \a mode is + TQClipboard::Selection, the data is retrieved from the global + mouse selection. + + \sa setData() +*/ + +/*! + \overload + + This function uses the TQClipboard::data() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is + TQClipboard::Clipboard. +*/ +// ### remove 4.0 +TQMimeSource *TQClipboard::data() const +{ + return data( selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! \fn void TQClipboard::setData( TQMimeSource *src, Mode mode ) + Sets the clipboard data to \a src. Ownership of the data is + transferred to the clipboard. If you want to remove the data + either call clear() or call setData() again with new data. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, the + data is retrieved from the global clipboard. If \a mode is + TQClipboard::Selection, the data is retrieved from the global + mouse selection. + + The TQDragObject subclasses are reasonable objects to put into the + clipboard (but do not try to call TQDragObject::drag() on the same + object). Any TQDragObject placed in the clipboard should have a + parent of 0. Do not put TQDragMoveEvent or TQDropEvent subclasses in + the clipboard, as they do not belong to the event handler which + receives them. + + The setText(), setImage() and setPixmap() functions are simpler + wrappers for setting text, image and pixmap data respectively. + + \sa data() +*/ + +/*! + \overload + + This function uses the TQClipboard::setData() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is + TQClipboard::Clipboard. +*/ +// ### remove 4.0 +void TQClipboard::setData( TQMimeSource *src ) +{ + setData( src, selectionModeEnabled() ? Selection : Clipboard ); +} + +/*! \fn void TQClipboard::clear( Mode mode ) + Clear the clipboard contents. + + The \a mode argument is used to control which part of the system + clipboard is used. If \a mode is TQClipboard::Clipboard, this + function clears the the global clipboard contents. If \a mode is + TQClipboard::Selection, this function clears the global mouse + selection contents. + + \sa TQClipboard::Mode, supportsSelection() +*/ + +/*! + \overload + + This function uses the TQClipboard::clear() function which takes + a TQClipboard::Mode argument. The value of the mode argument is + determined by the return value of selectionModeEnabled(). + If selectionModeEnabled() returns TRUE, the mode argument is + TQClipboard::Selection, otherwise the mode argument is TQClipboard::Clipboard. +*/ +// ### remove 4.0 +void TQClipboard::clear() +{ + clear( selectionModeEnabled() ? Selection : Clipboard ); +} + +#endif // QT_NO_MIMECLIPBOARD +#endif // QT_NO_CLIPBOARD diff --git a/src/kernel/qclipboard.h b/src/kernel/qclipboard.h new file mode 100644 index 000000000..74449d705 --- /dev/null +++ b/src/kernel/qclipboard.h @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Definition of TQClipboard class +** +** Created : 960430 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQCLIPBOARD_H +#define TQCLIPBOARD_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_CLIPBOARD + +class TQMimeSource; + +class Q_EXPORT TQClipboard : public TQObject +{ + Q_OBJECT +private: + TQClipboard( TQObject *parent=0, const char *name=0 ); + ~TQClipboard(); + +public: + enum Mode { Clipboard, Selection }; + + void clear( Mode mode ); // ### default arg = Clipboard in 4.0 + void clear(); // ### remove 4.0 + + bool supportsSelection() const; + bool ownsSelection() const; + bool ownsClipboard() const; + + void setSelectionMode(bool enable); // ### remove 4.0 + bool selectionModeEnabled() const; // ### remove 4.0 + + // ### default arg mode = Clipboard in 4.0 for all of these + TQString text( Mode mode ) const; + TQString text( TQCString& subtype, Mode mode ) const; + void setText( const TQString &, Mode mode ); + +#ifndef QT_NO_MIMECLIPBOARD + TQMimeSource *data( Mode mode ) const; + void setData( TQMimeSource*, Mode mode ); + + TQImage image( Mode mode ) const; + TQPixmap pixmap( Mode mode ) const; + void setImage( const TQImage &, Mode mode ); + void setPixmap( const TQPixmap &, Mode mode ); +#endif + + // ### remove all of these in 4.0 + TQString text() const; + TQString text(TQCString& subtype) const; + void setText( const TQString &); + +#ifndef QT_NO_MIMECLIPBOARD + TQMimeSource *data() const; + void setData( TQMimeSource* ); + + TQImage image() const; + TQPixmap pixmap() const; + void setImage( const TQImage & ); + void setPixmap( const TQPixmap & ); +#endif + +signals: + void selectionChanged(); + void dataChanged(); + +private slots: + void ownerDestroyed(); + +protected: + void connectNotify( const char * ); + bool event( TQEvent * ); + + friend class TQApplication; + friend class TQBaseApplication; + friend class TQDragManager; + friend class TQMimeSource; + +private: +#if defined(Q_WS_MAC) + void loadScrap(bool convert); + void saveScrap(); +#endif + + // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQClipboard( const TQClipboard & ); + TQClipboard &operator=( const TQClipboard & ); +#endif +}; + +#endif // QT_NO_CLIPBOARD + +#endif // TQCLIPBOARD_H diff --git a/src/kernel/qclipboard_x11.cpp b/src/kernel/qclipboard_x11.cpp new file mode 100644 index 000000000..eb03b8f4d --- /dev/null +++ b/src/kernel/qclipboard_x11.cpp @@ -0,0 +1,1674 @@ +/**************************************************************************** +** +** Implementation of TQClipboard class for X11 +** +** Created : 960430 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +// #define TQCLIPBOARD_DEBUG +// #define TQCLIPBOARD_DEBUG_VERBOSE + +#ifdef TQCLIPBOARD_DEBUG +# define TQDEBUG qDebug +#else +# define TQDEBUG if (FALSE) qDebug +#endif + +#ifdef TQCLIPBOARD_DEBUG_VERBOSE +# define VTQDEBUG qDebug +#else +# define VTQDEBUG if (FALSE) qDebug +#endif + +#include "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +#include "qclipboard.h" + +#ifndef QT_NO_CLIPBOARD + +#include "qapplication.h" +#include "qeventloop.h" +#include "qbitmap.h" +#include "qdatetime.h" +#include "qdragobject.h" +#include "qbuffer.h" +#include "qtextcodec.h" +#include "qvaluelist.h" +#include "qmap.h" +#include "qt_x11_p.h" +#include "qapplication_p.h" + + +// REVISED: arnt + +/***************************************************************************** + Internal TQClipboard functions for X11. + *****************************************************************************/ + +// from qapplication_x11.cpp +typedef int (*QX11EventFilter) (XEvent*); +extern QX11EventFilter qt_set_x11_event_filter (QX11EventFilter filter); + +extern Time qt_x_time; // def. in qapplication_x11.cpp +extern Time qt_x_incr; // def. in qapplication_x11.cpp +extern Atom qt_xa_clipboard; +extern Atom qt_selection_property; +extern Atom qt_clipboard_sentinel; +extern Atom qt_selection_sentinel; +extern Atom qt_utf8_string; + +// from qdnd_x11.cpp +extern Atom* qt_xdnd_str_to_atom( const char *mimeType ); +extern const char* qt_xdnd_atom_to_str( Atom ); + + +static int clipboard_timeout = 5000; // 5s timeout on clipboard operations + +static TQWidget * owner = 0; +static TQWidget *requestor = 0; +static bool inSelectionMode_obsolete = FALSE; // ### remove 4.0 +static bool timer_event_clear = FALSE; +static int timer_id = 0; + +static int pending_timer_id = 0; +static bool pending_clipboard_changed = FALSE; +static bool pending_selection_changed = FALSE; + +Q_EXPORT bool qt_qclipboard_bailout_hack = false; + +// event capture mechanism for qt_xclb_wait_for_event +static bool waiting_for_data = FALSE; +static bool has_captured_event = FALSE; +static Window capture_event_win = None; +static int capture_event_type = -1; +static XEvent captured_event; + +class TQClipboardWatcher; // forward decl +static TQClipboardWatcher *selection_watcher = 0; +static TQClipboardWatcher *clipboard_watcher = 0; + +static void cleanup() +{ + delete owner; + delete requestor; + owner = 0; + requestor = 0; +} + +static +void setupOwner() +{ + if ( owner ) + return; + owner = new TQWidget( 0, "internal clipboard owner" ); + requestor = new TQWidget(0, "internal clipboard requestor"); + qAddPostRoutine( cleanup ); +} + +static +int sizeof_format(int format) +{ + int sz; + switch (format) { + default: + case 8: sz = sizeof( char); break; + case 16: sz = sizeof(short); break; + case 32: sz = sizeof( long); break; + } + return sz; +} + +class TQClipboardWatcher : public TQMimeSource { +public: + TQClipboardWatcher( TQClipboard::Mode mode ); + ~TQClipboardWatcher(); + bool empty() const; + const char* format( int n ) const; + TQByteArray encodedData( const char* fmt ) const; + TQByteArray getDataInFormat(Atom fmtatom) const; + + Atom atom; + TQValueList formatList; +}; + + + +class TQClipboardData +{ +public: + TQClipboardData(); + ~TQClipboardData(); + + void setSource(TQMimeSource* s) + { + clear(TRUE); + src = s; + } + + TQMimeSource *source() const { return src; } + + void addTransferredPixmap(TQPixmap pm) + { + /* TODO: queue them */ + transferred[tindex] = pm; + tindex=(tindex+1)%2; + } + void clearTransfers() + { + transferred[0] = TQPixmap(); + transferred[1] = TQPixmap(); + } + + void clear(bool destruct=TRUE); + + TQMimeSource *src; + Time timestamp; + + TQPixmap transferred[2]; + int tindex; +}; + +TQClipboardData::TQClipboardData() +{ + src = 0; + timestamp = CurrentTime; + tindex=0; +} + +TQClipboardData::~TQClipboardData() +{ clear(); } + +void TQClipboardData::clear(bool destruct) +{ + if(destruct) + delete src; + src = 0; + timestamp = CurrentTime; +} + + +static TQClipboardData *internalCbData = 0; +static TQClipboardData *internalSelData = 0; + +static void cleanupClipboardData() +{ + delete internalCbData; + internalCbData = 0; +} + +static TQClipboardData *clipboardData() +{ + if ( internalCbData == 0 ) { + internalCbData = new TQClipboardData; + Q_CHECK_PTR( internalCbData ); + qAddPostRoutine( cleanupClipboardData ); + } + return internalCbData; +} + +void qt_clipboard_cleanup_mime_source(TQMimeSource *src) +{ + if(internalCbData && internalCbData->source() == src) + internalCbData->clear(FALSE); +} + +static void cleanupSelectionData() +{ + delete internalSelData; + internalSelData = 0; +} + +static TQClipboardData *selectionData() +{ + if (internalSelData == 0) { + internalSelData = new TQClipboardData; + Q_CHECK_PTR(internalSelData); + qAddPostRoutine(cleanupSelectionData); + } + return internalSelData; +} + +class TQClipboardINCRTransaction +{ +public: + TQClipboardINCRTransaction(Window w, Atom p, Atom t, int f, TQByteArray d, unsigned int i); + ~TQClipboardINCRTransaction(void); + + int x11Event(XEvent *event); + + Window window; + Atom property, target; + int format; + TQByteArray data; + unsigned int increment; + unsigned int offset; +}; + +typedef TQMap TransactionMap; +static TransactionMap *transactions = 0; +static QX11EventFilter prev_x11_event_filter = 0; +static int incr_timer_id = 0; + +static int qt_xclb_transation_event_handler(XEvent *event) +{ + TransactionMap::Iterator it = transactions->find(event->xany.window); + if (it != transactions->end()) { + if ((*it)->x11Event(event) != 0) + return 1; + } + if (prev_x11_event_filter) + return prev_x11_event_filter(event); + return 0; +} + +/* + called when no INCR activity has happened for 'clipboard_timeout' + milliseconds... we assume that all unfinished transactions have + timed out and remove everything from the transaction map +*/ +static void qt_xclb_incr_timeout(void) +{ + qWarning("TQClipboard: timed out while sending data"); + + while (transactions) + delete *transactions->begin(); +} + +TQClipboardINCRTransaction::TQClipboardINCRTransaction(Window w, Atom p, Atom t, int f, + TQByteArray d, unsigned int i) + : window(w), property(p), target(t), format(f), data(d), increment(i), offset(0u) +{ + TQDEBUG("TQClipboard: sending %d bytes (INCR transaction %p)", d.size(), this); + + XSelectInput(TQPaintDevice::x11AppDisplay(), window, PropertyChangeMask); + + if (! transactions) { + VTQDEBUG("TQClipboard: created INCR transaction map"); + transactions = new TransactionMap; + prev_x11_event_filter = qt_set_x11_event_filter(qt_xclb_transation_event_handler); + + incr_timer_id = TQApplication::clipboard()->startTimer(clipboard_timeout); + } + transactions->insert(window, this); +} + +TQClipboardINCRTransaction::~TQClipboardINCRTransaction(void) +{ + VTQDEBUG("TQClipboard: destroyed INCR transacton %p", this); + + XSelectInput(TQPaintDevice::x11AppDisplay(), window, NoEventMask); + + transactions->remove(window); + if (transactions->isEmpty()) { + VTQDEBUG("TQClipboard: no more INCR transations"); + delete transactions; + transactions = 0; + (void)qt_set_x11_event_filter(prev_x11_event_filter); + + if (incr_timer_id != 0) { + TQApplication::clipboard()->killTimer(incr_timer_id); + incr_timer_id = 0; + } + } +} + +int TQClipboardINCRTransaction::x11Event(XEvent *event) +{ + if (event->type != PropertyNotify + || (event->xproperty.state != PropertyDelete + || event->xproperty.atom != property)) + return 0; + + // restart the INCR timer + if (incr_timer_id) TQApplication::clipboard()->killTimer(incr_timer_id); + incr_timer_id = TQApplication::clipboard()->startTimer(clipboard_timeout); + + unsigned int bytes_left = data.size() - offset; + if (bytes_left > 0) { + unsigned int xfer = TQMIN(increment, bytes_left); + VTQDEBUG("TQClipboard: sending %d bytes, %d remaining (INCR transaction %p)", + xfer, bytes_left - xfer, this); + + XChangeProperty(TQPaintDevice::x11AppDisplay(), window, property, target, format, + PropModeReplace, (uchar *) data.data() + offset, xfer); + offset += xfer; + } else { + // INCR transaction finished... + XChangeProperty(TQPaintDevice::x11AppDisplay(), window, property, target, format, + PropModeReplace, (uchar *) data.data(), 0); + delete this; + } + + return 1; +} + + +/***************************************************************************** + TQClipboard member functions for X11. + *****************************************************************************/ + + +void TQClipboard::clear( Mode mode ) +{ setData(0, mode); } + + +/*! + Returns TRUE if the clipboard supports mouse selection; otherwise + returns FALSE. +*/ +bool TQClipboard::supportsSelection() const +{ return TRUE; } + + +/*! + Returns TRUE if this clipboard object owns the mouse selection + data; otherwise returns FALSE. +*/ +bool TQClipboard::ownsSelection() const +{ return selectionData()->timestamp != CurrentTime; } + +/*! + Returns TRUE if this clipboard object owns the clipboard data; + otherwise returns FALSE. +*/ +bool TQClipboard::ownsClipboard() const +{ return clipboardData()->timestamp != CurrentTime; } + + +/*! \obsolete + + Use the TQClipboard::data(), TQClipboard::setData() and related functions + which take a TQClipboard::Mode argument. + + Sets the clipboard selection mode. If \a enable is TRUE, then + subsequent calls to TQClipboard::setData() and other functions + which put data into the clipboard will put the data into the mouse + selection, otherwise the data will be put into the clipboard. + + \sa supportsSelection(), selectionModeEnabled() +*/ +void TQClipboard::setSelectionMode(bool enable) +{ inSelectionMode_obsolete = enable; } + + +/*! \obsolete + + Use the TQClipboard::data(), TQClipboard::setData() and related functions + which take a TQClipboard::Mode argument. + + Returns the selection mode. + + \sa setSelectionMode(), supportsSelection() +*/ +bool TQClipboard::selectionModeEnabled() const +{ return inSelectionMode_obsolete; } + + +// event filter function... captures interesting events while +// qt_xclb_wait_for_event is running the event loop +static int qt_xclb_event_filter(XEvent *event) +{ + if (event->xany.type == capture_event_type && + event->xany.window == capture_event_win) { + VTQDEBUG( "TQClipboard: event_filter(): caught event type %d", event->type ); + has_captured_event = TRUE; + captured_event = *event; + return 1; + } + + return 0; +} + +static Bool checkForClipboardEvents(Display *, XEvent *e, XPointer) +{ + return ((e->type == SelectionRequest && (e->xselectionrequest.selection == XA_PRIMARY + || e->xselectionrequest.selection == qt_xa_clipboard)) + || (e->type == SelectionClear && (e->xselectionclear.selection == XA_PRIMARY + || e->xselectionclear.selection == qt_xa_clipboard))); +} + +static bool selection_request_pending = false; + +static Bool check_selection_request_pending( Display*, XEvent* e, XPointer ) + { + if( e->type == SelectionRequest && e->xselectionrequest.owner == owner->winId()) + selection_request_pending = true; + return False; + } + +bool qt_xclb_wait_for_event( Display *dpy, Window win, int type, XEvent *event, + int timeout ) +{ + TQTime started = TQTime::currentTime(); + TQTime now = started; + + if (qApp->eventLoop()->inherits("TQMotif")) { // yes yes, evil hack, we know + if ( waiting_for_data ) + qFatal( "TQClipboard: internal error, qt_xclb_wait_for_event recursed" ); + + waiting_for_data = TRUE; + has_captured_event = FALSE; + capture_event_win = win; + capture_event_type = type; + + QX11EventFilter old_event_filter = qt_set_x11_event_filter(qt_xclb_event_filter); + + do { + if ( XCheckTypedWindowEvent(dpy,win,type,event) ) { + waiting_for_data = FALSE; + qt_set_x11_event_filter(old_event_filter); + return TRUE; + } + + now = TQTime::currentTime(); + if ( started > now ) // crossed midnight + started = now; + + // 0x08 == ExcludeTimers for X11 only + qApp->eventLoop()->processEvents( TQEventLoop::ExcludeUserInput | + TQEventLoop::ExcludeSocketNotifiers | + TQEventLoop::WaitForMore | 0x08 ); + + if ( has_captured_event ) { + waiting_for_data = FALSE; + *event = captured_event; + qt_set_x11_event_filter(old_event_filter); + return TRUE; + } + } while ( started.msecsTo(now) < timeout ); + + waiting_for_data = FALSE; + qt_set_x11_event_filter(old_event_filter); + + return FALSE; + } + + bool flushed = FALSE; + do { + if ( XCheckTypedWindowEvent(dpy,win,type,event) ) + return TRUE; + if( qt_qclipboard_bailout_hack ) { + XEvent dummy; + selection_request_pending = false; + if ( owner != NULL ) + XCheckIfEvent(dpy,&dummy,check_selection_request_pending,NULL); + if( selection_request_pending ) + return TRUE; + } + + // process other clipboard events, since someone is probably requesting data from us + XEvent e; + if (XCheckIfEvent(dpy, &e, checkForClipboardEvents, 0)) + qApp->x11ProcessEvent(&e); + + now = TQTime::currentTime(); + if ( started > now ) // crossed midnight + started = now; + + if(!flushed) { + XFlush( dpy ); + flushed = TRUE; + } + + // sleep 50ms, so we don't use up CPU cycles all the time. + struct timeval usleep_tv; + usleep_tv.tv_sec = 0; + usleep_tv.tv_usec = 50000; + select(0, 0, 0, 0, &usleep_tv); + } while ( started.msecsTo(now) < timeout ); + + return FALSE; +} + + +static inline int maxSelectionIncr( Display *dpy ) +{ return XMaxRequestSize(dpy) > 65536 ? 65536*4 : XMaxRequestSize(dpy)*4 - 100; } + +// uglyhack: externed into qt_xdnd.cpp. qt is really not designed for +// single-platform, multi-purpose blocks of code... +bool qt_xclb_read_property( Display *dpy, Window win, Atom property, + bool deleteProperty, + TQByteArray *buffer, int *size, Atom *type, + int *format, bool nullterm ) +{ + int maxsize = maxSelectionIncr(dpy); + ulong bytes_left; // bytes_after + ulong length; // nitems + uchar *data; + Atom dummy_type; + int dummy_format; + int r; + + if ( !type ) // allow null args + type = &dummy_type; + if ( !format ) + format = &dummy_format; + + // Don't read anything, just get the size of the property data + r = XGetWindowProperty( dpy, win, property, 0, 0, False, + AnyPropertyType, type, format, + &length, &bytes_left, &data ); + if (r != Success || (type && *type == None)) { + buffer->resize( 0 ); + return FALSE; + } + XFree( (char*)data ); + + int offset = 0, buffer_offset = 0, format_inc = 1, proplen = bytes_left; + + VTQDEBUG("TQClipboard: read_property(): initial property length: %d", proplen); + + switch (*format) { + case 8: + default: + format_inc = sizeof(char) / 1; + break; + + case 16: + format_inc = sizeof(short) / 2; + proplen *= sizeof(short) / 2; + break; + + case 32: + format_inc = sizeof(long) / 4; + proplen *= sizeof(long) / 4; + break; + } + + bool ok = buffer->resize( proplen + (nullterm ? 1 : 0) ); + + VTQDEBUG("TQClipboard: read_property(): buffer resized to %d", buffer->size()); + + if ( ok ) { + // could allocate buffer + + while ( bytes_left ) { + // more to read... + + r = XGetWindowProperty( dpy, win, property, offset, maxsize/4, + False, AnyPropertyType, type, format, + &length, &bytes_left, &data ); + if (r != Success || (type && *type == None)) + break; + + offset += length / (32 / *format); + length *= format_inc * (*format) / 8; + + // Here we check if we get a buffer overflow and tries to + // recover -- this shouldn't normally happen, but it doesn't + // hurt to be defensive + if (buffer_offset + length > buffer->size()) { + length = buffer->size() - buffer_offset; + + // escape loop + bytes_left = 0; + } + + memcpy(buffer->data() + buffer_offset, data, length); + buffer_offset += length; + + XFree( (char*)data ); + } + + static Atom xa_compound_text = *qt_xdnd_str_to_atom( "COMPOUND_TEXT" ); + if ( *format == 8 && *type == xa_compound_text ) { + // convert COMPOUND_TEXT to a multibyte string + XTextProperty textprop; + textprop.encoding = *type; + textprop.format = *format; + textprop.nitems = length; + textprop.value = (unsigned char *) buffer->data(); + + char **list_ret = 0; + int count; + if ( XmbTextPropertyToTextList( dpy, &textprop, &list_ret, + &count ) == Success && + count && list_ret ) { + offset = strlen( list_ret[0] ); + buffer->resize( offset + ( nullterm ? 1 : 0 ) ); + memcpy( buffer->data(), list_ret[0], offset ); + } + if (list_ret) XFreeStringList(list_ret); + } + + // zero-terminate (for text) + if (nullterm) + buffer->at(buffer_offset) = '\0'; + } + + // correct size, not 0-term. + if ( size ) + *size = buffer_offset; + + VTQDEBUG("TQClipboard: read_property(): buffer size %d, buffer offset %d, offset %d", + buffer->size(), buffer_offset, offset); + + if ( deleteProperty ) + XDeleteProperty( dpy, win, property ); + + XFlush( dpy ); + + return ok; +} + + +// this is externed into qt_xdnd.cpp too. +TQByteArray qt_xclb_read_incremental_property( Display *dpy, Window win, + Atom property, int nbytes, + bool nullterm ) +{ + XEvent event; + + TQByteArray buf; + TQByteArray tmp_buf; + bool alloc_error = FALSE; + int length; + int offset = 0; + + if ( nbytes > 0 ) { + // Reserve buffer + zero-terminator (for text data) + // We want to complete the INCR transfer even if we cannot + // allocate more memory + alloc_error = !buf.resize(nbytes+1); + } + + for (;;) { + XFlush( dpy ); + if ( !qt_xclb_wait_for_event(dpy,win,PropertyNotify,&event,clipboard_timeout) ) + break; + if ( event.xproperty.atom != property || + event.xproperty.state != PropertyNewValue ) + continue; + if ( qt_xclb_read_property(dpy, win, property, TRUE, &tmp_buf, + &length,0, 0, FALSE) ) { + if ( length == 0 ) { // no more data, we're done + if ( nullterm ) { + buf.resize( offset+1 ); + buf.at( offset ) = '\0'; + } else { + buf.resize(offset); + } + return buf; + } else if ( !alloc_error ) { + if ( offset+length > (int)buf.size() ) { + if ( !buf.resize(offset+length+65535) ) { + alloc_error = TRUE; + length = buf.size() - offset; + } + } + memcpy( buf.data()+offset, tmp_buf.data(), length ); + tmp_buf.resize( 0 ); + offset += length; + } + } else { + break; + } + } + + // timed out ... create a new requestor window, otherwise the requestor + // could consider next request to be still part of this timed out request + delete requestor; + requestor = new TQWidget( 0, "internal clipboard requestor" ); + + return TQByteArray(); +} + +static Atom send_selection(TQClipboardData *d, Atom target, Window window, Atom property, + int format = 0, TQByteArray data = TQByteArray()); + +static Atom send_targets_selection(TQClipboardData *d, Window window, Atom property) +{ + int atoms = 0; + while (d->source()->format(atoms)) atoms++; + if (d->source()->provides("image/ppm")) atoms++; + if (d->source()->provides("image/pbm")) atoms++; + if (d->source()->provides("text/plain")) atoms+=4; + + VTQDEBUG("TQClipboard: send_targets_selection(): %d provided types", atoms); + + // for 64 bit cleanness... XChangeProperty expects long* for data with format == 32 + TQByteArray data((atoms+3) * sizeof(long)); // plus TARGETS, MULTIPLE and TIMESTAMP + long *atarget = (long *) data.data(); + + const char *fmt; + int n = 0; + while ((fmt=d->source()->format(n)) && n < atoms) + atarget[n++] = *qt_xdnd_str_to_atom(fmt); + + static Atom xa_text = *qt_xdnd_str_to_atom("TEXT"); + static Atom xa_compound_text = *qt_xdnd_str_to_atom("COMPOUND_TEXT"); + static Atom xa_targets = *qt_xdnd_str_to_atom("TARGETS"); + static Atom xa_multiple = *qt_xdnd_str_to_atom("MULTIPLE"); + static Atom xa_timestamp = *qt_xdnd_str_to_atom("TIMESTAMP"); + + if (d->source()->provides("image/ppm")) + atarget[n++] = XA_PIXMAP; + if (d->source()->provides("image/pbm")) + atarget[n++] = XA_BITMAP; + if (d->source()->provides("text/plain")) { + atarget[n++] = qt_utf8_string; + atarget[n++] = xa_text; + atarget[n++] = xa_compound_text; + atarget[n++] = XA_STRING; + } + + atarget[n++] = xa_targets; + atarget[n++] = xa_multiple; + atarget[n++] = xa_timestamp; + +#if defined(TQCLIPBOARD_DEBUG_VERBOSE) + for (int index = 0; index < n; index++) { + VTQDEBUG(" atom %d: 0x%lx (%s)", index, atarget[index], + qt_xdnd_atom_to_str(atarget[index])); + } +#endif + + XChangeProperty(TQPaintDevice::x11AppDisplay(), window, property, XA_ATOM, 32, + PropModeReplace, (uchar *) data.data(), n); + return property; +} + +static Atom send_string_selection(TQClipboardData *d, Atom target, Window window, Atom property) +{ + static Atom xa_text = *qt_xdnd_str_to_atom("TEXT"); + static Atom xa_compound_text = *qt_xdnd_str_to_atom("COMPOUND_TEXT"); + + TQDEBUG("TQClipboard: send_string_selection():\n" + " property type %lx\n" + " property name '%s'", + target, qt_xdnd_atom_to_str(target)); + + if (target == xa_text || target == xa_compound_text) { + // the ICCCM states that TEXT and COMPOUND_TEXT are in the + // encoding of choice, so we choose the encoding of the locale + TQByteArray data = d->source()->encodedData("text/plain"); + if(data.resize(data.size() + 1)) + data[int(data.size() - 1)] = '\0'; + char *list[] = { data.data(), NULL }; + + XICCEncodingStyle style = + (target == xa_compound_text) ? XCompoundTextStyle : XStdICCTextStyle; + XTextProperty textprop; + if (list[0] != NULL + && XmbTextListToTextProperty(TQPaintDevice::x11AppDisplay(), + list, 1, style, &textprop) == Success) { + TQDEBUG(" textprop type %lx\n" + " textprop name '%s'\n" + " format %d\n" + " %ld items", + textprop.encoding, qt_xdnd_atom_to_str(textprop.encoding), + textprop.format, textprop.nitems); + + int sz = sizeof_format(textprop.format); + data.duplicate((const char *) textprop.value, textprop.nitems * sz); + XFree(textprop.value); + + return send_selection(d, textprop.encoding, window, property, textprop.format, data); + } + + return None; + } + + Atom xtarget = None; + const char *fmt = 0; + if (target == XA_STRING + || (target == xa_text && TQTextCodec::codecForLocale()->mibEnum() == 4)) { + // the ICCCM states that STRING is latin1 plus newline and tab + // see section 2.6.2 + fmt = "text/plain;charset=ISO-8859-1"; + xtarget = XA_STRING; + } else if (target == qt_utf8_string) { + // proposed UTF8_STRING conversion type + fmt = "text/plain;charset=UTF-8"; + xtarget = qt_utf8_string; + } + + if (xtarget == None) // should not happen + return None; + + TQByteArray data = d->source()->encodedData(fmt); + + TQDEBUG(" format 8\n %d bytes", data.size()); + + return send_selection(d, xtarget, window, property, 8, data); +} + +static Atom send_pixmap_selection(TQClipboardData *d, Atom target, Window window, Atom property) +{ + TQPixmap pm; + + if ( target == XA_PIXMAP ) { + TQByteArray data = d->source()->encodedData("image/ppm"); + pm.loadFromData(data); + } else if ( target == XA_BITMAP ) { + TQByteArray data = d->source()->encodedData("image/pbm"); + TQImage img; + img.loadFromData(data); + if ( img.depth() != 1 ) + img = img.convertDepth(1); + } + + if (pm.isNull()) // should never happen + return None; + + Pixmap handle = pm.handle(); + XChangeProperty(TQPaintDevice::x11AppDisplay(), window, property, + target, 32, PropModeReplace, (uchar *) &handle, 1); + d->addTransferredPixmap(pm); + return property; + +} + +static Atom send_selection(TQClipboardData *d, Atom target, Window window, Atom property, + int format, TQByteArray data) +{ + if (format == 0) format = 8; + + if (data.isEmpty()) { + const char *fmt = qt_xdnd_atom_to_str(target); + TQDEBUG("TQClipboard: send_selection(): converting to type '%s'", fmt); + if (fmt && !d->source()->provides(fmt)) // Not a MIME type we can produce + return None; + data = d->source()->encodedData(fmt); + } + + TQDEBUG("TQClipboard: send_selection():\n" + " property type %lx\n" + " property name '%s'\n" + " format %d\n" + " %d bytes", + target, qt_xdnd_atom_to_str(target), format, data.size()); + + // don't allow INCR transfers when using MULTIPLE or to + // Motif clients (since Motif doesn't support INCR) + static Atom motif_clip_temporary = *qt_xdnd_str_to_atom("CLIP_TEMPORARY"); + bool allow_incr = property != motif_clip_temporary; + + // X_ChangeProperty protocol request is 24 bytes + const unsigned int increment = (XMaxRequestSize(TQPaintDevice::x11AppDisplay()) * 4) - 24; + if (data.size() > increment && allow_incr) { + long bytes = data.size(); + XChangeProperty(TQPaintDevice::x11AppDisplay(), window, property, + qt_x_incr, 32, PropModeReplace, (uchar *) &bytes, 1); + + (void)new TQClipboardINCRTransaction(window, property, target, format, data, increment); + return qt_x_incr; + } + + // make sure we can perform the XChangeProperty in a single request + if (data.size() > increment) + return None; // ### perhaps use several XChangeProperty calls w/ PropModeAppend? + + // use a single request to transfer data + XChangeProperty(TQPaintDevice::x11AppDisplay(), window, property, target, + format, PropModeReplace, (uchar *) data.data(), + data.size() / sizeof_format(format)); + return property; +} + +/*! \internal + Internal cleanup for Windows. +*/ +void TQClipboard::ownerDestroyed() +{ } + + +/*! \internal + Internal optimization for Windows. +*/ +void TQClipboard::connectNotify( const char * ) +{ } + + +/*! \reimp + */ +bool TQClipboard::event( TQEvent *e ) +{ + if ( e->type() == TQEvent::Timer ) { + TQTimerEvent *te = (TQTimerEvent *) e; + + if ( waiting_for_data ) // should never happen + return FALSE; + + if (te->timerId() == timer_id) { + killTimer(timer_id); + timer_id = 0; + + timer_event_clear = TRUE; + if ( selection_watcher ) // clear selection + selectionData()->clear(); + if ( clipboard_watcher ) // clear clipboard + clipboardData()->clear(); + timer_event_clear = FALSE; + + return TRUE; + } else if ( te->timerId() == pending_timer_id ) { + // I hate klipper + killTimer( pending_timer_id ); + pending_timer_id = 0; + + if ( pending_clipboard_changed ) { + pending_clipboard_changed = FALSE; + clipboardData()->clear(); + emit dataChanged(); + } + if ( pending_selection_changed ) { + pending_selection_changed = FALSE; + selectionData()->clear(); + emit selectionChanged(); + } + + return TRUE; + } else if (te->timerId() == incr_timer_id) { + killTimer(incr_timer_id); + incr_timer_id = 0; + + qt_xclb_incr_timeout(); + + return TRUE; + } else { + return TQObject::event( e ); + } + } else if ( e->type() != TQEvent::Clipboard ) { + return TQObject::event( e ); + } + + XEvent *xevent = (XEvent *)(((TQCustomEvent *)e)->data()); + Display *dpy = TQPaintDevice::x11AppDisplay(); + + if ( !xevent ) + return TRUE; + + switch ( xevent->type ) { + + case SelectionClear: + // new selection owner + if (xevent->xselectionclear.selection == XA_PRIMARY) { + TQClipboardData *d = selectionData(); + + // ignore the event if it was generated before we gained selection ownership + if (d->timestamp != CurrentTime && xevent->xselectionclear.time < d->timestamp) + break; + + TQDEBUG("TQClipboard: new selection owner 0x%lx at time %lx (ours %lx)", + XGetSelectionOwner(dpy, XA_PRIMARY), + xevent->xselectionclear.time, d->timestamp); + + if ( ! waiting_for_data ) { + d->clear(); + emit selectionChanged(); + } else { + pending_selection_changed = TRUE; + if ( ! pending_timer_id ) + pending_timer_id = TQApplication::clipboard()->startTimer( 0 ); + } + } else if (xevent->xselectionclear.selection == qt_xa_clipboard) { + TQClipboardData *d = clipboardData(); + + // ignore the event if it was generated before we gained selection ownership + if (d->timestamp != CurrentTime && xevent->xselectionclear.time < d->timestamp) + break; + + TQDEBUG("TQClipboard: new clipboard owner 0x%lx at time %lx (%lx)", + XGetSelectionOwner(dpy, qt_xa_clipboard), + xevent->xselectionclear.time, d->timestamp); + + if ( ! waiting_for_data ) { + d->clear(); + emit dataChanged(); + } else { + pending_clipboard_changed = TRUE; + if ( ! pending_timer_id ) + pending_timer_id = TQApplication::clipboard()->startTimer( 0 ); + } + } else { +#ifdef QT_CHECK_STATE + qWarning("TQClipboard: Unknown SelectionClear event received."); +#endif + return FALSE; + } + break; + + case SelectionNotify: + /* + Something has delivered data to us, but this was not caught + by TQClipboardWatcher::getDataInFormat() + + Just skip the event to prevent Bad Things (tm) from + happening later on... + */ + break; + + case SelectionRequest: + { + // someone wants our data + XSelectionRequestEvent *req = &xevent->xselectionrequest; + + if (requestor && req->requestor == requestor->winId()) + break; + + XEvent event; + event.xselection.type = SelectionNotify; + event.xselection.display = req->display; + event.xselection.requestor = req->requestor; + event.xselection.selection = req->selection; + event.xselection.target = req->target; + event.xselection.property = None; + event.xselection.time = req->time; + + TQDEBUG("TQClipboard: SelectionRequest from %lx\n" + " selection 0x%lx (%s) target 0x%lx (%s)", + req->requestor, + req->selection, + qt_xdnd_atom_to_str(req->selection), + req->target, + qt_xdnd_atom_to_str(req->target)); + + TQClipboardData *d; + + if ( req->selection == XA_PRIMARY ) { + d = selectionData(); + } else if ( req->selection == qt_xa_clipboard ) { + d = clipboardData(); + } else { +#ifdef QT_CHECK_RANGE + qWarning("TQClipboard: unknown selection '%lx'", req->selection); +#endif // QT_CHECK_RANGE + + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + break; + } + + if (! d->source()) { +#ifdef QT_CHECK_STATE + qWarning("TQClipboard: cannot transfer data, no data available"); +#endif // QT_CHECK_STATE + + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + break; + } + + TQDEBUG("TQClipboard: SelectionRequest at time %lx (ours %lx)", + req->time, d->timestamp); + + if (d->timestamp == CurrentTime // we don't own the selection anymore + || (req->time != CurrentTime && req->time < d->timestamp)) { + TQDEBUG("TQClipboard: SelectionRequest too old"); + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + break; + } + + static Atom xa_text = *qt_xdnd_str_to_atom("TEXT"); + static Atom xa_compound_text = *qt_xdnd_str_to_atom("COMPOUND_TEXT"); + static Atom xa_targets = *qt_xdnd_str_to_atom("TARGETS"); + static Atom xa_multiple = *qt_xdnd_str_to_atom("MULTIPLE"); + static Atom xa_timestamp = *qt_xdnd_str_to_atom("TIMESTAMP"); + + struct AtomPair { Atom target; Atom property; } *multi = 0; + Atom multi_type = None; + int multi_format = 0; + int nmulti = 0; + int imulti = -1; + bool multi_writeback = FALSE; + + if ( req->target == xa_multiple ) { + TQByteArray multi_data; + if (req->property == None + || !qt_xclb_read_property(dpy, req->requestor, req->property, + FALSE, &multi_data, 0, &multi_type, &multi_format, 0) + || multi_format != 32) { + // MULTIPLE property not formatted correctly + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + break; + } + nmulti = multi_data.size()/sizeof(*multi); + multi = new AtomPair[nmulti]; + memcpy(multi,multi_data.data(),multi_data.size()); + imulti = 0; + } + + for (; imulti < nmulti; ++imulti) { + Atom target; + Atom property; + + if ( multi ) { + target = multi[imulti].target; + property = multi[imulti].property; + } else { + target = req->target; + property = req->property; + if (property == None) // obsolete client + property = target; + } + + Atom ret = None; + if (target == None || property == None) { + ; + } else if (target == xa_timestamp) { + if (d->timestamp != CurrentTime) { + XChangeProperty(dpy, req->requestor, property, xa_timestamp, 32, + PropModeReplace, (uchar *) &d->timestamp, 1); + ret = property; + } else { + +#ifdef QT_CHECK_STATE + qWarning("TQClipboard: invalid data timestamp"); +#endif // QT_CHECK_STATE + } + } else if (target == xa_targets) { + ret = send_targets_selection(d, req->requestor, property); + } else if (target == XA_STRING + || target == xa_text + || target == xa_compound_text + || target == qt_utf8_string) { + ret = send_string_selection(d, target, req->requestor, property); + } else if (target == XA_PIXMAP + || target == XA_BITMAP) { + ret = send_pixmap_selection(d, target, req->requestor, property); + } else { + ret = send_selection(d, target, req->requestor, property); + } + + if (nmulti > 0) { + if (ret == None) { + multi[imulti].property = None; + multi_writeback = TRUE; + } + } else { + event.xselection.property = ret; + break; + } + } + + if (nmulti > 0) { + if (multi_writeback) { + // according to ICCCM 2.6.2 says to put None back + // into the original property on the requestor window + XChangeProperty(dpy, req->requestor, req->property, multi_type, 32, + PropModeReplace, (uchar *) multi, nmulti * 2); + } + + delete [] multi; + event.xselection.property = req->property; + } + + // send selection notify to requestor + XSendEvent(dpy, req->requestor, False, NoEventMask, &event); + + TQDEBUG("TQClipboard: SelectionNotify to 0x%lx\n" + " property 0x%lx (%s)", + req->requestor, event.xselection.property, + qt_xdnd_atom_to_str(event.xselection.property)); + } + break; + } + + return TRUE; +} + + + + + + +TQClipboardWatcher::TQClipboardWatcher( TQClipboard::Mode mode ) +{ + switch ( mode ) { + case TQClipboard::Selection: + atom = XA_PRIMARY; + break; + + case TQClipboard::Clipboard: + atom = qt_xa_clipboard; + break; + +#ifdef QT_CHECK_RANGE + default: + qWarning( "TQClipboardWatcher: internal error, unknown clipboard mode" ); + break; +#endif // QT_CHECK_RANGE + } + + setupOwner(); +} + +TQClipboardWatcher::~TQClipboardWatcher() +{ + if( selection_watcher == this ) + selection_watcher = 0; + if( clipboard_watcher == this ) + clipboard_watcher = 0; +} + +bool TQClipboardWatcher::empty() const +{ + Display *dpy = TQPaintDevice::x11AppDisplay(); + Window win = XGetSelectionOwner( dpy, atom ); + +#ifdef QT_CHECK_STATE + if( win == requestor->winId()) { + qWarning( "TQClipboardWatcher::empty: internal error, app owns the selection" ); + return TRUE; + } +#endif // QT_CHECK_STATE + + return win == None; +} + +const char* TQClipboardWatcher::format( int n ) const +{ + if ( empty() ) + return 0; + + if (! formatList.count()) { + // get the list of targets from the current clipboard owner - we do this + // once so that multiple calls to this function don't retquire multiple + // server round trips... + static Atom xa_targets = *qt_xdnd_str_to_atom( "TARGETS" ); + + TQClipboardWatcher *that = (TQClipboardWatcher *) this; + TQByteArray ba = getDataInFormat(xa_targets); + if (ba.size() > 0) { + Atom *unsorted_target = (Atom *) ba.data(); + static Atom xa_text = *qt_xdnd_str_to_atom( "TEXT" ); + static Atom xa_compound_text = *qt_xdnd_str_to_atom( "COMPOUND_TEXT" ); + int i, size = ba.size() / sizeof(Atom); + + // sort TARGETS to prefer some types over others. some apps + // will report XA_STRING before COMPOUND_TEXT, and we want the + // latter, not the former (if it is present). + Atom* target = new Atom[size+4]; + memset( target, 0, (size+4) * sizeof(Atom) ); + + for ( i = 0; i < size; ++i ) { + if ( unsorted_target[i] == qt_utf8_string ) + target[0] = unsorted_target[i]; + else if ( unsorted_target[i] == xa_compound_text ) + target[1] = unsorted_target[i]; + else if ( unsorted_target[i] == xa_text ) + target[2] = unsorted_target[i]; + else if ( unsorted_target[i] == XA_STRING ) + target[3] = unsorted_target[i]; + else + target[i + 4] = unsorted_target[i]; + } + + for (i = 0; i < size + 4; ++i) { + if ( target[i] == 0 ) continue; + + VTQDEBUG(" format: %s", qt_xdnd_atom_to_str(target[i])); + + if ( target[i] == XA_PIXMAP ) + that->formatList.append("image/ppm"); + else if ( target[i] == XA_STRING ) + that->formatList.append( "text/plain;charset=ISO-8859-1" ); + else if ( target[i] == qt_utf8_string ) + that->formatList.append( "text/plain;charset=UTF-8" ); + else if ( target[i] == xa_text || + target[i] == xa_compound_text ) + that->formatList.append( "text/plain" ); + else + that->formatList.append(qt_xdnd_atom_to_str(target[i])); + } + delete []target; + + TQDEBUG("TQClipboardWatcher::format: %d formats available", + int(that->formatList.count())); + } + } + + if (n >= 0 && n < (signed) formatList.count()) + return formatList[n]; + if (n == 0) + return "text/plain"; + return 0; +} + +TQByteArray TQClipboardWatcher::encodedData( const char* fmt ) const +{ + if ( !fmt || empty() ) + return TQByteArray( 0 ); + + TQDEBUG("TQClipboardWatcher::encodedData: fetching format '%s'", fmt); + + Atom fmtatom = 0; + + if ( 0==qstricmp(fmt,"text/plain;charset=iso-8859-1") ) { + // ICCCM section 2.6.2 says STRING is latin1 text + fmtatom = XA_STRING; + } else if ( 0==qstricmp(fmt,"text/plain;charset=utf-8") ) { + // proprosed UTF8_STRING conversion type + fmtatom = *qt_xdnd_str_to_atom( "UTF8_STRING" ); + } else if ( 0==qstrcmp(fmt,"text/plain") ) { + fmtatom = *qt_xdnd_str_to_atom( "COMPOUND_TEXT" ); + } else if ( 0==qstrcmp(fmt,"image/ppm") ) { + fmtatom = XA_PIXMAP; + TQByteArray pmd = getDataInFormat(fmtatom); + if ( pmd.size() == sizeof(Pixmap) ) { + Pixmap xpm = *((Pixmap*)pmd.data()); + Display *dpy = TQPaintDevice::x11AppDisplay(); + Window r; + int x,y; + uint w,h,bw,d; + if ( ! xpm ) + return TQByteArray( 0 ); + XGetGeometry(dpy,xpm, &r,&x,&y,&w,&h,&bw,&d); + TQImageIO iio; + GC gc = XCreateGC( dpy, xpm, 0, 0 ); + if ( d == 1 ) { + TQBitmap qbm(w,h); + XCopyArea(dpy,xpm,qbm.handle(),gc,0,0,w,h,0,0); + iio.setFormat("PBMRAW"); + iio.setImage(qbm.convertToImage()); + } else { + TQPixmap qpm(w,h); + XCopyArea(dpy,xpm,qpm.handle(),gc,0,0,w,h,0,0); + iio.setFormat("PPMRAW"); + iio.setImage(qpm.convertToImage()); + } + XFreeGC(dpy,gc); + TQBuffer buf; + buf.open(IO_WriteOnly); + iio.setIODevice(&buf); + iio.write(); + return buf.buffer(); + } else { + fmtatom = *qt_xdnd_str_to_atom(fmt); + } + } else { + fmtatom = *qt_xdnd_str_to_atom(fmt); + } + return getDataInFormat(fmtatom); +} + +TQByteArray TQClipboardWatcher::getDataInFormat(Atom fmtatom) const +{ + TQByteArray buf; + + Display *dpy = TQPaintDevice::x11AppDisplay(); + Window win = requestor->winId(); + + TQDEBUG("TQClipboardWatcher::getDataInFormat: selection '%s' format '%s'", + qt_xdnd_atom_to_str(atom), qt_xdnd_atom_to_str(fmtatom)); + + XSelectInput(dpy, win, NoEventMask); // don't listen for any events + + XDeleteProperty(dpy, win, qt_selection_property); + XConvertSelection(dpy, atom, fmtatom, qt_selection_property, win, qt_x_time); + XSync(dpy, FALSE); + + VTQDEBUG("TQClipboardWatcher::getDataInFormat: waiting for SelectionNotify event"); + + XEvent xevent; + if ( !qt_xclb_wait_for_event(dpy,win,SelectionNotify,&xevent,clipboard_timeout) || + xevent.xselection.property == None ) { + TQDEBUG("TQClipboardWatcher::getDataInFormat: format not available"); + return buf; + } + + VTQDEBUG("TQClipboardWatcher::getDataInFormat: fetching data..."); + + Atom type; + XSelectInput(dpy, win, PropertyChangeMask); + + if ( qt_xclb_read_property(dpy,win,qt_selection_property,TRUE, + &buf,0,&type,0,FALSE) ) { + if ( type == qt_x_incr ) { + int nbytes = buf.size() >= 4 ? *((int*)buf.data()) : 0; + buf = qt_xclb_read_incremental_property( dpy, win, + qt_selection_property, + nbytes, FALSE ); + } + } + + XSelectInput(dpy, win, NoEventMask); + + TQDEBUG("TQClipboardWatcher::getDataInFormat: %d bytes received", buf.size()); + + return buf; +} + + +TQMimeSource* TQClipboard::data( Mode mode ) const +{ + TQClipboardData *d; + switch ( mode ) { + case Selection: d = selectionData(); break; + case Clipboard: d = clipboardData(); break; + + default: +#ifdef QT_CHECK_RANGE + qWarning( "TQClipboard::data: invalid mode '%d'", mode ); +#endif // QT_CHECK_RANGE + return 0; + } + + if ( ! d->source() && ! timer_event_clear ) { + if ( mode == Selection ) { + if ( ! selection_watcher ) + selection_watcher = new TQClipboardWatcher( mode ); + d->setSource( selection_watcher ); + } else { + if ( ! clipboard_watcher ) + clipboard_watcher = new TQClipboardWatcher( mode ); + d->setSource( clipboard_watcher ); + } + + if (! timer_id) { + // start a zero timer - we will clear cached data when the timer + // times out, which will be the next time we hit the event loop... + // that way, the data is cached long enough for calls within a single + // loop/function, but the data doesn't linger around in case the selection + // changes + TQClipboard *that = ((TQClipboard *) this); + timer_id = that->startTimer(0); + } + } + + return d->source(); +} + + +void TQClipboard::setData( TQMimeSource* src, Mode mode ) +{ + Atom atom, sentinel_atom; + TQClipboardData *d; + switch ( mode ) { + case Selection: + atom = XA_PRIMARY; + sentinel_atom = qt_selection_sentinel; + d = selectionData(); + break; + + case Clipboard: + atom = qt_xa_clipboard; + sentinel_atom = qt_clipboard_sentinel; + d = clipboardData(); + break; + + default: +#ifdef QT_CHECK_RANGE + qWarning( "TQClipboard::data: invalid mode '%d'", mode ); +#endif // QT_CHECK_RANGE + return; + } + + Display *dpy = TQPaintDevice::x11AppDisplay(); + Window newOwner; + + if (! src) { // no data, clear clipboard contents + newOwner = None; + d->clear(); + } else { + setupOwner(); + + newOwner = owner->winId(); + + d->setSource(src); + d->timestamp = qt_x_time; + } + + Window prevOwner = XGetSelectionOwner( dpy, atom ); + // use qt_x_time, since d->timestamp == CurrentTime when clearing + XSetSelectionOwner(dpy, atom, newOwner, qt_x_time); + + if ( mode == Selection ) + emit selectionChanged(); + else + emit dataChanged(); + + if (XGetSelectionOwner(dpy, atom) != newOwner) { +#ifdef QT_CHECK_STATE + qWarning("TQClipboard::setData: Cannot set X11 selection owner for %s", + qt_xdnd_atom_to_str(atom)); +#endif // QT_CHECK_STATE + + d->clear(); + return; + } + + // Signal to other TQt processes that the selection has changed + Window owners[2]; + owners[0] = newOwner; + owners[1] = prevOwner; + XChangeProperty( dpy, TQApplication::desktop()->screen(0)->winId(), + sentinel_atom, XA_WINDOW, 32, PropModeReplace, + (unsigned char*)&owners, 2 ); +} + + +/* + Called by the main event loop in qapplication_x11.cpp when the + _QT_SELECTION_SENTINEL property has been changed (i.e. when some TQt + process has performed TQClipboard::setData(). If it returns TRUE, the + TQClipBoard dataChanged() signal should be emitted. +*/ + +bool qt_check_selection_sentinel() +{ + bool doIt = TRUE; + if ( owner ) { + /* + Since the X selection mechanism cannot give any signal when + the selection has changed, we emulate it (for TQt processes) here. + The notification should be ignored in case of either + a) This is the process that did setData (because setData() + then has already emitted dataChanged()) + b) This is the process that owned the selection when dataChanged() + was called (because we have then received a SelectionClear event, + and have already emitted dataChanged() as a result of that) + */ + + Window* owners; + Atom actualType; + int actualFormat; + ulong nitems; + ulong bytesLeft; + + if (XGetWindowProperty(TQPaintDevice::x11AppDisplay(), + TQApplication::desktop()->screen(0)->winId(), + qt_selection_sentinel, 0, 2, False, XA_WINDOW, + &actualType, &actualFormat, &nitems, + &bytesLeft, (unsigned char**)&owners) == Success) { + if ( actualType == XA_WINDOW && actualFormat == 32 && nitems == 2 ) { + Window win = owner->winId(); + if ( owners[0] == win || owners[1] == win ) + doIt = FALSE; + } + + XFree( owners ); + } + } + + if (doIt) { + if ( waiting_for_data ) { + pending_selection_changed = TRUE; + if ( ! pending_timer_id ) + pending_timer_id = TQApplication::clipboard()->startTimer( 0 ); + doIt = FALSE; + } else { + selectionData()->clear(); + } + } + + return doIt; +} + + +bool qt_check_clipboard_sentinel() +{ + bool doIt = TRUE; + if (owner) { + Window *owners; + Atom actualType; + int actualFormat; + unsigned long nitems, bytesLeft; + + if (XGetWindowProperty(TQPaintDevice::x11AppDisplay(), + TQApplication::desktop()->screen(0)->winId(), + qt_clipboard_sentinel, 0, 2, False, XA_WINDOW, + &actualType, &actualFormat, &nitems, &bytesLeft, + (unsigned char **) &owners) == Success) { + if (actualType == XA_WINDOW && actualFormat == 32 && nitems == 2) { + Window win = owner->winId(); + if (owners[0] == win || owners[1] == win) + doIt = FALSE; + } + + XFree(owners); + } + } + + if (doIt) { + if ( waiting_for_data ) { + pending_clipboard_changed = TRUE; + if ( ! pending_timer_id ) + pending_timer_id = TQApplication::clipboard()->startTimer( 0 ); + doIt = FALSE; + } else { + clipboardData()->clear(); + } + } + + return doIt; +} + +#endif // QT_NO_CLIPBOARD diff --git a/src/kernel/qcolor.cpp b/src/kernel/qcolor.cpp new file mode 100644 index 000000000..eec43cd5e --- /dev/null +++ b/src/kernel/qcolor.cpp @@ -0,0 +1,1030 @@ +/**************************************************************************** +** +** Implementation of TQColor class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qcolor.h" +#include "qnamespace.h" +#include "qdatastream.h" + +#include + + +/*! + \class TQColor qcolor.h + \brief The TQColor class provides colors based on RGB or HSV values. + + \ingroup images + \ingroup graphics + \ingroup appearance + + A color is normally specified in terms of RGB (red, green and blue) + components, but it is also possible to specify HSV (hue, saturation + and value) or set a color name (the names are copied from from the + X11 color database). + + In addition to the RGB value, a TQColor also has a pixel value and a + validity. The pixel value is used by the underlying window system + to refer to a color. It can be thought of as an index into the + display hardware's color table. + + The validity (isValid()) indicates whether the color is legal at + all. For example, a RGB color with RGB values out of range is + illegal. For performance reasons, TQColor mostly disregards illegal + colors. The result of using an invalid color is unspecified and + will usually be surprising. + + There are 19 predefined TQColor objects: \c white, \c black, \c + red, \c darkRed, \c green, \c darkGreen, \c blue, \c darkBlue, \c + cyan, \c darkCyan, \c magenta, \c darkMagenta, \c yellow, \c + darkYellow, \c gray, \c darkGray, \c lightGray, \c color0 and \c + color1, accessible as members of the TQt namespace (ie. \c TQt::red). + + \img qt-colors.png TQt Colors + + The colors \c color0 (zero pixel value) and \c color1 (non-zero + pixel value) are special colors for drawing in \link TQBitmap + bitmaps\endlink. Painting with \c color0 sets the bitmap bits to 0 + (transparent, i.e. background), and painting with \c color1 sets the + bits to 1 (opaque, i.e. foreground). + + The TQColor class has an efficient, dynamic color allocation + strategy. A color is normally allocated the first time it is used + (lazy allocation), that is, whenever the pixel() function is called. + The following steps are taken to allocate a color. If, at any point, + a suitable color is found then the appropriate pixel value is + returned and the subsequent steps are not taken: + + \list 1 + \i Is the pixel value valid? If it is, just return it; otherwise, + allocate a pixel value. + \i Check an internal hash table to see if we allocated an equal RGB + value earlier. If we did, set the corresponding pixel value for the + color and return it. + \i Try to allocate the RGB value. If we succeed, we get a pixel value + that we save in the internal table with the RGB value. + Return the pixel value. + \i The color could not be allocated. Find the closest matching + color, save it in the internal table, and return it. + \endlist + + A color can be set by passing setNamedColor() an RGB string like + "#112233", or a color name, e.g. "blue". The names are taken from + X11's rgb.txt database but can also be used under Windows. To get + a lighter or darker color use light() and dark() respectively. + Colors can also be set using setRgb() and setHsv(). The color + components can be accessed in one go with rgb() and hsv(), or + individually with red(), green() and blue(). + + Use maxColors() and numBitPlanes() to determine the maximum number + of colors and the number of bit planes supported by the underlying + window system, + + If you need to allocate many colors temporarily, for example in an + image viewer application, enterAllocContext(), leaveAllocContext() and + destroyAllocContext() will prove useful. + + \section1 HSV Colors + + Because many people don't know the HSV color model very well, we'll + cover it briefly here. + + The RGB model is hardware-oriented. Its representation is close to + what most monitors show. In contrast, HSV represents color in a way + more suited to the human perception of color. For example, the + relationships "stronger than", "darker than" and "the opposite of" + are easily expressed in HSV but are much harder to express in RGB. + + HSV, like RGB, has three components: + + \list + + \i H, for hue, is either 0-359 if the color is chromatic (not + gray), or meaningless if it is gray. It represents degrees on the + color wheel familiar to most people. Red is 0 (degrees), green is + 120 and blue is 240. + + \i S, for saturation, is 0-255, and the bigger it is, the + stronger the color is. Grayish colors have saturation near 0; very + strong colors have saturation near 255. + + \i V, for value, is 0-255 and represents lightness or brightness + of the color. 0 is black; 255 is as far from black as possible. + + \endlist + + Here are some examples: Pure red is H=0, S=255, V=255. A dark red, + moving slightly towards the magenta, could be H=350 (equivalent to + -10), S=255, V=180. A grayish light red could have H about 0 (say + 350-359 or 0-10), S about 50-100, and S=255. + + TQt returns a hue value of -1 for achromatic colors. If you pass a + too-big hue value, TQt forces it into range. Hue 360 or 720 is + treated as 0; hue 540 is treated as 180. + + \sa TQPalette, TQColorGroup, TQApplication::setColorSpec(), + \link http://www.poynton.com/ColorFAQ.html Color FAQ\endlink +*/ + +/***************************************************************************** + Global colors + *****************************************************************************/ + +#if defined(Q_WS_WIN) +#define COLOR0_PIX 0x00ffffff +#define COLOR1_PIX 0 +#else +#define COLOR0_PIX 0 +#define COLOR1_PIX 1 +#endif + +#if (defined(Q_CC_GNU) && defined(Q_OS_WIN)) +// workaround - bug in mingw +static TQColor stdcol[19] = { + TQColor( 255, 255, 255 ), + TQColor( 0, 0, 0 ), + TQColor( 0, 0, 0 ), + TQColor( 255, 255, 255 ), + TQColor( 128, 128, 128 ), + TQColor( 160, 160, 164 ), + TQColor( 192, 192, 192 ), + TQColor( 255, 0, 0 ), + TQColor( 0, 255, 0 ), + TQColor( 0, 0, 255 ), + TQColor( 0, 255, 255 ), + TQColor( 255, 0, 255 ), + TQColor( 255, 255, 0 ), + TQColor( 128, 0, 0 ), + TQColor( 0, 128, 0 ), + TQColor( 0, 0, 128 ), + TQColor( 0, 128, 128 ), + TQColor( 128, 0, 128 ), + TQColor( 128, 128, 0 ) }; +#else + static TQColor stdcol[19]; +#endif + +QT_STATIC_CONST_IMPL TQColor & TQt::color0 = stdcol[0]; +QT_STATIC_CONST_IMPL TQColor & TQt::color1 = stdcol[1]; +QT_STATIC_CONST_IMPL TQColor & TQt::black = stdcol[2]; +QT_STATIC_CONST_IMPL TQColor & TQt::white = stdcol[3]; +QT_STATIC_CONST_IMPL TQColor & TQt::darkGray = stdcol[4]; +QT_STATIC_CONST_IMPL TQColor & TQt::gray = stdcol[5]; +QT_STATIC_CONST_IMPL TQColor & TQt::lightGray = stdcol[6]; +QT_STATIC_CONST_IMPL TQColor & TQt::red = stdcol[7]; +QT_STATIC_CONST_IMPL TQColor & TQt::green = stdcol[8]; +QT_STATIC_CONST_IMPL TQColor & TQt::blue = stdcol[9]; +QT_STATIC_CONST_IMPL TQColor & TQt::cyan = stdcol[10]; +QT_STATIC_CONST_IMPL TQColor & TQt::magenta = stdcol[11]; +QT_STATIC_CONST_IMPL TQColor & TQt::yellow = stdcol[12]; +QT_STATIC_CONST_IMPL TQColor & TQt::darkRed = stdcol[13]; +QT_STATIC_CONST_IMPL TQColor & TQt::darkGreen = stdcol[14]; +QT_STATIC_CONST_IMPL TQColor & TQt::darkBlue = stdcol[15]; +QT_STATIC_CONST_IMPL TQColor & TQt::darkCyan = stdcol[16]; +QT_STATIC_CONST_IMPL TQColor & TQt::darkMagenta = stdcol[17]; +QT_STATIC_CONST_IMPL TQColor & TQt::darkYellow = stdcol[18]; + + +/***************************************************************************** + TQColor member functions + *****************************************************************************/ + +bool TQColor::color_init = FALSE; // color system not initialized +bool TQColor::globals_init = FALSE; // global color not initialized +TQColor::ColorModel TQColor::colormodel = d32; + + +TQColor* TQColor::globalColors() +{ + return stdcol; +} + + +/*! + Initializes the global colors. This function is called if a global + color variable is initialized before the constructors for our + global color objects are executed. Without this mechanism, + assigning a color might assign an uninitialized value. + + Example: + \code + TQColor myColor = red; // will initialize red etc. + + int main( int argc, char **argc ) + { + } + \endcode +*/ + +void TQColor::initGlobalColors() +{ + globals_init = TRUE; + + #ifdef Q_WS_X11 + // HACK: we need a way to recognize color0 and color1 uniquely, so + // that we can use color0 and color1 with fixed pixel values on + // all screens + stdcol[ 0].d.argb = qRgba(255, 255, 255, 1); + stdcol[ 1].d.argb = qRgba( 0, 0, 0, 1); + #else + stdcol[ 0].d.argb = qRgb(255,255,255); + stdcol[ 1].d.argb = 0; + #endif // Q_WS_X11 + stdcol[ 0].setPixel( COLOR0_PIX ); + stdcol[ 1].setPixel( COLOR1_PIX ); + + // From the "The Palette Manager: How and Why" by Ron Gery, March 23, + // 1992, archived on MSDN: + // The Windows system palette is broken up into two sections, + // one with fixed colors and one with colors that can be changed + // by applications. The system palette predefines 20 entries; + // these colors are known as the static or reserved colors and + // consist of the 16 colors found in the Windows version 3.0 VGA + // driver and 4 additional colors chosen for their visual appeal. + // The DEFAULT_PALETTE stock object is, as the name implies, the + // default palette selected into a device context (DC) and consists + // of these static colors. Applications can set the remaining 236 + // colors using the Palette Manager. + // The 20 reserved entries have indices in [0,9] and [246,255]. We + // reuse 17 of them. + stdcol[ 2].setRgb( 0, 0, 0 ); // index 0 black + stdcol[ 3].setRgb( 255, 255, 255 ); // index 255 white + stdcol[ 4].setRgb( 128, 128, 128 ); // index 248 medium gray + stdcol[ 5].setRgb( 160, 160, 164 ); // index 247 light gray + stdcol[ 6].setRgb( 192, 192, 192 ); // index 7 light gray + stdcol[ 7].setRgb( 255, 0, 0 ); // index 249 red + stdcol[ 8].setRgb( 0, 255, 0 ); // index 250 green + stdcol[ 9].setRgb( 0, 0, 255 ); // index 252 blue + stdcol[10].setRgb( 0, 255, 255 ); // index 254 cyan + stdcol[11].setRgb( 255, 0, 255 ); // index 253 magenta + stdcol[12].setRgb( 255, 255, 0 ); // index 251 yellow + stdcol[13].setRgb( 128, 0, 0 ); // index 1 dark red + stdcol[14].setRgb( 0, 128, 0 ); // index 2 dark green + stdcol[15].setRgb( 0, 0, 128 ); // index 4 dark blue + stdcol[16].setRgb( 0, 128, 128 ); // index 6 dark cyan + stdcol[17].setRgb( 128, 0, 128 ); // index 5 dark magenta + stdcol[18].setRgb( 128, 128, 0 ); // index 3 dark yellow +} + +/*! + \enum TQColor::Spec + + The type of color specified, either RGB or HSV, e.g. in the + \c{TQColor::TQColor( x, y, z, colorSpec)} constructor. + + \value Rgb + \value Hsv +*/ + + +/*! + \fn TQColor::TQColor() + + Constructs an invalid color with the RGB value (0, 0, 0). An + invalid color is a color that is not properly set up for the + underlying window system. + + The alpha value of an invalid color is unspecified. + + \sa isValid() +*/ + + +/*! + \fn TQColor::TQColor( int r, int g, int b ) + + Constructs a color with the RGB value \a r, \a g, \a b, in the + same way as setRgb(). + + The color is left invalid if any or the arguments are illegal. + + \sa setRgb() +*/ + + +/*! + Constructs a color with the RGB value \a rgb and a custom pixel + value \a pixel. + + If \a pixel == 0xffffffff (the default), then the color uses the + RGB value in a standard way. If \a pixel is something else, then + the pixel value is set directly to \a pixel, skipping the normal + allocation procedure. +*/ + +TQColor::TQColor( TQRgb rgb, uint pixel ) +{ + if ( pixel == 0xffffffff ) { + setRgb( rgb ); + } else { + d.argb = rgb; + setPixel( pixel ); + } +} + +void TQColor::setPixel( uint pixel ) +{ + switch ( colormodel ) { + case d8: + d.d8.direct = TRUE; + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pixel; + break; + case d32: + d.d32.pix = pixel; + break; + } +} + + +/*! + Constructs a color with the RGB or HSV value \a x, \a y, \a z. + + The arguments are an RGB value if \a colorSpec is TQColor::Rgb. \a + x (red), \a y (green), and \a z (blue). All of them must be in the + range 0-255. + + The arguments are an HSV value if \a colorSpec is TQColor::Hsv. \a + x (hue) must be -1 for achromatic colors and 0-359 for chromatic + colors; \a y (saturation) and \a z (value) must both be in the + range 0-255. + + \sa setRgb(), setHsv() +*/ + +TQColor::TQColor( int x, int y, int z, Spec colorSpec ) +{ + d.d32.argb = Invalid; + d.d32.pix = Dirt; + if ( colorSpec == Hsv ) + setHsv( x, y, z ); + else + setRgb( x, y, z ); +} + + +/*! + Constructs a named color in the same way as setNamedColor() using + name \a name. + + The color is left invalid if \a name cannot be parsed. + + \sa setNamedColor() +*/ + +TQColor::TQColor( const TQString& name ) +{ + setNamedColor( name ); +} + + +/*! + Constructs a named color in the same way as setNamedColor() using + name \a name. + + The color is left invalid if \a name cannot be parsed. + + \sa setNamedColor() +*/ + +TQColor::TQColor( const char *name ) +{ + setNamedColor( TQString(name) ); +} + + + +/*! + Constructs a color that is a copy of \a c. +*/ + +TQColor::TQColor( const TQColor &c ) +{ + if ( !globals_init ) + initGlobalColors(); + d.argb = c.d.argb; + d.d32.pix = c.d.d32.pix; +} + + +/*! + Assigns a copy of the color \a c and returns a reference to this + color. +*/ + +TQColor &TQColor::operator=( const TQColor &c ) +{ + if ( !globals_init ) + initGlobalColors(); + d.argb = c.d.argb; + d.d32.pix = c.d.d32.pix; + return *this; +} + + +/*! + \fn bool TQColor::isValid() const + + Returns FALSE if the color is invalid, i.e. it was constructed using the + default constructor; otherwise returns TRUE. +*/ + +/*! + \internal +*/ +bool TQColor::isDirty() const +{ + if ( colormodel == d8 ) { + return d.d8.dirty; + } else { + return d.d32.probablyDirty(); + } +} + +/*! + Returns the name of the color in the format "#RRGGBB", i.e. a "#" + character followed by three two-digit hexadecimal numbers. + + \sa setNamedColor() +*/ + +TQString TQColor::name() const +{ +#ifndef QT_NO_SPRINTF + TQString s; + s.sprintf( "#%02x%02x%02x", red(), green(), blue() ); + return s; +#else + char s[20]; + sprintf( s, "#%02x%02x%02x", red(), green(), blue() ); + return TQString(s); +#endif +} + +static int hex2int( TQChar hexchar ) +{ + int v; + if ( hexchar.isDigit() ) + v = hexchar.digitValue(); + else if ( hexchar >= 'A' && hexchar <= 'F' ) + v = hexchar.cell() - 'A' + 10; + else if ( hexchar >= 'a' && hexchar <= 'f' ) + v = hexchar.cell() - 'a' + 10; + else + v = -1; + return v; +} + + +/*! + Sets the RGB value to \a name, which may be in one of these + formats: + \list + \i #RGB (each of R, G and B is a single hex digit) + \i #RRGGBB + \i #RRRGGGBBB + \i #RRRRGGGGBBBB + \i A name from the X color database (rgb.txt) (e.g. + "steelblue" or "gainsboro"). These color names also work + under Windows. + \endlist + + The color is invalid if \a name cannot be parsed. +*/ + +void TQColor::setNamedColor( const TQString &name ) +{ + if ( name.isEmpty() ) { + d.argb = 0; + if ( colormodel == d8 ) { + d.d8.invalid = TRUE; + } else { + d.d32.argb = Invalid; + } + } else if ( name[0] == '#' ) { + const TQChar *p = name.unicode()+1; + int len = name.length()-1; + int r, g, b; + if ( len == 12 ) { + r = (hex2int(p[0]) << 4) + hex2int(p[1]); + g = (hex2int(p[4]) << 4) + hex2int(p[5]); + b = (hex2int(p[8]) << 4) + hex2int(p[9]); + } else if ( len == 9 ) { + r = (hex2int(p[0]) << 4) + hex2int(p[1]); + g = (hex2int(p[3]) << 4) + hex2int(p[4]); + b = (hex2int(p[6]) << 4) + hex2int(p[7]); + } else if ( len == 6 ) { + r = (hex2int(p[0]) << 4) + hex2int(p[1]); + g = (hex2int(p[2]) << 4) + hex2int(p[3]); + b = (hex2int(p[4]) << 4) + hex2int(p[5]); + } else if ( len == 3 ) { + r = (hex2int(p[0]) << 4) + hex2int(p[0]); + g = (hex2int(p[1]) << 4) + hex2int(p[1]); + b = (hex2int(p[2]) << 4) + hex2int(p[2]); + } else { + r = g = b = -1; + } + if ( (uint)r > 255 || (uint)g > 255 || (uint)b > 255 ) { + d.d32.argb = Invalid; + d.d32.pix = Dirt; +#if defined(QT_CHECK_RANGE) + qWarning( "TQColor::setNamedColor: could not parse color '%s'", + name.local8Bit().data() ); +#endif + } else { + setRgb( r, g, b ); + } + } else { + setSystemNamedColor( name ); + } +} + + +#undef max +#undef min + +/*! + \fn void TQColor::getHsv( int &h, int &s, int &v ) const + \obsolete +*/ + +/*! \fn void TQColor::getHsv( int *h, int *s, int *v ) const + + Returns the current RGB value as HSV. The contents of the \a h, \a + s and \a v pointers are set to the HSV values. If any of the three + pointers are null, the function does nothing. + + The hue (which \a h points to) is set to -1 if the color is + achromatic. + + \warning Colors are stored internally as RGB values, so getHSv() + may return slightly different values to those set by setHsv(). + + \sa setHsv(), rgb() +*/ + +/*! \obsolete Use getHsv() instead. + */ +void TQColor::hsv( int *h, int *s, int *v ) const +{ + if ( !h || !s || !v ) + return; + int r = qRed(d.argb); + int g = qGreen(d.argb); + int b = qBlue(d.argb); + uint max = r; // maximum RGB component + int whatmax = 0; // r=>0, g=>1, b=>2 + if ( (uint)g > max ) { + max = g; + whatmax = 1; + } + if ( (uint)b > max ) { + max = b; + whatmax = 2; + } + uint min = r; // find minimum value + if ( (uint)g < min ) min = g; + if ( (uint)b < min ) min = b; + int delta = max-min; + *v = max; // calc value + *s = max ? (510*delta+max)/(2*max) : 0; + if ( *s == 0 ) { + *h = -1; // undefined hue + } else { + switch ( whatmax ) { + case 0: // red is max component + if ( g >= b ) + *h = (120*(g-b)+delta)/(2*delta); + else + *h = (120*(g-b+delta)+delta)/(2*delta) + 300; + break; + case 1: // green is max component + if ( b > r ) + *h = 120 + (120*(b-r)+delta)/(2*delta); + else + *h = 60 + (120*(b-r+delta)+delta)/(2*delta); + break; + case 2: // blue is max component + if ( r > g ) + *h = 240 + (120*(r-g)+delta)/(2*delta); + else + *h = 180 + (120*(r-g+delta)+delta)/(2*delta); + break; + } + } +} + + +/*! + Sets a HSV color value. \a h is the hue, \a s is the saturation + and \a v is the value of the HSV color. + + If \a s or \a v are not in the range 0-255, or \a h is < -1, the + color is not changed. + + \warning Colors are stored internally as RGB values, so getHSv() + may return slightly different values to those set by setHsv(). + + \sa hsv(), setRgb() +*/ + +void TQColor::setHsv( int h, int s, int v ) +{ + if ( h < -1 || (uint)s > 255 || (uint)v > 255 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQColor::setHsv: HSV parameters out of range" ); +#endif + return; + } + int r=v, g=v, b=v; + if ( s == 0 || h == -1 ) { // achromatic case + // Ignore + } else { // chromatic case + if ( (uint)h >= 360 ) + h %= 360; + uint f = h%60; + h /= 60; + uint p = (uint)(2*v*(255-s)+255)/510; + uint q, t; + if ( h&1 ) { + q = (uint)(2*v*(15300-s*f)+15300)/30600; + switch( h ) { + case 1: r=(int)q; g=(int)v, b=(int)p; break; + case 3: r=(int)p; g=(int)q, b=(int)v; break; + case 5: r=(int)v; g=(int)p, b=(int)q; break; + } + } else { + t = (uint)(2*v*(15300-(s*(60-f)))+15300)/30600; + switch( h ) { + case 0: r=(int)v; g=(int)t, b=(int)p; break; + case 2: r=(int)p; g=(int)v, b=(int)t; break; + case 4: r=(int)t; g=(int)p, b=(int)v; break; + } + } + } + setRgb( r, g, b ); +} + + +/*! + \fn TQRgb TQColor::rgb() const + + Returns the RGB value. + + The return type \e TQRgb is equivalent to \c unsigned \c int. + + For an invalid color, the alpha value of the returned color is + unspecified. + + \sa setRgb(), hsv(), qRed(), qBlue(), qGreen(), isValid() +*/ + +/*! \fn void TQColor::getRgb( int *r, int *g, int *b ) const + + Sets the contents pointed to by \a r, \a g and \a b to the red, + green and blue components of the RGB value respectively. The value + range for a component is 0..255. + + \sa rgb(), setRgb(), getHsv() +*/ + +/*! \obsolete Use getRgb() instead */ +void TQColor::rgb( int *r, int *g, int *b ) const +{ + *r = qRed(d.argb); + *g = qGreen(d.argb); + *b = qBlue(d.argb); +} + + +/*! + Sets the RGB value to \a r, \a g, \a b. The arguments, \a r, \a g + and \a b must all be in the range 0..255. If any of them are + outside the legal range, the color is not changed. + + \sa rgb(), setHsv() +*/ + +void TQColor::setRgb( int r, int g, int b ) +{ + if ( (uint)r > 255 || (uint)g > 255 || (uint)b > 255 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQColor::setRgb: RGB parameter(s) out of range" ); +#endif + return; + } + d.argb = qRgb( r, g, b ); + if ( colormodel == d8 ) { + d.d8.invalid = FALSE; + d.d8.direct = FALSE; + d.d8.dirty = TRUE; + } else { + d.d32.pix = Dirt; + } +} + + +/*! + \overload + Sets the RGB value to \a rgb. + + The type \e TQRgb is equivalent to \c unsigned \c int. + + \sa rgb(), setHsv() +*/ + +void TQColor::setRgb( TQRgb rgb ) +{ + d.argb = rgb; + if ( colormodel == d8 ) { + d.d8.invalid = FALSE; + d.d8.direct = FALSE; + d.d8.dirty = TRUE; + } else { + d.d32.pix = Dirt; + } +} + +/*! + \fn int TQColor::red() const + + Returns the R (red) component of the RGB value. +*/ + + +/*! + \fn int TQColor::green() const + + Returns the G (green) component of the RGB value. +*/ + +/*! + \fn int TQColor::blue() const + + Returns the B (blue) component of the RGB value. +*/ + + +/*! + Returns a lighter (or darker) color, but does not change this + object. + + Returns a lighter color if \a factor is greater than 100. Setting + \a factor to 150 returns a color that is 50% brighter. + + Returns a darker color if \a factor is less than 100. We recommend + using dark() for this purpose. If \a factor is 0 or negative, the + return value is unspecified. + + (This function converts the current RGB color to HSV, multiplies V + by \a factor, and converts the result back to RGB.) + + \sa dark() +*/ + +TQColor TQColor::light( int factor ) const +{ + if ( factor <= 0 ) // invalid lightness factor + return *this; + else if ( factor < 100 ) // makes color darker + return dark( 10000/factor ); + + int h, s, v; + hsv( &h, &s, &v ); + v = (factor*v)/100; + if ( v > 255 ) { // overflow + s -= v-255; // adjust saturation + if ( s < 0 ) + s = 0; + v = 255; + } + TQColor c; + c.setHsv( h, s, v ); + return c; +} + + +/*! + Returns a darker (or lighter) color, but does not change this + object. + + Returns a darker color if \a factor is greater than 100. Setting + \a factor to 300 returns a color that has one-third the + brightness. + + Returns a lighter color if \a factor is less than 100. We + recommend using lighter() for this purpose. If \a factor is 0 or + negative, the return value is unspecified. + + (This function converts the current RGB color to HSV, divides V by + \a factor and converts back to RGB.) + + \sa light() +*/ + +TQColor TQColor::dark( int factor ) const +{ + if ( factor <= 0 ) // invalid darkness factor + return *this; + else if ( factor < 100 ) // makes color lighter + return light( 10000/factor ); + int h, s, v; + hsv( &h, &s, &v ); + v = (v*100)/factor; + TQColor c; + c.setHsv( h, s, v ); + return c; +} + + +/*! + \fn bool TQColor::operator==( const TQColor &c ) const + + Returns TRUE if this color has the same RGB value as \a c; + otherwise returns FALSE. +*/ + +/*! + \fn bool TQColor::operator!=( const TQColor &c ) const + Returns TRUE if this color has a different RGB value from \a c; + otherwise returns FALSE. +*/ + +/*! + Returns the pixel value. + + This value is used by the underlying window system to refer to a + color. It can be thought of as an index into the display + hardware's color table, but the value is an arbitrary 32-bit + value. + + \sa alloc() +*/ +uint TQColor::pixel() const +{ + if ( isDirty() ) + return ((TQColor*)this)->alloc(); + else if ( colormodel == d8 ) +#ifdef Q_WS_WIN + // since d.d8.pix is uchar we have to use the PALETTEINDEX + // macro to get the respective palette entry index. + return (0x01000000 | (int)(short)(d.d8.pix)); +#else + return d.d8.pix; +#endif + else + return d.d32.pix; +} + +/*! + \fn TQStringList TQColor::colorNames() + Returns a TQStringList containing the color names TQt knows about. +*/ + +/***************************************************************************** + TQColor stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates TQColor + Writes a color object, \a c to the stream, \a s. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQColor &c ) +{ + Q_UINT32 p = (Q_UINT32)c.rgb(); + if ( s.version() == 1 ) // Swap red and blue + p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00); + return s << p; +} + +/*! + \relates TQColor + Reads a color object, \a c, from the stream, \a s. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQColor &c ) +{ + Q_UINT32 p; + s >> p; + if ( s.version() == 1 ) // Swap red and blue + p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00); + c.setRgb( p ); + return s; +} +#endif + +/***************************************************************************** + TQColor global functions (documentation only) + *****************************************************************************/ + +/*! + \fn int qRed( TQRgb rgb ) + \relates TQColor + + Returns the red component of the RGB triplet \a rgb. + \sa qRgb(), TQColor::red() +*/ + +/*! + \fn int qGreen( TQRgb rgb ) + \relates TQColor + + Returns the green component of the RGB triplet \a rgb. + \sa qRgb(), TQColor::green() +*/ + +/*! + \fn int qBlue( TQRgb rgb ) + \relates TQColor + + Returns the blue component of the RGB triplet \a rgb. + \sa qRgb(), TQColor::blue() +*/ + +/*! + \fn int qAlpha( TQRgb rgba ) + \relates TQColor + + Returns the alpha component of the RGBA quadruplet \a rgba. + */ + +/*! + \fn TQRgb qRgb( int r, int g, int b ) + \relates TQColor + + Returns the RGB triplet \a (r,g,b). + + The return type TQRgb is equivalent to \c unsigned \c int. + + \sa qRgba(), qRed(), qGreen(), qBlue() +*/ + +/*! + \fn TQRgb qRgba( int r, int g, int b, int a ) + \relates TQColor + + Returns the RGBA quadruplet \a (r,g,b,a). + + The return type TQRgba is equivalent to \c unsigned \c int. + + \sa qRgb(), qRed(), qGreen(), qBlue() +*/ + +/*! + \fn int qGray( int r, int g, int b ) + \relates TQColor + + Returns a gray value 0..255 from the (\a r, \a g, \a b) triplet. + + The gray value is calculated using the formula (r*11 + g*16 + + b*5)/32. +*/ + +/*! + \overload int qGray( qRgb rgb ) + \relates TQColor + + Returns a gray value 0..255 from the given \a rgb colour. +*/ + diff --git a/src/kernel/qcolor.h b/src/kernel/qcolor.h new file mode 100644 index 000000000..14e4a9575 --- /dev/null +++ b/src/kernel/qcolor.h @@ -0,0 +1,229 @@ +/**************************************************************************** +** +** Definition of TQColor class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQCOLOR_H +#define TQCOLOR_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qstringlist.h" +#endif // QT_H + +const TQRgb RGB_MASK = 0x00ffffff; // masks RGB values + +Q_EXPORT inline int qRed( TQRgb rgb ) // get red part of RGB +{ return (int)((rgb >> 16) & 0xff); } + +Q_EXPORT inline int qGreen( TQRgb rgb ) // get green part of RGB +{ return (int)((rgb >> 8) & 0xff); } + +Q_EXPORT inline int qBlue( TQRgb rgb ) // get blue part of RGB +{ return (int)(rgb & 0xff); } + +Q_EXPORT inline int qAlpha( TQRgb rgb ) // get alpha part of RGBA +{ return (int)((rgb >> 24) & 0xff); } + +Q_EXPORT inline TQRgb qRgb( int r, int g, int b )// set RGB value +{ return (0xff << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); } + +Q_EXPORT inline TQRgb qRgba( int r, int g, int b, int a )// set RGBA value +{ return ((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); } + +Q_EXPORT inline int qGray( int r, int g, int b )// convert R,G,B to gray 0..255 +{ return (r*11+g*16+b*5)/32; } + +Q_EXPORT inline int qGray( TQRgb rgb ) // convert RGB to gray 0..255 +{ return qGray( qRed(rgb), qGreen(rgb), qBlue(rgb) ); } + + +class Q_EXPORT TQColor +{ +public: + enum Spec { Rgb, Hsv }; + + TQColor(); + TQColor( int r, int g, int b ); + TQColor( int x, int y, int z, Spec ); + TQColor( TQRgb rgb, uint pixel=0xffffffff); + TQColor( const TQString& name ); + TQColor( const char *name ); + TQColor( const TQColor & ); + TQColor &operator=( const TQColor & ); + + bool isValid() const; + bool isDirty() const; + TQString name() const; + void setNamedColor( const TQString& name ); + + TQRgb rgb() const; + void setRgb( int r, int g, int b ); + void setRgb( TQRgb rgb ); + void getRgb( int *r, int *g, int *b ) const { rgb( r, g, b ); } + void rgb( int *r, int *g, int *b ) const; // obsolete + + int red() const; + int green() const; + int blue() const; + + void setHsv( int h, int s, int v ); + void getHsv( int *h, int *s, int *v ) const { hsv( h, s, v ); } + void hsv( int *h, int *s, int *v ) const; // obsolete + void getHsv( int &h, int &s, int &v ) const { hsv( &h, &s, &v ); } // obsolete + + TQColor light( int f = 150 ) const; + TQColor dark( int f = 200 ) const; + + bool operator==( const TQColor &c ) const; + bool operator!=( const TQColor &c ) const; + + uint alloc(); + uint pixel() const; + +#if defined(Q_WS_X11) + // ### in 4.0, make this take a default argument of -1 for default screen? + uint alloc( int screen ); + uint pixel( int screen ) const; +#endif + + static int maxColors(); + static int numBitPlanes(); + + static int enterAllocContext(); + static void leaveAllocContext(); + static int currentAllocContext(); + static void destroyAllocContext( int ); + +#if defined(Q_WS_WIN) + static const TQRgb* palette( int* numEntries = 0 ); + static int setPaletteEntries( const TQRgb* entries, int numEntries, + int base = -1 ); + static HPALETTE hPal() { return hpal; } + static uint realizePal( TQWidget * ); +#endif + + static void initialize(); + static void cleanup(); +#ifndef QT_NO_STRINGLIST + static TQStringList colorNames(); +#endif + enum { Dirt = 0x44495254, Invalid = 0x49000000 }; + +private: + void setSystemNamedColor( const TQString& name ); + void setPixel( uint pixel ); + static void initGlobalColors(); + static uint argbToPix32(TQRgb); + static TQColor* globalColors(); + static bool color_init; + static bool globals_init; +#if defined(Q_WS_WIN) + static HPALETTE hpal; +#endif + static enum ColorModel { d8, d32 } colormodel; + union { + TQRgb argb; + struct D8 { + TQRgb argb; + uchar pix; + uchar invalid; + uchar dirty; + uchar direct; + } d8; + struct D32 { + TQRgb argb; + uint pix; + bool invalid() const { return argb == TQColor::Invalid && pix == TQColor::Dirt; } + bool probablyDirty() const { return pix == TQColor::Dirt; } + } d32; + } d; +}; + + +inline TQColor::TQColor() +{ d.d32.argb = Invalid; d.d32.pix = Dirt; } + +inline TQColor::TQColor( int r, int g, int b ) +{ + d.d32.argb = Invalid; + d.d32.pix = Dirt; + setRgb( r, g, b ); +} + +inline TQRgb TQColor::rgb() const +{ return d.argb; } + +inline int TQColor::red() const +{ return qRed(d.argb); } + +inline int TQColor::green() const +{ return qGreen(d.argb); } + +inline int TQColor::blue() const +{ return qBlue(d.argb); } + +inline bool TQColor::isValid() const +{ + if ( colormodel == d8 ) + return !d.d8.invalid; + else + return !d.d32.invalid(); +} + +inline bool TQColor::operator==( const TQColor &c ) const +{ + return d.argb == c.d.argb && isValid() == c.isValid(); +} + +inline bool TQColor::operator!=( const TQColor &c ) const +{ + return !operator==(c); +} + + +/***************************************************************************** + TQColor stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQColor & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQColor & ); +#endif + +#endif // TQCOLOR_H diff --git a/src/kernel/qcolor_p.cpp b/src/kernel/qcolor_p.cpp new file mode 100644 index 000000000..e38669285 --- /dev/null +++ b/src/kernel/qcolor_p.cpp @@ -0,0 +1,797 @@ +/**************************************************************************** +** +** Named color support for non-X platforms. +** The color names have been borrowed from X. +** +** Created : 000228 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qglobal.h" +#if defined(Q_CC_BOR) +// needed for qsort() because of a std namespace problem on Borland +#include "qplatformdefs.h" +#endif + +#include "qcolor.h" + +#ifndef QT_NO_COLORNAMES + +#include + +#undef TQRGB +#define TQRGB(r,g,b) (r*65536 + g*256 + b) + +const int rgbTblSize = 657; + +static const struct RGBData { + uint value; + const char *name; +} rgbTbl[] = { + { TQRGB(240,248,255), "aliceblue" }, + { TQRGB(250,235,215), "antiquewhite" }, + { TQRGB(255,239,219), "antiquewhite1" }, + { TQRGB(238,223,204), "antiquewhite2" }, + { TQRGB(205,192,176), "antiquewhite3" }, + { TQRGB(139,131,120), "antiquewhite4" }, + { TQRGB(127,255,212), "aquamarine" }, + { TQRGB(127,255,212), "aquamarine1" }, + { TQRGB(118,238,198), "aquamarine2" }, + { TQRGB(102,205,170), "aquamarine3" }, + { TQRGB( 69,139,116), "aquamarine4" }, + { TQRGB(240,255,255), "azure" }, + { TQRGB(240,255,255), "azure1" }, + { TQRGB(224,238,238), "azure2" }, + { TQRGB(193,205,205), "azure3" }, + { TQRGB(131,139,139), "azure4" }, + { TQRGB(245,245,220), "beige" }, + { TQRGB(255,228,196), "bisque" }, + { TQRGB(255,228,196), "bisque1" }, + { TQRGB(238,213,183), "bisque2" }, + { TQRGB(205,183,158), "bisque3" }, + { TQRGB(139,125,107), "bisque4" }, + { TQRGB( 0, 0, 0), "black" }, + { TQRGB(255,235,205), "blanchedalmond" }, + { TQRGB( 0, 0,255), "blue" }, + { TQRGB( 0, 0,255), "blue1" }, + { TQRGB( 0, 0,238), "blue2" }, + { TQRGB( 0, 0,205), "blue3" }, + { TQRGB( 0, 0,139), "blue4" }, + { TQRGB(138, 43,226), "blueviolet" }, + { TQRGB(165, 42, 42), "brown" }, + { TQRGB(255, 64, 64), "brown1" }, + { TQRGB(238, 59, 59), "brown2" }, + { TQRGB(205, 51, 51), "brown3" }, + { TQRGB(139, 35, 35), "brown4" }, + { TQRGB(222,184,135), "burlywood" }, + { TQRGB(255,211,155), "burlywood1" }, + { TQRGB(238,197,145), "burlywood2" }, + { TQRGB(205,170,125), "burlywood3" }, + { TQRGB(139,115, 85), "burlywood4" }, + { TQRGB( 95,158,160), "cadetblue" }, + { TQRGB(152,245,255), "cadetblue1" }, + { TQRGB(142,229,238), "cadetblue2" }, + { TQRGB(122,197,205), "cadetblue3" }, + { TQRGB( 83,134,139), "cadetblue4" }, + { TQRGB(127,255, 0), "chartreuse" }, + { TQRGB(127,255, 0), "chartreuse1" }, + { TQRGB(118,238, 0), "chartreuse2" }, + { TQRGB(102,205, 0), "chartreuse3" }, + { TQRGB( 69,139, 0), "chartreuse4" }, + { TQRGB(210,105, 30), "chocolate" }, + { TQRGB(255,127, 36), "chocolate1" }, + { TQRGB(238,118, 33), "chocolate2" }, + { TQRGB(205,102, 29), "chocolate3" }, + { TQRGB(139, 69, 19), "chocolate4" }, + { TQRGB(255,127, 80), "coral" }, + { TQRGB(255,114, 86), "coral1" }, + { TQRGB(238,106, 80), "coral2" }, + { TQRGB(205, 91, 69), "coral3" }, + { TQRGB(139, 62, 47), "coral4" }, + { TQRGB(100,149,237), "cornflowerblue" }, + { TQRGB(255,248,220), "cornsilk" }, + { TQRGB(255,248,220), "cornsilk1" }, + { TQRGB(238,232,205), "cornsilk2" }, + { TQRGB(205,200,177), "cornsilk3" }, + { TQRGB(139,136,120), "cornsilk4" }, + { TQRGB( 0,255,255), "cyan" }, + { TQRGB( 0,255,255), "cyan1" }, + { TQRGB( 0,238,238), "cyan2" }, + { TQRGB( 0,205,205), "cyan3" }, + { TQRGB( 0,139,139), "cyan4" }, + { TQRGB( 0, 0,139), "darkblue" }, + { TQRGB( 0,139,139), "darkcyan" }, + { TQRGB(184,134, 11), "darkgoldenrod" }, + { TQRGB(255,185, 15), "darkgoldenrod1" }, + { TQRGB(238,173, 14), "darkgoldenrod2" }, + { TQRGB(205,149, 12), "darkgoldenrod3" }, + { TQRGB(139,101, 8), "darkgoldenrod4" }, + { TQRGB(169,169,169), "darkgray" }, + { TQRGB( 0,100, 0), "darkgreen" }, + { TQRGB(169,169,169), "darkgrey" }, + { TQRGB(189,183,107), "darkkhaki" }, + { TQRGB(139, 0,139), "darkmagenta" }, + { TQRGB( 85,107, 47), "darkolivegreen" }, + { TQRGB(202,255,112), "darkolivegreen1" }, + { TQRGB(188,238,104), "darkolivegreen2" }, + { TQRGB(162,205, 90), "darkolivegreen3" }, + { TQRGB(110,139, 61), "darkolivegreen4" }, + { TQRGB(255,140, 0), "darkorange" }, + { TQRGB(255,127, 0), "darkorange1" }, + { TQRGB(238,118, 0), "darkorange2" }, + { TQRGB(205,102, 0), "darkorange3" }, + { TQRGB(139, 69, 0), "darkorange4" }, + { TQRGB(153, 50,204), "darkorchid" }, + { TQRGB(191, 62,255), "darkorchid1" }, + { TQRGB(178, 58,238), "darkorchid2" }, + { TQRGB(154, 50,205), "darkorchid3" }, + { TQRGB(104, 34,139), "darkorchid4" }, + { TQRGB(139, 0, 0), "darkred" }, + { TQRGB(233,150,122), "darksalmon" }, + { TQRGB(143,188,143), "darkseagreen" }, + { TQRGB(193,255,193), "darkseagreen1" }, + { TQRGB(180,238,180), "darkseagreen2" }, + { TQRGB(155,205,155), "darkseagreen3" }, + { TQRGB(105,139,105), "darkseagreen4" }, + { TQRGB( 72, 61,139), "darkslateblue" }, + { TQRGB( 47, 79, 79), "darkslategray" }, + { TQRGB(151,255,255), "darkslategray1" }, + { TQRGB(141,238,238), "darkslategray2" }, + { TQRGB(121,205,205), "darkslategray3" }, + { TQRGB( 82,139,139), "darkslategray4" }, + { TQRGB( 47, 79, 79), "darkslategrey" }, + { TQRGB( 0,206,209), "darkturquoise" }, + { TQRGB(148, 0,211), "darkviolet" }, + { TQRGB(255, 20,147), "deeppink" }, + { TQRGB(255, 20,147), "deeppink1" }, + { TQRGB(238, 18,137), "deeppink2" }, + { TQRGB(205, 16,118), "deeppink3" }, + { TQRGB(139, 10, 80), "deeppink4" }, + { TQRGB( 0,191,255), "deepskyblue" }, + { TQRGB( 0,191,255), "deepskyblue1" }, + { TQRGB( 0,178,238), "deepskyblue2" }, + { TQRGB( 0,154,205), "deepskyblue3" }, + { TQRGB( 0,104,139), "deepskyblue4" }, + { TQRGB(105,105,105), "dimgray" }, + { TQRGB(105,105,105), "dimgrey" }, + { TQRGB( 30,144,255), "dodgerblue" }, + { TQRGB( 30,144,255), "dodgerblue1" }, + { TQRGB( 28,134,238), "dodgerblue2" }, + { TQRGB( 24,116,205), "dodgerblue3" }, + { TQRGB( 16, 78,139), "dodgerblue4" }, + { TQRGB(178, 34, 34), "firebrick" }, + { TQRGB(255, 48, 48), "firebrick1" }, + { TQRGB(238, 44, 44), "firebrick2" }, + { TQRGB(205, 38, 38), "firebrick3" }, + { TQRGB(139, 26, 26), "firebrick4" }, + { TQRGB(255,250,240), "floralwhite" }, + { TQRGB( 34,139, 34), "forestgreen" }, + { TQRGB(220,220,220), "gainsboro" }, + { TQRGB(248,248,255), "ghostwhite" }, + { TQRGB(255,215, 0), "gold" }, + { TQRGB(255,215, 0), "gold1" }, + { TQRGB(238,201, 0), "gold2" }, + { TQRGB(205,173, 0), "gold3" }, + { TQRGB(139,117, 0), "gold4" }, + { TQRGB(218,165, 32), "goldenrod" }, + { TQRGB(255,193, 37), "goldenrod1" }, + { TQRGB(238,180, 34), "goldenrod2" }, + { TQRGB(205,155, 29), "goldenrod3" }, + { TQRGB(139,105, 20), "goldenrod4" }, + { TQRGB(190,190,190), "gray" }, + { TQRGB( 0, 0, 0), "gray0" }, + { TQRGB( 3, 3, 3), "gray1" }, + { TQRGB( 26, 26, 26), "gray10" }, + { TQRGB(255,255,255), "gray100" }, + { TQRGB( 28, 28, 28), "gray11" }, + { TQRGB( 31, 31, 31), "gray12" }, + { TQRGB( 33, 33, 33), "gray13" }, + { TQRGB( 36, 36, 36), "gray14" }, + { TQRGB( 38, 38, 38), "gray15" }, + { TQRGB( 41, 41, 41), "gray16" }, + { TQRGB( 43, 43, 43), "gray17" }, + { TQRGB( 46, 46, 46), "gray18" }, + { TQRGB( 48, 48, 48), "gray19" }, + { TQRGB( 5, 5, 5), "gray2" }, + { TQRGB( 51, 51, 51), "gray20" }, + { TQRGB( 54, 54, 54), "gray21" }, + { TQRGB( 56, 56, 56), "gray22" }, + { TQRGB( 59, 59, 59), "gray23" }, + { TQRGB( 61, 61, 61), "gray24" }, + { TQRGB( 64, 64, 64), "gray25" }, + { TQRGB( 66, 66, 66), "gray26" }, + { TQRGB( 69, 69, 69), "gray27" }, + { TQRGB( 71, 71, 71), "gray28" }, + { TQRGB( 74, 74, 74), "gray29" }, + { TQRGB( 8, 8, 8), "gray3" }, + { TQRGB( 77, 77, 77), "gray30" }, + { TQRGB( 79, 79, 79), "gray31" }, + { TQRGB( 82, 82, 82), "gray32" }, + { TQRGB( 84, 84, 84), "gray33" }, + { TQRGB( 87, 87, 87), "gray34" }, + { TQRGB( 89, 89, 89), "gray35" }, + { TQRGB( 92, 92, 92), "gray36" }, + { TQRGB( 94, 94, 94), "gray37" }, + { TQRGB( 97, 97, 97), "gray38" }, + { TQRGB( 99, 99, 99), "gray39" }, + { TQRGB( 10, 10, 10), "gray4" }, + { TQRGB(102,102,102), "gray40" }, + { TQRGB(105,105,105), "gray41" }, + { TQRGB(107,107,107), "gray42" }, + { TQRGB(110,110,110), "gray43" }, + { TQRGB(112,112,112), "gray44" }, + { TQRGB(115,115,115), "gray45" }, + { TQRGB(117,117,117), "gray46" }, + { TQRGB(120,120,120), "gray47" }, + { TQRGB(122,122,122), "gray48" }, + { TQRGB(125,125,125), "gray49" }, + { TQRGB( 13, 13, 13), "gray5" }, + { TQRGB(127,127,127), "gray50" }, + { TQRGB(130,130,130), "gray51" }, + { TQRGB(133,133,133), "gray52" }, + { TQRGB(135,135,135), "gray53" }, + { TQRGB(138,138,138), "gray54" }, + { TQRGB(140,140,140), "gray55" }, + { TQRGB(143,143,143), "gray56" }, + { TQRGB(145,145,145), "gray57" }, + { TQRGB(148,148,148), "gray58" }, + { TQRGB(150,150,150), "gray59" }, + { TQRGB( 15, 15, 15), "gray6" }, + { TQRGB(153,153,153), "gray60" }, + { TQRGB(156,156,156), "gray61" }, + { TQRGB(158,158,158), "gray62" }, + { TQRGB(161,161,161), "gray63" }, + { TQRGB(163,163,163), "gray64" }, + { TQRGB(166,166,166), "gray65" }, + { TQRGB(168,168,168), "gray66" }, + { TQRGB(171,171,171), "gray67" }, + { TQRGB(173,173,173), "gray68" }, + { TQRGB(176,176,176), "gray69" }, + { TQRGB( 18, 18, 18), "gray7" }, + { TQRGB(179,179,179), "gray70" }, + { TQRGB(181,181,181), "gray71" }, + { TQRGB(184,184,184), "gray72" }, + { TQRGB(186,186,186), "gray73" }, + { TQRGB(189,189,189), "gray74" }, + { TQRGB(191,191,191), "gray75" }, + { TQRGB(194,194,194), "gray76" }, + { TQRGB(196,196,196), "gray77" }, + { TQRGB(199,199,199), "gray78" }, + { TQRGB(201,201,201), "gray79" }, + { TQRGB( 20, 20, 20), "gray8" }, + { TQRGB(204,204,204), "gray80" }, + { TQRGB(207,207,207), "gray81" }, + { TQRGB(209,209,209), "gray82" }, + { TQRGB(212,212,212), "gray83" }, + { TQRGB(214,214,214), "gray84" }, + { TQRGB(217,217,217), "gray85" }, + { TQRGB(219,219,219), "gray86" }, + { TQRGB(222,222,222), "gray87" }, + { TQRGB(224,224,224), "gray88" }, + { TQRGB(227,227,227), "gray89" }, + { TQRGB( 23, 23, 23), "gray9" }, + { TQRGB(229,229,229), "gray90" }, + { TQRGB(232,232,232), "gray91" }, + { TQRGB(235,235,235), "gray92" }, + { TQRGB(237,237,237), "gray93" }, + { TQRGB(240,240,240), "gray94" }, + { TQRGB(242,242,242), "gray95" }, + { TQRGB(245,245,245), "gray96" }, + { TQRGB(247,247,247), "gray97" }, + { TQRGB(250,250,250), "gray98" }, + { TQRGB(252,252,252), "gray99" }, + { TQRGB( 0,255, 0), "green" }, + { TQRGB( 0,255, 0), "green1" }, + { TQRGB( 0,238, 0), "green2" }, + { TQRGB( 0,205, 0), "green3" }, + { TQRGB( 0,139, 0), "green4" }, + { TQRGB(173,255, 47), "greenyellow" }, + { TQRGB(190,190,190), "grey" }, + { TQRGB( 0, 0, 0), "grey0" }, + { TQRGB( 3, 3, 3), "grey1" }, + { TQRGB( 26, 26, 26), "grey10" }, + { TQRGB(255,255,255), "grey100" }, + { TQRGB( 28, 28, 28), "grey11" }, + { TQRGB( 31, 31, 31), "grey12" }, + { TQRGB( 33, 33, 33), "grey13" }, + { TQRGB( 36, 36, 36), "grey14" }, + { TQRGB( 38, 38, 38), "grey15" }, + { TQRGB( 41, 41, 41), "grey16" }, + { TQRGB( 43, 43, 43), "grey17" }, + { TQRGB( 46, 46, 46), "grey18" }, + { TQRGB( 48, 48, 48), "grey19" }, + { TQRGB( 5, 5, 5), "grey2" }, + { TQRGB( 51, 51, 51), "grey20" }, + { TQRGB( 54, 54, 54), "grey21" }, + { TQRGB( 56, 56, 56), "grey22" }, + { TQRGB( 59, 59, 59), "grey23" }, + { TQRGB( 61, 61, 61), "grey24" }, + { TQRGB( 64, 64, 64), "grey25" }, + { TQRGB( 66, 66, 66), "grey26" }, + { TQRGB( 69, 69, 69), "grey27" }, + { TQRGB( 71, 71, 71), "grey28" }, + { TQRGB( 74, 74, 74), "grey29" }, + { TQRGB( 8, 8, 8), "grey3" }, + { TQRGB( 77, 77, 77), "grey30" }, + { TQRGB( 79, 79, 79), "grey31" }, + { TQRGB( 82, 82, 82), "grey32" }, + { TQRGB( 84, 84, 84), "grey33" }, + { TQRGB( 87, 87, 87), "grey34" }, + { TQRGB( 89, 89, 89), "grey35" }, + { TQRGB( 92, 92, 92), "grey36" }, + { TQRGB( 94, 94, 94), "grey37" }, + { TQRGB( 97, 97, 97), "grey38" }, + { TQRGB( 99, 99, 99), "grey39" }, + { TQRGB( 10, 10, 10), "grey4" }, + { TQRGB(102,102,102), "grey40" }, + { TQRGB(105,105,105), "grey41" }, + { TQRGB(107,107,107), "grey42" }, + { TQRGB(110,110,110), "grey43" }, + { TQRGB(112,112,112), "grey44" }, + { TQRGB(115,115,115), "grey45" }, + { TQRGB(117,117,117), "grey46" }, + { TQRGB(120,120,120), "grey47" }, + { TQRGB(122,122,122), "grey48" }, + { TQRGB(125,125,125), "grey49" }, + { TQRGB( 13, 13, 13), "grey5" }, + { TQRGB(127,127,127), "grey50" }, + { TQRGB(130,130,130), "grey51" }, + { TQRGB(133,133,133), "grey52" }, + { TQRGB(135,135,135), "grey53" }, + { TQRGB(138,138,138), "grey54" }, + { TQRGB(140,140,140), "grey55" }, + { TQRGB(143,143,143), "grey56" }, + { TQRGB(145,145,145), "grey57" }, + { TQRGB(148,148,148), "grey58" }, + { TQRGB(150,150,150), "grey59" }, + { TQRGB( 15, 15, 15), "grey6" }, + { TQRGB(153,153,153), "grey60" }, + { TQRGB(156,156,156), "grey61" }, + { TQRGB(158,158,158), "grey62" }, + { TQRGB(161,161,161), "grey63" }, + { TQRGB(163,163,163), "grey64" }, + { TQRGB(166,166,166), "grey65" }, + { TQRGB(168,168,168), "grey66" }, + { TQRGB(171,171,171), "grey67" }, + { TQRGB(173,173,173), "grey68" }, + { TQRGB(176,176,176), "grey69" }, + { TQRGB( 18, 18, 18), "grey7" }, + { TQRGB(179,179,179), "grey70" }, + { TQRGB(181,181,181), "grey71" }, + { TQRGB(184,184,184), "grey72" }, + { TQRGB(186,186,186), "grey73" }, + { TQRGB(189,189,189), "grey74" }, + { TQRGB(191,191,191), "grey75" }, + { TQRGB(194,194,194), "grey76" }, + { TQRGB(196,196,196), "grey77" }, + { TQRGB(199,199,199), "grey78" }, + { TQRGB(201,201,201), "grey79" }, + { TQRGB( 20, 20, 20), "grey8" }, + { TQRGB(204,204,204), "grey80" }, + { TQRGB(207,207,207), "grey81" }, + { TQRGB(209,209,209), "grey82" }, + { TQRGB(212,212,212), "grey83" }, + { TQRGB(214,214,214), "grey84" }, + { TQRGB(217,217,217), "grey85" }, + { TQRGB(219,219,219), "grey86" }, + { TQRGB(222,222,222), "grey87" }, + { TQRGB(224,224,224), "grey88" }, + { TQRGB(227,227,227), "grey89" }, + { TQRGB( 23, 23, 23), "grey9" }, + { TQRGB(229,229,229), "grey90" }, + { TQRGB(232,232,232), "grey91" }, + { TQRGB(235,235,235), "grey92" }, + { TQRGB(237,237,237), "grey93" }, + { TQRGB(240,240,240), "grey94" }, + { TQRGB(242,242,242), "grey95" }, + { TQRGB(245,245,245), "grey96" }, + { TQRGB(247,247,247), "grey97" }, + { TQRGB(250,250,250), "grey98" }, + { TQRGB(252,252,252), "grey99" }, + { TQRGB(240,255,240), "honeydew" }, + { TQRGB(240,255,240), "honeydew1" }, + { TQRGB(224,238,224), "honeydew2" }, + { TQRGB(193,205,193), "honeydew3" }, + { TQRGB(131,139,131), "honeydew4" }, + { TQRGB(255,105,180), "hotpink" }, + { TQRGB(255,110,180), "hotpink1" }, + { TQRGB(238,106,167), "hotpink2" }, + { TQRGB(205, 96,144), "hotpink3" }, + { TQRGB(139, 58, 98), "hotpink4" }, + { TQRGB(205, 92, 92), "indianred" }, + { TQRGB(255,106,106), "indianred1" }, + { TQRGB(238, 99, 99), "indianred2" }, + { TQRGB(205, 85, 85), "indianred3" }, + { TQRGB(139, 58, 58), "indianred4" }, + { TQRGB(255,255,240), "ivory" }, + { TQRGB(255,255,240), "ivory1" }, + { TQRGB(238,238,224), "ivory2" }, + { TQRGB(205,205,193), "ivory3" }, + { TQRGB(139,139,131), "ivory4" }, + { TQRGB(240,230,140), "khaki" }, + { TQRGB(255,246,143), "khaki1" }, + { TQRGB(238,230,133), "khaki2" }, + { TQRGB(205,198,115), "khaki3" }, + { TQRGB(139,134, 78), "khaki4" }, + { TQRGB(230,230,250), "lavender" }, + { TQRGB(255,240,245), "lavenderblush" }, + { TQRGB(255,240,245), "lavenderblush1" }, + { TQRGB(238,224,229), "lavenderblush2" }, + { TQRGB(205,193,197), "lavenderblush3" }, + { TQRGB(139,131,134), "lavenderblush4" }, + { TQRGB(124,252, 0), "lawngreen" }, + { TQRGB(255,250,205), "lemonchiffon" }, + { TQRGB(255,250,205), "lemonchiffon1" }, + { TQRGB(238,233,191), "lemonchiffon2" }, + { TQRGB(205,201,165), "lemonchiffon3" }, + { TQRGB(139,137,112), "lemonchiffon4" }, + { TQRGB(173,216,230), "lightblue" }, + { TQRGB(191,239,255), "lightblue1" }, + { TQRGB(178,223,238), "lightblue2" }, + { TQRGB(154,192,205), "lightblue3" }, + { TQRGB(104,131,139), "lightblue4" }, + { TQRGB(240,128,128), "lightcoral" }, + { TQRGB(224,255,255), "lightcyan" }, + { TQRGB(224,255,255), "lightcyan1" }, + { TQRGB(209,238,238), "lightcyan2" }, + { TQRGB(180,205,205), "lightcyan3" }, + { TQRGB(122,139,139), "lightcyan4" }, + { TQRGB(238,221,130), "lightgoldenrod" }, + { TQRGB(255,236,139), "lightgoldenrod1" }, + { TQRGB(238,220,130), "lightgoldenrod2" }, + { TQRGB(205,190,112), "lightgoldenrod3" }, + { TQRGB(139,129, 76), "lightgoldenrod4" }, + { TQRGB(250,250,210), "lightgoldenrodyellow" }, + { TQRGB(211,211,211), "lightgray" }, + { TQRGB(144,238,144), "lightgreen" }, + { TQRGB(211,211,211), "lightgrey" }, + { TQRGB(255,182,193), "lightpink" }, + { TQRGB(255,174,185), "lightpink1" }, + { TQRGB(238,162,173), "lightpink2" }, + { TQRGB(205,140,149), "lightpink3" }, + { TQRGB(139, 95,101), "lightpink4" }, + { TQRGB(255,160,122), "lightsalmon" }, + { TQRGB(255,160,122), "lightsalmon1" }, + { TQRGB(238,149,114), "lightsalmon2" }, + { TQRGB(205,129, 98), "lightsalmon3" }, + { TQRGB(139, 87, 66), "lightsalmon4" }, + { TQRGB( 32,178,170), "lightseagreen" }, + { TQRGB(135,206,250), "lightskyblue" }, + { TQRGB(176,226,255), "lightskyblue1" }, + { TQRGB(164,211,238), "lightskyblue2" }, + { TQRGB(141,182,205), "lightskyblue3" }, + { TQRGB( 96,123,139), "lightskyblue4" }, + { TQRGB(132,112,255), "lightslateblue" }, + { TQRGB(119,136,153), "lightslategray" }, + { TQRGB(119,136,153), "lightslategrey" }, + { TQRGB(176,196,222), "lightsteelblue" }, + { TQRGB(202,225,255), "lightsteelblue1" }, + { TQRGB(188,210,238), "lightsteelblue2" }, + { TQRGB(162,181,205), "lightsteelblue3" }, + { TQRGB(110,123,139), "lightsteelblue4" }, + { TQRGB(255,255,224), "lightyellow" }, + { TQRGB(255,255,224), "lightyellow1" }, + { TQRGB(238,238,209), "lightyellow2" }, + { TQRGB(205,205,180), "lightyellow3" }, + { TQRGB(139,139,122), "lightyellow4" }, + { TQRGB( 50,205, 50), "limegreen" }, + { TQRGB(250,240,230), "linen" }, + { TQRGB(255, 0,255), "magenta" }, + { TQRGB(255, 0,255), "magenta1" }, + { TQRGB(238, 0,238), "magenta2" }, + { TQRGB(205, 0,205), "magenta3" }, + { TQRGB(139, 0,139), "magenta4" }, + { TQRGB(176, 48, 96), "maroon" }, + { TQRGB(255, 52,179), "maroon1" }, + { TQRGB(238, 48,167), "maroon2" }, + { TQRGB(205, 41,144), "maroon3" }, + { TQRGB(139, 28, 98), "maroon4" }, + { TQRGB(102,205,170), "mediumaquamarine" }, + { TQRGB( 0, 0,205), "mediumblue" }, + { TQRGB(186, 85,211), "mediumorchid" }, + { TQRGB(224,102,255), "mediumorchid1" }, + { TQRGB(209, 95,238), "mediumorchid2" }, + { TQRGB(180, 82,205), "mediumorchid3" }, + { TQRGB(122, 55,139), "mediumorchid4" }, + { TQRGB(147,112,219), "mediumpurple" }, + { TQRGB(171,130,255), "mediumpurple1" }, + { TQRGB(159,121,238), "mediumpurple2" }, + { TQRGB(137,104,205), "mediumpurple3" }, + { TQRGB( 93, 71,139), "mediumpurple4" }, + { TQRGB( 60,179,113), "mediumseagreen" }, + { TQRGB(123,104,238), "mediumslateblue" }, + { TQRGB( 0,250,154), "mediumspringgreen" }, + { TQRGB( 72,209,204), "mediumturquoise" }, + { TQRGB(199, 21,133), "mediumvioletred" }, + { TQRGB( 25, 25,112), "midnightblue" }, + { TQRGB(245,255,250), "mintcream" }, + { TQRGB(255,228,225), "mistyrose" }, + { TQRGB(255,228,225), "mistyrose1" }, + { TQRGB(238,213,210), "mistyrose2" }, + { TQRGB(205,183,181), "mistyrose3" }, + { TQRGB(139,125,123), "mistyrose4" }, + { TQRGB(255,228,181), "moccasin" }, + { TQRGB(255,222,173), "navajowhite" }, + { TQRGB(255,222,173), "navajowhite1" }, + { TQRGB(238,207,161), "navajowhite2" }, + { TQRGB(205,179,139), "navajowhite3" }, + { TQRGB(139,121, 94), "navajowhite4" }, + { TQRGB( 0, 0,128), "navy" }, + { TQRGB( 0, 0,128), "navyblue" }, + { TQRGB(253,245,230), "oldlace" }, + { TQRGB(107,142, 35), "olivedrab" }, + { TQRGB(192,255, 62), "olivedrab1" }, + { TQRGB(179,238, 58), "olivedrab2" }, + { TQRGB(154,205, 50), "olivedrab3" }, + { TQRGB(105,139, 34), "olivedrab4" }, + { TQRGB(255,165, 0), "orange" }, + { TQRGB(255,165, 0), "orange1" }, + { TQRGB(238,154, 0), "orange2" }, + { TQRGB(205,133, 0), "orange3" }, + { TQRGB(139, 90, 0), "orange4" }, + { TQRGB(255, 69, 0), "orangered" }, + { TQRGB(255, 69, 0), "orangered1" }, + { TQRGB(238, 64, 0), "orangered2" }, + { TQRGB(205, 55, 0), "orangered3" }, + { TQRGB(139, 37, 0), "orangered4" }, + { TQRGB(218,112,214), "orchid" }, + { TQRGB(255,131,250), "orchid1" }, + { TQRGB(238,122,233), "orchid2" }, + { TQRGB(205,105,201), "orchid3" }, + { TQRGB(139, 71,137), "orchid4" }, + { TQRGB(238,232,170), "palegoldenrod" }, + { TQRGB(152,251,152), "palegreen" }, + { TQRGB(154,255,154), "palegreen1" }, + { TQRGB(144,238,144), "palegreen2" }, + { TQRGB(124,205,124), "palegreen3" }, + { TQRGB( 84,139, 84), "palegreen4" }, + { TQRGB(175,238,238), "paleturquoise" }, + { TQRGB(187,255,255), "paleturquoise1" }, + { TQRGB(174,238,238), "paleturquoise2" }, + { TQRGB(150,205,205), "paleturquoise3" }, + { TQRGB(102,139,139), "paleturquoise4" }, + { TQRGB(219,112,147), "palevioletred" }, + { TQRGB(255,130,171), "palevioletred1" }, + { TQRGB(238,121,159), "palevioletred2" }, + { TQRGB(205,104,137), "palevioletred3" }, + { TQRGB(139, 71, 93), "palevioletred4" }, + { TQRGB(255,239,213), "papayawhip" }, + { TQRGB(255,218,185), "peachpuff" }, + { TQRGB(255,218,185), "peachpuff1" }, + { TQRGB(238,203,173), "peachpuff2" }, + { TQRGB(205,175,149), "peachpuff3" }, + { TQRGB(139,119,101), "peachpuff4" }, + { TQRGB(205,133, 63), "peru" }, + { TQRGB(255,192,203), "pink" }, + { TQRGB(255,181,197), "pink1" }, + { TQRGB(238,169,184), "pink2" }, + { TQRGB(205,145,158), "pink3" }, + { TQRGB(139, 99,108), "pink4" }, + { TQRGB(221,160,221), "plum" }, + { TQRGB(255,187,255), "plum1" }, + { TQRGB(238,174,238), "plum2" }, + { TQRGB(205,150,205), "plum3" }, + { TQRGB(139,102,139), "plum4" }, + { TQRGB(176,224,230), "powderblue" }, + { TQRGB(160, 32,240), "purple" }, + { TQRGB(155, 48,255), "purple1" }, + { TQRGB(145, 44,238), "purple2" }, + { TQRGB(125, 38,205), "purple3" }, + { TQRGB( 85, 26,139), "purple4" }, + { TQRGB(255, 0, 0), "red" }, + { TQRGB(255, 0, 0), "red1" }, + { TQRGB(238, 0, 0), "red2" }, + { TQRGB(205, 0, 0), "red3" }, + { TQRGB(139, 0, 0), "red4" }, + { TQRGB(188,143,143), "rosybrown" }, + { TQRGB(255,193,193), "rosybrown1" }, + { TQRGB(238,180,180), "rosybrown2" }, + { TQRGB(205,155,155), "rosybrown3" }, + { TQRGB(139,105,105), "rosybrown4" }, + { TQRGB( 65,105,225), "royalblue" }, + { TQRGB( 72,118,255), "royalblue1" }, + { TQRGB( 67,110,238), "royalblue2" }, + { TQRGB( 58, 95,205), "royalblue3" }, + { TQRGB( 39, 64,139), "royalblue4" }, + { TQRGB(139, 69, 19), "saddlebrown" }, + { TQRGB(250,128,114), "salmon" }, + { TQRGB(255,140,105), "salmon1" }, + { TQRGB(238,130, 98), "salmon2" }, + { TQRGB(205,112, 84), "salmon3" }, + { TQRGB(139, 76, 57), "salmon4" }, + { TQRGB(244,164, 96), "sandybrown" }, + { TQRGB( 46,139, 87), "seagreen" }, + { TQRGB( 84,255,159), "seagreen1" }, + { TQRGB( 78,238,148), "seagreen2" }, + { TQRGB( 67,205,128), "seagreen3" }, + { TQRGB( 46,139, 87), "seagreen4" }, + { TQRGB(255,245,238), "seashell" }, + { TQRGB(255,245,238), "seashell1" }, + { TQRGB(238,229,222), "seashell2" }, + { TQRGB(205,197,191), "seashell3" }, + { TQRGB(139,134,130), "seashell4" }, + { TQRGB(160, 82, 45), "sienna" }, + { TQRGB(255,130, 71), "sienna1" }, + { TQRGB(238,121, 66), "sienna2" }, + { TQRGB(205,104, 57), "sienna3" }, + { TQRGB(139, 71, 38), "sienna4" }, + { TQRGB(135,206,235), "skyblue" }, + { TQRGB(135,206,255), "skyblue1" }, + { TQRGB(126,192,238), "skyblue2" }, + { TQRGB(108,166,205), "skyblue3" }, + { TQRGB( 74,112,139), "skyblue4" }, + { TQRGB(106, 90,205), "slateblue" }, + { TQRGB(131,111,255), "slateblue1" }, + { TQRGB(122,103,238), "slateblue2" }, + { TQRGB(105, 89,205), "slateblue3" }, + { TQRGB( 71, 60,139), "slateblue4" }, + { TQRGB(112,128,144), "slategray" }, + { TQRGB(198,226,255), "slategray1" }, + { TQRGB(185,211,238), "slategray2" }, + { TQRGB(159,182,205), "slategray3" }, + { TQRGB(108,123,139), "slategray4" }, + { TQRGB(112,128,144), "slategrey" }, + { TQRGB(255,250,250), "snow" }, + { TQRGB(255,250,250), "snow1" }, + { TQRGB(238,233,233), "snow2" }, + { TQRGB(205,201,201), "snow3" }, + { TQRGB(139,137,137), "snow4" }, + { TQRGB( 0,255,127), "springgreen" }, + { TQRGB( 0,255,127), "springgreen1" }, + { TQRGB( 0,238,118), "springgreen2" }, + { TQRGB( 0,205,102), "springgreen3" }, + { TQRGB( 0,139, 69), "springgreen4" }, + { TQRGB( 70,130,180), "steelblue" }, + { TQRGB( 99,184,255), "steelblue1" }, + { TQRGB( 92,172,238), "steelblue2" }, + { TQRGB( 79,148,205), "steelblue3" }, + { TQRGB( 54,100,139), "steelblue4" }, + { TQRGB(210,180,140), "tan" }, + { TQRGB(255,165, 79), "tan1" }, + { TQRGB(238,154, 73), "tan2" }, + { TQRGB(205,133, 63), "tan3" }, + { TQRGB(139, 90, 43), "tan4" }, + { TQRGB(216,191,216), "thistle" }, + { TQRGB(255,225,255), "thistle1" }, + { TQRGB(238,210,238), "thistle2" }, + { TQRGB(205,181,205), "thistle3" }, + { TQRGB(139,123,139), "thistle4" }, + { TQRGB(255, 99, 71), "tomato" }, + { TQRGB(255, 99, 71), "tomato1" }, + { TQRGB(238, 92, 66), "tomato2" }, + { TQRGB(205, 79, 57), "tomato3" }, + { TQRGB(139, 54, 38), "tomato4" }, + { TQRGB( 64,224,208), "turquoise" }, + { TQRGB( 0,245,255), "turquoise1" }, + { TQRGB( 0,229,238), "turquoise2" }, + { TQRGB( 0,197,205), "turquoise3" }, + { TQRGB( 0,134,139), "turquoise4" }, + { TQRGB(238,130,238), "violet" }, + { TQRGB(208, 32,144), "violetred" }, + { TQRGB(255, 62,150), "violetred1" }, + { TQRGB(238, 58,140), "violetred2" }, + { TQRGB(205, 50,120), "violetred3" }, + { TQRGB(139, 34, 82), "violetred4" }, + { TQRGB(245,222,179), "wheat" }, + { TQRGB(255,231,186), "wheat1" }, + { TQRGB(238,216,174), "wheat2" }, + { TQRGB(205,186,150), "wheat3" }, + { TQRGB(139,126,102), "wheat4" }, + { TQRGB(255,255,255), "white" }, + { TQRGB(245,245,245), "whitesmoke" }, + { TQRGB(255,255, 0), "yellow" }, + { TQRGB(255,255, 0), "yellow1" }, + { TQRGB(238,238, 0), "yellow2" }, + { TQRGB(205,205, 0), "yellow3" }, + { TQRGB(139,139, 0), "yellow4" }, + { TQRGB(154,205, 50), "yellowgreen" } }; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +#ifdef Q_OS_TEMP +static int __cdecl rgb_cmp( const void *d1, const void *d2 ) +#else +static int rgb_cmp( const void *d1, const void *d2 ) +#endif +{ + return qstricmp( ((RGBData *)d1)->name, ((RGBData *)d2)->name ); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + +bool qt_get_named_rgb( const char *name, TQRgb* rgb ) +{ + Q_LONG len = strlen(name)+1; + char *name_no_space = (char *)malloc(len); + for(Q_LONG o=0,i=0; i < len; i++) { + if(name[i] != '\t' && name[i] != ' ') + name_no_space[o++] = name[i]; + } + + RGBData x; + x.name = name_no_space; + // Funtion bsearch() is supposed to be + // void *bsearch(const void *key, const void *base, ... + // So why (char*)? Are there broken bsearch() declarations out there? + RGBData *r = (RGBData*)bsearch((char*)&x, (char*)rgbTbl, rgbTblSize, + sizeof(RGBData), rgb_cmp); + free(name_no_space); + if ( r ) { + *rgb = r->value; + return TRUE; + } else { + return FALSE; + } +} + +uint qt_get_rgb_val( const char *name ) +{ + TQRgb r = 0; + qt_get_named_rgb(name,&r); + return r; +} +#ifndef QT_NO_STRINGLIST +TQStringList TQColor::colorNames() +{ + int i = 0; + TQStringList lst; + for ( i = 0; i < rgbTblSize; i++ ) + lst << rgbTbl[i].name; + + return lst; +} +#endif +#else + +bool qt_get_named_rgb( const char *, TQRgb* ) +{ + return FALSE; +} + +uint qt_get_rgb_val( const char * ) +{ + return 0; +} +#ifndef QT_NO_STRINGLIST +TQStringList TQColor::colorNames() +{ + return TQStringList(); +} +#endif +#endif // QT_NO_COLORNAMES diff --git a/src/kernel/qcolor_p.h b/src/kernel/qcolor_p.h new file mode 100644 index 000000000..a849f6798 --- /dev/null +++ b/src/kernel/qcolor_p.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Named color support for non-X platforms. +** The color names have been borrowed from X. +** +** Created : 000228 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQCOLOR_P_H +#define TQCOLOR_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of qmenudata.cpp, qmenubar.cpp, qmenubar.cpp, qpopupmenu.cpp, +// qmotifstyle.cpp and qwindowssstyle.cpp. This header file may change +// from version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#endif // QT_H + +extern uint qt_get_rgb_val( const char *name ); +extern bool qt_get_named_rgb( const char *, TQRgb* ); +extern void qt_reset_color_avail(); + +#endif diff --git a/src/kernel/qcolor_x11.cpp b/src/kernel/qcolor_x11.cpp new file mode 100644 index 000000000..ffd8ae5b4 --- /dev/null +++ b/src/kernel/qcolor_x11.cpp @@ -0,0 +1,835 @@ +/**************************************************************************** +** +** Implementation of TQColor class for X11 +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qcolor.h" +#include "qcolor_p.h" +#include "string.h" +#include "qpaintdevice.h" +#include "qapplication.h" +#include "qapplication_p.h" +#include "qt_x11_p.h" + +// NOT REVISED + +/***************************************************************************** + The color dictionary speeds up color allocation significantly for X11. + When there are no more colors, TQColor::alloc() will set the colors_avail + flag to FALSE and try to find the nearest color. + NOTE: From deep within the event loop, the colors_avail flag is reset to + TRUE (calls the function qt_reset_color_avail()), because some other + application might free its colors, thereby making them available for + this TQt application. + *****************************************************************************/ + +#include "qintdict.h" + +struct TQColorData { + uint pix; // allocated pixel value + int context; // allocation context +}; + +typedef TQIntDict TQColorDict; +typedef TQIntDictIterator TQColorDictIt; +static int current_alloc_context = 0; // current color alloc context +static const uint col_std_dict = 419; +static const uint col_large_dict = 18397; + +class TQColorScreenData { +public: + TQColorScreenData() + { + colorDict = 0; + colors_avail = TRUE; + g_vis = 0; + g_carr = 0; + g_carr_fetch = TRUE; + g_cells = 0; + g_our_alloc = 0; + color_reduce = FALSE; + } + + TQColorDict *colorDict; // dict of allocated colors + bool colors_avail; // X colors available + bool g_truecolor; // truecolor visual + Visual *g_vis; // visual + XColor *g_carr; // color array + bool g_carr_fetch; // perform XQueryColors? + int g_cells; // number of entries in g_carr + bool *g_our_alloc; // our allocated colors + uint red_mask , green_mask , blue_mask; + int red_shift, green_shift, blue_shift; + bool color_reduce; + int col_div_r; + int col_div_g; + int col_div_b; +}; + +static int screencount = 0; +static TQColorScreenData **screendata = 0; // array of screendata pointers + + +/* + This function is called from the event loop. It resets the colors_avail + flag so that the application can retry to allocate read-only colors + that other applications may have deallocated lately. + + The g_our_alloc and g_carr are global arrays that optimize color + approximation when there are no more colors left to allocate. +*/ + +void qt_reset_color_avail() +{ + int i; + for ( i = 0; i < screencount; i++ ) { + screendata[i]->colors_avail = TRUE; + screendata[i]->g_carr_fetch = TRUE; // do XQueryColors if !colors_avail + } +} + + +/* + Finds the nearest color. +*/ + +static int find_nearest_color( int r, int g, int b, int* mindist_out, + TQColorScreenData *sd ) +{ + int mincol = -1; + int mindist = 200000; + int rx, gx, bx, dist; + XColor *xc = &sd->g_carr[0]; + for ( int i=0; ig_cells; i++ ) { + rx = r - (xc->red >> 8); + gx = g - (xc->green >> 8); + bx = b - (xc->blue>> 8); + dist = rx*rx + gx*gx + bx*bx; // calculate distance + if ( dist < mindist ) { // minimal? + mindist = dist; + mincol = i; + } + xc++; + } + *mindist_out = mindist; + return mincol; +} + + +/***************************************************************************** + TQColor misc internal functions + *****************************************************************************/ + +static int highest_bit( uint v ) +{ + int i; + uint b = (uint)1 << 31; // get pos of highest bit in v + for ( i=31; ((b & v) == 0) && i>=0; i-- ) + b >>= 1; + return i; +} + + +/***************************************************************************** + TQColor static member functions + *****************************************************************************/ + +/*! + Returns the maximum number of colors supported by the underlying + window system if the window system uses a palette. + + Otherwise returns -1. Use numBitPlanes() to calculate the available + colors in that case. +*/ + +int TQColor::maxColors() +{ + Visual *visual = (Visual *) TQPaintDevice::x11AppVisual(); + if (visual->c_class & 1) + return TQPaintDevice::x11AppCells(); + return -1; +} + +/*! + Returns the number of color bit planes for the underlying window + system. + + The returned value is equal to the default pixmap depth. + + \sa TQPixmap::defaultDepth() +*/ + +int TQColor::numBitPlanes() +{ + return TQPaintDevice::x11AppDepth(); +} + + +/*! + Internal initialization retquired for TQColor. + This function is called from the TQApplication constructor. + + \sa cleanup() +*/ + +void TQColor::initialize() +{ + static const int blackIdx = 2; + static const int whiteIdx = 3; + + if ( color_init ) // already initialized + return; + color_init = TRUE; + + Display *dpy = TQPaintDevice::x11AppDisplay(); + int spec = TQApplication::colorSpec(); + + screencount = ScreenCount( dpy ); + screendata = new TQColorScreenData*[ screencount ]; + + int scr; + for ( scr = 0; scr < screencount; ++scr ) { + screendata[scr] = new TQColorScreenData; + screendata[scr]->g_vis = (Visual *) TQPaintDevice::x11AppVisual( scr ); + screendata[scr]->g_truecolor = screendata[scr]->g_vis->c_class == TrueColor + || screendata[scr]->g_vis->c_class == DirectColor; + + int ncols = TQPaintDevice::x11AppCells( scr ); + + if ( screendata[scr]->g_truecolor ) { + if (scr == DefaultScreen(dpy)) + colormodel = d32; + } else { + if (scr == DefaultScreen(dpy)) + colormodel = d8; + + // Create the g_our_alloc array, which remembers which color pixels + // we allocated. + screendata[scr]->g_cells = TQMIN(ncols,256); + screendata[scr]->g_carr = new XColor[screendata[scr]->g_cells]; + Q_CHECK_PTR( screendata[scr]->g_carr ); + memset( screendata[scr]->g_carr, 0, + screendata[scr]->g_cells*sizeof(XColor) ); + screendata[scr]->g_carr_fetch = TRUE; // run XQueryColors on demand + screendata[scr]->g_our_alloc = new bool[screendata[scr]->g_cells]; + Q_CHECK_PTR( screendata[scr]->g_our_alloc ); + memset( screendata[scr]->g_our_alloc, FALSE, + screendata[scr]->g_cells*sizeof(bool) ); + XColor *xc = &screendata[scr]->g_carr[0]; + for ( int i=0; ig_cells; i++ ) { + xc->pixel = i; // g_carr[i] = color i + xc++; + } + } + + int dictsize; + if ( screendata[scr]->g_truecolor ) { // truecolor + dictsize = 1; // will not need color dict + screendata[scr]->red_mask = (uint)screendata[scr]->g_vis->red_mask; + screendata[scr]->green_mask = (uint)screendata[scr]->g_vis->green_mask; + screendata[scr]->blue_mask = (uint)screendata[scr]->g_vis->blue_mask; + screendata[scr]->red_shift = + highest_bit( screendata[scr]->red_mask ) - 7; + screendata[scr]->green_shift = + highest_bit( screendata[scr]->green_mask ) - 7; + screendata[scr]->blue_shift = + highest_bit( screendata[scr]->blue_mask ) - 7; + } else { + dictsize = col_std_dict; + } + screendata[scr]->colorDict = new TQColorDict(dictsize); // create dictionary + Q_CHECK_PTR( screendata[scr]->colorDict ); + + if ( spec == (int)TQApplication::ManyColor ) { + screendata[scr]->color_reduce = TRUE; + + switch ( qt_ncols_option ) { + case 216: + // 6:6:6 + screendata[scr]->col_div_r = screendata[scr]->col_div_g = + screendata[scr]->col_div_b = (255/(6-1)); + break; + default: { + // 2:3:1 proportions, solved numerically + if ( qt_ncols_option > 255 ) qt_ncols_option = 255; + if ( qt_ncols_option < 1 ) qt_ncols_option = 1; + int nr = 2; + int ng = 2; + int nb = 2; + for (;;) { + if ( nb*2 < nr && (nb+1)*nr*ng < qt_ncols_option ) + nb++; + else if ( nr*3 < ng*2 && nb*(nr+1)*ng < qt_ncols_option ) + nr++; + else if ( nb*nr*(ng+1) < qt_ncols_option ) + ng++; + else break; + } + qt_ncols_option = nr*ng*nb; + screendata[scr]->col_div_r = (255/(nr-1)); + screendata[scr]->col_div_g = (255/(ng-1)); + screendata[scr]->col_div_b = (255/(nb-1)); + } + } + } + } + + scr = TQPaintDevice::x11AppScreen(); + + // Initialize global color objects + if ( TQPaintDevice::x11AppDefaultVisual(scr) && + TQPaintDevice::x11AppDefaultColormap(scr) ) { + globalColors()[blackIdx].setPixel((uint) BlackPixel(dpy, scr)); + globalColors()[whiteIdx].setPixel((uint) WhitePixel(dpy, scr)); + } else { + globalColors()[blackIdx].alloc(scr); + globalColors()[whiteIdx].alloc(scr); + } + +#if 0 /* 0 == allocate colors on demand */ + setLazyAlloc( FALSE ); // allocate global colors + ((TQColor*)(&darkGray))-> alloc(); + ((TQColor*)(&gray))-> alloc(); + ((TQColor*)(&lightGray))-> alloc(); + ((TQColor*)(&::red))-> alloc(); + ((TQColor*)(&::green))-> alloc(); + ((TQColor*)(&::blue))-> alloc(); + ((TQColor*)(&cyan))-> alloc(); + ((TQColor*)(&magenta))-> alloc(); + ((TQColor*)(&yellow))-> alloc(); + ((TQColor*)(&darkRed))-> alloc(); + ((TQColor*)(&darkGreen))-> alloc(); + ((TQColor*)(&darkBlue))-> alloc(); + ((TQColor*)(&darkCyan))-> alloc(); + ((TQColor*)(&darkMagenta))-> alloc(); + ((TQColor*)(&darkYellow))-> alloc(); + setLazyAlloc( TRUE ); +#endif +} + +/*! + Internal clean up retquired for TQColor. + This function is called from the TQApplication destructor. + + \sa initialize() +*/ + +void TQColor::cleanup() +{ + if ( !color_init ) + return; + color_init = FALSE; + int scr; + for ( scr = 0; scr < screencount; scr++ ) { + if ( screendata[scr]->g_carr ) { + delete [] screendata[scr]->g_carr; + screendata[scr]->g_carr = 0; + } + if ( screendata[scr]->g_our_alloc ) { + delete [] screendata[scr]->g_our_alloc; + screendata[scr]->g_our_alloc = 0; + } + if ( screendata[scr]->colorDict ) { + screendata[scr]->colorDict->setAutoDelete( TRUE ); + screendata[scr]->colorDict->clear(); + delete screendata[scr]->colorDict; + screendata[scr]->colorDict = 0; + } + delete screendata[scr]; + screendata[scr] = 0; + } + delete [] screendata; + screendata = 0; + screencount = 0; +} + + +/***************************************************************************** + TQColor member functions + *****************************************************************************/ + +/*! + \internal + Allocates the color on screen \a screen. Only used in X11. + + \sa alloc(), pixel() +*/ +uint TQColor::alloc( int screen ) +{ + Display *dpy = TQPaintDevice::x11AppDisplay(); + if ( screen < 0 ) + screen = TQPaintDevice::x11AppScreen(); + if ( !color_init ) + return dpy ? (uint)BlackPixel(dpy, screen) : 0; + int r = qRed(d.argb); + int g = qGreen(d.argb); + int b = qBlue(d.argb); + uint pix = 0; + TQColorScreenData *sd = screendata[screen]; + if ( sd->g_truecolor ) { // truecolor: map to pixel + r = sd->red_shift > 0 ? r << sd->red_shift : r >> -sd->red_shift; + g = sd->green_shift > 0 ? g << sd->green_shift : g >> -sd->green_shift; + b = sd->blue_shift > 0 ? b << sd->blue_shift : b >> -sd->blue_shift; + pix = (b & sd->blue_mask) | (g & sd->green_mask) | (r & sd->red_mask) + | ~(sd->blue_mask | sd->green_mask | sd->red_mask); + if ( screen == TQPaintDevice::x11AppScreen() ) + d.d32.pix = pix; + return pix; + } + TQColorData *c = sd->colorDict->find( (long)(d.argb) ); + if ( c ) { // found color in dictionary + pix = c->pix; + if ( screen == TQPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; // color ok + d.d8.dirty = FALSE; + d.d8.pix = pix; // use same pixel value + if ( c->context != current_alloc_context ) { + c->context = 0; // convert to default context + sd->g_our_alloc[pix] = TRUE; // reuse without XAllocColor + } + } + return pix; + } + + XColor col; + col.red = r << 8; + col.green = g << 8; + col.blue = b << 8; + + bool try_again = FALSE; + bool try_alloc = !sd->color_reduce; + int try_count = 0; + + do { + // This loop is run until we manage to either allocate or + // find an approximate color, it stops after a few iterations. + + try_again = FALSE; + + if ( try_alloc && sd->colors_avail && + XAllocColor(dpy, TQPaintDevice::x11AppColormap( screen ),&col) ) { + // We could allocate the color + pix = (uint) col.pixel; + if ( screen == TQPaintDevice::x11AppScreen() ) { + d.d8.pix = pix; + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + sd->g_carr[d.d8.pix] = col; // update color array + if ( current_alloc_context == 0 ) + sd->g_our_alloc[d.d8.pix] = TRUE; // reuse without XAllocColor + } + } else { + // No available colors, or we did not want to allocate one + int i; + sd->colors_avail = FALSE; // no more available colors + if ( sd->g_carr_fetch ) { // refetch color array + sd->g_carr_fetch = FALSE; + XQueryColors( dpy, TQPaintDevice::x11AppColormap( screen ), sd->g_carr, + sd->g_cells ); + } + int mindist; + i = find_nearest_color( r, g, b, &mindist, sd ); + + if ( mindist != 0 && !try_alloc ) { + // Not an exact match with an existing color + int rr = ((r+sd->col_div_r/2)/sd->col_div_r)*sd->col_div_r; + int rg = ((g+sd->col_div_g/2)/sd->col_div_g)*sd->col_div_g; + int rb = ((b+sd->col_div_b/2)/sd->col_div_b)*sd->col_div_b; + int rx = rr - r; + int gx = rg - g; + int bx = rb - b; + int dist = rx*rx + gx*gx + bx*bx; // calculate distance + if ( dist < mindist ) { + // reduced color is closer - try to alloc it + r = rr; + g = rg; + b = rb; + col.red = r << 8; + col.green = g << 8; + col.blue = b << 8; + try_alloc = TRUE; + try_again = TRUE; + sd->colors_avail = TRUE; + continue; // Try alloc reduced color + } + } + + if ( i == -1 ) { // no nearest color?! + int unused, value; + hsv(&unused, &unused, &value); + if (value < 128) { // dark, use black + d.argb = qRgb(0,0,0); + pix = (uint)BlackPixel( dpy, screen ); + if ( screen == TQPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; + } + } else { // light, use white + d.argb = qRgb(0xff,0xff,0xff); + pix = (uint)WhitePixel( dpy, screen ); + if ( screen == TQPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; + } + } + return pix; + } + if ( sd->g_our_alloc[i] ) { // we've already allocated it + ; // i == g_carr[i].pixel + } else { + // Try to allocate existing color + col = sd->g_carr[i]; + if ( XAllocColor(dpy, TQPaintDevice::x11AppColormap( screen ), &col) ) { + i = (uint)col.pixel; + sd->g_carr[i] = col; // update color array + if ( screen == TQPaintDevice::x11AppScreen() ) { + if ( current_alloc_context == 0 ) + sd->g_our_alloc[i] = TRUE; // only in the default context + } + } else { + // Oops, it's gone again + try_count++; + try_again = TRUE; + sd->colors_avail = TRUE; + sd->g_carr_fetch = TRUE; + } + } + if ( !try_again ) { // got it + pix = (uint)sd->g_carr[i].pixel; + if ( screen == TQPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; // allocated X11 color + } + } + } + + } while ( try_again && try_count < 2 ); + + if ( try_again ) { // no hope of allocating color + int unused, value; + hsv(&unused, &unused, &value); + if (value < 128) { // dark, use black + d.argb = qRgb(0,0,0); + pix = (uint)BlackPixel( dpy, screen ); + if ( screen == TQPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; + } + } else { // light, use white + d.argb = qRgb(0xff,0xff,0xff); + pix = (uint)WhitePixel( dpy, screen ); + if ( screen == TQPaintDevice::x11AppScreen() ) { + d.d8.invalid = FALSE; + d.d8.dirty = FALSE; + d.d8.pix = pix; + } + } + return pix; + } + // All colors outside context 0 must go into the dictionary + bool many = sd->colorDict->count() >= sd->colorDict->size() * 8; + if ( many && sd->colorDict->size() == col_std_dict ) { + sd->colorDict->resize( col_large_dict ); + } + if ( !many || current_alloc_context != 0 ) { + c = new TQColorData; // insert into color dict + Q_CHECK_PTR( c ); + c->pix = pix; + c->context = current_alloc_context; + sd->colorDict->insert( (long)d.argb, c ); // store color in dict + } + return pix; +} + +/*! + Allocates the RGB color and returns the pixel value. + + Allocating a color means to obtain a pixel value from the RGB + specification. The pixel value is an index into the global color + table, but should be considered an arbitrary platform-dependent value. + + The pixel() function calls alloc() if necessary, so in general you + don't need to call this function. + + \sa enterAllocContext() +*/ +// ### 4.0 - remove me? +uint TQColor::alloc() +{ + return alloc( -1 ); +} + +/*! + \overload + + Returns the pixel value for screen \a screen. + + This value is used by the underlying window system to refer to a color. + It can be thought of as an index into the display hardware's color table, + but the value is an arbitrary 32-bit value. + + \sa alloc() +*/ +uint TQColor::pixel( int screen ) const +{ + if (screen != TQPaintDevice::x11AppScreen() && + // don't allocate color0 or color1, they have fixed pixel + // values for all screens + d.argb != qRgba(255, 255, 255, 1) && d.argb != qRgba(0, 0, 0, 1)) + return ((TQColor*)this)->alloc( screen ); + return pixel(); +} + + +void TQColor::setSystemNamedColor( const TQString& name ) +{ + // setSystemNamedColor should look up rgb values from the built in + // color tables first (see qcolor_p.cpp), and failing that, use + // the window system's interface for translating names to rgb values... + // we do this so that things like uic can load an XPM file with named colors + // and convert it to a png without having to use window system functions... + d.argb = qt_get_rgb_val( name.latin1() ); + TQRgb rgb; + if ( qt_get_named_rgb( name.latin1(), &rgb ) ) { + setRgb( qRed(rgb), qGreen(rgb), qBlue(rgb) ); + if ( colormodel == d8 ) { + d.d8.invalid = FALSE; + d.d8.dirty = TRUE; + d.d8.pix = 0; + } else { + alloc(); + } + } else if ( !color_init ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQColor::setSystemNamedColor: Cannot perform this operation " + "because TQApplication does not exist" ); +#endif + // set color to invalid + *this = TQColor(); + } else { + XColor col, hw_col; + if ( XLookupColor(TQPaintDevice::x11AppDisplay(), + TQPaintDevice::x11AppColormap(), name.latin1(), + &col, &hw_col) ) { + setRgb( col.red>>8, col.green>>8, col.blue>>8 ); + } else { + // set color to invalid + *this = TQColor(); + } + } +} + + +#define MAX_CONTEXTS 16 +static int context_stack[MAX_CONTEXTS]; +static int context_ptr = 0; + +static void init_context_stack() +{ + static bool did_init = FALSE; + if ( !did_init ) { + did_init = TRUE; + context_stack[0] = current_alloc_context = 0; + } +} + + +/*! + Enters a color allocation context and returns a non-zero unique + identifier. + + Color allocation contexts are useful for programs that need to + allocate many colors and throw them away later, like image + viewers. The allocation context functions work for true color + displays as well as for colormap displays, except that + TQColor::destroyAllocContext() does nothing for true color. + + Example: + \code + TQPixmap loadPixmap( TQString fileName ) + { + static int alloc_context = 0; + if ( alloc_context ) + TQColor::destroyAllocContext( alloc_context ); + alloc_context = TQColor::enterAllocContext(); + TQPixmap pm( fileName ); + TQColor::leaveAllocContext(); + return pm; + } + \endcode + + The example code loads a pixmap from file. It frees up all colors + that were allocated the last time loadPixmap() was called. + + The initial/default context is 0. TQt keeps a list of colors + associated with their allocation contexts. You can call + destroyAllocContext() to get rid of all colors that were allocated + in a specific context. + + Calling enterAllocContext() enters an allocation context. The + allocation context lasts until you call leaveAllocContext(). + TQColor has an internal stack of allocation contexts. Each call to + enterAllocContex() must have a corresponding leaveAllocContext(). + + \code + // context 0 active + int c1 = TQColor::enterAllocContext(); // enter context c1 + // context c1 active + int c2 = TQColor::enterAllocContext(); // enter context c2 + // context c2 active + TQColor::leaveAllocContext(); // leave context c2 + // context c1 active + TQColor::leaveAllocContext(); // leave context c1 + // context 0 active + // Now, free all colors that were allocated in context c2 + TQColor::destroyAllocContext( c2 ); + \endcode + + You may also want to set the application's color specification. + See TQApplication::setColorSpec() for more information. + + \sa leaveAllocContext(), currentAllocContext(), destroyAllocContext(), + TQApplication::setColorSpec() +*/ + +int TQColor::enterAllocContext() +{ + static int context_seq_no = 0; + init_context_stack(); + if ( context_ptr+1 == MAX_CONTEXTS ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQColor::enterAllocContext: Context stack overflow" ); +#endif + return 0; + } + current_alloc_context = context_stack[++context_ptr] = ++context_seq_no; + return current_alloc_context; +} + + +/*! + Leaves a color allocation context. + + See enterAllocContext() for a detailed explanation. + + \sa enterAllocContext(), currentAllocContext() +*/ + +void TQColor::leaveAllocContext() +{ + init_context_stack(); + if ( context_ptr == 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQColor::leaveAllocContext: Context stack underflow" ); +#endif + return; + } + current_alloc_context = context_stack[--context_ptr]; +} + + +/*! + Returns the current color allocation context. + + The default context is 0. + + \sa enterAllocContext(), leaveAllocContext() +*/ + +int TQColor::currentAllocContext() +{ + return current_alloc_context; +} + + +/*! + Destroys a color allocation context, \e context. + + This function deallocates all colors that were allocated in the + specified \a context. If \a context == -1, it frees up all colors + that the application has allocated. If \a context == -2, it frees + up all colors that the application has allocated, except those in + the default context. + + The function does nothing for true color displays. + + \sa enterAllocContext(), alloc() +*/ + +void TQColor::destroyAllocContext( int context ) +{ + init_context_stack(); + if ( !color_init ) + return; + + int screen; + for ( screen = 0; screen < screencount; ++screen ) { + if ( screendata[screen]->g_truecolor ) + continue; + + ulong pixels[256]; + bool freeing[256]; + memset( freeing, FALSE, screendata[screen]->g_cells*sizeof(bool) ); + TQColorData *d; + TQColorDictIt it( *screendata[screen]->colorDict ); + int i = 0; + uint rgbv; + while ( (d=it.current()) ) { + rgbv = (uint)it.currentKey(); + if ( (d->context || context==-1) && + (d->context == context || context < 0) ) { + if ( !screendata[screen]->g_our_alloc[d->pix] && !freeing[d->pix] ) { + // will free this color + pixels[i++] = d->pix; + freeing[d->pix] = TRUE; + } + // remove from dict + screendata[screen]->colorDict->remove( (long)rgbv ); + } + ++it; + } + if ( i ) + XFreeColors( TQPaintDevice::x11AppDisplay(), + TQPaintDevice::x11AppColormap( screen ), + pixels, i, 0 ); + } +} diff --git a/src/kernel/qconnection.cpp b/src/kernel/qconnection.cpp new file mode 100644 index 000000000..9d9aeaf49 --- /dev/null +++ b/src/kernel/qconnection.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Implementation of TQConnection class +** +** Created : 930417 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qconnection.h" + +/*! \class TQConnection qconnection.h + \brief The TQConnection class is an internal class, used in the signal/slot mechanism. + + \internal + + Do not use this class directly in application programs. + + TQObject has a list of TQConnection for each signal that is connected to the + outside world. +*/ + +TQConnection::TQConnection( const TQObject *object, int member, + const char *memberName, int memberType ) +{ + obj = (TQObject *)object; + mbr = member; + mbr_name = memberName; + mbr_type = memberType; + nargs = 0; + if ( strstr(memberName,"()") == 0 ) { + const char *p = memberName; + nargs++; + while ( *p ) { + if ( *p++ == ',' ) + nargs++; + } + } +} + +/*! + \fn TQConnection::~TQConnection() +*/ + +/*! + \fn bool TQConnection::isConnected() const +*/ + +/*! + \fn TQObject *TQConnection::object() const +*/ + +/*! + \fn int TQConnection::member() const +*/ + +/*! + \fn const char *TQConnection::memberName() const +*/ + +/*! + \fn int TQConnection::numArgs() const +*/ diff --git a/src/kernel/qconnection.h b/src/kernel/qconnection.h new file mode 100644 index 000000000..9a1f01cc7 --- /dev/null +++ b/src/kernel/qconnection.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Definition of TQConnection class +** +** Created : 930417 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQCONNECTION_H +#define TQCONNECTION_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +class Q_EXPORT TQConnection +{ +public: + TQConnection( const TQObject *, int, const char *memberName, int memberType ); + ~TQConnection() {} + + bool isConnected() const { return obj != 0; } + + TQObject *object() const { return obj; } // get object/member pointer + int member() const { return mbr; } + const char *memberName() const { return mbr_name; } + int memberType() const { return mbr_type; } + int numArgs() const { return nargs; } + +private: + TQObject *obj; // object connected to + int mbr; // member connected to + const char *mbr_name; + int mbr_type; + int nargs; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQConnection( const TQConnection & ); + TQConnection &operator=( const TQConnection & ); +#endif +}; + +#define Q_DEFINED_QCONNECTION +#include "qwinexport.h" +#endif // TQCONNECTION_H diff --git a/src/kernel/qcursor.cpp b/src/kernel/qcursor.cpp new file mode 100644 index 000000000..0b9b50dfc --- /dev/null +++ b/src/kernel/qcursor.cpp @@ -0,0 +1,287 @@ +/**************************************************************************** +** +** Implementation of TQCursor class +** +** Created : 940220 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qcursor.h" + +#ifndef QT_NO_CURSOR + +#include "qbitmap.h" +#include "qimage.h" +#include "qdatastream.h" + + +/*! + \class TQCursor qcursor.h + + \brief The TQCursor class provides a mouse cursor with an arbitrary + shape. + + \ingroup appearance + \ingroup shared + + This class is mainly used to create mouse cursors that are + associated with particular widgets and to get and set the position + of the mouse cursor. + + TQt has a number of standard cursor shapes, but you can also make + custom cursor shapes based on a TQBitmap, a mask and a hotspot. + + To associate a cursor with a widget, use TQWidget::setCursor(). To + associate a cursor with all widgets (normally for a short period + of time), use TQApplication::setOverrideCursor(). + + To set a cursor shape use TQCursor::setShape() or use the TQCursor + constructor which takes the shape as argument, or you can use one + of the predefined cursors defined in the \l CursorShape enum. + + If you want to create a cursor with your own bitmap, either use + the TQCursor constructor which takes a bitmap and a mask or the + constructor which takes a pixmap as arguments. + + To set or get the position of the mouse cursor use the static + methods TQCursor::pos() and TQCursor::setPos(). + + \img cursors.png Cursor Shapes + + \sa TQWidget \link guibooks.html#fowler GUI Design Handbook: + Cursors\endlink + + On X11, TQt supports the \link + http://www.xfree86.org/4.3.0/Xcursor.3.html Xcursor\endlink + library, which allows for full color icon themes. The table below + shows the cursor name used for each TQt::CursorShape value. If a + cursor cannot be found using the name shown below, a standard X11 + cursor will be used instead. Note: X11 does not provide + appropriate cursors for all possible TQt::CursorShape values. It + is possible that some cursors will be taken from the Xcursor + theme, while others will use an internal bitmap cursor. + + \table + \header \i TQt::CursorShape Values \i Cursor Names + \row \i TQt::ArrowCursor \i left_ptr + \row \i TQt::UpArrowCursor \i up_arrow + \row \i TQt::CrossCursor \i cross + \row \i TQt::WaitCursor \i wait + \row \i TQt::BusyCursor \i left_ptr_watch + \row \i TQt::IbeamCursor \i ibeam + \row \i TQt::SizeVerCursor \i size_ver + \row \i TQt::SizeHorCursor \i size_hor + \row \i TQt::SizeBDiagCursor \i size_bdiag + \row \i TQt::SizeFDiagCursor \i size_fdiag + \row \i TQt::SizeAllCursor \i size_all + \row \i TQt::SplitVCursor \i split_v + \row \i TQt::SplitHCursor \i split_h + \row \i TQt::PointingHandCursor \i pointing_hand + \row \i TQt::ForbiddenCursor \i forbidden + \row \i TQt::WhatsThisCursor \i whats_this + \endtable +*/ + +/*! + \enum TQt::CursorShape + + This enum type defines the various cursors that can be used. + + \value ArrowCursor standard arrow cursor + \value UpArrowCursor upwards arrow + \value CrossCursor crosshair + \value WaitCursor hourglass/watch + \value BusyCursor standard arrow with hourglass/watch + \value IbeamCursor ibeam/text entry + \value SizeVerCursor vertical resize + \value SizeHorCursor horizontal resize + \value SizeFDiagCursor diagonal resize (\) + \value SizeBDiagCursor diagonal resize (/) + \value SizeAllCursor all directions resize + \value BlankCursor blank/invisible cursor + \value SplitVCursor vertical splitting + \value SplitHCursor horizontal splitting + \value PointingHandCursor a pointing hand + \value ForbiddenCursor a slashed circle + \value WhatsThisCursor an arrow with a question mark + \value BitmapCursor + + ArrowCursor is the default for widgets in a normal state. + + \img cursors.png Cursor Shapes +*/ + +/***************************************************************************** + TQCursor stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM + + +/*! + \relates TQCursor + Writes the cursor \a c to the stream \a s. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQCursor &c ) +{ + s << (Q_INT16)c.shape(); // write shape id to stream + if ( c.shape() == TQt::BitmapCursor ) { // bitmap cursor +#if !defined(QT_NO_IMAGEIO) + s << *c.bitmap() << *c.mask(); + s << c.hotSpot(); +#else + qWarning("No Image Cursor I/O"); +#endif + } + return s; +} + +/*! + \relates TQCursor + Reads a cursor from the stream \a s and sets \a c to the read data. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQCursor &c ) +{ + Q_INT16 shape; + s >> shape; // read shape id from stream + if ( shape == TQt::BitmapCursor ) { // read bitmap cursor +#if !defined(QT_NO_IMAGEIO) + TQBitmap bm, bmm; + TQPoint hot; + s >> bm >> bmm >> hot; + c = TQCursor( bm, bmm, hot.x(), hot.y() ); +#else + qWarning("No Image Cursor I/O"); +#endif + } else { + c.setShape( (int)shape ); // create cursor with shape + } + return s; +} +#endif // QT_NO_DATASTREAM + + +/*! + Constructs a custom pixmap cursor. + + \a pixmap is the image. It is usual to give it a mask (set using + TQPixmap::setMask()). \a hotX and \a hotY define the cursor's hot + spot. + + If \a hotX is negative, it is set to the \c{pixmap().width()/2}. + If \a hotY is negative, it is set to the \c{pixmap().height()/2}. + + Valid cursor sizes depend on the display hardware (or the + underlying window system). We recommend using 32x32 cursors, + because this size is supported on all platforms. Some platforms + also support 16x16, 48x48 and 64x64 cursors. + + Currently, only black-and-white pixmaps can be used. + + \sa TQPixmap::TQPixmap(), TQPixmap::setMask() +*/ + +TQCursor::TQCursor( const TQPixmap &pixmap, int hotX, int hotY ) +{ + TQImage img = pixmap.convertToImage(). + convertDepth( 8, TQt::ThresholdDither|TQt::AvoidDither ); + TQBitmap bm; + bm.convertFromImage( img, TQt::ThresholdDither|TQt::AvoidDither ); + TQBitmap bmm; + if ( bm.mask() ) { + bmm = *bm.mask(); + TQBitmap nullBm; + bm.setMask( nullBm ); + } + else if ( pixmap.mask() ) { + TQImage mimg = pixmap.mask()->convertToImage(). + convertDepth( 8, TQt::ThresholdDither|TQt::AvoidDither ); + bmm.convertFromImage( mimg, TQt::ThresholdDither|TQt::AvoidDither ); + } + else { + bmm.resize( bm.size() ); + bmm.fill( TQt::color1 ); + } + + setBitmap(bm,bmm,hotX,hotY); +} + + + +/*! + Constructs a custom bitmap cursor. + + \a bitmap and + \a mask make up the bitmap. + \a hotX and + \a hotY define the cursor's hot spot. + + If \a hotX is negative, it is set to the \c{bitmap().width()/2}. + If \a hotY is negative, it is set to the \c{bitmap().height()/2}. + + The cursor \a bitmap (B) and \a mask (M) bits are combined like this: + \list + \i B=1 and M=1 gives black. + \i B=0 and M=1 gives white. + \i B=0 and M=0 gives transparent. + \i B=1 and M=0 gives an undefined result. + \endlist + + Use the global TQt color \c color0 to draw 0-pixels and \c color1 to + draw 1-pixels in the bitmaps. + + Valid cursor sizes depend on the display hardware (or the + underlying window system). We recommend using 32x32 cursors, + because this size is supported on all platforms. Some platforms + also support 16x16, 48x48 and 64x64 cursors. + + \sa TQBitmap::TQBitmap(), TQBitmap::setMask() +*/ + +TQCursor::TQCursor( const TQBitmap &bitmap, const TQBitmap &mask, + int hotX, int hotY ) +{ + setBitmap(bitmap,mask,hotX,hotY); +} + +#endif // QT_NO_CURSOR + + diff --git a/src/kernel/qcursor.h b/src/kernel/qcursor.h new file mode 100644 index 000000000..1ffae46c1 --- /dev/null +++ b/src/kernel/qcursor.h @@ -0,0 +1,153 @@ +/**************************************************************************** +** +** Definition of TQCursor class +** +** Created : 940219 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQCURSOR_H +#define TQCURSOR_H + +#ifndef QT_H +#include "qpoint.h" +#include "qshared.h" +#endif // QT_H + +/* + ### The fake cursor has to go first with old qdoc. +*/ +#ifdef QT_NO_CURSOR + +class Q_EXPORT TQCursor : public TQt +{ +public: + static TQPoint pos(); + static void setPos( int x, int y ); + static void setPos( const TQPoint & ); +private: + TQCursor(); +}; + +#endif // QT_NO_CURSOR + +#ifndef QT_NO_CURSOR + +struct TQCursorData; + + +class Q_EXPORT TQCursor : public TQt +{ +public: + TQCursor(); // create default arrow cursor + TQCursor( int shape ); + TQCursor( const TQBitmap &bitmap, const TQBitmap &mask, + int hotX=-1, int hotY=-1 ); + TQCursor( const TQPixmap &pixmap, + int hotX=-1, int hotY=-1 ); + TQCursor( const TQCursor & ); + ~TQCursor(); + TQCursor &operator=( const TQCursor & ); + + int shape() const; + void setShape( int ); + + const TQBitmap *bitmap() const; + const TQBitmap *mask() const; + TQPoint hotSpot() const; + +#if defined(Q_WS_WIN) + HCURSOR handle() const; + TQCursor( HCURSOR ); +#elif defined(Q_WS_X11) + HANDLE handle() const; + TQCursor( HANDLE ); +#elif defined(Q_WS_MAC) + HANDLE handle() const; +#elif defined(Q_WS_QWS) + HANDLE handle() const; +#endif + + static TQPoint pos(); + static void setPos( int x, int y ); + static void setPos( const TQPoint & ); + + static void initialize(); + static void cleanup(); + +#if defined(Q_WS_X11) + static int x11Screen(); +#endif +private: + void setBitmap( const TQBitmap &bitmap, const TQBitmap &mask, + int hotX, int hotY ); + void update() const; + TQCursorData *data; + TQCursor *find_cur(int); +#if defined(Q_WS_MAC) + friend void qt_mac_set_cursor(const TQCursor *c, const Point *p); +#endif +}; + + +#if !defined(QT_CLEAN_NAMESPACE) +// CursorShape is defined in X11/X.h +#ifdef CursorShape +#define X_CursorShape CursorShape +#undef CursorShape +#endif +typedef TQt::CursorShape TQCursorShape; +#ifdef X_CursorShape +#define CursorShape X_CursorShape +#endif +#endif + + +/***************************************************************************** + TQCursor stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQCursor & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQCursor & ); +#endif +#endif // QT_NO_CURSOR + + +inline void TQCursor::setPos( const TQPoint &p ) +{ + setPos( p.x(), p.y() ); +} + +#endif // TQCURSOR_H diff --git a/src/kernel/qcursor_x11.cpp b/src/kernel/qcursor_x11.cpp new file mode 100644 index 000000000..d0ba96e84 --- /dev/null +++ b/src/kernel/qcursor_x11.cpp @@ -0,0 +1,833 @@ +/**************************************************************************** +** +** Implementation of TQCursor class for X11 +** +** Created : 940219 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qcursor.h" +#include "qbitmap.h" +#include "qimage.h" +#include "qapplication.h" +#include "qdatastream.h" +#include "qnamespace.h" +#include "qt_x11_p.h" +#include + +#ifndef QT_NO_XCURSOR +# include +#endif // QT_NO_XCURSOR + +// Define QT_USE_APPROXIMATE_CURSORS when compiling if you REALLY want to +// use the ugly X11 cursors. + +/***************************************************************************** + Internal TQCursorData class + *****************************************************************************/ + +struct TQCursorData : public TQShared +{ + TQCursorData( int s = 0 ); + ~TQCursorData(); + int cshape; + TQBitmap *bm, *bmm; + short hx, hy; + XColor fg,bg; + Cursor hcurs; + Pixmap pm, pmm; +}; + +TQCursorData::TQCursorData( int s ) +{ + cshape = s; + hcurs = 0; + bm = bmm = 0; + hx = hy = 0; + pm = pmm = 0; +} + +TQCursorData::~TQCursorData() +{ + Display *dpy = TQPaintDevice::x11AppDisplay(); + + // Add in checking for the display too as on HP-UX + // we seem to get a core dump as the cursor data is + // deleted again from main() on exit... + if ( hcurs && dpy ) + XFreeCursor( dpy, hcurs ); + if ( pm && dpy ) + XFreePixmap( dpy, pm ); + if ( pmm && dpy ) + XFreePixmap( dpy, pmm ); + delete bm; + delete bmm; +} + + +/***************************************************************************** + Global cursors + *****************************************************************************/ + +static TQCursor cursorTable[TQt::LastCursor+1]; + +static const int arrowCursorIdx = 0; + +QT_STATIC_CONST_IMPL TQCursor & TQt::arrowCursor = cursorTable[0]; +QT_STATIC_CONST_IMPL TQCursor & TQt::upArrowCursor = cursorTable[1]; +QT_STATIC_CONST_IMPL TQCursor & TQt::crossCursor = cursorTable[2]; +QT_STATIC_CONST_IMPL TQCursor & TQt::waitCursor = cursorTable[3]; +QT_STATIC_CONST_IMPL TQCursor & TQt::ibeamCursor = cursorTable[4]; +QT_STATIC_CONST_IMPL TQCursor & TQt::sizeVerCursor = cursorTable[5]; +QT_STATIC_CONST_IMPL TQCursor & TQt::sizeHorCursor = cursorTable[6]; +QT_STATIC_CONST_IMPL TQCursor & TQt::sizeBDiagCursor = cursorTable[7]; +QT_STATIC_CONST_IMPL TQCursor & TQt::sizeFDiagCursor = cursorTable[8]; +QT_STATIC_CONST_IMPL TQCursor & TQt::sizeAllCursor = cursorTable[9]; +QT_STATIC_CONST_IMPL TQCursor & TQt::blankCursor = cursorTable[10]; +QT_STATIC_CONST_IMPL TQCursor & TQt::splitVCursor = cursorTable[11]; +QT_STATIC_CONST_IMPL TQCursor & TQt::splitHCursor = cursorTable[12]; +QT_STATIC_CONST_IMPL TQCursor & TQt::pointingHandCursor = cursorTable[13]; +QT_STATIC_CONST_IMPL TQCursor & TQt::forbiddenCursor = cursorTable[14]; +QT_STATIC_CONST_IMPL TQCursor & TQt::whatsThisCursor = cursorTable[15]; +QT_STATIC_CONST_IMPL TQCursor & TQt::busyCursor = cursorTable[16]; + + +TQCursor *TQCursor::find_cur( int shape ) // find predefined cursor +{ + return (uint)shape <= LastCursor ? &cursorTable[shape] : 0; +} + + +static bool initialized = FALSE; + +/*! + Internal function that deinitializes the predefined cursors. + This function is called from the TQApplication destructor. + + \sa initialize() +*/ +void TQCursor::cleanup() +{ + if ( !initialized ) + return; + + int shape; + for( shape = 0; shape <= LastCursor; shape++ ) { + if ( cursorTable[shape].data && cursorTable[shape].data->deref() ) + delete cursorTable[shape].data; + cursorTable[shape].data = 0; + } + initialized = FALSE; +} + + +/*! + Internal function that initializes the predefined cursors. + This function is called from the TQApplication constructor. + + \sa cleanup() +*/ + +void TQCursor::initialize() +{ + int shape; + for( shape = 0; shape <= LastCursor; shape++ ) + cursorTable[shape].data = new TQCursorData( shape ); + initialized = TRUE; + qAddPostRoutine( cleanup ); +} + + +/*! + Constructs a cursor with the default arrow shape. +*/ +TQCursor::TQCursor() +{ + if ( !initialized ) { + if ( qApp->startingUp() ) { + data = 0; + return; + } + initialize(); + } + TQCursor* c = &cursorTable[arrowCursorIdx]; + c->data->ref(); + data = c->data; +} + + + +/*! + Constructs a cursor with the specified \a shape. + + See \l CursorShape for a list of shapes. + + \sa setShape() +*/ + +TQCursor::TQCursor( int shape ) +{ + if ( !initialized ) + initialize(); + TQCursor *c = find_cur( shape ); + if ( !c ) // not found + c = &cursorTable[arrowCursorIdx]; // then use arrowCursor + c->data->ref(); + data = c->data; +} + +/*! + Constructs a cursor from the window system cursor \a cursor. + + \warning Using this function is not portable. This function is only + available on X11 and Windows. +*/ +TQCursor::TQCursor( HANDLE cursor ) +{ + if ( !initialized ) + initialize(); + + data = new TQCursorData; + Q_CHECK_PTR( data ); + data->hcurs = cursor; +} + + + +void TQCursor::setBitmap( const TQBitmap &bitmap, const TQBitmap &mask, + int hotX, int hotY ) +{ + if ( !initialized ) + initialize(); + if ( bitmap.depth() != 1 || mask.depth() != 1 || + bitmap.size() != mask.size() ) { +#if defined(QT_CHECK_NULL) + qWarning( "TQCursor: Cannot create bitmap cursor; invalid bitmap(s)" ); +#endif + TQCursor *c = &cursorTable[arrowCursorIdx]; + c->data->ref(); + data = c->data; + return; + } + data = new TQCursorData; + Q_CHECK_PTR( data ); + data->bm = new TQBitmap( bitmap ); + data->bmm = new TQBitmap( mask ); + data->hcurs = 0; + data->cshape = BitmapCursor; + data->hx = hotX >= 0 ? hotX : bitmap.width()/2; + data->hy = hotY >= 0 ? hotY : bitmap.height()/2; + data->fg.red = 0 << 8; + data->fg.green = 0 << 8; + data->fg.blue = 0 << 8; + data->bg.red = 255 << 8; + data->bg.green = 255 << 8; + data->bg.blue = 255 << 8; + update(); // Xcursor's backward compatibility hack needs the cursor to be created + // right after the bitmaps are created and filled with data +} + + +/*! + Constructs a copy of the cursor \a c. +*/ + +TQCursor::TQCursor( const TQCursor &c ) +{ + if ( !initialized ) + initialize(); + data = c.data; // shallow copy + data->ref(); +} + +/*! + Destroys the cursor. +*/ + +TQCursor::~TQCursor() +{ + if ( data && data->deref() ) + delete data; +} + + +/*! + Assigns \a c to this cursor and returns a reference to this + cursor. +*/ + +TQCursor &TQCursor::operator=( const TQCursor &c ) +{ + if ( !initialized ) + initialize(); + c.data->ref(); // avoid c = c + if ( data->deref() ) + delete data; + data = c.data; + return *this; +} + + +/*! + Returns the cursor shape identifier. The return value is one of + the \l CursorShape enum values (cast to an int). + + \sa setShape() +*/ + +int TQCursor::shape() const +{ + if ( !initialized ) + initialize(); + return data->cshape; +} + +/*! + Sets the cursor to the shape identified by \a shape. + + See \l CursorShape for the list of cursor shapes. + + \sa shape() +*/ + +void TQCursor::setShape( int shape ) +{ + if ( !initialized ) + initialize(); + TQCursor *c = find_cur( shape ); // find one of the global ones + if ( !c ) // not found + c = &cursorTable[arrowCursorIdx]; // then use arrowCursor + c->data->ref(); + if ( data->deref() ) // make shallow copy + delete data; + data = c->data; +} + + +/*! + Returns the cursor bitmap, or 0 if it is one of the standard + cursors. +*/ +const TQBitmap *TQCursor::bitmap() const +{ + if ( !initialized ) + initialize(); + return data->bm; +} + +/*! + Returns the cursor bitmap mask, or 0 if it is one of the standard + cursors. +*/ + +const TQBitmap *TQCursor::mask() const +{ + if ( !initialized ) + initialize(); + return data->bmm; +} + +/*! + Returns the cursor hot spot, or (0, 0) if it is one of the + standard cursors. +*/ + +TQPoint TQCursor::hotSpot() const +{ + if ( !initialized ) + initialize(); + return TQPoint( data->hx, data->hy ); +} + + +/*! + Returns the window system cursor handle. + + \warning + Portable in principle, but if you use it you are probably about to + do something non-portable. Be careful. +*/ + +TQt::HANDLE TQCursor::handle() const +{ + if ( !initialized ) + initialize(); + if ( !data->hcurs ) + update(); + return data->hcurs; +} + +/*! + \fn TQCursor::TQCursor( HCURSOR handle ) + + Creates a cursor with the specified window system handle \a + handle. + + \warning + Portable in principle, but if you use it you are probably about to + do something non-portable. Be careful. +*/ + +/*! + Returns the position of the cursor (hot spot) in global screen + coordinates. + + You can call TQWidget::mapFromGlobal() to translate it to widget + coordinates. + + \sa setPos(), TQWidget::mapFromGlobal(), TQWidget::mapToGlobal() +*/ +TQPoint TQCursor::pos() +{ + Window root; + Window child; + int root_x, root_y, win_x, win_y; + uint buttons; + Display* dpy = TQPaintDevice::x11AppDisplay(); + for ( int i = 0; i < ScreenCount( dpy ); i++ ) { + if ( XQueryPointer( dpy, TQPaintDevice::x11AppRootWindow( i ), &root, &child, + &root_x, &root_y, &win_x, &win_y, &buttons ) ) + + return TQPoint( root_x, root_y ); + } + return TQPoint(); +} + +/*! \internal +*/ +int TQCursor::x11Screen() +{ + Window root; + Window child; + int root_x, root_y, win_x, win_y; + uint buttons; + Display* dpy = TQPaintDevice::x11AppDisplay(); + for ( int i = 0; i < ScreenCount( dpy ); i++ ) { + if ( XQueryPointer( dpy, TQPaintDevice::x11AppRootWindow( i ), &root, &child, + &root_x, &root_y, &win_x, &win_y, &buttons ) ) + return i; + } + return -1; +} + +/*! + Moves the cursor (hot spot) to the global screen position (\a x, + \a y). + + You can call TQWidget::mapToGlobal() to translate widget + coordinates to global screen coordinates. + + \sa pos(), TQWidget::mapFromGlobal(), TQWidget::mapToGlobal() +*/ + +void TQCursor::setPos( int x, int y ) +{ + TQPoint current, target(x, y); + + // this is copied from pos(), since we need the screen number for the correct + // root window in the XWarpPointer call + Window root; + Window child; + int root_x, root_y, win_x, win_y; + uint buttons; + Display* dpy = TQPaintDevice::x11AppDisplay(); + int screen; + for ( screen = 0; screen < ScreenCount( dpy ); screen++ ) { + if ( XQueryPointer( dpy, TQPaintDevice::x11AppRootWindow( screen ), &root, &child, + &root_x, &root_y, &win_x, &win_y, &buttons ) ) { + current = TQPoint( root_x, root_y ); + break; + } + } + + if ( screen >= ScreenCount( dpy ) ) + return; + + // Need to check, since some X servers generate null mouse move + // events, causing looping in applications which call setPos() on + // every mouse move event. + // + if ( current == target ) + return; + + XWarpPointer( TQPaintDevice::x11AppDisplay(), None, + TQPaintDevice::x11AppRootWindow( screen ), + 0, 0, 0, 0, x, y ); +} + +/*! + \overload void TQCursor::setPos ( const TQPoint & ) +*/ + + +/*! + \internal + + Creates the cursor. +*/ + +void TQCursor::update() const +{ + if ( !initialized ) + initialize(); + register TQCursorData *d = data; // cheat const! + if ( d->hcurs ) // already loaded + return; + + Display *dpy = TQPaintDevice::x11AppDisplay(); + Window rootwin = TQPaintDevice::x11AppRootWindow(); + + if ( d->cshape == BitmapCursor ) { + d->hcurs = XCreatePixmapCursor( dpy, d->bm->handle(), d->bmm->handle(), + &d->fg, &d->bg, d->hx, d->hy ); + return; + } + +#ifndef QT_NO_XCURSOR + static const char *cursorNames[] = { + "left_ptr", + "up_arrow", + "cross", + "wait", + "ibeam", + "size_ver", + "size_hor", + "size_bdiag", + "size_fdiag", + "size_all", + "blank", + "split_v", + "split_h", + "pointing_hand", + "forbidden", + "whats_this", + "left_ptr_watch" + }; + + d->hcurs = XcursorLibraryLoadCursor( dpy, cursorNames[d->cshape] ); + if ( d->hcurs ) + return; +#endif // QT_NO_XCURSOR + + static uchar cur_blank_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 }; + + // Non-standard X11 cursors are created from bitmaps + +#ifndef QT_USE_APPROXIMATE_CURSORS + static const uchar cur_ver_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0xc0, 0x03, 0xe0, 0x07, 0xf0, 0x0f, + 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0xf0, 0x0f, + 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01, 0x00, 0x00 }; + static const uchar mcur_ver_bits[] = { + 0x00, 0x00, 0x80, 0x03, 0xc0, 0x07, 0xe0, 0x0f, 0xf0, 0x1f, 0xf8, 0x3f, + 0xfc, 0x7f, 0xc0, 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xfc, 0x7f, 0xf8, 0x3f, + 0xf0, 0x1f, 0xe0, 0x0f, 0xc0, 0x07, 0x80, 0x03 }; + static const uchar cur_hor_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x08, 0x30, 0x18, + 0x38, 0x38, 0xfc, 0x7f, 0xfc, 0x7f, 0x38, 0x38, 0x30, 0x18, 0x20, 0x08, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar mcur_hor_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x40, 0x04, 0x60, 0x0c, 0x70, 0x1c, 0x78, 0x3c, + 0xfc, 0x7f, 0xfe, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xfc, 0x7f, 0x78, 0x3c, + 0x70, 0x1c, 0x60, 0x0c, 0x40, 0x04, 0x00, 0x00 }; + static const uchar cur_bdiag_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x3e, 0x00, 0x3c, 0x00, 0x3e, + 0x00, 0x37, 0x88, 0x23, 0xd8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0xf8, 0x00, + 0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar mcur_bdiag_bits[] = { + 0x00, 0x00, 0xc0, 0x7f, 0x80, 0x7f, 0x00, 0x7f, 0x00, 0x7e, 0x04, 0x7f, + 0x8c, 0x7f, 0xdc, 0x77, 0xfc, 0x63, 0xfc, 0x41, 0xfc, 0x00, 0xfc, 0x01, + 0xfc, 0x03, 0xfc, 0x07, 0x00, 0x00, 0x00, 0x00 }; + static const uchar cur_fdiag_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, + 0xf8, 0x00, 0xd8, 0x01, 0x88, 0x23, 0x00, 0x37, 0x00, 0x3e, 0x00, 0x3c, + 0x00, 0x3e, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00 }; + static const uchar mcur_fdiag_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xfc, 0x03, 0xfc, 0x01, 0xfc, 0x00, + 0xfc, 0x41, 0xfc, 0x63, 0xdc, 0x77, 0x8c, 0x7f, 0x04, 0x7f, 0x00, 0x7e, + 0x00, 0x7f, 0x80, 0x7f, 0xc0, 0x7f, 0x00, 0x00 }; + static const uchar *cursor_bits16[] = { + cur_ver_bits, mcur_ver_bits, cur_hor_bits, mcur_hor_bits, + cur_bdiag_bits, mcur_bdiag_bits, cur_fdiag_bits, mcur_fdiag_bits, + 0, 0, cur_blank_bits, cur_blank_bits }; + + static const uchar vsplit_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, 0x80, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xe0, 0x03, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, + 0x00, 0xc0, 0x01, 0x00, 0x00, 0x80, 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 const uchar vsplitm_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0xc0, 0x01, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0xf0, 0x07, 0x00, + 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x01, 0x00, + 0x00, 0xc0, 0x01, 0x00, 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00, + 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0x00, + 0x80, 0xff, 0xff, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x01, 0x00, + 0x00, 0xc0, 0x01, 0x00, 0x00, 0xf8, 0x0f, 0x00, 0x00, 0xf0, 0x07, 0x00, + 0x00, 0xe0, 0x03, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar hsplit_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, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, + 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, + 0x00, 0x41, 0x82, 0x00, 0x80, 0x41, 0x82, 0x01, 0xc0, 0x7f, 0xfe, 0x03, + 0x80, 0x41, 0x82, 0x01, 0x00, 0x41, 0x82, 0x00, 0x00, 0x40, 0x02, 0x00, + 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, + 0x00, 0x40, 0x02, 0x00, 0x00, 0x40, 0x02, 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, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar hsplitm_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, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, + 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe2, 0x47, 0x00, 0x00, 0xe3, 0xc7, 0x00, + 0x80, 0xe3, 0xc7, 0x01, 0xc0, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0x07, + 0xc0, 0xff, 0xff, 0x03, 0x80, 0xe3, 0xc7, 0x01, 0x00, 0xe3, 0xc7, 0x00, + 0x00, 0xe2, 0x47, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, + 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 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, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar whatsthis_bits[] = { + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, 0xf0, 0x07, 0x00, + 0x09, 0x18, 0x0e, 0x00, 0x11, 0x1c, 0x0e, 0x00, 0x21, 0x1c, 0x0e, 0x00, + 0x41, 0x1c, 0x0e, 0x00, 0x81, 0x1c, 0x0e, 0x00, 0x01, 0x01, 0x07, 0x00, + 0x01, 0x82, 0x03, 0x00, 0xc1, 0xc7, 0x01, 0x00, 0x49, 0xc0, 0x01, 0x00, + 0x95, 0xc0, 0x01, 0x00, 0x93, 0xc0, 0x01, 0x00, 0x21, 0x01, 0x00, 0x00, + 0x20, 0xc1, 0x01, 0x00, 0x40, 0xc2, 0x01, 0x00, 0x40, 0x02, 0x00, 0x00, + 0x80, 0x01, 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, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; + static const uchar whatsthism_bits[] = { + 0x01, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x07, 0x00, 0x07, 0xf8, 0x0f, 0x00, + 0x0f, 0xfc, 0x1f, 0x00, 0x1f, 0x3e, 0x1f, 0x00, 0x3f, 0x3e, 0x1f, 0x00, + 0x7f, 0x3e, 0x1f, 0x00, 0xff, 0x3e, 0x1f, 0x00, 0xff, 0x9d, 0x0f, 0x00, + 0xff, 0xc3, 0x07, 0x00, 0xff, 0xe7, 0x03, 0x00, 0x7f, 0xe0, 0x03, 0x00, + 0xf7, 0xe0, 0x03, 0x00, 0xf3, 0xe0, 0x03, 0x00, 0xe1, 0xe1, 0x03, 0x00, + 0xe0, 0xe1, 0x03, 0x00, 0xc0, 0xe3, 0x03, 0x00, 0xc0, 0xe3, 0x03, 0x00, + 0x80, 0x01, 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, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; + static const uchar busy_bits[] = { + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, + 0x41, 0xe0, 0xff, 0x00, 0x81, 0x20, 0x80, 0x00, 0x01, 0xe1, 0xff, 0x00, + 0x01, 0x42, 0x40, 0x00, 0xc1, 0x47, 0x40, 0x00, 0x49, 0x40, 0x55, 0x00, + 0x95, 0x80, 0x2a, 0x00, 0x93, 0x00, 0x15, 0x00, 0x21, 0x01, 0x0a, 0x00, + 0x20, 0x01, 0x11, 0x00, 0x40, 0x82, 0x20, 0x00, 0x40, 0x42, 0x44, 0x00, + 0x80, 0x41, 0x4a, 0x00, 0x00, 0x40, 0x55, 0x00, 0x00, 0xe0, 0xff, 0x00, + 0x00, 0x20, 0x80, 0x00, 0x00, 0xe0, 0xff, 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, 0x00, 0x00, 0x00, 0x00, 0x00}; + static const uchar busym_bits[] = { + 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, + 0x0f, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, + 0x7f, 0xe0, 0xff, 0x00, 0xff, 0xe0, 0xff, 0x00, 0xff, 0xe1, 0xff, 0x00, + 0xff, 0xc3, 0x7f, 0x00, 0xff, 0xc7, 0x7f, 0x00, 0x7f, 0xc0, 0x7f, 0x00, + 0xf7, 0x80, 0x3f, 0x00, 0xf3, 0x00, 0x1f, 0x00, 0xe1, 0x01, 0x0e, 0x00, + 0xe0, 0x01, 0x1f, 0x00, 0xc0, 0x83, 0x3f, 0x00, 0xc0, 0xc3, 0x7f, 0x00, + 0x80, 0xc1, 0x7f, 0x00, 0x00, 0xc0, 0x7f, 0x00, 0x00, 0xe0, 0xff, 0x00, + 0x00, 0xe0, 0xff, 0x00, 0x00, 0xe0, 0xff, 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, 0x00, 0x00, 0x00, 0x00, 0x00}; + + static const uchar * const cursor_bits32[] = { + vsplit_bits, vsplitm_bits, hsplit_bits, hsplitm_bits, + 0, 0, 0, 0, whatsthis_bits, whatsthism_bits, busy_bits, busym_bits + }; + + static const uchar forbidden_bits[] = { + 0x00,0x00,0x00,0x80,0x1f,0x00,0xe0,0x7f,0x00,0xf0,0xf0,0x00,0x38,0xc0,0x01, + 0x7c,0x80,0x03,0xec,0x00,0x03,0xce,0x01,0x07,0x86,0x03,0x06,0x06,0x07,0x06, + 0x06,0x0e,0x06,0x06,0x1c,0x06,0x0e,0x38,0x07,0x0c,0x70,0x03,0x1c,0xe0,0x03, + 0x38,0xc0,0x01,0xf0,0xe0,0x00,0xe0,0x7f,0x00,0x80,0x1f,0x00,0x00,0x00,0x00 }; + + static const unsigned char forbiddenm_bits[] = { + 0x80,0x1f,0x00,0xe0,0x7f,0x00,0xf0,0xff,0x00,0xf8,0xff,0x01,0xfc,0xf0,0x03, + 0xfe,0xc0,0x07,0xfe,0x81,0x07,0xff,0x83,0x0f,0xcf,0x07,0x0f,0x8f,0x0f,0x0f, + 0x0f,0x1f,0x0f,0x0f,0x3e,0x0f,0x1f,0xfc,0x0f,0x1e,0xf8,0x07,0x3e,0xf0,0x07, + 0xfc,0xe0,0x03,0xf8,0xff,0x01,0xf0,0xff,0x00,0xe0,0x7f,0x00,0x80,0x1f,0x00}; + + static const uchar * const cursor_bits20[] = { + forbidden_bits, forbiddenm_bits + }; + + if ( d->cshape >= SizeVerCursor && d->cshape < SizeAllCursor || + d->cshape == BlankCursor ) { + XColor bg, fg; + bg.red = 255 << 8; + bg.green = 255 << 8; + bg.blue = 255 << 8; + fg.red = 0; + fg.green = 0; + fg.blue = 0; + int i = (d->cshape - SizeVerCursor)*2; + d->pm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits16[i], + 16, 16 ); + d->pmm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits16[i+1], + 16,16); + d->hcurs = XCreatePixmapCursor( dpy, d->pm, d->pmm, &fg, &bg, 8, 8 ); + return; + } + if ( ( d->cshape >= SplitVCursor && d->cshape <= SplitHCursor ) || + d->cshape == WhatsThisCursor || d->cshape == BusyCursor ) { + XColor bg, fg; + bg.red = 255 << 8; + bg.green = 255 << 8; + bg.blue = 255 << 8; + fg.red = 0; + fg.green = 0; + fg.blue = 0; + int i = (d->cshape - SplitVCursor)*2; + d->pm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits32[i], + 32, 32 ); + d->pmm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits32[i+1], + 32, 32); + int hs = ( d->cshape == PointingHandCursor || + d->cshape == WhatsThisCursor || + d->cshape == BusyCursor ) ? 0 : 16; + d->hcurs = XCreatePixmapCursor( dpy, d->pm, d->pmm, &fg, &bg, hs, hs ); + return; + } + if ( d->cshape == ForbiddenCursor ) { + XColor bg, fg; + bg.red = 255 << 8; + bg.green = 255 << 8; + bg.blue = 255 << 8; + fg.red = 0; + fg.green = 0; + fg.blue = 0; + int i = (d->cshape - ForbiddenCursor)*2; + d->pm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits20[i], + 20, 20 ); + d->pmm = XCreateBitmapFromData( dpy, rootwin, (char *)cursor_bits20[i+1], + 20, 20); + d->hcurs = XCreatePixmapCursor( dpy, d->pm, d->pmm, &fg, &bg, 10, 10 ); + return; + } +#endif /* ! QT_USE_APPROXIMATE_CURSORS */ + + uint sh; + switch ( d->cshape ) { // map Q cursor to X cursor + case ArrowCursor: + sh = XC_left_ptr; + break; + case UpArrowCursor: + sh = XC_center_ptr; + break; + case CrossCursor: + sh = XC_crosshair; + break; + case WaitCursor: + sh = XC_watch; + break; + case IbeamCursor: + sh = XC_xterm; + break; + case SizeAllCursor: + sh = XC_fleur; + break; + case PointingHandCursor: + sh = XC_hand2; + break; +#ifdef QT_USE_APPROXIMATE_CURSORS + case SizeBDiagCursor: + sh = XC_top_right_corner; + break; + case SizeFDiagCursor: + sh = XC_bottom_right_corner; + break; + case BlankCursor: + XColor bg, fg; + bg.red = 255 << 8; + bg.green = 255 << 8; + bg.blue = 255 << 8; + fg.red = 0; + fg.green = 0; + fg.blue = 0; + d->pm = XCreateBitmapFromData( dpy, rootwin, + (char *)cur_blank_bits, 16, 16 ); + d->pmm = XCreateBitmapFromData( dpy, rootwin, + (char *)cur_blank_bits, 16,16); + d->hcurs = XCreatePixmapCursor( dpy, d->pm, d->pmm, &fg, + &bg, 8, 8 ); + return; + break; + case SizeVerCursor: + case SplitVCursor: + sh = XC_sb_v_double_arrow; + break; + case SizeHorCursor: + case SplitHCursor: + sh = XC_sb_h_double_arrow; + break; + case WhatsThisCursor: + sh = XC_question_arrow; + break; + case ForbiddenCursor: + sh = XC_circle; + break; + case BusyCursor: + sh = XC_watch; + break; +#endif /* QT_USE_APPROXIMATE_CURSORS */ + default: +#if defined(QT_CHECK_RANGE) + qWarning( "TQCursor::update: Invalid cursor shape %d", d->cshape ); +#endif + return; + } + d->hcurs = XCreateFontCursor( dpy, sh ); +} diff --git a/src/kernel/qdesktopwidget.h b/src/kernel/qdesktopwidget.h new file mode 100644 index 000000000..8616a9f46 --- /dev/null +++ b/src/kernel/qdesktopwidget.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Definition of TQDesktopWidget class. +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQDESKTOPWIDGET_H +#define TQDESKTOPWIDGET_H + +#ifndef QT_H +#include "qwidget.h" +#endif // QT_H + +class TQApplication; +class TQDesktopWidgetPrivate; /* Don't touch! */ + +class Q_EXPORT TQDesktopWidget : public TQWidget +{ + Q_OBJECT +public: + TQDesktopWidget(); + ~TQDesktopWidget(); + + bool isVirtualDesktop() const; + + int numScreens() const; + int primaryScreen() const; + + int screenNumber( TQWidget *widget = 0 ) const; // ### 4.0: const TQWidget* + int screenNumber( const TQPoint & ) const; + + TQWidget *screen( int screen = -1 ); + + const TQRect& screenGeometry( int screen = -1 ) const; + const TQRect& screenGeometry( TQWidget *widget ) const + { return screenGeometry( screenNumber( widget ) ); } + const TQRect& screenGeometry( const TQPoint &point ) const + { return screenGeometry( screenNumber( point ) ); } + + const TQRect& availableGeometry( int screen = -1 ) const; + const TQRect& availableGeometry( TQWidget *widget ) const + { return availableGeometry( screenNumber( widget ) ); } + const TQRect& availableGeometry( const TQPoint &point ) const + { return availableGeometry( screenNumber( point ) ); } + + void insertChild( TQObject * ); + + inline void emitResizedSignal(int value) { emit resized(value); } + +signals: + void resized( int ); + void workAreaResized( int ); + +protected: + void resizeEvent( TQResizeEvent *e ); + +private: + TQDesktopWidgetPrivate *d; + +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQDesktopWidget( const TQDesktopWidget & ); + TQDesktopWidget &operator=( const TQDesktopWidget & ); +#endif + + friend class TQApplication; +#ifdef Q_WS_QWS + friend class TQWSDisplay; +#endif +}; + +#endif //TQDESKTOPWIDGET_H diff --git a/src/kernel/qdesktopwidget_x11.cpp b/src/kernel/qdesktopwidget_x11.cpp new file mode 100644 index 000000000..3672b8c1f --- /dev/null +++ b/src/kernel/qdesktopwidget_x11.cpp @@ -0,0 +1,356 @@ +/**************************************************************************** +** +** Implementation of TQDesktopWidget class. +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qdesktopwidget.h" +#include "qapplication.h" +#include "qobjectlist.h" +#include "qt_x11_p.h" +#include + +// defined in qwidget_x11.cpp +extern int qt_x11_create_desktop_on_screen; + +// defined in qapplication_x11.cpp +extern Atom qt_net_workarea; +extern bool qt_net_supports(Atom atom); + +// function to update the workarea of the screen +static bool qt_desktopwidget_workarea_dirty = TRUE; +void qt_desktopwidget_update_workarea() +{ + qt_desktopwidget_workarea_dirty = TRUE; +} + + +class TQSingleDesktopWidget : public TQWidget +{ +public: + TQSingleDesktopWidget(); + ~TQSingleDesktopWidget(); +}; + +TQSingleDesktopWidget::TQSingleDesktopWidget() + : TQWidget( 0, "desktop", WType_Desktop ) +{ +} + +TQSingleDesktopWidget::~TQSingleDesktopWidget() +{ + while ( children() ) + removeChild( children()->getFirst() ); +} + + +class TQDesktopWidgetPrivate +{ +public: + TQDesktopWidgetPrivate(); + ~TQDesktopWidgetPrivate(); + + void init(); + + bool use_xinerama; + int defaultScreen; + int screenCount; + + TQWidget **screens; + TQRect *rects; + TQRect *workareas; +}; + +TQDesktopWidgetPrivate::TQDesktopWidgetPrivate() + : use_xinerama(FALSE), defaultScreen(0), screenCount(1), + screens( 0 ), rects( 0 ), workareas( 0 ) +{ +} + +TQDesktopWidgetPrivate::~TQDesktopWidgetPrivate() +{ + if ( screens ) { + for ( int i = 0; i < screenCount; ++i ) { + if (i == defaultScreen) continue; + delete screens[i]; + screens[i] = 0; + } + + free(screens); + } + + if ( rects ) delete [] rects; + if ( workareas ) delete [] workareas; +} + +void TQDesktopWidgetPrivate::init() +{ + // get the screen count + int newScreenCount; + +#ifndef QT_NO_XINERAMA + XineramaScreenInfo *xinerama_screeninfo = 0; + int unused; + use_xinerama = (XineramaQueryExtension(TQPaintDevice::x11AppDisplay(), + &unused, &unused) && + XineramaIsActive(TQPaintDevice::x11AppDisplay())); + + if (use_xinerama) { + xinerama_screeninfo = + XineramaQueryScreens(TQPaintDevice::x11AppDisplay(), &newScreenCount); + + if (xinerama_screeninfo) + defaultScreen = 0; + } else +#endif // QT_NO_XINERAMA + { + defaultScreen = DefaultScreen(TQPaintDevice::x11AppDisplay()); + newScreenCount = ScreenCount(TQPaintDevice::x11AppDisplay()); + use_xinerama = false; + } + + delete [] rects; + rects = new TQRect[ newScreenCount ]; + delete [] workareas; + workareas = new TQRect[ newScreenCount ]; + + // get the geometry of each screen + int i, j, x, y, w, h; + for ( i = 0, j = 0; i < newScreenCount; i++ ) { + +#ifndef QT_NO_XINERAMA + if (use_xinerama) { + x = xinerama_screeninfo[i].x_org; + y = xinerama_screeninfo[i].y_org; + w = xinerama_screeninfo[i].width; + h = xinerama_screeninfo[i].height; + } else +#endif // QT_NO_XINERAMA + { + x = 0; + y = 0; + w = WidthOfScreen(ScreenOfDisplay(TQPaintDevice::x11AppDisplay(), i)); + h = HeightOfScreen(ScreenOfDisplay(TQPaintDevice::x11AppDisplay(), i)); + } + + workareas[i] = TQRect(); + rects[j].setRect(x, y, w, h); + + // overlapping? + if (j > 0 && rects[j-1].intersects(rects[j])) { + // pick the bigger one, ignore the other + if ((rects[j].width()*rects[j].height()) > + (rects[j-1].width()*rects[j-1].height())) + rects[j-1] = rects[j]; + } + else + j++; + } + + if (screens) { + // leaks TQWidget* pointers on purpose, can't delete them as pointer escapes + screens = (TQWidget**) realloc(screens, j * sizeof(TQWidget*)); + if (j > screenCount) + memset(&screens[screenCount], 0, (j-screenCount) * sizeof(TQWidget*)); + } + + screenCount = j; + +#ifndef QT_NO_XINERAMA + if (use_xinerama && screenCount == 1) + use_xinerama = false; + + if (xinerama_screeninfo) + XFree(xinerama_screeninfo); +#endif // QT_NO_XINERAMA + +} + +// the TQDesktopWidget itself will be created on the default screen +// as qt_x11_create_desktop_on_screen defaults to -1 +TQDesktopWidget::TQDesktopWidget() + : TQWidget( 0, "desktop", WType_Desktop ) +{ + d = new TQDesktopWidgetPrivate(); + + /* + we don't call d->init() here, since the initial resize event + will end up calling init() a second time, which is inefficient. + instead, for the sending of all posted event to the desktop + widget (including the initial resize event, which calls + d->init()). + */ + TQApplication::sendPostedEvents( this, 0 ); +} + +TQDesktopWidget::~TQDesktopWidget() +{ + delete d; +} + +bool TQDesktopWidget::isVirtualDesktop() const +{ + return d->use_xinerama; +} + +int TQDesktopWidget::primaryScreen() const +{ + return d->defaultScreen; +} + +int TQDesktopWidget::numScreens() const +{ + return d->screenCount; +} + +TQWidget *TQDesktopWidget::screen( int screen ) +{ + if (d->use_xinerama) + return this; + + if ( screen < 0 || screen >= d->screenCount ) + screen = d->defaultScreen; + + if ( ! d->screens ) { + d->screens = (TQWidget**) calloc( d->screenCount, sizeof(TQWidget*)); + d->screens[ d->defaultScreen ] = this; + } + + if ( ! d->screens[screen] || // not created yet + ! d->screens[screen]->isDesktop() ) { // reparented away + qt_x11_create_desktop_on_screen = screen; + d->screens[screen] = new TQSingleDesktopWidget; + qt_x11_create_desktop_on_screen = -1; + } + + return d->screens[screen]; +} + +const TQRect& TQDesktopWidget::availableGeometry( int screen ) const +{ + if ( qt_desktopwidget_workarea_dirty ) { + // the workareas are dirty, invalidate them + for ( int i = 0; i < d->screenCount; ++i ) + d->workareas[i] = TQRect(); + qt_desktopwidget_workarea_dirty = FALSE; + } + + if ( screen < 0 || screen >= d->screenCount ) + screen = d->defaultScreen; + + if ( d->workareas[screen].isValid() ) + return d->workareas[screen]; + + if ( ! isVirtualDesktop() && qt_net_supports( qt_net_workarea ) ) { + Atom ret; + int format, e; + unsigned char *data = 0; + unsigned long nitems, after; + + e = XGetWindowProperty( TQPaintDevice::x11AppDisplay(), + TQPaintDevice::x11AppRootWindow( screen ), + qt_net_workarea, 0, 4, False, XA_CARDINAL, + &ret, &format, &nitems, &after, &data ); + + if (e == Success && ret == XA_CARDINAL && + format == 32 && nitems == 4) { + long *workarea = (long *) data; + d->workareas[screen].setRect( workarea[0], workarea[1], + workarea[2], workarea[3] ); + } else { + d->workareas[screen] = screenGeometry(screen); + } + if ( data ) + XFree( data ); + } else { + d->workareas[screen] = screenGeometry(screen); + } + + return d->workareas[screen]; +} + +const TQRect& TQDesktopWidget::screenGeometry( int screen ) const +{ + if ( screen < 0 || screen >= d->screenCount ) + screen = d->defaultScreen; + + return d->rects[ screen ]; +} + +int TQDesktopWidget::screenNumber( TQWidget *widget ) const +{ + if ( !widget ) + return d->defaultScreen; + +#ifndef QT_NO_XINERAMA + if (d->use_xinerama) { + // this is how we do it for xinerama + TQRect frame = widget->frameGeometry(); + if ( !widget->isTopLevel() ) + frame.moveTopLeft( widget->mapToGlobal( TQPoint( 0, 0 ) ) ); + + int maxSize = -1; + int maxScreen = -1; + + for ( int i = 0; i < d->screenCount; ++i ) { + TQRect sect = d->rects[i].intersect( frame ); + int size = sect.width() * sect.height(); + if ( size > maxSize && sect.width() > 0 && sect.height() > 0 ) { + maxSize = size; + maxScreen = i; + } + } + return maxScreen; + } +#endif // QT_NO_XINERAMA + + return widget->x11Screen(); +} + +int TQDesktopWidget::screenNumber( const TQPoint &point ) const +{ + for ( int i = 0; i < d->screenCount; ++i ) { + if ( d->rects[i].contains( point ) ) + return i; + } + return -1; +} + +void TQDesktopWidget::resizeEvent( TQResizeEvent *event ) +{ + d->init(); + qt_desktopwidget_workarea_dirty = TRUE; + TQWidget::resizeEvent( event ); +} diff --git a/src/kernel/qdnd_x11.cpp b/src/kernel/qdnd_x11.cpp new file mode 100644 index 000000000..288bfa880 --- /dev/null +++ b/src/kernel/qdnd_x11.cpp @@ -0,0 +1,1859 @@ +/**************************************************************************** +** +** XDND implementation for TQt. See http://www.cco.caltech.edu/~jafl/xdnd/ +** +** Created : 980320 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qplatformdefs.h" + +#include "qapplication.h" + +#ifndef QT_NO_DRAGANDDROP + +#include "qwidget.h" +#include "qintdict.h" +#include "qdatetime.h" +#include "qdict.h" +#include "qguardedptr.h" +#include "qdragobject.h" +#include "qobjectlist.h" +#include "qcursor.h" +#include "qbitmap.h" +#include "qpainter.h" + +#include "qt_x11_p.h" + +// conflict resolution + +const int XKeyPress = KeyPress; +const int XKeyRelease = KeyRelease; +#undef KeyPress +#undef KeyRelease + +// this stuff is copied from qapp_x11.cpp + +extern void qt_x11_intern_atom( const char *, Atom * ); + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +extern void qt_ignore_badwindow(); +extern bool qt_badwindow(); +extern void qt_enter_modal( TQWidget *widget ); +extern void qt_leave_modal( TQWidget *widget ); + +#if defined(Q_C_CALLBACKS) +} +#endif + +extern Window qt_x11_findClientWindow( Window, Atom, bool ); +extern Atom qt_wm_state; +extern Time qt_x_time; +extern Time qt_x_user_time; + +// this stuff is copied from qclb_x11.cpp + +extern bool qt_xclb_wait_for_event( Display *dpy, Window win, int type, + XEvent *event, int timeout ); +extern bool qt_xclb_read_property( Display *dpy, Window win, Atom property, + bool deleteProperty, + TQByteArray *buffer, int *size, Atom *type, + int *format, bool nullterm ); +extern TQByteArray qt_xclb_read_incremental_property( Display *dpy, Window win, + Atom property, + int nbytes, bool nullterm ); +// and all this stuff is copied -into- qapp_x11.cpp + +void qt_xdnd_setup(); +void qt_handle_xdnd_enter( TQWidget *, const XEvent *, bool ); +void qt_handle_xdnd_position( TQWidget *, const XEvent *, bool ); +void qt_handle_xdnd_status( TQWidget *, const XEvent *, bool ); +void qt_handle_xdnd_leave( TQWidget *, const XEvent *, bool ); +void qt_handle_xdnd_drop( TQWidget *, const XEvent *, bool ); +void qt_handle_xdnd_finished( TQWidget *, const XEvent *, bool ); +void qt_xdnd_handle_selection_request( const XSelectionRequestEvent * ); +bool qt_xdnd_handle_badwindow(); +// client messages +Atom qt_xdnd_enter; +Atom qt_xdnd_position; +Atom qt_xdnd_status; +Atom qt_xdnd_leave; +Atom qt_xdnd_drop; +Atom qt_xdnd_finished; +Atom qt_xdnd_type_list; +const int qt_xdnd_version = 4; + +extern int qt_x11_translateButtonState( int s ); + +// Actions +// +// The Xdnd spec allows for user-defined actions. This could be implemented +// with a registration process in TQt. WE SHOULD do that later. +// +Atom qt_xdnd_action_copy; +Atom qt_xdnd_action_link; +Atom qt_xdnd_action_move; +Atom qt_xdnd_action_private; +static +TQDropEvent::Action xdndaction_to_qtaction(Atom atom) +{ + if ( atom == qt_xdnd_action_copy || atom == 0 ) + return TQDropEvent::Copy; + if ( atom == qt_xdnd_action_link ) + return TQDropEvent::Link; + if ( atom == qt_xdnd_action_move ) + return TQDropEvent::Move; + return TQDropEvent::Private; +} +static +int qtaction_to_xdndaction(TQDropEvent::Action a) +{ + switch ( a ) { + case TQDropEvent::Copy: + return qt_xdnd_action_copy; + case TQDropEvent::Link: + return qt_xdnd_action_link; + case TQDropEvent::Move: + return qt_xdnd_action_move; + case TQDropEvent::Private: + return qt_xdnd_action_private; + default: + return qt_xdnd_action_copy; + } +} + +// clean up the stuff used. +static void qt_xdnd_cleanup(); + +static void qt_xdnd_send_leave(); + +// XDND selection +Atom qt_xdnd_selection; +// other selection +static Atom qt_selection_property; +// INCR +static Atom qt_incr_atom; + +// properties for XDND drop sites +Atom qt_xdnd_aware; +Atom qt_xdnd_proxy; + +// real variables: +// xid of current drag source +static Atom qt_xdnd_dragsource_xid = 0; + +// the types in this drop. 100 is no good, but at least it's big. +const int qt_xdnd_max_type = 100; +static Atom qt_xdnd_types[qt_xdnd_max_type]; + +static TQIntDict * qt_xdnd_drag_types = 0; +static TQDict * qt_xdnd_atom_numbers = 0; + +// timer used when target wants "continuous" move messages (eg. scroll) +static int heartbeat = -1; +// rectangle in which the answer will be the same +static TQRect qt_xdnd_source_sameanswer; +//static TQRect qt_xdnd_target_sameanswer; +static bool qt_xdnd_target_answerwas; +// top-level window we sent position to last. +static Window qt_xdnd_current_target; +// window to send events to (always valid if qt_xdnd_current_target) +static Window qt_xdnd_current_proxy_target; +// widget we forwarded position to last, and local position +static TQGuardedPtr qt_xdnd_current_widget; +static TQPoint qt_xdnd_current_position; +// time of this drop, as type Atom to save on casts +static Atom qt_xdnd_source_current_time; +// timestamp from the XdndPosition and XdndDrop +static Time qt_xdnd_target_current_time; +// screen number containing the pointer... -1 means default +static int qt_xdnd_current_screen = -1; +// state of dragging... true if dragging, false if not +bool qt_xdnd_dragging = FALSE; +// need to check state of keyboard modifiers +static bool need_modifiers_check = FALSE; + +// dict of payload data, sorted by type atom +static TQIntDict * qt_xdnd_target_data = 0; + +// first drag object, or 0 +static TQDragObject * qt_xdnd_source_object = 0; + +// Motif dnd +extern void qt_motifdnd_enable( TQWidget *, bool ); +extern TQByteArray qt_motifdnd_obtain_data( const char *format ); +extern const char *qt_motifdnd_format( int n ); + +bool qt_motifdnd_active = FALSE; +static bool dndCancelled = FALSE; + +// Shift/Ctrl handling, and final drop status +static TQDragObject::DragMode drag_mode; +static TQDropEvent::Action global_requested_action = TQDropEvent::Copy; +static TQDropEvent::Action global_accepted_action = TQDropEvent::Copy; + +// for embedding only +static TQWidget* current_embedding_widget = 0; +static XEvent last_enter_event; + +// cursors +static TQCursor *noDropCursor = 0; +static TQCursor *moveCursor = 0; +static TQCursor *copyCursor = 0; +static TQCursor *linkCursor = 0; + +static TQPixmap *defaultPm = 0; + +static const int default_pm_hotx = -2; +static const int default_pm_hoty = -16; +static const char* const default_pm[] = { +"13 9 3 1", +". c None", +" c #000000", +"X c #FFFFFF", +"X X X X X X X", +" X X X X X X ", +"X ......... X", +" X.........X ", +"X ......... X", +" X.........X ", +"X ......... X", +" X X X X X X ", +"X X X X X X X" +}; + +class TQShapedPixmapWidget : public TQWidget { + +public: + TQShapedPixmapWidget(int screen = -1) : + TQWidget(TQApplication::desktop()->screen( screen ), + 0, WStyle_Customize | WStyle_Tool | WStyle_NoBorder | WX11BypassWM ), oldpmser( 0 ), oldbmser( 0 ) + { + x11SetWindowType( X11WindowTypeDND ); + } + + void setPixmap(TQPixmap pm, TQPoint hot) + { + int bmser = pm.mask() ? pm.mask()->serialNumber() : 0; + if( oldpmser == pm.serialNumber() && oldbmser == bmser + && oldhot == hot ) + return; + oldpmser = pm.serialNumber(); + oldbmser = bmser; + oldhot = hot; + bool hotspot_in = !(hot.x() < 0 || hot.y() < 0 || hot.x() >= pm.width() || hot.y() >= pm.height()); +// if the pixmap has hotspot in its area, make a "hole" in it at that position +// this will allow XTranslateCoordinates() to find directly the window below the cursor instead +// of finding this pixmap, and therefore there won't be needed any (slow) search for the window +// using findRealWindow() + if( hotspot_in ) { + TQBitmap mask = pm.mask() ? *pm.mask() : TQBitmap( pm.width(), pm.height()); + if( !pm.mask()) + mask.fill( TQt::color1 ); + TQPainter p( &mask ); + p.setPen( TQt::color0 ); + p.drawPoint( hot.x(), hot.y()); + p.end(); + pm.setMask( mask ); + setMask( mask ); + } else if ( pm.mask() ) { + setMask( *pm.mask() ); + } else { + clearMask(); + } + resize(pm.width(),pm.height()); + setErasePixmap(pm); + erase(); + } +private: + int oldpmser; + int oldbmser; + TQPoint oldhot; +}; + +static TQShapedPixmapWidget * qt_xdnd_deco = 0; + +static TQWidget* desktop_proxy = 0; + +class TQExtraWidget : public TQWidget +{ +public: + TQWExtra* extraData() { return TQWidget::extraData(); } + TQTLWExtra* topData() { return TQWidget::topData(); } +}; + + +static bool qt_xdnd_enable( TQWidget* w, bool on ) +{ + if ( on ) { + TQWidget * xdnd_widget = 0; + if ( w->isDesktop() ) { + if ( desktop_proxy ) // *WE* already have one. + return FALSE; + + // As per Xdnd4, use XdndProxy + XGrabServer( w->x11Display() ); + Atom type = None; + int f; + unsigned long n, a; + WId *proxy_id_ptr; + XGetWindowProperty( w->x11Display(), w->winId(), + qt_xdnd_proxy, 0, 1, False, + XA_WINDOW, &type, &f,&n,&a,(uchar**)&proxy_id_ptr ); + WId proxy_id = 0; + if ( type == XA_WINDOW && proxy_id_ptr ) { + proxy_id = *proxy_id_ptr; + XFree(proxy_id_ptr); + proxy_id_ptr = 0; + // Already exists. Real? + qt_ignore_badwindow(); + XGetWindowProperty( w->x11Display(), proxy_id, + qt_xdnd_proxy, 0, 1, False, + XA_WINDOW, &type, &f,&n,&a,(uchar**)&proxy_id_ptr ); + if ( qt_badwindow() || type != XA_WINDOW || !proxy_id_ptr || *proxy_id_ptr != proxy_id ) { + // Bogus - we will overwrite. + proxy_id = 0; + } + } + if ( proxy_id_ptr ) + XFree(proxy_id_ptr); + + if ( !proxy_id ) { + xdnd_widget = desktop_proxy = new TQWidget; + proxy_id = desktop_proxy->winId(); + XChangeProperty ( w->x11Display(), + w->winId(), qt_xdnd_proxy, + XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&proxy_id, 1 ); + XChangeProperty ( w->x11Display(), + proxy_id, qt_xdnd_proxy, + XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&proxy_id, 1 ); + } + + XUngrabServer( w->x11Display() ); + } else { + xdnd_widget = w->topLevelWidget(); + } + if ( xdnd_widget ) { + Atom atm = (Atom)qt_xdnd_version; + XChangeProperty ( xdnd_widget->x11Display(), xdnd_widget->winId(), + qt_xdnd_aware, XA_ATOM, 32, PropModeReplace, + (unsigned char *)&atm, 1 ); + return TRUE; + } else { + return FALSE; + } + } else { + if ( w->isDesktop() ) { + XDeleteProperty( w->x11Display(), w->winId(), + qt_xdnd_proxy ); + delete desktop_proxy; + desktop_proxy = 0; + } + return TRUE; + } +} + +const char* qt_xdnd_atom_to_str( Atom a ) +{ + if ( !a ) return 0; + + if ( a == XA_STRING ) + return "text/plain"; // some Xdnd clients are dumb + + if ( !qt_xdnd_drag_types ) { + qt_xdnd_drag_types = new TQIntDict( 17 ); + qt_xdnd_drag_types->setAutoDelete( TRUE ); + } + TQCString* result; + if ( !(result=qt_xdnd_drag_types->find( a )) ) { + const char* mimeType = XGetAtomName( TQPaintDevice::x11AppDisplay(), a ); + if ( !mimeType ) + return 0; // only happens on protocol error + result = new TQCString( mimeType ); + qt_xdnd_drag_types->insert( (long)a, result ); + XFree((void*)mimeType); + } + return *result; +} + +Atom* qt_xdnd_str_to_atom( const char *mimeType ) +{ + if ( !mimeType || !*mimeType ) + return 0; + if ( !qt_xdnd_atom_numbers ) { + qt_xdnd_atom_numbers = new TQDict( 17 ); + qt_xdnd_atom_numbers->setAutoDelete( TRUE ); + } + + Atom * result; + if ( (result = qt_xdnd_atom_numbers->find( mimeType )) ) + return result; + + result = new Atom; + *result = 0; + qt_x11_intern_atom( mimeType, result ); + qt_xdnd_atom_numbers->insert( mimeType, result ); + qt_xdnd_atom_to_str( *result ); + + return result; +} + + +void qt_xdnd_setup() { + // set up protocol atoms + qt_x11_intern_atom( "XdndEnter", &qt_xdnd_enter ); + qt_x11_intern_atom( "XdndPosition", &qt_xdnd_position ); + qt_x11_intern_atom( "XdndStatus", &qt_xdnd_status ); + qt_x11_intern_atom( "XdndLeave", &qt_xdnd_leave ); + qt_x11_intern_atom( "XdndDrop", &qt_xdnd_drop ); + qt_x11_intern_atom( "XdndFinished", &qt_xdnd_finished ); + qt_x11_intern_atom( "XdndTypeList", &qt_xdnd_type_list ); + + qt_x11_intern_atom( "XdndSelection", &qt_xdnd_selection ); + + qt_x11_intern_atom( "XdndAware", &qt_xdnd_aware ); + qt_x11_intern_atom( "XdndProxy", &qt_xdnd_proxy ); + + + qt_x11_intern_atom( "XdndActionCopy", &qt_xdnd_action_copy ); + qt_x11_intern_atom( "XdndActionLink", &qt_xdnd_action_link ); + qt_x11_intern_atom( "XdndActionMove", &qt_xdnd_action_move ); + qt_x11_intern_atom( "XdndActionPrivate", &qt_xdnd_action_private ); + + qt_x11_intern_atom( "QT_SELECTION", &qt_selection_property ); + qt_x11_intern_atom( "INCR", &qt_incr_atom ); + + qAddPostRoutine( qt_xdnd_cleanup ); +} + + +void qt_xdnd_cleanup() +{ + delete qt_xdnd_drag_types; + qt_xdnd_drag_types = 0; + delete qt_xdnd_atom_numbers; + qt_xdnd_atom_numbers = 0; + delete qt_xdnd_target_data; + qt_xdnd_target_data = 0; + delete noDropCursor; + noDropCursor = 0; + delete copyCursor; + copyCursor = 0; + delete moveCursor; + moveCursor = 0; + delete linkCursor; + linkCursor = 0; + delete defaultPm; + defaultPm = 0; + delete desktop_proxy; + desktop_proxy = 0; +} + + +static TQWidget * find_child( TQWidget * tlw, TQPoint & p ) +{ + TQWidget * w = tlw; + + p = w->mapFromGlobal( p ); + bool done = FALSE; + while ( !done ) { + done = TRUE; + if ( ((TQExtraWidget*)w)->extraData() && + ((TQExtraWidget*)w)->extraData()->xDndProxy != 0 ) + break; // stop searching for widgets under the mouse cursor if found widget is a proxy. + if ( w->children() ) { + TQObjectListIt it( *w->children() ); + it.toLast(); + TQObject * o; + while( (o=it.current()) ) { + --it; + if ( o->isWidgetType() && + ((TQWidget*)o)->isVisible() && + ((TQWidget*)o)->geometry().contains( p ) && + !((TQWidget*)o)->isTopLevel()) { + w = (TQWidget *)o; + done = FALSE; + p = w->mapFromParent( p ); + break; + } + } + } + } + return w; +} + + +static bool checkEmbedded(TQWidget* w, const XEvent* xe) +{ + if (!w) + return FALSE; + + if (current_embedding_widget != 0 && current_embedding_widget != w) { + qt_xdnd_current_target = ((TQExtraWidget*)current_embedding_widget)->extraData()->xDndProxy; + qt_xdnd_current_proxy_target = qt_xdnd_current_target; + qt_xdnd_send_leave(); + qt_xdnd_current_target = 0; + qt_xdnd_current_proxy_target = 0; + current_embedding_widget = 0; + } + + TQWExtra* extra = ((TQExtraWidget*)w)->extraData(); + if ( extra && extra->xDndProxy != 0 ) { + + if (current_embedding_widget != w) { + + last_enter_event.xany.window = extra->xDndProxy; + XSendEvent( TQPaintDevice::x11AppDisplay(), extra->xDndProxy, False, NoEventMask, + &last_enter_event ); + current_embedding_widget = w; + } + + ((XEvent*)xe)->xany.window = extra->xDndProxy; + XSendEvent( TQPaintDevice::x11AppDisplay(), extra->xDndProxy, False, NoEventMask, + (XEvent*)xe ); + qt_xdnd_current_widget = w; + return TRUE; + } + current_embedding_widget = 0; + return FALSE; +} + +void qt_handle_xdnd_enter( TQWidget *, const XEvent * xe, bool /*passive*/ ) +{ + //if ( !w->neveHadAChildWithDropEventsOn() ) + //return; // haven't been set up for dnd + + qt_motifdnd_active = FALSE; + + last_enter_event.xclient = xe->xclient; + + qt_xdnd_target_answerwas = FALSE; + + const long *l = xe->xclient.data.l; + int version = (int)(((unsigned long)(l[1])) >> 24); + + if ( version > qt_xdnd_version ) + return; + + qt_xdnd_dragsource_xid = l[0]; + + int j = 0; + if ( l[1] & 1 ) { + // get the types from XdndTypeList + Atom type = None; + int f; + unsigned long n, a; + Atom *data; + XGetWindowProperty( TQPaintDevice::x11AppDisplay(), qt_xdnd_dragsource_xid, + qt_xdnd_type_list, 0, + qt_xdnd_max_type, False, XA_ATOM, &type, &f,&n,&a,(uchar**)&data ); + for ( ; jxclient.data.l; + + TQPoint p( (l[2] & 0xffff0000) >> 16, l[2] & 0x0000ffff ); + TQWidget * c = find_child( w, p ); // changes p to to c-local coordinates + + if (!passive && checkEmbedded(c, xe)) + return; + + if ( !c || !c->acceptDrops() && c->isDesktop() ) { + return; + } + + if ( l[0] != qt_xdnd_dragsource_xid ) { + //qDebug( "xdnd drag position from unexpected source (%08lx not %08lx)", + // l[0], qt_xdnd_dragsource_xid ); + return; + } + + if (l[3] != 0) { + // timestamp from the source + qt_xdnd_target_current_time = qt_x_user_time = l[3]; + } + + XClientMessageEvent response; + response.type = ClientMessage; + response.window = qt_xdnd_dragsource_xid; + response.format = 32; + response.message_type = qt_xdnd_status; + response.data.l[0] = w->winId(); + response.data.l[1] = 0; // flags + response.data.l[2] = 0; // x, y + response.data.l[3] = 0; // w, h + response.data.l[4] = 0; // action + + if ( !passive ) { // otherwise just reject + while ( c && !c->acceptDrops() && !c->isTopLevel() ) { + p = c->mapToParent( p ); + c = c->parentWidget(); + } + + TQRect answerRect( c->mapToGlobal( p ), TQSize( 1,1 ) ); + + TQDragMoveEvent me( p ); + TQDropEvent::Action accepted_action = xdndaction_to_qtaction(l[4]); + me.setAction(accepted_action); + + if ( c != qt_xdnd_current_widget ) { + qt_xdnd_target_answerwas = FALSE; + if ( qt_xdnd_current_widget ) { + TQDragLeaveEvent e; + TQApplication::sendEvent( qt_xdnd_current_widget, &e ); + } + if ( c->acceptDrops() ) { + qt_xdnd_current_widget = c; + qt_xdnd_current_position = p; + + TQDragEnterEvent de( p ); + de.setAction(accepted_action); + TQApplication::sendEvent( c, &de ); + if ( de.isAccepted() ) { + me.accept( de.answerRect() ); + if ( !de.isActionAccepted() ) // only as a copy (move if we del) + accepted_action = TQDropEvent::Copy; + else + me.acceptAction(TRUE); + } else { + me.ignore( de.answerRect() ); + } + } + } else { + if ( qt_xdnd_target_answerwas ) { + me.accept(); + me.acceptAction(global_requested_action == global_accepted_action); + } + } + + if ( !c->acceptDrops() ) { + qt_xdnd_current_widget = 0; + answerRect = TQRect( p, TQSize( 1, 1 ) ); + } else if ( xdndaction_to_qtaction(l[4]) < TQDropEvent::Private ) { + qt_xdnd_current_widget = c; + qt_xdnd_current_position = p; + + TQApplication::sendEvent( c, &me ); + qt_xdnd_target_answerwas = me.isAccepted(); + if ( me.isAccepted() ) { + response.data.l[1] = 1; // yes + if ( !me.isActionAccepted() ) // only as a copy (move if we del) + accepted_action = TQDropEvent::Copy; + } else { + response.data.l[0] = 0; + } + answerRect = me.answerRect().intersect( c->rect() ); + } else { + response.data.l[0] = 0; + answerRect = TQRect( p, TQSize( 1, 1 ) ); + } + answerRect = TQRect( c->mapToGlobal( answerRect.topLeft() ), + answerRect.size() ); + + if ( answerRect.left() < 0 ) + answerRect.setLeft( 0 ); + if ( answerRect.right() > 4096 ) + answerRect.setRight( 4096 ); + if ( answerRect.top() < 0 ) + answerRect.setTop( 0 ); + if ( answerRect.bottom() > 4096 ) + answerRect.setBottom( 4096 ); + if ( answerRect.width() < 0 ) + answerRect.setWidth( 0 ); + if ( answerRect.height() < 0 ) + answerRect.setHeight( 0 ); + + response.data.l[2] = (answerRect.x() << 16) + answerRect.y(); + response.data.l[3] = (answerRect.width() << 16) + answerRect.height(); + response.data.l[4] = qtaction_to_xdndaction(accepted_action); + global_accepted_action = accepted_action; + } + + // reset + qt_xdnd_target_current_time = CurrentTime; + + TQWidget * source = TQWidget::find( qt_xdnd_dragsource_xid ); + + if ( source && source->isDesktop() && !source->acceptDrops() ) + source = 0; + + if ( source ) + qt_handle_xdnd_status( source, (const XEvent *)&response, passive ); + else + XSendEvent( TQPaintDevice::x11AppDisplay(), qt_xdnd_dragsource_xid, False, + NoEventMask, (XEvent*)&response ); +} + + +void qt_handle_xdnd_status( TQWidget * w, const XEvent * xe, bool /*passive*/ ) +{ + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + // Messy: TQDragResponseEvent is just a call to TQDragManager function + global_accepted_action = xdndaction_to_qtaction(l[4]); + TQDragResponseEvent e( (int)(l[1] & 1) ); + TQApplication::sendEvent( w, &e ); + + if ( (int)(l[1] & 2) == 0 ) { + TQPoint p( (l[2] & 0xffff0000) >> 16, l[2] & 0x0000ffff ); + TQSize s( (l[3] & 0xffff0000) >> 16, l[3] & 0x0000ffff ); + qt_xdnd_source_sameanswer = TQRect( p, s ); + if ( qt_xdnd_source_sameanswer.isNull() ) { + // Application wants "coninutous" move events + } + } else { + qt_xdnd_source_sameanswer = TQRect(); + } +} + + +void qt_handle_xdnd_leave( TQWidget *w, const XEvent * xe, bool /*passive*/ ) +{ + //qDebug( "xdnd leave" ); + if ( !qt_xdnd_current_widget || + w->topLevelWidget() != qt_xdnd_current_widget->topLevelWidget() ) { + return; // sanity + } + + if (checkEmbedded(current_embedding_widget, xe)) { + current_embedding_widget = 0; + qt_xdnd_current_widget = 0; + return; + } + + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + + TQDragLeaveEvent e; + TQApplication::sendEvent( qt_xdnd_current_widget, &e ); + + if ( l[0] != qt_xdnd_dragsource_xid ) { + // This often happens - leave other-process window tquickly + //qDebug( "xdnd drag leave from unexpected source (%08lx not %08lx", + //l[0], qt_xdnd_dragsource_xid ); + qt_xdnd_current_widget = 0; + return; + } + + qt_xdnd_dragsource_xid = 0; + qt_xdnd_types[0] = 0; + qt_xdnd_current_widget = 0; +} + + +void qt_xdnd_send_leave() +{ + if ( !qt_xdnd_current_target ) + return; + + XClientMessageEvent leave; + leave.type = ClientMessage; + leave.window = qt_xdnd_current_target; + leave.format = 32; + leave.message_type = qt_xdnd_leave; + leave.data.l[0] = qt_xdnd_dragsource_xid; + leave.data.l[1] = 0; // flags + leave.data.l[2] = 0; // x, y + leave.data.l[3] = 0; // w, h + leave.data.l[4] = 0; // just null + + TQWidget * w = TQWidget::find( qt_xdnd_current_proxy_target ); + + if ( w && w->isDesktop() && !w->acceptDrops() ) + w = 0; + + if ( w ) + qt_handle_xdnd_leave( w, (const XEvent *)&leave, FALSE ); + else + XSendEvent( TQPaintDevice::x11AppDisplay(), qt_xdnd_current_proxy_target, False, + NoEventMask, (XEvent*)&leave ); + qt_xdnd_current_target = 0; + qt_xdnd_current_proxy_target = 0; +} + + + +void qt_handle_xdnd_drop( TQWidget *, const XEvent * xe, bool passive ) +{ + if ( !qt_xdnd_current_widget ) { + qt_xdnd_dragsource_xid = 0; + return; // sanity + } + + if (!passive && checkEmbedded(qt_xdnd_current_widget, xe)){ + current_embedding_widget = 0; + qt_xdnd_dragsource_xid = 0; + qt_xdnd_current_widget = 0; + return; + } + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + + //qDebug( "xdnd drop" ); + + if ( l[0] != qt_xdnd_dragsource_xid ) { + //qDebug( "xdnd drop from unexpected source (%08lx not %08lx", + // l[0], qt_xdnd_dragsource_xid ); + return; + } + + if (l[2] != 0) { + // update the "user time" from the timestamp in the event. + qt_xdnd_target_current_time = qt_x_user_time = l[2]; + } + + if ( qt_xdnd_source_object ) + qt_xdnd_source_object->setTarget( qt_xdnd_current_widget ); + + if ( !passive ) { + TQDropEvent de( qt_xdnd_current_position ); + de.setAction( global_accepted_action ); + TQApplication::sendEvent( qt_xdnd_current_widget, &de ); + if ( !de.isAccepted() ) { + // Ignore a failed drag + global_accepted_action = TQDropEvent::Copy; + dndCancelled = TRUE; + } + XClientMessageEvent finished; + finished.type = ClientMessage; + finished.window = qt_xdnd_dragsource_xid; + finished.format = 32; + finished.message_type = qt_xdnd_finished; + finished.data.l[0] = qt_xdnd_current_widget?qt_xdnd_current_widget->topLevelWidget()->winId():0; + finished.data.l[1] = 0; // flags + XSendEvent( TQPaintDevice::x11AppDisplay(), qt_xdnd_dragsource_xid, False, + NoEventMask, (XEvent*)&finished ); + } else { + TQDragLeaveEvent e; + TQApplication::sendEvent( qt_xdnd_current_widget, &e ); + } + qt_xdnd_dragsource_xid = 0; + qt_xdnd_current_widget = 0; + + // reset + qt_xdnd_target_current_time = CurrentTime; +} + + +void qt_handle_xdnd_finished( TQWidget *, const XEvent * xe, bool passive ) +{ + const unsigned long *l = (const unsigned long *)xe->xclient.data.l; + + if ( l[0] && (l[0] == qt_xdnd_current_target + || l[0] == qt_xdnd_current_proxy_target) ) { + // + if ( !passive ) + (void ) checkEmbedded( qt_xdnd_current_widget, xe); + current_embedding_widget = 0; + qt_xdnd_current_target = 0; + qt_xdnd_current_proxy_target = 0; + delete qt_xdnd_source_object; + qt_xdnd_source_object = 0; + } +} + + +void TQDragManager::timerEvent( TQTimerEvent* e ) +{ + if ( e->timerId() == heartbeat ) { + if( need_modifiers_check ) { + Window root, child; + int root_x, root_y, win_x, win_y; + unsigned int mask; + XQueryPointer( qt_xdisplay(), qt_xrootwin( qt_xdnd_current_screen ), + &root, &child, &root_x, &root_y, &win_x, &win_y, &mask ); + if( updateMode( (ButtonState)qt_x11_translateButtonState( mask ))) + qt_xdnd_source_sameanswer = TQRect(); // force move + } + need_modifiers_check = TRUE; + if( qt_xdnd_source_sameanswer.isNull() ) + move( TQCursor::pos() ); + } +} + +static bool qt_xdnd_was_move = false; +static bool qt_xdnd_found = false; +// check whole incoming X queue for move events +// checking whole queue is done by always returning False in the predicate +// if there's another move event in the queue, and there's not a mouse button +// or keyboard or ClientMessage event before it, the current move event +// may be safely discarded +// this helps avoiding being overloaded by being flooded from many events +// from the XServer +static +Bool qt_xdnd_predicate( Display*, XEvent* ev, XPointer ) +{ + if( qt_xdnd_found ) + return False; + if( ev->type == MotionNotify ) + { + qt_xdnd_was_move = true; + qt_xdnd_found = true; + } + if( ev->type == ButtonPress || ev->type == ButtonRelease + || ev->type == XKeyPress || ev->type == XKeyRelease + || ev->type == ClientMessage ) + { + qt_xdnd_was_move = false; + qt_xdnd_found = true; + } + return False; +} + +static +bool qt_xdnd_another_movement() +{ + qt_xdnd_was_move = false; + qt_xdnd_found = false; + XEvent dummy; + XCheckIfEvent( qt_xdisplay(), &dummy, qt_xdnd_predicate, NULL ); + return qt_xdnd_was_move; +} + +bool TQDragManager::eventFilter( TQObject * o, TQEvent * e) +{ + if ( beingCancelled ) { + if ( e->type() == TQEvent::KeyRelease && + ((TQKeyEvent*)e)->key() == Key_Escape ) { + qApp->removeEventFilter( this ); + object = 0; + dragSource = 0; + beingCancelled = FALSE; + qApp->exit_loop(); + return TRUE; // block the key release + } + return FALSE; + } + + Q_ASSERT( object != 0 ); + + if ( !o->isWidgetType() ) + return FALSE; + + if ( e->type() == TQEvent::MouseMove ) { + TQMouseEvent* me = (TQMouseEvent *)e; + if( !qt_xdnd_another_movement()) { + updateMode(me->stateAfter()); + move( me->globalPos() ); + } + need_modifiers_check = FALSE; + return TRUE; + } else if ( e->type() == TQEvent::MouseButtonRelease ) { + qApp->removeEventFilter( this ); + if ( willDrop ) + drop(); + else + cancel(); + object = 0; + dragSource = 0; + beingCancelled = FALSE; + qApp->exit_loop(); + return TRUE; + } else if ( e->type() == TQEvent::DragResponse ) { + if ( ((TQDragResponseEvent *)e)->dragAccepted() ) { + if ( !willDrop ) { + willDrop = TRUE; + } + } else { + if ( willDrop ) { + willDrop = FALSE; + } + } + updateCursor(); + return TRUE; + } + + if ( e->type() == TQEvent::KeyPress + || e->type() == TQEvent::KeyRelease ) + { + TQKeyEvent *ke = ((TQKeyEvent*)e); + if ( ke->key() == Key_Escape && e->type() == TQEvent::KeyPress ) { + cancel(); + qApp->removeEventFilter( this ); + object = 0; + dragSource = 0; + beingCancelled = FALSE; + qApp->exit_loop(); + } else { + if( updateMode(ke->stateAfter())) { + qt_xdnd_source_sameanswer = TQRect(); // force move + move( TQCursor::pos() ); + } + need_modifiers_check = FALSE; + } + return TRUE; // Eat all key events + } + + // ### We bind modality to widgets, so we have to do this + // ### "manually". + // DnD is modal - eat all other interactive events + switch ( e->type() ) { + case TQEvent::MouseButtonPress: + case TQEvent::MouseButtonRelease: + case TQEvent::MouseButtonDblClick: + case TQEvent::MouseMove: + case TQEvent::KeyPress: + case TQEvent::KeyRelease: + case TQEvent::Wheel: + case TQEvent::Accel: + case TQEvent::AccelAvailable: + case TQEvent::AccelOverride: + return TRUE; + default: + return FALSE; + } +} + + +static TQt::ButtonState oldstate; +bool TQDragManager::updateMode( ButtonState newstate ) +{ + if ( newstate == oldstate ) + return false; + const int both = ShiftButton|ControlButton; + if ( (newstate & both) == both ) { + global_requested_action = TQDropEvent::Link; + } else { + bool local = qt_xdnd_source_object != 0; + if ( drag_mode == TQDragObject::DragMove ) + global_requested_action = TQDropEvent::Move; + else if ( drag_mode == TQDragObject::DragCopy ) + global_requested_action = TQDropEvent::Copy; + else if ( drag_mode == TQDragObject::DragLink ) + global_requested_action = TQDropEvent::Link; + else { + if ( drag_mode == TQDragObject::DragDefault && local ) + global_requested_action = TQDropEvent::Move; + else + global_requested_action = TQDropEvent::Copy; + if ( newstate & ShiftButton ) + global_requested_action = TQDropEvent::Move; + else if ( newstate & ControlButton ) + global_requested_action = TQDropEvent::Copy; + } + } + oldstate = newstate; + return true; +} + + +void TQDragManager::createCursors() +{ + if ( !noDropCursor ) { + noDropCursor = new TQCursor( ForbiddenCursor ); + if ( !pm_cursor[0].isNull() ) + moveCursor = new TQCursor(pm_cursor[0], 0,0); + if ( !pm_cursor[1].isNull() ) + copyCursor = new TQCursor(pm_cursor[1], 0,0); + if ( !pm_cursor[2].isNull() ) + linkCursor = new TQCursor(pm_cursor[2], 0,0); + } +} + +void TQDragManager::updateCursor() +{ + TQCursor *c; + if ( willDrop ) { + if ( global_accepted_action == TQDropEvent::Copy ) { + if ( global_requested_action == TQDropEvent::Move ) + c = moveCursor; // (source can delete) + else + c = copyCursor; + } else if ( global_accepted_action == TQDropEvent::Link ) { + c = linkCursor; + } else { + c = moveCursor; + } + if ( qt_xdnd_deco ) { + qt_xdnd_deco->show(); + qt_xdnd_deco->raise(); + } + } else { + c = noDropCursor; + //if ( qt_xdnd_deco ) + // qt_xdnd_deco->hide(); + } +#ifndef QT_NO_CURSOR + if ( c ) + qApp->setOverrideCursor( *c, TRUE ); +#endif +} + + +void TQDragManager::cancel( bool deleteSource ) +{ + killTimer( heartbeat ); + heartbeat = -1; + if ( object ) { + beingCancelled = TRUE; + object = 0; + } + + if ( qt_xdnd_current_target ) { + qt_xdnd_send_leave(); + } + +#ifndef QT_NO_CURSOR + if ( restoreCursor ) { + TQApplication::restoreOverrideCursor(); + restoreCursor = FALSE; + } +#endif + + if ( deleteSource ) + delete qt_xdnd_source_object; + qt_xdnd_source_object = 0; + delete qt_xdnd_deco; + qt_xdnd_deco = 0; + + dndCancelled = TRUE; +} + +static +Window findRealWindow( const TQPoint & pos, Window w, int md ) +{ + if ( qt_xdnd_deco && w == qt_xdnd_deco->winId() ) + return 0; + + if ( md ) { + qt_ignore_badwindow(); + XWindowAttributes attr; + XGetWindowAttributes( TQPaintDevice::x11AppDisplay(), w, &attr ); + if (qt_badwindow()) + return 0; + + if ( attr.map_state == IsViewable + && TQRect(attr.x,attr.y,attr.width,attr.height) + .contains(pos) ) + { + { + Atom type = None; + int f; + unsigned long n, a; + unsigned char *data; + + XGetWindowProperty( TQPaintDevice::x11AppDisplay(), w, qt_xdnd_aware, 0, + 0, False, AnyPropertyType, &type, &f,&n,&a,&data ); + if ( data ) XFree(data); + if ( type ) return w; + } + + Window r, p; + Window* c; + uint nc; + if ( XQueryTree( TQPaintDevice::x11AppDisplay(), w, &r, &p, &c, &nc ) ) { + r=0; + for (uint i=nc; !r && i--; ) { + r = findRealWindow( pos-TQPoint(attr.x,attr.y), + c[i], md-1 ); + } + XFree(c); + if ( r ) + return r; + + // We didn't find a client window! Just use the + // innermost window. + } + + // No children! + return w; + } + } + return 0; +} + +void TQDragManager::move( const TQPoint & globalPos ) +{ + if (!object) { + // perhaps the target crashed? + return; + } + + int screen = TQCursor::x11Screen(); + if ( ( qt_xdnd_current_screen == -1 && screen != TQPaintDevice::x11AppScreen() ) || + ( screen != qt_xdnd_current_screen ) ) { + // recreate the pixmap on the new screen... + delete qt_xdnd_deco; + qt_xdnd_deco = new TQShapedPixmapWidget( screen ); + qt_xdnd_deco->x11SetWindowTransient( dragSource->topLevelWidget()); + if (!TQWidget::mouseGrabber()) { + updatePixmap(); + qt_xdnd_deco->grabMouse(); + } + } + updatePixmap( globalPos ); + + if ( qt_xdnd_source_sameanswer.contains( globalPos ) && + qt_xdnd_source_sameanswer.isValid() ) { + return; + } + + qt_xdnd_current_screen = screen; + Window rootwin = TQPaintDevice::x11AppRootWindow( qt_xdnd_current_screen ); + Window target = 0; + int lx = 0, ly = 0; + if ( !XTranslateCoordinates( TQPaintDevice::x11AppDisplay(), rootwin, rootwin, + globalPos.x(), globalPos.y(), + &lx, &ly, &target) ) + // some wierd error... + return; + + if ( target == rootwin ) { + // Ok. + } else if ( target ) { + //me + Window src = rootwin; + while (target != 0) { + int lx2, ly2; + Window t; + // translate coordinates + if (!XTranslateCoordinates(TQPaintDevice::x11AppDisplay(), src, target, + lx, ly, &lx2, &ly2, &t)) { + target = 0; + break; + } + lx = lx2; + ly = ly2; + src = target; + + // check if it has XdndAware + Atom type = None; + int f; + unsigned long n, a; + unsigned char *data = 0; + XGetWindowProperty(TQPaintDevice::x11AppDisplay(), target, qt_xdnd_aware, 0, 0, False, + AnyPropertyType, &type, &f,&n,&a,&data); + if (data) + XFree(data); + if (type) + break; + + // find child at the coordinates + if (!XTranslateCoordinates( TQPaintDevice::x11AppDisplay(), src, src, + lx, ly, &lx2, &ly2, &target)) { + target = 0; + break; + } + } + if ( qt_xdnd_deco && (!target || target == qt_xdnd_deco->winId()) ) { + target = findRealWindow(globalPos,rootwin,6); + } + } + + TQWidget* w; + if ( target ) { + w = TQWidget::find( (WId)target ); + if ( w && w->isDesktop() && !w->acceptDrops() ) + w = 0; + } else { + w = 0; + target = rootwin; + } + + WId proxy_target = target; + int target_version = 1; + + { + Atom type = None; + int r, f; + unsigned long n, a; + WId *proxy_id; + qt_ignore_badwindow(); + r = XGetWindowProperty( qt_xdisplay(), target, qt_xdnd_proxy, 0, + 1, False, XA_WINDOW, &type, &f,&n,&a,(uchar**)&proxy_id ); + if ( ( r != Success ) || qt_badwindow() ) { + proxy_target = target = 0; + } else if ( type == XA_WINDOW && proxy_id ) { + proxy_target = *proxy_id; + XFree(proxy_id); + proxy_id = 0; + r = XGetWindowProperty( qt_xdisplay(), proxy_target, qt_xdnd_proxy, 0, + 1, False, XA_WINDOW, &type, &f,&n,&a,(uchar**)&proxy_id ); + if ( ( r != Success ) || qt_badwindow() || !type || !proxy_id || *proxy_id != proxy_target ) { + // Bogus + proxy_target = 0; + target = 0; + } + if ( proxy_id ) + XFree(proxy_id); + } + if ( proxy_target ) { + int *tv; + qt_ignore_badwindow(); + r = XGetWindowProperty( qt_xdisplay(), proxy_target, qt_xdnd_aware, 0, + 1, False, AnyPropertyType, &type, &f,&n,&a,(uchar**)&tv ); + if ( r != Success ) { + target = 0; + } else { + target_version = TQMIN(qt_xdnd_version,tv ? *tv : 1); + if ( tv ) + XFree( tv ); + if (!(!qt_badwindow() && type)) + target = 0; + } + } + } + + if ( target != qt_xdnd_current_target ) { + if ( qt_xdnd_current_target ) + qt_xdnd_send_leave(); + + qt_xdnd_current_target = target; + qt_xdnd_current_proxy_target = proxy_target; + if ( target ) { + TQMemArray type; + int flags = target_version << 24; + const char* fmt; + int nfmt=0; + for (nfmt=0; (fmt=object->format(nfmt)); nfmt++) { + type.resize(nfmt+1); + type[nfmt] = *qt_xdnd_str_to_atom( fmt ); + } + if ( nfmt >= 3 ) { + XChangeProperty( TQPaintDevice::x11AppDisplay(), + object->source()->winId(), qt_xdnd_type_list, + XA_ATOM, 32, PropModeReplace, + (unsigned char *)type.data(), + type.size() ); + flags |= 0x0001; + } + XClientMessageEvent enter; + enter.type = ClientMessage; + enter.window = target; + enter.format = 32; + enter.message_type = qt_xdnd_enter; + enter.data.l[0] = object->source()->winId(); + enter.data.l[1] = flags; + enter.data.l[2] = type.size()>0 ? type[0] : 0; + enter.data.l[3] = type.size()>1 ? type[1] : 0; + enter.data.l[4] = type.size()>2 ? type[2] : 0; + // provisionally set the rectangle to 5x5 pixels... + qt_xdnd_source_sameanswer = TQRect( globalPos.x() - 2, + globalPos.y() -2 , 5, 5 ); + + if ( w ) { + qt_handle_xdnd_enter( w, (const XEvent *)&enter, FALSE ); + } else if ( target ) { + XSendEvent( TQPaintDevice::x11AppDisplay(), proxy_target, False, NoEventMask, + (XEvent*)&enter ); + } + } + } + + if ( target ) { + XClientMessageEvent move; + move.type = ClientMessage; + move.window = target; + move.format = 32; + move.message_type = qt_xdnd_position; + move.window = target; + move.data.l[0] = object->source()->winId(); + move.data.l[1] = 0; // flags + move.data.l[2] = (globalPos.x() << 16) + globalPos.y(); + move.data.l[3] = qt_x_time; + move.data.l[4] = qtaction_to_xdndaction( global_requested_action ); + + if ( w ) + qt_handle_xdnd_position( w, (const XEvent *)&move, FALSE ); + else + XSendEvent( TQPaintDevice::x11AppDisplay(), proxy_target, False, NoEventMask, + (XEvent*)&move ); + } else { + if ( willDrop ) { + willDrop = FALSE; + updateCursor(); + } + } +} + + +void TQDragManager::drop() +{ + killTimer( heartbeat ); + heartbeat = -1; + if ( !qt_xdnd_current_target ) + return; + + delete qt_xdnd_deco; + qt_xdnd_deco = 0; + + XClientMessageEvent drop; + drop.type = ClientMessage; + drop.window = qt_xdnd_current_target; + drop.format = 32; + drop.message_type = qt_xdnd_drop; + drop.data.l[0] = object->source()->winId(); + drop.data.l[1] = 0; // flags + drop.data.l[2] = qt_x_time; + drop.data.l[3] = 0; + drop.data.l[4] = 0; + + TQWidget * w = TQWidget::find( qt_xdnd_current_proxy_target ); + + if ( w && w->isDesktop() && !w->acceptDrops() ) + w = 0; + + if ( w ) + qt_handle_xdnd_drop( w, (const XEvent *)&drop, FALSE ); + else + XSendEvent( TQPaintDevice::x11AppDisplay(), qt_xdnd_current_proxy_target, False, + NoEventMask, (XEvent*)&drop ); + +#ifndef QT_NO_CURSOR + if ( restoreCursor ) { + TQApplication::restoreOverrideCursor(); + restoreCursor = FALSE; + } +#endif +} + + + +bool qt_xdnd_handle_badwindow() +{ + if ( qt_xdnd_source_object && qt_xdnd_current_target ) { + qt_xdnd_current_target = 0; + qt_xdnd_current_proxy_target = 0; + delete qt_xdnd_source_object; + qt_xdnd_source_object = 0; + delete qt_xdnd_deco; + qt_xdnd_deco = 0; + return TRUE; + } + if ( qt_xdnd_dragsource_xid ) { + qt_xdnd_dragsource_xid = 0; + if ( qt_xdnd_current_widget ) { + TQDragLeaveEvent e; + TQApplication::sendEvent( qt_xdnd_current_widget, &e ); + qt_xdnd_current_widget = 0; + } + return TRUE; + } + return FALSE; +} + + +/*! + \class TQDragMoveEvent qevent.h + \ingroup events + \ingroup draganddrop + \brief The TQDragMoveEvent class provides an event which is sent while a drag and drop is in progress. + + When a widget \link TQWidget::setAcceptDrops() accepts drop + events\endlink, it will receive this event repeatedly while the + drag is within the widget's boundaries. The widget should examine + the event to see what data it \link TQDragMoveEvent::provides() + provides\endlink, and accept() the drop if appropriate. + + Note that this class inherits most of its functionality from + TQDropEvent. +*/ + + +/*! + Returns TRUE if this event provides format \a mimeType; otherwise + returns FALSE. + + \sa data() +*/ + +bool TQDropEvent::provides( const char *mimeType ) const +{ + if ( qt_motifdnd_active && qstrnicmp( mimeType, "text/", 5 ) == 0 ) + return TRUE; + + int n=0; + const char* f; + do { + f = format( n ); + if ( !f ) + return FALSE; + n++; + } while( qstricmp( mimeType, f ) ); + return TRUE; +} + +void qt_xdnd_handle_selection_request( const XSelectionRequestEvent * req ) +{ + if ( !req ) + return; + XEvent evt; + evt.xselection.type = SelectionNotify; + evt.xselection.display = req->display; + evt.xselection.requestor = req->requestor; + evt.xselection.selection = req->selection; + evt.xselection.target = req->target; + evt.xselection.property = None; + evt.xselection.time = req->time; + const char* format = qt_xdnd_atom_to_str( req->target ); + if ( format && qt_xdnd_source_object && + qt_xdnd_source_object->provides( format ) ) { + TQByteArray a = qt_xdnd_source_object->encodedData(format); + XChangeProperty ( TQPaintDevice::x11AppDisplay(), req->requestor, req->property, + req->target, 8, PropModeReplace, + (unsigned char *)a.data(), a.size() ); + evt.xselection.property = req->property; + } + // ### this can die if req->requestor crashes at the wrong + // ### moment + XSendEvent( TQPaintDevice::x11AppDisplay(), req->requestor, False, 0, &evt ); +} + +/* + XChangeProperty ( TQPaintDevice::x11AppDisplay(), req->requestor, req->property, + XA_STRING, 8, + PropModeReplace, + (uchar *)d->text(), strlen(d->text()) ); + evt.xselection.property = req->property; +*/ + +static TQByteArray qt_xdnd_obtain_data( const char *format ) +{ + TQByteArray result; + + TQWidget* w; + if ( qt_xdnd_dragsource_xid && qt_xdnd_source_object && + (w=TQWidget::find( qt_xdnd_dragsource_xid )) + && (!w->isDesktop() || w->acceptDrops()) ) + { + TQDragObject * o = qt_xdnd_source_object; + if ( o->provides( format ) ) + result = o->encodedData(format); + return result; + } + + Atom * a = qt_xdnd_str_to_atom( format ); + if ( !a || !*a ) + return result; + + if ( !qt_xdnd_target_data ) + qt_xdnd_target_data = new TQIntDict( 17 ); + + if ( qt_xdnd_target_data->find( (int)*a ) ) { + result = *(qt_xdnd_target_data->find( (int)*a )); + } else { + if ( XGetSelectionOwner( TQPaintDevice::x11AppDisplay(), + qt_xdnd_selection ) == None ) + return result; // should never happen? + + TQWidget* tw = qt_xdnd_current_widget; + if ( !qt_xdnd_current_widget || + qt_xdnd_current_widget->isDesktop() ) { + tw = new TQWidget; + } + XConvertSelection( TQPaintDevice::x11AppDisplay(), + qt_xdnd_selection, + *a, + qt_xdnd_selection, + tw->winId(), + qt_xdnd_target_current_time ); + XFlush( TQPaintDevice::x11AppDisplay() ); + + XEvent xevent; + bool got=qt_xclb_wait_for_event( TQPaintDevice::x11AppDisplay(), + tw->winId(), + SelectionNotify, &xevent, 5000); + if ( got ) { + Atom type; + + if ( qt_xclb_read_property( TQPaintDevice::x11AppDisplay(), + tw->winId(), + qt_xdnd_selection, TRUE, + &result, 0, &type, 0, FALSE ) ) { + if ( type == qt_incr_atom ) { + int nbytes = result.size() >= 4 ? *((int*)result.data()) : 0; + result = qt_xclb_read_incremental_property( TQPaintDevice::x11AppDisplay(), + tw->winId(), + qt_xdnd_selection, + nbytes, FALSE ); + } else if ( type != *a ) { + // (includes None) qDebug( "TQt clipboard: unknown atom %ld", type); + } +#if 0 + // this needs to be matched by a qt_xdnd_target_data->clear() + // when each drag is finished. for 2.0, we do the safe thing + // and disable the entire caching. + if ( type != None ) + qt_xdnd_target_data->insert( (int)((long)a), new TQByteArray(result) ); +#endif + } + } + if ( !qt_xdnd_current_widget || + qt_xdnd_current_widget->isDesktop() ) { + delete tw; + } + } + + return result; +} + + +/* + Enable drag and drop for widget w by installing the proper + properties on w's toplevel widget. +*/ +bool qt_dnd_enable( TQWidget* w, bool on ) +{ + w = w->topLevelWidget(); + + if ( on ) { + if ( ( (TQExtraWidget*)w)->topData()->dnd ) + return TRUE; // been there, done that + ((TQExtraWidget*)w)->topData()->dnd = 1; + } + + qt_motifdnd_enable( w, on ); + return qt_xdnd_enable( w, on ); +} + + +/*! + \class TQDropEvent qevent.h + \ingroup events + \ingroup draganddrop + + \brief The TQDropEvent class provides an event which is sent when a drag and drop is completed. + + When a widget \link TQWidget::setAcceptDrops() accepts drop + events\endlink, it will receive this event if it has accepted the + most recent TQDragEnterEvent or TQDragMoveEvent sent to it. + + The widget should use data() to extract the data in an appropriate + format. +*/ + + +/*! + \fn TQDropEvent::TQDropEvent (const TQPoint & pos, Type typ) + + Constructs a drop event that drops a drop of type \a typ on point + \a pos. +*/ // ### pos is in which coordinate system? + + +/*! + Returns a byte array containing the drag's data, in \a format. + + data() normally needs to get the data from the drag source, which + is potentially very slow, so it's advisable to call this function + only if you're sure that you will need the data in \a format. + + The resulting data will have a size of 0 if the format was not + available. + + \sa format() TQByteArray::size() +*/ + +TQByteArray TQDropEvent::encodedData( const char *format ) const +{ + if ( qt_motifdnd_active ) + return qt_motifdnd_obtain_data( format ); + return qt_xdnd_obtain_data( format ); +} + +/*! + Returns a string describing one of the available data types for + this drag. Common examples are "text/plain" and "image/gif". If \a + n is less than zero or greater than the number of available data + types, format() returns 0. + + This function is provided mainly for debugging. Most drop targets + will use provides(). + + \sa data() provides() +*/ + +const char* TQDropEvent::format( int n ) const +{ + if ( qt_motifdnd_active ) + return qt_motifdnd_format( n ); + + int i = 0; + while( iparent() ) + return FALSE; + + if ( object ) { + cancel(); + qApp->removeEventFilter( this ); + beingCancelled = FALSE; + } + + if ( qt_xdnd_source_object ) { + // the last drag and drop operation hasn't finished, so we are going to wait + // for one second to see if it does... if the finish message comes after this, + // then we could still have problems, but this is highly unlikely + TQApplication::flushX(); + + TQTime started = TQTime::currentTime(); + TQTime now = started; + do { + XEvent event; + if ( XCheckTypedEvent( TQPaintDevice::x11AppDisplay(), + ClientMessage, &event ) ) + qApp->x11ProcessEvent( &event ); + + now = TQTime::currentTime(); + if ( started > now ) // crossed midnight + started = now; + + // sleep 50ms, so we don't use up CPU cycles all the time. + struct timeval usleep_tv; + usleep_tv.tv_sec = 0; + usleep_tv.tv_usec = 50000; + select(0, 0, 0, 0, &usleep_tv); + } while ( qt_xdnd_source_object && started.msecsTo(now) < 1000 ); + } + + qt_xdnd_source_object = o; + qt_xdnd_source_object->setTarget( 0 ); + qt_xdnd_deco = new TQShapedPixmapWidget(); + + willDrop = FALSE; + + object = o; + updatePixmap(); + + dragSource = (TQWidget *)(object->parent()); + + qt_xdnd_deco->x11SetWindowTransient( dragSource->topLevelWidget()); + qApp->installEventFilter( this ); + qt_xdnd_source_current_time = qt_x_time; + XSetSelectionOwner( TQPaintDevice::x11AppDisplay(), qt_xdnd_selection, + dragSource->topLevelWidget()->winId(), + qt_xdnd_source_current_time ); + oldstate = ButtonState(-1); // #### Should use state that caused the drag + drag_mode = mode; + global_accepted_action = TQDropEvent::Copy; + updateMode(ButtonState(0)); + qt_xdnd_source_sameanswer = TQRect(); + move(TQCursor::pos()); + heartbeat = startTimer(200); + need_modifiers_check = FALSE; + +#ifndef QT_NO_CURSOR + qApp->setOverrideCursor( arrowCursor ); + restoreCursor = TRUE; + updateCursor(); +#endif + + dndCancelled = FALSE; + qt_xdnd_dragging = TRUE; + + if (!TQWidget::mouseGrabber()) + qt_xdnd_deco->grabMouse(); + + qApp->enter_loop(); // Do the DND. + +#ifndef QT_NO_CURSOR + qApp->restoreOverrideCursor(); +#endif + + delete qt_xdnd_deco; + qt_xdnd_deco = 0; + killTimer( heartbeat ); + heartbeat = -1; + qt_xdnd_current_screen = -1; + qt_xdnd_dragging = FALSE; + + return ((! dndCancelled) && // source del? + (global_accepted_action == TQDropEvent::Copy && + global_requested_action == TQDropEvent::Move)); + + // qt_xdnd_source_object persists until we get an xdnd_finish message +} + +void TQDragManager::updatePixmap( const TQPoint& cursorPos ) +{ + if ( qt_xdnd_deco ) { + TQPixmap pm; + TQPoint pm_hot(default_pm_hotx,default_pm_hoty); + if ( object ) { + pm = object->pixmap(); + if ( !pm.isNull() ) + pm_hot = object->pixmapHotSpot(); + } + if ( pm.isNull() ) { + if ( !defaultPm ) + defaultPm = new TQPixmap(default_pm); + pm = *defaultPm; + } + qt_xdnd_deco->setPixmap(pm, pm_hot); + qt_xdnd_deco->move(cursorPos-pm_hot); + //if ( willDrop ) { + qt_xdnd_deco->show(); + //} else { + // qt_xdnd_deco->hide(); + //} + } +} + +void TQDragManager::updatePixmap() +{ + updatePixmap( TQCursor::pos()); +} + +#endif // QT_NO_DRAGANDDROP diff --git a/src/kernel/qdragobject.cpp b/src/kernel/qdragobject.cpp new file mode 100644 index 000000000..15085ce68 --- /dev/null +++ b/src/kernel/qdragobject.cpp @@ -0,0 +1,1811 @@ +/**************************************************************************** +** +** Implementation of Drag and Drop support +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +#ifndef QT_NO_MIME + +#include "qdragobject.h" +#include "qtextcodec.h" +#include "qapplication.h" +#include "qpoint.h" +#include "qwidget.h" +#include "qbuffer.h" +#include "qgif.h" +#include "qregexp.h" +#include "qdir.h" +#include + +// both a struct for storing stuff in and a wrapper to avoid polluting +// the name space + +class TQDragObjectData +{ +public: + TQDragObjectData(): hot(0,0) {} + TQPixmap pixmap; + TQPoint hot; + // store default cursors + TQPixmap *pm_cursor; +}; + +static TQWidget* last_target; + +/*! + After the drag completes, this function will return the TQWidget + which received the drop, or 0 if the data was dropped on another + application. + + This can be useful for detecting the case where drag and drop is + to and from the same widget. +*/ +TQWidget * TQDragObject::target() +{ + return last_target; +} + +/*! + \internal + Sets the target. +*/ +void TQDragObject::setTarget(TQWidget* t) +{ + last_target = t; +} + +class TQStoredDragData +{ +public: + TQStoredDragData() {} + const char* fmt; + TQByteArray enc; +}; + + +// These pixmaps approximate the images in the Windows User Interface Guidelines. + +// XPM + +static const char * const move_xpm[] = { +"11 20 3 1", +". c None", +#if defined(Q_WS_WIN) +"a c #000000", +"X c #FFFFFF", // Windows cursor is traditionally white +#else +"a c #FFFFFF", +"X c #000000", // X11 cursor is traditionally black +#endif +"aa.........", +"aXa........", +"aXXa.......", +"aXXXa......", +"aXXXXa.....", +"aXXXXXa....", +"aXXXXXXa...", +"aXXXXXXXa..", +"aXXXXXXXXa.", +"aXXXXXXXXXa", +"aXXXXXXaaaa", +"aXXXaXXa...", +"aXXaaXXa...", +"aXa..aXXa..", +"aa...aXXa..", +"a.....aXXa.", +"......aXXa.", +".......aXXa", +".......aXXa", +"........aa."}; + +/* XPM */ +static const char * const copy_xpm[] = { +"24 30 3 1", +". c None", +"a c #000000", +"X c #FFFFFF", +#if defined(Q_WS_WIN) // Windows cursor is traditionally white +"aa......................", +"aXa.....................", +"aXXa....................", +"aXXXa...................", +"aXXXXa..................", +"aXXXXXa.................", +"aXXXXXXa................", +"aXXXXXXXa...............", +"aXXXXXXXXa..............", +"aXXXXXXXXXa.............", +"aXXXXXXaaaa.............", +"aXXXaXXa................", +"aXXaaXXa................", +"aXa..aXXa...............", +"aa...aXXa...............", +"a.....aXXa..............", +"......aXXa..............", +".......aXXa.............", +".......aXXa.............", +"........aa...aaaaaaaaaaa", +#else +"XX......................", +"XaX.....................", +"XaaX....................", +"XaaaX...................", +"XaaaaX..................", +"XaaaaaX.................", +"XaaaaaaX................", +"XaaaaaaaX...............", +"XaaaaaaaaX..............", +"XaaaaaaaaaX.............", +"XaaaaaaXXXX.............", +"XaaaXaaX................", +"XaaXXaaX................", +"XaX..XaaX...............", +"XX...XaaX...............", +"X.....XaaX..............", +"......XaaX..............", +".......XaaX.............", +".......XaaX.............", +"........XX...aaaaaaaaaaa", +#endif +".............aXXXXXXXXXa", +".............aXXXXXXXXXa", +".............aXXXXaXXXXa", +".............aXXXXaXXXXa", +".............aXXaaaaaXXa", +".............aXXXXaXXXXa", +".............aXXXXaXXXXa", +".............aXXXXXXXXXa", +".............aXXXXXXXXXa", +".............aaaaaaaaaaa"}; + +/* XPM */ +static const char * const link_xpm[] = { +"24 30 3 1", +". c None", +"a c #000000", +"X c #FFFFFF", +#if defined(Q_WS_WIN) // Windows cursor is traditionally white +"aa......................", +"aXa.....................", +"aXXa....................", +"aXXXa...................", +"aXXXXa..................", +"aXXXXXa.................", +"aXXXXXXa................", +"aXXXXXXXa...............", +"aXXXXXXXXa..............", +"aXXXXXXXXXa.............", +"aXXXXXXaaaa.............", +"aXXXaXXa................", +"aXXaaXXa................", +"aXa..aXXa...............", +"aa...aXXa...............", +"a.....aXXa..............", +"......aXXa..............", +".......aXXa.............", +".......aXXa.............", +"........aa...aaaaaaaaaaa", +#else +"XX......................", +"XaX.....................", +"XaaX....................", +"XaaaX...................", +"XaaaaX..................", +"XaaaaaX.................", +"XaaaaaaX................", +"XaaaaaaaX...............", +"XaaaaaaaaX..............", +"XaaaaaaaaaX.............", +"XaaaaaaXXXX.............", +"XaaaXaaX................", +"XaaXXaaX................", +"XaX..XaaX...............", +"XX...XaaX...............", +"X.....XaaX..............", +"......XaaX..............", +".......XaaX.............", +".......XaaX.............", +"........XX...aaaaaaaaaaa", +#endif +".............aXXXXXXXXXa", +".............aXXXaaaaXXa", +".............aXXXXaaaXXa", +".............aXXXaaaaXXa", +".............aXXaaaXaXXa", +".............aXXaaXXXXXa", +".............aXXaXXXXXXa", +".............aXXXaXXXXXa", +".............aXXXXXXXXXa", +".............aaaaaaaaaaa"}; + +#ifndef QT_NO_DRAGANDDROP + +// the universe's only drag manager +TQDragManager * qt_dnd_manager = 0; + + +TQDragManager::TQDragManager() + : TQObject( qApp, "global drag manager" ) +{ + n_cursor = 3; + pm_cursor = new TQPixmap[n_cursor]; + pm_cursor[0] = TQPixmap((const char **)move_xpm); + pm_cursor[1] = TQPixmap((const char **)copy_xpm); + pm_cursor[2] = TQPixmap((const char **)link_xpm); +#if defined(Q_WS_X11) + createCursors(); // Xcursors cache can hold only 8 bitmaps (4 cursors) +#endif + object = 0; + dragSource = 0; + dropWidget = 0; + if ( !qt_dnd_manager ) + qt_dnd_manager = this; + beingCancelled = FALSE; + restoreCursor = FALSE; + willDrop = FALSE; +} + + +TQDragManager::~TQDragManager() +{ +#ifndef QT_NO_CURSOR + if ( restoreCursor ) + TQApplication::restoreOverrideCursor(); +#endif + qt_dnd_manager = 0; + delete [] pm_cursor; +} + +#endif + + +/*! + Constructs a drag object called \a name, which is a child of \a + dragSource. + + Note that the drag object will be deleted when \a dragSource is + deleted. +*/ + +TQDragObject::TQDragObject( TQWidget * dragSource, const char * name ) + : TQObject( dragSource, name ) +{ + d = new TQDragObjectData(); + d->pm_cursor = 0; +#ifndef QT_NO_DRAGANDDROP + if ( !qt_dnd_manager && qApp ) + (void)new TQDragManager(); +#endif +} + + +/*! + Destroys the drag object, canceling any drag and drop operation in + which it is involved, and frees up the storage used. +*/ + +TQDragObject::~TQDragObject() +{ +#ifndef QT_NO_DRAGANDDROP + if ( qt_dnd_manager && qt_dnd_manager->object == this ) + qt_dnd_manager->cancel( FALSE ); + if ( d->pm_cursor ) { + for ( int i = 0; i < qt_dnd_manager->n_cursor; i++ ) + qt_dnd_manager->pm_cursor[i] = d->pm_cursor[i]; + delete [] d->pm_cursor; + } +#endif + delete d; +} + +#ifndef QT_NO_DRAGANDDROP +/*! + Set the pixmap \a pm to display while dragging the object. The + platform-specific implementation will use this where it can - so + provide a small masked pixmap, and do not assume that the user + will actually see it. For example, cursors on Windows 95 are of + limited size. + + The \a hotspot is the point on (or off) the pixmap that should be + under the cursor as it is dragged. It is relative to the top-left + pixel of the pixmap. + + \warning We have seen problems with drag cursors on different + graphics hardware and driver software on Windows. Setting the + graphics acceleration in the display settings down one tick solved + the problems in all cases. +*/ +void TQDragObject::setPixmap(TQPixmap pm, const TQPoint& hotspot) +{ + d->pixmap = pm; + d->hot = hotspot; + if ( qt_dnd_manager && qt_dnd_manager->object == this ) + qt_dnd_manager->updatePixmap(); +} + +/*! + \overload + Uses a hotspot that positions the pixmap below and to the right of + the mouse pointer. This allows the user to clearly see the point + on the window which they are dragging the data onto. +*/ +void TQDragObject::setPixmap(TQPixmap pm) +{ + setPixmap(pm,TQPoint(-10, -10)); +} + +/*! + Returns the currently set pixmap (which \link TQPixmap::isNull() + isNull()\endlink if none is set). +*/ +TQPixmap TQDragObject::pixmap() const +{ + return d->pixmap; +} + +/*! + Returns the currently set pixmap hotspot. +*/ +TQPoint TQDragObject::pixmapHotSpot() const +{ + return d->hot; +} + +#if 0 + +// ## reevaluate for TQt 4 +/*! + Set the \a cursor used when dragging in mode \a m. + Note: X11 only allow bitmaps for cursors. +*/ +void TQDragObject::setCursor( DragMode m, const TQPixmap &cursor ) +{ + if ( d->pm_cursor == 0 ) { + // safe default cursors + d->pm_cursor = new TQPixmap[qt_dnd_manager->n_cursor]; + for ( int i = 0; i < qt_dnd_manager->n_cursor; i++ ) + d->pm_cursor[i] = qt_dnd_manager->pm_cursor[i]; + } + + int index; + switch ( m ) { + case DragCopy: + index = 1; + break; + case DragLink: + index = 2; + break; + default: + index = 0; + break; + } + + // override default cursor + for ( index = 0; index < qt_dnd_manager->n_cursor; index++ ) + qt_dnd_manager->pm_cursor[index] = cursor; +} + +/*! + Returns the cursor used when dragging in mode \a m, or null if no cursor + has been set for that mode. +*/ +TQPixmap *TQDragObject::cursor( DragMode m ) const +{ + if ( !d->pm_cursor ) + return 0; + + int index; + switch ( m ) { + case DragCopy: + index = 1; + break; + case DragLink: + index = 2; + break; + default: + index = 0; + break; + } + + return qt_dnd_manager->pm_cursor+index; +} + +#endif // 0 + +/*! + Starts a drag operation using the contents of this object, using + DragDefault mode. + + The function returns TRUE if the caller should delete the original + copy of the dragged data (but see target()); otherwise returns + FALSE. + + If the drag contains \e references to information (e.g. file names + in a TQUriDrag are references) then the return value should always + be ignored, as the target is expected to manipulate the + referred-to content directly. On X11 the return value should + always be correct anyway, but on Windows this is not necessarily + the case (e.g. the file manager starts a background process to + move files, so the source \e{must not} delete the files!) +*/ +bool TQDragObject::drag() +{ + return drag( DragDefault ); +} + + +/*! + Starts a drag operation using the contents of this object, using + \c DragMove mode. Be sure to read the constraints described in + drag(). + + \sa drag() dragCopy() dragLink() +*/ +bool TQDragObject::dragMove() +{ + return drag( DragMove ); +} + + +/*! + Starts a drag operation using the contents of this object, using + \c DragCopy mode. Be sure to read the constraints described in + drag(). + + \sa drag() dragMove() dragLink() +*/ +void TQDragObject::dragCopy() +{ + (void)drag( DragCopy ); +} + +/*! + Starts a drag operation using the contents of this object, using + \c DragLink mode. Be sure to read the constraints described in + drag(). + + \sa drag() dragCopy() dragMove() +*/ +void TQDragObject::dragLink() +{ + (void)drag( DragLink ); +} + + +/*! + \enum TQDragObject::DragMode + + This enum describes the possible drag modes. + + \value DragDefault The mode is determined heuristically. + \value DragCopy The data is copied, never moved. + \value DragMove The data is moved, if dragged at all. + \value DragLink The data is linked, if dragged at all. + \value DragCopyOrMove The user chooses the mode by using a + control key to switch from the default. +*/ + + +/*! + \overload + Starts a drag operation using the contents of this object. + + At this point, the object becomes owned by TQt, not the + application. You should not delete the drag object or anything it + references. The actual transfer of data to the target application + will be done during future event processing - after that time the + drag object will be deleted. + + Returns TRUE if the dragged data was dragged as a \e move, + indicating that the caller should remove the original source of + the data (the drag object must continue to have a copy); otherwise + returns FALSE. + + The \a mode specifies the drag mode (see + \l{TQDragObject::DragMode}.) Normally one of the simpler drag(), + dragMove(), or dragCopy() functions would be used instead. +*/ +bool TQDragObject::drag( DragMode mode ) +{ + if ( qt_dnd_manager ) + return qt_dnd_manager->drag( this, mode ); + else + return FALSE; +} + +#endif + + +/*! + Returns a pointer to the drag source where this object originated. +*/ + +TQWidget * TQDragObject::source() +{ + if ( parent() && parent()->isWidgetType() ) + return (TQWidget *)parent(); + else + return 0; +} + + +/*! + \class TQDragObject qdragobject.h + + \brief The TQDragObject class encapsulates MIME-based data + transfer. + + \ingroup draganddrop + + TQDragObject is the base class for all data that needs to be + transferred between and within applications, both for drag and + drop and for the \link qclipboard.html clipboard\endlink. + + See the \link dnd.html Drag-and-drop documentation\endlink for an + overview of how to provide drag and drop in your application. + + See the TQClipboard documentation for an overview of how to provide + cut-and-paste in your application. + + The drag() function is used to start a drag operation. You can + specify the \l DragMode in the call or use one of the convenience + functions dragCopy(), dragMove() or dragLink(). The drag source + where the data originated is retrieved with source(). If the data + was dropped on a widget within the application, target() will + return a pointer to that widget. Specify the pixmap to display + during the drag with setPixmap(). +*/ + +static +void stripws(TQCString& s) +{ + int f; + while ( (f=s.find(' ')) >= 0 ) + s.remove(f,1); +} + +static +const char * staticCharset(int i) +{ + static TQCString localcharset; + + switch ( i ) { + case 0: + return "UTF-8"; + case 1: + return "ISO-10646-UCS-2"; + case 2: + return ""; // in the 3rd place - some Xdnd targets might only look at 3 + case 3: + if ( localcharset.isNull() ) { + TQTextCodec *localCodec = TQTextCodec::codecForLocale(); + if ( localCodec ) { + localcharset = localCodec->name(); + localcharset = localcharset.lower(); + stripws(localcharset); + } else { + localcharset = ""; + } + } + return localcharset; + } + return 0; +} + + +class TQTextDragPrivate { +public: + TQTextDragPrivate(); + + enum { nfmt=4 }; + + TQString txt; + TQCString fmt[nfmt]; + TQCString subtype; + + void setSubType(const TQCString & st) + { + subtype = st.lower(); + for ( int i=0; isetSubType(st); +} + +/*! + \class TQTextDrag qdragobject.h + + \brief The TQTextDrag class is a drag and drop object for + transferring plain and Unicode text. + + \ingroup draganddrop + + Plain text is passed in a TQString which may contain multiple lines + (i.e. may contain newline characters). The drag target will receive + the newlines according to the runtime environment, e.g. LF on Unix, + and CRLF on Windows. + + TQt provides no built-in mechanism for delivering only a single-line. + + For more information about drag and drop, see the TQDragObject class + and the \link dnd.html drag and drop documentation\endlink. +*/ + + +/*! + Constructs a text drag object and sets its data to \a text. \a + dragSource must be the drag source; \a name is the object name. +*/ + +TQTextDrag::TQTextDrag( const TQString &text, + TQWidget * dragSource, const char * name ) + : TQDragObject( dragSource, name ) +{ + d = new TQTextDragPrivate; + setText( text ); +} + + +/*! + Constructs a default text drag object. \a dragSource must be the + drag source; \a name is the object name. +*/ + +TQTextDrag::TQTextDrag( TQWidget * dragSource, const char * name ) + : TQDragObject( dragSource, name ) +{ + d = new TQTextDragPrivate; +} + + +/*! + Destroys the text drag object and frees up all allocated + resources. +*/ +TQTextDrag::~TQTextDrag() +{ + delete d; +} + + +/*! + Sets the text to be dragged to \a text. You will need to call this + if you did not pass the text during construction. +*/ +void TQTextDrag::setText( const TQString &text ) +{ + d->txt = text; +} + + +/*! + \reimp +*/ +const char * TQTextDrag::format(int i) const +{ + if ( i >= d->nfmt ) + return 0; + return d->fmt[i]; +} + +TQTextCodec* qt_findcharset(const TQCString& mimetype) +{ + int i=mimetype.find("charset="); + if ( i >= 0 ) { + TQCString cs = mimetype.mid(i+8); + stripws(cs); + i = cs.find(';'); + if ( i >= 0 ) + cs = cs.left(i); + // win98 often has charset=utf16, and we need to get the correct codec for + // it to be able to get Unicode text drops. + if ( cs == "utf16" ) + cs = "ISO-10646-UCS-2"; + // May return 0 if unknown charset + return TQTextCodec::codecForName(cs); + } + // no charset=, use locale + return TQTextCodec::codecForLocale(); +} + +static TQTextCodec *codecForHTML(const TQCString &ba) +{ + // determine charset + int mib = 0; + int pos; + TQTextCodec *c = 0; + + if (ba.size() > 1 && (((uchar)ba[0] == 0xfe && (uchar)ba[1] == 0xff) + || ((uchar)ba[0] == 0xff && (uchar)ba[1] == 0xfe))) { + mib = 1000; // utf16 + } else if (ba.size() > 2 + && (uchar)ba[0] == 0xef + && (uchar)ba[1] == 0xbb + && (uchar)ba[2] == 0xbf) { + mib = 106; // utf-8 + } else { + pos = 0; + while ((pos = ba.find("format(i)); i++ ) { + bool html = !qstrnicmp(f, "text/html", 9); + if (html) + r = codecForHTML(TQCString(e->encodedData(f))); + if (!r) + r = qt_findcharset(TQCString(f).lower()); + if (r) + return r; + } + return 0; +} + + + +/*! + \reimp +*/ +TQByteArray TQTextDrag::encodedData(const char* mime) const +{ + TQCString r; + if ( 0==qstrnicmp(mime,"text/",5) ) { + TQCString m(mime); + m = m.lower(); + TQTextCodec *codec = qt_findcharset(m); + if ( !codec ) + return r; + TQString text( d->txt ); +#if defined(Q_WS_WIN) + int index = text.find( TQString::fromLatin1("\r\n"), 0 ); + while ( index != -1 ) { + text.replace( index, 2, TQChar('\n') ); + index = text.find( "\r\n", index ); + } +#endif + r = codec->fromUnicode(text); + if (!codec || codec->mibEnum() != 1000) { + // Don't include NUL in size (TQCString::resize() adds NUL) +#if defined(Q_WS_WIN) + // This is needed to ensure the \0 isn't lost on Windows 95 + if ( qWinVersion() & TQt::WV_DOS_based ) + ((TQByteArray&)r).resize(r.length()+1); + else +#endif + ((TQByteArray&)r).resize(r.length()); + } + } + return r; +} + +/*! + Returns TRUE if the information in \a e can be decoded into a + TQString; otherwise returns FALSE. + + \sa decode() +*/ +bool TQTextDrag::canDecode( const TQMimeSource* e ) +{ + const char* f; + for (int i=0; (f=e->format(i)); i++) { + if ( 0==qstrnicmp(f,"text/",5) ) { + return findcodec(e) != 0; + } + } + return 0; +} + +/*! + \overload + + Attempts to decode the dropped information in \a e into \a str. + Returns TRUE if successful; otherwise returns FALSE. If \a subtype + is null, any text subtype is accepted; otherwise only the + specified \a subtype is accepted. + + \sa canDecode() +*/ +bool TQTextDrag::decode( const TQMimeSource* e, TQString& str, TQCString& subtype ) +{ + if(!e) + return FALSE; + + // when subtype is not specified, try text/plain first, otherwise this may read + // things like text/x-moz-url even though better targets are available + if( subtype.isNull()) { + TQCString subtmp = "plain"; + if( decode( e, str, subtmp )) { + subtype = subtmp; + return true; + } + } + + if ( e->cacheType == TQMimeSource::Text ) { + str = *e->cache.txt.str; + subtype = *e->cache.txt.subtype; + return TRUE; + } + + const char* mime; + for (int i=0; (mime = e->format(i)); i++) { + if ( 0==qstrnicmp(mime,"text/",5) ) { + TQCString m(mime); + m = m.lower(); + int semi = m.find(';'); + if ( semi < 0 ) + semi = m.length(); + TQCString foundst = m.mid(5,semi-5); + if ( subtype.isNull() || foundst == subtype ) { + bool html = !qstrnicmp(mime, "text/html", 9); + TQTextCodec* codec = 0; + if (html) { + TQByteArray bytes = e->encodedData(mime); + // search for the charset tag in the HTML + codec = codecForHTML(TQCString(bytes.data(), bytes.size())); + } + if (!codec) + codec = qt_findcharset(m); + if ( codec ) { + TQByteArray payload; + + payload = e->encodedData(mime); + if ( payload.size() ) { + int l; + if ( codec->mibEnum() != 1000) { + // length is at NUL or payload.size() + l = 0; + while ( l < (int)payload.size() && payload[l] ) + l++; + } else { + l = payload.size(); + } + + str = codec->toUnicode(payload,l); + + if ( subtype.isNull() ) + subtype = foundst; + + TQMimeSource *m = (TQMimeSource*)e; + m->clearCache(); + m->cacheType = TQMimeSource::Text; + m->cache.txt.str = new TQString( str ); + m->cache.txt.subtype = new TQCString( subtype ); + + return TRUE; + } + } + } + } + } + return FALSE; +} + +/*! + Attempts to decode the dropped information in \a e into \a str. + Returns TRUE if successful; otherwise returns FALSE. + + \sa canDecode() +*/ +bool TQTextDrag::decode( const TQMimeSource* e, TQString& str ) +{ + TQCString st; + return decode(e,str,st); +} + + +/* + TQImageDrag could use an internal MIME type for communicating TQPixmaps + and TQImages rather than always converting to raw data. This is available + for that purpose and others. It is not currently used. +*/ +class TQImageDragData +{ +public: +}; + + +/*! + \class TQImageDrag qdragobject.h + + \brief The TQImageDrag class provides a drag and drop object for + transferring images. + + \ingroup draganddrop + + Images are offered to the receiving application in multiple + formats, determined by TQt's \link TQImage::outputFormats() output + formats\endlink. + + For more information about drag and drop, see the TQDragObject + class and the \link dnd.html drag and drop documentation\endlink. +*/ + +/*! + Constructs an image drag object and sets its data to \a image. \a + dragSource must be the drag source; \a name is the object name. +*/ + +TQImageDrag::TQImageDrag( TQImage image, + TQWidget * dragSource, const char * name ) + : TQDragObject( dragSource, name ), + d(0) +{ + setImage( image ); +} + +/*! + Constructs a default image drag object. \a dragSource must be the + drag source; \a name is the object name. +*/ + +TQImageDrag::TQImageDrag( TQWidget * dragSource, const char * name ) + : TQDragObject( dragSource, name ), + d(0) +{ +} + + +/*! + Destroys the image drag object and frees up all allocated + resources. +*/ + +TQImageDrag::~TQImageDrag() +{ + // nothing +} + + +/*! + Sets the image to be dragged to \a image. You will need to call + this if you did not pass the image during construction. +*/ +void TQImageDrag::setImage( TQImage image ) +{ + img = image; // ### detach? + ofmts = TQImage::outputFormats(); + ofmts.remove("PBM"); // remove non-raw PPM + if ( image.depth()!=32 ) { + // BMP better than PPM for paletted images + if ( ofmts.remove("BMP") ) // move to front + ofmts.insert(0,"BMP"); + } + // PNG is best of all + if ( ofmts.remove("PNG") ) // move to front + ofmts.insert(0,"PNG"); + + if(cacheType == TQMimeSource::NoCache) { //cache it + cacheType = TQMimeSource::Graphics; + cache.gfx.img = new TQImage( img ); + cache.gfx.pix = 0; + } +} + +/*! + \reimp +*/ +const char * TQImageDrag::format(int i) const +{ + if ( i < (int)ofmts.count() ) { + static TQCString str; + str.sprintf("image/%s",(((TQImageDrag*)this)->ofmts).at(i)); + str = str.lower(); + if ( str == "image/pbmraw" ) + str = "image/ppm"; + return str; + } else { + return 0; + } +} + +/*! + \reimp +*/ +TQByteArray TQImageDrag::encodedData(const char* fmt) const +{ + if ( qstrnicmp( fmt, "image/", 6 )==0 ) { + TQCString f = fmt+6; + TQByteArray data; + TQBuffer w( data ); + w.open( IO_WriteOnly ); + TQImageIO io( &w, f.upper() ); + io.setImage( img ); + if ( !io.write() ) + return TQByteArray(); + w.close(); + return data; + } else { + return TQByteArray(); + } +} + +/*! + Returns TRUE if the information in mime source \a e can be decoded + into an image; otherwise returns FALSE. + + \sa decode() +*/ +bool TQImageDrag::canDecode( const TQMimeSource* e ) { + TQStrList fileFormats = TQImageIO::inputFormats(); + + fileFormats.first(); + while ( fileFormats.current()) { + TQCString format = fileFormats.current(); + TQCString type = "image/" + format.lower(); + if ( e->provides(type.data())) + return TRUE; + fileFormats.next(); + } + + return FALSE; +} + +/*! + Attempts to decode the dropped information in mime source \a e + into \a img. Returns TRUE if successful; otherwise returns FALSE. + + \sa canDecode() +*/ +bool TQImageDrag::decode( const TQMimeSource* e, TQImage& img ) +{ + if ( !e ) + return FALSE; + if ( e->cacheType == TQMimeSource::Graphics ) { + img = *e->cache.gfx.img; + return TRUE; + } + + TQByteArray payload; + TQStrList fileFormats = TQImageIO::inputFormats(); + // PNG is best of all + if ( fileFormats.remove("PNG") ) // move to front + fileFormats.insert(0,"PNG"); + fileFormats.first(); + while ( fileFormats.current() ) { + TQCString format = fileFormats.current(); + fileFormats.next(); + + TQCString type = "image/" + format.lower(); + if ( ! e->provides( type.data() ) ) continue; + payload = e->encodedData( type.data() ); + if ( !payload.isEmpty() ) + break; + } + + if ( payload.isEmpty() ) + return FALSE; + + img.loadFromData(payload); + if ( img.isNull() ) + return FALSE; + TQMimeSource *m = (TQMimeSource*)e; + m->clearCache(); + m->cacheType = TQMimeSource::Graphics; + m->cache.gfx.img = new TQImage( img ); + m->cache.gfx.pix = 0; + return TRUE; +} + +/*! + \overload + + Attempts to decode the dropped information in mime source \a e + into pixmap \a pm. Returns TRUE if successful; otherwise returns + FALSE. + + This is a convenience function that converts to a TQPixmap via a + TQImage. + + \sa canDecode() +*/ +bool TQImageDrag::decode( const TQMimeSource* e, TQPixmap& pm ) +{ + if ( !e ) + return FALSE; + + if ( e->cacheType == TQMimeSource::Graphics && e->cache.gfx.pix) { + pm = *e->cache.gfx.pix; + return TRUE; + } + + TQImage img; + // We avoid dither, since the image probably came from this display + if ( decode( e, img ) ) { + if ( !pm.convertFromImage( img, AvoidDither ) ) + return FALSE; + // decode initialized the cache for us + + TQMimeSource *m = (TQMimeSource*)e; + m->cache.gfx.pix = new TQPixmap( pm ); + return TRUE; + } + return FALSE; +} + + + + +/*! + \class TQStoredDrag qdragobject.h + \brief The TQStoredDrag class provides a simple stored-value drag object for arbitrary MIME data. + + \ingroup draganddrop + + When a block of data has only one representation, you can use a + TQStoredDrag to hold it. + + For more information about drag and drop, see the TQDragObject + class and the \link dnd.html drag and drop documentation\endlink. +*/ + +/*! + Constructs a TQStoredDrag. The \a dragSource and \a name are passed + to the TQDragObject constructor, and the format is set to \a + mimeType. + + The data will be unset. Use setEncodedData() to set it. +*/ +TQStoredDrag::TQStoredDrag( const char* mimeType, TQWidget * dragSource, const char * name ) : + TQDragObject(dragSource,name) +{ + d = new TQStoredDragData(); + d->fmt = qstrdup(mimeType); +} + +/*! + Destroys the drag object and frees up all allocated resources. +*/ +TQStoredDrag::~TQStoredDrag() +{ + delete [] (char*)d->fmt; + delete d; +} + +/*! + \reimp +*/ +const char * TQStoredDrag::format(int i) const +{ + if ( i==0 ) + return d->fmt; + else + return 0; +} + + +/*! + Sets the encoded data of this drag object to \a encodedData. The + encoded data is what's delivered to the drop sites. It must be in + a strictly defined and portable format. + + The drag object can't be dropped (by the user) until this function + has been called. +*/ + +void TQStoredDrag::setEncodedData( const TQByteArray & encodedData ) +{ + d->enc = encodedData.copy(); +} + +/*! + Returns the stored data. \a m contains the data's format. + + \sa setEncodedData() +*/ +TQByteArray TQStoredDrag::encodedData(const char* m) const +{ + if ( !qstricmp(m,d->fmt) ) + return d->enc; + else + return TQByteArray(); +} + + +/*! + \class TQUriDrag qdragobject.h + \brief The TQUriDrag class provides a drag object for a list of URI references. + + \ingroup draganddrop + + URIs are a useful way to refer to files that may be distributed + across multiple machines. A URI will often refer to a file on a + machine local to both the drag source and the drop target, so the + URI can be equivalent to passing a file name but is more + extensible. + + Use URIs in Unicode form so that the user can comfortably edit and + view them. For use in HTTP or other protocols, use the correctly + escaped ASCII form. + + You can convert a list of file names to file URIs using + setFileNames(), or into human-readble form with setUnicodeUris(). + + Static functions are provided to convert between filenames and + URIs, e.g. uriToLocalFile() and localFileToUri(), and to and from + human-readable form, e.g. uriToUnicodeUri(), unicodeUriToUri(). + You can also decode URIs from a mimesource into a list with + decodeLocalFiles() and decodeToUnicodeUris(). +*/ + +/*! + Constructs an object to drag the list of URIs in \a uris. The \a + dragSource and \a name arguments are passed on to TQStoredDrag. + Note that URIs are always in escaped UTF8 encoding. +*/ +TQUriDrag::TQUriDrag( TQStrList uris, + TQWidget * dragSource, const char * name ) : + TQStoredDrag( "text/uri-list", dragSource, name ) +{ + setUris(uris); +} + +/*! + Constructs an object to drag. You must call setUris() before you + start the drag(). Passes \a dragSource and \a name to the + TQStoredDrag constructor. +*/ +TQUriDrag::TQUriDrag( TQWidget * dragSource, const char * name ) : + TQStoredDrag( "text/uri-list", dragSource, name ) +{ +} + +/*! + Destroys the object. +*/ +TQUriDrag::~TQUriDrag() +{ +} + +/*! + Changes the list of \a uris to be dragged. + + Note that URIs are always in escaped UTF8 encoding. +*/ +void TQUriDrag::setUris( TQStrList uris ) +{ + TQByteArray a; + int c=0; + for ( const char* s = uris.first(); s; s = uris.next() ) { + int l = qstrlen(s); + a.resize(c+l+2); + memcpy(a.data()+c,s,l); + memcpy(a.data()+c+l,"\r\n",2); + c+=l+2; + } + a.resize(c+1); + a[c] = 0; + setEncodedData(a); +} + + +/*! + Returns TRUE if decode() would be able to decode \a e; otherwise + returns FALSE. +*/ +bool TQUriDrag::canDecode( const TQMimeSource* e ) +{ + return e->provides( "text/uri-list" ); +} + +/*! + Decodes URIs from \a e, placing the result in \a l (which is first + cleared). + + Returns TRUE if \a e contained a valid list of URIs; otherwise + returns FALSE. +*/ +bool TQUriDrag::decode( const TQMimeSource* e, TQStrList& l ) +{ + TQByteArray payload = e->encodedData( "text/uri-list" ); + if ( payload.size() ) { + l.clear(); + l.setAutoDelete(TRUE); + uint c=0; + const char* d = payload.data(); + while (c < payload.size() && d[c]) { + uint f = c; + // Find line end + while (c < payload.size() && d[c] && d[c]!='\r' + && d[c] != '\n') + c++; + TQCString s(d+f,c-f+1); + if ( s[0] != '#' ) // non-comment? + l.append( s ); + // Skip junk + while (c < payload.size() && d[c] && + (d[c]=='\n' || d[c]=='\r')) + c++; + } + return TRUE; + } + return FALSE; +} + +static uint htod( int h ) +{ + if ( isdigit(h) ) + return h - '0'; + return tolower( h ) - 'a' + 10; +} + +/*! + \fn TQUriDrag::setFilenames( const TQStringList & ) + \obsolete + + Use setFileNames() instead (notice the N). +*/ + +/*! + Sets the URIs to be the local-file URIs equivalent to \a fnames. + + \sa localFileToUri(), setUris() +*/ +void TQUriDrag::setFileNames( const TQStringList & fnames ) +{ + TQStrList uris; + for ( TQStringList::ConstIterator i = fnames.begin(); + i != fnames.end(); ++i ) { + TQCString fileUri = localFileToUri(*i); + if (!fileUri.isEmpty()) + uris.append(fileUri); + } + setUris( uris ); +} + +/*! + Sets the URIs in \a uuris to be the Unicode URIs (only useful for + displaying to humans). + + \sa localFileToUri(), setUris() +*/ +void TQUriDrag::setUnicodeUris( const TQStringList & uuris ) +{ + TQStrList uris; + for ( TQStringList::ConstIterator i = uuris.begin(); + i != uuris.end(); ++i ) + uris.append( unicodeUriToUri(*i) ); + setUris( uris ); +} + +/*! + Returns the URI equivalent of the Unicode URI given in \a uuri + (only useful for displaying to humans). + + \sa uriToLocalFile() +*/ +TQCString TQUriDrag::unicodeUriToUri(const TQString& uuri) +{ + TQCString utf8 = uuri.utf8(); + TQCString escutf8; + int n = utf8.length(); + bool isFile = uuri.startsWith("file://"); + for (int i=0; i= 'a' && utf8[i] <= 'z' + || utf8[i] == '/' + || utf8[i] >= '0' && utf8[i] <= '9' + || utf8[i] >= 'A' && utf8[i] <= 'Z' + + || utf8[i] == '-' || utf8[i] == '_' + || utf8[i] == '.' || utf8[i] == '!' + || utf8[i] == '~' || utf8[i] == '*' + || utf8[i] == '(' || utf8[i] == ')' + || utf8[i] == '\'' + + // Allow this through, so that all URI-references work. + || (!isFile && utf8[i] == '#') + + || utf8[i] == ';' + || utf8[i] == '?' || utf8[i] == ':' + || utf8[i] == '@' || utf8[i] == '&' + || utf8[i] == '=' || utf8[i] == '+' + || utf8[i] == '$' || utf8[i] == ',' ) + { + escutf8 += utf8[i]; + } else { + // Everything else is escaped as %HH + TQCString s(4); + s.sprintf("%%%02x",(uchar)utf8[i]); + escutf8 += s; + } + } + return escutf8; +} + +/*! + Returns the URI equivalent to the absolute local file \a filename. + + \sa uriToLocalFile() +*/ +TQCString TQUriDrag::localFileToUri(const TQString& filename) +{ + TQString r = filename; + + //check that it is an absolute file + if (TQDir::isRelativePath(r)) + return TQCString(); + +#ifdef Q_WS_WIN + + + bool hasHost = FALSE; + // convert form network path + if (r.left(2) == "\\\\" || r.left(2) == "//") { + r.remove(0, 2); + hasHost = TRUE; + } + + // Slosh -> Slash + int slosh; + while ( (slosh=r.find('\\')) >= 0 ) { + r[slosh] = '/'; + } + + // Drive + if ( r[0] != '/' && !hasHost) + r.insert(0,'/'); + +#endif +#if defined ( Q_WS_X11 ) && 0 + // URL without the hostname is considered to be errorneous by XDnD. + // See: http://www.newplanetsoftware.com/xdnd/dragging_files.html + // This feature is not active because this would break dnd between old and new qt apps. + char hostname[257]; + if ( gethostname( hostname, 255 ) == 0 ) { + hostname[256] = '\0'; + r.prepend( TQString::fromLatin1( hostname ) ); + } +#endif + return unicodeUriToUri(TQString("file://" + r)); +} + +/*! + Returns the Unicode URI (only useful for displaying to humans) + equivalent of \a uri. + + Note that URIs are always in escaped UTF8 encoding. + + \sa localFileToUri() +*/ +TQString TQUriDrag::uriToUnicodeUri(const char* uri) +{ + TQCString utf8; + + while (*uri) { + switch (*uri) { + case '%': { + uint ch = (uchar) uri[1]; + if ( ch && uri[2] ) { + ch = htod( ch ) * 16 + htod( (uchar) uri[2] ); + utf8 += (char) ch; + uri += 2; + } + } + break; + default: + utf8 += *uri; + } + ++uri; + } + + return TQString::fromUtf8(utf8); +} + +/*! + Returns the name of a local file equivalent to \a uri or a null + string if \a uri is not a local file. + + Note that URIs are always in escaped UTF8 encoding. + + \sa localFileToUri() +*/ +TQString TQUriDrag::uriToLocalFile(const char* uri) +{ + TQString file; + + if (!uri) + return file; + if (0==qstrnicmp(uri,"file:/",6)) // It is a local file uri + uri += 6; + else if (TQString(uri).find(":/") != -1) // It is a different scheme uri + return file; + + bool local = uri[0] != '/' || ( uri[0] != '\0' && uri[1] == '/' ); +#ifdef Q_WS_X11 + // do we have a hostname? + if ( !local && uri[0] == '/' && uri[2] != '/' ) { + // then move the pointer to after the 'hostname/' part of the uri + const char* hostname_end = strchr( uri+1, '/' ); + if ( hostname_end != NULL ) { + char hostname[ 257 ]; + if ( gethostname( hostname, 255 ) == 0 ) { + hostname[ 256 ] = '\0'; + if ( qstrncmp( uri+1, hostname, hostname_end - ( uri+1 )) == 0 ) { + uri = hostname_end + 1; // point after the slash + local = TRUE; + } + } + } + } +#endif + if ( local ) { + file = uriToUnicodeUri(uri); + if ( uri[1] == '/' ) { + file.remove((uint)0,1); + } else { + file.insert(0,'/'); + } +#ifdef Q_WS_WIN + if ( file.length() > 2 && file[0] == '/' && file[2] == '|' ) { + file[2] = ':'; + file.remove(0,1); + } else if (file.length() > 2 && file[0] == '/' && file[1].isLetter() && file[2] == ':') { + file.remove(0, 1); + } + // Leave slash as slashes. +#endif + } +#ifdef Q_WS_WIN + else { + file = uriToUnicodeUri(uri); + // convert to network path + file.insert(1, '/'); // leave as forward slashes + } +#endif + + return file; +} + +/*! + Decodes URIs from the mime source event \a e, converts them to + local files if they refer to local files, and places them in \a l + (which is first cleared). + + Returns TRUE if \e contained a valid list of URIs; otherwise + returns FALSE. The list will be empty if no URIs were local files. +*/ +bool TQUriDrag::decodeLocalFiles( const TQMimeSource* e, TQStringList& l ) +{ + TQStrList u; + if ( !decode( e, u ) ) + return FALSE; + + l.clear(); + for (const char* s=u.first(); s; s=u.next()) { + TQString lf = uriToLocalFile(s); + if ( !lf.isNull() ) + l.append( lf ); + } + return TRUE; +} + +/*! + Decodes URIs from the mime source event \a e, converts them to + Unicode URIs (only useful for displaying to humans), placing them + in \a l (which is first cleared). + + Returns TRUE if \e contained a valid list of URIs; otherwise + returns FALSE. +*/ +bool TQUriDrag::decodeToUnicodeUris( const TQMimeSource* e, TQStringList& l ) +{ + TQStrList u; + if ( !decode( e, u ) ) + return FALSE; + + l.clear(); + for (const char* s=u.first(); s; s=u.next()) + l.append( uriToUnicodeUri(s) ); + + return TRUE; +} + + +#ifndef QT_NO_DRAGANDDROP +/*! + If the source of the drag operation is a widget in this + application, this function returns that source, otherwise it + returns 0. The source of the operation is the first parameter to + drag object subclasses. + + This is useful if your widget needs special behavior when dragging + to itself, etc. + + See TQDragObject::TQDragObject() and subclasses. +*/ +TQWidget* TQDropEvent::source() const +{ + return qt_dnd_manager ? qt_dnd_manager->dragSource : 0; +} +#endif + +/*! + \class TQColorDrag qdragobject.h + + \brief The TQColorDrag class provides a drag and drop object for + transferring colors. + + \ingroup draganddrop + + This class provides a drag object which can be used to transfer data + about colors for drag and drop and in the clipboard. For example, it + is used in TQColorDialog. + + The color is set in the constructor but can be changed with + setColor(). + + For more information about drag and drop, see the TQDragObject class + and the \link dnd.html drag and drop documentation\endlink. +*/ + +/*! + Constructs a color drag object with the color \a col. Passes \a + dragsource and \a name to the TQStoredDrag constructor. +*/ + +TQColorDrag::TQColorDrag( const TQColor &col, TQWidget *dragsource, const char *name ) + : TQStoredDrag( "application/x-color", dragsource, name ) +{ + setColor( col ); +} + +/*! + Constructs a color drag object with a white color. Passes \a + dragsource and \a name to the TQStoredDrag constructor. +*/ + +TQColorDrag::TQColorDrag( TQWidget *dragsource, const char *name ) + : TQStoredDrag( "application/x-color", dragsource, name ) +{ + setColor( TQt::white ); +} + +/*! + Sets the color of the color drag to \a col. +*/ + +void TQColorDrag::setColor( const TQColor &col ) +{ + short r = (col.red() << 8) | col.red(); + short g = (col.green() << 8) | col.green(); + short b = (col.blue() << 8) | col.blue(); + + // make sure we transmit data in network order + r = htons(r); + g = htons(g); + b = htons(b); + + ushort rgba[4] = { + r, g, b, + 0xffff // Alpha not supported yet. + }; + TQByteArray data(sizeof(rgba)); + memcpy(data.data(), rgba, sizeof(rgba)); + setEncodedData(data); +} + +/*! + Returns TRUE if the color drag object can decode the mime source + \a e; otherwise returns FALSE. +*/ + +bool TQColorDrag::canDecode( TQMimeSource *e ) +{ + return e->provides( "application/x-color" ); +} + +/*! + Decodes the mime source \a e and sets the decoded values to \a + col. +*/ + +bool TQColorDrag::decode( TQMimeSource *e, TQColor &col ) +{ + TQByteArray data = e->encodedData("application/x-color"); + ushort rgba[4]; + if (data.size() != sizeof(rgba)) + return FALSE; + + memcpy(rgba, data.data(), sizeof(rgba)); + + short r = rgba[0]; + short g = rgba[1]; + short b = rgba[2]; + + // data is in network order + r = ntohs(r); + g = ntohs(g); + b = ntohs(b); + + r = (r >> 8) & 0xff; + g = (g >> 8) & 0xff; + b = (b >> 8) & 0xff; + + col.setRgb(r, g, b); + return TRUE; +} + +#endif // QT_NO_MIME diff --git a/src/kernel/qdragobject.h b/src/kernel/qdragobject.h new file mode 100644 index 000000000..84b0291be --- /dev/null +++ b/src/kernel/qdragobject.h @@ -0,0 +1,279 @@ +/**************************************************************************** +** +** Definition of TQDragObject +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQDRAGOBJECT_H +#define TQDRAGOBJECT_H + +class TQWidget; +class TQTextDragPrivate; +class TQDragObjectData; +class TQStoredDragData; +class TQImageDragData; + +#ifndef QT_H +#include "qobject.h" +#include "qimage.h" +#include "qstrlist.h" +#include "qcolor.h" +#endif // QT_H + +#ifndef QT_NO_MIME + +class Q_EXPORT TQDragObject: public TQObject, public TQMimeSource { + Q_OBJECT +public: + TQDragObject( TQWidget * dragSource = 0, const char * name = 0 ); + virtual ~TQDragObject(); + +#ifndef QT_NO_DRAGANDDROP + bool drag(); + bool dragMove(); + void dragCopy(); + void dragLink(); + + virtual void setPixmap(TQPixmap); + virtual void setPixmap(TQPixmap, const TQPoint& hotspot); + TQPixmap pixmap() const; + TQPoint pixmapHotSpot() const; +#endif + + TQWidget * source(); + static TQWidget * target(); + + static void setTarget(TQWidget*); + +#ifndef QT_NO_DRAGANDDROP + enum DragMode { DragDefault, DragCopy, DragMove, DragLink, DragCopyOrMove }; + +protected: + virtual bool drag(DragMode); +#endif + +private: + TQDragObjectData * d; +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQDragObject( const TQDragObject & ); + TQDragObject &operator=( const TQDragObject & ); +#endif +}; + +class Q_EXPORT TQStoredDrag: public TQDragObject { + Q_OBJECT + TQStoredDragData * d; + +public: + TQStoredDrag( const char * mimeType, + TQWidget * dragSource = 0, const char * name = 0 ); + ~TQStoredDrag(); + + virtual void setEncodedData( const TQByteArray & ); + + const char * format(int i) const; + virtual TQByteArray encodedData(const char*) const; + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQStoredDrag( const TQStoredDrag & ); + TQStoredDrag &operator=( const TQStoredDrag & ); +#endif +}; + +class Q_EXPORT TQTextDrag: public TQDragObject { + Q_OBJECT + TQTextDragPrivate* d; +public: + TQTextDrag( const TQString &, + TQWidget * dragSource = 0, const char * name = 0 ); + TQTextDrag( TQWidget * dragSource = 0, const char * name = 0 ); + ~TQTextDrag(); + + virtual void setText( const TQString &); + virtual void setSubtype( const TQCString &); + + const char * format(int i) const; + virtual TQByteArray encodedData(const char*) const; + + static bool canDecode( const TQMimeSource* e ); + static bool decode( const TQMimeSource* e, TQString& s ); + static bool decode( const TQMimeSource* e, TQString& s, TQCString& subtype ); + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQTextDrag( const TQTextDrag & ); + TQTextDrag &operator=( const TQTextDrag & ); +#endif +}; + +class Q_EXPORT TQImageDrag: public TQDragObject { + Q_OBJECT + TQImage img; + TQStrList ofmts; + TQImageDragData* d; + +public: + TQImageDrag( TQImage image, TQWidget * dragSource = 0, const char * name = 0 ); + TQImageDrag( TQWidget * dragSource = 0, const char * name = 0 ); + ~TQImageDrag(); + + virtual void setImage( TQImage image ); + + const char * format(int i) const; + virtual TQByteArray encodedData(const char*) const; + + static bool canDecode( const TQMimeSource* e ); + static bool decode( const TQMimeSource* e, TQImage& i ); + static bool decode( const TQMimeSource* e, TQPixmap& i ); + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQImageDrag( const TQImageDrag & ); + TQImageDrag &operator=( const TQImageDrag & ); +#endif +}; + + +class Q_EXPORT TQUriDrag: public TQStoredDrag { + Q_OBJECT + +public: + TQUriDrag( TQStrList uris, TQWidget * dragSource = 0, const char * name = 0 ); + TQUriDrag( TQWidget * dragSource = 0, const char * name = 0 ); + ~TQUriDrag(); + + void setFilenames( const TQStringList & fnames ) { setFileNames( fnames ); } + void setFileNames( const TQStringList & fnames ); + void setUnicodeUris( const TQStringList & uuris ); + virtual void setUris( TQStrList uris ); + + static TQString uriToLocalFile(const char*); + static TQCString localFileToUri(const TQString&); + static TQString uriToUnicodeUri(const char*); + static TQCString unicodeUriToUri(const TQString&); + static bool canDecode( const TQMimeSource* e ); + static bool decode( const TQMimeSource* e, TQStrList& i ); + static bool decodeToUnicodeUris( const TQMimeSource* e, TQStringList& i ); + static bool decodeLocalFiles( const TQMimeSource* e, TQStringList& i ); + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQUriDrag( const TQUriDrag & ); + TQUriDrag &operator=( const TQUriDrag & ); +#endif +}; + +class Q_EXPORT TQColorDrag : public TQStoredDrag +{ + Q_OBJECT + TQColor color; + +public: + TQColorDrag( const TQColor &col, TQWidget *dragsource = 0, const char *name = 0 ); + TQColorDrag( TQWidget * dragSource = 0, const char * name = 0 ); + void setColor( const TQColor &col ); + + static bool canDecode( TQMimeSource * ); + static bool decode( TQMimeSource *, TQColor &col ); + +private: +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQColorDrag( const TQColorDrag & ); + TQColorDrag &operator=( const TQColorDrag & ); +#endif +}; + +#ifndef QT_NO_COMPAT +typedef TQUriDrag TQUrlDrag; +#endif + +#ifndef QT_NO_DRAGANDDROP + +// TQDragManager is not part of the public API. It is defined in a +// header file simply so different .cpp files can implement different +// member functions. +// + +class Q_EXPORT TQDragManager: public TQObject { + Q_OBJECT + +private: + TQDragManager(); + ~TQDragManager(); + // only friend classes can use TQDragManager. + friend class TQDragObject; + friend class TQDragMoveEvent; + friend class TQDropEvent; + friend class TQApplication; + + bool eventFilter( TQObject *, TQEvent * ); + void timerEvent( TQTimerEvent* ); + + bool drag( TQDragObject *, TQDragObject::DragMode ); + + void cancel( bool deleteSource = TRUE ); + void move( const TQPoint & ); + void drop(); + void updatePixmap(); + void updatePixmap( const TQPoint& cursorPos ); + +private: + TQDragObject * object; + bool updateMode( ButtonState newstate ); + void updateCursor(); +#if defined(Q_WS_X11) + void createCursors(); +#endif + + TQWidget * dragSource; + TQWidget * dropWidget; + bool beingCancelled; + bool restoreCursor; + bool willDrop; + + TQPixmap *pm_cursor; + int n_cursor; +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQDragManager( const TQDragManager & ); + TQDragManager &operator=( const TQDragManager & ); +#endif +}; + +#endif + +#endif // QT_NO_MIME + +#endif // TQDRAGOBJECT_H diff --git a/src/kernel/qdrawutil.cpp b/src/kernel/qdrawutil.cpp new file mode 100644 index 000000000..03db0d0f1 --- /dev/null +++ b/src/kernel/qdrawutil.cpp @@ -0,0 +1,957 @@ +/**************************************************************************** +** +** Implementation of draw utilities +** +** Created : 950920 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qdrawutil.h" +#ifndef QT_NO_DRAWUTIL +#include "qbitmap.h" +#include "qpixmapcache.h" +#include "qapplication.h" +#include "qpainter.h" + +/*! + \relates TQPainter + + \c{#include } + + Draws a horizontal (\a y1 == \a y2) or vertical (\a x1 == \a x2) + shaded line using the painter \a p. + + Nothing is drawn if \a y1 != \a y2 and \a x1 != \a x2 (i.e. the + line is neither horizontal nor vertical). + + The color group argument \a g specifies the shading colors (\link + TQColorGroup::light() light\endlink, \link TQColorGroup::dark() + dark\endlink and \link TQColorGroup::mid() middle\endlink colors). + + The line appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The \a lineWidth argument specifies the line width for each of the + lines. It is not the total line width. + + The \a midLineWidth argument specifies the width of a middle line + drawn in the TQColorGroup::mid() color. + + If you want to use a TQFrame widget instead, you can make it + display a shaded line, for example \c{TQFrame::setFrameStyle( + TQFrame::HLine | TQFrame::Sunken )}. + + \warning This function does not look at TQWidget::style() or + TQApplication::style(). Use the drawing functions in TQStyle to make + widgets that follow the current GUI style. + + \sa qDrawShadeRect(), qDrawShadePanel(), TQStyle::drawPrimitive() +*/ + +void qDrawShadeLine( TQPainter *p, int x1, int y1, int x2, int y2, + const TQColorGroup &g, bool sunken, + int lineWidth, int midLineWidth ) +{ + if (!( p && lineWidth >= 0 && midLineWidth >= 0 ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawShadeLine invalid parameters." ); +#endif + return; + } + int tlw = lineWidth*2 + midLineWidth; // total line width + TQPen oldPen = p->pen(); // save pen + if ( sunken ) + p->setPen( g.dark() ); + else + p->setPen( g.light() ); + TQPointArray a; + int i; + if ( y1 == y2 ) { // horizontal line + int y = y1 - tlw/2; + if ( x1 > x2 ) { // swap x1 and x2 + int t = x1; + x1 = x2; + x2 = t; + } + x2--; + for ( i=0; idrawPolyline( a ); + } + if ( midLineWidth > 0 ) { + p->setPen( g.mid() ); + for ( i=0; idrawLine( x1+lineWidth, y+lineWidth+i, + x2-lineWidth, y+lineWidth+i ); + } + if ( sunken ) + p->setPen( g.light() ); + else + p->setPen( g.dark() ); + for ( i=0; idrawPolyline( a ); + } + } + else if ( x1 == x2 ) { // vertical line + int x = x1 - tlw/2; + if ( y1 > y2 ) { // swap y1 and y2 + int t = y1; + y1 = y2; + y2 = t; + } + y2--; + for ( i=0; idrawPolyline( a ); + } + if ( midLineWidth > 0 ) { + p->setPen( g.mid() ); + for ( i=0; idrawLine( x+lineWidth+i, y1+lineWidth, x+lineWidth+i, y2 ); + } + if ( sunken ) + p->setPen( g.light() ); + else + p->setPen( g.dark() ); + for ( i=0; idrawPolyline( a ); + } + } + p->setPen( oldPen ); +} + + +/*! + \relates TQPainter + + \c{#include } + + Draws the shaded rectangle specified by (\a x, \a y, \a w, \a h) + using the painter \a p. + + The color group argument \a g specifies the shading colors (\link + TQColorGroup::light() light\endlink, \link TQColorGroup::dark() + dark\endlink and \link TQColorGroup::mid() middle\endlink colors). + + The rectangle appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The \a lineWidth argument specifies the line width for each of the + lines. It is not the total line width. + + The \a midLineWidth argument specifies the width of a middle line + drawn in the TQColorGroup::mid() color. + + The rectangle's interior is filled with the \a fill brush unless + \a fill is 0. + + If you want to use a TQFrame widget instead, you can make it + display a shaded rectangle, for example \c{TQFrame::setFrameStyle( + TQFrame::Box | TQFrame::Raised )}. + + \warning This function does not look at TQWidget::style() or + TQApplication::style(). Use the drawing functions in TQStyle to make + widgets that follow the current GUI style. + + \sa qDrawShadeLine(), qDrawShadePanel(), qDrawPlainRect(), + TQStyle::drawItem(), TQStyle::drawControl() + TQStyle::drawComplexControl() +*/ + +void qDrawShadeRect( TQPainter *p, int x, int y, int w, int h, + const TQColorGroup &g, bool sunken, + int lineWidth, int midLineWidth, + const TQBrush *fill ) +{ + if ( w == 0 || h == 0 ) + return; + if ( ! ( w > 0 && h > 0 && lineWidth >= 0 && midLineWidth >= 0 ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawShadeRect(): Invalid parameters" ); +#endif + return; + } + TQPen oldPen = p->pen(); + if ( sunken ) + p->setPen( g.dark() ); + else + p->setPen( g.light() ); + int x1=x, y1=y, x2=x+w-1, y2=y+h-1; + TQPointArray a; + + if ( lineWidth == 1 && midLineWidth == 0 ) {// standard shade rectangle + p->drawRect( x1, y1, w-1, h-1 ); + if ( sunken ) + p->setPen( g.light() ); + else + p->setPen( g.dark() ); + a.setPoints( 8, x1+1,y1+1, x2-2,y1+1, x1+1,y1+2, x1+1,y2-2, + x1,y2, x2,y2, x2,y1, x2,y2-1 ); + p->drawLineSegments( a ); // draw bottom/right lines + } else { // more complicated + int m = lineWidth+midLineWidth; + int i, j=0, k=m; + for ( i=0; idrawLineSegments( a ); + k++; + } + p->setPen( g.mid() ); + j = lineWidth*2; + for ( i=0; idrawRect( x1+lineWidth+i, y1+lineWidth+i, w-j, h-j ); + j += 2; + } + if ( sunken ) + p->setPen( g.light() ); + else + p->setPen( g.dark() ); + k = m; + for ( i=0; idrawLineSegments( a ); + k++; + } + } + if ( fill ) { + TQBrush oldBrush = p->brush(); + int tlw = lineWidth + midLineWidth; + p->setPen( TQt::NoPen ); + p->setBrush( *fill ); + p->drawRect( x+tlw, y+tlw, w-2*tlw, h-2*tlw ); + p->setBrush( oldBrush ); + } + p->setPen( oldPen ); // restore pen +} + + +/*! + \relates TQPainter + + \c{#include } + + Draws the shaded panel specified by (\a x, \a y, \a w, \a h) using + the painter \a p. + + The color group argument \a g specifies the shading colors (\link + TQColorGroup::light() light\endlink, \link TQColorGroup::dark() + dark\endlink and \link TQColorGroup::mid() middle\endlink colors). + + The panel appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The \a lineWidth argument specifies the line width. + + The panel's interior is filled with the \a fill brush unless \a + fill is 0. + + If you want to use a TQFrame widget instead, you can make it + display a shaded panel, for example \c{TQFrame::setFrameStyle( + TQFrame::Panel | TQFrame::Sunken )}. + + \warning This function does not look at TQWidget::style() or + TQApplication::style(). Use the drawing functions in TQStyle to make + widgets that follow the current GUI style. + + \sa qDrawWinPanel(), qDrawShadeLine(), qDrawShadeRect(), + TQStyle::drawPrimitive() +*/ + +void qDrawShadePanel( TQPainter *p, int x, int y, int w, int h, + const TQColorGroup &g, bool sunken, + int lineWidth, const TQBrush *fill ) +{ + if ( w == 0 || h == 0 ) + return; + if ( !( w > 0 && h > 0 && lineWidth >= 0 ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawShadePanel() Invalid parameters." ); +#endif + } + TQColor shade = g.dark(); + TQColor light = g.light(); + if ( fill ) { + if ( fill->color() == shade ) + shade = g.shadow(); + if ( fill->color() == light ) + light = g.midlight(); + } + TQPen oldPen = p->pen(); // save pen + TQPointArray a( 4*lineWidth ); + if ( sunken ) + p->setPen( shade ); + else + p->setPen( light ); + int x1, y1, x2, y2; + int i; + int n = 0; + x1 = x; + y1 = y2 = y; + x2 = x+w-2; + for ( i=0; idrawLineSegments( a ); + n = 0; + if ( sunken ) + p->setPen( light ); + else + p->setPen( shade ); + x1 = x; + y1 = y2 = y+h-1; + x2 = x+w-1; + for ( i=0; idrawLineSegments( a ); + if ( fill ) { // fill with fill color + TQBrush oldBrush = p->brush(); + p->setPen( TQt::NoPen ); + p->setBrush( *fill ); + p->drawRect( x+lineWidth, y+lineWidth, w-lineWidth*2, h-lineWidth*2 ); + p->setBrush( oldBrush ); + } + p->setPen( oldPen ); // restore pen +} + + +/*! + \internal + This function draws a rectangle with two pixel line width. + It is called from qDrawWinButton() and qDrawWinPanel(). + + c1..c4 and fill are used: + + 1 1 1 1 1 2 + 1 3 3 3 4 2 + 1 3 F F 4 2 + 1 3 F F 4 2 + 1 4 4 4 4 2 + 2 2 2 2 2 2 +*/ + +static void qDrawWinShades( TQPainter *p, + int x, int y, int w, int h, + const TQColor &c1, const TQColor &c2, + const TQColor &c3, const TQColor &c4, + const TQBrush *fill ) +{ + if ( w < 2 || h < 2 ) // can't do anything with that + return; + TQPen oldPen = p->pen(); + TQPointArray a( 3 ); + a.setPoints( 3, x, y+h-2, x, y, x+w-2, y ); + p->setPen( c1 ); + p->drawPolyline( a ); + a.setPoints( 3, x, y+h-1, x+w-1, y+h-1, x+w-1, y ); + p->setPen( c2 ); + p->drawPolyline( a ); + if ( w > 4 && h > 4 ) { + a.setPoints( 3, x+1, y+h-3, x+1, y+1, x+w-3, y+1 ); + p->setPen( c3 ); + p->drawPolyline( a ); + a.setPoints( 3, x+1, y+h-2, x+w-2, y+h-2, x+w-2, y+1 ); + p->setPen( c4 ); + p->drawPolyline( a ); + if ( fill ) { + TQBrush oldBrush = p->brush(); + p->setBrush( *fill ); + p->setPen( TQt::NoPen ); + p->drawRect( x+2, y+2, w-4, h-4 ); + p->setBrush( oldBrush ); + } + } + p->setPen( oldPen ); +} + + +/*! + \relates TQPainter + + \c{#include } + + Draws the Windows-style button specified by (\a x, \a y, \a w, \a + h) using the painter \a p. + + The color group argument \a g specifies the shading colors (\link + TQColorGroup::light() light\endlink, \link TQColorGroup::dark() + dark\endlink and \link TQColorGroup::mid() middle\endlink colors). + + The button appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The line width is 2 pixels. + + The button's interior is filled with the \a *fill brush unless \a + fill is 0. + + \warning This function does not look at TQWidget::style() or + TQApplication::style(). Use the drawing functions in TQStyle to make + widgets that follow the current GUI style. + + \sa qDrawWinPanel(), TQStyle::drawControl() +*/ + +void qDrawWinButton( TQPainter *p, int x, int y, int w, int h, + const TQColorGroup &g, bool sunken, + const TQBrush *fill ) +{ + if ( sunken ) + qDrawWinShades( p, x, y, w, h, + g.shadow(), g.light(), g.dark(), g.button(), fill ); + else + qDrawWinShades( p, x, y, w, h, + g.light(), g.shadow(), g.button(), g.dark(), fill ); +} + +/*! + \relates TQPainter + + \c{#include } + + Draws the Windows-style panel specified by (\a x, \a y, \a w, \a + h) using the painter \a p. + + The color group argument \a g specifies the shading colors. + + The panel appears sunken if \a sunken is TRUE, or raised if \a + sunken is FALSE. + + The line width is 2 pixels. + + The button's interior is filled with the \a fill brush unless \a + fill is 0. + + If you want to use a TQFrame widget instead, you can make it + display a shaded panel, for example \c{TQFrame::setFrameStyle( + TQFrame::WinPanel | TQFrame::Raised )}. + + \warning This function does not look at TQWidget::style() or + TQApplication::style(). Use the drawing functions in TQStyle to make + widgets that follow the current GUI style. + + \sa qDrawShadePanel(), qDrawWinButton(), TQStyle::drawPrimitive() +*/ + +void qDrawWinPanel( TQPainter *p, int x, int y, int w, int h, + const TQColorGroup &g, bool sunken, + const TQBrush *fill ) +{ + if ( sunken ) + qDrawWinShades( p, x, y, w, h, + g.dark(), g.light(), g.shadow(), g.midlight(), fill ); + else + qDrawWinShades( p, x, y, w, h, + g.light(), g.shadow(), g.midlight(), g.dark(), fill ); +} + + +/*! + \relates TQPainter + + \c{#include } + + Draws the plain rectangle specified by (\a x, \a y, \a w, \a h) + using the painter \a p. + + The color argument \a c specifies the line color. + + The \a lineWidth argument specifies the line width. + + The rectangle's interior is filled with the \a fill brush unless + \a fill is 0. + + If you want to use a TQFrame widget instead, you can make it + display a plain rectangle, for example \c{TQFrame::setFrameStyle( + TQFrame::Box | TQFrame::Plain )}. + + \warning This function does not look at TQWidget::style() or + TQApplication::style(). Use the drawing functions in TQStyle to make + widgets that follow the current GUI style. + + \sa qDrawShadeRect(), TQStyle::drawPrimitive() +*/ + +void qDrawPlainRect( TQPainter *p, int x, int y, int w, int h, const TQColor &c, + int lineWidth, const TQBrush *fill ) +{ + if ( w == 0 || h == 0 ) + return; + if ( !( w > 0 && h > 0 && lineWidth >= 0 ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawPlainRect() Invalid parameters." ); +#endif + } + TQPen oldPen = p->pen(); + TQBrush oldBrush = p->brush(); + p->setPen( c ); + p->setBrush( TQt::NoBrush ); + for ( int i=0; idrawRect( x+i, y+i, w-i*2, h-i*2 ); + if ( fill ) { // fill with fill color + p->setPen( TQt::NoPen ); + p->setBrush( *fill ); + p->drawRect( x+lineWidth, y+lineWidth, w-lineWidth*2, h-lineWidth*2 ); + } + p->setPen( oldPen ); + p->setBrush( oldBrush ); +} + + +TQRect qItemRect( TQPainter *p, TQt::GUIStyle gs, + int x, int y, int w, int h, + int flags, + bool enabled, + const TQPixmap *pixmap, + const TQString& text, int len ) +{ + TQRect result; + + if ( pixmap ) { + if ( (flags & TQt::AlignVCenter) == TQt::AlignVCenter ) + y += h/2 - pixmap->height()/2; + else if ( (flags & TQt::AlignBottom) == TQt::AlignBottom) + y += h - pixmap->height(); + if ( (flags & TQt::AlignRight) == TQt::AlignRight ) + x += w - pixmap->width(); + else if ( (flags & TQt::AlignHCenter) == TQt::AlignHCenter ) + x += w/2 - pixmap->width()/2; + else if ( (flags & TQt::AlignLeft) != TQt::AlignLeft && TQApplication::reverseLayout() ) + x += w - pixmap->width(); + result = TQRect(x, y, pixmap->width(), pixmap->height()); + } else if ( !text.isNull() && p ) { + result = p->boundingRect( x, y, w, h, flags, text, len ); + if ( gs == TQt::WindowsStyle && !enabled ) { + result.setWidth(result.width()+1); + result.setHeight(result.height()+1); + } + } else { + result = TQRect(x, y, w, h); + } + + return result; +} + + +void qDrawItem( TQPainter *p, TQt::GUIStyle gs, + int x, int y, int w, int h, + int flags, + const TQColorGroup &g, bool enabled, + const TQPixmap *pixmap, + const TQString& text, int len , const TQColor* penColor ) +{ + p->setPen( penColor?*penColor:g.foreground() ); + if ( pixmap ) { + TQPixmap pm( *pixmap ); + bool clip = (flags & TQt::DontClip) == 0; + if ( clip ) { + if ( pm.width() < w && pm.height() < h ) + clip = FALSE; + else + p->setClipRect( x, y, w, h ); + } + if ( (flags & TQt::AlignVCenter) == TQt::AlignVCenter ) + y += h/2 - pm.height()/2; + else if ( (flags & TQt::AlignBottom) == TQt::AlignBottom) + y += h - pm.height(); + if ( (flags & TQt::AlignRight) == TQt::AlignRight ) + x += w - pm.width(); + else if ( (flags & TQt::AlignHCenter) == TQt::AlignHCenter ) + x += w/2 - pm.width()/2; + else if ( ((flags & TQt::AlignLeft) != TQt::AlignLeft) && TQApplication::reverseLayout() ) // AlignAuto && rightToLeft + x += w - pm.width(); + + if ( !enabled ) { + if ( pm.mask() ) { // pixmap with a mask + if ( !pm.selfMask() ) { // mask is not pixmap itself + TQPixmap pmm( *pm.mask() ); + pmm.setMask( *((TQBitmap *)&pmm) ); + pm = pmm; + } + } else if ( pm.depth() == 1 ) { // monochrome pixmap, no mask + pm.setMask( *((TQBitmap *)&pm) ); +#ifndef QT_NO_IMAGE_HEURISTIC_MASK + } else { // color pixmap, no mask + TQString k; + k.sprintf( "$qt-drawitem-%x", pm.serialNumber() ); + TQPixmap *mask = TQPixmapCache::find(k); + bool del=FALSE; + if ( !mask ) { + mask = new TQPixmap( pm.createHeuristicMask() ); + mask->setMask( *((TQBitmap*)mask) ); + del = !TQPixmapCache::insert( k, mask ); + } + pm = *mask; + if (del) delete mask; +#endif + } + if ( gs == TQt::WindowsStyle ) { + p->setPen( g.light() ); + p->drawPixmap( x+1, y+1, pm ); + p->setPen( g.text() ); + } + } + p->drawPixmap( x, y, pm ); + if ( clip ) + p->setClipping( FALSE ); + } else if ( !text.isNull() ) { + if ( gs == TQt::WindowsStyle && !enabled ) { + p->setPen( g.light() ); + p->drawText( x+1, y+1, w, h, flags, text, len ); + p->setPen( g.text() ); + } + p->drawText( x, y, w, h, flags, text, len ); + } +} + + +/***************************************************************************** + Overloaded functions. + *****************************************************************************/ + +/*! + \overload void qDrawShadeLine( TQPainter *p, const TQPoint &p1, const TQPoint &p2, const TQColorGroup &g, bool sunken, int lineWidth, int midLineWidth ) +*/ + +void qDrawShadeLine( TQPainter *p, const TQPoint &p1, const TQPoint &p2, + const TQColorGroup &g, bool sunken, + int lineWidth, int midLineWidth ) +{ + qDrawShadeLine( p, p1.x(), p1.y(), p2.x(), p2.y(), g, sunken, + lineWidth, midLineWidth ); +} + +/*! + \overload void qDrawShadeRect( TQPainter *p, const TQRect &r, const TQColorGroup &g, bool sunken, int lineWidth, int midLineWidth, const TQBrush *fill ) +*/ + +void qDrawShadeRect( TQPainter *p, const TQRect &r, + const TQColorGroup &g, bool sunken, + int lineWidth, int midLineWidth, + const TQBrush *fill ) +{ + qDrawShadeRect( p, r.x(), r.y(), r.width(), r.height(), g, sunken, + lineWidth, midLineWidth, fill ); +} + +/*! + \overload void qDrawShadePanel( TQPainter *p, const TQRect &r, const TQColorGroup &g, bool sunken, int lineWidth, const TQBrush *fill ) +*/ + +void qDrawShadePanel( TQPainter *p, const TQRect &r, + const TQColorGroup &g, bool sunken, + int lineWidth, const TQBrush *fill ) +{ + qDrawShadePanel( p, r.x(), r.y(), r.width(), r.height(), g, sunken, + lineWidth, fill ); +} + +/*! + \overload void qDrawWinButton( TQPainter *p, const TQRect &r, const TQColorGroup &g, bool sunken, const TQBrush *fill ) +*/ + +void qDrawWinButton( TQPainter *p, const TQRect &r, + const TQColorGroup &g, bool sunken, + const TQBrush *fill ) +{ + qDrawWinButton( p, r.x(), r.y(), r.width(), r.height(), g, sunken, fill ); +} + +/*! + \overload void qDrawWinPanel( TQPainter *p, const TQRect &r, const TQColorGroup &g, bool sunken, const TQBrush *fill ) +*/ + +void qDrawWinPanel( TQPainter *p, const TQRect &r, + const TQColorGroup &g, bool sunken, + const TQBrush *fill ) +{ + qDrawWinPanel( p, r.x(), r.y(), r.width(), r.height(), g, sunken, fill ); +} + +/*! + \overload void qDrawPlainRect( TQPainter *p, const TQRect &r, const TQColor &c, int lineWidth, const TQBrush *fill ) +*/ + +void qDrawPlainRect( TQPainter *p, const TQRect &r, const TQColor &c, + int lineWidth, const TQBrush *fill ) +{ + qDrawPlainRect( p, r.x(), r.y(), r.width(), r.height(), c, + lineWidth, fill ); +} + + +static void qDrawWinArrow( TQPainter *p, TQt::ArrowType type, bool down, + int x, int y, int w, int h, + const TQColorGroup &g, bool enabled ) +{ + TQPointArray a; // arrow polygon + switch ( type ) { + case TQt::UpArrow: + a.setPoints( 7, -3,1, 3,1, -2,0, 2,0, -1,-1, 1,-1, 0,-2 ); + break; + case TQt::DownArrow: + a.setPoints( 7, -3,-1, 3,-1, -2,0, 2,0, -1,1, 1,1, 0,2 ); + break; + case TQt::LeftArrow: + a.setPoints( 7, 1,-3, 1,3, 0,-2, 0,2, -1,-1, -1,1, -2,0 ); + break; + case TQt::RightArrow: + a.setPoints( 7, -1,-3, -1,3, 0,-2, 0,2, 1,-1, 1,1, 2,0 ); + break; + } + if ( a.isNull() ) + return; + + if ( down ) { + x++; + y++; + } + + TQPen savePen = p->pen(); // save current pen + if (down) + p->setBrushOrigin(p->brushOrigin() + TQPoint(1,1)); + p->fillRect( x, y, w, h, g.brush( TQColorGroup::Button ) ); + if (down) + p->setBrushOrigin(p->brushOrigin() - TQPoint(1,1)); + if ( enabled ) { + a.translate( x+w/2, y+h/2 ); + p->setPen( g.foreground() ); + p->drawLineSegments( a, 0, 3 ); // draw arrow + p->drawPoint( a[6] ); + } else { + a.translate( x+w/2+1, y+h/2+1 ); + p->setPen( g.light() ); + p->drawLineSegments( a, 0, 3 ); // draw arrow + p->drawPoint( a[6] ); + a.translate( -1, -1 ); + p->setPen( g.mid() ); + p->drawLineSegments( a, 0, 3 ); // draw arrow + p->drawPoint( a[6] ); + } + p->setPen( savePen ); // restore pen +} + + +#if defined(Q_CC_MSVC) +#pragma warning(disable: 4244) +#endif + +#ifndef QT_NO_STYLE_MOTIF +// motif arrows look the same whether they are used or not +// is this correct? +static void qDrawMotifArrow( TQPainter *p, TQt::ArrowType type, bool down, + int x, int y, int w, int h, + const TQColorGroup &g, bool ) +{ + TQPointArray bFill; // fill polygon + TQPointArray bTop; // top shadow. + TQPointArray bBot; // bottom shadow. + TQPointArray bLeft; // left shadow. +#ifndef QT_NO_TRANSFORMATIONS + TQWMatrix matrix; // xform matrix +#endif + bool vertical = type == TQt::UpArrow || type == TQt::DownArrow; + bool horizontal = !vertical; + int dim = w < h ? w : h; + int colspec = 0x0000; // color specification array + + if ( dim < 2 ) // too small arrow + return; + + if ( dim > 3 ) { + if ( dim > 6 ) + bFill.resize( dim & 1 ? 3 : 4 ); + bTop.resize( (dim/2)*2 ); + bBot.resize( dim & 1 ? dim + 1 : dim ); + bLeft.resize( dim > 4 ? 4 : 2 ); + bLeft.putPoints( 0, 2, 0,0, 0,dim-1 ); + if ( dim > 4 ) + bLeft.putPoints( 2, 2, 1,2, 1,dim-3 ); + bTop.putPoints( 0, 4, 1,0, 1,1, 2,1, 3,1 ); + bBot.putPoints( 0, 4, 1,dim-1, 1,dim-2, 2,dim-2, 3,dim-2 ); + + for( int i=0; i 6 ) { // dim>6: must fill interior + bFill.putPoints( 0, 2, 1,dim-3, 1,2 ); + if ( dim & 1 ) // if size is an odd number + bFill.setPoint( 2, dim - 3, dim / 2 ); + else + bFill.putPoints( 2, 2, dim-4,dim/2-1, dim-4,dim/2 ); + } + } + else { + if ( dim == 3 ) { // 3x3 arrow pattern + bLeft.setPoints( 4, 0,0, 0,2, 1,1, 1,1 ); + bTop .setPoints( 2, 1,0, 1,0 ); + bBot .setPoints( 2, 1,2, 2,1 ); + } + else { // 2x2 arrow pattern + bLeft.setPoints( 2, 0,0, 0,1 ); + bTop .setPoints( 2, 1,0, 1,0 ); + bBot .setPoints( 2, 1,1, 1,1 ); + } + } + + if ( type == TQt::UpArrow || type == TQt::LeftArrow ) { +#ifndef QT_NO_TRANSFORMATIONS // #### fix me! + matrix.translate( x, y ); + if ( vertical ) { + matrix.translate( 0, h - 1 ); + matrix.rotate( -90 ); + } else { + matrix.translate( w - 1, h - 1 ); + matrix.rotate( 180 ); + } +#endif + if ( down ) + colspec = horizontal ? 0x2334 : 0x2343; + else + colspec = horizontal ? 0x1443 : 0x1434; + } + else if ( type == TQt::DownArrow || type == TQt::RightArrow ) { +#ifndef QT_NO_TRANSFORMATIONS // #### fix me! + matrix.translate( x, y ); + if ( vertical ) { + matrix.translate( w-1, 0 ); + matrix.rotate( 90 ); + } +#endif + if ( down ) + colspec = horizontal ? 0x2443 : 0x2434; + else + colspec = horizontal ? 0x1334 : 0x1343; + } + + TQColor *cols[5]; + cols[0] = 0; + cols[1] = (TQColor *)&g.button(); + cols[2] = (TQColor *)&g.mid(); + cols[3] = (TQColor *)&g.light(); + cols[4] = (TQColor *)&g.dark(); +#define CMID *cols[ (colspec>>12) & 0xf ] +#define CLEFT *cols[ (colspec>>8) & 0xf ] +#define CTOP *cols[ (colspec>>4) & 0xf ] +#define CBOT *cols[ colspec & 0xf ] + + TQPen savePen = p->pen(); // save current pen + TQBrush saveBrush = p->brush(); // save current brush +#ifndef QT_NO_TRANSFORMATIONS + TQWMatrix wxm = p->worldMatrix(); +#endif + TQPen pen( TQt::NoPen ); + const TQBrush &brush = g.brush( TQColorGroup::Button ); + + p->setPen( pen ); + p->setBrush( brush ); +#ifndef QT_NO_TRANSFORMATIONS + p->setWorldMatrix( matrix, TRUE ); // set transformation matrix +#endif + p->drawPolygon( bFill ); // fill arrow + p->setBrush( TQt::NoBrush ); // don't fill + + p->setPen( CLEFT ); + p->drawLineSegments( bLeft ); + p->setPen( CTOP ); + p->drawLineSegments( bTop ); + p->setPen( CBOT ); + p->drawLineSegments( bBot ); + +#ifndef QT_NO_TRANSFORMATIONS + p->setWorldMatrix( wxm ); +#endif + p->setBrush( saveBrush ); // restore brush + p->setPen( savePen ); // restore pen + +#undef CMID +#undef CLEFT +#undef CTOP +#undef CBOT +} +#endif + +void qDrawArrow( TQPainter *p, TQt::ArrowType type, TQt::GUIStyle style, bool down, + int x, int y, int w, int h, + const TQColorGroup &g, bool enabled ) +{ + switch ( style ) { + case TQt::WindowsStyle: + qDrawWinArrow( p, type, down, x, y, w, h, g, enabled ); + break; +#ifndef QT_NO_STYLE_MOTIF + case TQt::MotifStyle: + qDrawMotifArrow( p, type, down, x, y, w, h, g, enabled ); + break; +#endif + default: +#if defined(QT_CHECK_RANGE) + qWarning( "qDrawArrow: Requested GUI style not supported" ); +#else + ; +#endif + } +} +#endif //QT_NO_DRAWUTIL diff --git a/src/kernel/qdrawutil.h b/src/kernel/qdrawutil.h new file mode 100644 index 000000000..567515339 --- /dev/null +++ b/src/kernel/qdrawutil.h @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Definition of draw utilities +** +** Created : 950920 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQDRAWUTIL_H +#define TQDRAWUTIL_H + +#ifndef QT_H +#include "qnamespace.h" +#include "qstring.h" // char*->TQString conversion +#endif // QT_H + +class TQPainter; +class TQColorGroup; +class TQPoint; +class TQBrush; +class TQRect; +class TQPixmap; + +#ifndef QT_NO_DRAWUTIL +// +// Standard shade drawing +// + +Q_EXPORT void qDrawShadeLine( TQPainter *p, int x1, int y1, int x2, int y2, + const TQColorGroup &g, bool sunken = TRUE, + int lineWidth = 1, int midLineWidth = 0 ); + +Q_EXPORT void qDrawShadeLine( TQPainter *p, const TQPoint &p1, const TQPoint &p2, + const TQColorGroup &g, bool sunken = TRUE, + int lineWidth = 1, int midLineWidth = 0 ); + +Q_EXPORT void qDrawShadeRect( TQPainter *p, int x, int y, int w, int h, + const TQColorGroup &, bool sunken=FALSE, + int lineWidth = 1, int midLineWidth = 0, + const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawShadeRect( TQPainter *p, const TQRect &r, + const TQColorGroup &, bool sunken=FALSE, + int lineWidth = 1, int midLineWidth = 0, + const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawShadePanel( TQPainter *p, int x, int y, int w, int h, + const TQColorGroup &, bool sunken=FALSE, + int lineWidth = 1, const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawShadePanel( TQPainter *p, const TQRect &r, + const TQColorGroup &, bool sunken=FALSE, + int lineWidth = 1, const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawWinButton( TQPainter *p, int x, int y, int w, int h, + const TQColorGroup &g, bool sunken = FALSE, + const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawWinButton( TQPainter *p, const TQRect &r, + const TQColorGroup &g, bool sunken = FALSE, + const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawWinPanel( TQPainter *p, int x, int y, int w, int h, + const TQColorGroup &, bool sunken=FALSE, + const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawWinPanel( TQPainter *p, const TQRect &r, + const TQColorGroup &, bool sunken=FALSE, + const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawPlainRect( TQPainter *p, int x, int y, int w, int h, const TQColor &, + int lineWidth = 1, const TQBrush *fill = 0 ); + +Q_EXPORT void qDrawPlainRect( TQPainter *p, const TQRect &r, const TQColor &, + int lineWidth = 1, const TQBrush *fill = 0 ); + + +// +// Other obsolete drawing functions. +// Use TQStyle::itemRect(), TQStyle::drawItem() and TQStyle::drawArrow() instead. +// +Q_EXPORT TQRect qItemRect( TQPainter *p, TQt::GUIStyle gs, int x, int y, int w, int h, + int flags, bool enabled, + const TQPixmap *pixmap, const TQString& text, int len=-1 ); + +Q_EXPORT void qDrawItem( TQPainter *p, TQt::GUIStyle gs, int x, int y, int w, int h, + int flags, const TQColorGroup &g, bool enabled, + const TQPixmap *pixmap, const TQString& text, + int len=-1, const TQColor* penColor = 0 ); + +Q_EXPORT void qDrawArrow( TQPainter *p, TQt::ArrowType type, TQt::GUIStyle style, bool down, + int x, int y, int w, int h, + const TQColorGroup &g, bool enabled ); + +#endif // QT_NO_DRAWUTIL +#endif // TQDRAWUTIL_H diff --git a/src/kernel/qdropsite.cpp b/src/kernel/qdropsite.cpp new file mode 100644 index 000000000..ce4c26f4f --- /dev/null +++ b/src/kernel/qdropsite.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Implementation of Drag and Drop support +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qdropsite.h" + +#ifndef QT_NO_DRAGANDDROP + +#include "qwidget.h" + + +// NOT REVISED +/*! + \class TQDropSite qdropsite.h + \brief The TQDropSite class provides nothing and does nothing. + + \obsolete + + If your code uses it, you can safely delete it. + + It was used in TQt 1.x to do some drag and drop; that has since been + folded into TQWidget. + + For detailed information about drag-and-drop, see the TQDragObject class. + + \sa TQDragObject, TQTextDrag, TQImageDrag +*/ + +/*! + Constructs a TQDropSite to handle events for the widget \a self. + + Pass \c this as the \a self parameter. + This enables dropping by calling TQWidget::setAcceptDrops(TRUE). +*/ +TQDropSite::TQDropSite( TQWidget* self ) +{ + self->setAcceptDrops( TRUE ); +} + +/*! + Destroys the drop site. +*/ +TQDropSite::~TQDropSite() +{ +} + +#endif // QT_NO_DRAGANDDROP diff --git a/src/kernel/qdropsite.h b/src/kernel/qdropsite.h new file mode 100644 index 000000000..2adfb26b8 --- /dev/null +++ b/src/kernel/qdropsite.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Definitation of Drag and Drop support +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQDROPSITE_H +#define TQDROPSITE_H + +#ifndef QT_H +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H +#endif + + +class TQWidget; + + +class Q_EXPORT TQDropSite { +public: + TQDropSite( TQWidget* parent ); + virtual ~TQDropSite(); +}; + + +#endif // TQDROPSITE_H diff --git a/src/kernel/qevent.cpp b/src/kernel/qevent.cpp new file mode 100644 index 000000000..6881c806a --- /dev/null +++ b/src/kernel/qevent.cpp @@ -0,0 +1,2571 @@ +/**************************************************************************** +** +** Implementation of event classes +** +** Created : 931029 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qevent.h" +#include "qcursor.h" +#include "qapplication.h" + + +/*! + \class TQEvent qevent.h + \brief The TQEvent class is the base class of all + event classes. Event objects contain event parameters. + + \ingroup events + \ingroup environment + + TQt's main event loop (TQApplication::exec()) fetches native window + system events from the event queue, translates them into TQEvents + and sends the translated events to TQObjects. + + In general, events come from the underlying window system + (spontaneous() returns TRUE) but it is also possible to manually + send events using TQApplication::sendEvent() and + TQApplication::postEvent() (spontaneous() returns FALSE). + + TQObjects receive events by having their TQObject::event() function + called. The function can be reimplemented in subclasses to + customize event handling and add additional event types; + TQWidget::event() is a notable example. By default, events are + dispatched to event handlers like TQObject::timerEvent() and + TQWidget::mouseMoveEvent(). TQObject::installEventFilter() allows an + object to intercept events destined for another object. + + The basic TQEvent contains only an event type parameter. + Subclasses of TQEvent contain additional parameters that describe + the particular event. + + \sa TQObject::event() TQObject::installEventFilter() + TQWidget::event() TQApplication::sendEvent() + TQApplication::postEvent() TQApplication::processEvents() +*/ + + +/*! + \enum TQt::ButtonState + + This enum type describes the state of the mouse and the modifier + buttons. + + \value NoButton used when the button state does not refer to any + button (see TQMouseEvent::button()). + \value LeftButton set if the left button is pressed, or if this + event refers to the left button. (The left button may be + the right button on left-handed mice.) + \value RightButton the right button. + \value MidButton the middle button. + \value ShiftButton a Shift key on the keyboard is also pressed. + \value ControlButton a Ctrl key on the keyboard is also pressed. + \value AltButton an Alt key on the keyboard is also pressed. + \value MetaButton a Meta key on the keyboard is also pressed. + \value Keypad a keypad button is pressed. + \value KeyButtonMask a mask for ShiftButton, ControlButton, + AltButton and MetaButton. + \value MouseButtonMask a mask for LeftButton, RightButton and MidButton. +*/ + +/*! + \enum TQEvent::Type + + This enum type defines the valid event types in TQt. The event + types and the specialized classes for each type are these: + + \value None Not an event. + \value Accessibility Accessibility information is requested + \value Timer Regular timer events, \l{TQTimerEvent}. + \value MouseButtonPress Mouse press, \l{TQMouseEvent}. + \value MouseButtonRelease Mouse release, \l{TQMouseEvent}. + \value MouseButtonDblClick Mouse press again, \l{TQMouseEvent}. + \value MouseMove Mouse move, \l{TQMouseEvent}. + \value KeyPress Key press (including Shift, for example), \l{TQKeyEvent}. + \value KeyRelease Key release, \l{TQKeyEvent}. + \value IMStart The start of input method composition, \l{TQIMEvent}. + \value IMCompose Input method composition is taking place, \l{TQIMEvent}. + \value IMEnd The end of input method composition, \l{TQIMEvent}. + \value FocusIn Widget gains keyboard focus, \l{TQFocusEvent}. + \value FocusOut Widget loses keyboard focus, \l{TQFocusEvent}. + \value Enter Mouse enters widget's boundaries. + \value Leave Mouse leaves widget's boundaries. + \value Paint Screen update necessary, \l{TQPaintEvent}. + \value Move Widget's position changed, \l{TQMoveEvent}. + \value Resize Widget's size changed, \l{TQResizeEvent}. + \value Show Widget was shown on screen, \l{TQShowEvent}. + \value Hide Widget was hidden, \l{TQHideEvent}. + \value ShowToParent A child widget has been shown. + \value HideToParent A child widget has been hidden. + \value Close Widget was closed (permanently), \l{TQCloseEvent}. + \value ShowNormal Widget should be shown normally (obsolete). + \value ShowMaximized Widget should be shown maximized (obsolete). + \value ShowMinimized Widget should be shown minimized (obsolete). + \value ShowFullScreen Widget should be shown full-screen (obsolete). + \value ShowWindowRequest Widget's window should be shown (obsolete). + \value DeferredDelete The object will be deleted after it has + cleaned up. + \value Accel Key press in child for shortcut key handling, \l{TQKeyEvent}. + \value Wheel Mouse wheel rolled, \l{TQWheelEvent}. + \value ContextMenu Context popup menu, \l{TQContextMenuEvent} + \value AccelOverride Key press in child, for overriding shortcut key handling, \l{TQKeyEvent}. + \value AccelAvailable internal. + \value WindowActivate Window was activated. + \value WindowDeactivate Window was deactivated. + \value CaptionChange Widget's caption changed. + \value IconChange Widget's icon changed. + \value ParentFontChange Font of the parent widget changed. + \value ApplicationFontChange Default application font changed. + \value PaletteChange Palette of the widget changed. + \value ParentPaletteChange Palette of the parent widget changed. + \value ApplicationPaletteChange Default application palette changed. + \value Clipboard Clipboard contents have changed. + \value SockAct Socket activated, used to implement \l{TQSocketNotifier}. + \value DragEnter A drag-and-drop enters widget, \l{TQDragEnterEvent}. + \value DragMove A drag-and-drop is in progress, \l{TQDragMoveEvent}. + \value DragLeave A drag-and-drop leaves widget, \l{TQDragLeaveEvent}. + \value Drop A drag-and-drop is completed, \l{TQDropEvent}. + \value DragResponse Internal event used by TQt on some platforms. + \value ChildInserted Object gets a child, \l{TQChildEvent}. + \value ChildRemoved Object loses a child, \l{TQChildEvent}. + \value LayoutHint Widget child has changed layout properties. + \value ActivateControl Internal event used by TQt on some platforms. + \value DeactivateControl Internal event used by TQt on some platforms. + \value LanguageChange The application translation changed, \l{TQTranslator} + \value LayoutDirectionChange The direction of layouts changed + \value LocaleChange The system locale changed + \value Quit Reserved. + \value Create Reserved. + \value Destroy Reserved. + \value Reparent Reserved. + \value Speech Reserved for speech input. + \value TabletMove A Wacom Tablet Move Event. + \value Style Internal use only + \value TabletPress A Wacom Tablet Press Event + \value TabletRelease A Wacom Tablet Release Event + \value OkRequest Internal event used by TQt on some platforms. + \value HelpRequest Internal event used by TQt on some platforms. + \value IconDrag Internal event used by TQt on some platforms when proxy icon is dragged. + \value WindowStateChange The window's state, i.e. minimized, + maximized or full-screen, has changed. See \l{TQWidget::windowState()}. + \value WindowBlocked The window is modally blocked + \value WindowUnblocked The window leaves modal blocking + + \value User User defined event. + \value MaxUser Last user event id. + + User events should have values between User and MaxUser inclusive. +*/ +/*! + \fn TQEvent::TQEvent( Type type ) + + Contructs an event object of type \a type. +*/ + +/*! + \fn TQEvent::Type TQEvent::type() const + + Returns the event type. +*/ + +/*! + \fn bool TQEvent::spontaneous() const + + Returns TRUE if the event originated outside the application, i.e. + it is a system event; otherwise returns FALSE. +*/ + + +/*! + \class TQTimerEvent qevent.h + \brief The TQTimerEvent class contains parameters that describe a + timer event. + + \ingroup events + + Timer events are sent at regular intervals to objects that have + started one or more timers. Each timer has a unique identifier. A + timer is started with TQObject::startTimer(). + + The TQTimer class provides a high-level programming interface that + uses signals instead of events. It also provides one-shot timers. + + The event handler TQObject::timerEvent() receives timer events. + + \sa TQTimer, TQObject::timerEvent(), TQObject::startTimer(), + TQObject::killTimer(), TQObject::killTimers() +*/ + +/*! + \fn TQTimerEvent::TQTimerEvent( int timerId ) + + Constructs a timer event object with the timer identifier set to + \a timerId. +*/ + +/*! + \fn int TQTimerEvent::timerId() const + + Returns the unique timer identifier, which is the same identifier + as returned from TQObject::startTimer(). +*/ + + +/*! + \class TQMouseEvent qevent.h + \ingroup events + + \brief The TQMouseEvent class contains parameters that describe a mouse event. + + Mouse events occur when a mouse button is pressed or released + inside a widget or when the mouse cursor is moved. + + Mouse move events will occur only when a mouse button is pressed + down, unless mouse tracking has been enabled with + TQWidget::setMouseTracking(). + + TQt automatically grabs the mouse when a mouse button is pressed + inside a widget; the widget will continue to receive mouse events + until the last mouse button is released. + + A mouse event contains a special accept flag that indicates + whether the receiver wants the event. You should call + TQMouseEvent::ignore() if the mouse event is not handled by your + widget. A mouse event is propagated up the parent widget chain + until a widget accepts it with TQMouseEvent::accept() or an event + filter consumes it. + + The functions pos(), x() and y() give the cursor position relative + to the widget that receives the mouse event. If you move the + widget as a result of the mouse event, use the global position + returned by globalPos() to avoid a shaking motion. + + The TQWidget::setEnabled() function can be used to enable or + disable mouse and keyboard events for a widget. + + The event handlers TQWidget::mousePressEvent(), + TQWidget::mouseReleaseEvent(), TQWidget::mouseDoubleClickEvent() and + TQWidget::mouseMoveEvent() receive mouse events. + + \sa TQWidget::setMouseTracking(), TQWidget::grabMouse(), + TQCursor::pos() +*/ + +/*! + \fn TQMouseEvent::TQMouseEvent( Type type, const TQPoint &pos, int button, int state ) + + Constructs a mouse event object. + + The \a type parameter must be one of \c TQEvent::MouseButtonPress, + \c TQEvent::MouseButtonRelease, \c TQEvent::MouseButtonDblClick or + \c TQEvent::MouseMove. + + The \a pos parameter specifies the position relative to the + receiving widget. \a button specifies the \link TQt::ButtonState + button\endlink that caused the event, which should be \c + TQt::NoButton (0), if \a type is \c MouseMove. \a state is the + \link TQt::ButtonState ButtonState\endlink at the time of the + event. + + The globalPos() is initialized to TQCursor::pos(), which may not be + appropriate. Use the other constructor to specify the global + position explicitly. +*/ + +TQMouseEvent::TQMouseEvent( Type type, const TQPoint &pos, int button, int state ) + : TQEvent(type), p(pos), b(button),s((ushort)state), accpt(TRUE){ + g = TQCursor::pos(); +} + + +/*! + \fn TQMouseEvent::TQMouseEvent( Type type, const TQPoint &pos, const TQPoint &globalPos, int button, int state ) + + Constructs a mouse event object. + + The \a type parameter must be \c TQEvent::MouseButtonPress, \c + TQEvent::MouseButtonRelease, \c TQEvent::MouseButtonDblClick or \c + TQEvent::MouseMove. + + The \a pos parameter specifies the position relative to the + receiving widget. \a globalPos is the position in absolute + coordinates. \a button specifies the \link TQt::ButtonState + button\endlink that caused the event, which should be \c + TQt::NoButton (0), if \a type is \c MouseMove. \a state is the + \link TQt::ButtonState ButtonState\endlink at the time of the + event. + +*/ + +/*! + \fn const TQPoint &TQMouseEvent::pos() const + + Returns the position of the mouse pointer relative to the widget + that received the event. + + If you move the widget as a result of the mouse event, use the + global position returned by globalPos() to avoid a shaking motion. + + \sa x(), y(), globalPos() +*/ + +/*! + \fn const TQPoint &TQMouseEvent::globalPos() const + + Returns the global position of the mouse pointer \e{at the time + of the event}. This is important on asynchronous window systems + like X11. Whenever you move your widgets around in response to + mouse events, globalPos() may differ a lot from the current + pointer position TQCursor::pos(), and from TQWidget::mapToGlobal( + pos() ). + + \sa globalX(), globalY() +*/ + +/*! + \fn int TQMouseEvent::x() const + + Returns the x-position of the mouse pointer, relative to the + widget that received the event. + + \sa y(), pos() +*/ + +/*! + \fn int TQMouseEvent::y() const + + Returns the y-position of the mouse pointer, relative to the + widget that received the event. + + \sa x(), pos() +*/ + +/*! + \fn int TQMouseEvent::globalX() const + + Returns the global x-position of the mouse pointer at the time of + the event. + + \sa globalY(), globalPos() +*/ + +/*! + \fn int TQMouseEvent::globalY() const + + Returns the global y-position of the mouse pointer at the time of + the event. + + \sa globalX(), globalPos() +*/ + +/*! + \fn ButtonState TQMouseEvent::button() const + + Returns the button that caused the event. + + Possible return values are \c LeftButton, \c RightButton, \c + MidButton and \c NoButton. + + Note that the returned value is always \c NoButton for mouse move + events. + + \sa state() TQt::ButtonState +*/ + + +/*! + \fn ButtonState TQMouseEvent::state() const + + Returns the button state (a combination of mouse buttons and + keyboard modifiers), i.e. what buttons and keys were being pressed + immediately before the event was generated. + + This means that if you have a \c TQEvent::MouseButtonPress or a \c + TQEvent::MouseButtonDblClick state() will \e not include the mouse + button that's pressed. But once the mouse button has been + released, the \c TQEvent::MouseButtonRelease event will have the + button() that was pressed. + + This value is mainly interesting for \c TQEvent::MouseMove; for the + other cases, button() is more useful. + + The returned value is \c LeftButton, \c RightButton, \c MidButton, + \c ShiftButton, \c ControlButton and \c AltButton OR'ed together. + + \sa button() stateAfter() TQt::ButtonState +*/ + +/*! + \fn ButtonState TQMouseEvent::stateAfter() const + + Returns the state of buttons after the event. + + \sa state() TQt::ButtonState +*/ +TQt::ButtonState TQMouseEvent::stateAfter() const +{ + return TQt::ButtonState(state()^button()); +} + + + +/*! + \fn bool TQMouseEvent::isAccepted() const + + Returns TRUE if the receiver of the event wants to keep the key; + otherwise returns FALSE. +*/ + +/*! + \fn void TQMouseEvent::accept() + + Sets the accept flag of the mouse event object. + + Setting the accept parameter indicates that the receiver of the + event wants the mouse event. Unwanted mouse events are sent to the + parent widget. + + The accept flag is set by default. + + \sa ignore() +*/ + + +/*! + \fn void TQMouseEvent::ignore() + + Clears the accept flag parameter of the mouse event object. + + Clearing the accept parameter indicates that the event receiver + does not want the mouse event. Unwanted mouse events are sent to + the parent widget. + + The accept flag is set by default. + + \sa accept() +*/ + + +/*! + \class TQWheelEvent qevent.h + \brief The TQWheelEvent class contains parameters that describe a wheel event. + + \ingroup events + + Wheel events are sent to the widget under the mouse, and if that widget + does not handle the event they are sent to the focus widget. The rotation + distance is provided by delta(). The functions pos() and globalPos() return + the mouse pointer location at the time of the event. + + A wheel event contains a special accept flag that indicates + whether the receiver wants the event. You should call + TQWheelEvent::accept() if you handle the wheel event; otherwise it + will be sent to the parent widget. + + The TQWidget::setEnable() function can be used to enable or disable + mouse and keyboard events for a widget. + + The event handler TQWidget::wheelEvent() receives wheel events. + + \sa TQMouseEvent, TQWidget::grabMouse() +*/ + +/*! + \fn Orientation TQWheelEvent::orientation() const + + Returns the wheel's orientation. +*/ + +/*! + \fn TQWheelEvent::TQWheelEvent( const TQPoint &pos, int delta, int state, Orientation orient = Vertical ); + + Constructs a wheel event object. + + The globalPos() is initialized to TQCursor::pos(), i.e. \a pos, + which is usually (but not always) right. Use the other constructor + if you need to specify the global position explicitly. \a delta + contains the rotation distance, \a state holds the keyboard + modifier flags at the time of the event and \a orient holds the + wheel's orientation. + + \sa pos(), delta(), state() +*/ +#ifndef QT_NO_WHEELEVENT +TQWheelEvent::TQWheelEvent( const TQPoint &pos, int delta, int state, Orientation orient ) + : TQEvent(Wheel), p(pos), d(delta), s((ushort)state), + accpt(TRUE), o(orient) +{ + g = TQCursor::pos(); +} +#endif +/*! + \fn TQWheelEvent::TQWheelEvent( const TQPoint &pos, const TQPoint& globalPos, int delta, int state, Orientation orient = Vertical ) + + Constructs a wheel event object. The position when the event + occurred is given in \a pos and \a globalPos. \a delta contains + the rotation distance, \a state holds the keyboard modifier flags + at the time of the event and \a orient holds the wheel's + orientation. + + \sa pos(), globalPos(), delta(), state() +*/ + +/*! + \fn int TQWheelEvent::delta() const + + Returns the distance that the wheel is rotated expressed in + multiples or divisions of the \e{wheel delta}, which is currently + defined to be 120. A positive value indicates that the wheel was + rotated forwards away from the user; a negative value indicates + that the wheel was rotated backwards toward the user. + + The \e{wheel delta} constant was defined to be 120 by wheel mouse + vendors to allow building finer-resolution wheels in the future, + including perhaps a freely rotating wheel with no notches. The + expectation is that such a device would send more messages per + rotation but with a smaller value in each message. +*/ + +/*! + \fn const TQPoint &TQWheelEvent::pos() const + + Returns the position of the mouse pointer, relative to the widget + that received the event. + + If you move your widgets around in response to mouse + events, use globalPos() instead of this function. + + \sa x(), y(), globalPos() +*/ + +/*! + \fn int TQWheelEvent::x() const + + Returns the x-position of the mouse pointer, relative to the + widget that received the event. + + \sa y(), pos() +*/ + +/*! + \fn int TQWheelEvent::y() const + + Returns the y-position of the mouse pointer, relative to the + widget that received the event. + + \sa x(), pos() +*/ + + +/*! + \fn const TQPoint &TQWheelEvent::globalPos() const + + Returns the global position of the mouse pointer \e{at the time + of the event}. This is important on asynchronous window systems + such as X11; whenever you move your widgets around in response to + mouse events, globalPos() can differ a lot from the current + pointer position TQCursor::pos(). + + \sa globalX(), globalY() +*/ + +/*! + \fn int TQWheelEvent::globalX() const + + Returns the global x-position of the mouse pointer at the time of + the event. + + \sa globalY(), globalPos() +*/ + +/*! + \fn int TQWheelEvent::globalY() const + + Returns the global y-position of the mouse pointer at the time of + the event. + + \sa globalX(), globalPos() +*/ + + +/*! + \fn ButtonState TQWheelEvent::state() const + + Returns the keyboard modifier flags of the event. + + The returned value is \c ShiftButton, \c ControlButton, and \c + AltButton OR'ed together. +*/ + +/*! + \fn bool TQWheelEvent::isAccepted() const + + Returns TRUE if the receiver of the event handles the wheel event; + otherwise returns FALSE. +*/ + +/*! + \fn void TQWheelEvent::accept() + + Sets the accept flag of the wheel event object. + + Setting the accept parameter indicates that the receiver of the + event wants the wheel event. Unwanted wheel events are sent to the + parent widget. + + The accept flag is set by default. + + \sa ignore() +*/ + +/*! + \fn void TQWheelEvent::ignore() + + Clears the accept flag parameter of the wheel event object. + + Clearing the accept parameter indicates that the event receiver + does not want the wheel event. Unwanted wheel events are sent to + the parent widget. The accept flag is set by default. + + \sa accept() +*/ + + +/*! + \enum TQt::Modifier + + This enum type describes the keyboard modifier keys supported by + TQt. + + \value SHIFT the Shift keys provided on all standard keyboards. + \value META the Meta keys. + \value CTRL the Ctrl keys. + \value ALT the normal Alt keys, but not e.g. AltGr. + \value MODIFIER_MASK is a mask of Shift, Ctrl, Alt and Meta. + \value UNICODE_ACCEL the accelerator is specified as a Unicode code + point, not as a TQt Key. +*/ + +/*! + \class TQKeyEvent qevent.h + \brief The TQKeyEvent class contains describes a key event. + + \ingroup events + + Key events occur when a key is pressed or released when a widget + has keyboard input focus. + + A key event contains a special accept flag that indicates whether the + receiver wants the key event. You should call TQKeyEvent::ignore() if the + key press or release event is not handled by your widget. A key event is + propagated up the parent widget chain until a widget accepts it with + TQKeyEvent::accept() or an event filter consumes it. + Key events for multi media keys are ignored by default. You should call + TQKeyEvent::accept() if your widget handles those events. + + The TQWidget::setEnable() function can be used to enable or disable + mouse and keyboard events for a widget. + + The event handlers TQWidget::keyPressEvent() and + TQWidget::keyReleaseEvent() receive key events. + + \sa TQFocusEvent, TQWidget::grabKeyboard() +*/ + +/*! + \fn TQKeyEvent::TQKeyEvent( Type type, int key, int ascii, int state, + const TQString& text, bool autorep, ushort count ) + + Constructs a key event object. + + The \a type parameter must be \c TQEvent::KeyPress or \c + TQEvent::KeyRelease. If \a key is 0 the event is not a result of a + known key (e.g. it may be the result of a compose sequence or + keyboard macro). \a ascii is the ASCII code of the key that was + pressed or released. \a state holds the keyboard modifiers. \a + text is the Unicode text that the key generated. If \a autorep is + TRUE, isAutoRepeat() will be TRUE. \a count is the number of + single keys. + + The accept flag is set to TRUE. +*/ + +/*! + \fn int TQKeyEvent::key() const + + Returns the code of the key that was pressed or released. + + See \l TQt::Key for the list of keyboard codes. These codes are + independent of the underlying window system. + + A value of either 0 or Key_unknown means that the event is not + the result of a known key (e.g. it may be the result of a compose + sequence or a keyboard macro, or due to key event compression). + + Applications should not use the TQt latin 1 keycodes between 128 + and 255, but should rather use the TQKeyEvent::text(). This is + mainly for compatibility. + + \sa TQWidget::setKeyCompression() +*/ + +/*! + \fn int TQKeyEvent::ascii() const + + Returns the ASCII code of the key that was pressed or released. We + recommend using text() instead. + + \sa text() +*/ + +/*! + \fn TQString TQKeyEvent::text() const + + Returns the Unicode text that this key generated. The text returned + migth be empty, which is the case when pressing or + releasing modifying keys as Shift, Control, Alt and Meta. In these + cases key() will contain a valid value. + + \sa TQWidget::setKeyCompression() +*/ + +/*! + \fn ButtonState TQKeyEvent::state() const + + Returns the keyboard modifier flags that existed immediately + before the event occurred. + + The returned value is \c ShiftButton, \c ControlButton, \c AltButton + and \c MetaButton OR'ed together. + + \sa stateAfter() +*/ + +/*! + \fn ButtonState TQKeyEvent::stateAfter() const + + Returns the keyboard modifier flags that existed immediately after + the event occurred. + + \warning This function cannot be trusted. + + \sa state() +*/ +//###### We must check with XGetModifierMapping +TQt::ButtonState TQKeyEvent::stateAfter() const +{ + if ( key() == Key_Shift ) + return TQt::ButtonState(state()^ShiftButton); + if ( key() == Key_Control ) + return TQt::ButtonState(state()^ControlButton); + if ( key() == Key_Alt ) + return TQt::ButtonState(state()^AltButton); + if ( key() == Key_Meta ) + return TQt::ButtonState(state()^MetaButton); + return state(); +} + +/*! + \fn bool TQKeyEvent::isAccepted() const + + Returns TRUE if the receiver of the event wants to keep the key; + otherwise returns FALSE +*/ + +/*! + \fn void TQKeyEvent::accept() + + Sets the accept flag of the key event object. + + Setting the accept parameter indicates that the receiver of the + event wants the key event. Unwanted key events are sent to the + parent widget. + + The accept flag is set by default. + + \sa ignore() +*/ + +/*! + \fn bool TQKeyEvent::isAutoRepeat() const + + Returns TRUE if this event comes from an auto-repeating key and + FALSE if it comes from an initial key press. + + Note that if the event is a multiple-key compressed event that is + partly due to auto-repeat, this function could return either TRUE + or FALSE indeterminately. +*/ + +/*! + \fn int TQKeyEvent::count() const + + Returns the number of single keys for this event. If text() is not + empty, this is simply the length of the string. + + \sa TQWidget::setKeyCompression() +*/ + +/*! + \fn void TQKeyEvent::ignore() + + Clears the accept flag parameter of the key event object. + + Clearing the accept parameter indicates that the event receiver + does not want the key event. Unwanted key events are sent to the + parent widget. + + The accept flag is set by default. + + \sa accept() +*/ + +/*! + \enum TQt::Key + + The key names used by TQt. + + \value Key_Escape + \value Key_Tab + \value Key_Backtab + \value Key_Backspace + \value Key_Return + \value Key_Enter + \value Key_Insert + \value Key_Delete + \value Key_Pause + \value Key_Print + \value Key_SysReq + \value Key_Home + \value Key_End + \value Key_Left + \value Key_Up + \value Key_Right + \value Key_Down + \value Key_Prior + \value Key_Next + \value Key_Shift + \value Key_Control + \value Key_Meta + \value Key_Alt + \value Key_CapsLock + \value Key_NumLock + \value Key_ScrollLock + \value Key_Clear + \value Key_F1 + \value Key_F2 + \value Key_F3 + \value Key_F4 + \value Key_F5 + \value Key_F6 + \value Key_F7 + \value Key_F8 + \value Key_F9 + \value Key_F10 + \value Key_F11 + \value Key_F12 + \value Key_F13 + \value Key_F14 + \value Key_F15 + \value Key_F16 + \value Key_F17 + \value Key_F18 + \value Key_F19 + \value Key_F20 + \value Key_F21 + \value Key_F22 + \value Key_F23 + \value Key_F24 + \value Key_F25 + \value Key_F26 + \value Key_F27 + \value Key_F28 + \value Key_F29 + \value Key_F30 + \value Key_F31 + \value Key_F32 + \value Key_F33 + \value Key_F34 + \value Key_F35 + \value Key_Super_L + \value Key_Super_R + \value Key_Menu + \value Key_Hyper_L + \value Key_Hyper_R + \value Key_Help + \value Key_Space + \value Key_Any + \value Key_Exclam + \value Key_QuoteDbl + \value Key_NumberSign + \value Key_Dollar + \value Key_Percent + \value Key_Ampersand + \value Key_Apostrophe + \value Key_ParenLeft + \value Key_ParenRight + \value Key_Asterisk + \value Key_Plus + \value Key_Comma + \value Key_Minus + \value Key_Period + \value Key_Slash + \value Key_0 + \value Key_1 + \value Key_2 + \value Key_3 + \value Key_4 + \value Key_5 + \value Key_6 + \value Key_7 + \value Key_8 + \value Key_9 + \value Key_Colon + \value Key_Semicolon + \value Key_Less + \value Key_Equal + \value Key_Greater + \value Key_Question + \value Key_At + \value Key_A + \value Key_B + \value Key_C + \value Key_D + \value Key_E + \value Key_F + \value Key_G + \value Key_H + \value Key_I + \value Key_J + \value Key_K + \value Key_L + \value Key_M + \value Key_N + \value Key_O + \value Key_P + \value Key_Q + \value Key_R + \value Key_S + \value Key_T + \value Key_U + \value Key_V + \value Key_W + \value Key_X + \value Key_Y + \value Key_Z + \value Key_BracketLeft + \value Key_Backslash + \value Key_BracketRight + \value Key_AsciiCircum + \value Key_Underscore + \value Key_QuoteLeft + \value Key_BraceLeft + \value Key_Bar + \value Key_BraceRight + \value Key_AsciiTilde + + \value Key_nobreakspace + \value Key_exclamdown + \value Key_cent + \value Key_sterling + \value Key_currency + \value Key_yen + \value Key_brokenbar + \value Key_section + \value Key_diaeresis + \value Key_copyright + \value Key_ordfeminine + \value Key_guillemotleft + \value Key_notsign + \value Key_hyphen + \value Key_registered + \value Key_macron + \value Key_degree + \value Key_plusminus + \value Key_twosuperior + \value Key_threesuperior + \value Key_acute + \value Key_mu + \value Key_paragraph + \value Key_periodcentered + \value Key_cedilla + \value Key_onesuperior + \value Key_masculine + \value Key_guillemotright + \value Key_onequarter + \value Key_onehalf + \value Key_threequarters + \value Key_questiondown + \value Key_Agrave + \value Key_Aacute + \value Key_Acircumflex + \value Key_Atilde + \value Key_Adiaeresis + \value Key_Aring + \value Key_AE + \value Key_Ccedilla + \value Key_Egrave + \value Key_Eacute + \value Key_Ecircumflex + \value Key_Ediaeresis + \value Key_Igrave + \value Key_Iacute + \value Key_Icircumflex + \value Key_Idiaeresis + \value Key_ETH + \value Key_Ntilde + \value Key_Ograve + \value Key_Oacute + \value Key_Ocircumflex + \value Key_Otilde + \value Key_Odiaeresis + \value Key_multiply + \value Key_Ooblique + \value Key_Ugrave + \value Key_Uacute + \value Key_Ucircumflex + \value Key_Udiaeresis + \value Key_Yacute + \value Key_THORN + \value Key_ssharp + \value Key_agrave + \value Key_aacute + \value Key_acircumflex + \value Key_atilde + \value Key_adiaeresis + \value Key_aring + \value Key_ae + \value Key_ccedilla + \value Key_egrave + \value Key_eacute + \value Key_ecircumflex + \value Key_ediaeresis + \value Key_igrave + \value Key_iacute + \value Key_icircumflex + \value Key_idiaeresis + \value Key_eth + \value Key_ntilde + \value Key_ograve + \value Key_oacute + \value Key_ocircumflex + \value Key_otilde + \value Key_odiaeresis + \value Key_division + \value Key_oslash + \value Key_ugrave + \value Key_uacute + \value Key_ucircumflex + \value Key_udiaeresis + \value Key_yacute + \value Key_thorn + \value Key_ydiaeresis + + Multimedia keys + + \value Key_Back + \value Key_Forward + \value Key_Stop + \value Key_Refresh + + \value Key_VolumeDown + \value Key_VolumeMute + \value Key_VolumeUp + \value Key_BassBoost + \value Key_BassUp + \value Key_BassDown + \value Key_TrebleUp + \value Key_TrebleDown + + \value Key_MediaPlay + \value Key_MediaStop + \value Key_MediaPrev + \value Key_MediaNext + \value Key_MediaRecord + + \value Key_HomePage + \value Key_Favorites + \value Key_Search + \value Key_Standby + \value Key_OpenUrl + + \value Key_LaunchMail + \value Key_LaunchMedia + \value Key_Launch0 + \value Key_Launch1 + \value Key_Launch2 + \value Key_Launch3 + \value Key_Launch4 + \value Key_Launch5 + \value Key_Launch6 + \value Key_Launch7 + \value Key_Launch8 + \value Key_Launch9 + \value Key_LaunchA + \value Key_LaunchB + \value Key_LaunchC + \value Key_LaunchD + \value Key_LaunchE + \value Key_LaunchF + + \value Key_MediaLast + + \value Key_unknown + + \value Key_Direction_L internal use only + \value Key_Direction_R internal use only + +*/ + + +/*! + \class TQFocusEvent qevent.h + \brief The TQFocusEvent class contains event parameters for widget focus + events. + + \ingroup events + + Focus events are sent to widgets when the keyboard input focus + changes. Focus events occur due to mouse actions, keypresses (e.g. + Tab or Backtab), the window system, popup menus, keyboard + shortcuts or other application specific reasons. The reason for a + particular focus event is returned by reason() in the appropriate + event handler. + + The event handlers TQWidget::focusInEvent() and + TQWidget::focusOutEvent() receive focus events. + + Use setReason() to set the reason for all focus events, and + resetReason() to set the reason for all focus events to the reason + in force before the last setReason() call. + + \sa TQWidget::setFocus(), TQWidget::setFocusPolicy() +*/ + +/*! + \fn TQFocusEvent::TQFocusEvent( Type type ) + + Constructs a focus event object. + + The \a type parameter must be either \c TQEvent::FocusIn or \c + TQEvent::FocusOut. +*/ + + + +TQFocusEvent::Reason TQFocusEvent::m_reason = TQFocusEvent::Other; +TQFocusEvent::Reason TQFocusEvent::prev_reason = TQFocusEvent::Other; + + +/*! + \enum TQFocusEvent::Reason + + This enum specifies why the focus changed. + + \value Mouse because of a mouse action. + \value Tab because of a Tab press. + \value Backtab because of a Backtab press + (possibly including Shift/Control, e.g. Shift+Tab). + \value ActiveWindow because the window system made this window (in)active. + \value Popup because the application opened/closed a popup that grabbed/released focus. + \value Shortcut because of a keyboard shortcut. + \value Other any other reason, usually application-specific. + + See the \link focus.html keyboard focus overview\endlink for more + about focus. +*/ + +/*! + Returns the reason for this focus event. + + \sa setReason() + */ +TQFocusEvent::Reason TQFocusEvent::reason() +{ + return m_reason; +} + +/*! + Sets the reason for all future focus events to \a reason. + + \sa reason(), resetReason() + */ +void TQFocusEvent::setReason( Reason reason ) +{ + prev_reason = m_reason; + m_reason = reason; +} + +/*! + Resets the reason for all future focus events to the value before + the last setReason() call. + + \sa reason(), setReason() + */ +void TQFocusEvent::resetReason() +{ + m_reason = prev_reason; +} + +/*! + \fn bool TQFocusEvent::gotFocus() const + + Returns TRUE if the widget received the text input focus; + otherwise returns FALSE. +*/ + +/*! + \fn bool TQFocusEvent::lostFocus() const + + Returns TRUE if the widget lost the text input focus; otherwise + returns FALSE. +*/ + + +/*! + \class TQPaintEvent qevent.h + \brief The TQPaintEvent class contains event parameters for paint events. + + \ingroup events + + Paint events are sent to widgets that need to update themselves, + for instance when part of a widget is exposed because a covering + widget is moved. + + The event contains a region() that needs to be updated, and a + rect() that is the bounding rectangle of that region. Both are + provided because many widgets can't make much use of region(), and + rect() can be much faster than region().boundingRect(). Painting + is clipped to region() during processing of a paint event. + + The erased() function returns TRUE if the region() has been + cleared to the widget's background (see + TQWidget::backgroundMode()), and FALSE if the region's contents are + arbitrary. + + \sa TQPainter TQWidget::update() TQWidget::repaint() + TQWidget::paintEvent() TQWidget::backgroundMode() TQRegion +*/ + +/*! + \fn TQPaintEvent::TQPaintEvent( const TQRegion &paintRegion, bool erased=TRUE ) + + Constructs a paint event object with the region that should be + updated. The region is given by \a paintRegion. If \a erased is + TRUE the region will be cleared before repainting. +*/ + +/*! + \fn TQPaintEvent::TQPaintEvent( const TQRect &paintRect, bool erased=TRUE ) + + Constructs a paint event object with the rectangle that should be + updated. The region is also given by \a paintRect. If \a erased is + TRUE the region will be cleared before repainting. +*/ + +/*! + \fn TQPaintEvent::TQPaintEvent( const TQRegion &paintRegion, const TQRect &paintRect, bool erased=TRUE ) + + Constructs a paint event object with the rectangle \a paintRect + that should be updated. The region is given by \a paintRegion. If + \a erased is TRUE the region will be cleared before repainting. +*/ + +/*! + \fn const TQRect &TQPaintEvent::rect() const + + Returns the rectangle that should be updated. + + \sa region(), TQPainter::setClipRect() +*/ + +/*! + \fn const TQRegion &TQPaintEvent::region() const + + Returns the region that should be updated. + + \sa rect(), TQPainter::setClipRegion() +*/ + +/*! + \fn bool TQPaintEvent::erased() const + + Returns TRUE if the paint event region (or rectangle) has been + erased with the widget's background; otherwise returns FALSE. +*/ + +/*! + \class TQMoveEvent qevent.h + \brief The TQMoveEvent class contains event parameters for move events. + + \ingroup events + + Move events are sent to widgets that have been moved to a new position + relative to their parent. + + The event handler TQWidget::moveEvent() receives move events. + + \sa TQWidget::move(), TQWidget::setGeometry() +*/ + +/*! + \fn TQMoveEvent::TQMoveEvent( const TQPoint &pos, const TQPoint &oldPos ) + + Constructs a move event with the new and old widget positions, \a + pos and \a oldPos respectively. +*/ + +/*! + \fn const TQPoint &TQMoveEvent::pos() const + + Returns the new position of the widget. This excludes the window + frame for top level widgets. +*/ + +/*! + \fn const TQPoint &TQMoveEvent::oldPos() const + + Returns the old position of the widget. +*/ + + +/*! + \class TQResizeEvent qevent.h + \brief The TQResizeEvent class contains event parameters for resize events. + + \ingroup events + + Resize events are sent to widgets that have been resized. + + The event handler TQWidget::resizeEvent() receives resize events. + + \sa TQWidget::resize(), TQWidget::setGeometry() +*/ + +/*! + \fn TQResizeEvent::TQResizeEvent( const TQSize &size, const TQSize &oldSize ) + + Constructs a resize event with the new and old widget sizes, \a + size and \a oldSize respectively. +*/ + +/*! + \fn const TQSize &TQResizeEvent::size() const + + Returns the new size of the widget, which is the same as + TQWidget::size(). +*/ + +/*! + \fn const TQSize &TQResizeEvent::oldSize() const + + Returns the old size of the widget. +*/ + + +/*! + \class TQCloseEvent qevent.h + \brief The TQCloseEvent class contains parameters that describe a close event. + + \ingroup events + + Close events are sent to widgets that the user wants to close, + usually by choosing "Close" from the window menu, or by clicking + the `X' titlebar button. They are also sent when you call + TQWidget::close() to close a widget programmatically. + + Close events contain a flag that indicates whether the receiver + wants the widget to be closed or not. When a widget accepts the + close event, it is hidden (and destroyed if it was created with + the \c WDestructiveClose flag). If it refuses to accept the close + event nothing happens. (Under X11 it is possible that the window + manager will forcibly close the window; but at the time of writing + we are not aware of any window manager that does this.) + + The application's main widget -- TQApplication::mainWidget() -- + is a special case. When it accepts the close event, TQt leaves the + main event loop and the application is immediately terminated + (i.e. it returns from the call to TQApplication::exec() in the + main() function). + + The event handler TQWidget::closeEvent() receives close events. The + default implementation of this event handler accepts the close + event. If you do not want your widget to be hidden, or want some + special handing, you should reimplement the event handler. + + The \link simple-application.html#closeEvent closeEvent() in the + Application Walkthrough\endlink shows a close event handler that + asks whether to save a document before closing. + + If you want the widget to be deleted when it is closed, create it + with the \c WDestructiveClose widget flag. This is very useful for + independent top-level windows in a multi-window application. + + \l{TQObject}s emits the \link TQObject::destroyed() + destroyed()\endlink signal when they are deleted. + + If the last top-level window is closed, the + TQApplication::lastWindowClosed() signal is emitted. + + The isAccepted() function returns TRUE if the event's receiver has + agreed to close the widget; call accept() to agree to close the + widget and call ignore() if the receiver of this event does not + want the widget to be closed. + + \sa TQWidget::close(), TQWidget::hide(), TQObject::destroyed(), + TQApplication::setMainWidget(), TQApplication::lastWindowClosed(), + TQApplication::exec(), TQApplication::tquit() +*/ + +/*! + \fn TQCloseEvent::TQCloseEvent() + + Constructs a close event object with the accept parameter flag set + to FALSE. + + \sa accept() +*/ + +/*! + \fn bool TQCloseEvent::isAccepted() const + + Returns TRUE if the receiver of the event has agreed to close the + widget; otherwise returns FALSE. + + \sa accept(), ignore() +*/ + +/*! + \fn void TQCloseEvent::accept() + + Sets the accept flag of the close event object. + + Setting the accept flag indicates that the receiver of this event + agrees to close the widget. + + The accept flag is \e not set by default. + + If you choose to accept in TQWidget::closeEvent(), the widget will + be hidden. If the widget's \c WDestructiveClose flag is set, it + will also be destroyed. + + \sa ignore(), TQWidget::hide() +*/ + +/*! + \fn void TQCloseEvent::ignore() + + Clears the accept flag of the close event object. + + Clearing the accept flag indicates that the receiver of this event + does not want the widget to be closed. + + The close event is constructed with the accept flag cleared. + + \sa accept() +*/ + +/*! + \class TQIconDragEvent qevent.h + \brief The TQIconDragEvent class signals that a main icon drag has begun. + + \ingroup events + + Icon drag events are sent to widgets when the main icon of a window has been dragged away. + On Mac OS X this is fired when the proxy icon of a window is dragged off titlebar, in response to + this event is is normal to begin using drag and drop. +*/ + +/*! + \fn TQIconDragEvent::TQIconDragEvent() + + Constructs an icon drag event object with the accept parameter + flag set to FALSE. + + \sa accept() +*/ + +/*! + \fn bool TQIconDragEvent::isAccepted() const + + Returns TRUE if the receiver of the event has started a drag and + drop operation; otherwise returns FALSE. + + \sa accept(), ignore() +*/ + +/*! + \fn void TQIconDragEvent::accept() + + Sets the accept flag of the icon drag event object. + + Setting the accept flag indicates that the receiver of this event + has started a drag and drop oeration. + + The accept flag is \e not set by default. + + \sa ignore(), TQWidget::hide() +*/ + +/*! + \fn void TQIconDragEvent::ignore() + + Clears the accept flag of the icon drag object. + + Clearing the accept flag indicates that the receiver of this event + has not handled the icon drag as a result other events can be sent. + + The icon drag event is constructed with the accept flag cleared. + + \sa accept() +*/ + +/*! + \class TQContextMenuEvent qevent.h + \brief The TQContextMenuEvent class contains parameters that describe a context menu event. + + \ingroup events + + Context menu events are sent to widgets when a user triggers a + context menu. What triggers this is platform dependent. For + example, on Windows, pressing the menu button or releasing the + right mouse button will cause this event to be sent. + + When this event occurs it is customary to show a TQPopupMenu with a + context menu, if this is relevant to the context. + + Context menu events contain a special accept flag that indicates + whether the receiver accepted the event. If the event handler does + not accept the event, then whatever triggered the event will be + handled as a regular input event if possible. + + \sa TQPopupMenu +*/ + +/*! + \fn TQContextMenuEvent::TQContextMenuEvent( Reason reason, const TQPoint &pos, const TQPoint &globalPos, int state ) + + Constructs a context menu event object with the accept parameter + flag set to FALSE. + + The \a reason parameter must be \c TQContextMenuEvent::Mouse or \c + TQContextMenuEvent::Keyboard. + + The \a pos parameter specifies the mouse position relative to the + receiving widget. \a globalPos is the mouse position in absolute + coordinates. \a state is the ButtonState at the time of the event. +*/ + + +/*! + \fn TQContextMenuEvent::TQContextMenuEvent( Reason reason, const TQPoint &pos, int state ) + + Constructs a context menu event object with the accept parameter + flag set to FALSE. + + The \a reason parameter must be \c TQContextMenuEvent::Mouse or \c + TQContextMenuEvent::Keyboard. + + The \a pos parameter specifies the mouse position relative to the + receiving widget. \a state is the ButtonState at the time of the + event. + + The globalPos() is initialized to TQCursor::pos(), which may not be + appropriate. Use the other constructor to specify the global + position explicitly. +*/ + +TQContextMenuEvent::TQContextMenuEvent( Reason reason, const TQPoint &pos, int state ) + : TQEvent( ContextMenu ), p( pos ), accpt(TRUE), consum(TRUE), + reas( reason ), s((ushort)state) +{ + gp = TQCursor::pos(); +} + +/*! + \fn const TQPoint &TQContextMenuEvent::pos() const + + Returns the position of the mouse pointer relative to the widget + that received the event. + + \sa x(), y(), globalPos() +*/ + +/*! + \fn int TQContextMenuEvent::x() const + + Returns the x-position of the mouse pointer, relative to the + widget that received the event. + + \sa y(), pos() +*/ + +/*! + \fn int TQContextMenuEvent::y() const + + Returns the y-position of the mouse pointer, relative to the + widget that received the event. + + \sa x(), pos() +*/ + +/*! + \fn const TQPoint &TQContextMenuEvent::globalPos() const + + Returns the global position of the mouse pointer at the time of + the event. + + \sa x(), y(), pos() +*/ + +/*! + \fn int TQContextMenuEvent::globalX() const + + Returns the global x-position of the mouse pointer at the time of + the event. + + \sa globalY(), globalPos() +*/ + +/*! + \fn int TQContextMenuEvent::globalY() const + + Returns the global y-position of the mouse pointer at the time of + the event. + + \sa globalX(), globalPos() +*/ + +/*! + \fn ButtonState TQContextMenuEvent::state() const + + Returns the button state (a combination of mouse buttons and + keyboard modifiers), i.e. what buttons and keys were being + pressed immediately before the event was generated. + + The returned value is \c LeftButton, \c RightButton, \c MidButton, + \c ShiftButton, \c ControlButton and \c AltButton OR'ed together. +*/ + +/*! + \fn bool TQContextMenuEvent::isConsumed() const + + Returns TRUE (which stops propagation of the event) if the + receiver has blocked the event; otherwise returns FALSE. + + \sa accept(), ignore(), consume() +*/ + +/*! + \fn void TQContextMenuEvent::consume() + + Sets the consume flag of the context event object. + + Setting the consume flag indicates that the receiver of this event + does not want the event to be propagated further (i.e. not sent to + parent classes.) + + The consumed flag is not set by default. + + \sa ignore() accept() +*/ + +/*! + \fn bool TQContextMenuEvent::isAccepted() const + + Returns TRUE if the receiver has processed the event; otherwise + returns FALSE. + + \sa accept(), ignore(), consume() +*/ + +/*! + \fn void TQContextMenuEvent::accept() + + Sets the accept flag of the context event object. + + Setting the accept flag indicates that the receiver of this event + has processed the event. Processing the event means you did + something with it and it will be implicitly consumed. + + The accept flag is not set by default. + + \sa ignore() consume() +*/ + +/*! + \fn void TQContextMenuEvent::ignore() + + Clears the accept flag of the context event object. + + Clearing the accept flag indicates that the receiver of this event + does not need to show a context menu. This will implicitly remove + the consumed flag as well. + + The accept flag is not set by default. + + \sa accept() consume() +*/ + +/*! + \enum TQContextMenuEvent::Reason + + This enum describes the reason the ContextMenuEvent was sent. The + values are: + + \value Mouse The mouse caused the event to be sent. Normally this + means the right mouse button was clicked, but this is platform + specific. + + \value Keyboard The keyboard caused this event to be sent. On + Windows this means the menu button was pressed. + + \value Other The event was sent by some other means (i.e. not by + the mouse or keyboard). +*/ + + +/*! + \fn TQContextMenuEvent::Reason TQContextMenuEvent::reason() const + + Returns the reason for this context event. +*/ + + +/*! + \class TQIMEvent qevent.h + \brief The TQIMEvent class provides parameters for input method events. + + \ingroup events + + Input method events are sent to widgets when an input method is + used to enter text into a widget. Input methods are widely used to + enter text in Asian and other complex languages. + + The events are of interest to widgets that accept keyboard input + and want to be able to correctly handle complex languages. Text + input in such languages is usually a three step process. + + \list 1 + \i Starting to Compose
+ When the user presses the first key on a keyboard an input context + is created. This input context will contain a string with the + typed characters. + + \i Composing
+ With every new key pressed, the input method will try to create a + matching string for the text typed so far. While the input context + is active, the user can only move the cursor inside the string + belonging to this input context. + + \i Completing
+ At some point, e.g. when the user presses the Spacebar, they get + to this stage, where they can choose from a number of strings that + match the text they have typed so far. The user can press Enter to + confirm their choice or Escape to cancel the input; in either case + the input context will be closed. + \endlist + + Note that the particular key presses used for a given input + context may differ from those we've mentioned here, i.e. they may + not be Spacebar, Enter and Escape. + + These three stages are represented by three different types of + events. The IMStartEvent, IMComposeEvent and IMEndEvent. When a + new input context is created, an IMStartEvent will be sent to the + widget and delivered to the \l TQWidget::imStartEvent() function. + The widget can then update internal data structures to reflect + this. + + After this, an IMComposeEvent will be sent to the widget for + every key the user presses. It will contain the current + composition string the widget has to show and the current cursor + position within the composition string. This string is temporary + and can change with every key the user types, so the widget will + need to store the state before the composition started (the state + it had when it received the IMStartEvent). IMComposeEvents will be + delivered to the \l TQWidget::imComposeEvent() function. + + Usually, widgets try to mark the part of the text that is part of + the current composition in a way that is visible to the user. A + commonly used visual cue is to use a dotted underline. + + After the user has selected the final string, an IMEndEvent will + be sent to the widget. The event contains the final string the + user selected, and could be empty if they canceled the + composition. This string should be accepted as the final text the + user entered, and the intermediate composition string should be + cleared. These events are delivered to \l TQWidget::imEndEvent(). + + If the user clicks another widget, taking the focus out of the + widget where the composition is taking place the IMEndEvent will + be sent and the string it holds will be the result of the + composition up to that point (which may be an empty string). +*/ + +/*! + \fn TQIMEvent::TQIMEvent( Type type, const TQString &text, int cursorPosition ) + + Constructs a new TQIMEvent with the accept flag set to FALSE. \a + type can be one of TQEvent::IMStartEvent, TQEvent::IMComposeEvent + or TQEvent::IMEndEvent. \a text contains the current compostion + string and \a cursorPosition the current position of the cursor + inside \a text. +*/ + +/*! + \fn const TQString &TQIMEvent::text() const + + Returns the composition text. This is a null string for an + IMStartEvent, and contains the final accepted string (which may be + empty) in the IMEndEvent. +*/ + +/*! + \fn int TQIMEvent::cursorPos() const + + Returns the current cursor position inside the composition string. + Will return -1 for IMStartEvent and IMEndEvent. +*/ + +/*! + \fn int TQIMEvent::selectionLength() const + + Returns the number of characters in the composition string ( + starting at cursorPos() ) that should be marked as selected by the + input widget receiving the event. + Will return 0 for IMStartEvent and IMEndEvent. +*/ + +/*! + \fn bool TQIMEvent::isAccepted() const + + Returns TRUE if the receiver of the event processed the event; + otherwise returns FALSE. +*/ + +/*! + \fn void TQIMEvent::accept() + + Sets the accept flag of the input method event object. + + Setting the accept parameter indicates that the receiver of the + event processed the input method event. + + The accept flag is not set by default. + + \sa ignore() +*/ + + +/*! + \fn void TQIMEvent::ignore() + + Clears the accept flag parameter of the input method event object. + + Clearing the accept parameter indicates that the event receiver + does not want the input method event. + + The accept flag is cleared by default. + + \sa accept() +*/ + +/*! + \class TQTabletEvent qevent.h + \brief The TQTabletEvent class contains parameters that describe a Tablet + event. + + \ingroup events + + Tablet Events are generated from a Wacom© tablet. Most of + the time you will want to deal with events from the tablet as if + they were events from a mouse, for example retrieving the position + with x(), y(), pos(), globalX(), globalY() and globalPos(). In + some situations you may wish to retrieve the extra information + provided by the tablet device driver, for example, you might want + to adjust color brightness based on pressure. TQTabletEvent allows + you to get the pressure(), the xTilt() and yTilt(), as well as the + type of device being used with device() (see \l{TabletDevice}). + + A tablet event contains a special accept flag that indicates + whether the receiver wants the event. You should call + TQTabletEvent::accept() if you handle the tablet event; otherwise + it will be sent to the parent widget. + + The TQWidget::setEnabled() function can be used to enable or + disable mouse and keyboard events for a widget. + + The event handler TQWidget::tabletEvent() receives all three types of tablet + events. TQt will first send a tabletEvent and then, if it is not accepted, + it will send a mouse event. This allows applications that don't utilize + tablets to use a tablet like a mouse while also enabling those who want to + use both tablets and mouses differently. + +*/ + +/*! + \enum TQTabletEvent::TabletDevice + + This enum defines what type of device is generating the event. + + \value NoDevice No device, or an unknown device. + \value Puck A Puck (a device that is similar to a flat mouse with + a transparent circle with cross-hairs). + \value Stylus A Stylus (the narrow end of the pen). + \value Eraser An Eraser (the broad end of the pen). + \omit + \value Menu A menu button was pressed (currently unimplemented). +*/ + +/*! + \fn TQTabletEvent::TQTabletEvent( Type t, const TQPoint &pos, + const TQPoint &globalPos, int device, + int pressure, int xTilt, int yTilt, + const TQPair &uId ) + Construct a tablet event of type \a t. The position of when the event occurred is given + int \a pos and \a globalPos. \a device contains the \link TabletDevice device type\endlink, + \a pressure contains the pressure exerted on the \a device, \a xTilt and \a yTilt contain + \a device's degree of tilt from the X and Y axis respectively. The \a uId contains an + event id. + + On Irix, \a globalPos will contain the high-resolution coordinates received from the + tablet device driver, instead of from the windowing system. + + \sa pos(), globalPos(), device(), pressure(), xTilt(), yTilt() +*/ + +TQTabletEvent::TQTabletEvent( Type t, const TQPoint &pos, const TQPoint &globalPos, int device, + int pressure, int xTilt, int yTilt, + const TQPair &uId ) + : TQEvent( t ), + mPos( pos ), + mGPos( globalPos ), + mDev( device ), + mPress( pressure ), + mXT( xTilt ), + mYT( yTilt ), + mType( uId.first ), + mPhy( uId.second ), + mbAcc(TRUE) +{} + +/*! + \obsolete + \fn TQTabletEvent::TQTabletEvent( const TQPoint &pos, const TQPoint &globalPos, int device, int pressure, int xTilt, int yTilt, const TQPair &uId ) + + Constructs a tablet event object. The position when the event + occurred is is given in \a pos and \a globalPos. \a device + contains the \link TabletDevice device type\endlink, \a pressure + contains the pressure exerted on the \a device, \a xTilt and \a + yTilt contain the \a device's degrees of tilt from the X and Y + axis respectively. The \a uId contains an event id. + + On Irix, \a globalPos will contain the high-resolution coordinates + received from the tablet device driver, instead of from the + windowing system. + + \sa pos(), globalPos(), device(), pressure(), xTilt(), yTilt() +*/ + +/*! + \fn TabletDevices TQTabletEvent::device() const + + Returns the type of device that generated the event. Useful if you + want one end of the pen to do something different than the other. + + \sa TabletDevice +*/ + +/*! + \fn int TQTabletEvent::pressure() const + + Returns the pressure that is exerted on the device. This number is + a value from 0 (no pressure) to 255 (maximum pressure). The + pressure is always scaled to be within this range no matter how + many pressure levels the underlying hardware supports. +*/ + +/*! + \fn int TQTabletEvent::xTilt() const + + Returns the difference from the perpendicular in the X Axis. + Positive values are towards the tablet's physical right. The angle + is in the range -60 to +60 degrees. + + \sa yTilt() +*/ + +/*! + \fn int TQTabletEvent::yTilt() const + + Returns the difference from the perpendicular in the Y Axis. + Positive values are towards the bottom of the tablet. The angle is + within the range -60 to +60 degrees. + + \sa xTilt() +*/ + +/*! + \fn const TQPoint &TQTabletEvent::pos() const + + Returns the position of the device, relative to the widget that + received the event. + + If you move widgets around in response to mouse events, use + globalPos() instead of this function. + + \sa x(), y(), globalPos() +*/ + +/*! + \fn int TQTabletEvent::x() const + + Returns the x-position of the device, relative to the widget that + received the event. + + \sa y(), pos() +*/ + +/*! + \fn int TQTabletEvent::y() const + + Returns the y-position of the device, relative to the widget that + received the event. + + \sa x(), pos() +*/ + +/*! + \fn const TQPoint &TQTabletEvent::globalPos() const + + Returns the global position of the device \e{at the time of the + event}. This is important on asynchronous windows systems like X11; + whenever you move your widgets around in response to mouse events, + globalPos() can differ significantly from the current position + TQCursor::pos(). + + \sa globalX(), globalY() +*/ + +/*! + \fn int TQTabletEvent::globalX() const + + Returns the global x-position of the mouse pointer at the time of + the event. + + \sa globalY(), globalPos() +*/ + +/*! + \fn int TQTabletEvent::globalY() const + + Returns the global y-position of the mouse pointer at the time of + the event. + + \sa globalX(), globalPos() +*/ + +/*! + \fn bool TQTabletEvent::isAccepted() const + + Returns TRUE if the receiver of the event handles the tablet + event; otherwise returns FALSE. +*/ + +/*! + \fn void TQTabletEvent::accept() + + Sets the accept flag of the tablet event object. + + Setting the accept flag indicates that the receiver of the event + wants the tablet event. Unwanted tablet events are sent to the + parent widget. + + The accept flag is set by default. + + \sa ignore() +*/ + +/*! + \fn void TQTabletEvent::ignore() + + Clears the accept flag parameter of the tablet event object. + + Clearing the accept flag indicates that the event receiver does + not want the tablet event. Unwanted tablet events are sent to the + parent widget. + + The accept flag is set by default. + + \sa accept() +*/ + +/*! + \fn TQPair TQTabletEvent::uniqueId() + + Returns a unique ID for the current device. It is possible to + generate a unique ID for any Wacom© device. This makes it + possible to differentiate between multiple devices being used at + the same time on the tablet. The \c first member contains a value + for the type, the \c second member contains a physical ID obtained + from the device. Each combination of these values is unique. Note: + for different platforms, the \c first value is different due to + different driver implementations. +*/ + +/*! + \class TQChildEvent qevent.h + \brief The TQChildEvent class contains event parameters for child object + events. + + \ingroup events + + Child events are sent to objects when children are inserted or + removed. + + A \c ChildRemoved event is sent immediately, but a \c + ChildInserted event is \e posted (with TQApplication::postEvent()). + + Note that if a child is removed immediately after it is inserted, + the \c ChildInserted event may be suppressed, but the \c + ChildRemoved event will always be sent. In this case there will be + a \c ChildRemoved event without a corresponding \c ChildInserted + event. + + The handler for these events is TQObject::childEvent(). +*/ + +/*! + \fn TQChildEvent::TQChildEvent( Type type, TQObject *child ) + + Constructs a child event object. The \a child is the object that + is to be removed or inserted. + + The \a type parameter must be either \c TQEvent::ChildInserted or + \c TQEvent::ChildRemoved. +*/ + +/*! + \fn TQObject *TQChildEvent::child() const + + Returns the child widget that was inserted or removed. +*/ + +/*! + \fn bool TQChildEvent::inserted() const + + Returns TRUE if the widget received a new child; otherwise returns + FALSE. +*/ + +/*! + \fn bool TQChildEvent::removed() const + + Returns TRUE if the object lost a child; otherwise returns FALSE. +*/ + + + + +/*! + \class TQCustomEvent qevent.h + \brief The TQCustomEvent class provides support for custom events. + + \ingroup events + + TQCustomEvent is a generic event class for user-defined events. + User defined events can be sent to widgets or other TQObject + instances using TQApplication::postEvent() or + TQApplication::sendEvent(). Subclasses of TQObject can easily + receive custom events by implementing the TQObject::customEvent() + event handler function. + + TQCustomEvent objects should be created with a type ID that + uniquely identifies the event type. To avoid clashes with the + TQt-defined events types, the value should be at least as large as + the value of the "User" entry in the TQEvent::Type enum. + + TQCustomEvent contains a generic void* data member that may be used + for transferring event-specific data to the receiver. Note that + since events are normally delivered asynchronously, the data + pointer, if used, must remain valid until the event has been + received and processed. + + TQCustomEvent can be used as-is for simple user-defined event + types, but normally you will want to make a subclass of it for + your event types. In a subclass, you can add data members that are + suitable for your event type. + + Example: + \code + class ColorChangeEvent : public TQCustomEvent + { + public: + ColorChangeEvent( TQColor color ) + : TQCustomEvent( 65432 ), c( color ) {} + TQColor color() const { return c; } + private: + TQColor c; + }; + + // To send an event of this custom event type: + + ColorChangeEvent* ce = new ColorChangeEvent( blue ); + TQApplication::postEvent( receiver, ce ); // TQt will delete it when done + + // To receive an event of this custom event type: + + void MyWidget::customEvent( TQCustomEvent * e ) + { + if ( e->type() == 65432 ) { // It must be a ColorChangeEvent + ColorChangeEvent* ce = (ColorChangeEvent*)e; + newColor = ce->color(); + } + } + \endcode + + \sa TQWidget::customEvent(), TQApplication::notify() +*/ + + +/*! + Constructs a custom event object with event type \a type. The + value of \a type must be at least as large as TQEvent::User. The + data pointer is set to 0. +*/ + +TQCustomEvent::TQCustomEvent( int type ) + : TQEvent( (TQEvent::Type)type ), d( 0 ) +{ +} + + +/*! + \fn TQCustomEvent::TQCustomEvent( Type type, void *data ) + + Constructs a custom event object with the event type \a type and a + pointer to \a data. (Note that any int value may safely be cast to + TQEvent::Type). +*/ + + +/*! + \fn void TQCustomEvent::setData( void* data ) + + Sets the generic data pointer to \a data. + + \sa data() +*/ + +/*! + \fn void *TQCustomEvent::data() const + + Returns a pointer to the generic event data. + + \sa setData() +*/ + + + +/*! + \fn TQDragMoveEvent::TQDragMoveEvent( const TQPoint& pos, Type type ) + + Creates a TQDragMoveEvent for which the mouse is at point \a pos, + and the event is of type \a type. + + \warning Do not create a TQDragMoveEvent yourself since these + objects rely on TQt's internal state. +*/ + +/*! + \fn void TQDragMoveEvent::accept( const TQRect & r ) + + The same as accept(), but also notifies that future moves will + also be acceptable if they remain within the rectangle \a r on the + widget: this can improve performance, but may also be ignored by + the underlying system. + + If the rectangle is \link TQRect::isEmpty() empty\endlink, then + drag move events will be sent continuously. This is useful if the + source is scrolling in a timer event. +*/ + +/*! + \fn void TQDragMoveEvent::ignore( const TQRect & r) + + The opposite of accept(const TQRect&), i.e. says that moves within + rectangle \a r are not acceptable (will be ignored). +*/ + +/*! + \fn TQRect TQDragMoveEvent::answerRect() const + + Returns the rectangle for which the acceptance of the move event + applies. +*/ + + + +/*! + \fn const TQPoint& TQDropEvent::pos() const + + Returns the position where the drop was made. +*/ + +/*! + \fn bool TQDropEvent::isAccepted () const + + Returns TRUE if the drop target accepts the event; otherwise + returns FALSE. +*/ + +/*! + \fn void TQDropEvent::accept(bool y=TRUE) + + Call this function to indicate whether the event provided data + which your widget processed. Set \a y to TRUE (the default) if + your widget could process the data, otherwise set \a y to FALSE. + To get the data, use encodedData(), or preferably, the decode() + methods of existing TQDragObject subclasses, such as + TQTextDrag::decode(), or your own subclasses. + + \sa acceptAction() +*/ + +/*! + \fn void TQDropEvent::acceptAction(bool y=TRUE) + + Call this to indicate that the action described by action() is + accepted (i.e. if \a y is TRUE, which is the default), not merely + the default copy action. If you call acceptAction(TRUE), there is + no need to also call accept(TRUE). +*/ + +/*! + \fn void TQDragMoveEvent::accept( bool y ) + \reimp + \internal + Remove in 3.0 +*/ + +/*! + \fn void TQDragMoveEvent::ignore() + \reimp + \internal + Remove in 3.0 +*/ + + +/*! + \enum TQDropEvent::Action + + This enum describes the action which a source requests that a + target perform with dropped data. + + \value Copy The default action. The source simply uses the data + provided in the operation. + \value Link The source should somehow create a link to the + location specified by the data. + \value Move The source should somehow move the object from the + location specified by the data to a new location. + \value Private The target has special knowledge of the MIME type, + which the source should respond to in a similar way to + a Copy. + \value UserAction The source and target can co-operate using + special actions. This feature is not currently + supported. + + The Link and Move actions only makes sense if the data is a + reference, for example, text/uri-list file lists (see TQUriDrag). +*/ + +/*! + \fn void TQDropEvent::setAction( Action a ) + + Sets the action to \a a. This is used internally, you should not + need to call this in your code: the \e source decides the action, + not the target. +*/ + +/*! + \fn Action TQDropEvent::action() const + + Returns the Action which the target is requesting to be performed + with the data. If your application understands the action and can + process the supplied data, call acceptAction(); if your + application can process the supplied data but can only perform the + Copy action, call accept(). +*/ + +/*! + \fn void TQDropEvent::ignore() + + The opposite of accept(), i.e. you have ignored the drop event. +*/ + +/*! + \fn bool TQDropEvent::isActionAccepted () const + + Returns TRUE if the drop action was accepted by the drop site; + otherwise returns FALSE. +*/ + + +/*! + \fn void TQDropEvent::setPoint (const TQPoint & np) + + Sets the drop to happen at point \a np. You do not normally need + to use this as it will be set internally before your widget + receives the drop event. +*/ // ### here too - what coordinate system? + + +/*! + \class TQDragEnterEvent qevent.h + \brief The TQDragEnterEvent class provides an event which is sent to the widget when a drag and drop first drags onto the widget. + + \ingroup events + \ingroup draganddrop + + This event is always immediately followed by a TQDragMoveEvent, so + you only need to respond to one or the other event. This class + inherits most of its functionality from TQDragMoveEvent, which in + turn inherits most of its functionality from TQDropEvent. + + \sa TQDragLeaveEvent, TQDragMoveEvent, TQDropEvent +*/ + +/*! + \fn TQDragEnterEvent::TQDragEnterEvent (const TQPoint & pos) + + Constructs a TQDragEnterEvent entering at the given point, \a pos. + + \warning Do not create a TQDragEnterEvent yourself since these + objects rely on TQt's internal state. +*/ + +/*! + \class TQDragLeaveEvent qevent.h + \brief The TQDragLeaveEvent class provides an event which is sent to the widget when a drag and drop leaves the widget. + + \ingroup events + \ingroup draganddrop + + This event is always preceded by a TQDragEnterEvent and a series of + \l{TQDragMoveEvent}s. It is not sent if a TQDropEvent is sent + instead. + + \sa TQDragEnterEvent, TQDragMoveEvent, TQDropEvent +*/ + +/*! + \fn TQDragLeaveEvent::TQDragLeaveEvent() + + Constructs a TQDragLeaveEvent. + + \warning Do not create a TQDragLeaveEvent yourself since these + objects rely on TQt's internal state. +*/ + +/*! + \class TQHideEvent qevent.h + \brief The TQHideEvent class provides an event which is sent after a widget is hidden. + + \ingroup events + + This event is sent just before TQWidget::hide() returns, and also + when a top-level window has been hidden (iconified) by the user. + + If spontaneous() is TRUE the event originated outside the + application, i.e. the user hid the window using the window manager + controls, either by iconifying the window or by switching to + another virtual desktop where the window isn't visible. The window + will become hidden but not withdrawn. If the window was iconified, + TQWidget::isMinimized() returns TRUE. + + \sa TQShowEvent +*/ + +/*! + \fn TQHideEvent::TQHideEvent() + + Constructs a TQHideEvent. +*/ + +/*! + \class TQShowEvent qevent.h + \brief The TQShowEvent class provides an event which is sent when a widget is shown. + + \ingroup events + + There are two kinds of show events: show events caused by the + window system (spontaneous) and internal show events. Spontaneous + show events are sent just after the window system shows the + window, including after a top-level window has been shown + (un-iconified) by the user. Internal show events are delivered + just before the widget becomes visible. + + \sa TQHideEvent +*/ + +/*! + \fn TQShowEvent::TQShowEvent() + + Constructs a TQShowEvent. +*/ + + +/*! + \fn TQByteArray TQDropEvent::data(const char* f) const + + \obsolete + + Use TQDropEvent::encodedData(). +*/ + + +/*! + Destroys the event. If it was \link + TQApplication::postEvent() posted \endlink, + it will be removed from the list of events to be posted. +*/ + +TQEvent::~TQEvent() +{ + if ( posted && qApp ) + TQApplication::removePostedEvent( this ); +} diff --git a/src/kernel/qevent.h b/src/kernel/qevent.h new file mode 100644 index 000000000..8f2706841 --- /dev/null +++ b/src/kernel/qevent.h @@ -0,0 +1,617 @@ +/**************************************************************************** +** +** Definition of event classes +** +** Created : 931029 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQEVENT_H +#define TQEVENT_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qregion.h" +#include "qnamespace.h" +#include "qmime.h" +#include "qpair.h" +#endif // QT_H + +class Q_EXPORT TQEvent: public TQt // event base class +{ +public: + enum Type { + + /* + If you get a strange compiler error on the line with None, + it's probably because you're also including X11 headers, + which #define the symbol None. Put the X11 includes after + the TQt includes to solve this problem. + */ + + None = 0, // invalid event + + + Timer = 1, // timer event + MouseButtonPress = 2, // mouse button pressed + MouseButtonRelease = 3, // mouse button released + MouseButtonDblClick = 4, // mouse button double click + MouseMove = 5, // mouse move + KeyPress = 6, // key pressed + KeyRelease = 7, // key released + FocusIn = 8, // keyboard focus received + FocusOut = 9, // keyboard focus lost + Enter = 10, // mouse enters widget + Leave = 11, // mouse leaves widget + Paint = 12, // paint widget + Move = 13, // move widget + Resize = 14, // resize widget + Create = 15, // after object creation + Destroy = 16, // during object destruction + Show = 17, // widget is shown + Hide = 18, // widget is hidden + Close = 19, // request to close widget + Quit = 20, // request to tquit application + Reparent = 21, // widget has been reparented + ShowMinimized = 22, // widget is shown minimized + ShowNormal = 23, // widget is shown normal + WindowActivate = 24, // window was activated + WindowDeactivate = 25, // window was deactivated + ShowToParent = 26, // widget is shown to parent + HideToParent = 27, // widget is hidden to parent + ShowMaximized = 28, // widget is shown maximized + ShowFullScreen = 29, // widget is shown full-screen + Accel = 30, // accelerator event + Wheel = 31, // wheel event + AccelAvailable = 32, // accelerator available event + CaptionChange = 33, // caption changed + IconChange = 34, // icon changed + ParentFontChange = 35, // parent font changed + ApplicationFontChange = 36, // application font changed + ParentPaletteChange = 37, // parent palette changed + ApplicationPaletteChange = 38, // application palette changed + PaletteChange = 39, // widget palette changed + Clipboard = 40, // internal clipboard event + Speech = 42, // reserved for speech input + SockAct = 50, // socket activation + AccelOverride = 51, // accelerator override event + DeferredDelete = 52, // deferred delete event + DragEnter = 60, // drag moves into widget + DragMove = 61, // drag moves in widget + DragLeave = 62, // drag leaves or is cancelled + Drop = 63, // actual drop + DragResponse = 64, // drag accepted/rejected + ChildInserted = 70, // new child widget + ChildRemoved = 71, // deleted child widget + LayoutHint = 72, // child min/max size changed + ShowWindowRequest = 73, // widget's window should be mapped + WindowBlocked = 74, // window is about to be blocked modally + WindowUnblocked = 75, // windows modal blocking has ended + ActivateControl = 80, // ActiveX activation + DeactivateControl = 81, // ActiveX deactivation + ContextMenu = 82, // context popup menu + IMStart = 83, // input method composition start + IMCompose = 84, // input method composition + IMEnd = 85, // input method composition end + Accessibility = 86, // accessibility information is requested + TabletMove = 87, // Wacom tablet event + LocaleChange = 88, // the system locale changed + LanguageChange = 89, // the application language changed + LayoutDirectionChange = 90, // the layout direction changed + Style = 91, // internal style event + TabletPress = 92, // tablet press + TabletRelease = 93, // tablet release + OkRequest = 94, // CE (Ok) button pressed + HelpRequest = 95, // CE (?) button pressed + WindowStateChange = 96, // window state has changed + IconDrag = 97, // proxy icon dragged + User = 1000, // first user event id + MaxUser = 65535 // last user event id + }; + + + TQEvent( Type type ) : t(type), posted(FALSE), spont(FALSE) {} + virtual ~TQEvent(); + Type type() const { return t; } + bool spontaneous() const { return spont; } +protected: + Type t; +private: + uint posted : 1; + uint spont : 1; + + + friend class TQApplication; + friend class TQAccelManager; + friend class TQBaseApplication; + friend class TQETWidget; +}; + + +class Q_EXPORT TQTimerEvent : public TQEvent +{ +public: + TQTimerEvent( int timerId ) + : TQEvent(Timer), id(timerId) {} + int timerId() const { return id; } +protected: + int id; +}; + + +class Q_EXPORT TQMouseEvent : public TQEvent +{ +public: + TQMouseEvent( Type type, const TQPoint &pos, int button, int state ); + + TQMouseEvent( Type type, const TQPoint &pos, const TQPoint&globalPos, + int button, int state ) + : TQEvent(type), p(pos), g(globalPos), b((ushort)button),s((ushort)state),accpt(TRUE) {}; + + const TQPoint &pos() const { return p; } + const TQPoint &globalPos() const { return g; } + int x() const { return p.x(); } + int y() const { return p.y(); } + int globalX() const { return g.x(); } + int globalY() const { return g.y(); } + ButtonState button() const { return (ButtonState) b; } + ButtonState state() const { return (ButtonState) s; } + ButtonState stateAfter() const; + bool isAccepted() const { return accpt; } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } +protected: + TQPoint p; + TQPoint g; + ushort b; + ushort s; + uint accpt:1; +}; + + +#ifndef QT_NO_WHEELEVENT +class Q_EXPORT TQWheelEvent : public TQEvent +{ +public: + TQWheelEvent( const TQPoint &pos, int delta, int state, Orientation orient = Vertical ); + TQWheelEvent( const TQPoint &pos, const TQPoint& globalPos, int delta, int state, Orientation orient = Vertical ) + : TQEvent(Wheel), p(pos), g(globalPos), d(delta), s((ushort)state), + accpt(TRUE), o(orient) {} + int delta() const { return d; } + const TQPoint &pos() const { return p; } + const TQPoint &globalPos() const { return g; } + int x() const { return p.x(); } + int y() const { return p.y(); } + int globalX() const { return g.x(); } + int globalY() const { return g.y(); } + ButtonState state() const { return ButtonState(s); } + Orientation orientation() const { return o; } + bool isAccepted() const { return accpt; } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } +protected: + TQPoint p; + TQPoint g; + int d; + ushort s; + bool accpt; + Orientation o; +}; +#endif + +class Q_EXPORT TQTabletEvent : public TQEvent +{ +public: + enum TabletDevice { NoDevice = -1, Puck, Stylus, Eraser }; + TQTabletEvent( Type t, const TQPoint &pos, const TQPoint &globalPos, int device, + int pressure, int xTilt, int yTilt, const TQPair &uId ); + TQTabletEvent( const TQPoint &pos, const TQPoint &globalPos, int device, + int pressure, int xTilt, int yTilt, const TQPair &uId ) + : TQEvent( TabletMove ), mPos( pos ), mGPos( globalPos ), mDev( device ), + mPress( pressure ), mXT( xTilt ), mYT( yTilt ), mType( uId.first ), + mPhy( uId.second ), mbAcc(TRUE) + {} + int pressure() const { return mPress; } + int xTilt() const { return mXT; } + int yTilt() const { return mYT; } + const TQPoint &pos() const { return mPos; } + const TQPoint &globalPos() const { return mGPos; } + int x() const { return mPos.x(); } + int y() const { return mPos.y(); } + int globalX() const { return mGPos.x(); } + int globalY() const { return mGPos.y(); } + TabletDevice device() const { return TabletDevice(mDev); } + int isAccepted() const { return mbAcc; } + void accept() { mbAcc = TRUE; } + void ignore() { mbAcc = FALSE; } + TQPair uniqueId() { return TQPair( mType, mPhy); } +protected: + TQPoint mPos; + TQPoint mGPos; + int mDev, + mPress, + mXT, + mYT, + mType, + mPhy; + bool mbAcc; + +}; + +class Q_EXPORT TQKeyEvent : public TQEvent +{ +public: + TQKeyEvent( Type type, int key, int ascii, int state, + const TQString& text=TQString::null, bool autorep=FALSE, ushort count=1 ) + : TQEvent(type), txt(text), k((ushort)key), s((ushort)state), + a((uchar)ascii), accpt(TRUE), autor(autorep), c(count) + { + if ( key >= Key_Back && key <= Key_MediaLast ) + accpt = FALSE; + } + int key() const { return k; } + int ascii() const { return a; } + ButtonState state() const { return ButtonState(s); } + ButtonState stateAfter() const; + bool isAccepted() const { return accpt; } + TQString text() const { return txt; } + bool isAutoRepeat() const { return autor; } + int count() const { return int(c); } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } + +protected: + TQString txt; + ushort k, s; + uchar a; + uint accpt:1; + uint autor:1; + ushort c; +}; + + +class Q_EXPORT TQFocusEvent : public TQEvent +{ +public: + + TQFocusEvent( Type type ) + : TQEvent(type) {} + + bool gotFocus() const { return type() == FocusIn; } + bool lostFocus() const { return type() == FocusOut; } + + enum Reason { Mouse, Tab, Backtab, ActiveWindow, Popup, Shortcut, Other }; + static Reason reason(); + static void setReason( Reason reason ); + static void resetReason(); + +private: + static Reason m_reason; + static Reason prev_reason; +}; + + +class Q_EXPORT TQPaintEvent : public TQEvent +{ +public: + TQPaintEvent( const TQRegion& paintRegion, bool erased = TRUE) + : TQEvent(Paint), + rec(paintRegion.boundingRect()), + reg(paintRegion), + erase(erased){} + TQPaintEvent( const TQRect &paintRect, bool erased = TRUE ) + : TQEvent(Paint), + rec(paintRect), + reg(paintRect), + erase(erased){} + TQPaintEvent( const TQRegion &paintRegion, const TQRect &paintRect, bool erased = TRUE ) + : TQEvent(Paint), + rec(paintRect), + reg(paintRegion), + erase(erased){} + + const TQRect &rect() const { return rec; } + const TQRegion ®ion() const { return reg; } + bool erased() const { return erase; } +protected: + friend class TQApplication; + friend class TQBaseApplication; + TQRect rec; + TQRegion reg; + bool erase; +}; + + +class Q_EXPORT TQMoveEvent : public TQEvent +{ +public: + TQMoveEvent( const TQPoint &pos, const TQPoint &oldPos ) + : TQEvent(Move), p(pos), oldp(oldPos) {} + const TQPoint &pos() const { return p; } + const TQPoint &oldPos()const { return oldp;} +protected: + TQPoint p, oldp; + friend class TQApplication; + friend class TQBaseApplication; +}; + + +class Q_EXPORT TQResizeEvent : public TQEvent +{ +public: + TQResizeEvent( const TQSize &size, const TQSize &oldSize ) + : TQEvent(Resize), s(size), olds(oldSize) {} + const TQSize &size() const { return s; } + const TQSize &oldSize()const { return olds;} +protected: + TQSize s, olds; + friend class TQApplication; + friend class TQBaseApplication; +}; + + +class Q_EXPORT TQCloseEvent : public TQEvent +{ +public: + TQCloseEvent() + : TQEvent(Close), accpt(FALSE) {} + bool isAccepted() const { return accpt; } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } +protected: + bool accpt; +}; + + +class Q_EXPORT TQIconDragEvent : public TQEvent +{ +public: + TQIconDragEvent() + : TQEvent(IconDrag), accpt(FALSE) {} + + bool isAccepted() const { return accpt; } + void accept() { accpt = TRUE; } + void ignore() { accpt = FALSE; } +protected: + bool accpt; +}; + +class Q_EXPORT TQShowEvent : public TQEvent +{ +public: + TQShowEvent() + : TQEvent(Show) {} +}; + + +class Q_EXPORT TQHideEvent : public TQEvent +{ +public: + TQHideEvent() + : TQEvent(Hide) {} +}; + +class Q_EXPORT TQContextMenuEvent : public TQEvent +{ +public: + enum Reason { Mouse, Keyboard, Other }; + TQContextMenuEvent( Reason reason, const TQPoint &pos, const TQPoint &globalPos, int state ) + : TQEvent( ContextMenu ), p( pos ), gp( globalPos ), accpt( TRUE ), consum( TRUE ), + reas( reason ), s((ushort)state) {} + TQContextMenuEvent( Reason reason, const TQPoint &pos, int state ); + + int x() const { return p.x(); } + int y() const { return p.y(); } + int globalX() const { return gp.x(); } + int globalY() const { return gp.y(); } + + const TQPoint& pos() const { return p; } + const TQPoint& globalPos() const { return gp; } + + ButtonState state() const { return (ButtonState) s; } + bool isAccepted() const { return accpt; } + bool isConsumed() const { return consum; } + void consume() { accpt = FALSE; consum = TRUE; } + void accept() { accpt = TRUE; consum = TRUE; } + void ignore() { accpt = FALSE; consum = FALSE; } + + Reason reason() const { return Reason( reas ); } + +protected: + TQPoint p; + TQPoint gp; + bool accpt; + bool consum; + uint reas:8; + ushort s; +}; + + +class Q_EXPORT TQIMEvent : public TQEvent +{ +public: + TQIMEvent( Type type, const TQString &text, int cursorPosition ) + : TQEvent(type), txt(text), cpos(cursorPosition), a(TRUE) {} + const TQString &text() const { return txt; } + int cursorPos() const { return cpos; } + bool isAccepted() const { return a; } + void accept() { a = TRUE; } + void ignore() { a = FALSE; } + int selectionLength() const; + +private: + TQString txt; + int cpos; + bool a; +}; + +class Q_EXPORT TQIMComposeEvent : public TQIMEvent +{ +public: + TQIMComposeEvent( Type type, const TQString &text, int cursorPosition, + int selLength ) + : TQIMEvent( type, text, cursorPosition ), selLen( selLength ) { } + +private: + int selLen; + + friend class TQIMEvent; +}; + +inline int TQIMEvent::selectionLength() const +{ + if ( type() != IMCompose ) return 0; + TQIMComposeEvent *that = (TQIMComposeEvent *) this; + return that->selLen; +} + + +#ifndef QT_NO_DRAGANDDROP + +// This class is rather closed at the moment. If you need to create your +// own DND event objects, write to qt-bugs@trolltech.com and we'll try to +// find a way to extend it so it covers your needs. + +class Q_EXPORT TQDropEvent : public TQEvent, public TQMimeSource +{ +public: + TQDropEvent( const TQPoint& pos, Type typ=Drop ) + : TQEvent(typ), p(pos), + act(0), accpt(0), accptact(0), resv(0), + d(0) + {} + const TQPoint &pos() const { return p; } + bool isAccepted() const { return accpt || accptact; } + void accept(bool y=TRUE) { accpt = y; } + void ignore() { accpt = FALSE; } + + bool isActionAccepted() const { return accptact; } + void acceptAction(bool y=TRUE) { accptact = y; } + enum Action { Copy, Link, Move, Private, UserAction=100 }; + void setAction( Action a ) { act = (uint)a; } + Action action() const { return Action(act); } + + TQWidget* source() const; + const char* format( int n = 0 ) const; + TQByteArray encodedData( const char* ) const; + bool provides( const char* ) const; + + TQByteArray data(const char* f) const { return encodedData(f); } + + void setPoint( const TQPoint& np ) { p = np; } + +protected: + TQPoint p; + uint act:8; + uint accpt:1; + uint accptact:1; + uint resv:5; + void * d; +}; + + + +class Q_EXPORT TQDragMoveEvent : public TQDropEvent +{ +public: + TQDragMoveEvent( const TQPoint& pos, Type typ=DragMove ) + : TQDropEvent(pos,typ), + rect( pos, TQSize( 1, 1 ) ) {} + TQRect answerRect() const { return rect; } + void accept( bool y=TRUE ) { TQDropEvent::accept(y); } + void accept( const TQRect & r) { accpt = TRUE; rect = r; } + void ignore( const TQRect & r) { accpt =FALSE; rect = r; } + void ignore() { TQDropEvent::ignore(); } + +protected: + TQRect rect; +}; + + +class Q_EXPORT TQDragEnterEvent : public TQDragMoveEvent +{ +public: + TQDragEnterEvent( const TQPoint& pos ) : + TQDragMoveEvent(pos, DragEnter) { } +}; + + +/* An internal class */ +class Q_EXPORT TQDragResponseEvent : public TQEvent +{ +public: + TQDragResponseEvent( bool accepted ) + : TQEvent(DragResponse), a(accepted) {} + bool dragAccepted() const { return a; } +protected: + bool a; +}; + + +class Q_EXPORT TQDragLeaveEvent : public TQEvent +{ +public: + TQDragLeaveEvent() + : TQEvent(DragLeave) {} +}; + +#endif // QT_NO_DRAGANDDROP + +class Q_EXPORT TQChildEvent : public TQEvent +{ +public: + TQChildEvent( Type type, TQObject *child ) + : TQEvent(type), c(child) {} + TQObject *child() const { return c; } + bool inserted() const { return t == ChildInserted; } + bool removed() const { return t == ChildRemoved; } +protected: + TQObject *c; +}; + + +class Q_EXPORT TQCustomEvent : public TQEvent +{ +public: + TQCustomEvent( int type ); + TQCustomEvent( Type type, void *data ) + : TQEvent(type), d(data) {}; + void *data() const { return d; } + void setData( void* data ) { d = data; } +private: + void *d; +}; + +#endif // TQEVENT_H diff --git a/src/kernel/qeventloop.cpp b/src/kernel/qeventloop.cpp new file mode 100644 index 000000000..480cbf54a --- /dev/null +++ b/src/kernel/qeventloop.cpp @@ -0,0 +1,394 @@ +/**************************************************************************** +** +** Implementation of TQEventLoop class +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qeventloop_p.h" // includes qplatformdefs.h +#include "qeventloop.h" +#include "qapplication.h" +#include "qdatetime.h" + +/*! + \class TQEventLoop + \brief The TQEventLoop class manages the event queue. + + \ingroup application + \ingroup events + + It receives events from the window system and other sources. It + then sends them to TQApplication for processing and delivery. + + TQEventLoop allows the application programmer to have more control + over event delivery. Programs that perform long operations can + call either processOneEvent() or processEvents() with various + ProcessEvent values OR'ed together to control which events should + be delivered. + + TQEventLoop also allows the integration of an external event loop + with the TQt event loop. The Motif Extension included with TQt + includes a reimplementation of TQEventLoop for merging TQt and Motif + events together. + + To use your own instance of TQEventLoop or TQEventLoop subclass create + it before you create the TQApplication object. +*/ + +/*! \enum TQEventLoop::ProcessEvents + + This enum controls the types of events processed by the + processEvents() functions. + + \value AllEvents - All events are processed + \value ExcludeUserInput - Do not process user input events. + ( ButtonPress, KeyPress, etc. ) + \value ExcludeSocketNotifiers - Do not process socket notifier + events. + \value WaitForMore - Wait for events if no pending events + are available. + + \sa processEvents() +*/ + +/*! \enum TQEventLoop::ProcessEventsFlags + A \c typedef to allow various ProcessEvents values to be OR'ed together. + + \sa ProcessEvents + */ + +/*! + Creates a TQEventLoop object, this object becomes the global event loop object. + There can only be one event loop object. The TQEventLoop is usually constructed + by calling TQApplication::eventLoop(). To create your own event loop object create + it before you instantiate the TQApplication object. + + The \a parent and \a name arguments are passed on to the TQObject constructor. +*/ +TQEventLoop::TQEventLoop( TQObject *parent, const char *name ) + : TQObject( parent, name ) +{ +#if defined(QT_CHECK_STATE) + if ( TQApplication::eventloop ) + qFatal( "TQEventLoop: there must be only one event loop object. \nConstruct it before TQApplication." ); + // for now ;) +#endif // QT_CHECK_STATE + + d = new TQEventLoopPrivate; + + init(); + TQApplication::eventloop = this; +} + +/*! + Destructs the TQEventLoop object. +*/ +TQEventLoop::~TQEventLoop() +{ + cleanup(); + delete d; + TQApplication::eventloop = 0; +} + +/*! + Enters the main event loop and waits until exit() is called, and + returns the value that was set to exit(). + + It is necessary to call this function to start event handling. The + main event loop receives events from the window system and + dispatches these to the application widgets. + + Generally speaking, no user interaction can take place before + calling exec(). As a special case, modal widgets like TQMessageBox + can be used before calling exec(), because modal widgets call + exec() to start a local event loop. + + To make your application perform idle processing, i.e. executing a + special function whenever there are no pending events, use a + TQTimer with 0 timeout. More advanced idle processing schemes can + be achieved using processEvents(). + + \sa TQApplication::tquit(), exit(), processEvents() +*/ +int TQEventLoop::exec() +{ + d->reset(); + + enterLoop(); + + // cleanup + d->looplevel = 0; + d->tquitnow = FALSE; + d->exitloop = FALSE; + d->shortcut = FALSE; + // don't reset tquitcode! + + return d->tquitcode; +} + +/*! \fn void TQEventLoop::exit( int retcode = 0 ) + + Tells the event loop to exit with a return code. + + After this function has been called, the event loop returns from + the call to exec(). The exec() function returns \a retcode. + + By convention, a \a retcode of 0 means success, and any non-zero + value indicates an error. + + Note that unlike the C library function of the same name, this + function \e does return to the caller -- it is event processing that + stops. + + \sa TQApplication::tquit(), exec() +*/ +void TQEventLoop::exit( int retcode ) +{ + if ( d->tquitnow ) // preserve existing tquitcode + return; + d->tquitcode = retcode; + d->tquitnow = TRUE; + d->exitloop = TRUE; + d->shortcut = TRUE; +} + + +/*! \fn int TQEventLoop::enterLoop() + + This function enters the main event loop (recursively). Do not call + it unless you really know what you are doing. + */ +int TQEventLoop::enterLoop() +{ + // save the current exitloop state + bool old_exitloop = d->exitloop; + d->exitloop = FALSE; + d->shortcut = FALSE; + + d->looplevel++; + while ( ! d->exitloop ) + processEvents( AllEvents | WaitForMore ); + d->looplevel--; + + // restore the exitloop state, but if tquitnow is TRUE, we need to keep + // exitloop set so that all other event loops drop out. + d->exitloop = old_exitloop || d->tquitnow; + d->shortcut = d->tquitnow; + + if ( d->looplevel < 1 ) { + d->tquitnow = FALSE; + d->exitloop = FALSE; + d->shortcut = FALSE; + emit qApp->aboutToQuit(); + + // send deferred deletes + TQApplication::sendPostedEvents( 0, TQEvent::DeferredDelete ); + } + + return d->looplevel; +} + +/*! \fn void TQEventLoop::exitLoop() + + This function exits from a recursive call to the main event loop. + Do not call it unless you really know what you are doing. +*/ +void TQEventLoop::exitLoop() +{ + d->exitloop = TRUE; + d->shortcut = TRUE; +} + +/*! \fn void TQEventLoop::loopLevel() const + + Returns the current loop level. +*/ +int TQEventLoop::loopLevel() const +{ + return d->looplevel; +} + +/*! + Process pending events that match \a flags for a maximum of \a + maxTime milliseconds, or until there are no more events to + process, which ever is shorter. + + This function is especially useful if you have a long running + operation and want to show its progress without allowing user + input, i.e. by using the \c ExcludeUserInput flag. + + NOTE: This function will not process events continuously; it + returns after all available events are processed. + + NOTE: Specifying the \c WaitForMore flag makes no sense and will + be ignored. +*/ +void TQEventLoop::processEvents( ProcessEventsFlags flags, int maxTime ) +{ + TQTime start = TQTime::currentTime(); + TQTime now; + while ( ! d->tquitnow && processEvents( flags & ~WaitForMore ) ) { + now = TQTime::currentTime(); + if ( start.msecsTo( now ) > maxTime ) + break; + } +} + +/*! + \fn bool TQEventLoop::processEvents( ProcessEventsFlags flags ) + \overload + + Processes pending events that match \a flags until there are no + more events to process. + + This function is especially useful if you have a long running + operation and want to show its progress without allowing user + input, i.e. by using the \c ExcludeUserInput flag. + + If the \c WaitForMore flag is set in \a flags, the behavior of + this function is as follows: + + \list + + \i If events are available, this function returns after processing + them. + + \i If no events are available, this function will wait until more + are available and return after processing newly available events. + + \endlist + + If the \c WaitForMore flag is \e not set in \a flags, and no + events are available, this function will return immediately. + + NOTE: This function will not process events continuously; it + returns after all available events are processed. + + This function returns TRUE if an event was processed; otherwise it + returns FALSE. + + \sa ProcessEvents hasPendingEvents() +*/ + +/*! \fn bool TQEventLoop::hasPendingEvents() const + + Returns TRUE if there is an event waiting, otherwise it returns FALSE. +*/ + +/*! \fn void TQEventLoop::registerSocketNotifier( TQSocketNotifier *notifier ) + + Registers \a notifier with the event loop. Subclasses need to + reimplement this method to tie a socket notifier into another + event loop. Reimplementations \e MUST call the base + implementation. +*/ + +/*! \fn void TQEventLoop::unregisterSocketNotifier( TQSocketNotifier *notifier ) + + Unregisters \a notifier from the event loop. Subclasses need to + reimplement this method to tie a socket notifier into another + event loop. Reimplementations \e MUST call the base + implementation. +*/ + +/*! \fn void TQEventLoop::setSocketNotifierPending( TQSocketNotifier *notifier ) + + Marks \a notifier as pending. The socket notifier will be + activated the next time activateSocketNotifiers() is called. +*/ + +/*! \fn int TQEventLoop::activateSocketNotifiers() + + Activates all pending socket notifiers and returns the number of + socket notifiers that were activated. +*/ + +/*! \fn int TQEventLoop::activateTimers() + + Activates all TQt timers and returns the number of timers that were + activated. + + TQEventLoop subclasses that do their own timer handling need to + call this after the time returned by timeToWait() has elapsed. + + Note: This function is only useful on systems where \c select() is + used to block the eventloop. On Windows, this function always + returns 0. On MacOS X, this function always returns 0 when the + GUI is enabled. On MacOS X, this function returns the documented + value when the GUI is disabled. +*/ + +/*! \fn int TQEventLoop::timeToWait() const + + Returns the number of milliseconds that TQt needs to handle its + timers or -1 if there are no timers running. + + TQEventLoop subclasses that do their own timer handling need to use + this to make sure that TQt's timers continue to work. + + Note: This function is only useful on systems where \c select() is + used to block the eventloop. On Windows, this function always + returns -1. On MacOS X, this function always returns -1 when the + GUI is enabled. On MacOS X, this function returns the documented + value when the GUI is disabled. +*/ + +/*! \fn void TQEventLoop::wakeUp() + \threadsafe + + Wakes up the event loop. + + \sa awake() +*/ + +/*! \fn void TQEventLoop::awake() + + This signal is emitted after the event loop returns from a + function that could block. + + \sa wakeUp() aboutToBlock() +*/ + +/*! \fn void TQEventLoop::aboutToBlock() + + This signal is emitted before the event loop calls a function that + could block. + + \sa awake() +*/ + +#if !defined(Q_WS_X11) +void TQEventLoop::appStartingUp(){} +void TQEventLoop::appClosingDown(){} +#endif // Q_WS_X11 diff --git a/src/kernel/qeventloop.h b/src/kernel/qeventloop.h new file mode 100644 index 000000000..9b47295d5 --- /dev/null +++ b/src/kernel/qeventloop.h @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Declaration of TQEventLoop class +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQEVENTLOOP_H +#define TQEVENTLOOP_H + +#ifndef QT_H +#include "qobject.h" +#include "qsocketnotifier.h" +#endif // QT_H + +class TQEventLoopPrivate; +class TQSocketNotifier; +class TQTimer; +#ifdef Q_WS_MAC +struct timeval; //stdc struct +struct TimerInfo; //internal structure (qeventloop_mac.cpp) +#endif + +#if defined(QT_THREAD_SUPPORT) +class TQMutex; +#endif // QT_THREAD_SUPPORT + + +class Q_EXPORT TQEventLoop : public TQObject +{ + Q_OBJECT + +public: + TQEventLoop( TQObject *parent = 0, const char *name = 0 ); + ~TQEventLoop(); + + enum ProcessEvents { + AllEvents = 0x00, + ExcludeUserInput = 0x01, + ExcludeSocketNotifiers = 0x02, + WaitForMore = 0x04 + }; + typedef uint ProcessEventsFlags; + + void processEvents( ProcessEventsFlags flags, int maxtime ); + virtual bool processEvents( ProcessEventsFlags flags ); + + virtual bool hasPendingEvents() const; + + virtual void registerSocketNotifier( TQSocketNotifier * ); + virtual void unregisterSocketNotifier( TQSocketNotifier * ); + void setSocketNotifierPending( TQSocketNotifier * ); + int activateSocketNotifiers(); + + int activateTimers(); + int timeToWait() const; + + virtual int exec(); + virtual void exit( int retcode = 0 ); + + virtual int enterLoop(); + virtual void exitLoop(); + virtual int loopLevel() const; + + virtual void wakeUp(); + +signals: + void awake(); + void aboutToBlock(); + +private: +#if defined(Q_WS_MAC) + friend TQMAC_PASCAL void qt_mac_select_timer_callbk(EventLoopTimerRef, void *); + int macHandleSelect(timeval *); + void macHandleTimer(TimerInfo *); +#endif // Q_WS_MAC + + // internal initialization/cleanup - implemented in various platform specific files + void init(); + void cleanup(); + virtual void appStartingUp(); + virtual void appClosingDown(); + + // data for the default implementation - other implementations should not + // use/need this data + TQEventLoopPrivate *d; + + friend class TQApplication; +}; + +#endif // TQEVENTLOOP_H diff --git a/src/kernel/qeventloop_p.h b/src/kernel/qeventloop_p.h new file mode 100644 index 000000000..d70bb0efd --- /dev/null +++ b/src/kernel/qeventloop_p.h @@ -0,0 +1,150 @@ +/**************************************************************************** +** +** Definition of TQEventLoop class +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** Licensees holding valid TQt Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQEVENTLOOP_P_H +#define TQEVENTLOOP_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. This header file may +// change from version to version without notice, or even be +// removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qplatformdefs.h" +#endif // QT_H + +// SCO OpenServer redefines raise -> kill +#if defined(raise) +# undef raise +#endif + +#include "qwindowdefs.h" + +class TQSocketNotifier; +#ifdef Q_OS_MAC +class TQMacSockNotPrivate; +#endif + +#if defined(Q_OS_UNIX) || defined (Q_WS_WIN) +#include "qptrlist.h" +#endif // Q_OS_UNIX || Q_WS_WIN + +#if defined(Q_OS_UNIX) +struct TQSockNot +{ + TQSocketNotifier *obj; + int fd; + fd_set *queue; +}; + +class TQSockNotType +{ +public: + TQSockNotType(); + ~TQSockNotType(); + + TQPtrList *list; + fd_set select_fds; + fd_set enabled_fds; + fd_set pending_fds; + +}; +#endif // Q_OS_UNIX + +#if defined(Q_WS_WIN) +struct TQSockNot { + TQSocketNotifier *obj; + int fd; +}; +#endif // Q_WS_WIN + +class TQEventLoopPrivate +{ +public: + TQEventLoopPrivate() + { + reset(); + } + + void reset() { + looplevel = 0; + tquitcode = 0; + tquitnow = FALSE; + exitloop = FALSE; + shortcut = FALSE; + } + + int looplevel; + int tquitcode; + unsigned int tquitnow : 1; + unsigned int exitloop : 1; + unsigned int shortcut : 1; + +#if defined(Q_WS_MAC) + uchar next_select_timer; + EventLoopTimerRef select_timer; +#endif + +#if defined(Q_WS_X11) + int xfd; +#endif // Q_WS_X11 + +#if defined(Q_OS_UNIX) + int thread_pipe[2]; + + // pending socket notifiers list + TQPtrList sn_pending_list; + // highest fd for all socket notifiers + int sn_highest; + // 3 socket notifier types - read, write and exception + TQSockNotType sn_vec[3]; +#endif + +#ifdef Q_WS_WIN + // pending socket notifiers list + TQPtrList sn_pending_list; +#endif // Q_WS_WIN + +}; + +#endif // TQEVENTLOOP_P_H diff --git a/src/kernel/qeventloop_unix.cpp b/src/kernel/qeventloop_unix.cpp new file mode 100644 index 000000000..ee4dc3b55 --- /dev/null +++ b/src/kernel/qeventloop_unix.cpp @@ -0,0 +1,587 @@ +/**************************************************************************** +** +** Implementation of TQEventLoop class +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qeventloop_p.h" // includes qplatformdefs.h +#include "qeventloop.h" +#include "qapplication.h" +#include "qbitarray.h" +#include +#include + + +/***************************************************************************** + Timer handling; UNIX has no application timer support so we'll have to + make our own from scratch. + + NOTE: These functions are for internal use. TQObject::startTimer() and + TQObject::killTimer() are for public use. + The TQTimer class provides a high-level interface which translates + timer events into signals. + + qStartTimer( interval, obj ) + Starts a timer which will run until it is killed with qKillTimer() + Arguments: + int interval timer interval in milliseconds + TQObject *obj where to send the timer event + Returns: + int timer identifier, or zero if not successful + + qKillTimer( timerId ) + Stops a timer specified by a timer identifier. + Arguments: + int timerId timer identifier + Returns: + bool TRUE if successful + + qKillTimer( obj ) + Stops all timers that are sent to the specified object. + Arguments: + TQObject *obj object receiving timer events + Returns: + bool TRUE if successful + *****************************************************************************/ + +// +// Internal data structure for timers +// + +struct TimerInfo { // internal timer info + int id; // - timer identifier + timeval interval; // - timer interval + timeval timeout; // - when to sent event + TQObject *obj; // - object to receive event +}; + +typedef TQPtrList TimerList; // list of TimerInfo structs + +static TQBitArray *timerBitVec; // timer bit vector +static TimerList *timerList = 0; // timer list + +static void initTimers(); +void cleanupTimers(); +static timeval watchtime; // watch if time is turned back +timeval *qt_wait_timer(); +timeval *qt_wait_timer_max = 0; + +// +// Internal operator functions for timevals +// + +static inline bool operator<( const timeval &t1, const timeval &t2 ) +{ + return t1.tv_sec < t2.tv_sec || + (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec); +} + +static inline bool operator==( const timeval &t1, const timeval &t2 ) +{ + return t1.tv_sec == t2.tv_sec && t1.tv_usec == t2.tv_usec; +} + +static inline timeval &operator+=( timeval &t1, const timeval &t2 ) +{ + t1.tv_sec += t2.tv_sec; + if ( (t1.tv_usec += t2.tv_usec) >= 1000000 ) { + t1.tv_sec++; + t1.tv_usec -= 1000000; + } + return t1; +} + +static inline timeval operator+( const timeval &t1, const timeval &t2 ) +{ + timeval tmp; + tmp.tv_sec = t1.tv_sec + t2.tv_sec; + if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 ) { + tmp.tv_sec++; + tmp.tv_usec -= 1000000; + } + return tmp; +} + +static inline timeval operator-( const timeval &t1, const timeval &t2 ) +{ + timeval tmp; + tmp.tv_sec = t1.tv_sec - t2.tv_sec; + if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 ) { + tmp.tv_sec--; + tmp.tv_usec += 1000000; + } + return tmp; +} + + +// +// Internal functions for manipulating timer data structures. +// The timerBitVec array is used for keeping track of timer identifiers. +// + +static int allocTimerId() // find avail timer identifier +{ + int i = timerBitVec->size()-1; + while ( i >= 0 && (*timerBitVec)[i] ) + i--; + if ( i < 0 ) { + i = timerBitVec->size(); + timerBitVec->resize( 4 * i ); + for( int j=timerBitVec->size()-1; j > i; j-- ) + timerBitVec->clearBit( j ); + } + timerBitVec->setBit( i ); + return i+1; +} + +static void insertTimer( const TimerInfo *ti ) // insert timer info into list +{ + TimerInfo *t = timerList->first(); + int index = 0; +#if defined(QT_DEBUG) + int dangerCount = 0; +#endif + while ( t && t->timeout < ti->timeout ) { // list is sorted by timeout +#if defined(QT_DEBUG) + if ( t->obj == ti->obj ) + dangerCount++; +#endif + t = timerList->next(); + index++; + } + timerList->insert( index, ti ); // inserts sorted +#if defined(QT_DEBUG) + if ( dangerCount > 16 ) + qDebug( "TQObject: %d timers now exist for object %s::%s", + dangerCount, ti->obj->className(), ti->obj->name() ); +#endif +} + +static inline void getTime( timeval &t ) // get time of day +{ + gettimeofday( &t, 0 ); + while ( t.tv_usec >= 1000000 ) { // NTP-related fix + t.tv_usec -= 1000000; + t.tv_sec++; + } + while ( t.tv_usec < 0 ) { + if ( t.tv_sec > 0 ) { + t.tv_usec += 1000000; + t.tv_sec--; + } else { + t.tv_usec = 0; + break; + } + } +} + +static void repairTimer( const timeval &time ) // repair broken timer +{ + timeval diff = watchtime - time; + register TimerInfo *t = timerList->first(); + while ( t ) { // repair all timers + t->timeout = t->timeout - diff; + t = timerList->next(); + } +} + +// +// Timer activation functions (called from the event loop) +// + +/* + Returns the time to wait for the next timer, or null if no timers are + waiting. + + The result is bounded to qt_wait_timer_max if this exists. +*/ + +timeval *qt_wait_timer() +{ + static timeval tm; + bool first = TRUE; + timeval currentTime; + if ( timerList && timerList->count() ) { // there are waiting timers + getTime( currentTime ); + if ( first ) { + if ( currentTime < watchtime ) // clock was turned back + repairTimer( currentTime ); + first = FALSE; + watchtime = currentTime; + } + TimerInfo *t = timerList->first(); // first waiting timer + if ( currentTime < t->timeout ) { // time to wait + tm = t->timeout - currentTime; + } else { + tm.tv_sec = 0; // no time to wait + tm.tv_usec = 0; + } + if ( qt_wait_timer_max && *qt_wait_timer_max < tm ) + tm = *qt_wait_timer_max; + return &tm; + } + if ( qt_wait_timer_max ) { + tm = *qt_wait_timer_max; + return &tm; + } + return 0; // no timers +} + +// Timer initialization +static void initTimers() // initialize timers +{ + timerBitVec = new TQBitArray( 128 ); + Q_CHECK_PTR( timerBitVec ); + int i = timerBitVec->size(); + while( i-- > 0 ) + timerBitVec->clearBit( i ); + timerList = new TimerList; + Q_CHECK_PTR( timerList ); + timerList->setAutoDelete( TRUE ); + gettimeofday( &watchtime, 0 ); +} + +// Timer cleanup +void cleanupTimers() +{ + delete timerList; + timerList = 0; + delete timerBitVec; + timerBitVec = 0; +} + +// Main timer functions for starting and killing timers +int qStartTimer( int interval, TQObject *obj ) +{ + if ( !timerList ) // initialize timer data + initTimers(); + int id = allocTimerId(); // get free timer id + if ( id <= 0 || + id > (int)timerBitVec->size() || !obj )// cannot create timer + return 0; + timerBitVec->setBit( id-1 ); // set timer active + TimerInfo *t = new TimerInfo; // create timer + Q_CHECK_PTR( t ); + t->id = id; + t->interval.tv_sec = interval/1000; + t->interval.tv_usec = (interval%1000)*1000; + timeval currentTime; + getTime( currentTime ); + t->timeout = currentTime + t->interval; + t->obj = obj; + insertTimer( t ); // put timer in list + return id; +} + +bool qKillTimer( int id ) +{ + register TimerInfo *t; + if ( !timerList || id <= 0 || + id > (int)timerBitVec->size() || !timerBitVec->testBit( id-1 ) ) + return FALSE; // not init'd or invalid timer + t = timerList->first(); + while ( t && t->id != id ) // find timer info in list + t = timerList->next(); + if ( t ) { // id found + timerBitVec->clearBit( id-1 ); // set timer inactive + return timerList->remove(); + } + else // id not found + return FALSE; +} + +bool qKillTimer( TQObject *obj ) +{ + register TimerInfo *t; + if ( !timerList ) // not initialized + return FALSE; + t = timerList->first(); + while ( t ) { // check all timers + if ( t->obj == obj ) { // object found + timerBitVec->clearBit( t->id-1 ); + timerList->remove(); + t = timerList->current(); + } else { + t = timerList->next(); + } + } + return TRUE; +} + +/***************************************************************************** + Socket notifier type + *****************************************************************************/ +TQSockNotType::TQSockNotType() + : list( 0 ) +{ + FD_ZERO( &select_fds ); + FD_ZERO( &enabled_fds ); + FD_ZERO( &pending_fds ); +} + +TQSockNotType::~TQSockNotType() +{ + if ( list ) + delete list; + list = 0; +} + +/***************************************************************************** + TQEventLoop implementations for UNIX + *****************************************************************************/ +void TQEventLoop::registerSocketNotifier( TQSocketNotifier *notifier ) +{ + int sockfd = notifier->socket(); + int type = notifier->type(); + if ( sockfd < 0 || sockfd >= FD_SETSIZE || type < 0 || type > 2 || notifier == 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQSocketNotifier: Internal error" ); +#endif + return; + } + + TQPtrList *list = d->sn_vec[type].list; + fd_set *fds = &d->sn_vec[type].enabled_fds; + TQSockNot *sn; + + if ( ! list ) { + // create new list, the TQSockNotType destructor will delete it for us + list = new TQPtrList; + Q_CHECK_PTR( list ); + list->setAutoDelete( TRUE ); + d->sn_vec[type].list = list; + } + + sn = new TQSockNot; + Q_CHECK_PTR( sn ); + sn->obj = notifier; + sn->fd = sockfd; + sn->queue = &d->sn_vec[type].pending_fds; + + if ( list->isEmpty() ) { + list->insert( 0, sn ); + } else { // sort list by fd, decreasing + TQSockNot *p = list->first(); + while ( p && p->fd > sockfd ) + p = list->next(); +#if defined(QT_CHECK_STATE) + if ( p && p->fd == sockfd ) { + static const char *t[] = { "read", "write", "exception" }; + qWarning( "TQSocketNotifier: Multiple socket notifiers for " + "same socket %d and type %s", sockfd, t[type] ); + } +#endif + if ( p ) + list->insert( list->at(), sn ); + else + list->append( sn ); + } + + FD_SET( sockfd, fds ); + d->sn_highest = TQMAX( d->sn_highest, sockfd ); +} + +void TQEventLoop::unregisterSocketNotifier( TQSocketNotifier *notifier ) +{ + int sockfd = notifier->socket(); + int type = notifier->type(); + if ( sockfd < 0 || type < 0 || type > 2 || notifier == 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQSocketNotifier: Internal error" ); +#endif + return; + } + + TQPtrList *list = d->sn_vec[type].list; + fd_set *fds = &d->sn_vec[type].enabled_fds; + TQSockNot *sn; + if ( ! list ) + return; + sn = list->first(); + while ( sn && !(sn->obj == notifier && sn->fd == sockfd) ) + sn = list->next(); + if ( !sn ) // not found + return; + + FD_CLR( sockfd, fds ); // clear fd bit + FD_CLR( sockfd, sn->queue ); + d->sn_pending_list.removeRef( sn ); // remove from activation list + list->remove(); // remove notifier found above + + if ( d->sn_highest == sockfd ) { // find highest fd + d->sn_highest = -1; + for ( int i=0; i<3; i++ ) { + if ( d->sn_vec[i].list && ! d->sn_vec[i].list->isEmpty() ) + d->sn_highest = TQMAX( d->sn_highest, // list is fd-sorted + d->sn_vec[i].list->getFirst()->fd ); + } + } +} + +void TQEventLoop::setSocketNotifierPending( TQSocketNotifier *notifier ) +{ + int sockfd = notifier->socket(); + int type = notifier->type(); + if ( sockfd < 0 || type < 0 || type > 2 || notifier == 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQSocketNotifier: Internal error" ); +#endif + return; + } + + TQPtrList *list = d->sn_vec[type].list; + TQSockNot *sn; + if ( ! list ) + return; + sn = list->first(); + while ( sn && !(sn->obj == notifier && sn->fd == sockfd) ) + sn = list->next(); + if ( ! sn ) { // not found + return; + } + + // We choose a random activation order to be more fair under high load. + // If a constant order is used and a peer early in the list can + // saturate the IO, it might grab our attention completely. + // Also, if we're using a straight list, the callback routines may + // delete other entries from the list before those other entries are + // processed. + if ( ! FD_ISSET( sn->fd, sn->queue ) ) { + d->sn_pending_list.insert( (rand() & 0xff) % + (d->sn_pending_list.count()+1), sn ); + FD_SET( sn->fd, sn->queue ); + } +} + +void TQEventLoop::wakeUp() +{ + /* + Apparently, there is not consistency among different operating + systems on how to use FIONREAD. + + FreeBSD, Linux and Solaris all expect the 3rd argument to + ioctl() to be an int, which is normally 32-bit even on 64-bit + machines. + + IRIX, on the other hand, expects a size_t, which is 64-bit on + 64-bit machines. + + So, the solution is to use size_t initialized to zero to make + sure all bits are set to zero, preventing underflow with the + FreeBSD/Linux/Solaris ioctls. + */ + size_t nbytes = 0; + char c = 0; + if ( ::ioctl( d->thread_pipe[0], FIONREAD, (char*)&nbytes ) >= 0 && nbytes == 0 ) { + ::write( d->thread_pipe[1], &c, 1 ); + } +} + +int TQEventLoop::timeToWait() const +{ + timeval *tm = qt_wait_timer(); + if ( ! tm ) // no active timers + return -1; + return (tm->tv_sec*1000) + (tm->tv_usec/1000); +} + +int TQEventLoop::activateTimers() +{ + if ( !timerList || !timerList->count() ) // no timers + return 0; + bool first = TRUE; + timeval currentTime; + int n_act = 0, maxCount = timerList->count(); + TimerInfo *begin = 0; + register TimerInfo *t; + + for ( ;; ) { + if ( ! maxCount-- ) + break; + getTime( currentTime ); // get current time + if ( first ) { + if ( currentTime < watchtime ) // clock was turned back + repairTimer( currentTime ); + first = FALSE; + watchtime = currentTime; + } + t = timerList->first(); + if ( !t || currentTime < t->timeout ) // no timer has expired + break; + if ( ! begin ) { + begin = t; + } else if ( begin == t ) { + // avoid sending the same timer multiple times + break; + } else if ( t->interval < begin->interval || t->interval == begin->interval ) { + begin = t; + } + timerList->take(); // unlink from list + t->timeout += t->interval; + if ( t->timeout < currentTime ) + t->timeout = currentTime + t->interval; + insertTimer( t ); // relink timer + if ( t->interval.tv_usec > 0 || t->interval.tv_sec > 0 ) + n_act++; + TQTimerEvent e( t->id ); + TQApplication::sendEvent( t->obj, &e ); // send event + if ( timerList->findRef( begin ) == -1 ) + begin = 0; + } + return n_act; +} + +int TQEventLoop::activateSocketNotifiers() +{ + if ( d->sn_pending_list.isEmpty() ) + return 0; + + // activate entries + int n_act = 0; + TQEvent event( TQEvent::SockAct ); + TQPtrListIterator it( d->sn_pending_list ); + TQSockNot *sn; + while ( (sn=it.current()) ) { + ++it; + d->sn_pending_list.removeRef( sn ); + if ( FD_ISSET(sn->fd, sn->queue) ) { + FD_CLR( sn->fd, sn->queue ); + TQApplication::sendEvent( sn->obj, &event ); + n_act++; + } + } + + return n_act; +} diff --git a/src/kernel/qeventloop_x11.cpp b/src/kernel/qeventloop_x11.cpp new file mode 100644 index 000000000..76d2e62e0 --- /dev/null +++ b/src/kernel/qeventloop_x11.cpp @@ -0,0 +1,417 @@ +/**************************************************************************** +** +** Implementation of TQEventLoop class +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qeventloop_p.h" // includes qplatformdefs.h +#include "qeventloop.h" +#include "qapplication.h" +#include "qbitarray.h" +#include "qcolor_p.h" +#include "qt_x11_p.h" + +#if defined(QT_THREAD_SUPPORT) +# include "qmutex.h" +#endif // QT_THREAD_SUPPORT + +#include + + +// resolve the conflict between X11's FocusIn and TQEvent::FocusIn +#undef FocusOut +#undef FocusIn + +static const int XKeyPress = KeyPress; +static const int XKeyRelease = KeyRelease; +#undef KeyPress +#undef KeyRelease + +// from qapplication.cpp +extern bool qt_is_gui_used; + +// from qeventloop_unix.cpp +extern timeval *qt_wait_timer(); +extern void cleanupTimers(); + +// ### this needs to go away at some point... +typedef void (*VFPTR)(); +typedef TQValueList TQVFuncList; +void qt_install_preselect_handler( VFPTR ); +void qt_remove_preselect_handler( VFPTR ); +static TQVFuncList *qt_preselect_handler = 0; +void qt_install_postselect_handler( VFPTR ); +void qt_remove_postselect_handler( VFPTR ); +static TQVFuncList *qt_postselect_handler = 0; + +void qt_install_preselect_handler( VFPTR handler ) +{ + if ( !qt_preselect_handler ) + qt_preselect_handler = new TQVFuncList; + qt_preselect_handler->append( handler ); +} +void qt_remove_preselect_handler( VFPTR handler ) +{ + if ( qt_preselect_handler ) { + TQVFuncList::Iterator it = qt_preselect_handler->find( handler ); + if ( it != qt_preselect_handler->end() ) + qt_preselect_handler->remove( it ); + } +} +void qt_install_postselect_handler( VFPTR handler ) +{ + if ( !qt_postselect_handler ) + qt_postselect_handler = new TQVFuncList; + qt_postselect_handler->prepend( handler ); +} +void qt_remove_postselect_handler( VFPTR handler ) +{ + if ( qt_postselect_handler ) { + TQVFuncList::Iterator it = qt_postselect_handler->find( handler ); + if ( it != qt_postselect_handler->end() ) + qt_postselect_handler->remove( it ); + } +} + + +void TQEventLoop::init() +{ + // initialize the common parts of the event loop + pipe( d->thread_pipe ); + fcntl(d->thread_pipe[0], F_SETFD, FD_CLOEXEC); + fcntl(d->thread_pipe[1], F_SETFD, FD_CLOEXEC); + + d->sn_highest = -1; + + // intitialize the X11 parts of the event loop + d->xfd = -1; + if ( qt_is_gui_used ) + d->xfd = XConnectionNumber( TQPaintDevice::x11AppDisplay() ); +} + +void TQEventLoop::cleanup() +{ + // cleanup the common parts of the event loop + close( d->thread_pipe[0] ); + close( d->thread_pipe[1] ); + cleanupTimers(); + + // cleanup the X11 parts of the event loop + d->xfd = -1; +} + +bool TQEventLoop::processEvents( ProcessEventsFlags flags ) +{ + // process events from the X server + XEvent event; + int nevents = 0; + +#if defined(QT_THREAD_SUPPORT) + TQMutexLocker locker( TQApplication::qt_mutex ); +#endif + + // handle gui and posted events + if ( qt_is_gui_used ) { + TQApplication::sendPostedEvents(); + + // Two loops so that posted events accumulate + while ( XPending( TQPaintDevice::x11AppDisplay() ) ) { + // also flushes output buffer + while ( XPending( TQPaintDevice::x11AppDisplay() ) ) { + if ( d->shortcut ) { + return FALSE; + } + + XNextEvent( TQPaintDevice::x11AppDisplay(), &event ); + + if ( flags & ExcludeUserInput ) { + switch ( event.type ) { + case ButtonPress: + case ButtonRelease: + case MotionNotify: + case XKeyPress: + case XKeyRelease: + case EnterNotify: + case LeaveNotify: + continue; + + case ClientMessage: + { + // from qapplication_x11.cpp + extern Atom qt_wm_protocols; + extern Atom qt_wm_take_focus; + extern Atom qt_qt_scrolldone; + + // only keep the wm_take_focus and + // qt_qt_scrolldone protocols, discard all + // other client messages + if ( event.xclient.format != 32 ) + continue; + + if ( event.xclient.message_type == qt_wm_protocols || + (Atom) event.xclient.data.l[0] == qt_wm_take_focus ) + break; + if ( event.xclient.message_type == qt_qt_scrolldone ) + break; + } + + default: break; + } + } + + nevents++; + if ( qApp->x11ProcessEvent( &event ) == 1 ) + return TRUE; + } + } + } + + if ( d->shortcut ) { + return FALSE; + } + + TQApplication::sendPostedEvents(); + + const uint exclude_all = ExcludeSocketNotifiers | 0x08; + // 0x08 == ExcludeTimers for X11 only + if ( nevents > 0 && ( flags & exclude_all ) == exclude_all && + ( flags & WaitForMore ) ) { + return TRUE; + } + + // don't block if exitLoop() or exit()/tquit() has been called. + bool canWait = d->exitloop || d->tquitnow ? FALSE : (flags & WaitForMore); + + // Process timers and socket notifiers - the common UNIX stuff + + // return the maximum time we can wait for an event. + static timeval zerotm; + timeval *tm = 0; + if ( ! ( flags & 0x08 ) ) { // 0x08 == ExcludeTimers for X11 only + tm = qt_wait_timer(); // wait for timer or X event + if ( !canWait ) { + if ( !tm ) + tm = &zerotm; + tm->tv_sec = 0; // no time to wait + tm->tv_usec = 0; + } + } + + int highest = 0; + if ( ! ( flags & ExcludeSocketNotifiers ) ) { + // return the highest fd we can wait for input on + if ( d->sn_highest >= 0 ) { // has socket notifier(s) + if ( d->sn_vec[0].list && ! d->sn_vec[0].list->isEmpty() ) + d->sn_vec[0].select_fds = d->sn_vec[0].enabled_fds; + else + FD_ZERO( &d->sn_vec[0].select_fds ); + + if ( d->sn_vec[1].list && ! d->sn_vec[1].list->isEmpty() ) + d->sn_vec[1].select_fds = d->sn_vec[1].enabled_fds; + else + FD_ZERO( &d->sn_vec[1].select_fds ); + + if ( d->sn_vec[2].list && ! d->sn_vec[2].list->isEmpty() ) + d->sn_vec[2].select_fds = d->sn_vec[2].enabled_fds; + else + FD_ZERO( &d->sn_vec[2].select_fds ); + } else { + FD_ZERO( &d->sn_vec[0].select_fds ); + + FD_ZERO( &d->sn_vec[1].select_fds ); + FD_ZERO( &d->sn_vec[2].select_fds ); + } + + highest = d->sn_highest; + } else { + FD_ZERO( &d->sn_vec[0].select_fds ); + FD_ZERO( &d->sn_vec[1].select_fds ); + FD_ZERO( &d->sn_vec[2].select_fds ); + } + + if ( qt_is_gui_used ) { + // select for events on the event socket - only on X11 + FD_SET( d->xfd, &d->sn_vec[0].select_fds ); + highest = TQMAX( highest, d->xfd ); + } + + FD_SET( d->thread_pipe[0], &d->sn_vec[0].select_fds ); + highest = TQMAX( highest, d->thread_pipe[0] ); + + if ( canWait ) + emit aboutToBlock(); + + if ( qt_preselect_handler ) { + TQVFuncList::Iterator it, end = qt_preselect_handler->end(); + for ( it = qt_preselect_handler->begin(); it != end; ++it ) + (**it)(); + } + + // unlock the GUI mutex and select. when we return from this function, there is + // something for us to do +#if defined(QT_THREAD_SUPPORT) + locker.mutex()->unlock(); +#endif + + int nsel; + do { + nsel = select( highest + 1, + &d->sn_vec[0].select_fds, + &d->sn_vec[1].select_fds, + &d->sn_vec[2].select_fds, + tm ); + } while (nsel == -1 && (errno == EINTR || errno == EAGAIN)); + + // relock the GUI mutex before processing any pending events +#if defined(QT_THREAD_SUPPORT) + locker.mutex()->lock(); +#endif + + // we are awake, broadcast it + emit awake(); + emit qApp->guiThreadAwake(); + + if (nsel == -1) { + if (errno == EBADF) { + // it seems a socket notifier has a bad fd... find out + // which one it is and disable it + fd_set fdset; + zerotm.tv_sec = zerotm.tv_usec = 0l; + + for (int type = 0; type < 3; ++type) { + TQPtrList *list = d->sn_vec[type].list; + if (!list) continue; + + TQSockNot *sn = list->first(); + while (sn) { + FD_ZERO(&fdset); + FD_SET(sn->fd, &fdset); + + int ret = -1; + do { + switch (type) { + case 0: // read + ret = select(sn->fd + 1, &fdset, 0, 0, &zerotm); + break; + case 1: // write + ret = select(sn->fd + 1, 0, &fdset, 0, &zerotm); + break; + case 2: // except + ret = select(sn->fd + 1, 0, 0, &fdset, &zerotm); + break; + } + } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); + + if (ret == -1 && errno == EBADF) { + // disable the invalid socket notifier + static const char *t[] = { "Read", "Write", "Exception" }; + qWarning("TQSocketNotifier: invalid socket %d and type '%s', disabling...", + sn->fd, t[type]); + sn->obj->setEnabled(FALSE); + } + + sn = list->next(); + } + } + } else { + // EINVAL... shouldn't happen, so let's complain to stderr + // and hope someone sends us a bug report + perror( "select" ); + } + } + + // some other thread woke us up... consume the data on the thread pipe so that + // select doesn't immediately return next time + if ( nsel > 0 && FD_ISSET( d->thread_pipe[0], &d->sn_vec[0].select_fds ) ) { + char c; + ::read( d->thread_pipe[0], &c, 1 ); + } + + if ( qt_postselect_handler ) { + TQVFuncList::Iterator it, end = qt_postselect_handler->end(); + for ( it = qt_postselect_handler->begin(); it != end; ++it ) + (**it)(); + } + + // activate socket notifiers + if ( ! ( flags & ExcludeSocketNotifiers ) && nsel > 0 && d->sn_highest >= 0 ) { + // if select says data is ready on any socket, then set the socket notifier + // to pending + int i; + for ( i=0; i<3; i++ ) { + if ( ! d->sn_vec[i].list ) + continue; + + TQPtrList *list = d->sn_vec[i].list; + TQSockNot *sn = list->first(); + while ( sn ) { + if ( FD_ISSET( sn->fd, &d->sn_vec[i].select_fds ) ) + setSocketNotifierPending( sn->obj ); + sn = list->next(); + } + } + + nevents += activateSocketNotifiers(); + } + + // activate timers + if ( ! ( flags & 0x08 ) ) { + // 0x08 == ExcludeTimers for X11 only + nevents += activateTimers(); + } + + // color approx. optimization - only on X11 + qt_reset_color_avail(); + + // return true if we handled events, false otherwise + return (nevents > 0); +} + +bool TQEventLoop::hasPendingEvents() const +{ + extern uint qGlobalPostedEventsCount(); // from qapplication.cpp + return ( qGlobalPostedEventsCount() || XPending( TQPaintDevice::x11AppDisplay() ) ); +} + +void TQEventLoop::appStartingUp() +{ + if ( qt_is_gui_used ) + d->xfd = XConnectionNumber( TQPaintDevice::x11AppDisplay() ); +} + +void TQEventLoop::appClosingDown() +{ + d->xfd = -1; +} diff --git a/src/kernel/qfocusdata.cpp b/src/kernel/qfocusdata.cpp new file mode 100644 index 000000000..6c5c02880 --- /dev/null +++ b/src/kernel/qfocusdata.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Implementation of TQFocusData class +** +** Created : 980622 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qfocusdata.h" + +/*! + \class TQFocusData qfocusdata.h + \brief The TQFocusData class maintains the list of widgets in the focus + chain. + + \ingroup misc + + This read-only list always contains at least one widget (i.e. the + top-level widget). It provides a simple cursor which can be reset + to the current focus widget using home(), or moved to its + neighboring widgets using next() and prev(). You can also retrieve + the count() of the number of widgets in the list. The list is a + loop, so if you keep iterating, for example using next(), you will + never come to the end. + + Some widgets in the list may not accept focus. Widgets are added + to the list as necessary, but not removed from it. This lets + widgets change focus policy dynamically without disrupting the + focus chain the user experiences. When a widget disables and + re-enables tab focus, its position in the focus chain does not + change. + + When reimplementing TQWidget::focusNextPrevChild() to provide + special focus flow, you will usually call TQWidget::focusData() to + retrieve the focus data stored at the top-level widget. A + top-level widget's focus data contains the focus list for its + hierarchy of widgets. + + The cursor may change at any time. + + This class is \e not thread-safe. + + \sa TQWidget::focusNextPrevChild() TQWidget::setTabOrder() + TQWidget::setFocusPolicy() +*/ + +/*! + \fn TQWidget* TQFocusData::focusWidget() const + + Returns the widgets in the hierarchy that are in the focus chain. +*/ + +/*! + \fn int TQFocusData::count() const + + Returns the number of widgets in the focus chain. +*/ + +/*! + Moves the cursor to the focusWidget() and returns that widget. You + must call this before next() or prev() to iterate meaningfully. +*/ +TQWidget* TQFocusData::home() +{ + focusWidgets.find(it.current()); + return focusWidgets.current(); +} + +/*! + Moves the cursor to the next widget in the focus chain. There is + \e always a next widget because the list is a loop. +*/ +TQWidget* TQFocusData::next() +{ + TQWidget* r = focusWidgets.next(); + if ( !r ) + r = focusWidgets.first(); + return r; +} + +/*! + Moves the cursor to the previous widget in the focus chain. There + is \e always a previous widget because the list is a loop. +*/ +TQWidget* TQFocusData::prev() +{ + TQWidget* r = focusWidgets.prev(); + if ( !r ) + r = focusWidgets.last(); + return r; +} + +/*! + Returns the last widget in the focus chain. + The cursor is not modified. +*/ +TQWidget *TQFocusData::last() const +{ + return focusWidgets.getLast(); +} + +/*! + Returns the first widget in the focus chain. + The cursor is not modified. +*/ +TQWidget *TQFocusData::first() const +{ + return focusWidgets.getFirst(); +} diff --git a/src/kernel/qfocusdata.h b/src/kernel/qfocusdata.h new file mode 100644 index 000000000..53f003126 --- /dev/null +++ b/src/kernel/qfocusdata.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Definition of internal TQFocusData class +** +** Created : 980405 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQFOCUSDATA_H +#define TQFOCUSDATA_H + +#ifndef QT_H +#include "qwidgetlist.h" +#endif // QT_H + + +class Q_EXPORT TQFocusData { +public: + TQWidget* focusWidget() const { return it.current(); } + + TQWidget* home(); + TQWidget* next(); + TQWidget* prev(); + TQWidget* first() const; + TQWidget* last() const; + int count() const { return focusWidgets.count(); } + +private: + friend class TQWidget; + + TQFocusData() + : it(focusWidgets) {} + TQWidgetList focusWidgets; + TQWidgetListIt it; +}; + + +#endif // TQFOCUSDATA_H diff --git a/src/kernel/qfont.cpp b/src/kernel/qfont.cpp new file mode 100644 index 000000000..671e56852 --- /dev/null +++ b/src/kernel/qfont.cpp @@ -0,0 +1,3356 @@ +/**************************************************************************** +** +** Implementation of TQFont, TQFontMetrics and TQFontInfo classes +** +** Created : 941207 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#define QT_FATAL_ASSERT + +#include "qfont.h" +#include "qfontdatabase.h" +#include "qfontmetrics.h" +#include "qfontinfo.h" +#include "qpainter.h" +#include "qdict.h" +#include "qcache.h" +#include "qdatastream.h" +#include "qapplication.h" +#include "qcleanuphandler.h" +#include "qstringlist.h" +#ifdef Q_WS_MAC +#include "qpaintdevicemetrics.h" +#endif + +#include +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include "qpainter_p.h" +#include "qtextengine_p.h" + +// #define TQFONTCACHE_DEBUG +#ifdef TQFONTCACHE_DEBUG +# define FC_DEBUG qDebug +#else +# define FC_DEBUG if (FALSE) qDebug +#endif + + + + +bool TQFontDef::operator==( const TQFontDef &other ) const +{ + /* + TQFontDef comparison is more complicated than just simple + per-member comparisons. + + When comparing point/pixel sizes, either point or pixelsize + could be -1. in This case we have to compare the non negative + size value. + + This test will fail if the point-sizes differ by 1/2 point or + more or they do not round to the same value. We have to do this + since our API still uses 'int' point-sizes in the API, but store + deci-point-sizes internally. + + To compare the family members, we need to parse the font names + and compare the family/foundry strings separately. This allows + us to compare e.g. "Helvetica" and "Helvetica [Adobe]" with + positive results. + */ + if (pixelSize != -1 && other.pixelSize != -1) { + if (pixelSize != other.pixelSize) + return FALSE; + } else if (pointSize != -1 && other.pointSize != -1) { + if (pointSize != other.pointSize + && (TQABS(pointSize - other.pointSize) >= 5 + || qRound(pointSize/10.) != qRound(other.pointSize/10.))) + return FALSE; + } else { + return FALSE; + } + + if (!ignorePitch && !other.ignorePitch && fixedPitch != other.fixedPitch) + return FALSE; + + if (stretch != 0 && other.stretch != 0 && stretch != other.stretch) + return FALSE; + + TQString this_family, this_foundry, other_family, other_foundry; + TQFontDatabase::parseFontName(family, this_foundry, this_family); + TQFontDatabase::parseFontName(other.family, other_foundry, other_family); + + return ( styleHint == other.styleHint + && styleStrategy == other.styleStrategy + && weight == other.weight + && italic == other.italic + && this_family == other_family + && (this_foundry.isEmpty() + || other_foundry.isEmpty() + || this_foundry == other_foundry) +#ifdef Q_WS_X11 + && addStyle == other.addStyle +#endif // Q_WS_X11 + ); +} + + + + +TQFontPrivate::TQFontPrivate() + : engineData( 0 ), paintdevice( 0 ), + rawMode( FALSE ), underline( FALSE ), overline( FALSE ), strikeOut( FALSE ), + mask( 0 ) +{ +#ifdef Q_WS_X11 + screen = TQPaintDevice::x11AppScreen(); +#else + screen = 0; +#endif // Q_WS_X11 +} + +TQFontPrivate::TQFontPrivate( const TQFontPrivate &other ) + : TQShared(), request( other.request ), engineData( 0 ), + paintdevice( other.paintdevice ), screen( other.screen ), + rawMode( other.rawMode ), underline( other.underline ), overline( other.overline ), + strikeOut( other.strikeOut ), mask( other.mask ) +{ +} + +TQFontPrivate::~TQFontPrivate() +{ + if ( engineData ) + engineData->deref(); + engineData = 0; +} + +void TQFontPrivate::resolve( const TQFontPrivate *other ) +{ +#ifdef QT_CHECK_STATE + Q_ASSERT( other != 0 ); +#endif + + if ( ( mask & Complete ) == Complete ) return; + + // assign the unset-bits with the set-bits of the other font def + if ( ! ( mask & Family ) ) + request.family = other->request.family; + + if ( ! ( mask & Size ) ) { + request.pointSize = other->request.pointSize; + request.pixelSize = other->request.pixelSize; + } + + if ( ! ( mask & StyleHint ) ) + request.styleHint = other->request.styleHint; + + if ( ! ( mask & StyleStrategy ) ) + request.styleStrategy = other->request.styleStrategy; + + if ( ! ( mask & Weight ) ) + request.weight = other->request.weight; + + if ( ! ( mask & Italic ) ) + request.italic = other->request.italic; + + if ( ! ( mask & FixedPitch ) ) + request.fixedPitch = other->request.fixedPitch; + + if ( ! ( mask & Stretch ) ) + request.stretch = other->request.stretch; + + if ( ! ( mask & Underline ) ) + underline = other->underline; + + if ( ! ( mask & Overline ) ) + overline = other->overline; + + if ( ! ( mask & StrikeOut ) ) + strikeOut = other->strikeOut; +} + + + + +TQFontEngineData::TQFontEngineData() + : lineWidth( 1 ) +{ +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + memset( engines, 0, TQFont::LastPrivateScript * sizeof( TQFontEngine * ) ); +#else + engine = 0; +#endif // Q_WS_X11 || Q_WS_WIN +#ifndef Q_WS_MAC + memset( widthCache, 0, widthCacheSize*sizeof( uchar ) ); +#endif +} + +TQFontEngineData::~TQFontEngineData() +{ +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + for ( int i = 0; i < TQFont::LastPrivateScript; i++ ) { + if ( engines[i] ) + engines[i]->deref(); + engines[i] = 0; + } +#else + if ( engine ) + engine->deref(); + engine = 0; +#endif // Q_WS_X11 || Q_WS_WIN +} + + + + +/*! + \class TQFont qfont.h + \brief The TQFont class specifies a font used for drawing text. + + \ingroup graphics + \ingroup appearance + \ingroup shared + \mainclass + + When you create a TQFont object you specify various attributes that + you want the font to have. TQt will use the font with the specified + attributes, or if no matching font exists, TQt will use the closest + matching installed font. The attributes of the font that is + actually used are retrievable from a TQFontInfo object. If the + window system provides an exact match exactMatch() returns TRUE. + Use TQFontMetrics to get measurements, e.g. the pixel length of a + string using TQFontMetrics::width(). + + Use TQApplication::setFont() to set the application's default font. + + If a choosen X11 font does not include all the characters that + need to be displayed, TQFont will try to find the characters in the + nearest equivalent fonts. When a TQPainter draws a character from a + font the TQFont will report whether or not it has the character; if + it does not, TQPainter will draw an unfilled square. + + Create TQFonts like this: + \code + TQFont serifFont( "Times", 10, Bold ); + TQFont sansFont( "Helvetica [Cronyx]", 12 ); + \endcode + + The attributes set in the constructor can also be set later, e.g. + setFamily(), setPointSize(), setPointSizeFloat(), setWeight() and + setItalic(). The remaining attributes must be set after + contstruction, e.g. setBold(), setUnderline(), setOverline(), + setStrikeOut() and setFixedPitch(). TQFontInfo objects should be + created \e after the font's attributes have been set. A TQFontInfo + object will not change, even if you change the font's + attributes. The corresponding "get" functions, e.g. family(), + pointSize(), etc., return the values that were set, even though + the values used may differ. The actual values are available from a + TQFontInfo object. + + If the requested font family is unavailable you can influence the + \link #fontmatching font matching algorithm\endlink by choosing a + particular \l{TQFont::StyleHint} and \l{TQFont::StyleStrategy} with + setStyleHint(). The default family (corresponding to the current + style hint) is returned by defaultFamily(). + + The font-matching algorithm has a lastResortFamily() and + lastResortFont() in cases where a suitable match cannot be found. + You can provide substitutions for font family names using + insertSubstitution() and insertSubstitutions(). Substitutions can + be removed with removeSubstitution(). Use substitute() to retrieve + a family's first substitute, or the family name itself if it has + no substitutes. Use substitutes() to retrieve a list of a family's + substitutes (which may be empty). + + Every TQFont has a key() which you can use, for example, as the key + in a cache or dictionary. If you want to store a user's font + preferences you could use TQSettings, writing the font information + with toString() and reading it back with fromString(). The + operator<<() and operator>>() functions are also available, but + they work on a data stream. + + It is possible to set the height of characters shown on the screen + to a specified number of pixels with setPixelSize(); however using + setPointSize() has a similar effect and provides device + independence. + + Under the X Window System you can set a font using its system + specific name with setRawName(). + + Loading fonts can be expensive, especially on X11. TQFont contains + extensive optimizations to make the copying of TQFont objects fast, + and to cache the results of the slow window system functions it + depends upon. + + \target fontmatching + The font matching algorithm works as follows: + \list 1 + \i The specified font family is searched for. + \i If not found, the styleHint() is used to select a replacement + family. + \i Each replacement font family is searched for. + \i If none of these are found or there was no styleHint(), "helvetica" + will be searched for. + \i If "helvetica" isn't found TQt will try the lastResortFamily(). + \i If the lastResortFamily() isn't found TQt will try the + lastResortFont() which will always return a name of some kind. + \endlist + + Once a font is found, the remaining attributes are matched in order of + priority: + \list 1 + \i fixedPitch() + \i pointSize() (see below) + \i weight() + \i italic() + \endlist + + If you have a font which matches on family, even if none of the + other attributes match, this font will be chosen in preference to + a font which doesn't match on family but which does match on the + other attributes. This is because font family is the dominant + search criteria. + + The point size is defined to match if it is within 20% of the + requested point size. When several fonts match and are only + distinguished by point size, the font with the closest point size + to the one requested will be chosen. + + The actual family, font size, weight and other font attributes + used for drawing text will depend on what's available for the + chosen family under the window system. A TQFontInfo object can be + used to determine the actual values used for drawing the text. + + Examples: + + \code + TQFont f("Helvetica"); + \endcode + If you had both an Adobe and a Cronyx Helvetica, you might get + either. + + \code + TQFont f1( "Helvetica [Cronyx]" ); // TQt 3.x + TQFont f2( "Cronyx-Helvetica" ); // TQt 2.x compatibility + \endcode + You can specify the foundry you want in the family name. Both fonts, + f1 and f2, in the above example will be set to "Helvetica + [Cronyx]". + + To determine the attributes of the font actually used in the window + system, use a TQFontInfo object, e.g. + \code + TQFontInfo info( f1 ); + TQString family = info.family(); + \endcode + + To find out font metrics use a TQFontMetrics object, e.g. + \code + TQFontMetrics fm( f1 ); + int pixelWidth = fm.width( "How many pixels wide is this text?" ); + int pixelHeight = fm.height(); + \endcode + + For more general information on fonts, see the + \link http://www.nwalsh.com/comp.fonts/FAQ/ comp.fonts FAQ.\endlink + Information on encodings can be found from + \link http://czyborra.com/ Roman Czyborra's\endlink page. + + \sa TQFontMetrics TQFontInfo TQFontDatabase TQApplication::setFont() + TQWidget::setFont() TQPainter::setFont() TQFont::StyleHint + TQFont::Weight +*/ + +/*! + \enum TQFont::Script + + This enum represents \link unicode.html Unicode \endlink allocated + scripts. For exhaustive coverage see \link + http://www.amazon.com/exec/obidos/ASIN/0201616335/trolltech/t The + Unicode Standard Version 3.0 \endlink. The following scripts are + supported: + + Modern European alphabetic scripts (left to right): + + \value Latin consists of most alphabets based on the original Latin alphabet. + \value Greek covers ancient and modern Greek and Coptic. + \value Cyrillic covers the Slavic and non-Slavic languages using + cyrillic alphabets. + \value Armenian contains the Armenian alphabet used with the + Armenian language. + \value Georgian covers at least the language Georgian. + \value Runic covers the known constituents of the Runic alphabets used + by the early and medieval societies in the Germanic, + Scandinavian, and Anglo-Saxon areas. + \value Ogham is an alphabetical script used to write a very early + form of Irish. + \value SpacingModifiers are small signs indicating modifications + to the preceeding letter. + \value CombiningMarks consist of diacritical marks not specific to + a particular alphabet, diacritical marks used in + combination with mathematical and technical symbols, and + glyph encodings applied to multiple letterforms. + + Middle Eastern scripts (right to left): + + \value Hebrew is used for writing Hebrew, Yiddish, and some other languages. + \value Arabic covers the Arabic language as well as Persian, Urdu, + Kurdish and some others. + \value Syriac is used to write the active liturgical languages and + dialects of several Middle Eastern and Southeast Indian + communities. + \value Thaana is used to write the Maledivian Dhivehi language. + + South and Southeast Asian scripts (left to right with few historical exceptions): + + \value Devanagari covers classical Sanskrit and modern Hindi as + well as several other languages. + \value Bengali is a relative to Devanagari employed to write the + Bengali language used in West Bengal/India and Bangladesh + as well as several minority languages. + \value Gurmukhi is another Devanagari relative used to write Punjabi. + \value Gujarati is closely related to Devanagari and used to write + the Gujarati language of the Gujarat state in India. + \value Oriya is used to write the Oriya language of Orissa state/India. + \value Tamil is used to write the Tamil language of Tamil Nadu state/India, + Sri Lanka, Singapore and parts of Malaysia as well as some + minority languages. + \value Telugu is used to write the Telugu language of Andhra + Pradesh state/India and some minority languages. + \value Kannada is another South Indian script used to write the + Kannada language of Karnataka state/India and some minority + languages. + \value Malayalam is used to write the Malayalam language of Kerala + state/India. + \value Sinhala is used for Sri Lanka's majority language Sinhala + and is also employed to write Pali, Sanskrit, and Tamil. + \value Thai is used to write Thai and other Southeast Asian languages. + \value Lao is a language and script tquite similar to Thai. + \value Tibetan is the script used to write Tibetan in several + countries like Tibet, the bordering Indian regions and + Nepal. It is also used in the Buddist philosophy and + liturgy of the Mongolian cultural area. + \value Myanmar is mainly used to write the Burmese language of + Myanmar (former Burma). + \value Khmer is the official language of Kampuchea. + + East Asian scripts (traditionally top-down, right to left, modern + often horizontal left to right): + + \value Han consists of the CJK (Chinese, Japanese, Korean) + idiographic characters. + \value Hiragana is a cursive syllabary used to indicate phonetics + and pronounciation of Japanese words. + \value Katakana is a non-cursive syllabic script used to write + Japanese words with visual emphasis and non-Japanese words + in a phonetical manner. + \value Hangul is a Korean script consisting of alphabetic components. + \value Bopomofo is a phonetic alphabet for Chinese (mainly Mandarin). + \value Yi (also called Cuan or Wei) is a syllabary used to write + the Yi language of Southwestern China, Myanmar, Laos, and Vietnam. + + Additional scripts that do not fit well into the script categories above: + + \value Ethiopic is a syllabary used by several Central East African languages. + \value Cherokee is a left-to-right syllabic script used to write + the Cherokee language. + \value CanadianAboriginal consists of the syllabics used by some + Canadian aboriginal societies. + \value Mongolian is the traditional (and recently reintroduced) + script used to write Mongolian. + + Symbols: + + \value CurrencySymbols contains currency symbols not encoded in other scripts. + \value LetterlikeSymbols consists of symbols derived from + ordinary letters of an alphabetical script. + \value NumberForms are provided for compatibility with other + existing character sets. + \value MathematicalOperators consists of encodings for operators, + relations and other symbols like arrows used in a mathematical context. + \value TechnicalSymbols contains representations for control + codes, the space symbol, APL symbols and other symbols + mainly used in the context of electronic data processing. + \value GeometricSymbols covers block elements and geometric shapes. + \value MiscellaneousSymbols consists of a heterogeneous collection + of symbols that do not fit any other Unicode character + block, e.g. Dingbats. + \value EnclosedAndSquare is provided for compatibility with some + East Asian standards. + \value Braille is an international writing system used by blind + people. This script encodes the 256 eight-dot patterns with + the 64 six-dot patterns as a subset. + + \value Tagalog + \value Hanunoo + \value Buhid + \value Tagbanwa + + \value KatakanaHalfWidth + + \value Limbu (Unicode 4.0) + \value TaiLe (Unicode 4.0) + + \value Unicode includes all the above scripts. +*/ + +/*! \internal + + Constructs a font for use on the paint device \a pd using the + specified font \a data. +*/ +TQFont::TQFont( TQFontPrivate *data, TQPaintDevice *pd ) +{ + d = new TQFontPrivate( *data ); + Q_CHECK_PTR( d ); + d->paintdevice = pd; + + // now a single reference + d->count = 1; +} + +/*! \internal + Detaches the font object from common font data. +*/ +void TQFont::detach() +{ + if (d->count == 1) { + if ( d->engineData ) + d->engineData->deref(); + d->engineData = 0; + + return; + } + + TQFontPrivate *old_d = d; + d = new TQFontPrivate( *old_d ); + + /* + if this font is a copy of the application default font, set the + fontdef mask to zero to indicate that *nothing* has been + explicitly set by the programmer. + */ + const TQFont appfont = TQApplication::font(); + if ( old_d == appfont.d ) + d->mask = 0; + + if ( old_d->deref() ) + delete old_d; +} + +/*! + Constructs a font object that uses the application's default font. + + \sa TQApplication::setFont(), TQApplication::font() +*/ +TQFont::TQFont() +{ + const TQFont appfont = TQApplication::font(); + d = appfont.d; + d->ref(); +} + +/*! + Constructs a font object with the specified \a family, \a + pointSize, \a weight and \a italic settings. + + If \a pointSize is <= 0 it is set to 1. + + The \a family name may optionally also include a foundry name, + e.g. "Helvetica [Cronyx]". (The TQt 2.x syntax, i.e. + "Cronyx-Helvetica", is also supported.) If the \a family is + available from more than one foundry and the foundry isn't + specified, an arbitrary foundry is chosen. If the family isn't + available a family will be set using the \link #fontmatching font + matching\endlink algorithm. + + \sa Weight, setFamily(), setPointSize(), setWeight(), setItalic(), + setStyleHint() TQApplication::font() +*/ +TQFont::TQFont( const TQString &family, int pointSize, int weight, bool italic ) +{ + + d = new TQFontPrivate; + Q_CHECK_PTR( d ); + + d->mask = TQFontPrivate::Family; + + if (pointSize <= 0) { + pointSize = 12; + } else { + d->mask |= TQFontPrivate::Size; + } + + if (weight < 0) { + weight = Normal; + } else { + d->mask |= TQFontPrivate::Weight | TQFontPrivate::Italic; + } + + d->request.family = family; + d->request.pointSize = pointSize * 10; + d->request.pixelSize = -1; + d->request.weight = weight; + d->request.italic = italic; +} + +/*! + Constructs a font that is a copy of \a font. +*/ +TQFont::TQFont( const TQFont &font ) +{ + d = font.d; + d->ref(); +} + +/*! + Destroys the font object and frees all allocated resources. +*/ +TQFont::~TQFont() +{ + if ( d->deref() ) + delete d; + d = 0; +} + +/*! + Assigns \a font to this font and returns a reference to it. +*/ +TQFont &TQFont::operator=( const TQFont &font ) +{ + if ( font.d != d ) { + if ( d->deref() ) + delete d; + d = font.d; + d->ref(); + } + + return *this; +} + +/*! + Returns the requested font family name, i.e. the name set in the + constructor or the last setFont() call. + + \sa setFamily() substitutes() substitute() +*/ +TQString TQFont::family() const +{ + return d->request.family; +} + +/*! + Sets the family name of the font. The name is case insensitive and + may include a foundry name. + + The \a family name may optionally also include a foundry name, + e.g. "Helvetica [Cronyx]". (The TQt 2.x syntax, i.e. + "Cronyx-Helvetica", is also supported.) If the \a family is + available from more than one foundry and the foundry isn't + specified, an arbitrary foundry is chosen. If the family isn't + available a family will be set using the \link #fontmatching font + matching\endlink algorithm. + + \sa family(), setStyleHint(), TQFontInfo +*/ +void TQFont::setFamily( const TQString &family ) +{ + detach(); + + d->request.family = family; +#if defined(Q_WS_X11) + d->request.addStyle = TQString::null; +#endif // Q_WS_X11 + + d->mask |= TQFontPrivate::Family; +} + +/*! + Returns the point size in 1/10ths of a point. + + The returned value will be -1 if the font size has been specified + in pixels. + + \sa pointSize() pointSizeFloat() + */ +int TQFont::deciPointSize() const +{ + return d->request.pointSize; +} + +/*! + Returns the point size of the font. Returns -1 if the font size + was specified in pixels. + + \sa setPointSize() deciPointSize() pointSizeFloat() +*/ +int TQFont::pointSize() const +{ + return d->request.pointSize == -1 ? -1 : (d->request.pointSize + 5) / 10; +} + +/*! + Sets the point size to \a pointSize. The point size must be + greater than zero. + + \sa pointSize() setPointSizeFloat() +*/ +void TQFont::setPointSize( int pointSize ) +{ + if ( pointSize <= 0 ) { + +#if defined(QT_CHECK_RANGE) + qWarning( "TQFont::setPointSize: Point size <= 0 (%d)", pointSize ); +#endif + + return; + } + + detach(); + + d->request.pointSize = pointSize * 10; + d->request.pixelSize = -1; + + d->mask |= TQFontPrivate::Size; +} + +/*! + Sets the point size to \a pointSize. The point size must be + greater than zero. The requested precision may not be achieved on + all platforms. + + \sa pointSizeFloat() setPointSize() setPixelSize() +*/ +void TQFont::setPointSizeFloat( float pointSize ) +{ + if ( pointSize <= 0.0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQFont::setPointSize: Point size <= 0 (%f)", pointSize ); +#endif + return; + } + + detach(); + + d->request.pointSize = qRound(pointSize * 10.0); + d->request.pixelSize = -1; + + d->mask |= TQFontPrivate::Size; +} + +/*! + Returns the point size of the font. Returns -1 if the font size was + specified in pixels. + + \sa pointSize() setPointSizeFloat() pixelSize() TQFontInfo::pointSize() TQFontInfo::pixelSize() +*/ +float TQFont::pointSizeFloat() const +{ + return float( d->request.pointSize == -1 ? -10 : d->request.pointSize ) / 10.0; +} + +/*! + Sets the font size to \a pixelSize pixels. + + Using this function makes the font device dependent. Use + setPointSize() or setPointSizeFloat() to set the size of the font + in a device independent manner. + + \sa pixelSize() +*/ +void TQFont::setPixelSize( int pixelSize ) +{ + if ( pixelSize <= 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQFont::setPixelSize: Pixel size <= 0 (%d)", pixelSize ); +#endif + return; + } + + detach(); + + d->request.pixelSize = pixelSize; + d->request.pointSize = -1; + + d->mask |= TQFontPrivate::Size; +} + +/*! + Returns the pixel size of the font if it was set with + setPixelSize(). Returns -1 if the size was set with setPointSize() + or setPointSizeFloat(). + + \sa setPixelSize() pointSize() TQFontInfo::pointSize() TQFontInfo::pixelSize() +*/ +int TQFont::pixelSize() const +{ + return d->request.pixelSize; +} + +/*! \obsolete + + Sets the logical pixel height of font characters when shown on + the screen to \a pixelSize. +*/ +void TQFont::setPixelSizeFloat( float pixelSize ) +{ + setPixelSize( (int)pixelSize ); +} + +/*! + Returns TRUE if italic has been set; otherwise returns FALSE. + + \sa setItalic() +*/ +bool TQFont::italic() const +{ + return d->request.italic; +} + +/*! + If \a enable is TRUE, italic is set on; otherwise italic is set + off. + + \sa italic(), TQFontInfo +*/ +void TQFont::setItalic( bool enable ) +{ + detach(); + + d->request.italic = enable; + d->mask |= TQFontPrivate::Italic; +} + +/*! + Returns the weight of the font which is one of the enumerated + values from \l{TQFont::Weight}. + + \sa setWeight(), Weight, TQFontInfo +*/ +int TQFont::weight() const +{ + return d->request.weight; +} + +/*! + \enum TQFont::Weight + + TQt uses a weighting scale from 0 to 99 similar to, but not the + same as, the scales used in Windows or CSS. A weight of 0 is + ultralight, whilst 99 will be an extremely black. + + This enum contains the predefined font weights: + + \value Light 25 + \value Normal 50 + \value DemiBold 63 + \value Bold 75 + \value Black 87 +*/ + +/*! + Sets the weight the font to \a weight, which should be a value + from the \l TQFont::Weight enumeration. + + \sa weight(), TQFontInfo +*/ +void TQFont::setWeight( int weight ) +{ + if ( weight < 0 || weight > 99 ) { + +#if defined(QT_CHECK_RANGE) + qWarning( "TQFont::setWeight: Value out of range (%d)", weight ); +#endif + + return; + } + + detach(); + + d->request.weight = weight; + d->mask |= TQFontPrivate::Weight; +} + +/*! + \fn bool TQFont::bold() const + + Returns TRUE if weight() is a value greater than \link Weight + TQFont::Normal \endlink; otherwise returns FALSE. + + \sa weight(), setBold(), TQFontInfo::bold() +*/ + +/*! + \fn void TQFont::setBold( bool enable ) + + If \a enable is true sets the font's weight to \link Weight + TQFont::Bold \endlink; otherwise sets the weight to \link Weight + TQFont::Normal\endlink. + + For finer boldness control use setWeight(). + + \sa bold(), setWeight() +*/ + +/*! + Returns TRUE if underline has been set; otherwise returns FALSE. + + \sa setUnderline() +*/ +bool TQFont::underline() const +{ + return d->underline; +} + +/*! + If \a enable is TRUE, sets underline on; otherwise sets underline + off. + + \sa underline(), TQFontInfo +*/ +void TQFont::setUnderline( bool enable ) +{ + detach(); + + d->underline = enable; + d->mask |= TQFontPrivate::Underline; +} + +/*! + Returns TRUE if overline has been set; otherwise returns FALSE. + + \sa setOverline() +*/ +bool TQFont::overline() const +{ + return d->overline; +} + +/*! + If \a enable is TRUE, sets overline on; otherwise sets overline off. + + \sa overline(), TQFontInfo +*/ +void TQFont::setOverline( bool enable ) +{ + detach(); + + d->overline = enable; + d->mask |= TQFontPrivate::Overline; +} + +/*! + Returns TRUE if strikeout has been set; otherwise returns FALSE. + + \sa setStrikeOut() +*/ +bool TQFont::strikeOut() const +{ + return d->strikeOut; +} + +/*! + If \a enable is TRUE, sets strikeout on; otherwise sets strikeout + off. + + \sa strikeOut(), TQFontInfo +*/ +void TQFont::setStrikeOut( bool enable ) +{ + detach(); + + d->strikeOut = enable; + d->mask |= TQFontPrivate::StrikeOut; +} + +/*! + Returns TRUE if fixed pitch has been set; otherwise returns FALSE. + + \sa setFixedPitch(), TQFontInfo::fixedPitch() +*/ +bool TQFont::fixedPitch() const +{ + return d->request.fixedPitch; +} + +/*! + If \a enable is TRUE, sets fixed pitch on; otherwise sets fixed + pitch off. + + \sa fixedPitch(), TQFontInfo +*/ +void TQFont::setFixedPitch( bool enable ) +{ + detach(); + + d->request.fixedPitch = enable; + d->request.ignorePitch = FALSE; + d->mask |= TQFontPrivate::FixedPitch; +} + +/*! + Returns the StyleStrategy. + + The style strategy affects the \link #fontmatching font + matching\endlink algorithm. See \l TQFont::StyleStrategy for the + list of strategies. + + \sa setStyleHint() TQFont::StyleHint +*/ +TQFont::StyleStrategy TQFont::styleStrategy() const +{ + return (StyleStrategy) d->request.styleStrategy; +} + +/*! + Returns the StyleHint. + + The style hint affects the \link #fontmatching font + matching\endlink algorithm. See \l TQFont::StyleHint for the list + of strategies. + + \sa setStyleHint(), TQFont::StyleStrategy TQFontInfo::styleHint() +*/ +TQFont::StyleHint TQFont::styleHint() const +{ + return (StyleHint) d->request.styleHint; +} + +/*! + \enum TQFont::StyleHint + + Style hints are used by the \link #fontmatching font + matching\endlink algorithm to find an appropriate default family + if a selected font family is not available. + + \value AnyStyle leaves the font matching algorithm to choose the + family. This is the default. + + \value SansSerif the font matcher prefer sans serif fonts. + \value Helvetica is a synonym for \c SansSerif. + + \value Serif the font matcher prefers serif fonts. + \value Times is a synonym for \c Serif. + + \value TypeWriter the font matcher prefers fixed pitch fonts. + \value Courier a synonym for \c TypeWriter. + + \value OldEnglish the font matcher prefers decorative fonts. + \value Decorative is a synonym for \c OldEnglish. + + \value System the font matcher prefers system fonts. +*/ + +/*! + \enum TQFont::StyleStrategy + + The style strategy tells the \link #fontmatching font + matching\endlink algorithm what type of fonts should be used to + find an appropriate default family. + + The following strategies are available: + + \value PreferDefault the default style strategy. It does not prefer + any type of font. + \value PreferBitmap prefers bitmap fonts (as opposed to outline + fonts). + \value PreferDevice prefers device fonts. + \value PreferOutline prefers outline fonts (as opposed to bitmap fonts). + \value ForceOutline forces the use of outline fonts. + \value NoAntialias don't antialias the fonts. + \value PreferAntialias antialias if possible. + \value OpenGLCompatible forces the use of OpenGL compatible + fonts. + + Any of these may be OR-ed with one of these flags: + + \value PreferMatch prefer an exact match. The font matcher will try to + use the exact font size that has been specified. + \value PreferQuality prefer the best quality font. The font matcher + will use the nearest standard point size that the font + supports. +*/ + +/*! + Sets the style hint and strategy to \a hint and \a strategy, + respectively. + + If these aren't set explicitly the style hint will default to + \c AnyStyle and the style strategy to \c PreferDefault. + + TQt does not support style hints on X11 since this information + is not provided by the window system. + + \sa StyleHint, styleHint(), StyleStrategy, styleStrategy(), TQFontInfo +*/ +void TQFont::setStyleHint( StyleHint hint, StyleStrategy strategy ) +{ + detach(); + + if ( ( d->mask & ( TQFontPrivate::StyleHint | TQFontPrivate::StyleStrategy ) ) && + (StyleHint) d->request.styleHint == hint && + (StyleStrategy) d->request.styleStrategy == strategy ) + return; + + d->request.styleHint = hint; + d->request.styleStrategy = strategy; + d->mask |= TQFontPrivate::StyleHint; + d->mask |= TQFontPrivate::StyleStrategy; + +#if defined(Q_WS_X11) + d->request.addStyle = TQString::null; +#endif // Q_WS_X11 +} + +/*! + Sets the style strategy for the font to \a s. + + \sa TQFont::StyleStrategy +*/ +void TQFont::setStyleStrategy( StyleStrategy s ) +{ + detach(); + + if ( ( d->mask & TQFontPrivate::StyleStrategy ) && + s == (StyleStrategy)d->request.styleStrategy ) + return; + + d->request.styleStrategy = s; + d->mask |= TQFontPrivate::StyleStrategy; +} + + +/*! + \enum TQFont::Stretch + + Predefined stretch values that follow the CSS naming convention. + + \value UltraCondensed 50 + \value ExtraCondensed 62 + \value Condensed 75 + \value SemiCondensed 87 + \value Unstretched 100 + \value SemiExpanded 112 + \value Expanded 125 + \value ExtraExpanded 150 + \value UltraExpanded 200 + + \sa setStretch() stretch() +*/ + +/*! + Returns the stretch factor for the font. + + \sa setStretch() + */ +int TQFont::stretch() const +{ + return d->request.stretch; +} + +/*! + Sets the stretch factor for the font. + + The stretch factor changes the width of all characters in the font + by \a factor percent. For example, setting \a factor to 150 + results in all characters in the font being 1.5 times ( ie. 150% ) + wider. The default stretch factor is 100. The minimum stretch + factor is 1, and the maximum stretch factor is 4000. + + The stretch factor is only applied to outline fonts. The stretch + factor is ignored for bitmap fonts. + + NOTE: TQFont cannot stretch XLFD fonts. When loading XLFD fonts on + X11, the stretch factor is matched against a predefined set of + values for the SETWIDTH_NAME field of the XLFD. + + \sa stretch() TQFont::StyleStrategy +*/ +void TQFont::setStretch( int factor ) +{ + if ( factor < 1 || factor > 4000 ) { +#ifdef QT_CHECK_RANGE + qWarning( "TQFont::setStretch(): parameter '%d' out of range", factor ); +#endif // QT_CHECK_RANGE + + return; + } + + detach(); + + if ( ( d->mask & TQFontPrivate::Stretch ) && + d->request.stretch == (uint)factor ) + return; + + d->request.stretch = (uint)factor; + d->mask |= TQFontPrivate::Stretch; +} + +/*! + If \a enable is TRUE, turns raw mode on; otherwise turns raw mode + off. This function only has an effect under X11. + + If raw mode is enabled, TQt will search for an X font with a + complete font name matching the family name, ignoring all other + values set for the TQFont. If the font name matches several fonts, + TQt will use the first font returned by X. TQFontInfo \e cannot be + used to fetch information about a TQFont using raw mode (it will + return the values set in the TQFont for all parameters, including + the family name). + + \warning Do not use raw mode unless you really, really need it! In + most (if not all) cases, setRawName() is a much better choice. + + \sa rawMode(), setRawName() +*/ +void TQFont::setRawMode( bool enable ) +{ + detach(); + + if ( (bool) d->rawMode == enable ) return; + + d->rawMode = enable; +} + +/*! + Returns TRUE if a window system font exactly matching the settings + of this font is available. + + \sa TQFontInfo +*/ +bool TQFont::exactMatch() const +{ + TQFontEngine *engine = d->engineForScript( TQFont::NoScript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return d->rawMode ? engine->type() != TQFontEngine::Box + : d->request == engine->fontDef; +} + +/*! + Returns TRUE if this font is equal to \a f; otherwise returns + FALSE. + + Two TQFonts are considered equal if their font attributes are + equal. If rawMode() is enabled for both fonts, only the family + fields are compared. + + \sa operator!=() isCopyOf() +*/ +bool TQFont::operator==( const TQFont &f ) const +{ + return f.d == d || ( f.d->request == d->request && + f.d->underline == d->underline && + f.d->overline == d->overline && + f.d->strikeOut == d->strikeOut ); +} + +/*! + Returns TRUE if this font is different from \a f; otherwise + returns FALSE. + + Two TQFonts are considered to be different if their font attributes + are different. If rawMode() is enabled for both fonts, only the + family fields are compared. + + \sa operator==() +*/ +bool TQFont::operator!=( const TQFont &f ) const +{ + return !(operator==( f )); +} + +/*! + Returns TRUE if this font and \a f are copies of each other, i.e. + one of them was created as a copy of the other and neither has + been modified since. This is much stricter than equality. + + \sa operator=() operator==() +*/ +bool TQFont::isCopyOf( const TQFont & f ) const +{ + return d == f.d; +} + +/*! + Returns TRUE if raw mode is used for font name matching; otherwise + returns FALSE. + + \sa setRawMode() rawName() +*/ +bool TQFont::rawMode() const +{ + return d->rawMode; +} + +/*! + Returns a new TQFont that has attributes copied from \a other. +*/ +TQFont TQFont::resolve( const TQFont &other ) const +{ + if ( *this == other && d->mask == other.d->mask ) + return *this; + + TQFont font( *this ); + font.detach(); + + /* + if this font is a copy of the application default font, set the + fontdef mask to zero to indicate that *nothing* has been + explicitly set by the programmer. + */ + const TQFont appfont = TQApplication::font(); + if ( d == appfont.d ) + font.d->mask = 0; + + font.d->resolve( other.d ); + + return font; +} + +#ifndef QT_NO_COMPAT + +/*! \obsolete + + Please use TQApplication::font() instead. +*/ +TQFont TQFont::defaultFont() +{ + return TQApplication::font(); +} + +/*! \obsolete + + Please use TQApplication::setFont() instead. +*/ +void TQFont::setDefaultFont( const TQFont &f ) +{ + TQApplication::setFont( f ); +} + + +#endif + + + + +#ifndef QT_NO_STRINGLIST + +/***************************************************************************** + TQFont substitution management + *****************************************************************************/ + +typedef TQDict TQFontSubst; +static TQFontSubst *fontSubst = 0; +static TQSingleCleanupHandler qfont_cleanup_fontsubst; + + +// create substitution dict +static void initFontSubst() +{ + // default substitutions + static const char *initTbl[] = { + +#if defined(Q_WS_X11) + "arial", "helvetica", + "helv", "helvetica", + "tms rmn", "times", +#elif defined(Q_WS_WIN) + "times", "Times New Roman", + "courier", "Courier New", + "helvetica", "Arial", +#endif + + 0, 0 + }; + + if (fontSubst) + return; + + fontSubst = new TQFontSubst(17, FALSE); + Q_CHECK_PTR( fontSubst ); + fontSubst->setAutoDelete( TRUE ); + qfont_cleanup_fontsubst.set(&fontSubst); + + for ( int i=0; initTbl[i] != 0; i += 2 ) + TQFont::insertSubstitution(TQString::fromLatin1(initTbl[i]), + TQString::fromLatin1(initTbl[i+1])); +} + + +/*! + Returns the first family name to be used whenever \a familyName is + specified. The lookup is case insensitive. + + If there is no substitution for \a familyName, \a familyName is + returned. + + To obtain a list of substitutions use substitutes(). + + \sa setFamily() insertSubstitutions() insertSubstitution() removeSubstitution() +*/ +TQString TQFont::substitute( const TQString &familyName ) +{ + initFontSubst(); + + TQStringList *list = fontSubst->find(familyName); + if (list && list->count() > 0) + return *(list->at(0)); + + return familyName; +} + + +/*! + Returns a list of family names to be used whenever \a familyName + is specified. The lookup is case insensitive. + + If there is no substitution for \a familyName, an empty list is + returned. + + \sa substitute() insertSubstitutions() insertSubstitution() removeSubstitution() + */ +TQStringList TQFont::substitutes(const TQString &familyName) +{ + initFontSubst(); + + TQStringList ret, *list = fontSubst->find(familyName); + if (list) + ret += *list; + return ret; +} + + +/*! + Inserts the family name \a substituteName into the substitution + table for \a familyName. + + \sa insertSubstitutions() removeSubstitution() substitutions() substitute() substitutes() +*/ +void TQFont::insertSubstitution(const TQString &familyName, + const TQString &substituteName) +{ + initFontSubst(); + + TQStringList *list = fontSubst->find(familyName); + if (! list) { + list = new TQStringList; + fontSubst->insert(familyName, list); + } + + if (! list->contains(substituteName)) + list->append(substituteName); +} + + +/*! + Inserts the list of families \a substituteNames into the + substitution list for \a familyName. + + \sa insertSubstitution(), removeSubstitution(), substitutions(), substitute() +*/ +void TQFont::insertSubstitutions(const TQString &familyName, + const TQStringList &substituteNames) +{ + initFontSubst(); + + TQStringList *list = fontSubst->find(familyName); + if (! list) { + list = new TQStringList; + fontSubst->insert(familyName, list); + } + + TQStringList::ConstIterator it = substituteNames.begin(); + while (it != substituteNames.end()) { + if (! list->contains(*it)) + list->append(*it); + it++; + } +} + +// ### mark: should be called removeSubstitutions() +/*! + Removes all the substitutions for \a familyName. + + \sa insertSubstitutions(), insertSubstitution(), substitutions(), substitute() +*/ +void TQFont::removeSubstitution( const TQString &familyName ) +{ // ### function name should be removeSubstitutions() or + // ### removeSubstitutionList() + initFontSubst(); + + fontSubst->remove(familyName); +} + + +/*! + Returns a sorted list of substituted family names. + + \sa insertSubstitution(), removeSubstitution(), substitute() +*/ +TQStringList TQFont::substitutions() +{ + initFontSubst(); + + TQStringList ret; + TQDictIterator it(*fontSubst); + + while (it.current()) { + ret.append(it.currentKey()); + ++it; + } + + ret.sort(); + + return ret; +} + +#endif // QT_NO_STRINGLIST + + +/* \internal + Internal function. Converts boolean font settings to an unsigned + 8-bit number. Used for serialization etc. +*/ +static Q_UINT8 get_font_bits( const TQFontPrivate *f ) +{ +#ifdef QT_CHECK_STATE + Q_ASSERT( f != 0 ); +#endif + + Q_UINT8 bits = 0; + if ( f->request.italic ) + bits |= 0x01; + if ( f->underline ) + bits |= 0x02; + if ( f->overline ) + bits |= 0x40; + if ( f->strikeOut ) + bits |= 0x04; + if ( f->request.fixedPitch ) + bits |= 0x08; + // if ( f.hintSetByUser ) + // bits |= 0x10; + if ( f->rawMode ) + bits |= 0x20; + return bits; +} + + +#ifndef QT_NO_DATASTREAM + +/* \internal + Internal function. Sets boolean font settings from an unsigned + 8-bit number. Used for serialization etc. +*/ +static void set_font_bits( Q_UINT8 bits, TQFontPrivate *f ) +{ +#ifdef QT_CHECK_STATE + Q_ASSERT( f != 0 ); +#endif + + f->request.italic = (bits & 0x01) != 0; + f->underline = (bits & 0x02) != 0; + f->overline = (bits & 0x40) != 0; + f->strikeOut = (bits & 0x04) != 0; + f->request.fixedPitch = (bits & 0x08) != 0; + // f->hintSetByUser = (bits & 0x10) != 0; + f->rawMode = (bits & 0x20) != 0; +} + +#endif + + +/*! + Returns the font's key, a textual representation of a font. It is + typically used as the key for a cache or dictionary of fonts. + + \sa TQMap +*/ +TQString TQFont::key() const +{ + return toString(); +} + +/*! + Returns a description of the font. The description is a + comma-separated list of the attributes, perfectly suited for use + in TQSettings. + + \sa fromString() operator<<() + */ +TQString TQFont::toString() const +{ + const TQChar comma( ',' ); + return family() + comma + + TQString::number( pointSizeFloat() ) + comma + + TQString::number( pixelSize() ) + comma + + TQString::number( (int) styleHint() ) + comma + + TQString::number( weight() ) + comma + + TQString::number( (int) italic() ) + comma + + TQString::number( (int) underline() ) + comma + + TQString::number( (int) strikeOut() ) + comma + + TQString::number( (int)fixedPitch() ) + comma + + TQString::number( (int) rawMode() ); +} + + +/*! + Sets this font to match the description \a descrip. The description + is a comma-separated list of the font attributes, as returned by + toString(). + + \sa toString() operator>>() + */ +bool TQFont::fromString(const TQString &descrip) +{ +#ifndef QT_NO_STRINGLIST + TQStringList l(TQStringList::split(',', descrip)); + + int count = (int)l.count(); +#else + int count = 0; + TQString l[11]; + int from = 0; + int to = descrip.find( ',' ); + while ( to > 0 && count < 11 ) { + l[count] = descrip.mid( from, to-from ); + count++; + from = to+1; + to = descrip.find( ',', from ); + } +#endif // QT_NO_STRINGLIST + if ( !count || ( count > 2 && count < 9 ) || count > 11 ) { + +#ifdef QT_CHECK_STATE + qWarning("TQFont::fromString: invalid description '%s'", + descrip.isEmpty() ? "(empty)" : descrip.latin1()); +#endif + + return FALSE; + } + + setFamily(l[0]); + if ( count > 1 && l[1].toDouble() > 0.0 ) + setPointSizeFloat(l[1].toDouble()); + if ( count == 9 ) { + setStyleHint((StyleHint) l[2].toInt()); + setWeight(l[3].toInt()); + setItalic(l[4].toInt()); + setUnderline(l[5].toInt()); + setStrikeOut(l[6].toInt()); + setFixedPitch(l[7].toInt()); + setRawMode(l[8].toInt()); + } else if ( count == 10 ) { + if ( l[2].toInt() > 0 ) + setPixelSize( l[2].toInt() ); + setStyleHint((StyleHint) l[3].toInt()); + setWeight(l[4].toInt()); + setItalic(l[5].toInt()); + setUnderline(l[6].toInt()); + setStrikeOut(l[7].toInt()); + setFixedPitch(l[8].toInt()); + setRawMode(l[9].toInt()); + } + + return TRUE; +} + +#if !defined( Q_WS_QWS ) +/*! \internal + + Internal function that dumps font cache statistics. +*/ +void TQFont::cacheStatistics() +{ + + +} +#endif // !Q_WS_QWS + + + +/***************************************************************************** + TQFont stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM + +/*! + \relates TQFont + + Writes the font \a font to the data stream \a s. (toString() + writes to a text stream.) + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ +TQDataStream &operator<<( TQDataStream &s, const TQFont &font ) +{ + if ( s.version() == 1 ) { + TQCString fam( font.d->request.family.latin1() ); + s << fam; + } else { + s << font.d->request.family; + } + + if ( s.version() <= 3 ) { + Q_INT16 pointSize = (Q_INT16) font.d->request.pointSize; + if ( pointSize == -1 ) { +#ifdef Q_WS_X11 + pointSize = (Q_INT16)(font.d->request.pixelSize*720/TQPaintDevice::x11AppDpiY()); +#else + pointSize = (Q_INT16)TQFontInfo( font ).pointSize() * 10; +#endif + } + s << pointSize; + } else { + s << (Q_INT16) font.d->request.pointSize; + s << (Q_INT16) font.d->request.pixelSize; + } + + s << (Q_UINT8) font.d->request.styleHint; + if ( s.version() >= 5 ) + s << (Q_UINT8 ) font.d->request.styleStrategy; + return s << (Q_UINT8) 0 + << (Q_UINT8) font.d->request.weight + << get_font_bits(font.d); +} + + +/*! + \relates TQFont + + Reads the font \a font from the data stream \a s. (fromString() + reads from a text stream.) + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ +TQDataStream &operator>>( TQDataStream &s, TQFont &font ) +{ + if (font.d->deref()) delete font.d; + + font.d = new TQFontPrivate; + font.d->mask = TQFontPrivate::Complete; + + Q_INT16 pointSize, pixelSize = -1; + Q_UINT8 styleHint, styleStrategy = TQFont::PreferDefault, charSet, weight, bits; + + if ( s.version() == 1 ) { + TQCString fam; + s >> fam; + font.d->request.family = TQString( fam ); + } else { + s >> font.d->request.family; + } + + s >> pointSize; + if ( s.version() >= 4 ) + s >> pixelSize; + s >> styleHint; + if ( s.version() >= 5 ) + s >> styleStrategy; + s >> charSet; + s >> weight; + s >> bits; + + font.d->request.pointSize = pointSize; + font.d->request.pixelSize = pixelSize; + font.d->request.styleHint = styleHint; + font.d->request.styleStrategy = styleStrategy; + font.d->request.weight = weight; + + set_font_bits( bits, font.d ); + + return s; +} + +#endif // QT_NO_DATASTREAM + + + + +/***************************************************************************** + TQFontMetrics member functions + *****************************************************************************/ + +/*! + \class TQFontMetrics qfontmetrics.h + \brief The TQFontMetrics class provides font metrics information. + + \ingroup graphics + \ingroup shared + + TQFontMetrics functions calculate the size of characters and + strings for a given font. There are three ways you can create a + TQFontMetrics object: + + \list 1 + \i Calling the TQFontMetrics constructor with a TQFont creates a + font metrics object for a screen-compatible font, i.e. the font + cannot be a printer font*. If the font is changed + later, the font metrics object is \e not updated. + + \i TQWidget::fontMetrics() returns the font metrics for a widget's + font. This is equivalent to TQFontMetrics(widget->font()). If the + widget's font is changed later, the font metrics object is \e not + updated. + + \i TQPainter::fontMetrics() returns the font metrics for a + painter's current font. If the painter's font is changed later, the + font metrics object is \e not updated. + \endlist + + * If you use a printer font the values returned may be + inaccurate. Printer fonts are not always accessible so the nearest + screen font is used if a printer font is supplied. + + Once created, the object provides functions to access the + individual metrics of the font, its characters, and for strings + rendered in the font. + + There are several functions that operate on the font: ascent(), + descent(), height(), leading() and lineSpacing() return the basic + size properties of the font. The underlinePos(), overlinePos(), + strikeOutPos() and lineWidth() functions, return the properties of + the line that underlines, overlines or strikes out the + characters. These functions are all fast. + + There are also some functions that operate on the set of glyphs in + the font: minLeftBearing(), minRightBearing() and maxWidth(). + These are by necessity slow, and we recommend avoiding them if + possible. + + For each character, you can get its width(), leftBearing() and + rightBearing() and find out whether it is in the font using + inFont(). You can also treat the character as a string, and use + the string functions on it. + + The string functions include width(), to return the width of a + string in pixels (or points, for a printer), boundingRect(), to + return a rectangle large enough to contain the rendered string, + and size(), to return the size of that rectangle. + + Example: + \code + TQFont font( "times", 24 ); + TQFontMetrics fm( font ); + int pixelsWide = fm.width( "What's the width of this text?" ); + int pixelsHigh = fm.height(); + \endcode + + \sa TQFont TQFontInfo TQFontDatabase +*/ + +/*! + Constructs a font metrics object for \a font. + + The font must be screen-compatible, i.e. a font you use when + drawing text in \link TQWidget widgets\endlink or \link TQPixmap + pixmaps\endlink, not TQPicture or TQPrinter. + + The font metrics object holds the information for the font that is + passed in the constructor at the time it is created, and is not + updated if the font's attributes are changed later. + + Use TQPainter::fontMetrics() to get the font metrics when painting. + This will give correct results also when painting on paint device + that is not screen-compatible. +*/ +TQFontMetrics::TQFontMetrics( const TQFont &font ) + : d( font.d ), painter( 0 ), fscript( TQFont::NoScript ) +{ + d->ref(); +} + +/*! + \overload + + Constructs a font metrics object for \a font using the given \a + script. +*/ +TQFontMetrics::TQFontMetrics( const TQFont &font, TQFont::Script script ) + : d( font.d ), painter( 0 ), fscript( script ) +{ + d->ref(); +} + +/*! \internal + + Constructs a font metrics object for the painter's font \a p. +*/ +TQFontMetrics::TQFontMetrics( const TQPainter *p ) + : painter ( (TQPainter *) p ), fscript( TQFont::NoScript ) +{ +#if defined(CHECK_STATE) + if ( !painter->isActive() ) + qWarning( "TQFontMetrics: Get font metrics between TQPainter::begin() " + "and TQPainter::end()" ); +#endif + + if ( painter->testf(TQPainter::DirtyFont) ) + painter->updateFont(); + + d = painter->pfont ? painter->pfont->d : painter->cfont.d; + +#if defined(Q_WS_X11) + if ( d->screen != p->scrn ) { + TQFontPrivate *new_d = new TQFontPrivate( *d ); + Q_CHECK_PTR( new_d ); + d = new_d; + d->screen = p->scrn; + d->count = 1; + } else +#endif // Q_WS_X11 + d->ref(); +} + +/*! + Constructs a copy of \a fm. +*/ +TQFontMetrics::TQFontMetrics( const TQFontMetrics &fm ) + : d( fm.d ), painter( 0 ), fscript( fm.fscript ) +{ + d->ref(); +} + +/*! + Destroys the font metrics object and frees all allocated + resources. +*/ +TQFontMetrics::~TQFontMetrics() +{ + if ( d->deref() ) + delete d; +} + +/*! + Assigns the font metrics \a fm. +*/ +TQFontMetrics &TQFontMetrics::operator=( const TQFontMetrics &fm ) +{ + if ( d != fm.d ) { + if ( d->deref() ) + delete d; + d = fm.d; + d->ref(); + } + painter = fm.painter; + return *this; +} + +/*! + Returns the ascent of the font. + + The ascent of a font is the distance from the baseline to the + highest position characters extend to. In practice, some font + designers break this rule, e.g. when they put more than one accent + on top of a character, or to accommodate an unusual character in + an exotic language, so it is possible (though rare) that this + value will be too small. + + \sa descent() +*/ +int TQFontMetrics::ascent() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); + TQFontEngine *latin_engine = d->engineForScript( TQFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return TQMAX(engine->ascent(), latin_engine->ascent()); +} + + +/*! + Returns the descent of the font. + + The descent is the distance from the base line to the lowest point + characters extend to. (Note that this is different from X, which + adds 1 pixel.) In practice, some font designers break this rule, + e.g. to accommodate an unusual character in an exotic language, so + it is possible (though rare) that this value will be too small. + + \sa ascent() +*/ +int TQFontMetrics::descent() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); + TQFontEngine *latin_engine = d->engineForScript( TQFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return TQMAX(engine->descent(), latin_engine->descent()); +} + +/*! + Returns the height of the font. + + This is always equal to ascent()+descent()+1 (the 1 is for the + base line). + + \sa leading(), lineSpacing() +*/ +int TQFontMetrics::height() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); + TQFontEngine *latin_engine = d->engineForScript( TQFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return (TQMAX(engine->ascent(), latin_engine->ascent()) + + TQMAX(engine->descent(), latin_engine->descent()) + 1); +} + +/*! + Returns the leading of the font. + + This is the natural inter-line spacing. + + \sa height(), lineSpacing() +*/ +int TQFontMetrics::leading() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); + TQFontEngine *latin_engine = d->engineForScript( TQFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return TQMAX(engine->leading(), latin_engine->leading()); +} + +/*! + Returns the distance from one base line to the next. + + This value is always equal to leading()+height(). + + \sa height(), leading() +*/ +int TQFontMetrics::lineSpacing() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); + TQFontEngine *latin_engine = d->engineForScript( TQFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return (TQMAX(engine->leading(), latin_engine->leading()) + + TQMAX(engine->ascent(), latin_engine->ascent()) + + TQMAX(engine->descent(), latin_engine->descent()) + 1); +} + +/*! + Returns the minimum left bearing of the font. + + This is the smallest leftBearing(char) of all characters in the + font. + + Note that this function can be very slow if the font is large. + + \sa minRightBearing(), leftBearing() +*/ +int TQFontMetrics::minLeftBearing() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); + TQFontEngine *latin_engine = d->engineForScript( TQFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return TQMIN(engine->minLeftBearing(), latin_engine->minLeftBearing()); +} + +/*! + Returns the minimum right bearing of the font. + + This is the smallest rightBearing(char) of all characters in the + font. + + Note that this function can be very slow if the font is large. + + \sa minLeftBearing(), rightBearing() +*/ +int TQFontMetrics::minRightBearing() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); + TQFontEngine *latin_engine = d->engineForScript( TQFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( latin_engine != 0 ); +#endif // QT_CHECK_STATE + + return TQMIN(engine->minRightBearing(), latin_engine->minRightBearing()); +} + +/*! + Returns the width of the widest character in the font. +*/ +int TQFontMetrics::maxWidth() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); + TQFontEngine *lengine = d->engineForScript( TQFont::Latin ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); + Q_ASSERT( lengine != 0 ); +#endif // QT_CHECK_STATE + + return TQMAX(engine->maxCharWidth(), lengine->maxCharWidth()); +} + +/*! + Returns TRUE if character \a ch is a valid character in the font; + otherwise returns FALSE. +*/ +bool TQFontMetrics::inFont(TQChar ch) const +{ + TQFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + TQFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + if ( engine->type() == TQFontEngine::Box ) return FALSE; + return engine->canRender( &ch, 1 ); +} + +/*! \fn int TQFontMetrics::leftBearing( TQChar ch ) const + Returns the left bearing of character \a ch in the font. + + The left bearing is the right-ward distance of the left-most pixel + of the character from the logical origin of the character. This + value is negative if the pixels of the character extend to the + left of the logical origin. + + See width(TQChar) for a graphical description of this metric. + + \sa rightBearing(), minLeftBearing(), width() +*/ +#if !defined(Q_WS_WIN) && !defined(Q_WS_QWS) +int TQFontMetrics::leftBearing(TQChar ch) const +{ + TQFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + TQFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + if ( engine->type() == TQFontEngine::Box ) return 0; + + glyph_t glyphs[10]; + int nglyphs = 9; + engine->stringToCMap( &ch, 1, glyphs, 0, &nglyphs, FALSE ); + // ### can nglyphs != 1 happen at all? Not currently I think + glyph_metrics_t gi = engine->boundingBox( glyphs[0] ); + return gi.x; +} +#endif // !Q_WS_WIN + +/*! \fn int TQFontMetrics::rightBearing(TQChar ch) const + Returns the right bearing of character \a ch in the font. + + The right bearing is the left-ward distance of the right-most + pixel of the character from the logical origin of a subsequent + character. This value is negative if the pixels of the character + extend to the right of the width() of the character. + + See width() for a graphical description of this metric. + + \sa leftBearing(), minRightBearing(), width() +*/ +#if !defined(Q_WS_WIN) && !defined(Q_WS_QWS) +int TQFontMetrics::rightBearing(TQChar ch) const +{ + TQFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + TQFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + if ( engine->type() == TQFontEngine::Box ) return 0; + + glyph_t glyphs[10]; + int nglyphs = 9; + engine->stringToCMap( &ch, 1, glyphs, 0, &nglyphs, FALSE ); + // ### can nglyphs != 1 happen at all? Not currently I think + glyph_metrics_t gi = engine->boundingBox( glyphs[0] ); + return gi.xoff - gi.x - gi.width; +} +#endif // !Q_WS_WIN + + +#ifndef Q_WS_QWS +/*! + Returns the width in pixels of the first \a len characters of \a + str. If \a len is negative (the default), the entire string is + used. + + Note that this value is \e not equal to boundingRect().width(); + boundingRect() returns a rectangle describing the pixels this + string will cover whereas width() returns the distance to where + the next string should be drawn. + + \sa boundingRect() +*/ +int TQFontMetrics::width( const TQString &str, int len ) const +{ + if (len < 0) + len = str.length(); + if (len == 0) + return 0; + + int pos = 0; + int width = 0; +#ifndef Q_WS_MAC + const TQChar *ch = str.unicode(); + + while (pos < len) { + unsigned short uc = ch->unicode(); + if (uc < TQFontEngineData::widthCacheSize && d->engineData && d->engineData->widthCache[uc]) + width += d->engineData->widthCache[uc]; + else { + TQFont::Script script; + SCRIPT_FOR_CHAR( script, *ch ); + + if (script >= TQFont::Arabic && script <= TQFont::Khmer) + break; + if ( ::category( *ch ) != TQChar::Mark_NonSpacing && !qIsZeroWidthChar(ch->unicode())) { + TQFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + glyph_t glyphs[8]; + advance_t advances[8]; + int nglyphs = 7; + engine->stringToCMap( ch, 1, glyphs, advances, &nglyphs, FALSE ); + + // ### can nglyphs != 1 happen at all? Not currently I think + if ( uc < TQFontEngineData::widthCacheSize && advances[0] > 0 && advances[0] < 0x100 ) + d->engineData->widthCache[ uc ] = advances[0]; + width += advances[0]; + } + } + ++pos; + ++ch; + } + if ( pos < len ) { +#endif + TQTextEngine layout( str, d ); + layout.itemize( TQTextEngine::WidthOnly ); + width += layout.width( pos, len-pos ); +#ifndef Q_WS_MAC + } +#endif + return width; +} +#endif + +/*! \fn int TQFontMetrics::width( TQChar ch ) const + + + + Returns the logical width of character \a ch in pixels. This is a + distance appropriate for drawing a subsequent character after \a + ch. + + Some of the metrics are described in the image to the right. The + central dark rectangles cover the logical width() of each + character. The outer pale rectangles cover the leftBearing() and + rightBearing() of each character. Notice that the bearings of "f" + in this particular font are both negative, while the bearings of + "o" are both positive. + + \warning This function will produce incorrect results for Arabic + characters or non spacing marks in the middle of a string, as the + glyph shaping and positioning of marks that happens when + processing strings cannot be taken into account. Use charWidth() + instead if you aren't looking for the width of isolated + characters. + + \sa boundingRect(), charWidth() +*/ + +/*! \fn int TQFontMetrics::width( char c ) const + + \overload + \obsolete + + Provided to aid porting from TQt 1.x. +*/ + +/*! \fn int TQFontMetrics::charWidth( const TQString &str, int pos ) const + Returns the width of the character at position \a pos in the + string \a str. + + The whole string is needed, as the glyph drawn may change + depending on the context (the letter before and after the current + one) for some languages (e.g. Arabic). + + This function also takes non spacing marks and ligatures into + account. +*/ + +#ifndef Q_WS_QWS +/*! + Returns the bounding rectangle of the first \a len characters of + \a str, which is the set of pixels the text would cover if drawn + at (0, 0). + + If \a len is negative (the default), the entire string is used. + + Note that the bounding rectangle may extend to the left of (0, 0), + e.g. for italicized fonts, and that the text output may cover \e + all pixels in the bounding rectangle. + + Newline characters are processed as normal characters, \e not as + linebreaks. + + Due to the different actual character heights, the height of the + bounding rectangle of e.g. "Yes" and "yes" may be different. + + \sa width(), TQPainter::boundingRect() +*/ +TQRect TQFontMetrics::boundingRect( const TQString &str, int len ) const +{ + if (len < 0) + len = str.length(); + if (len == 0) + return TQRect(); + + TQTextEngine layout( str, d ); + layout.itemize( TQTextEngine::NoBidi|TQTextEngine::SingleLine ); + glyph_metrics_t gm = layout.boundingBox( 0, len ); + return TQRect( gm.x, gm.y, gm.width, gm.height ); +} +#endif + +/*! + Returns the rectangle that is covered by ink if the character + specified by \a ch were to be drawn at the origin of the coordinate + system. + + Note that the bounding rectangle may extend to the left of (0, 0), + e.g. for italicized fonts, and that the text output may cover \e + all pixels in the bounding rectangle. For a space character the rectangle + will usually be empty. + + Note that the rectangle usually extends both above and below the + base line. + + \warning The width of the returned rectangle is not the advance width + of the character. Use boundingRect(const TQString &) or width() instead. + + \sa width() +*/ +TQRect TQFontMetrics::boundingRect( TQChar ch ) const +{ + TQFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + TQFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + glyph_t glyphs[10]; + int nglyphs = 9; + engine->stringToCMap( &ch, 1, glyphs, 0, &nglyphs, FALSE ); + glyph_metrics_t gi = engine->boundingBox( glyphs[0] ); + return TQRect( gi.x, gi.y, gi.width, gi.height ); +} + +/*! + \overload + + Returns the bounding rectangle of the first \a len characters of + \a str, which is the set of pixels the text would cover if drawn + at (0, 0). The drawing, and hence the bounding rectangle, is + constrained to the rectangle (\a x, \a y, \a w, \a h). + + If \a len is negative (which is the default), the entire string is + used. + + The \a flgs argument is the bitwise OR of the following flags: + \list + \i \c AlignAuto aligns to the left border for all languages except + Arabic and Hebrew where it aligns to the right. + \i \c AlignLeft aligns to the left border. + \i \c AlignRight aligns to the right border. + \i \c AlignJustify produces justified text. + \i \c AlignHCenter aligns horizontally centered. + \i \c AlignTop aligns to the top border. + \i \c AlignBottom aligns to the bottom border. + \i \c AlignVCenter aligns vertically centered + \i \c AlignCenter (== \c{AlignHCenter | AlignVCenter}) + \i \c SingleLine ignores newline characters in the text. + \i \c ExpandTabs expands tabs (see below) + \i \c ShowPrefix interprets "&x" as "x", i.e. underlined. + \i \c WordBreak breaks the text to fit the rectangle. + \endlist + + Horizontal alignment defaults to \c AlignAuto and vertical + alignment defaults to \c AlignTop. + + If several of the horizontal or several of the vertical alignment + flags are set, the resulting alignment is undefined. + + These flags are defined in \c qnamespace.h. + + If \c ExpandTabs is set in \a flgs, then: if \a tabarray is + non-null, it specifies a 0-terminated sequence of pixel-positions + for tabs; otherwise if \a tabstops is non-zero, it is used as the + tab spacing (in pixels). + + Note that the bounding rectangle may extend to the left of (0, 0), + e.g. for italicized fonts, and that the text output may cover \e + all pixels in the bounding rectangle. + + Newline characters are processed as linebreaks. + + Despite the different actual character heights, the heights of the + bounding rectangles of "Yes" and "yes" are the same. + + The bounding rectangle given by this function is somewhat larger + than that calculated by the simpler boundingRect() function. This + function uses the \link minLeftBearing() maximum left \endlink and + \link minRightBearing() right \endlink font bearings as is + necessary for multi-line text to align correctly. Also, + fontHeight() and lineSpacing() are used to calculate the height, + rather than individual character heights. + + The \a intern argument should not be used. + + \sa width(), TQPainter::boundingRect(), TQt::AlignmentFlags +*/ +TQRect TQFontMetrics::boundingRect( int x, int y, int w, int h, int flgs, + const TQString& str, int len, int tabstops, + int *tabarray, TQTextParag **intern ) const +{ + if ( len < 0 ) + len = str.length(); + + int tabarraylen=0; + if (tabarray) + while (tabarray[tabarraylen]) + tabarraylen++; + + TQRect rb; + TQRect r(x, y, w, h); + qt_format_text( TQFont( d, d->paintdevice ), r, flgs|TQt::DontPrint, str, len, &rb, + tabstops, tabarray, tabarraylen, intern, 0 ); + + return rb; +} + +/*! + Returns the size in pixels of the first \a len characters of \a + str. + + If \a len is negative (the default), the entire string is used. + + The \a flgs argument is the bitwise OR of the following flags: + \list + \i \c SingleLine ignores newline characters. + \i \c ExpandTabs expands tabs (see below) + \i \c ShowPrefix interprets "&x" as "x", i.e. underlined. + \i \c WordBreak breaks the text to fit the rectangle. + \endlist + + These flags are defined in \c qnamespace.h. + + If \c ExpandTabs is set in \a flgs, then: if \a tabarray is + non-null, it specifies a 0-terminated sequence of pixel-positions + for tabs; otherwise if \a tabstops is non-zero, it is used as the + tab spacing (in pixels). + + Newline characters are processed as linebreaks. + + Despite the different actual character heights, the heights of the + bounding rectangles of "Yes" and "yes" are the same. + + The \a intern argument should not be used. + + \sa boundingRect() +*/ +TQSize TQFontMetrics::size( int flgs, const TQString &str, int len, int tabstops, + int *tabarray, TQTextParag **intern ) const +{ + return boundingRect(0,0,0,0,flgs,str,len,tabstops,tabarray,intern).size(); +} + +/*! + Returns the distance from the base line to where an underscore + should be drawn. + + \sa overlinePos(), strikeOutPos(), lineWidth() +*/ +int TQFontMetrics::underlinePos() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return engine->underlinePosition(); +} + +/*! + Returns the distance from the base line to where an overline + should be drawn. + + \sa underlinePos(), strikeOutPos(), lineWidth() +*/ +int TQFontMetrics::overlinePos() const +{ + int pos = ascent() + 1; + return pos > 0 ? pos : 1; +} + +/*! + Returns the distance from the base line to where the strikeout + line should be drawn. + + \sa underlinePos(), overlinePos(), lineWidth() +*/ +int TQFontMetrics::strikeOutPos() const +{ + int pos = ascent() / 3; + return pos > 0 ? pos : 1; +} + +/*! + Returns the width of the underline and strikeout lines, adjusted + for the point size of the font. + + \sa underlinePos(), overlinePos(), strikeOutPos() +*/ +int TQFontMetrics::lineWidth() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return engine->lineThickness(); +} + + + + +/***************************************************************************** + TQFontInfo member functions + *****************************************************************************/ + +/*! + \class TQFontInfo qfontinfo.h + + \brief The TQFontInfo class provides general information about fonts. + + \ingroup graphics + \ingroup shared + + The TQFontInfo class provides the same access functions as TQFont, + e.g. family(), pointSize(), italic(), weight(), fixedPitch(), + styleHint() etc. But whilst the TQFont access functions return the + values that were set, a TQFontInfo object returns the values that + apply to the font that will actually be used to draw the text. + + For example, when the program asks for a 25pt Courier font on a + machine that has a non-scalable 24pt Courier font, TQFont will + (normally) use the 24pt Courier for rendering. In this case, + TQFont::pointSize() returns 25 and TQFontInfo::pointSize() returns + 24. + + There are three ways to create a TQFontInfo object. + \list 1 + \i Calling the TQFontInfo constructor with a TQFont creates a font + info object for a screen-compatible font, i.e. the font cannot be + a printer font*. If the font is changed later, the font + info object is \e not updated. + + \i TQWidget::fontInfo() returns the font info for a widget's font. + This is equivalent to calling TQFontInfo(widget->font()). If the + widget's font is changed later, the font info object is \e not + updated. + + \i TQPainter::fontInfo() returns the font info for a painter's + current font. If the painter's font is changed later, the font + info object is \e not updated. + \endlist + + * If you use a printer font the values returned may be + inaccurate. Printer fonts are not always accessible so the nearest + screen font is used if a printer font is supplied. + + \sa TQFont TQFontMetrics TQFontDatabase +*/ + +/*! + Constructs a font info object for \a font. + + The font must be screen-compatible, i.e. a font you use when + drawing text in \link TQWidget widgets\endlink or \link TQPixmap + pixmaps\endlink, not TQPicture or TQPrinter. + + The font info object holds the information for the font that is + passed in the constructor at the time it is created, and is not + updated if the font's attributes are changed later. + + Use TQPainter::fontInfo() to get the font info when painting. + This will give correct results also when painting on paint device + that is not screen-compatible. +*/ +TQFontInfo::TQFontInfo( const TQFont &font ) + : d( font.d ), painter( 0 ), fscript( TQFont::NoScript ) +{ + d->ref(); +} + +/*! + Constructs a font info object for \a font using the specified \a + script. +*/ +TQFontInfo::TQFontInfo( const TQFont &font, TQFont::Script script ) + : d( font.d ), painter( 0 ), fscript( script ) +{ + d->ref(); +} + +/*! \internal + + Constructs a font info object from the painter's font \a p. +*/ +TQFontInfo::TQFontInfo( const TQPainter *p ) + : painter( 0 ), fscript( TQFont::NoScript ) +{ + TQPainter *painter = (TQPainter *) p; + +#if defined(CHECK_STATE) + if ( !painter->isActive() ) + qWarning( "TQFontInfo: Get font info between TQPainter::begin() " + "and TQPainter::end()" ); +#endif + + painter->setf( TQPainter::FontInf ); + if ( painter->testf(TQPainter::DirtyFont) ) + painter->updateFont(); + if ( painter->pfont ) + d = painter->pfont->d; + else + d = painter->cfont.d; + d->ref(); +} + +/*! + Constructs a copy of \a fi. +*/ +TQFontInfo::TQFontInfo( const TQFontInfo &fi ) + : d(fi.d), painter(0), fscript( fi.fscript ) +{ + d->ref(); +} + +/*! + Destroys the font info object. +*/ +TQFontInfo::~TQFontInfo() +{ + if ( d->deref() ) + delete d; +} + +/*! + Assigns the font info in \a fi. +*/ +TQFontInfo &TQFontInfo::operator=( const TQFontInfo &fi ) +{ + if ( d != fi.d ) { + if ( d->deref() ) + delete d; + d = fi.d; + d->ref(); + } + painter = 0; + fscript = fi.fscript; + return *this; +} + +/*! + Returns the family name of the matched window system font. + + \sa TQFont::family() +*/ +TQString TQFontInfo::family() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return engine->fontDef.family; +} + +/*! + Returns the point size of the matched window system font. + + \sa TQFont::pointSize() +*/ +int TQFontInfo::pointSize() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return ( engine->fontDef.pointSize + 5 ) / 10; +} + +/*! + Returns the pixel size of the matched window system font. + + \sa TQFont::pointSize() +*/ +int TQFontInfo::pixelSize() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return engine->fontDef.pixelSize; +} + +/*! + Returns the italic value of the matched window system font. + + \sa TQFont::italic() +*/ +bool TQFontInfo::italic() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return engine->fontDef.italic; +} + +/*! + Returns the weight of the matched window system font. + + \sa TQFont::weight(), bold() +*/ +int TQFontInfo::weight() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return engine->fontDef.weight; + +} + +/*! + \fn bool TQFontInfo::bold() const + + Returns TRUE if weight() would return a value greater than \c + TQFont::Normal; otherwise returns FALSE. + + \sa weight(), TQFont::bold() +*/ + +/*! + Returns the underline value of the matched window system font. + + \sa TQFont::underline() + + \internal + + Here we read the underline flag directly from the TQFont. + This is OK for X11 and for Windows because we always get what we want. +*/ +bool TQFontInfo::underline() const +{ + return d->underline; +} + +/*! + Returns the overline value of the matched window system font. + + \sa TQFont::overline() + + \internal + + Here we read the overline flag directly from the TQFont. + This is OK for X11 and for Windows because we always get what we want. +*/ +bool TQFontInfo::overline() const +{ + return d->overline; +} + +/*! + Returns the strikeout value of the matched window system font. + + \sa TQFont::strikeOut() + + \internal Here we read the strikeOut flag directly from the TQFont. + This is OK for X11 and for Windows because we always get what we want. +*/ +bool TQFontInfo::strikeOut() const +{ + return d->strikeOut; +} + +/*! + Returns the fixed pitch value of the matched window system font. + + \sa TQFont::fixedPitch() +*/ +bool TQFontInfo::fixedPitch() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE +#ifdef Q_OS_MAC + if (!engine->fontDef.fixedPitchComputed) { + TQChar ch[2] = { TQChar('i'), TQChar('m') }; + glyph_t g[2]; + int l = 2; + advance_t a[2]; + engine->stringToCMap(ch, 2, g, a, &l, FALSE); + engine->fontDef.fixedPitch = a[0] == a[1]; + engine->fontDef.fixedPitchComputed = TRUE; + } +#endif + return engine->fontDef.fixedPitch; +} + +/*! + Returns the style of the matched window system font. + + Currently only returns the style hint set in TQFont. + + \sa TQFont::styleHint() TQFont::StyleHint +*/ +TQFont::StyleHint TQFontInfo::styleHint() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + return (TQFont::StyleHint) engine->fontDef.styleHint; +} + +/*! + Returns TRUE if the font is a raw mode font; otherwise returns + FALSE. + + If it is a raw mode font, all other functions in TQFontInfo will + return the same values set in the TQFont, regardless of the font + actually used. + + \sa TQFont::rawMode() +*/ +bool TQFontInfo::rawMode() const +{ + return d->rawMode; +} + +/*! + Returns TRUE if the matched window system font is exactly the same + as the one specified by the font; otherwise returns FALSE. + + \sa TQFont::exactMatch() +*/ +bool TQFontInfo::exactMatch() const +{ + TQFontEngine *engine = d->engineForScript( (TQFont::Script) fscript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return d->rawMode ? engine->type() != TQFontEngine::Box + : d->request == engine->fontDef; +} + + + + +// ********************************************************************** +// TQFontCache +// ********************************************************************** + +#ifdef TQFONTCACHE_DEBUG +// fast timeouts for debugging +static const int fast_timeout = 1000; // 1s +static const int slow_timeout = 5000; // 5s +#else +static const int fast_timeout = 10000; // 10s +static const int slow_timeout = 300000; // 5m +#endif // TQFONTCACHE_DEBUG + +TQFontCache *TQFontCache::instance = 0; +const uint TQFontCache::min_cost = 4*1024; // 4mb + +static TQSingleCleanupHandler cleanup_fontcache; + + +TQFontCache::TQFontCache() + : TQObject( qApp, "global font cache" ), total_cost( 0 ), max_cost( min_cost ), + current_timestamp( 0 ), fast( FALSE ), timer_id( -1 ) +{ + Q_ASSERT( instance == 0 ); + instance = this; + cleanup_fontcache.set( &instance ); +} + +TQFontCache::~TQFontCache() +{ + { + EngineDataCache::Iterator it = engineDataCache.begin(), + end = engineDataCache.end(); + while ( it != end ) { + if ( it.data()->count == 0 ) + delete it.data(); + else + FC_DEBUG("TQFontCache::~TQFontCache: engineData %p still has refcount %d", + it.data(), it.data()->count); + ++it; + } + } + EngineCache::Iterator it = engineCache.begin(), + end = engineCache.end(); + while ( it != end ) { + if ( it.data().data->count == 0 ) { + if ( --it.data().data->cache_count == 0 ) { + FC_DEBUG("TQFontCache::~TQFontCache: deleting engine %p key=(%d / %d %d %d %d %d)", + it.data().data, it.key().script, it.key().def.pointSize, + it.key().def.pixelSize, it.key().def.weight, it.key().def.italic, + it.key().def.fixedPitch); + + delete it.data().data; + } + } else { + FC_DEBUG("TQFontCache::~TQFontCache: engine = %p still has refcount %d", + it.data().data, it.data().data->count); + } + ++it; + } + instance = 0; +} + +#ifdef Q_WS_QWS +void TQFontCache::clear() +{ + { + EngineDataCache::Iterator it = engineDataCache.begin(), + end = engineDataCache.end(); + while ( it != end ) { + TQFontEngineData *data = it.data(); + if ( data->engine ) + data->engine->deref(); + data->engine = 0; + ++it; + } + } + + EngineCache::Iterator it = engineCache.begin(), + end = engineCache.end(); + while ( it != end ) { + if ( it.data().data->count == 0 ) { + if ( --it.data().data->cache_count == 0 ) { + FC_DEBUG("TQFontCache::~TQFontCache: deleting engine %p key=(%d / %d %d %d %d %d)", + it.data().data, it.key().script, it.key().def.pointSize, + it.key().def.pixelSize, it.key().def.weight, it.key().def.italic, + it.key().def.fixedPitch); + delete it.data().data; + } + } else { + FC_DEBUG("TQFontCache::~TQFontCache: engine = %p still has refcount %d", + it.data().data, it.data().data->count); + } + ++it; + } +} +#endif + +TQFontEngineData *TQFontCache::findEngineData( const Key &key ) const +{ + EngineDataCache::ConstIterator it = engineDataCache.find( key ), + end = engineDataCache.end(); + if ( it == end ) return 0; + + // found + return it.data(); +} + +void TQFontCache::insertEngineData( const Key &key, TQFontEngineData *engineData ) +{ + FC_DEBUG( "TQFontCache: inserting new engine data %p", engineData ); + + engineDataCache.insert( key, engineData ); + increaseCost( sizeof( TQFontEngineData ) ); +} + +TQFontEngine *TQFontCache::findEngine( const Key &key ) +{ + EngineCache::Iterator it = engineCache.find( key ), + end = engineCache.end(); + if ( it == end ) return 0; + + // found... update the hitcount and timestamp + it.data().hits++; + it.data().timestamp = ++current_timestamp; + + FC_DEBUG( "TQFontCache: found font engine\n" + " %p: timestamp %4u hits %3u ref %2d/%2d, type '%s'", + it.data().data, it.data().timestamp, it.data().hits, + it.data().data->count, it.data().data->cache_count, + it.data().data->name() ); + + return it.data().data; +} + +void TQFontCache::insertEngine( const Key &key, TQFontEngine *engine ) +{ + FC_DEBUG( "TQFontCache: inserting new engine %p", engine ); + + Engine data( engine ); + data.timestamp = ++current_timestamp; + + engineCache.insert( key, data ); + + // only increase the cost if this is the first time we insert the engine + if ( engine->cache_count == 0 ) + increaseCost( engine->cache_cost ); + + ++engine->cache_count; +} + +void TQFontCache::increaseCost( uint cost ) +{ + cost = ( cost + 512 ) / 1024; // store cost in kb + cost = cost > 0 ? cost : 1; + total_cost += cost; + + FC_DEBUG( " COST: increased %u kb, total_cost %u kb, max_cost %u kb", + cost, total_cost, max_cost ); + + if ( total_cost > max_cost) { + max_cost = total_cost; + + if ( timer_id == -1 || ! fast ) { + FC_DEBUG( " TIMER: starting fast timer (%d ms)", fast_timeout ); + + if (timer_id != -1) killTimer( timer_id ); + timer_id = startTimer( fast_timeout ); + fast = TRUE; + } + } +} + +void TQFontCache::decreaseCost( uint cost ) +{ + cost = ( cost + 512 ) / 1024; // cost is stored in kb + cost = cost > 0 ? cost : 1; + Q_ASSERT( cost <= total_cost ); + total_cost -= cost; + + FC_DEBUG( " COST: decreased %u kb, total_cost %u kb, max_cost %u kb", + cost, total_cost, max_cost ); +} + +#if defined(Q_WS_WIN ) || defined (Q_WS_QWS) +void TQFontCache::cleanupPrinterFonts() +{ + FC_DEBUG( "TQFontCache::cleanupPrinterFonts" ); + + { + FC_DEBUG( " CLEAN engine data:" ); + + // clean out all unused engine datas + EngineDataCache::Iterator it = engineDataCache.begin(), + end = engineDataCache.end(); + while ( it != end ) { + if ( it.key().screen == 0 ) { + ++it; + continue; + } + + if( it.data()->count > 0 ) { +#ifdef Q_WS_WIN + for(int i = 0; i < TQFont::LastPrivateScript; ++i) { + if( it.data()->engines[i] ) { + it.data()->engines[i]->deref(); + it.data()->engines[i] = 0; + } + } +#else + if ( it.data()->engine ) { + it.data()->engine->deref(); + it.data()->engine = 0; + } +#endif + ++it; + } else { + + EngineDataCache::Iterator rem = it++; + + decreaseCost( sizeof( TQFontEngineData ) ); + + FC_DEBUG( " %p", rem.data() ); + + delete rem.data(); + engineDataCache.remove( rem ); + } + } + } + + EngineCache::Iterator it = engineCache.begin(), + end = engineCache.end(); + while( it != end ) { + if ( it.data().data->count > 0 || it.key().screen == 0) { + ++it; + continue; + } + + FC_DEBUG( " %p: timestamp %4u hits %2u ref %2d/%2d, type '%s'", + it.data().data, it.data().timestamp, it.data().hits, + it.data().data->count, it.data().data->cache_count, + it.data().data->name() ); + + if ( --it.data().data->cache_count == 0 ) { + FC_DEBUG( " DELETE: last occurence in cache" ); + + decreaseCost( it.data().data->cache_cost ); + delete it.data().data; + } + + engineCache.remove( it++ ); + } +} +#endif + +void TQFontCache::timerEvent( TQTimerEvent * ) +{ + FC_DEBUG( "TQFontCache::timerEvent: performing cache maintenance (timestamp %u)", + current_timestamp ); + + if ( total_cost <= max_cost && max_cost <= min_cost ) { + FC_DEBUG( " cache redused sufficiently, stopping timer" ); + + killTimer( timer_id ); + timer_id = -1; + fast = FALSE; + + return; + } + + // go through the cache and count up everything in use + uint in_use_cost = 0; + + { + FC_DEBUG( " SWEEP engine data:" ); + + // make sure the cost of each engine data is at least 1kb + const uint engine_data_cost = + sizeof( TQFontEngineData ) > 1024 ? sizeof( TQFontEngineData ) : 1024; + + EngineDataCache::ConstIterator it = engineDataCache.begin(), + end = engineDataCache.end(); + for ( ; it != end; ++it ) { +#ifdef TQFONTCACHE_DEBUG + FC_DEBUG( " %p: ref %2d", it.data(), it.data()->count ); + +# if defined(Q_WS_X11) || defined(Q_WS_WIN) + // print out all engines + for ( int i = 0; i < TQFont::LastPrivateScript; ++i ) { + if ( ! it.data()->engines[i] ) continue; + FC_DEBUG( " contains %p", it.data()->engines[i] ); + } +# endif // Q_WS_X11 || Q_WS_WIN +#endif // TQFONTCACHE_DEBUG + + if ( it.data()->count > 0 ) + in_use_cost += engine_data_cost; + } + } + + { + FC_DEBUG( " SWEEP engine:" ); + + EngineCache::ConstIterator it = engineCache.begin(), + end = engineCache.end(); + for ( ; it != end; ++it ) { + FC_DEBUG( " %p: timestamp %4u hits %2u ref %2d/%2d, cost %u bytes", + it.data().data, it.data().timestamp, it.data().hits, + it.data().data->count, it.data().data->cache_count, + it.data().data->cache_cost ); + + if ( it.data().data->count > 0 ) + in_use_cost += it.data().data->cache_cost / it.data().data->cache_count; + } + + // attempt to make up for rounding errors + in_use_cost += (uint)engineCache.count(); + } + + in_use_cost = ( in_use_cost + 512 ) / 1024; // cost is stored in kb + + /* + calculate the new maximum cost for the cache + + NOTE: in_use_cost is *not* correct due to rounding errors in the + above algorithm. instead of worrying about getting the + calculation correct, we are more interested in speed, and use + in_use_cost as a floor for new_max_cost + */ + uint new_max_cost = TQMAX( TQMAX( max_cost / 2, in_use_cost ), min_cost ); + + FC_DEBUG( " after sweep, in use %u kb, total %u kb, max %u kb, new max %u kb", + in_use_cost, total_cost, max_cost, new_max_cost ); + + if ( new_max_cost == max_cost ) { + if ( fast ) { + FC_DEBUG( " cannot shrink cache, slowing timer" ); + + killTimer( timer_id ); + timer_id = startTimer( slow_timeout ); + fast = FALSE; + } + + return; + } else if ( ! fast ) { + FC_DEBUG( " dropping into passing gear" ); + + killTimer( timer_id ); + timer_id = startTimer( fast_timeout ); + fast = TRUE; + } + + max_cost = new_max_cost; + + { + FC_DEBUG( " CLEAN engine data:" ); + + // clean out all unused engine datas + EngineDataCache::Iterator it = engineDataCache.begin(), + end = engineDataCache.end(); + while ( it != end ) { + if ( it.data()->count > 0 ) { + ++it; + continue; + } + + EngineDataCache::Iterator rem = it++; + + decreaseCost( sizeof( TQFontEngineData ) ); + + FC_DEBUG( " %p", rem.data() ); + + delete rem.data(); + engineDataCache.remove( rem ); + } + } + + // clean out the engine cache just enough to get below our new max cost + uint current_cost; + do { + current_cost = total_cost; + + EngineCache::Iterator it = engineCache.begin(), + end = engineCache.end(); + // determine the oldest and least popular of the unused engines + uint oldest = ~0; + uint least_popular = ~0; + + for ( ; it != end; ++it ) { + if ( it.data().data->count > 0 ) continue; + + if ( it.data().timestamp < oldest && + it.data().hits <= least_popular ) { + oldest = it.data().timestamp; + least_popular = it.data().hits; + } + } + + FC_DEBUG( " oldest %u least popular %u", oldest, least_popular ); + + for ( it = engineCache.begin(); it != end; ++it ) { + if ( it.data().data->count == 0 && + it.data().timestamp == oldest && + it.data().hits == least_popular) + break; + } + + if ( it != end ) { + FC_DEBUG( " %p: timestamp %4u hits %2u ref %2d/%2d, type '%s'", + it.data().data, it.data().timestamp, it.data().hits, + it.data().data->count, it.data().data->cache_count, + it.data().data->name() ); + + if ( --it.data().data->cache_count == 0 ) { + FC_DEBUG( " DELETE: last occurence in cache" ); + + decreaseCost( it.data().data->cache_cost ); + delete it.data().data; + } else { + /* + this particular font engine is in the cache multiple + times... set current_cost to zero, so that we can + keep looping to get rid of all occurences + */ + current_cost = 0; + } + + engineCache.remove( it ); + } + } while ( current_cost != total_cost && total_cost > max_cost ); +} diff --git a/src/kernel/qfont.h b/src/kernel/qfont.h new file mode 100644 index 000000000..8b54c0203 --- /dev/null +++ b/src/kernel/qfont.h @@ -0,0 +1,372 @@ +/**************************************************************************** +** +** Definition of TQFont class +** +** Created : 940514 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQFONT_H +#define TQFONT_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qstring.h" +#endif // QT_H + + +class TQFontPrivate; /* don't touch */ +class TQStringList; +class TQTextFormatCollection; + +class Q_EXPORT TQFont +{ +public: + enum StyleHint { + Helvetica, SansSerif = Helvetica, + Times, Serif = Times, + Courier, TypeWriter = Courier, + OldEnglish, Decorative = OldEnglish, + System, + AnyStyle + }; + + enum StyleStrategy { + PreferDefault = 0x0001, + PreferBitmap = 0x0002, + PreferDevice = 0x0004, + PreferOutline = 0x0008, + ForceOutline = 0x0010, + PreferMatch = 0x0020, + PreferQuality = 0x0040, + PreferAntialias = 0x0080, + NoAntialias = 0x0100, + OpenGLCompatible = 0x0200 + }; + + enum Weight { + Light = 25, + Normal = 50, + DemiBold = 63, + Bold = 75, + Black = 87 + }; + + enum Stretch { + UltraCondensed = 50, + ExtraCondensed = 62, + Condensed = 75, + SemiCondensed = 87, + Unstretched = 100, + SemiExpanded = 112, + Expanded = 125, + ExtraExpanded = 150, + UltraExpanded = 200 + }; + + // default font + TQFont(); + // specific font +#ifdef Q_QDOC + TQFont( const TQString &family, int pointSize = 12, int weight = Normal, + bool italic = FALSE ); +#else + TQFont( const TQString &family, int pointSize = -1, int weight = -1, + bool italic = FALSE ); +#endif + // copy constructor + TQFont( const TQFont & ); + + ~TQFont(); + + TQString family() const; + void setFamily( const TQString &); + + int pointSize() const; + float pointSizeFloat() const; + void setPointSize( int ); + void setPointSizeFloat( float ); + + int pixelSize() const; + void setPixelSize( int ); + void setPixelSizeFloat( float ); + + int weight() const; + void setWeight( int ); + + bool bold() const; + void setBold( bool ); + + bool italic() const; + void setItalic( bool ); + + bool underline() const; + void setUnderline( bool ); + + bool overline() const; + void setOverline( bool ); + + bool strikeOut() const; + void setStrikeOut( bool ); + + bool fixedPitch() const; + void setFixedPitch( bool ); + + StyleHint styleHint() const; + StyleStrategy styleStrategy() const; + void setStyleHint( StyleHint, StyleStrategy = PreferDefault ); + void setStyleStrategy( StyleStrategy s ); + + int stretch() const; + void setStretch( int ); + + // is raw mode still needed? + bool rawMode() const; + void setRawMode( bool ); + + // dupicated from TQFontInfo + bool exactMatch() const; + + TQFont &operator=( const TQFont & ); + bool operator==( const TQFont & ) const; + bool operator!=( const TQFont & ) const; + bool isCopyOf( const TQFont & ) const; + + +#ifdef Q_WS_WIN + HFONT handle() const; +#else // !Q_WS_WIN + TQt::HANDLE handle() const; +#endif // Q_WS_WIN + + + // needed for X11 + void setRawName( const TQString & ); + TQString rawName() const; + + TQString key() const; + + TQString toString() const; + bool fromString(const TQString &); + +#ifndef QT_NO_STRINGLIST + static TQString substitute(const TQString &); + static TQStringList substitutes(const TQString &); + static TQStringList substitutions(); + static void insertSubstitution(const TQString&, const TQString &); + static void insertSubstitutions(const TQString&, const TQStringList &); + static void removeSubstitution(const TQString &); +#endif //QT_NO_STRINGLIST + static void initialize(); + static void cleanup(); +#ifndef Q_WS_QWS + static void cacheStatistics(); +#endif + +#if defined(Q_WS_QWS) + void qwsRenderToDisk(bool all=TRUE); +#endif + + + // a copy of this lives in qunicodetables.cpp, as we can't include + // qfont.h it in tools/. Do not modify without changing the script + // enum in qunicodetable_p.h aswell. + enum Script { + // European Alphabetic Scripts + Latin, + Greek, + Cyrillic, + Armenian, + Georgian, + Runic, + Ogham, + SpacingModifiers, + CombiningMarks, + + // Middle Eastern Scripts + Hebrew, + Arabic, + Syriac, + Thaana, + + // South and Southeast Asian Scripts + Devanagari, + Bengali, + Gurmukhi, + Gujarati, + Oriya, + Tamil, + Telugu, + Kannada, + Malayalam, + Sinhala, + Thai, + Lao, + Tibetan, + Myanmar, + Khmer, + + // East Asian Scripts + Han, + Hiragana, + Katakana, + Hangul, + Bopomofo, + Yi, + + // Additional Scripts + Ethiopic, + Cherokee, + CanadianAboriginal, + Mongolian, + + // Symbols + CurrencySymbols, + LetterlikeSymbols, + NumberForms, + MathematicalOperators, + TechnicalSymbols, + GeometricSymbols, + MiscellaneousSymbols, + EnclosedAndSquare, + Braille, + + Unicode, + + // some scripts added in Unicode 3.2 + Tagalog, + Hanunoo, + Buhid, + Tagbanwa, + + KatakanaHalfWidth, + + // from Unicode 4.0 + Limbu, + TaiLe, + + // End +#if !defined(Q_QDOC) + NScripts, + UnknownScript = NScripts, + + NoScript, + + // ---------------------------------------- + // Dear User, you can see values > NScript, + // but they are internal - do not touch. + + Han_Japanese, + Han_SimplifiedChinese, + Han_TraditionalChinese, + Han_Korean, + + LastPrivateScript +#endif + }; + + TQString defaultFamily() const; + TQString lastResortFamily() const; + TQString lastResortFont() const; + +#ifndef QT_NO_COMPAT + + static TQFont defaultFont(); + static void setDefaultFont( const TQFont & ); + +#endif // QT_NO_COMPAT + + TQFont resolve( const TQFont & ) const; + +protected: + // why protected? + bool dirty() const; + int deciPointSize() const; + +private: + TQFont( TQFontPrivate *, TQPaintDevice *pd ); + + void detach(); + +#if defined(Q_WS_MAC) + void macSetFont(TQPaintDevice *); +#elif defined(Q_WS_X11) + void x11SetScreen( int screen = -1 ); + int x11Screen() const; +#endif + + friend class TQFontMetrics; + friend class TQFontInfo; + friend class TQPainter; + friend class TQPSPrinterFont; + friend class TQApplication; + friend class TQWidget; + friend class TQTextFormatCollection; + friend class TQTextLayout; + friend class TQTextItem; + friend class TQGLContext; +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + friend TQt::HANDLE qt_xft_handle(const TQFont &font); +#endif +#ifndef QT_NO_DATASTREAM + friend Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQFont & ); + friend Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQFont & ); +#endif + + TQFontPrivate *d; +}; + + +inline bool TQFont::bold() const +{ return weight() > Normal; } + + +inline void TQFont::setBold( bool enable ) +{ setWeight( enable ? Bold : Normal ); } + + + + +/***************************************************************************** + TQFont stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQFont & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQFont & ); +#endif + + +#endif // TQFONT_H diff --git a/src/kernel/qfont_x11.cpp b/src/kernel/qfont_x11.cpp new file mode 100644 index 000000000..4b91ac134 --- /dev/null +++ b/src/kernel/qfont_x11.cpp @@ -0,0 +1,737 @@ +/**************************************************************************** +** +** Implementation of TQFont, TQFontMetrics and TQFontInfo classes for X11 +** +** Created : 940515 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#define QT_FATAL_ASSERT + +// REVISED: brad + +#include "qplatformdefs.h" + +#include "qfont.h" +#include "qapplication.h" +#include "qcleanuphandler.h" +#include "qfontinfo.h" +#include "qfontdatabase.h" +#include "qfontmetrics.h" +#include "qpaintdevice.h" +#include "qpaintdevicemetrics.h" +#include "qtextcodec.h" + +#include +#include +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include "qtextengine_p.h" + +#include "qt_x11_p.h" + +#include +#include +#include + +#define TQFONTLOADER_DEBUG +#define TQFONTLOADER_DEBUG_VERBOSE + +Q_EXPORT bool qt_has_xft = FALSE; + +#ifndef QT_NO_XFTFREETYPE +TQt::HANDLE qt_xft_handle(const TQFont &font) +{ + TQFontEngine *engine = font.d->engineForScript( TQFontPrivate::defaultScript ); + if (!engine->type() == TQFontEngine::Xft) + return 0; + return (long)static_cast(engine)->font(); +} +#endif + +double qt_pixelSize(double pointSize, TQPaintDevice *paintdevice, int scr) +{ + if (pointSize < 0) return -1.; + + double result = pointSize; + if (paintdevice && TQPaintDeviceMetrics( paintdevice ).logicalDpiY() != 75) + result *= TQPaintDeviceMetrics( paintdevice ).logicalDpiY() / 72.; + else if (TQPaintDevice::x11AppDpiY( scr ) != 75) + result *= TQPaintDevice::x11AppDpiY( scr ) / 72.; + + return result; +} + +double qt_pointSize(double pixelSize, TQPaintDevice *paintdevice, int scr) +{ + if (pixelSize < 0) return -1.; + + double result = pixelSize; + if ( paintdevice && TQPaintDeviceMetrics( paintdevice ).logicalDpiY() != 75) + result *= 72. / TQPaintDeviceMetrics( paintdevice ).logicalDpiY(); + else if (TQPaintDevice::x11AppDpiY(scr) != 75) + result *= 72. / TQPaintDevice::x11AppDpiY( scr ); + + return result; +} + +static inline double pixelSize( const TQFontDef &request, TQPaintDevice *paintdevice, + int scr ) +{ + return ((request.pointSize != -1) ? + qt_pixelSize(request.pointSize / 10., paintdevice, scr) : + (double)request.pixelSize); +} + +static inline double pointSize( const TQFontDef &request, TQPaintDevice *paintdevice, + int scr ) +{ + return ((request.pixelSize != -1) ? + qt_pointSize(request.pixelSize, paintdevice, scr) * 10.: + (double)request.pointSize); +} + + +/* + Removes wildcards from an XLFD. + + Returns \a xlfd with all wildcards removed if a match for \a xlfd is + found, otherwise it returns \a xlfd. +*/ +static TQCString qt_fixXLFD( const TQCString &xlfd ) +{ + TQCString ret = xlfd; + int count = 0; + char **fontNames = + XListFonts( TQPaintDevice::x11AppDisplay(), xlfd, 32768, &count ); + if ( count > 0 ) + ret = fontNames[0]; + XFreeFontNames( fontNames ); + return ret ; +} + +typedef TQMap FallbackMap; +static FallbackMap *fallbackMap = 0; +static TQSingleCleanupHandler qt_fallback_font_family_cleanup; + +static void ensure_fallback_map() +{ + if ( fallbackMap ) return; + fallbackMap = new FallbackMap; + qt_fallback_font_family_cleanup.set( &fallbackMap ); +} + +// Returns the user-configured fallback family for the specified script. +TQString qt_fallback_font_family( TQFont::Script script ) +{ + TQString ret; + + if ( fallbackMap ) { + FallbackMap::ConstIterator it, end = fallbackMap->end(); + it = fallbackMap->find( script ); + if ( it != end ) + ret = it.data(); + } + + return ret; +} + +// Sets the fallback family for the specified script. +void qt_set_fallback_font_family( TQFont::Script script, const TQString &family ) +{ + ensure_fallback_map(); + + if ( ! family.isEmpty() ) + fallbackMap->insert( script, family ); + else + fallbackMap->remove( script ); +} + + +TQFont::Script TQFontPrivate::defaultScript = TQFont::UnknownScript; +int TQFontPrivate::defaultEncodingID = -1; + +/*! + Internal function that initializes the font system. + + \internal + The font cache and font dict do not alloc the keys. The key is a TQString + which is shared between TQFontPrivate and TQXFontName. +*/ +void TQFont::initialize() +{ + // create global font cache + if ( ! TQFontCache::instance ) (void) new TQFontCache; + +#ifndef QT_NO_CODECS +#ifndef QT_NO_BIG_CODECS + static bool codecs_once = FALSE; + if ( ! codecs_once ) { + (void) new TQFontJis0201Codec; + (void) new TQFontJis0208Codec; + (void) new TQFontKsc5601Codec; + (void) new TQFontGb2312Codec; + (void) new TQFontGbkCodec; + (void) new TQFontGb18030_0Codec; + (void) new TQFontBig5Codec; + (void) new TQFontBig5hkscsCodec; + (void) new TQFontLaoCodec; + codecs_once = TRUE; + } +#endif // QT_NO_BIG_CODECS +#endif // QT_NO_CODECS + + extern int qt_encoding_id_for_mib( int mib ); // from qfontdatabase_x11.cpp + TQTextCodec *codec = TQTextCodec::codecForLocale(); + // determine the default encoding id using the locale, otherwise + // fallback to latin1 ( mib == 4 ) + int mib = codec ? codec->mibEnum() : 4; + + // for asian locales, use the mib for the font codec instead of the locale codec + switch (mib) { + case 38: // eucKR + mib = 36; + break; + + case 2025: // GB2312 + mib = 57; + break; + + case 113: // GBK + mib = -113; + break; + + case 114: // GB18030 + mib = -114; + break; + + case 2026: // Big5 + mib = -2026; + break; + + case 2101: // Big5-HKSCS + mib = -2101; + break; + + case 16: // JIS7 + mib = 15; + break; + + case 17: // SJIS + case 18: // eucJP + mib = 63; + break; + } + + // get the default encoding id for the locale encoding... + TQFontPrivate::defaultEncodingID = qt_encoding_id_for_mib( mib ); + + // get some sample text based on the users locale. we use this to determine the + // default script for the font system + TQCString oldlctime = setlocale(LC_TIME, 0); + TQCString lctime = setlocale(LC_TIME, ""); + + time_t ttmp = time(0); + struct tm *tt = 0; + char samp[64]; + TQString sample; + + if ( ttmp != -1 ) { +#if defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) + // use the reentrant versions of localtime() where available + tm res; + tt = localtime_r( &ttmp, &res ); +#else + tt = localtime( &ttmp ); +#endif // QT_THREAD_SUPPORT && _POSIX_THREAD_SAFE_FUNCTIONS + + if ( tt != 0 && strftime( samp, 64, "%A%B", tt ) > 0 ) + if ( codec ) + sample = codec->toUnicode( samp ); + } + + if ( ! sample.isNull() && ! sample.isEmpty() ) { + TQFont::Script cs = TQFont::NoScript, tmp; + const TQChar *uc = sample.unicode(); + TQFontPrivate *priv = new TQFontPrivate; + + for ( uint i = 0; i < sample.length(); i++ ) { + SCRIPT_FOR_CHAR( tmp, *uc ); + uc++; + if ( tmp != cs && tmp != TQFont::UnknownScript ) { + cs = tmp; + break; + } + } + delete priv; + + if ( cs != TQFont::UnknownScript ) + TQFontPrivate::defaultScript = cs; + } + + setlocale( LC_TIME, oldlctime.data() ); +} + +/*! \internal + + Internal function that cleans up the font system. +*/ +void TQFont::cleanup() +{ + // delete the global font cache + delete TQFontCache::instance; +} + +/*! + \internal + X11 Only: Returns the screen with which this font is associated. +*/ +int TQFont::x11Screen() const +{ + return d->screen; +} + +/*! \internal + X11 Only: Associate the font with the specified \a screen. +*/ +void TQFont::x11SetScreen( int screen ) +{ + if ( screen < 0 ) // assume default + screen = TQPaintDevice::x11AppScreen(); + + if ( screen == d->screen ) + return; // nothing to do + + detach(); + d->screen = screen; +} + +/*! \internal + Returns a TQFontEngine for the specified \a script that matches the + TQFontDef \e request member variable. +*/ +void TQFontPrivate::load( TQFont::Script script ) +{ + // NOTE: the X11 and Windows implementations of this function are + // identical... if you change one, change both. + +#ifdef QT_CHECK_STATE + // sanity checks + if (!TQFontCache::instance) + qWarning("Must construct a TQApplication before a TQFont"); + Q_ASSERT( script >= 0 && script < TQFont::LastPrivateScript ); +#endif // QT_CHECK_STATE + + TQFontDef req = request; + req.pixelSize = qRound(pixelSize(req, paintdevice, screen)); + req.pointSize = 0; + + if ( ! engineData ) { + TQFontCache::Key key( req, TQFont::NoScript, screen, paintdevice ); + + // look for the requested font in the engine data cache + engineData = TQFontCache::instance->findEngineData( key ); + + if ( ! engineData ) { + // create a new one + engineData = new TQFontEngineData; + TQFontCache::instance->insertEngineData( key, engineData ); + } else { + engineData->ref(); + } + } + + // the cached engineData could have already loaded the engine we want + if ( engineData->engines[script] ) return; + + // load the font + TQFontEngine *engine = 0; + // double scale = 1.0; // ### TODO: fix the scale calculations + + // list of families to try + TQStringList family_list; + + if (!req.family.isEmpty()) { + family_list = TQStringList::split( ',', req.family ); + + // append the substitute list for each family in family_list + TQStringList subs_list; + TQStringList::ConstIterator it = family_list.begin(), end = family_list.end(); + for ( ; it != end; ++it ) + subs_list += TQFont::substitutes( *it ); + family_list += subs_list; + +#ifndef QT_XFT2 + // with Xft2, we want to use fontconfig to determine better fallbacks, + // otherwise we might run into trouble with default fonts as "serif" + + // append the default fallback font for the specified script + TQString fallback = qt_fallback_font_family( script ); + if ( ! fallback.isEmpty() && ! family_list.contains( fallback ) ) + family_list << fallback; + + // add the default family + TQString defaultFamily = TQApplication::font().family(); + if ( ! family_list.contains( defaultFamily ) ) + family_list << defaultFamily; + + // add TQFont::defaultFamily() to the list, for compatibility with + // previous versions + family_list << TQApplication::font().defaultFamily(); +#endif // QT_XFT2 + } + + // null family means find the first font matching the specified script + family_list << TQString::null; + + TQStringList::ConstIterator it = family_list.begin(), end = family_list.end(); + for ( ; ! engine && it != end; ++it ) { + req.family = *it; + + engine = TQFontDatabase::findFont( script, this, req ); + if ( engine ) { + if ( engine->type() != TQFontEngine::Box ) + break; + + if ( ! req.family.isEmpty() ) + engine = 0; + + continue; + } + } + + engine->ref(); + engineData->engines[script] = engine; +} + +/*! + Returns TRUE if the font attributes have been changed and the font + has to be (re)loaded; otherwise returns FALSE. +*/ +bool TQFont::dirty() const +{ + return d->engineData == 0; +} + +/*! + Returns the window system handle to the font, for low-level + access. Using this function is \e not portable. +*/ +TQt::HANDLE TQFont::handle() const +{ + TQFontEngine *engine = d->engineForScript( TQFontPrivate::defaultScript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + switch ( engine->type() ) { + case TQFontEngine::XLFD: + return ((TQFontEngineXLFD *) engine)->handle(); + case TQFontEngine::LatinXLFD: + return ((TQFontEngineLatinXLFD *) engine)->handle(); + + default: break; + } + return 0; +} + +/*! + Returns the name of the font within the underlying window system. + + On Windows, this is usually just the family name of a TrueType + font. + + On X11, it is an XLFD (X Logical Font Description). When TQt is + build with Xft support on X11, the return value can be an Xft + pattern or an XLFD. + + Using the return value of this function is usually \e not \e + portable. + + \sa setRawName() +*/ +TQString TQFont::rawName() const +{ + TQFontEngine *engine = d->engineForScript( TQFontPrivate::defaultScript ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + return TQString::fromLatin1( engine->name() ); +} + +/*! + Sets a font by its system specific name. The function is + particularly useful under X, where system font settings (for + example X resources) are usually available in XLFD (X Logical Font + Description) form only. You can pass an XLFD as \a name to this + function. + + A font set with setRawName() is still a full-featured TQFont. It can + be queried (for example with italic()) or modified (for example with + setItalic()) and is therefore also suitable for rendering rich text. + + If TQt's internal font database cannot resolve the raw name, the + font becomes a raw font with \a name as its family. + + Note that the present implementation does not handle wildcards in + XLFDs well, and that font aliases (file \c fonts.alias in the font + directory on X11) are not supported. + + \sa rawName(), setRawMode(), setFamily() +*/ +void TQFont::setRawName( const TQString &name ) +{ + detach(); + + // from qfontdatabase_x11.cpp + extern bool qt_fillFontDef( const TQCString &xlfd, TQFontDef *fd, int screen ); + + if ( ! qt_fillFontDef( qt_fixXLFD( name.latin1() ), &d->request, d->screen ) ) { +#ifdef QT_CHECK_STATE + qWarning("TQFont::setRawName(): Invalid XLFD: \"%s\"", name.latin1()); +#endif // QT_CHECK_STATE + + setFamily( name ); + setRawMode( TRUE ); + } else { + d->mask = TQFontPrivate::Complete; + } +} + +/*! + Returns the "last resort" font family name. + + The current implementation tries a wide variety of common fonts, + returning the first one it finds. Is is possible that no family is + found in which case a null string is returned. + + \sa lastResortFont() +*/ +TQString TQFont::lastResortFamily() const +{ + return TQString::fromLatin1( "Helvetica" ); +} + +/*! + Returns the family name that corresponds to the current style + hint. + + \sa StyleHint styleHint() setStyleHint() +*/ +TQString TQFont::defaultFamily() const +{ + switch ( d->request.styleHint ) { + case TQFont::Times: + return TQString::fromLatin1( "Times" ); + + case TQFont::Courier: + return TQString::fromLatin1( "Courier" ); + + case TQFont::Decorative: + return TQString::fromLatin1( "Old English" ); + + case TQFont::Helvetica: + case TQFont::System: + default: + return TQString::fromLatin1( "Helvetica" ); + } +} + +/* + Returns a last resort raw font name for the font matching algorithm. + This is used if even the last resort family is not available. It + returns \e something, almost no matter what. The current + implementation tries a wide variety of common fonts, returning the + first one it finds. The implementation may change at any time. +*/ +static const char * const tryFonts[] = { + "-*-helvetica-medium-r-*-*-*-120-*-*-*-*-*-*", + "-*-courier-medium-r-*-*-*-120-*-*-*-*-*-*", + "-*-times-medium-r-*-*-*-120-*-*-*-*-*-*", + "-*-lucida-medium-r-*-*-*-120-*-*-*-*-*-*", + "-*-helvetica-*-*-*-*-*-120-*-*-*-*-*-*", + "-*-courier-*-*-*-*-*-120-*-*-*-*-*-*", + "-*-times-*-*-*-*-*-120-*-*-*-*-*-*", + "-*-lucida-*-*-*-*-*-120-*-*-*-*-*-*", + "-*-helvetica-*-*-*-*-*-*-*-*-*-*-*-*", + "-*-courier-*-*-*-*-*-*-*-*-*-*-*-*", + "-*-times-*-*-*-*-*-*-*-*-*-*-*-*", + "-*-lucida-*-*-*-*-*-*-*-*-*-*-*-*", + "-*-fixed-*-*-*-*-*-*-*-*-*-*-*-*", + "6x13", + "7x13", + "8x13", + "9x15", + "fixed", + 0 +}; + +// Returns TRUE if the font exists, FALSE otherwise +static bool fontExists( const TQString &fontName ) +{ + int count; + char **fontNames = XListFonts( TQPaintDevice::x11AppDisplay(), + (char*)fontName.latin1(), 32768, &count ); + if ( fontNames ) XFreeFontNames( fontNames ); + + return count != 0; +} + +/*! + Returns a "last resort" font name for the font matching algorithm. + This is used if the last resort family is not available. It will + always return a name, if necessary returning something like + "fixed" or "system". + + The current implementation tries a wide variety of common fonts, + returning the first one it finds. The implementation may change + at any time, but this function will always return a string + containing something. + + It is theoretically possible that there really isn't a + lastResortFont() in which case TQt will abort with an error + message. We have not been able to identify a case where this + happens. Please \link bughowto.html report it as a bug\endlink if + it does, preferably with a list of the fonts you have installed. + + \sa lastResortFamily() rawName() +*/ +TQString TQFont::lastResortFont() const +{ + static TQString last; + + // already found + if ( ! last.isNull() ) + return last; + + int i = 0; + const char* f; + + while ( ( f = tryFonts[i] ) ) { + last = TQString::fromLatin1( f ); + + if ( fontExists( last ) ) + return last; + + i++; + } + +#if defined(CHECK_NULL) + qFatal( "TQFontPrivate::lastResortFont: Cannot find any reasonable font" ); +#endif + + return last; +} + + + + +// ********************************************************************** +// TQFontMetrics member methods +// ********************************************************************** + +int TQFontMetrics::width( TQChar ch ) const +{ + unsigned short uc = ch.unicode(); + if ( uc < TQFontEngineData::widthCacheSize && + d->engineData && d->engineData->widthCache[ uc ] ) + return d->engineData->widthCache[ uc ]; + + if ( ::category( ch ) == TQChar::Mark_NonSpacing || qIsZeroWidthChar(ch.unicode())) + return 0; + + TQFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + TQFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + glyph_t glyphs[8]; + advance_t advances[8]; + int nglyphs = 7; + engine->stringToCMap( &ch, 1, glyphs, advances, &nglyphs, FALSE ); + + // ### can nglyphs != 1 happen at all? Not currently I think + if ( uc < TQFontEngineData::widthCacheSize && advances[0] > 0 && advances[0] < 0x100 ) + d->engineData->widthCache[ uc ] = advances[0]; + + return advances[0]; +} + + +int TQFontMetrics::charWidth( const TQString &str, int pos ) const +{ + if ( pos < 0 || pos > (int)str.length() ) + return 0; + + const TQChar &ch = str.unicode()[ pos ]; + if ( ch.unicode() < TQFontEngineData::widthCacheSize && + d->engineData && d->engineData->widthCache[ ch.unicode() ] ) + return d->engineData->widthCache[ ch.unicode() ]; + + TQFont::Script script; + SCRIPT_FOR_CHAR( script, ch ); + + int width; + + if ( script >= TQFont::Arabic && script <= TQFont::Khmer ) { + // complex script shaping. Have to do some hard work + int from = TQMAX( 0, pos - 8 ); + int to = TQMIN( (int)str.length(), pos + 8 ); + TQConstString cstr( str.unicode()+from, to-from); + TQTextEngine layout( cstr.string(), d ); + layout.itemize( TQTextEngine::WidthOnly ); + width = layout.width( pos-from, 1 ); + } else if ( ::category( ch ) == TQChar::Mark_NonSpacing || qIsZeroWidthChar(ch.unicode())) { + width = 0; + } else { + TQFontEngine *engine = d->engineForScript( script ); +#ifdef QT_CHECK_STATE + Q_ASSERT( engine != 0 ); +#endif // QT_CHECK_STATE + + glyph_t glyphs[8]; + advance_t advances[8]; + int nglyphs = 7; + engine->stringToCMap( &ch, 1, glyphs, advances, &nglyphs, FALSE ); + width = advances[0]; + } + if ( ch.unicode() < TQFontEngineData::widthCacheSize && width > 0 && width < 0x100 ) + d->engineData->widthCache[ ch.unicode() ] = width; + return width; +} diff --git a/src/kernel/qfontdata_p.h b/src/kernel/qfontdata_p.h new file mode 100644 index 000000000..55714b15c --- /dev/null +++ b/src/kernel/qfontdata_p.h @@ -0,0 +1,278 @@ +/**************************************************************************** +** +** Definition of internal TQFontData struct +** +** Created : 941229 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQFONTDATA_P_H +#define TQFONTDATA_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of internal files. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// +// + +#include "qobject.h" +#include "qfont.h" +#include "qpaintdevicemetrics.h" + +// forwards +class TQFontEngine; +class TQPaintDevice; + + +struct TQFontDef +{ + inline TQFontDef() + : pointSize( -1 ), pixelSize( -1 ), + styleHint( TQFont::AnyStyle ), styleStrategy( TQFont::PreferDefault ), + weight( 50 ), italic( FALSE ), fixedPitch( FALSE ), stretch( 100 ), + ignorePitch(TRUE) +#ifdef Q_WS_MAC + ,fixedPitchComputed(FALSE) +#endif + { + } + + TQString family; + +#ifdef Q_WS_X11 + TQString addStyle; +#endif // Q_WS_X11 + + int pointSize; + int pixelSize; + + uint styleHint : 8; + uint styleStrategy : 16; + + uint weight : 7; // 0-99 + uint italic : 1; + uint fixedPitch : 1; + uint stretch : 12; // 0-400 + + uint ignorePitch : 1; + uint fixedPitchComputed : 1; // for Mac OS X only + uint reserved : 14; // for future extensions + + bool operator==( const TQFontDef &other ) const; + inline bool operator<( const TQFontDef &other ) const + { + if ( pixelSize != other.pixelSize ) return pixelSize < other.pixelSize; + if ( weight != other.weight ) return weight < other.weight; + if ( italic != other.italic ) return italic < other.italic; + if ( stretch != other.stretch ) return stretch < other.stretch; + if ( styleHint != other.styleHint ) return styleHint < other.styleHint; + if ( styleStrategy != other.styleStrategy ) return styleStrategy < other.styleStrategy; + if ( family != other.family ) return family < other.family; + +#ifdef Q_WS_X11 + if ( addStyle != other.addStyle ) return addStyle < other.addStyle; +#endif // Q_WS_X11 + + return FALSE; + } +}; + +class TQFontEngineData : public TQShared +{ +public: + TQFontEngineData(); + ~TQFontEngineData(); + + uint lineWidth; + +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + TQFontEngine *engines[TQFont::LastPrivateScript]; +#else + TQFontEngine *engine; +#endif // Q_WS_X11 || Q_WS_WIN +#ifndef Q_WS_MAC + enum { widthCacheSize = 0x500 }; + uchar widthCache[widthCacheSize]; +#endif +}; + + +class TQFontPrivate : public TQShared +{ +public: + static TQFont::Script defaultScript; +#ifdef Q_WS_X11 + static int defaultEncodingID; +#endif // Q_WS_X11 + + TQFontPrivate(); + TQFontPrivate( const TQFontPrivate &other ); + ~TQFontPrivate(); + + void load( TQFont::Script script ); + TQFontEngine *engineForScript( TQFont::Script script ) const { + if ( script == TQFont::NoScript ) + script = TQFontPrivate::defaultScript; +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + if ( ! engineData || ! engineData->engines[script] ) + ((TQFontPrivate *) this)->load( script ); + return engineData->engines[script]; +#else + if ( ! engineData || ! engineData->engine ) + ((TQFontPrivate *) this)->load( script ); + return engineData->engine; +#endif // Q_WS_X11 || Q_WS_WIN + } + + TQFontDef request; + TQFontEngineData *engineData; + TQPaintDevice *paintdevice; + int screen; + + uint rawMode : 1; + uint underline : 1; + uint overline : 1; + uint strikeOut : 1; + + enum { + Family = 0x0001, + Size = 0x0002, + StyleHint = 0x0004, + StyleStrategy = 0x0008, + Weight = 0x0010, + Italic = 0x0020, + Underline = 0x0040, + Overline = 0x0080, + StrikeOut = 0x0100, + FixedPitch = 0x0200, + Stretch = 0x0400, + Complete = 0x07ff + }; + + uint mask; + + void resolve( const TQFontPrivate *other ); +}; + + +class TQFontCache : public TQObject +{ +public: + static TQFontCache *instance; + + TQFontCache(); + ~TQFontCache(); + +#ifdef Q_WS_QWS + void clear(); +#endif + // universal key structure. TQFontEngineDatas and TQFontEngines are cached using + // the same keys + struct Key { + Key() : script(0), screen( 0 ), dpi(0) { } + Key( const TQFontDef &d, TQFont::Script c, int s, TQPaintDevice *pdev ) + : script(c), screen(s) { + def = d; +#ifdef Q_WS_X11 + dpi = pdev ? TQPaintDeviceMetrics(pdev).logicalDpiY() : 0; +#else + Q_UNUSED(pdev); + dpi = 0; +#endif + } + + TQFontDef def; + int script; + int screen; + int dpi; + + inline bool operator<( const Key &other ) const + { + if ( script != other.script ) return script < other.script; + if ( screen != other.screen ) return screen < other.screen; + if ( dpi != other.dpi ) return dpi < other.dpi; + return def < other.def; + } + inline bool operator==( const Key &other ) const + { return def == other.def && script == other.script && + screen == other.screen && dpi == other.dpi; } + }; + + // TQFontEngineData cache + typedef TQMap EngineDataCache; + EngineDataCache engineDataCache; + + TQFontEngineData *findEngineData( const Key &key ) const; + void insertEngineData( const Key &key, TQFontEngineData *engineData ); + + // TQFontEngine cache + struct Engine { + Engine() : data( 0 ), timestamp( 0 ), hits( 0 ) { } + Engine( TQFontEngine *d ) : data( d ), timestamp( 0 ), hits( 0 ) { } + + TQFontEngine *data; + uint timestamp; + uint hits; + }; + + typedef TQMap EngineCache; + EngineCache engineCache; + + TQFontEngine *findEngine( const Key &key ); + void insertEngine( const Key &key, TQFontEngine *engine ); + +#if defined(Q_WS_WIN) || defined(Q_WS_QWS) + void cleanupPrinterFonts(); +#endif + + private: + void increaseCost( uint cost ); + void decreaseCost( uint cost ); + void timerEvent( TQTimerEvent *event ); + + static const uint min_cost; + uint total_cost, max_cost; + uint current_timestamp; + bool fast; + int timer_id; +}; + +#endif // TQFONTDATA_P_H diff --git a/src/kernel/qfontdatabase.cpp b/src/kernel/qfontdatabase.cpp new file mode 100644 index 000000000..70e24b2e0 --- /dev/null +++ b/src/kernel/qfontdatabase.cpp @@ -0,0 +1,2491 @@ +/**************************************************************************** +** +** Implementation of font database class. +** +** Created : 990603 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qfontdatabase.h" + +#ifndef QT_NO_FONTDATABASE + +#include +#include + +#include +#include "qfontengine_p.h" + +#include + +#ifdef Q_WS_X11 +#include +#endif +#include + +//#define TQFONTDATABASE_DEBUG +#ifdef TQFONTDATABASE_DEBUG +# define FD_DEBUG qDebug +#else +# define FD_DEBUG if (FALSE) qDebug +#endif + +//#define FONT_MATCH_DEBUG +#ifdef FONT_MATCH_DEBUG +# define FM_DEBUG qDebug +#else +# define FM_DEBUG if (FALSE) qDebug +#endif + +#if defined(Q_CC_MSVC) && !defined(Q_CC_MSVC_NET) +# define for if(0){}else for +#endif + +static int ucstricmp( const TQString &as, const TQString &bs ) +{ + const TQChar *a = as.unicode(); + const TQChar *b = bs.unicode(); + if ( a == b ) + return 0; + if ( a == 0 ) + return 1; + if ( b == 0 ) + return -1; + int l=TQMIN(as.length(),bs.length()); + while ( l-- && ::lower( *a ) == ::lower( *b ) ) + a++,b++; + if ( l==-1 ) + return ( as.length()-bs.length() ); + return ::lower( *a ).unicode() - ::lower( *b ).unicode(); +} + +static int getFontWeight( const TQString &weightString ) +{ + TQString s = weightString.lower(); + + // Test in decreasing order of commonness + if (s == "medium" || + s == "normal") + return TQFont::Normal; + if (s == "bold") + return TQFont::Bold; + if (s == "demibold" || s == "demi bold") + return TQFont::DemiBold; + if (s == "black") + return TQFont::Black; + if (s == "light") + return TQFont::Light; + + if (s.contains("bold")) { + if (s.contains("demi")) + return (int) TQFont::DemiBold; + return (int) TQFont::Bold; + } + + if (s.contains("light")) + return (int) TQFont::Light; + + if (s.contains("black")) + return (int) TQFont::Black; + + return (int) TQFont::Normal; +} + +#ifdef Q_WS_X11 +struct TQtFontEncoding +{ + signed int encoding : 16; + + uint xpoint : 16; + uint xres : 8; + uint yres : 8; + uint avgwidth : 16; + uchar pitch : 8; +}; +#endif // Q_WS_X11 + +struct TQtFontSize +{ + unsigned short pixelSize; + +#ifdef Q_WS_X11 + int count; + TQtFontEncoding *encodings; + TQtFontEncoding *encodingID( int id, uint xpoint = 0, uint xres = 0, + uint yres = 0, uint avgwidth = 0, bool add = FALSE); +#endif // Q_WS_X11 +}; + + +#ifdef Q_WS_X11 +TQtFontEncoding *TQtFontSize::encodingID( int id, uint xpoint, uint xres, + uint yres, uint avgwidth, bool add ) +{ + // we don't match using the xpoint, xres and yres parameters, only the id + for ( int i = 0; i < count; ++i ) { + if ( encodings[i].encoding == id ) + return encodings + i; + } + + if ( !add ) return 0; + + if ( !(count % 4) ) + encodings = ( TQtFontEncoding * ) + realloc( encodings, + (((count+4) >> 2 ) << 2 ) * sizeof( TQtFontEncoding ) ); + encodings[count].encoding = id; + encodings[count].xpoint = xpoint; + encodings[count].xres = xres; + encodings[count].yres = yres; + encodings[count].avgwidth = avgwidth; + encodings[count].pitch = '*'; + return encodings + count++; +} +#endif // Q_WS_X11 + +struct TQtFontStyle +{ + struct Key { + Key( const TQString &styleString ); + Key() : italic( FALSE ), oblique( FALSE ), + weight( TQFont::Normal ), stretch( 0 ) { } + Key( const Key &o ) : italic( o.italic ), oblique( o.oblique ), + weight( o.weight ), stretch( o.stretch ) { } + uint italic : 1; + uint oblique : 1; + signed int weight : 8; + signed int stretch : 12; + + bool operator==( const Key & other ) { + return ( italic == other.italic && + oblique == other.oblique && + weight == other.weight && + (stretch == 0 || other.stretch == 0 || stretch == other.stretch) ); + } + bool operator!=( const Key &other ) { + return !operator==(other); + } + bool operator <( const Key &o ) { + int x = (italic << 13) + (oblique << 12) + (weight << 14) + stretch; + int y = (o.italic << 13) + (o.oblique << 12) + (o.weight << 14) + o.stretch; + return ( x < y ); + } + }; + + TQtFontStyle( const Key &k ) + : key( k ), bitmapScalable( FALSE ), smoothScalable( FALSE ), + fakeOblique( FALSE ), count( 0 ), pixelSizes( 0 ) + { +#if defined(Q_WS_X11) + weightName = setwidthName = 0; +#endif // Q_WS_X11 + } + + ~TQtFontStyle() { +#ifdef Q_WS_X11 + delete [] weightName; + delete [] setwidthName; + while ( count-- ) + free(pixelSizes[count].encodings); +#endif + free( pixelSizes ); + } + + Key key; + bool bitmapScalable : 1; + bool smoothScalable : 1; + bool fakeOblique : 1; + int count : 29; + TQtFontSize *pixelSizes; + +#ifdef Q_WS_X11 + const char *weightName; + const char *setwidthName; +#endif // Q_WS_X11 + + TQtFontSize *pixelSize( unsigned short size, bool = FALSE ); +}; + +TQtFontStyle::Key::Key( const TQString &styleString ) + : italic( FALSE ), oblique( FALSE ), weight( TQFont::Normal ), stretch( 0 ) +{ + weight = getFontWeight( styleString ); + + if ( styleString.contains( "Italic" ) ) + italic = TRUE; + else if ( styleString.contains( "Oblique" ) ) + oblique = TRUE; +} + +TQtFontSize *TQtFontStyle::pixelSize( unsigned short size, bool add ) +{ + for ( int i = 0; i < count; i++ ) { + if ( pixelSizes[i].pixelSize == size ) + return pixelSizes + i; + } + if ( !add ) + return 0; + + if ( !(count % 8) ) + pixelSizes = (TQtFontSize *) + realloc( pixelSizes, + (((count+8) >> 3 ) << 3) * sizeof(TQtFontSize) ); + pixelSizes[count].pixelSize = size; +#ifdef Q_WS_X11 + pixelSizes[count].count = 0; + pixelSizes[count].encodings = 0; +#endif + return pixelSizes + (count++); +} + +struct TQtFontFoundry +{ + TQtFontFoundry( const TQString &n ) : name( n ), count( 0 ), styles( 0 ) {} + ~TQtFontFoundry() { + while ( count-- ) + delete styles[count]; + free( styles ); + } + + TQString name; + + int count; + TQtFontStyle **styles; + TQtFontStyle *style( const TQtFontStyle::Key &, bool = FALSE ); +}; + +TQtFontStyle *TQtFontFoundry::style( const TQtFontStyle::Key &key, bool create ) +{ + int pos = 0; + if ( count ) { + int low = 0; + int high = count; + pos = count / 2; + while ( high > low ) { + if ( styles[pos]->key == key ) + return styles[pos]; + if ( styles[pos]->key < key ) + low = pos + 1; + else + high = pos; + pos = (high + low) / 2; + }; + pos = low; + } + if ( !create ) + return 0; + +// qDebug("adding key (weight=%d, italic=%d, oblique=%d stretch=%d) at %d", key.weight, key.italic, key.oblique, key.stretch, pos ); + if ( !(count % 8) ) + styles = (TQtFontStyle **) + realloc( styles, (((count+8) >> 3 ) << 3) * sizeof( TQtFontStyle * ) ); + + memmove( styles + pos + 1, styles + pos, (count-pos)*sizeof(TQtFontStyle *) ); + styles[pos] = new TQtFontStyle( key ); + count++; + return styles[pos]; +} + + +struct TQtFontFamily +{ + enum ScriptStatus { Unknown = 0, Supported = 1, + UnSupported_Xft= 2, UnSupported_Xlfd = 4, UnSupported = 6 }; + + TQtFontFamily(const TQString &n ) + : +#ifdef Q_WS_X11 + fixedPitch( TRUE ), hasXft( FALSE ), xftScriptCheck( FALSE ), xlfdLoaded( FALSE ), synthetic(FALSE), +#else + fixedPitch( FALSE ), +#endif +#ifdef Q_WS_WIN + scriptCheck( FALSE ), +#endif +#if defined(Q_OS_MAC) && !defined(TQWS) + fixedPitchComputed(FALSE), +#endif + fullyLoaded( FALSE ), + name( n ), count( 0 ), foundries( 0 ) { + memset( scripts, 0, sizeof( scripts ) ); + } + ~TQtFontFamily() { + while ( count-- ) + delete foundries[count]; + free( foundries ); + } + + bool fixedPitch : 1; +#ifdef Q_WS_X11 + bool hasXft : 1; + bool xftScriptCheck : 1; + bool xlfdLoaded : 1; + bool synthetic : 1; +#endif +#ifdef Q_WS_WIN + bool scriptCheck : 1; +#endif +#if defined(Q_OS_MAC) && !defined(TQWS) + bool fixedPitchComputed : 1; +#endif + bool fullyLoaded : 1; + TQString name; + TQString rawName; +#ifdef Q_WS_X11 + TQCString fontFilename; + int fontFileIndex; +#endif +#ifdef Q_WS_MAC + FMFontFamily macFamily; +#endif +#ifdef Q_WS_WIN + TQString english_name; +#endif + int count; + TQtFontFoundry **foundries; + + unsigned char scripts[TQFont::LastPrivateScript]; + + TQtFontFoundry *foundry( const TQString &f, bool = FALSE ); +}; + +TQtFontFoundry *TQtFontFamily::foundry( const TQString &f, bool create ) +{ + if ( f.isNull() && count == 1 ) + return foundries[0]; + + for ( int i = 0; i < count; i++ ) { + if ( ucstricmp( foundries[i]->name, f ) == 0 ) + return foundries[i]; + } + if ( !create ) + return 0; + + if ( !(count % 8) ) + foundries = (TQtFontFoundry **) + realloc( foundries, + (((count+8) >> 3 ) << 3) * sizeof( TQtFontFoundry * ) ); + + foundries[count] = new TQtFontFoundry( f ); + return foundries[count++]; +} + +class TQFontDatabasePrivate { +public: + TQFontDatabasePrivate() : count( 0 ), families( 0 ) { } + ~TQFontDatabasePrivate() { + while ( count-- ) + delete families[count]; + free( families ); + } + TQtFontFamily *family( const TQString &f, bool = FALSE ); + + int count; + TQtFontFamily **families; +}; + +TQtFontFamily *TQFontDatabasePrivate::family( const TQString &f, bool create ) +{ + int low = 0; + int high = count; + int pos = count / 2; + int res = 1; + if ( count ) { + while ( (res = ucstricmp( families[pos]->name, f )) && pos != low ) { + if ( res > 0 ) + high = pos; + else + low = pos; + pos = (high + low) / 2; + }; + if ( !res ) + return families[pos]; + } + if ( !create ) + return 0; + + if ( res < 0 ) + pos++; + + // qDebug("adding family %s at %d total=%d", f.latin1(), pos, count); + if ( !(count % 8) ) + families = (TQtFontFamily **) + realloc( families, + (((count+8) >> 3 ) << 3) * sizeof( TQtFontFamily * ) ); + + memmove( families + pos + 1, families + pos, (count-pos)*sizeof(TQtFontFamily *) ); + families[pos] = new TQtFontFamily( f ); + count++; + return families[pos]; +} + + + + +#if defined(Q_WS_X11) || defined(Q_WS_WIN) +static const unsigned short sample_chars[TQFont::LastPrivateScript][14] = +{ + // European Alphabetic Scripts + // Latin, + { 0x0041, 0x0 }, + // Greek, + { 0x0391, 0x0 }, + // Cyrillic, + { 0x0410, 0x0 }, + // Armenian, + { 0x0540, 0x0 }, + // Georgian, + { 0x10d0, 0x0 }, + // Runic, + { 0x16a0, 0x0 }, + // Ogham, + { 0x1680, 0x0 }, + // SpacingModifiers, + { 0x02c6, 0x0 }, + // CombiningMarks, + { 0x0300, 0x0 }, + + // Middle Eastern Scripts + // Hebrew, + { 0x05d0, 0x0 }, + // Arabic, + { 0x0630, 0x0 }, + // Syriac, + { 0x0710, 0x0 }, + // Thaana, + { 0x0780, 0x0 }, + + // South and Southeast Asian Scripts + // Devanagari, + { 0x0910, 0x0 }, + // Bengali, + { 0x0990, 0x0 }, + // Gurmukhi, + { 0x0a10, 0x0 }, + // Gujarati, + { 0x0a90, 0x0 }, + // Oriya, + { 0x0b10, 0x0 }, + // Tamil, + { 0x0b90, 0x0 }, + // Telugu, + { 0x0c10, 0x0 }, + // Kannada, + { 0x0c90, 0x0 }, + // Malayalam, + { 0x0d10, 0x0 }, + // Sinhala, + { 0x0d90, 0x0 }, + // Thai, + { 0x0e10, 0x0 }, + // Lao, + { 0x0e81, 0x0 }, + // Tibetan, + { 0x0f00, 0x0 }, + // Myanmar, + { 0x1000, 0x0 }, + // Khmer, + { 0x1780, 0x0 }, + + // East Asian Scripts + // Han, + { 0x4e00, 0x0 }, + // Hiragana, + { 0x3050, 0x4e00, 0x25EF, 0x3012, 0x3013, 0x30FB, 0x30FC, 0x5CE0, 0 }, + // Katakana, + { 0x30b0, 0x4e00, 0x25EF, 0x3012, 0x3013, 0x30FB, 0x30FC, 0x5CE0, 0 }, + // Hangul, + { 0xac00, 0x0 }, + // Bopomofo, + { 0x3110, 0x0 }, + // Yi, + { 0xa000, 0x0 }, + + // Additional Scripts + // Ethiopic, + { 0x1200, 0x0 }, + // Cherokee, + { 0x13a0, 0x0 }, + // CanadianAboriginal, + { 0x1410, 0x0 }, + // Mongolian, + { 0x1800, 0x0 }, + + // Symbols + // CurrencySymbols, + { 0x20aa, 0x0 }, + // LetterlikeSymbols, + { 0x2103, 0x0 }, + // NumberForms, + { 0x2160, 0x0 }, + // MathematicalOperators, + { 0x222b, 0x0 }, + // TechnicalSymbols, + { 0x2312, 0x0 }, + // GeometricSymbols, + { 0x2500, 0x0 }, + // MiscellaneousSymbols, + { 0x2640, 0x2714, 0x0 }, + // EnclosedAndSquare, + { 0x2460, 0x0 }, + // Braille, + { 0x2800, 0x0 }, + + // Unicode, + { 0xfffd, 0x0 }, + + // some scripts added in Unicode 3.2 + // Tagalog, + { 0x1700, 0x0 }, + // Hanunoo, + { 0x1720, 0x0 }, + // Buhid, + { 0x1740, 0x0 }, + // Tagbanwa, + { 0x1770, 0x0 }, + + // KatakanaHalfWidth + { 0xff65, 0x0 }, + + // Limbu + { 0x1901, 0x0 }, + // TaiLe + { 0x1950, 0x0 }, + + // NScripts + { 0x0000, 0x0 }, + // NoScript + { 0x0000, 0x0 }, + + // Han_Japanese + { 0x4e00, 0x25EF, 0x3012, 0x3013, 0x30FB, 0x5CE0, 0 }, + // Han_SimplifiedChinese, 0x3400 is optional + { 0x4e00, 0x201C, 0x3002, 0x6237, 0x9555, 0xFFE5, 0 }, + // Han_TraditionalChinese, 0xF6B1 is optional + // OR Han_HongkongChinese, 0x3435, 0xE000, 0xF6B1 are optional + { 0x4e00, 0x201C, 0x3002, 0x6236, 0x9F98, 0xFFE5, 0 }, + // Han_Korean + { 0x4e00, 0 } + // Taiwan would be 0x201C, 0x3002, 0x4E00, 0x9F98, 0xFFE5 +}; + +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) +static inline bool retquiresOpenType(TQFont::Script s) +{ + return (s >= TQFont::Syriac && s <= TQFont::Sinhala) + || (s >= TQFont::Myanmar && s <= TQFont::Khmer); +} +#endif + +static inline bool canRender( TQFontEngine *fe, TQFont::Script script ) +{ + if ( !fe ) return FALSE; + + bool hasChar = true; + + if (!sample_chars[script][0]) + hasChar = false; + + int i = 0; + while (hasChar && sample_chars[script][i]){ + TQChar sample(sample_chars[script][i]); + if ( !fe->canRender( &sample, 1 ) ) { + hasChar = false; +#ifdef FONT_MATCH_DEBUG + FM_DEBUG(" font has NOT char 0x%04x", sample.unicode() ); + } else { + FM_DEBUG(" font has char 0x%04x", sample.unicode() ); +#endif + } + ++i; + } +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + if (hasChar && retquiresOpenType(script)) { + TQOpenType *ot = fe->openType(); + if (!ot || !ot->supportsScript(script)) + return FALSE; + } +#endif + + return hasChar; +} +#endif // Q_WS_X11 || Q_WS_WIN + + +static TQSingleCleanupHandler qfontdatabase_cleanup; +static TQFontDatabasePrivate *db=0; +#define SMOOTH_SCALABLE 0xffff + +#if defined( Q_WS_X11 ) +# include "qfontdatabase_x11.cpp" +#elif defined( Q_WS_MAC ) +# include "qfontdatabase_mac.cpp" +#elif defined( Q_WS_WIN ) +# include "qfontdatabase_win.cpp" +#elif defined( Q_WS_QWS ) +# include "qfontdatabase_qws.cpp" +#endif + +static TQtFontStyle *bestStyle(TQtFontFoundry *foundry, const TQtFontStyle::Key &styleKey) +{ + int best = 0; + int dist = 0xffff; + + for ( int i = 0; i < foundry->count; i++ ) { + TQtFontStyle *style = foundry->styles[i]; + + int d = TQABS( styleKey.weight - style->key.weight ); + + if ( styleKey.stretch != 0 && style->key.stretch != 0 ) { + d += TQABS( styleKey.stretch - style->key.stretch ); + } + + if ( styleKey.italic ) { + if ( !style->key.italic ) + d += style->key.oblique ? 0x0001 : 0x1000; + } else if ( styleKey.oblique ) { + if (!style->key.oblique ) + d += style->key.italic ? 0x0001 : 0x1000; + } else if ( style->key.italic || style->key.oblique ) { + d += 0x1000; + } + + if ( d < dist ) { + best = i; + dist = d; + } + } + + FM_DEBUG( " best style has distance 0x%x", dist ); + if (!foundry->count) { + TQtFontStyle *temp = NULL; + return temp; + } + return foundry->styles[best]; +} + +#if defined(Q_WS_X11) +static TQtFontEncoding *findEncoding(TQFont::Script script, int styleStrategy, + TQtFontSize *size, int force_encoding_id) +{ + TQtFontEncoding *encoding = 0; + + if (force_encoding_id >= 0) { + encoding = size->encodingID(force_encoding_id); + if (!encoding) + FM_DEBUG(" retquired encoding_id not available"); + return encoding; + } + + if (styleStrategy & (TQFont::OpenGLCompatible | TQFont::PreferBitmap)) { + FM_DEBUG(" PreferBitmap and/or OpenGL set, skipping Xft"); + } else { + encoding = size->encodingID(-1); // -1 == prefer Xft + if (encoding) return encoding; + } + + // Xft not available, find an XLFD font, trying the default encoding first + encoding = size->encodingID(TQFontPrivate::defaultEncodingID); + + if (!encoding || !scripts_for_xlfd_encoding[encoding->encoding][script]) { + // find the first encoding that supports the requested script + encoding = 0; + for (int x = 0; !encoding && x < size->count; ++x) { + const int enc = size->encodings[x].encoding; + if (scripts_for_xlfd_encoding[enc][script]) { + encoding = size->encodings + x; + break; + } + } + } + + return encoding; +} +#endif // Q_WS_X11 + + +#if defined(Q_WS_X11) || defined(Q_WS_WIN) +static +unsigned int bestFoundry( TQFont::Script script, unsigned int score, int styleStrategy, + const TQtFontFamily *family, const TQString &foundry_name, + TQtFontStyle::Key styleKey, int pixelSize, char pitch, + TQtFontFoundry **best_foundry, TQtFontStyle **best_style, + TQtFontSize **best_size +#ifdef Q_WS_X11 + , TQtFontEncoding **best_encoding, int force_encoding_id +#endif + ) +{ + Q_UNUSED( script ); + Q_UNUSED( pitch ); + + FM_DEBUG( " REMARK: looking for best foundry for family '%s'", family->name.latin1() ); + + for ( int x = 0; x < family->count; ++x ) { + TQtFontFoundry *foundry = family->foundries[x]; + if ( ! foundry_name.isEmpty() && + ucstricmp( foundry->name, foundry_name ) != 0 ) + continue; + + FM_DEBUG( " looking for matching style in foundry '%s'", + foundry->name.isEmpty() ? "-- none --" : foundry->name.latin1() ); + + TQtFontStyle *style = bestStyle(foundry, styleKey); + + if ( ! style->smoothScalable && ( styleStrategy & TQFont::ForceOutline ) ) { + FM_DEBUG( " ForceOutline set, but not smoothly scalable" ); + continue; + } + + int px = -1; + TQtFontSize *size = 0; + + // 1. see if we have an exact matching size + if (! (styleStrategy & TQFont::ForceOutline)) { + size = style->pixelSize(pixelSize); + if (size) { + FM_DEBUG(" found exact size match (%d pixels)", size->pixelSize); + px = size->pixelSize; + } + } + + // 2. see if we have a smoothly scalable font + if (! size && style->smoothScalable && ! (styleStrategy & TQFont::PreferBitmap)) { + size = style->pixelSize(SMOOTH_SCALABLE); + if (size) { + FM_DEBUG(" found smoothly scalable font (%d pixels)", pixelSize); + px = pixelSize; + } + } + + // 3. see if we have a bitmap scalable font + if (! size && style->bitmapScalable && (styleStrategy & TQFont::PreferMatch)) { + size = style->pixelSize(0); + if (size) { + FM_DEBUG(" found bitmap scalable font (%d pixels)", pixelSize); + px = pixelSize; + } + } + +#ifdef Q_WS_X11 + TQtFontEncoding *encoding = 0; +#endif + + // 4. find closest size match + if (! size) { + unsigned int distance = ~0u; + for (int x = 0; x < style->count; ++x) { +#ifdef Q_WS_X11 + encoding = + findEncoding(script, styleStrategy, style->pixelSizes + x, force_encoding_id); + if (!encoding) { + FM_DEBUG(" size %3d does not support the script we want", + style->pixelSizes[x].pixelSize); + continue; + } +#endif + + unsigned int d = TQABS(style->pixelSizes[x].pixelSize - pixelSize); + if (d < distance) { + distance = d; + size = style->pixelSizes + x; + FM_DEBUG(" best size so far: %3d (%d)", size->pixelSize, pixelSize); + } + } + + if (!size) { + FM_DEBUG(" no size supports the script we want"); + continue; + } + + if (style->bitmapScalable && ! (styleStrategy & TQFont::PreferQuality) && + (distance * 10 / pixelSize) >= 2) { + // the closest size is not close enough, go ahead and + // use a bitmap scaled font + size = style->pixelSize(0); + px = pixelSize; + } else { + px = size->pixelSize; + } + } + +#ifdef Q_WS_X11 + if (size) { + encoding = findEncoding(script, styleStrategy, size, force_encoding_id); + if (!encoding) size = 0; + } + if ( ! encoding ) { + FM_DEBUG( " foundry doesn't support the script we want" ); + continue; + } +#endif // Q_WS_X11 + + unsigned int this_score = 0x0000; + enum { + PitchMismatch = 0x4000, + StyleMismatch = 0x2000, + BitmapScaledPenalty = 0x1000, + EncodingMismatch = 0x0002, + XLFDPenalty = 0x0001 + }; + +#ifdef Q_WS_X11 + if ( encoding->encoding != -1 ) { + this_score += XLFDPenalty; + if ( encoding->encoding != TQFontPrivate::defaultEncodingID ) + this_score += EncodingMismatch; + } + if (pitch != '*') { + if ( !( pitch == 'm' && encoding->pitch == 'c' ) && pitch != encoding->pitch ) + this_score += PitchMismatch; + } +#else + // ignore pitch for asian fonts, some of them misreport it, and they are all + // fixed pitch anyway. + if (pitch != '*' && (script <= TQFont::NScripts && script != TQFont::KatakanaHalfWidth + && (script < TQFont::Han || script > TQFont::Yi))) { + if ((pitch == 'm' && !family->fixedPitch) + || (pitch == 'p' && family->fixedPitch)) + this_score += PitchMismatch; + } +#endif + if ( styleKey != style->key ) + this_score += StyleMismatch; + if ( !style->smoothScalable && px != size->pixelSize ) // bitmap scaled + this_score += BitmapScaledPenalty; + if (px != pixelSize) // close, but not exact, size match + this_score += TQABS(px - pixelSize); + + if ( this_score < score ) { + FM_DEBUG( " found a match: score %x best score so far %x", + this_score, score ); + + score = this_score; + *best_foundry = foundry; + *best_style = style; + *best_size = size; +#ifdef Q_WS_X11 + *best_encoding = encoding; +#endif // Q_WS_X11 + } else { + FM_DEBUG( " score %x no better than best %x", this_score, score); + } + } + + return score; +} + +/*! + \internal +*/ +TQFontEngine * +TQFontDatabase::findFont( TQFont::Script script, const TQFontPrivate *fp, + const TQFontDef &request, int force_encoding_id ) +{ +#ifndef Q_WS_X11 + Q_UNUSED( force_encoding_id ); +#endif + + if ( !db ) + initializeDb(); + + TQFontEngine *fe = 0; + if ( fp ) { + if ( fp->rawMode ) { + fe = loadEngine( script, fp, request, 0, 0, 0 +#ifdef Q_WS_X11 + , 0, 0, FALSE +#endif + ); + + // if we fail to load the rawmode font, use a 12pixel box engine instead + if (! fe) fe = new TQFontEngineBox( 12 ); + return fe; + } + + TQFontCache::Key key( request, script, +#ifdef Q_WS_WIN + (int)fp->paintdevice, +#else + fp->screen, +#endif + fp->paintdevice + ); + fe = TQFontCache::instance->findEngine( key ); + if ( fe ) return fe; + } + +#ifdef Q_WS_WIN + if (request.styleStrategy & TQFont::PreferDevice) { + TQFontEngine *fe = loadEngine(script, fp, request, 0, 0, 0); + if(fe) + return fe; + } +#endif + + TQString family_name, foundry_name; + TQtFontStyle::Key styleKey; + styleKey.italic = request.italic; + styleKey.weight = request.weight; + styleKey.stretch = request.stretch; + char pitch = request.ignorePitch ? '*' : request.fixedPitch ? 'm' : 'p'; + + parseFontName( request.family, foundry_name, family_name ); + +#ifdef Q_WS_X11 + if (script == TQFont::Han) { + // modify script according to locale + static TQFont::Script defaultHan; + TQCString locale = setlocale(LC_ALL, NULL); + + if (locale.contains("ko")) + defaultHan = TQFont::Han_Korean; + else if (locale.contains("zh_TW") || locale.contains("zh_HK")) + defaultHan = TQFont::Han_TraditionalChinese; + else if (locale.contains("zh")) + defaultHan = TQFont::Han_SimplifiedChinese; + else if (locale.contains("ja")) + defaultHan = TQFont::Han_Japanese; + else + defaultHan = TQFont::Han; // don't change + + script = defaultHan; + } +#endif + + FM_DEBUG( "TQFontDatabase::findFont\n" + " request:\n" + " family: %s [%s], script: %d (%s)\n" + " weight: %d, italic: %d\n" + " stretch: %d\n" + " pixelSize: %d\n" + " pitch: %c", + family_name.isEmpty() ? "-- first in script --" : family_name.latin1(), + foundry_name.isEmpty() ? "-- any --" : foundry_name.latin1(), + script, scriptName( script ).latin1(), + request.weight, request.italic, request.stretch, request.pixelSize, pitch ); + + bool usesFontConfig = FALSE; +#ifdef QT_XFT2 + if (family_name.isEmpty() + || family_name == "Sans Serif" + || family_name == "Serif" + || family_name == "Monospace") { + fe = loadFontConfigFont(fp, request, script); + usesFontConfig = (fe != 0); + } + if (!fe) +#endif + { + TQtFontFamily *best_family = 0; + TQtFontFoundry *best_foundry = 0; + TQtFontStyle *best_style = 0; + TQtFontSize *best_size = 0; +#ifdef Q_WS_X11 + TQtFontEncoding *best_encoding = 0; +#endif // Q_WS_X11 + + unsigned int score = ~0; + + load( family_name, script ); + + for ( int x = 0; x < db->count; ++x ) { + TQtFontFamily *try_family = db->families[x]; +#ifdef Q_WS_X11 + if (try_family->synthetic) // skip generated fontconfig fonts + continue; +#endif + + if ( !family_name.isEmpty() && + ucstricmp( try_family->name, family_name ) != 0 +#ifdef Q_WS_WIN + && ucstricmp( try_family->english_name, family_name ) != 0 +#endif + ) + continue; + + if ( family_name.isEmpty() ) + load( try_family->name, script ); + + uint score_adjust = 0; + TQFont::Script override_script = script; + if ( ! ( try_family->scripts[script] & TQtFontFamily::Supported ) + && script != TQFont::Unicode) { + // family not supported in the script we want +#ifdef Q_WS_X11 + if (script >= TQFont::Han_Japanese && script <= TQFont::Han_Korean + && try_family->scripts[TQFont::Han] == TQtFontFamily::Supported) { + // try with the han script instead, give it a penalty + if (override_script == TQFont::Han_TraditionalChinese + && (try_family->scripts[TQFont::Han_SimplifiedChinese] & TQtFontFamily::Supported)) { + override_script = TQFont::Han_SimplifiedChinese; + score_adjust = 200; + } else if (override_script == TQFont::Han_SimplifiedChinese + && (try_family->scripts[TQFont::Han_TraditionalChinese] & TQtFontFamily::Supported)) { + override_script = TQFont::Han_TraditionalChinese; + score_adjust = 200; + } else { + override_script = TQFont::Han; + score_adjust = 400; + } + } else +#endif + if (family_name.isEmpty()) { + continue; + } else if (try_family->scripts[TQFont::UnknownScript] & TQtFontFamily::Supported) { + // try with the unknown script (for a symbol font) + override_script = TQFont::UnknownScript; +#ifndef QT_XFT2 + } else if (try_family->scripts[TQFont::Unicode] & TQtFontFamily::Supported) { + // try with the unicode script instead + override_script = TQFont::Unicode; +#endif + } else { + // family not supported by unicode/unknown scripts + continue; + } + } + + TQtFontFoundry *try_foundry = 0; + TQtFontStyle *try_style = 0; + TQtFontSize *try_size = 0; +#ifdef Q_WS_X11 + TQtFontEncoding *try_encoding = 0; +#endif // Q_WS_X11 + + // as we know the script is supported, we can be sure + // to find a matching font here. + unsigned int newscore = + bestFoundry( override_script, score, request.styleStrategy, + try_family, foundry_name, styleKey, request.pixelSize, pitch, + &try_foundry, &try_style, &try_size +#ifdef Q_WS_X11 + , &try_encoding, force_encoding_id +#endif + ); + if ( try_foundry == 0 ) { + // the specific foundry was not found, so look for + // any foundry matching our retquirements + newscore = bestFoundry( override_script, score, request.styleStrategy, try_family, + TQString::null, styleKey, request.pixelSize, + pitch, &try_foundry, &try_style, &try_size +#ifdef Q_WS_X11 + , &try_encoding, force_encoding_id +#endif + ); + } + newscore += score_adjust; + + if ( newscore < score ) { + score = newscore; + best_family = try_family; + best_foundry = try_foundry; + best_style = try_style; + best_size = try_size; +#ifdef Q_WS_X11 + best_encoding = try_encoding; +#endif // Q_WS_X11 + } + if ( newscore < 10 ) // xlfd instead of xft... just accept it + break; + } + + if ( best_family != 0 && best_foundry != 0 && best_style != 0 +#ifdef Q_WS_X11 + && best_size != 0 && best_encoding != 0 +#endif + ) { + FM_DEBUG( " BEST:\n" + " family: %s [%s]\n" + " weight: %d, italic: %d, oblique: %d\n" + " stretch: %d\n" + " pixelSize: %d\n" + " pitch: %c\n" + " encoding: %d\n", + best_family->name.latin1(), + best_foundry->name.isEmpty() ? "-- none --" : best_foundry->name.latin1(), + best_style->key.weight, best_style->key.italic, best_style->key.oblique, + best_style->key.stretch, best_size ? best_size->pixelSize : 0xffff, +#ifdef Q_WS_X11 + best_encoding->pitch, best_encoding->encoding +#else + 'p', 0 +#endif + ); + + fe = loadEngine( script, fp, request, best_family, best_foundry, best_style +#ifdef Q_WS_X11 + , best_size, best_encoding, ( force_encoding_id >= 0 ) +#endif + ); + } + if (fe) { + fe->fontDef.family = best_family->name; + if ( ! best_foundry->name.isEmpty() ) { + fe->fontDef.family += TQString::fromLatin1( " [" ); + fe->fontDef.family += best_foundry->name; + fe->fontDef.family += TQString::fromLatin1( "]" ); + } + + if ( best_style->smoothScalable ) + fe->fontDef.pixelSize = request.pixelSize; + else if ( best_style->bitmapScalable && + ( request.styleStrategy & TQFont::PreferMatch ) ) + fe->fontDef.pixelSize = request.pixelSize; + else + fe->fontDef.pixelSize = best_size->pixelSize; + + fe->fontDef.styleHint = request.styleHint; + fe->fontDef.styleStrategy = request.styleStrategy; + + fe->fontDef.weight = best_style->key.weight; + fe->fontDef.italic = best_style->key.italic || best_style->key.oblique; + fe->fontDef.fixedPitch = best_family->fixedPitch; + fe->fontDef.stretch = best_style->key.stretch; + fe->fontDef.ignorePitch = FALSE; + } + } + + if ( fe ) { + if ( script != TQFont::Unicode && !canRender( fe, script ) ) { + FM_DEBUG( " WARN: font loaded cannot render a sample char" ); + + delete fe; + fe = 0; + } else if ( fp ) { + TQFontDef def = request; + if (def.family.isEmpty()) { + def.family = fp->request.family; + def.family = def.family.left(def.family.find(',')); + } + TQFontCache::Key key( def, script, +#ifdef Q_WS_WIN + (int)fp->paintdevice, +#else + fp->screen, +#endif + fp->paintdevice + ); + TQFontCache::instance->insertEngine( key, fe ); + if (!usesFontConfig) { + for ( int i = 0; i < TQFont::NScripts; ++i ) { + if ( i == script ) continue; + + if (!canRender(fe, (TQFont::Script) i)) + continue; + + key.script = i; + TQFontCache::instance->insertEngine( key, fe ); + } + } + } + } + + if (!fe) { + if ( !request.family.isEmpty() ) + return 0; + + FM_DEBUG( "returning box engine" ); + + fe = new TQFontEngineBox( request.pixelSize ); + fe->fontDef = request; + + if ( fp ) { + TQFontCache::Key key( request, script, +#ifdef Q_WS_WIN + (int)fp->paintdevice, +#else + fp->screen, +#endif + fp->paintdevice + ); + TQFontCache::instance->insertEngine( key, fe ); + } + } + + if ( fp ) { +#if defined(Q_WS_X11) + fe->fontDef.pointSize = + qRound(10. * qt_pointSize(fe->fontDef.pixelSize, fp->paintdevice, fp->screen)); +#elif defined(Q_WS_WIN) + fe->fontDef.pointSize = int( double( fe->fontDef.pixelSize ) * 720.0 / + GetDeviceCaps(shared_dc,LOGPIXELSY) ); +#else + fe->fontDef.pointSize = int( double( fe->fontDef.pixelSize ) * 720.0 / + 96.0 ); +#endif + } else { + fe->fontDef.pointSize = request.pointSize; + } + + return fe; +} +#endif // Q_WS_X11 || Q_WS_WIN + + + + +static TQString styleString( int weight, bool italic, bool oblique ) +{ + TQString result; + if ( weight >= TQFont::Black ) + result = "Black"; + else if ( weight >= TQFont::Bold ) + result = "Bold"; + else if ( weight >= TQFont::DemiBold ) + result = "Demi Bold"; + else if ( weight < TQFont::Normal ) + result = "Light"; + + if ( italic ) + result += " Italic"; + else if ( oblique ) + result += " Oblique"; + + if ( result.isEmpty() ) + result = "Normal"; + + return result.simplifyWhiteSpace(); +} + +/*! + Returns a string that describes the style of the font \a f. For + example, "Bold Italic", "Bold", "Italic" or "Normal". An empty + string may be returned. +*/ +TQString TQFontDatabase::styleString( const TQFont &f ) +{ + // ### fix oblique here + return ::styleString( f.weight(), f.italic(), FALSE ); +} + + +/*! + \class TQFontDatabase qfontdatabase.h + \brief The TQFontDatabase class provides information about the fonts available in the underlying window system. + + \ingroup environment + \ingroup graphics + + The most common uses of this class are to query the database for + the list of font families() and for the pointSizes() and styles() + that are available for each family. An alternative to pointSizes() + is smoothSizes() which returns the sizes at which a given family + and style will look attractive. + + If the font family is available from two or more foundries the + foundry name is included in the family name, e.g. "Helvetica + [Adobe]" and "Helvetica [Cronyx]". When you specify a family you + can either use the old hyphenated TQt 2.x "foundry-family" format, + e.g. "Cronyx-Helvetica", or the new bracketed TQt 3.x "family + [foundry]" format e.g. "Helvetica [Cronyx]". If the family has a + foundry it is always returned, e.g. by families(), using the + bracketed format. + + The font() function returns a TQFont given a family, style and + point size. + + A family and style combination can be checked to see if it is + italic() or bold(), and to retrieve its weight(). Similarly we can + call isBitmapScalable(), isSmoothlyScalable(), isScalable() and + isFixedPitch(). + + A text version of a style is given by styleString(). + + The TQFontDatabase class also supports some static functions, for + example, standardSizes(). You can retrieve the Unicode 3.0 + description of a \link TQFont::Script script\endlink using + scriptName(), and a sample of characters in a script with + scriptSample(). + + Example: +\code +#include +#include +#include + +int main( int argc, char **argv ) +{ + TQApplication app( argc, argv ); + TQFontDatabase fdb; + TQStringList families = fdb.families(); + for ( TQStringList::Iterator f = families.begin(); f != families.end(); ++f ) { + TQString family = *f; + qDebug( family ); + TQStringList styles = fdb.styles( family ); + for ( TQStringList::Iterator s = styles.begin(); s != styles.end(); ++s ) { + TQString style = *s; + TQString dstyle = "\t" + style + " ("; + TQValueList smoothies = fdb.smoothSizes( family, style ); + for ( TQValueList::Iterator points = smoothies.begin(); + points != smoothies.end(); ++points ) { + dstyle += TQString::number( *points ) + " "; + } + dstyle = dstyle.left( dstyle.length() - 1 ) + ")"; + qDebug( dstyle ); + } + } + return 0; +} +\endcode + This example gets the list of font families, then the list of + styles for each family and the point sizes that are available for + each family/style combination. +*/ +/*! + \obsolete + \fn inline TQStringList TQFontDatabase::families( bool ) const +*/ +/*! + \obsolete + \fn inline TQStringList TQFontDatabase::styles( const TQString &family, + const TQString & ) const +*/ +/*! + \obsolete + \fn inline TQValueList TQFontDatabase::pointSizes( const TQString &family, + const TQString &style , + const TQString & ) +*/ + +/*! + \obsolete + \fn inline TQValueList TQFontDatabase::smoothSizes( const TQString &family, + const TQString &style, + const TQString & ) +*/ +/*! + \obsolete + \fn inline TQFont TQFontDatabase::font( const TQString &familyName, + const TQString &style, + int pointSize, + const TQString &) +*/ +/*! + \obsolete + \fn inline bool TQFontDatabase::isBitmapScalable( const TQString &family, + const TQString &style, + const TQString & ) const +*/ + +/*! + \obsolete + \fn inline bool TQFontDatabase::isSmoothlyScalable( const TQString &family, + const TQString &style, + const TQString & ) const +*/ + +/*! + \obsolete + \fn inline bool TQFontDatabase::isScalable( const TQString &family, + const TQString &style, + const TQString & ) const +*/ + +/*! + \obsolete + \fn inline bool TQFontDatabase::isFixedPitch( const TQString &family, + const TQString &style, + const TQString & ) const +*/ + +/*! + \obsolete + \fn inline bool TQFontDatabase::italic( const TQString &family, + const TQString &style, + const TQString & ) const +*/ + +/*! + \obsolete + \fn inline bool TQFontDatabase::bold( const TQString &family, + const TQString &style, + const TQString & ) const +*/ + +/*! + \obsolete + \fn inline int TQFontDatabase::weight( const TQString &family, + const TQString &style, + const TQString & ) const +*/ + + +/*! + Creates a font database object. +*/ +TQFontDatabase::TQFontDatabase() +{ + createDatabase(); + + d = db; +} + + +/*! Returns a sorted list of the names of the available font families. + + If a family exists in several foundries, the returned name for + that font is in the form "family [foundry]". Examples: "Times + [Adobe]", "Times [Cronyx]", "Palatino". +*/ +TQStringList TQFontDatabase::families() const +{ + load(); + + TQStringList flist; + for ( int i = 0; i < d->count; i++ ) { + TQtFontFamily *f = d->families[i]; + if ( f->count == 0 ) + continue; + if ( f->count == 1 ) { + flist.append( f->name ); + } else { + for ( int j = 0; j < f->count; j++ ) { + TQString str = f->name; + TQString foundry = f->foundries[j]->name; + if ( !foundry.isEmpty() ) { + str += " ["; + str += foundry; + str += "]"; + } + flist.append( str ); + } + } + } + return flist; +} + +/*! + \overload + + Returns a sorted list of the available font families which support + the Unicode script \a script. + + If a family exists in several foundries, the returned name for + that font is in the form "family [foundry]". Examples: "Times + [Adobe]", "Times [Cronyx]", "Palatino". +*/ +TQStringList TQFontDatabase::families( TQFont::Script script ) const +{ + load(); + + TQStringList flist; + for ( int i = 0; i < d->count; i++ ) { + TQtFontFamily *f = d->families[i]; + if ( f->count == 0 ) + continue; + if (!(f->scripts[script] & TQtFontFamily::Supported)) + continue; + if ( f->count == 1 ) { + flist.append( f->name ); + } else { + for ( int j = 0; j < f->count; j++ ) { + TQString str = f->name; + TQString foundry = f->foundries[j]->name; + if ( !foundry.isEmpty() ) { + str += " ["; + str += foundry; + str += "]"; + } + flist.append( str ); + } + } + } + return flist; +} + +/*! + Returns a list of the styles available for the font family \a + family. Some example styles: "Light", "Light Italic", "Bold", + "Oblique", "Demi". The list may be empty. +*/ +TQStringList TQFontDatabase::styles( const TQString &family ) const +{ + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQStringList l; + TQtFontFamily *f = d->family( familyName ); + if ( !f ) + return l; + + TQtFontFoundry allStyles( foundryName ); + for ( int j = 0; j < f->count; j++ ) { + TQtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) { + TQtFontStyle::Key ke( foundry->styles[k]->key ); + ke.stretch = 0; + allStyles.style( ke, TRUE ); + } + } + } + + for ( int i = 0; i < allStyles.count; i++ ) + l.append( ::styleString( allStyles.styles[i]->key.weight, + allStyles.styles[i]->key.italic, + allStyles.styles[i]->key.oblique ) ); + return l; +} + +/*! + Returns TRUE if the font that has family \a family and style \a + style is fixed pitch; otherwise returns FALSE. +*/ + +bool TQFontDatabase::isFixedPitch(const TQString &family, + const TQString &style) const +{ + Q_UNUSED(style); + + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontFamily *f = d->family( familyName ); +#if defined(Q_OS_MAC) && !defined(TQWS) + if (f) { + if (!f->fixedPitchComputed) { + TQFontMetrics fm(familyName); + f->fixedPitch = fm.width('i') == fm.width('m'); + f->fixedPitchComputed = TRUE; + } + } +#endif + + return ( f && f->fixedPitch ); +} + +/*! + Returns TRUE if the font that has family \a family and style \a + style is a scalable bitmap font; otherwise returns FALSE. Scaling + a bitmap font usually produces an unattractive hardly readable + result, because the pixels of the font are scaled. If you need to + scale a bitmap font it is better to scale it to one of the fixed + sizes returned by smoothSizes(). + + \sa isScalable(), isSmoothlyScalable() +*/ +bool TQFontDatabase::isBitmapScalable( const TQString &family, + const TQString &style) const +{ + bool bitmapScalable = FALSE; + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontStyle::Key styleKey( style ); + + TQtFontFamily *f = d->family( familyName ); + if ( !f ) return bitmapScalable; + + for ( int j = 0; j < f->count; j++ ) { + TQtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + if ((style.isEmpty() || foundry->styles[k]->key == styleKey) && + foundry->styles[k]->bitmapScalable && !foundry->styles[k]->smoothScalable) { + bitmapScalable = TRUE; + goto end; + } + } + } + end: + return bitmapScalable; +} + + +/*! + Returns TRUE if the font that has family \a family and style \a + style is smoothly scalable; otherwise returns FALSE. If this + function returns TRUE, it's safe to scale this font to any size, + and the result will always look attractive. + + \sa isScalable(), isBitmapScalable() +*/ +bool TQFontDatabase::isSmoothlyScalable( const TQString &family, + const TQString &style) const +{ + bool smoothScalable = FALSE; + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontStyle::Key styleKey( style ); + + TQtFontFamily *f = d->family( familyName ); + if ( !f ) return smoothScalable; + + for ( int j = 0; j < f->count; j++ ) { + TQtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + if ((style.isEmpty() || foundry->styles[k]->key == styleKey) && foundry->styles[k]->smoothScalable) { + smoothScalable = TRUE; + goto end; + } + } + } + end: + return smoothScalable; +} + +/*! + Returns TRUE if the font that has family \a family and style \a + style is scalable; otherwise returns FALSE. + + \sa isBitmapScalable(), isSmoothlyScalable() +*/ +bool TQFontDatabase::isScalable( const TQString &family, + const TQString &style) const +{ + if ( isSmoothlyScalable( family, style) ) + return TRUE; + + return isBitmapScalable( family, style); +} + + +/*! + Returns a list of the point sizes available for the font that has + family \a family and style \a style. The list may be empty. + + \sa smoothSizes(), standardSizes() +*/ +TQValueList TQFontDatabase::pointSizes( const TQString &family, + const TQString &style) +{ +#if defined(Q_WS_MAC) + // windows and macosx are always smoothly scalable + Q_UNUSED( family ); + Q_UNUSED( style ); + return standardSizes(); +#else + bool smoothScalable = FALSE; + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontStyle::Key styleKey( style ); + + TQValueList sizes; + + TQtFontFamily *fam = d->family( familyName ); + if ( !fam ) return sizes; + + for ( int j = 0; j < fam->count; j++ ) { + TQtFontFoundry *foundry = fam->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + TQtFontStyle *style = foundry->style( styleKey ); + if ( !style ) continue; + + if ( style->smoothScalable ) { + smoothScalable = TRUE; + goto end; + } + for ( int l = 0; l < style->count; l++ ) { + const TQtFontSize *size = style->pixelSizes + l; + + if (size->pixelSize != 0 && size->pixelSize != USHRT_MAX) { +#ifdef Q_WS_X11 + const uint pointSize = qRound(qt_pointSize(size->pixelSize, 0, -1)); +#else + const uint pointSize = size->pixelSize; // embedded uses 72dpi +#endif + if (! sizes.contains(pointSize)) + sizes.append(pointSize); + } + } + } + } + end: + if ( smoothScalable ) + return standardSizes(); + + qHeapSort( sizes ); + return sizes; +#endif +} + +/*! + Returns a TQFont object that has family \a family, style \a style + and point size \a pointSize. If no matching font could be created, + a TQFont object that uses the application's default font is + returned. +*/ +TQFont TQFontDatabase::font( const TQString &family, const TQString &style, + int pointSize) +{ + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontFoundry allStyles( foundryName ); + TQtFontFamily *f = d->family( familyName ); + if ( !f ) return TQApplication::font(); + + for ( int j = 0; j < f->count; j++ ) { + TQtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + allStyles.style( foundry->styles[k]->key, TRUE ); + } + } + + TQtFontStyle::Key styleKey( style ); + TQtFontStyle *s = bestStyle(&allStyles, styleKey); + + if ( !s ) // no styles found? + return TQApplication::font(); + return TQFont( family, pointSize, s->key.weight, + s->key.italic ? TRUE : s->key.oblique ? TRUE : FALSE ); +} + + +/*! + Returns the point sizes of a font that has family \a family and + style \a style that will look attractive. The list may be empty. + For non-scalable fonts and bitmap scalable fonts, this function + is equivalent to pointSizes(). + + \sa pointSizes(), standardSizes() +*/ +TQValueList TQFontDatabase::smoothSizes( const TQString &family, + const TQString &style) +{ +#ifdef Q_WS_WIN + Q_UNUSED( family ); + Q_UNUSED( style ); + return TQFontDatabase::standardSizes(); +#else + bool smoothScalable = FALSE; + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontStyle::Key styleKey( style ); + + TQValueList sizes; + + TQtFontFamily *fam = d->family( familyName ); + if ( !fam ) + return sizes; + + for ( int j = 0; j < fam->count; j++ ) { + TQtFontFoundry *foundry = fam->foundries[j]; + if ( foundryName.isEmpty() || + ucstricmp( foundry->name, foundryName ) == 0 ) { + TQtFontStyle *style = foundry->style( styleKey ); + if ( !style ) continue; + + if ( style->smoothScalable ) { + smoothScalable = TRUE; + goto end; + } + for ( int l = 0; l < style->count; l++ ) { + const TQtFontSize *size = style->pixelSizes + l; + + if ( size->pixelSize != 0 && size->pixelSize != USHRT_MAX ) { +#ifdef Q_WS_X11 + const uint pointSize = qRound(qt_pointSize(size->pixelSize, 0, -1)); +#else + const uint pointSize = size->pixelSize; // embedded uses 72dpi +#endif + if (! sizes.contains(pointSize)) + sizes.append( pointSize ); + } + } + } + } + end: + if ( smoothScalable ) + return TQFontDatabase::standardSizes(); + + qHeapSort( sizes ); + return sizes; +#endif +} + + +/*! + Returns a list of standard font sizes. + + \sa smoothSizes(), pointSizes() +*/ +TQValueList TQFontDatabase::standardSizes() +{ + TQValueList ret; + static const unsigned short standard[] = + { 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, 0 }; + const unsigned short *sizes = standard; + while ( *sizes ) ret << *sizes++; + return ret; +} + + +/*! + Returns TRUE if the font that has family \a family and style \a + style is italic; otherwise returns FALSE. + + \sa weight(), bold() +*/ +bool TQFontDatabase::italic( const TQString &family, + const TQString &style) const +{ + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontFoundry allStyles( foundryName ); + TQtFontFamily *f = d->family( familyName ); + if ( !f ) return FALSE; + + for ( int j = 0; j < f->count; j++ ) { + TQtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + allStyles.style( foundry->styles[k]->key, TRUE ); + } + } + + TQtFontStyle::Key styleKey( style ); + TQtFontStyle *s = allStyles.style( styleKey ); + return s && s->key.italic; +} + + +/*! + Returns TRUE if the font that has family \a family and style \a + style is bold; otherwise returns FALSE. + + \sa italic(), weight() +*/ +bool TQFontDatabase::bold( const TQString &family, + const TQString &style) const +{ + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontFoundry allStyles( foundryName ); + TQtFontFamily *f = d->family( familyName ); + if ( !f ) return FALSE; + + for ( int j = 0; j < f->count; j++ ) { + TQtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || + ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + allStyles.style( foundry->styles[k]->key, TRUE ); + } + } + + TQtFontStyle::Key styleKey( style ); + TQtFontStyle *s = allStyles.style( styleKey ); + return s && s->key.weight >= TQFont::Bold; +} + + +/*! + Returns the weight of the font that has family \a family and style + \a style. If there is no such family and style combination, + returns -1. + + \sa italic(), bold() +*/ +int TQFontDatabase::weight( const TQString &family, + const TQString &style) const +{ + TQString familyName, foundryName; + parseFontName( family, foundryName, familyName ); + + load( familyName ); + + TQtFontFoundry allStyles( foundryName ); + TQtFontFamily *f = d->family( familyName ); + if ( !f ) return -1; + + for ( int j = 0; j < f->count; j++ ) { + TQtFontFoundry *foundry = f->foundries[j]; + if ( foundryName.isEmpty() || + ucstricmp( foundry->name, foundryName ) == 0 ) { + for ( int k = 0; k < foundry->count; k++ ) + allStyles.style( foundry->styles[k]->key, TRUE ); + } + } + + TQtFontStyle::Key styleKey( style ); + TQtFontStyle *s = allStyles.style( styleKey ); + return s ? s->key.weight : -1; +} + + +/*! + Returns a string that gives a default description of the \a script + (e.g. for displaying to the user in a dialog). The name matches + the name of the script as defined by the Unicode 3.0 standard. + + \sa TQFont::Script +*/ +TQString TQFontDatabase::scriptName(TQFont::Script script) +{ + const char *name = 0; + + switch (script) { + case TQFont::Latin: + name = QT_TRANSLATE_NOOP("TQFont", "Latin"); + break; + case TQFont::Greek: + name = QT_TRANSLATE_NOOP("TQFont", "Greek" ); + break; + case TQFont::Cyrillic: + name = QT_TRANSLATE_NOOP("TQFont", "Cyrillic" ); + break; + case TQFont::Armenian: + name = QT_TRANSLATE_NOOP("TQFont", "Armenian" ); + break; + case TQFont::Georgian: + name = QT_TRANSLATE_NOOP("TQFont", "Georgian" ); + break; + case TQFont::Runic: + name = QT_TRANSLATE_NOOP("TQFont", "Runic" ); + break; + case TQFont::Ogham: + name = QT_TRANSLATE_NOOP("TQFont", "Ogham" ); + break; + case TQFont::SpacingModifiers: + name = QT_TRANSLATE_NOOP("TQFont", "SpacingModifiers" ); + break; + case TQFont::CombiningMarks: + name = QT_TRANSLATE_NOOP("TQFont", "CombiningMarks" ); + break; + case TQFont::Hebrew: + name = QT_TRANSLATE_NOOP("TQFont", "Hebrew" ); + break; + case TQFont::Arabic: + name = QT_TRANSLATE_NOOP("TQFont", "Arabic" ); + break; + case TQFont::Syriac: + name = QT_TRANSLATE_NOOP("TQFont", "Syriac" ); + break; + case TQFont::Thaana: + name = QT_TRANSLATE_NOOP("TQFont", "Thaana" ); + break; + case TQFont::Devanagari: + name = QT_TRANSLATE_NOOP("TQFont", "Devanagari" ); + break; + case TQFont::Bengali: + name = QT_TRANSLATE_NOOP("TQFont", "Bengali" ); + break; + case TQFont::Gurmukhi: + name = QT_TRANSLATE_NOOP("TQFont", "Gurmukhi" ); + break; + case TQFont::Gujarati: + name = QT_TRANSLATE_NOOP("TQFont", "Gujarati" ); + break; + case TQFont::Oriya: + name = QT_TRANSLATE_NOOP("TQFont", "Oriya" ); + break; + case TQFont::Tamil: + name = QT_TRANSLATE_NOOP("TQFont", "Tamil" ); + break; + case TQFont::Telugu: + name = QT_TRANSLATE_NOOP("TQFont", "Telugu" ); + break; + case TQFont::Kannada: + name = QT_TRANSLATE_NOOP("TQFont", "Kannada" ); + break; + case TQFont::Malayalam: + name = QT_TRANSLATE_NOOP("TQFont", "Malayalam" ); + break; + case TQFont::Sinhala: + name = QT_TRANSLATE_NOOP("TQFont", "Sinhala" ); + break; + case TQFont::Thai: + name = QT_TRANSLATE_NOOP("TQFont", "Thai" ); + break; + case TQFont::Lao: + name = QT_TRANSLATE_NOOP("TQFont", "Lao" ); + break; + case TQFont::Tibetan: + name = QT_TRANSLATE_NOOP("TQFont", "Tibetan" ); + break; + case TQFont::Myanmar: + name = QT_TRANSLATE_NOOP("TQFont", "Myanmar" ); + break; + case TQFont::Khmer: + name = QT_TRANSLATE_NOOP("TQFont", "Khmer" ); + break; + case TQFont::Han: + name = QT_TRANSLATE_NOOP("TQFont", "Han" ); + break; + case TQFont::Hiragana: + name = QT_TRANSLATE_NOOP("TQFont", "Hiragana" ); + break; + case TQFont::Katakana: + name = QT_TRANSLATE_NOOP("TQFont", "Katakana" ); + break; + case TQFont::Hangul: + name = QT_TRANSLATE_NOOP("TQFont", "Hangul" ); + break; + case TQFont::Bopomofo: + name = QT_TRANSLATE_NOOP("TQFont", "Bopomofo" ); + break; + case TQFont::Yi: + name = QT_TRANSLATE_NOOP("TQFont", "Yi" ); + break; + case TQFont::Ethiopic: + name = QT_TRANSLATE_NOOP("TQFont", "Ethiopic" ); + break; + case TQFont::Cherokee: + name = QT_TRANSLATE_NOOP("TQFont", "Cherokee" ); + break; + case TQFont::CanadianAboriginal: + name = QT_TRANSLATE_NOOP("TQFont", "Canadian Aboriginal" ); + break; + case TQFont::Mongolian: + name = QT_TRANSLATE_NOOP("TQFont", "Mongolian" ); + break; + + case TQFont::CurrencySymbols: + name = QT_TRANSLATE_NOOP("TQFont", "Currency Symbols" ); + break; + + case TQFont::LetterlikeSymbols: + name = QT_TRANSLATE_NOOP("TQFont", "Letterlike Symbols" ); + break; + + case TQFont::NumberForms: + name = QT_TRANSLATE_NOOP("TQFont", "Number Forms" ); + break; + + case TQFont::MathematicalOperators: + name = QT_TRANSLATE_NOOP("TQFont", "Mathematical Operators" ); + break; + + case TQFont::TechnicalSymbols: + name = QT_TRANSLATE_NOOP("TQFont", "Technical Symbols" ); + break; + + case TQFont::GeometricSymbols: + name = QT_TRANSLATE_NOOP("TQFont", "Geometric Symbols" ); + break; + + case TQFont::MiscellaneousSymbols: + name = QT_TRANSLATE_NOOP("TQFont", "Miscellaneous Symbols" ); + break; + + case TQFont::EnclosedAndSquare: + name = QT_TRANSLATE_NOOP("TQFont", "Enclosed and Square" ); + break; + + case TQFont::Braille: + name = QT_TRANSLATE_NOOP("TQFont", "Braille" ); + break; + + case TQFont::Unicode: + name = QT_TRANSLATE_NOOP("TQFont", "Unicode" ); + break; + + case TQFont::Tagalog: + name = QT_TRANSLATE_NOOP( "TQFont", "Tagalog" ); + break; + + case TQFont::Hanunoo: + name = QT_TRANSLATE_NOOP( "TQFont", "Hanunoo" ); + break; + + case TQFont::Buhid: + name = QT_TRANSLATE_NOOP( "TQFont", "Buhid" ); + break; + + case TQFont::Tagbanwa: + name = QT_TRANSLATE_NOOP( "TQFont", "Tagbanwa" ); + break; + + case TQFont::KatakanaHalfWidth: + name = QT_TRANSLATE_NOOP( "TQFont", "Katakana Half-Width Forms" ); + break; + + case TQFont::Han_Japanese: + name = QT_TRANSLATE_NOOP( "TQFont", "Han (Japanese)" ); + break; + + case TQFont::Han_SimplifiedChinese: + name = QT_TRANSLATE_NOOP( "TQFont", "Han (Simplified Chinese)" ); + break; + + case TQFont::Han_TraditionalChinese: + name = QT_TRANSLATE_NOOP( "TQFont", "Han (Traditional Chinese)" ); + break; + + case TQFont::Han_Korean: + name = QT_TRANSLATE_NOOP( "TQFont", "Han (Korean)" ); + break; + + default: + name = QT_TRANSLATE_NOOP( "TQFont", "Unknown Script" ); + break; + } + + return qApp ? qApp->translate("TQFont", name) : TQString::fromLatin1(name); +} + + +/*! + Returns a string with sample characters from \a script. + + \sa TQFont::Script +*/ +TQString TQFontDatabase::scriptSample(TQFont::Script script) +{ + TQString sample = "AaBb"; + + switch (script) { + case TQFont::Latin: + // This is cheating... we only show latin-1 characters so that we don't + // end up loading lots of fonts - at least on X11... + sample += TQChar(0x00C3); + sample += TQChar(0x00E1); + sample += "Zz"; + break; + case TQFont::Greek: + sample += TQChar(0x0393); + sample += TQChar(0x03B1); + sample += TQChar(0x03A9); + sample += TQChar(0x03C9); + break; + case TQFont::Cyrillic: + sample += TQChar(0x0414); + sample += TQChar(0x0434); + sample += TQChar(0x0436); + sample += TQChar(0x0402); + break; + case TQFont::Armenian: + sample += TQChar(0x053f); + sample += TQChar(0x054f); + sample += TQChar(0x056f); + sample += TQChar(0x057f); + break; + case TQFont::Georgian: + sample += TQChar(0x10a0); + sample += TQChar(0x10b0); + sample += TQChar(0x10c0); + sample += TQChar(0x10d0); + break; + case TQFont::Runic: + sample += TQChar(0x16a0); + sample += TQChar(0x16b0); + sample += TQChar(0x16c0); + sample += TQChar(0x16d0); + break; + case TQFont::Ogham: + sample += TQChar(0x1681); + sample += TQChar(0x1687); + sample += TQChar(0x1693); + sample += TQChar(0x168d); + break; + + + + case TQFont::Hebrew: + sample += TQChar(0x05D0); + sample += TQChar(0x05D1); + sample += TQChar(0x05D2); + sample += TQChar(0x05D3); + break; + case TQFont::Arabic: + sample += TQChar(0x0628); + sample += TQChar(0x0629); + sample += TQChar(0x062A); + sample += TQChar(0x063A); + break; + case TQFont::Syriac: + sample += TQChar(0x0715); + sample += TQChar(0x0725); + sample += TQChar(0x0716); + sample += TQChar(0x0726); + break; + case TQFont::Thaana: + sample += TQChar(0x0784); + sample += TQChar(0x0794); + sample += TQChar(0x078c); + sample += TQChar(0x078d); + break; + + + + case TQFont::Devanagari: + sample += TQChar(0x0905); + sample += TQChar(0x0915); + sample += TQChar(0x0925); + sample += TQChar(0x0935); + break; + case TQFont::Bengali: + sample += TQChar(0x0986); + sample += TQChar(0x0996); + sample += TQChar(0x09a6); + sample += TQChar(0x09b6); + break; + case TQFont::Gurmukhi: + sample += TQChar(0x0a05); + sample += TQChar(0x0a15); + sample += TQChar(0x0a25); + sample += TQChar(0x0a35); + break; + case TQFont::Gujarati: + sample += TQChar(0x0a85); + sample += TQChar(0x0a95); + sample += TQChar(0x0aa5); + sample += TQChar(0x0ab5); + break; + case TQFont::Oriya: + sample += TQChar(0x0b06); + sample += TQChar(0x0b16); + sample += TQChar(0x0b2b); + sample += TQChar(0x0b36); + break; + case TQFont::Tamil: + sample += TQChar(0x0b89); + sample += TQChar(0x0b99); + sample += TQChar(0x0ba9); + sample += TQChar(0x0bb9); + break; + case TQFont::Telugu: + sample += TQChar(0x0c05); + sample += TQChar(0x0c15); + sample += TQChar(0x0c25); + sample += TQChar(0x0c35); + break; + case TQFont::Kannada: + sample += TQChar(0x0c85); + sample += TQChar(0x0c95); + sample += TQChar(0x0ca5); + sample += TQChar(0x0cb5); + break; + case TQFont::Malayalam: + sample += TQChar(0x0d05); + sample += TQChar(0x0d15); + sample += TQChar(0x0d25); + sample += TQChar(0x0d35); + break; + case TQFont::Sinhala: + sample += TQChar(0x0d90); + sample += TQChar(0x0da0); + sample += TQChar(0x0db0); + sample += TQChar(0x0dc0); + break; + case TQFont::Thai: + sample += TQChar(0x0e02); + sample += TQChar(0x0e12); + sample += TQChar(0x0e22); + sample += TQChar(0x0e32); + break; + case TQFont::Lao: + sample += TQChar(0x0e8d); + sample += TQChar(0x0e9d); + sample += TQChar(0x0ead); + sample += TQChar(0x0ebd); + break; + case TQFont::Tibetan: + sample += TQChar(0x0f00); + sample += TQChar(0x0f01); + sample += TQChar(0x0f02); + sample += TQChar(0x0f03); + break; + case TQFont::Myanmar: + sample += TQChar(0x1000); + sample += TQChar(0x1001); + sample += TQChar(0x1002); + sample += TQChar(0x1003); + break; + case TQFont::Khmer: + sample += TQChar(0x1780); + sample += TQChar(0x1790); + sample += TQChar(0x17b0); + sample += TQChar(0x17c0); + break; + + + + case TQFont::Han: + sample += TQChar(0x6f84); + sample += TQChar(0x820a); + sample += TQChar(0x61a9); + sample += TQChar(0x9781); + break; + case TQFont::Hiragana: + sample += TQChar(0x3050); + sample += TQChar(0x3060); + sample += TQChar(0x3070); + sample += TQChar(0x3080); + break; + case TQFont::Katakana: + sample += TQChar(0x30b0); + sample += TQChar(0x30c0); + sample += TQChar(0x30d0); + sample += TQChar(0x30e0); + break; + case TQFont::Hangul: + sample += TQChar(0xac00); + sample += TQChar(0xac11); + sample += TQChar(0xac1a); + sample += TQChar(0xac2f); + break; + case TQFont::Bopomofo: + sample += TQChar(0x3105); + sample += TQChar(0x3115); + sample += TQChar(0x3125); + sample += TQChar(0x3129); + break; + case TQFont::Yi: + sample += TQChar(0xa1a8); + sample += TQChar(0xa1a6); + sample += TQChar(0xa200); + sample += TQChar(0xa280); + break; + + + + case TQFont::Ethiopic: + sample += TQChar(0x1200); + sample += TQChar(0x1240); + sample += TQChar(0x1280); + sample += TQChar(0x12c0); + break; + case TQFont::Cherokee: + sample += TQChar(0x13a0); + sample += TQChar(0x13b0); + sample += TQChar(0x13c0); + sample += TQChar(0x13d0); + break; + case TQFont::CanadianAboriginal: + sample += TQChar(0x1410); + sample += TQChar(0x1500); + sample += TQChar(0x15f0); + sample += TQChar(0x1650); + break; + case TQFont::Mongolian: + sample += TQChar(0x1820); + sample += TQChar(0x1840); + sample += TQChar(0x1860); + sample += TQChar(0x1880); + break; + + + case TQFont::CurrencySymbols: + case TQFont::LetterlikeSymbols: + case TQFont::NumberForms: + case TQFont::MathematicalOperators: + case TQFont::TechnicalSymbols: + case TQFont::GeometricSymbols: + case TQFont::MiscellaneousSymbols: + case TQFont::EnclosedAndSquare: + case TQFont::Braille: + break; + + + case TQFont::Unicode: + sample += TQChar(0x0174); + sample += TQChar(0x0628); + sample += TQChar(0x0e02); + sample += TQChar(0x263A); + sample += TQChar(0x3129); + sample += TQChar(0x61a9); + sample += TQChar(0xac2f); + break; + + + + default: + sample += TQChar(0xfffd); + sample += TQChar(0xfffd); + sample += TQChar(0xfffd); + sample += TQChar(0xfffd); + break; + } + + return sample; +} + + + + +/*! + \internal + + This makes sense of the font family name: + + 1) if the family name contains a '-' (ie. "Adobe-Courier"), then we + split at the '-', and use the string as the foundry, and the string to + the right as the family + + 2) if the family name contains a '[' and a ']', then we take the text + between the square brackets as the foundry, and the text before the + square brackets as the family (ie. "Arial [Monotype]") +*/ +void TQFontDatabase::parseFontName(const TQString &name, TQString &foundry, TQString &family) +{ + if ( name.contains('-') ) { + int i = name.find('-'); + foundry = name.left( i ); + family = name.right( name.length() - i - 1 ); + } else if ( name.contains('[') && name.contains(']')) { + int i = name.find('['); + int li = name.findRev(']'); + + if (i < li) { + foundry = name.mid(i + 1, li - i - 1); + if (name[i - 1] == ' ') + i--; + family = name.left(i); + } + } else { + foundry = TQString::null; + family = name; + } +} + +#endif // QT_NO_FONTDATABASE diff --git a/src/kernel/qfontdatabase.h b/src/kernel/qfontdatabase.h new file mode 100644 index 000000000..1378bd4f7 --- /dev/null +++ b/src/kernel/qfontdatabase.h @@ -0,0 +1,227 @@ +/**************************************************************************** +** +** Definition of the TQFontDatabase class +** +** Created : 981126 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQFONTDATABASE_H +#define TQFONTDATABASE_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qstring.h" +#include "qstringlist.h" +#include "qfont.h" +#include "qvaluelist.h" +#endif // QT_H + + +#ifndef QT_NO_FONTDATABASE + +class TQFontStylePrivate; /* Don't touch! */ +struct TQtFontStyle; +struct TQtFontFamily; +struct TQtFontFoundry; +struct TQFontDef; +class TQFontEngine; +#ifdef Q_WS_QWS +class TQDiskFont; +#endif + +class TQFontDatabasePrivate; + +class Q_EXPORT TQFontDatabase +{ +public: + static TQValueList standardSizes(); + + TQFontDatabase(); + + TQStringList families() const; + TQStringList families( TQFont::Script ) const; + TQStringList styles( const TQString & ) const; + TQValueList pointSizes( const TQString &, const TQString & = TQString::null); + TQValueList smoothSizes( const TQString &, const TQString &); + TQString styleString( const TQFont &); + + TQFont font( const TQString &, const TQString &, int); + + bool isBitmapScalable( const TQString &, const TQString & = TQString::null) const; + bool isSmoothlyScalable( const TQString &, const TQString & = TQString::null) const; + bool isScalable( const TQString &, const TQString & = TQString::null) const; + bool isFixedPitch( const TQString &, const TQString & = TQString::null) const; + + bool italic( const TQString &, const TQString &) const; + bool bold( const TQString &, const TQString &) const; + int weight( const TQString &, const TQString &) const; + + static TQString scriptName(TQFont::Script); + static TQString scriptSample(TQFont::Script); + +#ifdef Q_WS_QWS + static void qwsAddDiskFont( TQDiskFont *qdf ); +#endif + + // For source compatibility with < 3.0 +#ifndef QT_NO_COMPAT + + TQStringList families(bool) const; + TQStringList styles( const TQString &, const TQString & ) const; + TQValueList pointSizes( const TQString &, const TQString &, const TQString & ); + TQValueList smoothSizes( const TQString &, const TQString &, const TQString & ); + + TQFont font( const TQString &, const TQString &, int, const TQString &); + + bool isBitmapScalable( const TQString &, const TQString &, const TQString & ) const; + bool isSmoothlyScalable( const TQString &, const TQString &, const TQString & ) const; + bool isScalable( const TQString &, const TQString &, const TQString & ) const; + bool isFixedPitch( const TQString &, const TQString &, const TQString & ) const; + + bool italic( const TQString &, const TQString &, const TQString & ) const; + bool bold( const TQString &, const TQString &, const TQString & ) const; + int weight( const TQString &, const TQString &, const TQString & ) const; + +#endif // QT_NO_COMPAT + +private: +#if defined(Q_WS_X11) || defined(Q_WS_WIN) + static TQFontEngine *findFont( TQFont::Script script, const TQFontPrivate *fp, + const TQFontDef &request, int force_encoding_id = -1 ); +#endif // Q_WS_X11 + + static void createDatabase(); + + static void parseFontName(const TQString &name, TQString &foundry, TQString &family); + + friend struct TQFontDef; + friend class TQFontPrivate; + friend class TQFontDialog; + friend class TQFontEngineLatinXLFD; + + TQFontDatabasePrivate *d; +}; + + +#ifndef QT_NO_COMPAT + +inline TQStringList TQFontDatabase::families( bool ) const +{ + return families(); +} + +inline TQStringList TQFontDatabase::styles( const TQString &family, + const TQString & ) const +{ + return styles(family); +} + +inline TQValueList TQFontDatabase::pointSizes( const TQString &family, + const TQString &style , + const TQString & ) +{ + return pointSizes(family, style); +} + +inline TQValueList TQFontDatabase::smoothSizes( const TQString &family, + const TQString &style, + const TQString & ) +{ + return smoothSizes(family, style); +} + +inline TQFont TQFontDatabase::font( const TQString &familyName, + const TQString &style, + int pointSize, + const TQString &) +{ + return font(familyName, style, pointSize); +} + +inline bool TQFontDatabase::isBitmapScalable( const TQString &family, + const TQString &style, + const TQString & ) const +{ + return isBitmapScalable(family, style); +} + +inline bool TQFontDatabase::isSmoothlyScalable( const TQString &family, + const TQString &style, + const TQString & ) const +{ + return isSmoothlyScalable(family, style); +} + +inline bool TQFontDatabase::isScalable( const TQString &family, + const TQString &style, + const TQString & ) const +{ + return isScalable(family, style); +} + +inline bool TQFontDatabase::isFixedPitch( const TQString &family, + const TQString &style, + const TQString & ) const +{ + return isFixedPitch(family, style); +} + +inline bool TQFontDatabase::italic( const TQString &family, + const TQString &style, + const TQString & ) const +{ + return italic(family, style); +} + +inline bool TQFontDatabase::bold( const TQString &family, + const TQString &style, + const TQString & ) const +{ + return bold(family, style); +} + +inline int TQFontDatabase::weight( const TQString &family, + const TQString &style, + const TQString & ) const +{ + return weight(family, style); +} + +#endif // QT_NO_COMPAT + +#endif // QT_NO_FONTDATABASE + +#endif // TQFONTDATABASE_H diff --git a/src/kernel/qfontdatabase_x11.cpp b/src/kernel/qfontdatabase_x11.cpp new file mode 100644 index 000000000..2c6775954 --- /dev/null +++ b/src/kernel/qfontdatabase_x11.cpp @@ -0,0 +1,2014 @@ +/**************************************************************************** +** +** Implementation of platform specific TQFontDatabase +** +** Created : 970521 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include + +#include +#include + +#include "qt_x11_p.h" + +#include +#include + +#include +#include +#include +#include + +#ifndef QT_NO_XFTFREETYPE +#include +#include FT_FREETYPE_H +#endif + +#ifndef QT_XFT2 +#define FcBool Bool +#define FcTrue True +#define FcFalse False +#endif + +#ifdef TQFONTDATABASE_DEBUG +# define FD_DEBUG qDebug +#else +# define FD_DEBUG if (FALSE) qDebug +#endif // TQFONTDATABASE_DEBUG + +// from qfont_x11.cpp +extern double qt_pointSize(double pixelSize, TQPaintDevice *paintdevice, int screen); +extern double qt_pixelSize(double pointSize, TQPaintDevice *paintdevice, int screen); + + +static inline void capitalize ( char *s ) +{ + bool space = TRUE; + while( *s ) { + if ( space ) + *s = toupper( *s ); + space = ( *s == ' ' ); + ++s; + } +} + + +// ----- begin of generated code ----- + +#define make_tag( c1, c2, c3, c4 ) \ +( (((unsigned int)c1)<<24) | (((unsigned int)c2)<<16) | \ +(((unsigned int)c3)<<8) | ((unsigned int)c4) ) + +struct XlfdEncoding { + const char *name; + int id; + int mib; + unsigned int hash1; + unsigned int hash2; +}; + +static const XlfdEncoding xlfd_encoding[] = { + { "iso8859-1", 0, 4, make_tag('i','s','o','8'), make_tag('5','9','-','1') }, + { "iso8859-2", 1, 5, make_tag('i','s','o','8'), make_tag('5','9','-','2') }, + { "iso8859-3", 2, 6, make_tag('i','s','o','8'), make_tag('5','9','-','3') }, + { "iso8859-4", 3, 7, make_tag('i','s','o','8'), make_tag('5','9','-','4') }, + { "iso8859-9", 4, 12, make_tag('i','s','o','8'), make_tag('5','9','-','9') }, + { "iso8859-10", 5, 13, make_tag('i','s','o','8'), make_tag('9','-','1','0') }, + { "iso8859-13", 6, 109, make_tag('i','s','o','8'), make_tag('9','-','1','3') }, + { "iso8859-14", 7, 110, make_tag('i','s','o','8'), make_tag('9','-','1','4') }, + { "iso8859-15", 8, 111, make_tag('i','s','o','8'), make_tag('9','-','1','5') }, + { "hp-roman8", 9, 2004, make_tag('h','p','-','r'), make_tag('m','a','n','8') }, + { "jisx0208*-0", 10, 63, make_tag('j','i','s','x'), 0 }, +#define LAST_LATIN_ENCODING 10 + { "iso8859-5", 11, 8, make_tag('i','s','o','8'), make_tag('5','9','-','5') }, + { "*-cp1251", 12, 2251, 0, make_tag('1','2','5','1') }, + { "koi8-ru", 13, 2084, make_tag('k','o','i','8'), make_tag('8','-','r','u') }, + { "koi8-u", 14, 2088, make_tag('k','o','i','8'), make_tag('i','8','-','u') }, + { "koi8-r", 15, 2084, make_tag('k','o','i','8'), make_tag('i','8','-','r') }, + { "iso8859-7", 16, 10, make_tag('i','s','o','8'), make_tag('5','9','-','7') }, + { "iso10646-1", 17, 0, make_tag('i','s','o','1'), make_tag('4','6','-','1') }, + { "iso8859-8", 18, 85, make_tag('i','s','o','8'), make_tag('5','9','-','8') }, + { "gb18030-0", 19, -114, make_tag('g','b','1','8'), make_tag('3','0','-','0') }, + { "gb18030.2000-0", 20, -113, make_tag('g','b','1','8'), make_tag('0','0','-','0') }, + { "gbk-0", 21, -113, make_tag('g','b','k','-'), make_tag('b','k','-','0') }, + { "gb2312.*-0", 22, 57, make_tag('g','b','2','3'), 0 }, + { "jisx0201*-0", 23, 15, make_tag('j','i','s','x'), 0 }, + { "ksc5601*-*", 24, 36, make_tag('k','s','c','5'), 0 }, + { "big5hkscs-0", 25, -2101, make_tag('b','i','g','5'), make_tag('c','s','-','0') }, + { "hkscs-1", 26, -2101, make_tag('h','k','s','c'), make_tag('c','s','-','1') }, + { "big5*-*", 27, -2026, make_tag('b','i','g','5'), 0 }, + { "tscii-*", 28, 2028, make_tag('t','s','c','i'), 0 }, + { "tis620*-*", 29, 2259, make_tag('t','i','s','6'), 0 }, + { "iso8859-11", 30, 2259, make_tag('i','s','o','8'), make_tag('9','-','1','1') }, + { "mulelao-1", 31, -4242, make_tag('m','u','l','e'), make_tag('a','o','-','1') }, + { "ethiopic-unicode", 32, 0, make_tag('e','t','h','i'), make_tag('c','o','d','e') }, + { "unicode-*", 33, 0, make_tag('u','n','i','c'), 0 }, + { "*-symbol", 34, 0, 0, make_tag('m','b','o','l') }, + { "*-fontspecific", 35, 0, 0, make_tag('i','f','i','c') }, + { "fontspecific-*", 36, 0, make_tag('f','o','n','t'), 0 }, + { 0, 0, 0, 0, 0 } +}; + +static const char scripts_for_xlfd_encoding[37][61] = { + // iso8859-1 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-2 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-3 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-4 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-9 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-10 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-13 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-14 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-15 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // hp-roman8 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // jisx0208*-0 + { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0 }, + // iso8859-5 + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // *-cp1251 + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // koi8-ru + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // koi8-u + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // koi8-r + { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-7 + { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso10646-1 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-8 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // gb18030-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0 }, + // gb18030.2000-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0 }, + // gbk-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0 }, + // gb2312.*-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0 }, + // jisx0201*-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // ksc5601*-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1 }, + // big5hkscs-0 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0 }, + // hkscs-1 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0 }, + // big5*-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0 }, + // tscii-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // tis620*-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // iso8859-11 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // mulelao-1 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // ethiopic-unicode + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // unicode-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0 }, + // *-symbol + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0 }, + // *-fontspecific + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0 }, + // fontspecific-* + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0 } + +}; + +// ----- end of generated code ----- + + +const int numEncodings = sizeof( xlfd_encoding ) / sizeof( XlfdEncoding ) - 1; + +int qt_xlfd_encoding_id( const char *encoding ) +{ + // qDebug("looking for encoding id for '%s'", encoding ); + int len = strlen( encoding ); + if ( len < 4 ) + return -1; + unsigned int hash1 = make_tag( encoding[0], encoding[1], encoding[2], encoding[3] ); + const char *ch = encoding + len - 4; + unsigned int hash2 = make_tag( ch[0], ch[1], ch[2], ch[3] ); + + const XlfdEncoding *enc = xlfd_encoding; + for ( ; enc->name; ++enc ) { + if ( (enc->hash1 && enc->hash1 != hash1) || + (enc->hash2 && enc->hash2 != hash2) ) + continue; + // hashes match, do a compare if strings match + // the enc->name can contain '*'s we have to interpret correctly + const char *n = enc->name; + const char *e = encoding; + while ( 1 ) { + // qDebug("bol: *e='%c', *n='%c'", *e, *n ); + if ( *e == '\0' ) { + if ( *n ) + break; + // qDebug( "found encoding id %d", enc->id ); + return enc->id; + } + if ( *e == *n ) { + ++e; + ++n; + continue; + } + if ( *n != '*' ) + break; + ++n; + // qDebug("skip: *e='%c', *n='%c'", *e, *n ); + while ( *e && *e != *n ) + ++e; + } + } + // qDebug( "couldn't find encoding %s", encoding ); + return -1; +} + +int qt_mib_for_xlfd_encoding( const char *encoding ) +{ + int id = qt_xlfd_encoding_id( encoding ); + if ( id != -1 ) return xlfd_encoding[id].mib; + return 0; +} + +int qt_encoding_id_for_mib( int mib ) +{ + const XlfdEncoding *enc = xlfd_encoding; + for ( ; enc->name; ++enc ) { + if ( enc->mib == mib ) + return enc->id; + } + return -1; +} + +static const char * xlfd_for_id( int id ) +{ + // special case: -1 returns the "*-*" encoding, allowing us to do full + // database population in a single X server round trip. + if ( id < 0 || id > numEncodings ) + return "*-*"; + return xlfd_encoding[id].name; +} + +enum XLFDFieldNames { + Foundry, + Family, + Weight, + Slant, + Width, + AddStyle, + PixelSize, + PointSize, + ResolutionX, + ResolutionY, + Spacing, + AverageWidth, + CharsetRegistry, + CharsetEncoding, + NFontFields +}; + +// Splits an X font name into fields separated by '-' +static bool parseXFontName( char *fontName, char **tokens ) +{ + if ( ! fontName || fontName[0] == '0' || fontName[0] != '-' ) { + tokens[0] = 0; + return FALSE; + } + + int i; + ++fontName; + for ( i = 0; i < NFontFields && fontName && fontName[0]; ++i ) { + tokens[i] = fontName; + for ( ;; ++fontName ) { + if ( *fontName == '-' ) + break; + if ( ! *fontName ) { + fontName = 0; + break; + } + } + + if ( fontName ) *fontName++ = '\0'; + } + + if ( i < NFontFields ) { + for ( int j = i ; j < NFontFields; ++j ) + tokens[j] = 0; + return FALSE; + } + + return TRUE; +} + +static inline bool isZero(char *x) +{ + return (x[0] == '0' && x[1] == 0); +} + +static inline bool isScalable( char **tokens ) +{ + return (isZero(tokens[PixelSize]) && + isZero(tokens[PointSize]) && + isZero(tokens[AverageWidth])); +} + +static inline bool isSmoothlyScalable( char **tokens ) +{ + return (isZero(tokens[ResolutionX]) && + isZero(tokens[ResolutionY])); +} + +static inline bool isFixedPitch( char **tokens ) +{ + return (tokens[Spacing][0] == 'm' || + tokens[Spacing][0] == 'c' || + tokens[Spacing][0] == 'M' || + tokens[Spacing][0] == 'C'); +} + +/* + Fills in a font definition (TQFontDef) from an XLFD (X Logical Font + Description). + + Returns TRUE if the the given xlfd is valid. The fields lbearing + and rbearing are not given any values. +*/ +bool qt_fillFontDef( const TQCString &xlfd, TQFontDef *fd, int screen ) +{ + char *tokens[NFontFields]; + TQCString buffer = xlfd.copy(); + if ( ! parseXFontName(buffer.data(), tokens) ) + return FALSE; + + capitalize(tokens[Family]); + capitalize(tokens[Foundry]); + + fd->family = TQString::fromLatin1(tokens[Family]); + TQString foundry = TQString::fromLatin1(tokens[Foundry]); + if ( ! foundry.isEmpty() && foundry != TQString::fromLatin1("*") ) + fd->family += + TQString::fromLatin1(" [") + foundry + TQString::fromLatin1("]"); + + if ( qstrlen( tokens[AddStyle] ) > 0 ) + fd->addStyle = TQString::fromLatin1(tokens[AddStyle]); + else + fd->addStyle = TQString::null; + + fd->pointSize = atoi(tokens[PointSize]); + fd->styleHint = TQFont::AnyStyle; // ### any until we match families + + char slant = tolower( (uchar) tokens[Slant][0] ); + fd->italic = ( slant == 'o' || slant == 'i' ); + char fixed = tolower( (uchar) tokens[Spacing][0] ); + fd->fixedPitch = ( fixed == 'm' || fixed == 'c' ); + fd->weight = getFontWeight( tokens[Weight] ); + + int r = atoi(tokens[ResolutionY]); + fd->pixelSize = atoi(tokens[PixelSize]); + // not "0" or "*", or retquired DPI + if ( r && fd->pixelSize && TQPaintDevice::x11AppDpiY( screen ) && + r != TQPaintDevice::x11AppDpiY( screen ) ) { + // calculate actual pointsize for display DPI + fd->pointSize = qRound(qt_pointSize(fd->pixelSize, 0, screen) * 10.); + } else if ( fd->pixelSize == 0 && fd->pointSize ) { + // calculate pixel size from pointsize/dpi + fd->pixelSize = qRound(qt_pixelSize(fd->pointSize / 10., 0, screen)); + } + + return TRUE; +} + +/* + Fills in a font definition (TQFontDef) from the font properties in an + XFontStruct. + + Returns TRUE if the TQFontDef could be filled with properties from + the XFontStruct. The fields lbearing and rbearing are not given any + values. +*/ +static bool qt_fillFontDef( XFontStruct *fs, TQFontDef *fd, int screen ) +{ + unsigned long value; + if ( fs && !XGetFontProperty( fs, XA_FONT, &value ) ) + return FALSE; + + char *n = XGetAtomName( TQPaintDevice::x11AppDisplay(), value ); + TQCString xlfd( n ); + if ( n ) + XFree( n ); + return qt_fillFontDef( xlfd.lower(), fd, screen ); +} + + +static TQtFontStyle::Key getStyle( char ** tokens ) +{ + TQtFontStyle::Key key; + + char slant0 = tolower( (uchar) tokens[Slant][0] ); + + if ( slant0 == 'r' ) { + if ( tokens[Slant][1]) { + char slant1 = tolower( (uchar) tokens[Slant][1] ); + + if ( slant1 == 'o' ) + key.oblique = TRUE; + else if ( slant1 == 'i' ) + key.italic = TRUE; + } + } else if ( slant0 == 'o' ) + key.oblique = TRUE; + else if ( slant0 == 'i' ) + key.italic = TRUE; + + key.weight = getFontWeight( tokens[Weight] ); + + if ( qstrcmp( tokens[Width], "normal" ) == 0 ) { + key.stretch = 100; + } else if ( qstrcmp( tokens[Width], "semi condensed" ) == 0 || + qstrcmp( tokens[Width], "semicondensed" ) == 0 ) { + key.stretch = 90; + } else if ( qstrcmp( tokens[Width], "condensed" ) == 0 ) { + key.stretch = 80; + } else if ( qstrcmp( tokens[Width], "narrow" ) == 0 ) { + key.stretch = 60; + } + + return key; +} + + +extern bool qt_has_xft; // defined in qfont_x11.cpp + +static bool xlfdsFullyLoaded = FALSE; +static unsigned char encodingLoaded[numEncodings]; + +static void loadXlfds( const char *reqFamily, int encoding_id ) +{ + TQtFontFamily *fontFamily = reqFamily ? db->family( reqFamily ) : 0; + + // make sure we don't load twice + if ( (encoding_id == -1 && xlfdsFullyLoaded) || (encoding_id != -1 && encodingLoaded[encoding_id]) ) + return; + if ( fontFamily && fontFamily->xlfdLoaded ) + return; + +#ifdef QT_XFT2 + if ( !qt_has_xft ) { +#endif // QT_XFT2 + int fontCount; + // force the X server to give us XLFDs + TQCString xlfd_pattern = "-*-"; + xlfd_pattern += reqFamily ? reqFamily : "*"; + xlfd_pattern += "-*-*-*-*-*-*-*-*-*-*-"; + xlfd_pattern += xlfd_for_id( encoding_id ); + + char **fontList = XListFonts( TQPaintDevice::x11AppDisplay(), + xlfd_pattern.data(), + 0xffff, &fontCount ); + // qDebug("requesting xlfd='%s', got %d fonts", xlfd_pattern.data(), fontCount ); + + + char *tokens[NFontFields]; + + for( int i = 0 ; i < fontCount ; i++ ) { + if ( ! parseXFontName( fontList[i], tokens ) ) continue; + + // get the encoding_id for this xlfd. we need to do this + // here, since we can pass -1 to this function to do full + // database population + *(tokens[CharsetEncoding]-1) = '-'; + int encoding_id = qt_xlfd_encoding_id( tokens[CharsetRegistry] ); + if ( encoding_id == -1 ) + continue; + + char *familyName = tokens[Family]; + capitalize( familyName ); + char *foundryName = tokens[Foundry]; + capitalize( foundryName ); + TQtFontStyle::Key styleKey = getStyle( tokens ); + + bool smooth_scalable = FALSE; + bool bitmap_scalable = FALSE; + if ( isScalable(tokens) ) { + if ( isSmoothlyScalable( tokens ) ) + smooth_scalable = TRUE; + else + bitmap_scalable = TRUE; + } + uint pixelSize = atoi( tokens[PixelSize] ); + uint xpointSize = atoi( tokens[PointSize] ); + uint xres = atoi( tokens[ResolutionX] ); + uint yres = atoi( tokens[ResolutionY] ); + uint avgwidth = atoi( tokens[AverageWidth] ); + bool fixedPitch = isFixedPitch( tokens ); + + if (avgwidth == 0 && pixelSize != 0) { + /* + Ignore bitmap scalable fonts that are automatically + generated by some X servers. We know they are bitmap + scalable because even though they have a specified pixel + size, the average width is zero. + */ + continue; + } + + TQtFontFamily *family = fontFamily ? fontFamily : db->family( familyName, TRUE ); + family->fontFileIndex = -1; + TQtFontFoundry *foundry = family->foundry( foundryName, TRUE ); + TQtFontStyle *style = foundry->style( styleKey, TRUE ); + + delete [] style->weightName; + style->weightName = qstrdup( tokens[Weight] ); + delete [] style->setwidthName; + style->setwidthName = qstrdup( tokens[Width] ); + + if ( smooth_scalable ) { + style->smoothScalable = TRUE; + style->bitmapScalable = FALSE; + pixelSize = SMOOTH_SCALABLE; + } + if ( !style->smoothScalable && bitmap_scalable ) + style->bitmapScalable = TRUE; + if ( !fixedPitch ) + family->fixedPitch = FALSE; + + TQtFontSize *size = style->pixelSize( pixelSize, TRUE ); + TQtFontEncoding *enc = + size->encodingID( encoding_id, xpointSize, xres, yres, avgwidth, TRUE ); + enc->pitch = *tokens[Spacing]; + if ( !enc->pitch ) enc->pitch = '*'; + + for ( int script = 0; script < TQFont::LastPrivateScript; ++script ) { + if ( scripts_for_xlfd_encoding[encoding_id][script] ) + family->scripts[script] = TQtFontFamily::Supported; + else + family->scripts[script] |= TQtFontFamily::UnSupported_Xlfd; + } + if ( encoding_id == -1 ) + family->xlfdLoaded = TRUE; + } + if ( !reqFamily ) { + // mark encoding as loaded + if ( encoding_id == -1 ) + xlfdsFullyLoaded = TRUE; + else + encodingLoaded[encoding_id] = TRUE; + } + + XFreeFontNames( fontList ); + +#ifdef QT_XFT2 + } +#endif // QT_XFT2 +} + +#ifndef QT_NO_XFTFREETYPE +static int getXftWeight(int xftweight) +{ + int qtweight = TQFont::Black; + if (xftweight <= (XFT_WEIGHT_LIGHT + XFT_WEIGHT_MEDIUM) / 2) + qtweight = TQFont::Light; + else if (xftweight <= (XFT_WEIGHT_MEDIUM + XFT_WEIGHT_DEMIBOLD) / 2) + qtweight = TQFont::Normal; + else if (xftweight <= (XFT_WEIGHT_DEMIBOLD + XFT_WEIGHT_BOLD) / 2) + qtweight = TQFont::DemiBold; + else if (xftweight <= (XFT_WEIGHT_BOLD + XFT_WEIGHT_BLACK) / 2) + qtweight = TQFont::Bold; + + return qtweight; +} + +static void loadXft() +{ + if (!qt_has_xft) + return; + +#ifdef QT_XFT2 + struct XftDefaultFont { + const char *qtname; + const char *rawname; + bool fixed; + }; + const XftDefaultFont defaults[] = { + { "Serif", "serif", FALSE }, + { "Sans Serif", "sans-serif", FALSE }, + { "Monospace", "monospace", TRUE }, + { 0, 0, FALSE } + }; + const XftDefaultFont *f = defaults; + while (f->qtname) { + TQtFontFamily *family = db->family( f->qtname, TRUE ); + family->fixedPitch = f->fixed; + family->rawName = f->rawname; + family->hasXft = TRUE; + family->synthetic = TRUE; + TQtFontFoundry *foundry + = family->foundry( TQString::null, TRUE ); + + for ( int i = 0; i < TQFont::LastPrivateScript; ++i ) { + if (i == TQFont::UnknownScript) + continue; + family->scripts[i] = TQtFontFamily::Supported; + } + + TQtFontStyle::Key styleKey; + styleKey.oblique = FALSE; + for (int i = 0; i < 4; ++i) { + styleKey.italic = (i%2); + styleKey.weight = (i > 1) ? TQFont::Bold : TQFont::Normal; + TQtFontStyle *style = foundry->style( styleKey, TRUE ); + style->smoothScalable = TRUE; + TQtFontSize *size = style->pixelSize( SMOOTH_SCALABLE, TRUE ); + TQtFontEncoding *enc = size->encodingID( -1, 0, 0, 0, 0, TRUE ); + enc->pitch = (f->fixed ? 'm' : 'p'); + } + ++f; + } +#endif +} + +#ifdef XFT_MATRIX +static void checkXftMatrix( TQtFontFamily* family ) { + for ( int j = 0; j < family->count; ++j ) { // each foundry + TQtFontFoundry *foundry = family->foundries[j]; + for ( int k = 0; k < foundry->count; ++k ) { + TQtFontStyle *style = foundry->styles[k]; + if ( style->key.italic || style->key.oblique ) continue; + + TQtFontSize *size = style->pixelSize( SMOOTH_SCALABLE ); + if ( ! size ) continue; + TQtFontEncoding *enc = size->encodingID( -1, 0, 0, 0, 0, TRUE ); + if ( ! enc ) continue; + + TQtFontStyle::Key key = style->key; + + // does this style have an italic equivalent? + key.italic = TRUE; + TQtFontStyle *equiv = foundry->style( key ); + if ( equiv ) continue; + + // does this style have an oblique equivalent? + key.italic = FALSE; + key.oblique = TRUE; + equiv = foundry->style( key ); + if ( equiv ) continue; + + // let's fake one... + equiv = foundry->style( key, TRUE ); + equiv->fakeOblique = TRUE; + equiv->smoothScalable = TRUE; + + TQtFontSize *equiv_size = equiv->pixelSize( SMOOTH_SCALABLE, TRUE ); + TQtFontEncoding *equiv_enc = equiv_size->encodingID( -1, 0, 0, 0, 0, TRUE ); + + // keep the same pitch + equiv_enc->pitch = enc->pitch; + } + } +} +#endif // XFT_MATRIX + +static bool loadXftFont( FcPattern* font ) +{ + TQString familyName; + TQString rawName; + char *value; + int weight_value; + int slant_value; + int spacing_value; + char *file_value; + int index_value; + char *foundry_value = 0; + FcBool scalable = FcTrue; + + if (XftPatternGetString( font, + XFT_FAMILY, 0, &value) != XftResultMatch ) + return false; + // capitalize( value ); + rawName = familyName = TQString::fromUtf8(value); + familyName.replace('-', ' '); + familyName.replace("/", ""); + + slant_value = XFT_SLANT_ROMAN; + weight_value = XFT_WEIGHT_MEDIUM; + spacing_value = XFT_PROPORTIONAL; + file_value = 0; + index_value = 0; + XftPatternGetInteger (font, XFT_SLANT, 0, &slant_value); + XftPatternGetInteger (font, XFT_WEIGHT, 0, &weight_value); + XftPatternGetInteger (font, XFT_SPACING, 0, &spacing_value); + XftPatternGetString (font, XFT_FILE, 0, &file_value); + XftPatternGetInteger (font, XFT_INDEX, 0, &index_value); +#ifdef QT_XFT2 + FcPatternGetBool(font, FC_SCALABLE, 0, &scalable); + foundry_value = 0; + XftPatternGetString(font, FC_FOUNDRY, 0, &foundry_value); +#endif + TQtFontFamily *family = db->family( familyName, TRUE ); + family->rawName = rawName; + family->hasXft = TRUE; + +#ifdef QT_XFT2 + FcCharSet *charset = 0; + FcResult res = FcPatternGetCharSet(font, FC_CHARSET, 0, &charset); + if (res == FcResultMatch && FcCharSetCount(charset) > 1) { + for (int i = 0; i < TQFont::LastPrivateScript; ++i) { + bool supported = sample_chars[i][0]; + for (int j = 0; sample_chars[i][j]; ++j){ + if (!FcCharSetHasChar(charset, sample_chars[i][j])) { + supported = false; + break; + } + } + if ( supported ){ + family->scripts[i] = TQtFontFamily::Supported; + } else { + family->scripts[i] |= TQtFontFamily::UnSupported_Xft; + } + } + family->xftScriptCheck = TRUE; + } else { + // we set UnknownScript to supported for symbol fonts. It makes no sense to merge these + // with other ones, as they are special in a way. + for ( int i = 0; i < TQFont::LastPrivateScript; ++i ) + family->scripts[i] |= TQtFontFamily::UnSupported_Xft; + family->scripts[TQFont::UnknownScript] = TQtFontFamily::Supported; + } +#endif // QT_XFT2 + + TQCString file = (file_value ? file_value : ""); + family->fontFilename = file; + family->fontFileIndex = index_value; + + TQtFontStyle::Key styleKey; + styleKey.italic = (slant_value == XFT_SLANT_ITALIC); + styleKey.oblique = (slant_value == XFT_SLANT_OBLIQUE); + styleKey.weight = getXftWeight( weight_value ); +#ifdef QT_XFT2 + if (!scalable) { + int width = 100; +#if FC_VERSION >= 20193 + XftPatternGetInteger (font, FC_WIDTH, 0, &width); +#endif + styleKey.stretch = width; + } +#endif + + TQtFontFoundry *foundry + = family->foundry( foundry_value ? TQString::fromUtf8(foundry_value) : TQString::null, TRUE ); + TQtFontStyle *style = foundry->style( styleKey, TRUE ); + + if (spacing_value < XFT_MONO ) + family->fixedPitch = FALSE; + + TQtFontSize *size; + if (scalable) { + style->smoothScalable = TRUE; + size = style->pixelSize( SMOOTH_SCALABLE, TRUE ); + } +#ifdef QT_XFT2 + else { + double pixel_size = 0; + XftPatternGetDouble (font, FC_PIXEL_SIZE, 0, &pixel_size); + size = style->pixelSize( (int)pixel_size, TRUE ); + } +#endif + TQtFontEncoding *enc = size->encodingID( -1, 0, 0, 0, 0, TRUE ); + enc->pitch = ( spacing_value >= XFT_CHARCELL ? 'c' : + ( spacing_value >= XFT_MONO ? 'm' : 'p' ) ); + + checkXftMatrix( family ); + + return true; +} + +#ifndef QT_XFT2 + +#define MAKE_TAG( _x1, _x2, _x3, _x4 ) \ + ( ( (Q_UINT32)_x1 << 24 ) | \ + ( (Q_UINT32)_x2 << 16 ) | \ + ( (Q_UINT32)_x3 << 8 ) | \ + (Q_UINT32)_x4 ) + +#ifdef _POSIX_MAPPED_FILES +static inline Q_UINT32 getUInt(unsigned char *p) +{ + Q_UINT32 val; + val = *p++ << 24; + val |= *p++ << 16; + val |= *p++ << 8; + val |= *p; + + return val; +} + +static inline Q_UINT16 getUShort(unsigned char *p) +{ + Q_UINT16 val; + val = *p++ << 8; + val |= *p; + + return val; +} + +static inline void tag_to_string( char *string, Q_UINT32 tag ) +{ + string[0] = (tag >> 24)&0xff; + string[1] = (tag >> 16)&0xff; + string[2] = (tag >> 8)&0xff; + string[3] = tag&0xff; + string[4] = 0; +} + +static Q_UINT16 getGlyphIndex( unsigned char *table, Q_UINT16 format, unsigned short unicode ) +{ + if ( format == 0 ) { + if ( unicode < 256 ) + return (int) *(table+6+unicode); + } else if ( format == 2 ) { + qWarning("format 2 encoding table for Unicode, not implemented!"); + } else if ( format == 4 ) { + Q_UINT16 segCountX2 = getUShort( table + 6 ); + unsigned char *ends = table + 14; + Q_UINT16 endIndex = 0; + int i = 0; + for ( ; i < segCountX2/2 && (endIndex = getUShort( ends + 2*i )) < unicode; i++ ); + + unsigned char *idx = ends + segCountX2 + 2 + 2*i; + Q_UINT16 startIndex = getUShort( idx ); + + if ( startIndex > unicode ) + return 0; + + idx += segCountX2; + Q_INT16 idDelta = (Q_INT16)getUShort( idx ); + idx += segCountX2; + Q_UINT16 idRangeoffset_t = (Q_UINT16)getUShort( idx ); + + Q_UINT16 glyphIndex; + if ( idRangeoffset_t ) { + Q_UINT16 id = getUShort( idRangeoffset_t + 2*(unicode - startIndex) + idx); + if ( id ) + glyphIndex = ( idDelta + id ) % 0x10000; + else + glyphIndex = 0; + } else { + glyphIndex = (idDelta + unicode) % 0x10000; + } + return glyphIndex; + } + + return 0; +} +#endif // _POSIX_MAPPED_FILES + +static inline void checkXftCoverage( TQtFontFamily *family ) +{ +#ifdef _POSIX_MAPPED_FILES + TQCString ext = family->fontFilename.mid( family->fontFilename.findRev( '.' ) ).lower(); + if ( family->fontFileIndex == 0 && ( ext == ".ttf" || ext == ".otf" ) ) { + void *map; + // qDebug("using own ttf code coverage checking of '%s'!", family->name.latin1() ); + int fd = open( family->fontFilename.data(), O_RDONLY ); + size_t pagesize = getpagesize(); + off_t offset = 0; + size_t length = (8192 / pagesize + 1) * pagesize; + + if ( fd == -1 ) + goto xftCheck; + { + if ( (map = mmap( 0, length, PROT_READ, MAP_SHARED, fd, offset ) ) == MAP_FAILED ) + goto error; + + unsigned char *ttf = (unsigned char *)map; + Q_UINT32 version = getUInt( ttf ); + if ( version != 0x00010000 ) { + // qDebug("file has wrong version %x", version ); + goto error1; + } + Q_UINT16 numTables = getUShort( ttf+4 ); + + unsigned char *table_dir = ttf + 12; + Q_UINT32 cmap_offset = 0; + Q_UINT32 cmap_length = 0; + for ( int n = 0; n < numTables; n++ ) { + Q_UINT32 tag = getUInt( table_dir + 16*n ); + if ( tag == MAKE_TAG( 'c', 'm', 'a', 'p' ) ) { + cmap_offset = getUInt( table_dir + 16*n + 8 ); + cmap_length = getUInt( table_dir + 16*n + 12 ); + break; + } + } + if ( !cmap_offset ) { + // qDebug("no cmap found" ); + goto error1; + } + + if ( cmap_offset + cmap_length > length ) { + munmap( map, length ); + offset = cmap_offset / pagesize * pagesize; + cmap_offset -= offset; + length = (cmap_offset + cmap_length); + if ( (map = mmap( 0, length, PROT_READ, MAP_SHARED, fd, offset ) ) == MAP_FAILED ) + goto error; + } + + unsigned char *cmap = ((unsigned char *)map) + cmap_offset; + + version = getUShort( cmap ); + if ( version != 0 ) { + // qDebug("wrong cmap version" ); + goto error1; + } + numTables = getUShort( cmap + 2 ); + unsigned char *unicode_table = 0; + bool symbol_table = TRUE; + for ( int n = 0; n < numTables; n++ ) { + Q_UINT32 version = getUInt( cmap + 4 + 8*n ); + // accept both symbol and Unicode encodings. prefer unicode. + if ( version == 0x00030001 || version == 0x00030000 ) { + unicode_table = cmap + getUInt( cmap + 4 + 8*n + 4 ); + if ( version == 0x00030001 ) { + symbol_table = FALSE; + break; + } + } + } + + if ( !unicode_table ) { + // qDebug("no unicode table found" ); + goto error1; + } + + Q_UINT16 format = getUShort( unicode_table ); + if ( format != 4 ) + goto error1; + + if (symbol_table) { + // we set UnknownScript to supported for symbol fonts. It makes no sense to merge these + // with other ones, as they are special in a way. + for ( int i = 0; i < TQFont::LastPrivateScript; ++i ) + family->scripts[i] |= TQtFontFamily::UnSupported_Xft; + family->scripts[TQFont::UnknownScript] = TQtFontFamily::Supported; + } else { + for ( int i = 0; i < TQFont::LastPrivateScript; ++i ) { + + bool supported = sample_chars[i][0]; + for (int j = 0; sample_chars[i][j]; ++j) { + if (!getGlyphIndex(unicode_table, format, sample_chars[i][j])) { + supported=false; + break; + } + } + if ( supported ){ + // qDebug("font can render script %d", i ); + family->scripts[i] = TQtFontFamily::Supported; + } else { + family->scripts[i] |= TQtFontFamily::UnSupported_Xft; + } + } + } + family->xftScriptCheck = TRUE; + } + error1: + munmap( map, length ); + error: + close( fd ); + if ( family->xftScriptCheck ) + return; + } + xftCheck: +#endif // _POSIX_MAPPED_FILES + + FD_DEBUG("using Freetype for checking of '%s'", family->name.latin1() ); + + FT_Library ft_lib; + FT_Error error = FT_Init_FreeType( &ft_lib ); + if ( error ) return; + FT_Face face; + error = FT_New_Face( ft_lib, family->fontFilename, family->fontFileIndex, &face ); + if ( error ) return; + + for ( int i = 0; i < TQFont::LastPrivateScript; ++i ) { + bool supported = sample_chars[i][j]; + for (int j = 0; sample_chars[i][j]; ++j){ + if (!FT_Get_Char_Index(face, sample_chars[i][j])) { + supported=false; + break; + } + } + if ( supported ){ + FD_DEBUG("font can render char %04x, %04x script %d '%s'", + ch.unicode(), FT_Get_Char_Index ( face, ch.unicode() ), + i, TQFontDatabase::scriptName( (TQFont::Script)i ).latin1() ); + + family->scripts[i] = TQtFontFamily::Supported; + } else { + family->scripts[i] |= TQtFontFamily::UnSupported_Xft; + } + } + FT_Done_Face( face ); + FT_Done_FreeType( ft_lib ); + family->xftScriptCheck = TRUE; +} +#endif // QT_XFT2 +#endif // QT_NO_XFTFREETYPE + +static void load( const TQString &family = TQString::null, int script = -1 ) +{ +#ifdef TQFONTDATABASE_DEBUG + TQTime t; + t.start(); +#endif + + if ( family.isNull() ) { +#ifndef QT_NO_XFTFREETYPE + static bool xft_readall_done = false; + if (qt_has_xft && !xft_readall_done) { + xft_readall_done = true; + XftFontSet *fonts = + XftListFonts(TQPaintDevice::x11AppDisplay(), + TQPaintDevice::x11AppScreen(), + (const char *)0, + XFT_FAMILY, XFT_WEIGHT, XFT_SLANT, + XFT_SPACING, XFT_FILE, XFT_INDEX, +#ifdef QT_XFT2 + FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE, +#if FC_VERSION >= 20193 + FC_WIDTH, +#endif +#endif // QT_XFT2 + (const char *)0); + for (int i = 0; i < fonts->nfont; i++) + loadXftFont( fonts->fonts[i] ); + XftFontSetDestroy (fonts); + } +#ifdef QT_XFT2 + if (qt_has_xft) + return; +#endif +#endif // QT_NO_XFTFREETYPE + if ( script == -1 ) + loadXlfds( 0, -1 ); + else { + for ( int i = 0; i < numEncodings; i++ ) { + if ( scripts_for_xlfd_encoding[i][script] ) + loadXlfds( 0, i ); + } + } + } else { + TQtFontFamily *f = db->family( family, TRUE ); + if ( !f->fullyLoaded ) { + +#ifndef QT_NO_XFTFREETYPE + if (qt_has_xft) { + TQString mfamily = family; + redo: + XftFontSet *fonts = + XftListFonts(TQPaintDevice::x11AppDisplay(), + TQPaintDevice::x11AppScreen(), + XFT_FAMILY, XftTypeString, mfamily.utf8().data(), + (const char *)0, + XFT_FAMILY, XFT_WEIGHT, XFT_SLANT, + XFT_SPACING, XFT_FILE, XFT_INDEX, +#ifdef QT_XFT2 + FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE, +#if FC_VERSION >= 20193 + FC_WIDTH, +#endif +#endif // QT_XFT2 + (const char *)0); + for (int i = 0; i < fonts->nfont; i++) + loadXftFont( fonts->fonts[i] ); + XftFontSetDestroy (fonts); + if (mfamily.contains(' ')) { + mfamily.replace(TQChar(' '), TQChar('-')); + goto redo; + } + f->fullyLoaded = TRUE; +#ifdef QT_XFT2 + return; +#endif + } +#ifndef QT_XFT2 + // need to check Xft coverage + if ( f->hasXft && !f->xftScriptCheck ) { + checkXftCoverage( f ); + } +#endif +#endif // QT_NO_XFTFREETYPE + // could reduce this further with some more magic: + // would need to remember the encodings loaded for the family. + if ( ( script == -1 && !f->xlfdLoaded ) || + ( !f->hasXft && !(f->scripts[script] & TQtFontFamily::Supported) && + !(f->scripts[script] & TQtFontFamily::UnSupported_Xlfd) ) ) { + loadXlfds( family, -1 ); + f->fullyLoaded = TRUE; + } + } + } + +#ifdef TQFONTDATABASE_DEBUG + FD_DEBUG("TQFontDatabase: load( %s, %d) took %d ms", family.latin1(), script, t.elapsed() ); +#endif +} + + +static void initializeDb() +{ + if ( db ) return; + db = new TQFontDatabasePrivate; + qfontdatabase_cleanup.set(&db); + +#ifndef QT_XFT2 + memset( encodingLoaded, FALSE, sizeof( encodingLoaded ) ); +#endif + + TQTime t; + t.start(); + +#ifndef QT_NO_XFTFREETYPE + loadXft(); + FD_DEBUG("TQFontDatabase: loaded Xft: %d ms", t.elapsed() ); +#endif + + t.start(); + +#ifndef QT_NO_XFTFREETYPE + for ( int i = 0; i < db->count; i++ ) { +#ifndef QT_XFT2 + checkXftCoverage( db->families[i] ); + FD_DEBUG("TQFontDatabase: xft coverage check: %d ms", t.elapsed() ); +#endif // QT_XFT2 + +#ifdef XFT_MATRIX + checkXftMatrix( db->families[i] ); +#endif // XFT_MATRIX + } +#endif + + +#ifdef TQFONTDATABASE_DEBUG +#ifdef QT_XFT2 + if (!qt_has_xft) +#endif + // load everything at startup in debug mode. + loadXlfds( 0, -1 ); + + // print the database + for ( int f = 0; f < db->count; f++ ) { + TQtFontFamily *family = db->families[f]; + FD_DEBUG("'%s' %s hasXft=%s", family->name.latin1(), (family->fixedPitch ? "fixed" : ""), + (family->hasXft ? "yes" : "no") ); + for ( int i = 0; i < TQFont::LastPrivateScript; ++i ) { + FD_DEBUG("\t%s: %s", TQFontDatabase::scriptName((TQFont::Script) i).latin1(), + ((family->scripts[i] & TQtFontFamily::Supported) ? "Supported" : + (family->scripts[i] & TQtFontFamily::UnSupported) == TQtFontFamily::UnSupported ? + "UnSupported" : "Unknown")); + } + + for ( int fd = 0; fd < family->count; fd++ ) { + TQtFontFoundry *foundry = family->foundries[fd]; + FD_DEBUG("\t\t'%s'", foundry->name.latin1() ); + for ( int s = 0; s < foundry->count; s++ ) { + TQtFontStyle *style = foundry->styles[s]; + FD_DEBUG("\t\t\tstyle: italic=%d oblique=%d (fake=%d) weight=%d (%s)\n" + "\t\t\tstretch=%d (%s)", + style->key.italic, style->key.oblique, style->fakeOblique, style->key.weight, + style->weightName, style->key.stretch, + style->setwidthName ? style->setwidthName : "nil" ); + if ( style->smoothScalable ) + FD_DEBUG("\t\t\t\tsmooth scalable" ); + else if ( style->bitmapScalable ) + FD_DEBUG("\t\t\t\tbitmap scalable" ); + if ( style->pixelSizes ) { + qDebug("\t\t\t\t%d pixel sizes", style->count ); + for ( int z = 0; z < style->count; ++z ) { + TQtFontSize *size = style->pixelSizes + z; + for ( int e = 0; e < size->count; ++e ) { + FD_DEBUG( "\t\t\t\t size %5d pitch %c encoding %s", + size->pixelSize, + size->encodings[e].pitch, + xlfd_for_id( size->encodings[e].encoding ) ); + } + } + } + } + } + } +#endif // TQFONTDATABASE_DEBUG +} + +void TQFontDatabase::createDatabase() +{ + initializeDb(); +} + + +// -------------------------------------------------------------------------------------- +// font loader +// -------------------------------------------------------------------------------------- +#define MAXFONTSIZE_XFT 256 +#define MAXFONTSIZE_XLFD 128 +#ifndef QT_NO_XFTFREETYPE +static double addPatternProps(XftPattern *pattern, const TQtFontStyle::Key &key, bool fakeOblique, + bool smoothScalable, const TQFontPrivate *fp, const TQFontDef &request) +{ + int weight_value = XFT_WEIGHT_BLACK; + if ( key.weight == 0 ) + weight_value = XFT_WEIGHT_MEDIUM; + else if ( key.weight < (TQFont::Light + TQFont::Normal) / 2 ) + weight_value = XFT_WEIGHT_LIGHT; + else if ( key.weight < (TQFont::Normal + TQFont::DemiBold) / 2 ) + weight_value = XFT_WEIGHT_MEDIUM; + else if ( key.weight < (TQFont::DemiBold + TQFont::Bold) / 2 ) + weight_value = XFT_WEIGHT_DEMIBOLD; + else if ( key.weight < (TQFont::Bold + TQFont::Black) / 2 ) + weight_value = XFT_WEIGHT_BOLD; + XftPatternAddInteger( pattern, XFT_WEIGHT, weight_value ); + + int slant_value = XFT_SLANT_ROMAN; + if ( key.italic ) + slant_value = XFT_SLANT_ITALIC; + else if ( key.oblique && !fakeOblique ) + slant_value = XFT_SLANT_OBLIQUE; + XftPatternAddInteger( pattern, XFT_SLANT, slant_value ); + + /* + Xft1 doesn't obey user settings for turning off anti-aliasing using + the following: + + match any size > 6 size < 12 edit antialias = false; + + ... if we request pixel sizes. so, work around this limitiation and + convert the pixel size to a point size and request that. + */ + double size_value = request.pixelSize; + double scale = 1.; + if ( size_value > MAXFONTSIZE_XFT ) { + scale = (double)size_value/(double)MAXFONTSIZE_XFT; + size_value = MAXFONTSIZE_XFT; + } + + size_value = size_value*72./TQPaintDevice::x11AppDpiY(fp->screen); + XftPatternAddDouble( pattern, XFT_SIZE, size_value ); + +#ifdef XFT_MATRIX +# ifdef QT_XFT2 + if (!smoothScalable) { +# if FC_VERSION >= 20193 + int stretch = request.stretch; + if (!stretch) + stretch = 100; + XftPatternAddInteger(pattern, FC_WIDTH, stretch); +# endif + } else +# endif + if ( ( request.stretch > 0 && request.stretch != 100 ) || + ( key.oblique && fakeOblique ) ) { + XftMatrix matrix; + XftMatrixInit( &matrix ); + + if ( request.stretch > 0 && request.stretch != 100 ) + XftMatrixScale( &matrix, double( request.stretch ) / 100.0, 1.0 ); + if ( key.oblique && fakeOblique ) + XftMatrixShear( &matrix, 0.20, 0.0 ); + + XftPatternAddMatrix( pattern, XFT_MATRIX, &matrix ); + } +#endif // XFT_MATRIX + if (request.styleStrategy & (TQFont::PreferAntialias|TQFont::NoAntialias)) { + XftPatternDel(pattern, XFT_ANTIALIAS); + XftPatternAddBool(pattern, XFT_ANTIALIAS, + !(request.styleStrategy & TQFont::NoAntialias)); + } + + return scale; +} +#endif // QT_NO_XFTFREETYPE + +static +TQFontEngine *loadEngine( TQFont::Script script, + const TQFontPrivate *fp, const TQFontDef &request, + TQtFontFamily *family, TQtFontFoundry *foundry, + TQtFontStyle *style, TQtFontSize *size, + TQtFontEncoding *encoding, bool forced_encoding ) +{ + Q_UNUSED(script); + + if ( fp && fp->rawMode ) { + TQCString xlfd = request.family.latin1(); + FM_DEBUG( "Loading XLFD (rawmode) '%s'", xlfd.data() ); + + XFontStruct *xfs; + if (! (xfs = XLoadQueryFont(TQPaintDevice::x11AppDisplay(), xlfd.data() ) ) ) + return 0; + + TQFontEngine *fe = new TQFontEngineXLFD( xfs, xlfd.data(), 0 ); + if ( ! qt_fillFontDef( xfs, &fe->fontDef, TQPaintDevice::x11AppScreen() ) && + ! qt_fillFontDef( xlfd, &fe->fontDef, TQPaintDevice::x11AppScreen() ) ) + fe->fontDef = TQFontDef(); + + return fe; + } + +#ifndef QT_NO_XFTFREETYPE + if ( encoding->encoding == -1 ) { + + FM_DEBUG( " using Xft" ); + + XftPattern *pattern = XftPatternCreate(); + if ( !pattern ) return 0; + + bool symbol = (family->scripts[TQFont::UnknownScript] == TQtFontFamily::Supported); +# ifdef QT_XFT2 + if (!symbol && script != TQFont::Unicode) { + FcCharSet *cs = FcCharSetCreate(); + for ( int j=0; sample_chars[script][j]; j++ ) + FcCharSetAddChar(cs, sample_chars[script][j]); + if (script == TQFont::Latin) + // add Euro character + FcCharSetAddChar(cs, 0x20ac); + FcPatternAddCharSet(pattern, FC_CHARSET, cs); + FcCharSetDestroy(cs); + } +# else + XftPatternAddString( pattern, XFT_ENCODING, symbol ? "adobe-fontspecific" : "iso10646-1"); +# endif // QT_XFT2 + + if ( !foundry->name.isEmpty() ) + XftPatternAddString( pattern, XFT_FOUNDRY, + foundry->name.utf8().data() ); + + if ( !family->rawName.isEmpty() ) + XftPatternAddString( pattern, XFT_FAMILY, + family->rawName.utf8().data() ); + + + char pitch_value = ( encoding->pitch == 'c' ? XFT_CHARCELL : + ( encoding->pitch == 'm' ? XFT_MONO : XFT_PROPORTIONAL ) ); + XftPatternAddInteger( pattern, XFT_SPACING, pitch_value ); + + double scale = addPatternProps(pattern, style->key, style->fakeOblique, + style->smoothScalable, fp, request); + + XftResult res; + XftPattern *result = + XftFontMatch( TQPaintDevice::x11AppDisplay(), fp->screen, pattern, &res ); +#ifdef QT_XFT2 + if (result && script == TQFont::Latin) { + // since we added the Euro char on top, check we actually got the family + // we requested. If we didn't get it correctly, remove the Euro from the pattern + // and try again. + FcChar8 *f; + res = FcPatternGetString(result, FC_FAMILY, 0, &f); + if (res == FcResultMatch && TQString::fromUtf8((char *)f) != family->rawName) { + FcPatternDel(pattern, FC_CHARSET); + FcCharSet *cs = FcCharSetCreate(); + for ( int j=0; sample_chars[script][j]; j++ ) + FcCharSetAddChar(cs, sample_chars[script][j]); + FcPatternAddCharSet(pattern, FC_CHARSET, cs); + FcCharSetDestroy(cs); + result = XftFontMatch( TQPaintDevice::x11AppDisplay(), fp->screen, pattern, &res ); + } + } +#endif + XftPatternDestroy(pattern); + if (!result) + return 0; + + // somehow this gets lost in the XftMatch call, reset the anitaliasing property correctly. + if (request.styleStrategy & (TQFont::PreferAntialias|TQFont::NoAntialias)) { + XftPatternDel(result, XFT_ANTIALIAS); + XftPatternAddBool(result, XFT_ANTIALIAS, + !(request.styleStrategy & TQFont::NoAntialias)); + } + // We pass a duplicate to XftFontOpenPattern because either xft font + // will own the pattern after the call or the pattern will be + // destroyed. + XftPattern *dup = XftPatternDuplicate( result ); + XftFont *xftfs = XftFontOpenPattern( TQPaintDevice::x11AppDisplay(), dup ); + + if ( ! xftfs ) // Xft couldn't find a font? + return 0; + + TQFontEngine *fe = new TQFontEngineXft( xftfs, result, symbol ? 1 : 0 ); + if (fp->paintdevice + && TQPaintDeviceMetrics(fp->paintdevice).logicalDpiY() != TQPaintDevice::x11AppDpiY()) { + double px; + XftPatternGetDouble(result, XFT_PIXEL_SIZE, 0, &px); + scale = (double)request.pixelSize/px; + } + fe->setScale( scale ); + return fe; + } +#endif // QT_NO_XFTFREETYPE + + FM_DEBUG( " using XLFD" ); + + TQCString xlfd = "-"; + xlfd += foundry->name.isEmpty() ? "*" : foundry->name.latin1(); + xlfd += "-"; + xlfd += family->name.isEmpty() ? "*" : family->name.latin1(); + + xlfd += "-"; + xlfd += style->weightName ? style->weightName : "*"; + xlfd += "-"; + xlfd += ( style->key.italic ? "i" : ( style->key.oblique ? "o" : "r" ) ); + + xlfd += "-"; + xlfd += style->setwidthName ? style->setwidthName : "*"; + // ### handle add-style + xlfd += "-*-"; + + int px = size->pixelSize; + if ( style->smoothScalable && px == SMOOTH_SCALABLE ) + px = request.pixelSize; + else if ( style->bitmapScalable && px == 0 ) + px = request.pixelSize; + double scale = 1.; + if ( px > MAXFONTSIZE_XLFD ) { + scale = (double)px/(double)MAXFONTSIZE_XLFD; + px = MAXFONTSIZE_XLFD; + } + if (fp && fp->paintdevice + && TQPaintDeviceMetrics(fp->paintdevice).logicalDpiY() != TQPaintDevice::x11AppDpiY()) + scale = (double)request.pixelSize/(double)px; + + xlfd += TQString::number( px ).latin1(); + xlfd += "-"; + xlfd += TQString::number( encoding->xpoint ); + xlfd += "-"; + xlfd += TQString::number( encoding->xres ); + xlfd += "-"; + xlfd += TQString::number( encoding->yres ); + xlfd += "-"; + + // ### handle cell spaced fonts + xlfd += encoding->pitch; + xlfd += "-"; + xlfd += TQString::number( encoding->avgwidth ); + xlfd += "-"; + xlfd += xlfd_for_id( encoding->encoding ); + + FM_DEBUG( " xlfd: '%s'", xlfd.data() ); + + XFontStruct *xfs; + if (! (xfs = XLoadQueryFont(TQPaintDevice::x11AppDisplay(), xlfd.data() ) ) ) + return 0; + + TQFontEngine *fe = 0; + const int mib = xlfd_encoding[ encoding->encoding ].mib; + if (script == TQFont::Latin && encoding->encoding <= LAST_LATIN_ENCODING && !forced_encoding) { + fe = new TQFontEngineLatinXLFD( xfs, xlfd.data(), mib ); + } else { + fe = new TQFontEngineXLFD( xfs, xlfd.data(), mib ); + } + + fe->setScale( scale ); + + return fe; +} + + +#ifdef QT_XFT2 + +static void parseFontName(const TQString &name, TQString &foundry, TQString &family) +{ + if ( name.contains('[') && name.contains(']')) { + int i = name.find('['); + int li = name.findRev(']'); + + if (i < li) { + foundry = name.mid(i + 1, li - i - 1); + if (name[i - 1] == ' ') + i--; + family = name.left(i); + } + } else { + foundry = TQString::null; + family = name; + } +} + + +static TQFontEngine *loadFontConfigFont(const TQFontPrivate *fp, const TQFontDef &request, TQFont::Script script) +{ + if (!qt_has_xft) + return 0; + + TQStringList family_list; + if (request.family.isEmpty()) { + family_list = TQStringList::split(TQChar(','), fp->request.family); + + TQString stylehint; + switch ( request.styleHint ) { + case TQFont::SansSerif: + stylehint = "sans-serif"; + break; + case TQFont::Serif: + stylehint = "serif"; + break; + case TQFont::TypeWriter: + stylehint = "monospace"; + break; + default: + if (request.fixedPitch) + stylehint = "monospace"; + break; + } + if (!stylehint.isEmpty()) + family_list << stylehint; + } else { + family_list << request.family; + } + + FcPattern *pattern = FcPatternCreate(); + + { + TQString family, foundry; + for (TQStringList::ConstIterator it = family_list.begin(); it != family_list.end(); ++it) { + parseFontName(*it, foundry, family); + XftPatternAddString(pattern, XFT_FAMILY, family.utf8().data()); + } + } + + TQtFontStyle::Key key; + key.italic = request.italic; + key.weight = request.weight; + key.stretch = request.stretch; + + double scale = addPatternProps(pattern, key, FALSE, TRUE, fp, request); +#ifdef FONT_MATCH_DEBUG + qDebug("original pattern contains:"); + FcPatternPrint(pattern); +#endif + + // XftFontMatch calls the right ConfigSubstitute variants, but as we use + // FcFontMatch/Sort here we have to do it manually. + FcConfigSubstitute(0, pattern, FcMatchPattern); + XftDefaultSubstitute(TQPaintDevice::x11AppDisplay(), TQPaintDevice::x11AppScreen(), pattern); + +// qDebug("1: pattern contains:"); +// FcPatternPrint(pattern); + + { + FcValue value; + value.type = FcTypeString; + + // these should only get added to the pattern _after_ substitution + // append the default fallback font for the specified script + extern TQString qt_fallback_font_family( TQFont::Script ); + TQString fallback = qt_fallback_font_family( script ); + if ( ! fallback.isEmpty() && ! family_list.contains( fallback ) ) { + TQCString cs = fallback.utf8(); + value.u.s = (const FcChar8 *)cs.data(); + FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue); + } + + // add the default family + TQString defaultFamily = TQApplication::font().family(); + if ( ! family_list.contains( defaultFamily ) ) { + TQCString cs = defaultFamily.utf8(); + value.u.s = (const FcChar8 *)cs.data(); + FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue); + } + + // add TQFont::defaultFamily() to the list, for compatibility with + // previous versions + defaultFamily = TQApplication::font().defaultFamily(); + if ( ! family_list.contains( defaultFamily ) ) { + TQCString cs = defaultFamily.utf8(); + value.u.s = (const FcChar8 *)cs.data(); + FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue); + } + } + + if (script != TQFont::Unicode) { + FcCharSet *cs = FcCharSetCreate(); + for ( int j=0; sample_chars[script][j]; j++ ) + FcCharSetAddChar(cs, sample_chars[script][j]); + if (script == TQFont::Latin) + // add Euro character + FcCharSetAddChar(cs, 0x20ac); + FcPatternAddCharSet(pattern, FC_CHARSET, cs); + FcCharSetDestroy(cs); + } + +#ifdef FONT_MATCH_DEBUG + printf("final pattern contains:\n"); + FcPatternPrint(pattern); +#endif + + TQFontEngine *fe = 0; + + for( int jj = (FcGetVersion() >= 20392 ? 0 : 1); jj < 2; ++jj ) { + bool use_fontsort = ( jj == 1 ); + + FcResult result; + FcFontSet *fs = 0; + FcPattern *fsp = 0; + if( use_fontsort ) { + fs = FcFontSort(0, pattern, FcFalse, 0, &result); + if (!fs) + continue; + } else { + fsp = FcFontMatch(0, pattern, &result); + if (!fsp) + continue; + } + +#ifdef FONT_MATCH_DEBUG + if( use_fontsort ) { + printf("fontset contains:\n"); + for (int i = 0; i < fs->nfont; ++i) { + FcPattern *test = fs->fonts[i]; + FcChar8 *fam; + FcPatternGetString(test, FC_FAMILY, 0, &fam); + printf(" %s\n", fam); + } + } else { + printf("fontmatch:"); + FcChar8 *fam; + FcPatternGetString(fsp, FC_FAMILY, 0, &fam); + printf(" %s\n", fam); + } +#endif + + double size_value = request.pixelSize; + if ( size_value > MAXFONTSIZE_XFT ) + size_value = MAXFONTSIZE_XFT; + + int cnt = use_fontsort ? fs->nfont : 1; + + for (int i = 0; i < cnt; ++i) { + FcPattern *font = use_fontsort ? fs->fonts[i] : fsp; + FcCharSet *cs; + FcResult res = FcPatternGetCharSet(font, FC_CHARSET, 0, &cs); + if (res != FcResultMatch) + continue; + bool do_break=true; + for ( int j=0; sample_chars[script][j]; j++ ){ + do_break=false; + if (!FcCharSetHasChar(cs, sample_chars[script][j])) { + do_break=true; + break; + } + } + if ( do_break ) + continue; + FcBool scalable; + res = FcPatternGetBool(font, FC_SCALABLE, 0, &scalable); + if (res != FcResultMatch || !scalable) { + int pixelSize; + res = FcPatternGetInteger(font, FC_PIXEL_SIZE, 0, &pixelSize); + if (res != FcResultMatch || TQABS((size_value-pixelSize)/size_value) > 0.2) + continue; + } + + XftPattern *pattern = XftPatternDuplicate(font); + // add properties back in as the font selected from the list doesn't contain them. + addPatternProps(pattern, key, FALSE, TRUE, fp, request); + + XftPattern *result = + XftFontMatch( TQPaintDevice::x11AppDisplay(), fp->screen, pattern, &res ); + XftPatternDestroy(pattern); + + // We pass a duplicate to XftFontOpenPattern because either xft font + // will own the pattern after the call or the pattern will be + // destroyed. + XftPattern *dup = XftPatternDuplicate( result ); + XftFont *xftfs = XftFontOpenPattern( TQPaintDevice::x11AppDisplay(), dup ); + + if ( !xftfs ) { + // Xft couldn't find a font? + qDebug("couldn't open fontconfigs chosen font with Xft!!!"); + } else { + fe = new TQFontEngineXft( xftfs, result, 0 ); + if (fp->paintdevice + && TQPaintDeviceMetrics(fp->paintdevice).logicalDpiY() != TQPaintDevice::x11AppDpiY()) { + double px; + XftPatternGetDouble(result, XFT_PIXEL_SIZE, 0, &px); + scale = request.pixelSize/px; + } + fe->setScale( scale ); + fe->fontDef = request; + if ( script != TQFont::Unicode && !canRender(fe, script) ) { + FM_DEBUG( " WARN: font loaded cannot render samples" ); + delete fe; + fe = 0; + }else + FM_DEBUG( " USE: %s", fe->fontDef.family.latin1() ); + } + if (fe) { + TQFontEngineXft *xft = (TQFontEngineXft *)fe; + char *family; + if (XftPatternGetString(xft->pattern(), XFT_FAMILY, 0, &family) == XftResultMatch) + xft->fontDef.family = TQString::fromUtf8(family); + + double px; + if (XftPatternGetDouble(xft->pattern(), XFT_PIXEL_SIZE, 0, &px) == XftResultMatch) + xft->fontDef.pixelSize = qRound(px); + + int weight = XFT_WEIGHT_MEDIUM; + XftPatternGetInteger(xft->pattern(), XFT_WEIGHT, 0, &weight); + xft->fontDef.weight = getXftWeight(weight); + + int slant = XFT_SLANT_ROMAN; + XftPatternGetInteger(xft->pattern(), XFT_SLANT, 0, &slant); + xft->fontDef.italic = (slant != XFT_SLANT_ROMAN); + + int spacing = XFT_PROPORTIONAL; + XftPatternGetInteger(xft->pattern(), XFT_SPACING, 0, &spacing); + xft->fontDef.fixedPitch = spacing != XFT_PROPORTIONAL; + + xft->fontDef.ignorePitch = FALSE; + break; + } + } + + if( use_fontsort ) + FcFontSetDestroy(fs); + else + FcPatternDestroy(fsp); + + if( fe ) + break; + + } // for( jj ) + + FcPatternDestroy(pattern); + + return fe; +} + +#endif diff --git a/src/kernel/qfontengine_p.h b/src/kernel/qfontengine_p.h new file mode 100644 index 000000000..91abbb594 --- /dev/null +++ b/src/kernel/qfontengine_p.h @@ -0,0 +1,632 @@ +/**************************************************************************** +** +** ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** Licensees holding valid TQt Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQFONTENGINE_P_H +#define TQFONTENGINE_P_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + +#ifdef Q_WS_WIN +#include "qt_windows.h" +#include "qptrdict.h" +#endif + +#include "qtextengine_p.h" + +class TQPaintDevice; + +struct glyph_metrics_t; +class TQChar; +typedef unsigned short glyph_t; +struct qoffset_t; +typedef int advance_t; +class TQOpenType; +struct TransformedFont; + +#if defined( Q_WS_X11 ) || defined( Q_WS_WIN) || defined( Q_WS_MAC ) +class TQFontEngine : public TQShared +{ +public: + enum Error { + NoError, + OutOfMemory + }; + + enum Type { + // X11 types + Box, + XLFD, + LatinXLFD, + Xft, + + // MS Windows types + Win, + Uniscribe, + + // Apple MacOS types + Mac, + + // Trolltech TQWS types + TQWS + }; + + TQFontEngine() { + count = 0; cache_count = 0; +#ifdef Q_WS_X11 + transformed_fonts = 0; +#endif + } + virtual ~TQFontEngine(); + + /* returns 0 as glyph index for non existant glyphs */ + virtual Error stringToCMap( const TQChar *str, int len, glyph_t *glyphs, + advance_t *advances, int *nglyphs, bool mirrored ) const = 0; + +#ifdef Q_WS_X11 + virtual int cmap() const { return -1; } + virtual TQOpenType *openType() const { return 0; } +#endif + + virtual void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ) = 0; + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, + const qoffset_t *offsets, int numGlyphs ) = 0; + virtual glyph_metrics_t boundingBox( glyph_t glyph ) = 0; + + virtual int ascent() const = 0; + virtual int descent() const = 0; + virtual int leading() const = 0; + + virtual int lineThickness() const; + virtual int underlinePosition() const; + + virtual int maxCharWidth() const = 0; + virtual int minLeftBearing() const { return 0; } + virtual int minRightBearing() const { return 0; } + + virtual const char *name() const = 0; + + virtual bool canRender( const TQChar *string, int len ) = 0; + + virtual void setScale( double ) {} + virtual double scale() const { return 1.; } + + virtual Type type() const = 0; + + TQFontDef fontDef; + uint cache_cost; // amount of mem used in kb by the font + int cache_count; + +#ifdef Q_WS_WIN + HDC dc() const; + void getGlyphIndexes( const TQChar *ch, int numChars, glyph_t *glyphs, bool mirrored ) const; + void getCMap(); + + TQCString _name; + HDC hdc; + HFONT hfont; + LOGFONT logfont; + uint stockFont : 1; + uint paintDevice : 1; + uint useTextOutA : 1; + uint ttf : 1; + uint symbol : 1; + union { + TEXTMETRICW w; + TEXTMETRICA a; + } tm; + int lw; + unsigned char *cmap; + void *script_cache; + static TQPtrDict cacheDict; + short lbearing; + short rbearing; +#endif // Q_WS_WIN +#ifdef Q_WS_X11 + TransformedFont *transformed_fonts; +#endif +}; +#elif defined( Q_WS_QWS ) +class TQGfx; + +class TQFontEngine : public TQShared +{ +public: + TQFontEngine( const TQFontDef&, const TQPaintDevice * = 0 ); + ~TQFontEngine(); + /*TQMemoryManager::FontID*/ void *handle() const; + + enum Type { + // X11 types + Box, + XLFD, + Xft, + + // MS Windows types + Win, + Uniscribe, + + // Apple MacOS types + Mac, + + // Trolltech TQWS types + Qws + }; + + enum TextFlags { + Underline = 0x01, + Overline = 0x02, + StrikeOut = 0x04 + }; + + enum Error { + NoError, + OutOfMemory + }; + /* returns 0 as glyph index for non existant glyphs */ + Error stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ); + + glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + int underlinePosition() const; + int lineThickness() const; + + Type type() { return Qws; } + + bool canRender( const TQChar *string, int len ); + inline const char *name() const { return 0; } + TQFontDef fontDef; + /*TQMemoryManager::FontID*/ void *id; + int cache_cost; + int cache_count; + int scale; +}; +#endif // WIN || X11 || MAC + + + +enum IndicFeatures { + CcmpFeature, + InitFeature, + NuktaFeature, + AkhantFeature, + RephFeature, + BelowFormFeature, + HalfFormFeature, + PostFormFeature, + VattuFeature, + PreSubstFeature, + AboveSubstFeature, + BelowSubstFeature, + PostSubstFeature, + HalantFeature +}; + +#if defined(Q_WS_X11) || defined(Q_WS_WIN) +class TQFontEngineBox : public TQFontEngine +{ +public: + TQFontEngineBox( int size ); + ~TQFontEngineBox(); + + Error stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ); + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const { return 0; } + int minRightBearing() const { return 0; } + +#ifdef Q_WS_X11 + int cmap() const; +#endif + const char *name() const; + + bool canRender( const TQChar *string, int len ); + + Type type() const; + inline int size() const { return _size; } + +private: + friend class TQFontPrivate; + int _size; +}; +#endif + +#ifdef Q_WS_X11 +#include "qt_x11_p.h" + + +struct TransformedFont +{ + float xx; + float xy; + float yx; + float yy; + union { + Font xlfd_font; +#ifndef QT_NO_XFTFREETYPE + XftFont *xft_font; +#endif + }; + TransformedFont *next; +}; + +#ifndef QT_NO_XFTFREETYPE +#include +#include FT_FREETYPE_H +#include "ftxopen.h" + +class TQTextCodec; + +class TQFontEngineXft : public TQFontEngine +{ +public: + TQFontEngineXft( XftFont *font, XftPattern *pattern, int cmap ); + ~TQFontEngineXft(); + + TQOpenType *openType() const; + + Error stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ); + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int lineThickness() const; + int underlinePosition() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + + int cmap() const; + const char *name() const; + + void setScale( double scale ); + double scale() const { return _scale; } + + bool canRender( const TQChar *string, int len ); + + Type type() const; + XftPattern *pattern() const { return _pattern; } + FT_Face face() const { return _face; } + XftFont *font() const { return _font; } + + void recalcAdvances( int len, glyph_t *glyphs, advance_t *advances ); + +private: + friend class TQFontPrivate; + friend class TQOpenType; + XftFont *_font; + XftPattern *_pattern; + FT_Face _face; + TQOpenType *_openType; + int _cmap; + short lbearing; + short rbearing; + float _scale; + enum { widthCacheSize = 0x800, cmapCacheSize = 0x500 }; + unsigned char widthCache[widthCacheSize]; + glyph_t cmapCache[cmapCacheSize]; +}; +#endif + +class TQFontEngineLatinXLFD; + +class TQFontEngineXLFD : public TQFontEngine +{ +public: + TQFontEngineXLFD( XFontStruct *fs, const char *name, int cmap ); + ~TQFontEngineXLFD(); + + Error stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ); + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + + int cmap() const; + const char *name() const; + + bool canRender( const TQChar *string, int len ); + + void setScale( double scale ); + double scale() const { return _scale; } + Type type() const; + + TQt::HANDLE handle() const { return (TQt::HANDLE) _fs->fid; } + +private: + friend class TQFontPrivate; + XFontStruct *_fs; + TQCString _name; + TQTextCodec *_codec; + float _scale; // needed for printing, to correctly scale font metrics for bitmap fonts + int _cmap; + short lbearing; + short rbearing; + enum XlfdTransformations { + XlfdTrUnknown, + XlfdTrSupported, + XlfdTrUnsupported + }; + XlfdTransformations xlfd_transformations; + + friend class TQFontEngineLatinXLFD; +}; + +class TQFontEngineLatinXLFD : public TQFontEngine +{ +public: + TQFontEngineLatinXLFD( XFontStruct *xfs, const char *name, int cmap ); + ~TQFontEngineLatinXLFD(); + + Error stringToCMap( const TQChar *str, int len, glyph_t *glyphs, + advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, + const TQScriptItem *si, int textFlags ); + + virtual glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, + const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + + int cmap() const { return -1; } // ### + const char *name() const; + + bool canRender( const TQChar *string, int len ); + + void setScale( double scale ); + double scale() const { return _engines[0]->scale(); } + Type type() const { return LatinXLFD; } + + TQt::HANDLE handle() const { return ((TQFontEngineXLFD *) _engines[0])->handle(); } + +private: + void findEngine( const TQChar &ch ); + + TQFontEngine **_engines; + int _count; + + glyph_t glyphIndices [0x200]; + advance_t glyphAdvances[0x200]; + glyph_t euroIndex; + advance_t euroAdvance; +}; + +class TQScriptItem; +class TQTextEngine; + +#ifndef QT_NO_XFTFREETYPE + +#include "qscriptengine_p.h" +#include "qtextengine_p.h" +#include +#include FT_FREETYPE_H +#include "ftxopen.h" + +enum { PositioningProperties = 0x80000000 }; + +class TQOpenType +{ +public: + TQOpenType(TQFontEngineXft *fe); + ~TQOpenType(); + + struct Features { + uint tag; + uint property; + }; + + bool supportsScript(unsigned int script) { + Q_ASSERT(script < TQFont::NScripts); + return supported_scripts[script]; + } + void selectScript(unsigned int script, const Features *features = 0); + + bool shape(TQShaperItem *item, const unsigned int *properties = 0); + bool positionAndAdd(TQShaperItem *item, bool doLogClusters = TRUE); + + OTL_GlyphItem glyphs() const { return otl_buffer->in_string; } + int len() const { return otl_buffer->in_length; } + void setProperty(int index, uint property) { otl_buffer->in_string[index].properties = property; } + + +private: + bool checkScript(unsigned int script); + TQFontEngine *fontEngine; + FT_Face face; + TTO_GDEF gdef; + TTO_GSUB gsub; + TTO_GPOS gpos; + bool supported_scripts[TQFont::NScripts]; + FT_ULong current_script; + bool positioned : 1; + OTL_Buffer otl_buffer; + GlyphAttributes *tmpAttributes; + unsigned int *tmpLogClusters; + int length; + int orig_nglyphs; + int loadFlags; +}; + +#endif // QT_NO_XFTFREETYPE + +#elif defined( Q_WS_MAC ) +#include "qt_mac.h" +#include +#include + +class TQFontEngineMac : public TQFontEngine +{ +#if 0 + ATSFontMetrics *info; +#else + FontInfo *info; +#endif + int psize; + FMFontFamily fmfam; + TQMacFontInfo *internal_fi; + mutable ATSUTextLayout mTextLayout; + enum { widthCacheSize = 0x500 }; + mutable unsigned char widthCache[widthCacheSize]; + friend class TQFont; + friend class TQGLContext; + friend class TQFontPrivate; + friend class TQMacSetFontInfo; + +public: + TQFontEngineMac(); + ~TQFontEngineMac(); + + Error stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ); + + glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const { return (int)info->ascent; } + int descent() const { return (int)info->descent; } + int leading() const { return (int)info->leading; } +#if 0 + int maxCharWidth() const { return (int)info->maxAdvanceWidth; } +#else + int maxCharWidth() const { return info->widMax; } +#endif + + const char *name() const { return "ATSUI"; } + + bool canRender( const TQChar *string, int len ); + + Type type() const { return TQFontEngine::Mac; } + + void calculateCost(); + + enum { WIDTH=0x01, DRAW=0x02, EXISTS=0x04 }; + int doTextTask(const TQChar *s, int pos, int use_len, int len, uchar task, int =-1, int y=-1, + TQPaintDevice *dev=NULL, const TQRegion *rgn=NULL) const; +}; + +#elif defined( Q_WS_WIN ) + +class TQFontEngineWin : public TQFontEngine +{ +public: + TQFontEngineWin( const char *name, HDC, HFONT, bool, LOGFONT ); + + Error stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const; + + void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ); + + glyph_metrics_t boundingBox( const glyph_t *glyphs, + const advance_t *advances, const qoffset_t *offsets, int numGlyphs ); + glyph_metrics_t boundingBox( glyph_t glyph ); + + int ascent() const; + int descent() const; + int leading() const; + int maxCharWidth() const; + int minLeftBearing() const; + int minRightBearing() const; + + const char *name() const; + + bool canRender( const TQChar *string, int len ); + + Type type() const; + + enum { widthCacheSize = 0x800, cmapCacheSize = 0x500 }; + unsigned char widthCache[widthCacheSize]; +}; + +#if 0 +class TQFontEngineUniscribe : public TQFontEngineWin +{ +public: + void draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ); + bool canRender( const TQChar *string, int len ); + + Type type() const; +}; +#endif + +#endif // Q_WS_WIN + +#endif diff --git a/src/kernel/qfontengine_x11.cpp b/src/kernel/qfontengine_x11.cpp new file mode 100644 index 000000000..54b29f026 --- /dev/null +++ b/src/kernel/qfontengine_x11.cpp @@ -0,0 +1,2724 @@ +/**************************************************************************** +** +** ??? +** +** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qfontengine_p.h" + +// #define FONTENGINE_DEBUG + +#include +#include +#include + +#include "qbitmap.h" +#include "qfontdatabase.h" +#include "qpaintdevice.h" +#include "qpaintdevicemetrics.h" +#include "qpainter.h" +#include "qimage.h" + +#include "qt_x11_p.h" + +#include "qfont.h" +#include "qtextengine_p.h" + +#include + +#include + +// defined in qfontdatbase_x11.cpp +extern int qt_mib_for_xlfd_encoding( const char *encoding ); +extern int qt_xlfd_encoding_id( const char *encoding ); + +extern void qt_draw_transformed_rect( TQPainter *p, int x, int y, int w, int h, bool fill ); + +static void drawLines( TQPainter *p, TQFontEngine *fe, int baseline, int x1, int w, int textFlags ) +{ + int lw = fe->lineThickness(); + if ( textFlags & TQt::Underline ) { + int pos = fe->underlinePosition(); + qt_draw_transformed_rect( p, x1, baseline+pos, w, lw, TRUE ); + } + if ( textFlags & TQt::Overline ) { + int pos = fe->ascent()+1; + if ( !pos ) pos = 1; + qt_draw_transformed_rect( p, x1, baseline-pos, w, lw, TRUE ); + } + if ( textFlags & TQt::StrikeOut ) { + int pos = fe->ascent()/3; + if ( !pos ) pos = 1; + qt_draw_transformed_rect( p, x1, baseline-pos, w, lw, TRUE ); + } +} + + +inline static void qSafeXDestroyImage( XImage *x ) +{ + if ( x->data ) { + free( x->data ); + x->data = 0; + } + XDestroyImage( x ); +} + +extern bool qt_xForm_helper( const TQWMatrix &trueMat, int xoffset, + int type, int depth, + uchar *dptr, int dbpl, int p_inc, int dHeight, + uchar *sptr, int sbpl, int sWidth, int sHeight + ); + +static TQBitmap transform(Display *dpy, const TQBitmap &source, int xoff, int yoff, int w, int h, const TQWMatrix &matrix) +{ + int ws = source.width(); + int hs = source.height(); + + bool invertible; + TQWMatrix mat = matrix.invert( &invertible ); // invert matrix + + if (!invertible ) + return TQBitmap(); + mat.translate(xoff, yoff); + + XImage *xi = XGetImage(dpy, source.handle(), 0, 0, ws, hs, AllPlanes, XYPixmap); + + if ( !xi ) + return TQBitmap(); + + int sbpl = xi->bytes_per_line; + uchar *sptr = (uchar *)xi->data; + + int dbpl = (w+7)/8; + int dbytes = dbpl*h; + + uchar *dptr = (uchar *)malloc( dbytes ); // create buffer for bits + memset( dptr, 0, dbytes ); + + int type = xi->bitmap_bit_order == MSBFirst ? QT_XFORM_TYPE_MSBFIRST : QT_XFORM_TYPE_LSBFIRST; + int xbpl, p_inc; + xbpl = (w+7)/8; + p_inc = dbpl - xbpl; + + bool ok = qt_xForm_helper( mat, xi->xoffset, type, 1, dptr, xbpl, p_inc, h, sptr, sbpl, ws, hs ); + qSafeXDestroyImage(xi); + TQBitmap bm; + if (ok) { + bm = TQBitmap( w, h, dptr, TQImage::systemBitOrder() != TQImage::BigEndian ); + } else { +#if defined(QT_CHECK_RANGE) + qWarning( "TQFontEngineXft::tranform: xform failed"); +#endif + } + + free( dptr ); + return bm; +} + + +static void drawScaled(int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags, + Display *dpy, GC gc, TQPaintDevice *pdev, TQFontEngine *fe, + const TQWMatrix &xmat, float scale) +{ + // font doesn't support transformations, need to do it by hand + int w = qRound(si->width/scale); + int h = qRound((si->ascent + si->descent + 1)/scale); + if (w == 0 || h == 0) + return; + TQWMatrix mat1 = xmat; + mat1.scale(scale, scale); + + w += h; // add some pixels to width because of italic correction + TQBitmap bm( w, h, TRUE ); // create bitmap + TQPainter paint; + paint.begin( &bm ); // draw text in bitmap + fe->draw( &paint, 0, si->ascent/scale, engine, si, textFlags ); + paint.end(); + + TQRect pdevRect; + if (pdev->devType() == TQInternal::Widget) + pdevRect = ((TQWidget *)pdev)->rect(); + else if (pdev->devType() == TQInternal::Pixmap) + pdevRect = ((TQPixmap *)pdev)->rect(); + else + return; + + + TQRect br = mat1.mapRect(TQRect(x, y - si->ascent, w, h)); + TQRect br2 = br & pdevRect; + if (br2.width() <= 0 || br2.height() <= 0 + || br2.width() >= 32768 || br2.height() >= 32768) + return; + TQWMatrix mat = TQPixmap::trueMatrix( mat1, w, h ); + TQBitmap wx_bm = ::transform(dpy, bm, br2.x() - br.x(), br2.y() - br.y(), br2.width(), br2.height(), mat); + if ( wx_bm.isNull() ) + return; + + x = br2.x(); + y = br2.y(); + + TQt::HANDLE hd = pdev->handle(); + XSetFillStyle( dpy, gc, FillStippled ); + XSetStipple( dpy, gc, wx_bm.handle() ); + XSetTSOrigin( dpy, gc, x, y ); + XFillRectangle( dpy, hd, gc, x, y, wx_bm.width(), wx_bm.height() ); + XSetTSOrigin( dpy, gc, 0, 0 ); + XSetFillStyle( dpy, gc, FillSolid ); +} + + +TQFontEngine::~TQFontEngine() +{ +} + +int TQFontEngine::lineThickness() const +{ + // ad hoc algorithm + int score = fontDef.weight * fontDef.pixelSize; + int lw = score / 700; + + // looks better with thicker line for small pointsizes + if ( lw < 2 && score >= 1050 ) lw = 2; + if ( lw == 0 ) lw = 1; + + return lw; +} + +int TQFontEngine::underlinePosition() const +{ + int pos = ( ( lineThickness() * 2 ) + 3 ) / 6; + return pos ? pos : 1; +} + +// ------------------------------------------------------------------ +// The box font engine +// ------------------------------------------------------------------ + + +TQFontEngineBox::TQFontEngineBox( int size ) + : _size( size ) +{ + cache_cost = sizeof( TQFontEngineBox ); +} + +TQFontEngineBox::~TQFontEngineBox() +{ +} + +TQFontEngine::Error TQFontEngineBox::stringToCMap( const TQChar *, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool ) const +{ + if ( *nglyphs < len ) { + *nglyphs = len; + return OutOfMemory; + } + + memset( glyphs, 0, len * sizeof( glyph_t ) ); + *nglyphs = len; + + if ( advances ) { + for ( int i = 0; i < len; i++ ) + *(advances++) = _size; + } + return NoError; +} + +void TQFontEngineBox::draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ) +{ + Display *dpy = TQPaintDevice::x11AppDisplay(); + TQt::HANDLE hd = p->device()->handle(); + GC gc = p->gc; + +#ifdef FONTENGINE_DEBUG + p->save(); + p->setBrush( TQt::white ); + glyph_metrics_t ci = boundingBox( glyphs, offsets, numGlyphs ); + p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height ); + p->drawRect( x + ci.x, y + 50 + ci.y, ci.width, ci.height ); + qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height ); + p->restore(); + int xp = x; + int yp = y; +#endif + + GlyphAttributes *glyphAttributes = engine->glyphAttributes( si ); + + if ( p->txop > TQPainter::TxTranslate ) { + int xp = x; + int yp = _size + 2; + int s = _size - 3; + for (int k = 0; k < si->num_glyphs; k++) { + if (!glyphAttributes[k].zeroWidth) + qt_draw_transformed_rect( p, xp, yp, s, s, FALSE ); + xp += _size; + } + } else { + if ( p->txop == TQPainter::TxTranslate ) + p->map( x, y, &x, &y ); + + XRectangle rects[64]; + + int gl = 0; + while (gl < si->num_glyphs) { + int toDraw = TQMIN(64, si->num_glyphs-gl); + int adv = toDraw*_size; + if (x + adv < SHRT_MAX && x > SHRT_MIN) { + int ng = 0; + for (int k = 0; k < toDraw; k++) { + if (!glyphAttributes[gl + k].zeroWidth) { + rects[ng].x = x + (k * _size); + rects[ng].y = y - _size + 2; + rects[ng].width = rects[k].height = _size - 3; + ++ng; + } + } + XDrawRectangles(dpy, hd, gc, rects, ng); + } + gl += toDraw; + x += adv; + } + } + + if ( textFlags != 0 ) + drawLines( p, this, y, x, si->num_glyphs*_size, textFlags ); + +#ifdef FONTENGINE_DEBUG + x = xp; + y = yp; + p->save(); + p->setPen( TQt::red ); + for ( int i = 0; i < numGlyphs; i++ ) { + glyph_metrics_t ci = boundingBox( glyphs[i] ); + x += offsets[i].x; + y += offsets[i].y; + p->drawRect( x + ci.x, y + 50 + ci.y, ci.width, ci.height ); + qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offset=(%d/%d)", i, ci.x, ci.y, ci.width, ci.height, + ci.xoff, ci.yoff, offsets[i].x, offsets[i].y ); + x += ci.xoff; + y += ci.yoff; + } + p->restore(); +#endif +} + +glyph_metrics_t TQFontEngineBox::boundingBox( const glyph_t *, const advance_t *, const qoffset_t *, int numGlyphs ) +{ + glyph_metrics_t overall; + overall.x = overall.y = 0; + overall.width = _size*numGlyphs; + overall.height = _size; + overall.xoff = overall.width; + overall.yoff = 0; + return overall; +} + +glyph_metrics_t TQFontEngineBox::boundingBox( glyph_t ) +{ + return glyph_metrics_t( 0, _size, _size, _size, _size, 0 ); +} + + + +int TQFontEngineBox::ascent() const +{ + return _size; +} + +int TQFontEngineBox::descent() const +{ + return 0; +} + +int TQFontEngineBox::leading() const +{ + int l = qRound( _size * 0.15 ); + return (l > 0) ? l : 1; +} + +int TQFontEngineBox::maxCharWidth() const +{ + return _size; +} + +int TQFontEngineBox::cmap() const +{ + return -1; +} + +const char *TQFontEngineBox::name() const +{ + return "null"; +} + +bool TQFontEngineBox::canRender( const TQChar *, int ) +{ + return TRUE; +} + +TQFontEngine::Type TQFontEngineBox::type() const +{ + return Box; +} + + + + +// ------------------------------------------------------------------ +// Xlfd cont engine +// ------------------------------------------------------------------ + +static inline XCharStruct *charStruct( XFontStruct *xfs, uint ch ) +{ + XCharStruct *xcs = 0; + unsigned char r = ch>>8; + unsigned char c = ch&0xff; + if ( r >= xfs->min_byte1 && + r <= xfs->max_byte1 && + c >= xfs->min_char_or_byte2 && + c <= xfs->max_char_or_byte2) { + if ( !xfs->per_char ) + xcs = &(xfs->min_bounds); + else { + xcs = xfs->per_char + ((r - xfs->min_byte1) * + (xfs->max_char_or_byte2 - + xfs->min_char_or_byte2 + 1)) + + (c - xfs->min_char_or_byte2); + if (xcs->width == 0 && xcs->ascent == 0 && xcs->descent == 0) + xcs = 0; + } + } + return xcs; +} + +TQFontEngineXLFD::TQFontEngineXLFD( XFontStruct *fs, const char *name, int mib ) + : _fs( fs ), _name( name ), _codec( 0 ), _scale( 1. ), _cmap( mib ) +{ + if ( _cmap ) _codec = TQTextCodec::codecForMib( _cmap ); + + cache_cost = (((fs->max_byte1 - fs->min_byte1) * + (fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1)) + + fs->max_char_or_byte2 - fs->min_char_or_byte2); + cache_cost = ((fs->max_bounds.ascent + fs->max_bounds.descent) * + (fs->max_bounds.width * cache_cost / 8)); + lbearing = SHRT_MIN; + rbearing = SHRT_MIN; + +#if 1 + // Server side transformations do not seem to work correctly for + // all types of fonts (for example, it works for bdf/pcf fonts, + // but not for ttf). It also seems to be extermely server + // dependent. The best thing is to just disable server side + // transformations until either server support matures or we + // figure out a better way to do it. + xlfd_transformations = XlfdTrUnsupported; +#else + xlfd_transformations = XlfdTrUnknown; + + // Hummingbird's Exceed X server will substitute 'fixed' for any + // known fonts, and it doesn't seem to support transformations, so + // we should never try to use xlfd transformations with it + if (strstr(ServerVendor(TQPaintDevice::x11AppDisplay()), "Hummingbird")) + xlfd_transformations = XlfdTrUnsupported; +#endif +} + +TQFontEngineXLFD::~TQFontEngineXLFD() +{ + XFreeFont( TQPaintDevice::x11AppDisplay(), _fs ); + _fs = 0; + TransformedFont *trf = transformed_fonts; + while ( trf ) { + XUnloadFont( TQPaintDevice::x11AppDisplay(), trf->xlfd_font ); + TransformedFont *tmp = trf; + trf = trf->next; + delete tmp; + } +} + +TQFontEngine::Error TQFontEngineXLFD::stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const +{ + if ( *nglyphs < len ) { + *nglyphs = len; + return OutOfMemory; + } + + if ( _codec ) { + bool haveNbsp = FALSE; + for ( int i = 0; i < len; i++ ) + if ( str[i].unicode() == 0xa0 ) { + haveNbsp = TRUE; + break; + } + + TQChar *chars = (TQChar *)str; + if ( haveNbsp || mirrored ) { + chars = (TQChar *)malloc( len*sizeof(TQChar) ); + for ( int i = 0; i < len; i++ ) + chars[i] = (str[i].unicode() == 0xa0 ? 0x20 : + (mirrored ? ::mirroredChar(str[i]).unicode() : str[i].unicode())); + } + _codec->fromUnicodeInternal( chars, glyphs, len ); + if (chars != str) + free( chars ); + } else { + glyph_t *g = glyphs + len; + const TQChar *c = str + len; + if ( mirrored ) { + while ( c != str ) + *(--g) = (--c)->unicode() == 0xa0 ? 0x20 : ::mirroredChar(*c).unicode(); + } else { + while ( c != str ) + *(--g) = (--c)->unicode() == 0xa0 ? 0x20 : c->unicode(); + } + } + *nglyphs = len; + + if ( advances ) { + glyph_t *g = glyphs + len; + advance_t *a = advances + len; + XCharStruct *xcs; + // inlined for better perfomance + if ( !_fs->per_char ) { + xcs = &_fs->min_bounds; + while ( a != advances ) + *(--a) = xcs->width; + } + else if ( !_fs->max_byte1 ) { + XCharStruct *base = _fs->per_char - _fs->min_char_or_byte2; + while ( g-- != glyphs ) { + unsigned int gl = *g; + xcs = (gl >= _fs->min_char_or_byte2 && gl <= _fs->max_char_or_byte2) ? + base + gl : 0; + *(--a) = (!xcs || (!xcs->width && !xcs->ascent && !xcs->descent)) ? _fs->ascent : xcs->width; + } + } + else { + while ( g != glyphs ) { + xcs = charStruct( _fs, *(--g) ); + *(--a) = (xcs ? xcs->width : _fs->ascent); + } + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } + } + return NoError; +} + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static bool x_font_load_error = FALSE; +static int x_font_errorhandler(Display *, XErrorEvent *) +{ + x_font_load_error = TRUE; + return 0; +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +void TQFontEngineXLFD::draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ) +{ + if ( !si->num_glyphs ) + return; + +// qDebug("TQFontEngineXLFD::draw( %d, %d, numglyphs=%d", x, y, si->num_glyphs ); + + Display *dpy = TQPaintDevice::x11AppDisplay(); + TQt::HANDLE hd = p->device()->handle(); + GC gc = p->gc; + + bool transform = FALSE; + int xorig = x; + int yorig = y; + + TQt::HANDLE font_id = _fs->fid; + if ( p->txop > TQPainter::TxTranslate || _scale < 0.9999 || _scale > 1.0001 ) { + bool degenerate = TQABS( p->m11()*p->m22() - p->m12()*p->m21() ) < 0.01; + if ( !degenerate && xlfd_transformations != XlfdTrUnsupported ) { + // need a transformed font from the server + TQCString xlfd_transformed = _name; + int field = 0; + char *data = xlfd_transformed.data(); + int pos = 0; + while ( field < 7 ) { + if ( data[pos] == '-' ) + field++; + pos++; + } + int endPos = pos; + while ( data[endPos] != '-' ) + endPos++; + float size = xlfd_transformed.mid( pos, endPos-pos ).toInt(); + float mat[4]; + mat[0] = p->m11()*size*_scale; + mat[1] = -p->m12()*size*_scale; + mat[2] = -p->m21()*size*_scale; + mat[3] = p->m22()*size*_scale; + + // check if we have it cached + TransformedFont *trf = transformed_fonts; + TransformedFont *prev = 0; + int i = 0; + while ( trf ) { + if ( trf->xx == mat[0] && + trf->xy == mat[1] && + trf->yx == mat[2] && + trf->yy == mat[3] ) + break; + TransformedFont *tmp = trf; + trf = trf->next; + if (i > 10) { + XUnloadFont( TQPaintDevice::x11AppDisplay(), tmp->xlfd_font ); + delete tmp; + prev->next = trf; + } else { + prev = tmp; + } + ++i; + } + if ( trf ) { + if ( prev ) { + // move to beginning of list + prev->next = trf->next; + trf->next = transformed_fonts; + transformed_fonts = trf; + } + font_id = trf->xlfd_font; + } else { + TQCString matrix="["; + for ( int i = 0; i < 4; i++ ) { + float f = mat[i]; + if ( f < 0 ) { + matrix += '~'; + f = -f; + } + matrix += TQString::number( f, 'f', 5 ).latin1(); + matrix += ' '; + } + matrix += ']'; + //qDebug("m: %2.2f %2.2f %2.2f %2.2f, matrix=%s", p->m11(), p->m12(), p->m21(), p->m22(), matrix.data()); + xlfd_transformed.replace( pos, endPos-pos, matrix ); + + x_font_load_error = FALSE; + XErrorHandler old_handler = XSetErrorHandler( x_font_errorhandler ); + font_id = XLoadFont( dpy, xlfd_transformed.data() ); + XSync( dpy, FALSE ); + XSetErrorHandler( old_handler ); + if ( x_font_load_error ) { + //qDebug( "couldn't load transformed font" ); + font_id = _fs->fid; + xlfd_transformations = XlfdTrUnsupported; + } else { + TransformedFont *trf = new TransformedFont; + trf->xx = mat[0]; + trf->xy = mat[1]; + trf->yx = mat[2]; + trf->yy = mat[3]; + trf->xlfd_font = font_id; + trf->next = transformed_fonts; + transformed_fonts = trf; + } + } + } + if ( degenerate || xlfd_transformations == XlfdTrUnsupported ) { + // XServer or font don't support server side transformations, need to do it by hand + float tmp = _scale; + _scale = 1.; + drawScaled(x, y, engine, si, textFlags, dpy, p->gc, p->device(), this, p->xmat, tmp); + _scale = tmp; + return; + } + transform = TRUE; + } else if ( p->txop == TQPainter::TxTranslate ) { + p->map( x, y, &x, &y ); + } + + XSetFont(dpy, gc, font_id); + +#ifdef FONTENGINE_DEBUG + p->save(); + p->setBrush( TQt::white ); + glyph_metrics_t ci = boundingBox( glyphs, advances, offsets, si->num_glyphs ); + p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height ); + p->drawRect( x + ci.x, y + 100 + ci.y, ci.width, ci.height ); + qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height ); + p->restore(); + int xp = x; + int yp = y; +#endif + + glyph_t *glyphs = engine->glyphs( si ); + advance_t *advances = engine->advances( si ); + qoffset_t *offsets = engine->offsets( si ); + + XChar2b ch[256]; + XChar2b *chars = ch; + if ( si->num_glyphs > 255 ) + chars = (XChar2b *)malloc( si->num_glyphs*sizeof(XChar2b) ); + + for (int i = 0; i < si->num_glyphs; i++) { + chars[i].byte1 = glyphs[i] >> 8; + chars[i].byte2 = glyphs[i] & 0xff; + } + + int xpos = x; + GlyphAttributes *glyphAttributes = engine->glyphAttributes( si ); + + if ( si->analysis.bidiLevel % 2 ) { + int i = si->num_glyphs; + while( i-- ) { + advance_t adv = advances[i]; + // qDebug("advance = %d/%d", adv.x, adv.y ); + x += adv; + glyph_metrics_t gi = boundingBox( glyphs[i] ); + int xp = x-offsets[i].x-gi.xoff; + int yp = y+offsets[i].y-gi.yoff; + if ( transform ) + p->map( xp, yp, &xp, &yp ); + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XDrawString16(dpy, hd, gc, xp, yp, chars+i, 1 ); + } + } else { + if ( transform || si->hasPositioning ) { + int i = 0; + while( i < si->num_glyphs ) { + int xp = x+offsets[i].x; + int yp = y+offsets[i].y; + if ( transform ) + p->map( xp, yp, &xp, &yp ); + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XDrawString16(dpy, hd, gc, xp, yp, chars+i, 1 ); + advance_t adv = advances[i]; + // qDebug("advance = %d/%d", adv.x, adv.y ); + x += adv; + i++; + } + } else { + // we can take a shortcut + int gl = 0; + while (gl < si->num_glyphs) { + int toDraw = TQMIN(64, si->num_glyphs-gl); + int adv = 0; + for (int i = gl; i < gl+toDraw; ++i) + adv += advances[i]; + if (x + adv < SHRT_MAX && x > SHRT_MIN) + XDrawString16(dpy, hd, gc, x, y, chars+gl, toDraw); + gl += toDraw; + x += adv; + } + } + } + + if ( chars != ch ) + free( chars ); + + if ( textFlags != 0 ) + drawLines( p, this, yorig, xorig, x-xpos, textFlags ); + +#ifdef FONTENGINE_DEBUG + x = xp; + y = yp; + p->save(); + p->setPen( TQt::red ); + for ( int i = 0; i < si->num_glyphs; i++ ) { + glyph_metrics_t ci = boundingBox( glyphs[i] ); + p->drawRect( x + ci.x + offsets[i].x, y + 100 + ci.y + offsets[i].y, ci.width, ci.height ); + qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=(%d/%d)", i, ci.x, ci.y, ci.width, ci.height, + ci.xoff, ci.yoff, offsets[i].x, offsets[i].y, + advances[i].x, advances[i].y); + x += advances[i].x; + y += advances[i].y; + } + p->restore(); +#endif +} + +glyph_metrics_t TQFontEngineXLFD::boundingBox( const glyph_t *glyphs, const advance_t *advances, const qoffset_t *offsets, int numGlyphs ) +{ + int i; + + glyph_metrics_t overall; + int ymax = 0; + int xmax = 0; + for (i = 0; i < numGlyphs; i++) { + XCharStruct *xcs = charStruct( _fs, glyphs[i] ); + if (xcs) { + int x = overall.xoff + offsets[i].x - xcs->lbearing; + int y = overall.yoff + offsets[i].y - xcs->ascent; + overall.x = TQMIN( overall.x, x ); + overall.y = TQMIN( overall.y, y ); + xmax = TQMAX( xmax, overall.xoff + offsets[i].x + xcs->rbearing ); + ymax = TQMAX( ymax, y + xcs->ascent + xcs->descent ); + overall.xoff += qRound(advances[i]/_scale); + } else { + int size = _fs->ascent; + overall.x = TQMIN(overall.x, overall.xoff ); + overall.y = TQMIN(overall.y, overall.yoff - size ); + ymax = TQMAX( ymax, overall.yoff ); + overall.xoff += size; + xmax = TQMAX( xmax, overall.xoff ); + } + } + overall.height = ymax - overall.y; + overall.width = xmax - overall.x; + + if ( _scale != 1. ) { + overall.x = qRound(overall.x * _scale); + overall.y = qRound(overall.y * _scale); + overall.height = qRound(overall.height * _scale); + overall.width = qRound(overall.width * _scale); + overall.xoff = qRound(overall.xoff * _scale); + overall.yoff = qRound(overall.yoff * _scale); + } + return overall; +} + +glyph_metrics_t TQFontEngineXLFD::boundingBox( glyph_t glyph ) +{ + glyph_metrics_t gm; + // ### scale missing! + XCharStruct *xcs = charStruct( _fs, glyph ); + if (xcs) { + gm = glyph_metrics_t( xcs->lbearing, -xcs->ascent, xcs->rbearing- xcs->lbearing, xcs->ascent + xcs->descent, xcs->width, 0 ); + } else { + int size = _fs->ascent; + gm = glyph_metrics_t( 0, size, size, size, size, 0 ); + } + if ( _scale != 1. ) { + gm.x = qRound(gm.x * _scale); + gm.y = qRound(gm.y * _scale); + gm.height = qRound(gm.height * _scale); + gm.width = qRound(gm.width * _scale); + gm.xoff = qRound(gm.xoff * _scale); + gm.yoff = qRound(gm.yoff * _scale); + } + return gm; +} + + +int TQFontEngineXLFD::ascent() const +{ + return qRound(_fs->ascent*_scale); +} + +int TQFontEngineXLFD::descent() const +{ + return qRound((_fs->descent-1)*_scale); +} + +int TQFontEngineXLFD::leading() const +{ + int l = qRound((TQMIN(_fs->ascent, _fs->max_bounds.ascent) + + TQMIN(_fs->descent, _fs->max_bounds.descent)) * _scale * 0.15 ); + return (l > 0) ? l : 1; +} + +int TQFontEngineXLFD::maxCharWidth() const +{ + return qRound(_fs->max_bounds.width*_scale); +} + + +// Loads the font for the specified script +static inline int maxIndex(XFontStruct *f) { + return (((f->max_byte1 - f->min_byte1) * + (f->max_char_or_byte2 - f->min_char_or_byte2 + 1)) + + f->max_char_or_byte2 - f->min_char_or_byte2); +} + +int TQFontEngineXLFD::minLeftBearing() const +{ + if ( lbearing == SHRT_MIN ) { + if ( _fs->per_char ) { + XCharStruct *cs = _fs->per_char; + int nc = maxIndex(_fs) + 1; + int mx = cs->lbearing; + + for (int c = 1; c < nc; c++) { + // ignore the bearings for characters whose ink is + // completely outside the normal bounding box + if ((cs[c].lbearing <= 0 && cs[c].rbearing <= 0) || + (cs[c].lbearing >= cs[c].width && cs[c].rbearing >= cs[c].width)) + continue; + + int nmx = cs[c].lbearing; + + if (nmx < mx) + mx = nmx; + } + + ((TQFontEngineXLFD *)this)->lbearing = mx; + } else + ((TQFontEngineXLFD *)this)->lbearing = _fs->min_bounds.lbearing; + } + return qRound (lbearing*_scale); +} + +int TQFontEngineXLFD::minRightBearing() const +{ + if ( rbearing == SHRT_MIN ) { + if ( _fs->per_char ) { + XCharStruct *cs = _fs->per_char; + int nc = maxIndex(_fs) + 1; + int mx = cs->rbearing; + + for (int c = 1; c < nc; c++) { + // ignore the bearings for characters whose ink is + // completely outside the normal bounding box + if ((cs[c].lbearing <= 0 && cs[c].rbearing <= 0) || + (cs[c].lbearing >= cs[c].width && cs[c].rbearing >= cs[c].width)) + continue; + + int nmx = cs[c].rbearing; + + if (nmx < mx) + mx = nmx; + } + + ((TQFontEngineXLFD *)this)->rbearing = mx; + } else + ((TQFontEngineXLFD *)this)->rbearing = _fs->min_bounds.rbearing; + } + return qRound (rbearing*_scale); +} + +int TQFontEngineXLFD::cmap() const +{ + return _cmap; +} + +const char *TQFontEngineXLFD::name() const +{ + return _name; +} + +bool TQFontEngineXLFD::canRender( const TQChar *string, int len ) +{ + glyph_t glyphs[256]; + int nglyphs = 255; + glyph_t *g = glyphs; + if ( stringToCMap( string, len, g, 0, &nglyphs, FALSE ) == OutOfMemory ) { + g = (glyph_t *)malloc( nglyphs*sizeof(glyph_t) ); + stringToCMap( string, len, g, 0, &nglyphs, FALSE ); + } + + bool allExist = TRUE; + for ( int i = 0; i < nglyphs; i++ ) { + if ( !g[i] || !charStruct( _fs, g[i] ) ) { + allExist = FALSE; + break; + } + } + + if ( g != glyphs ) + free( g ); + + return allExist; +} + + +void TQFontEngineXLFD::setScale( double scale ) +{ + _scale = scale; +} + + +TQFontEngine::Type TQFontEngineXLFD::type() const +{ + return XLFD; +} + + +// ------------------------------------------------------------------ +// LatinXLFD engine +// ------------------------------------------------------------------ + +static const int engine_array_inc = 4; + +TQFontEngineLatinXLFD::TQFontEngineLatinXLFD( XFontStruct *xfs, const char *name, + int mib ) +{ + _engines = new TQFontEngine*[ engine_array_inc ]; + _engines[0] = new TQFontEngineXLFD( xfs, name, mib ); + _count = 1; + + cache_cost = _engines[0]->cache_cost; + + memset( glyphIndices, 0, sizeof( glyphIndices ) ); + memset( glyphAdvances, 0, sizeof( glyphAdvances ) ); + euroIndex = 0; + euroAdvance = 0; +} + +TQFontEngineLatinXLFD::~TQFontEngineLatinXLFD() +{ + for ( int i = 0; i < _count; ++i ) { + delete _engines[i]; + _engines[i] = 0; + } + delete [] _engines; + _engines = 0; +} + +void TQFontEngineLatinXLFD::findEngine( const TQChar &ch ) +{ + if ( ch.unicode() == 0 ) return; + + static const char *alternate_encodings[] = { + "iso8859-1", + "iso8859-2", + "iso8859-3", + "iso8859-4", + "iso8859-9", + "iso8859-10", + "iso8859-13", + "iso8859-14", + "iso8859-15", + "hp-roman8" + }; + static const int mib_count = sizeof( alternate_encodings ) / sizeof( const char * ); + + // see if one of the above mibs can map the char we want + TQTextCodec *codec = 0; + int which = -1; + int i; + for ( i = 0; i < mib_count; ++i ) { + const int mib = qt_mib_for_xlfd_encoding( alternate_encodings[i] ); + bool skip = FALSE; + for ( int e = 0; e < _count; ++e ) { + if ( _engines[e]->cmap() == mib ) { + skip = TRUE; + break; + } + } + if ( skip ) continue; + + codec = TQTextCodec::codecForMib( mib ); + if ( codec && codec->canEncode( ch ) ) { + which = i; + break; + } + } + + if ( ! codec || which == -1 ) + return; + + const int enc_id = qt_xlfd_encoding_id( alternate_encodings[which] ); + TQFontDef req = fontDef; + TQFontEngine *engine = TQFontDatabase::findFont( TQFont::Latin, 0, req, enc_id ); + if ( ! engine ) { + req.family = TQString::null; + engine = TQFontDatabase::findFont( TQFont::Latin, 0, req, enc_id ); + if ( ! engine ) return; + } + engine->setScale( scale() ); + + if ( ! ( _count % engine_array_inc ) ) { + // grow the engines array + TQFontEngine **old = _engines; + int new_size = + ( ( ( _count+engine_array_inc ) / engine_array_inc ) * engine_array_inc ); + _engines = new TQFontEngine*[new_size]; + for ( i = 0; i < _count; ++i ) + _engines[i] = old[i]; + delete [] old; + } + + _engines[_count] = engine; + const int hi = _count << 8; + ++_count; + + unsigned short chars[0x201]; + glyph_t glyphs[0x201]; + advance_t advances[0x201]; + for ( i = 0; i < 0x200; ++i ) + chars[i] = i; + chars[0x200] = 0x20ac; + int glyphCount = 0x201; + engine->stringToCMap( (const TQChar *) chars, 0x201, glyphs, advances, &glyphCount, FALSE ); + + // merge member data with the above + for ( i = 0; i < 0x200; ++i ) { + if ( glyphIndices[i] != 0 || glyphs[i] == 0 ) continue; + glyphIndices[i] = glyphs[i] >= 0x2100 ? glyphs[i] : hi | glyphs[i]; + glyphAdvances[i] = advances[i]; + } + if (!euroIndex && glyphs[0x200]) { + euroIndex = hi | glyphs[0x200]; + euroAdvance = advances[0x200]; + } +} + +TQFontEngine::Error +TQFontEngineLatinXLFD::stringToCMap( const TQChar *str, int len, glyph_t *glyphs, + advance_t *advances, int *nglyphs, bool mirrored ) const +{ + if ( *nglyphs < len ) { + *nglyphs = len; + return OutOfMemory; + } + + int i; + bool missing = FALSE; + const TQChar *c = str+len; + glyph_t *g = glyphs+len; + if ( advances ) { + int asc = ascent(); + advance_t *a = advances+len; + if ( mirrored ) { + while ( c != str ) { + --c; + --g; + --a; + if ( c->unicode() < 0x200 ) { + unsigned short ch = ::mirroredChar(*c).unicode(); + *g = glyphIndices[ch]; + *a = glyphAdvances[ch]; + } else { + if ( c->unicode() == 0x20ac ) { + *g = euroIndex; + *a = euroAdvance; + } else { + *g = 0; + *a = asc; + } + } + missing = ( missing || ( *g == 0 ) ); + } + } else { + while ( c != str ) { + --c; + --g; + --a; + if ( c->unicode() < 0x200 ) { + *g = glyphIndices[c->unicode()]; + *a = glyphAdvances[c->unicode()]; + } else { + if ( c->unicode() == 0x20ac ) { + *g = euroIndex; + *a = euroAdvance; + } else { + *g = 0; + *a = asc; + } + } + missing = ( missing || ( *g == 0 ) ); + } + } + } else { + if ( mirrored ) { + while ( c != str ) { + --c; + --g; + *g = ( ( c->unicode() < 0x200 ) ? glyphIndices[::mirroredChar(*c).unicode()] + : (c->unicode() == 0x20ac) ? euroIndex : 0 ); + missing = ( missing || ( *g == 0 ) ); + } + } else { + while ( c != str ) { + --c; + --g; + *g = ( ( c->unicode() < 0x200 ) ? glyphIndices[c->unicode()] + : (c->unicode() == 0x20ac) ? euroIndex : 0 ); + missing = ( missing || ( *g == 0 ) ); + } + } + } + + if ( missing ) { + for ( i = 0; i < len; ++i ) { + unsigned short uc = str[i].unicode(); + if ( glyphs[i] != 0 || (uc >= 0x200 && uc != 0x20ac) ) + continue; + + TQFontEngineLatinXLFD *that = (TQFontEngineLatinXLFD *) this; + that->findEngine( str[i] ); + glyphs[i] = (uc == 0x20ac ? euroIndex : that->glyphIndices[uc]); + if ( advances ) + advances[i] = (uc == 0x20ac ? euroAdvance : glyphAdvances[uc]); + } + } + + *nglyphs = len; + return NoError; +} + +void TQFontEngineLatinXLFD::draw( TQPainter *p, int x, int y, const TQTextEngine *engine, + const TQScriptItem *si, int textFlags ) +{ + if ( !si->num_glyphs ) return; + + glyph_t *glyphs = engine->glyphs( si ); + advance_t *advances = engine->advances( si ); + int which = glyphs[0] >> 8; + if (which > 0x20) + which = 0; + + int start = 0; + int end, i; + for ( end = 0; end < si->num_glyphs; ++end ) { + int e = glyphs[end] >> 8; + if (e > 0x20) + e = 0; + if ( e == which ) continue; + + // set the high byte to zero + if (which != 0) { + for ( i = start; i < end; ++i ) + glyphs[i] = glyphs[i] & 0xff; + } + + // draw the text + TQScriptItem si2 = *si; + si2.glyph_data_offset = si->glyph_data_offset + start; + si2.num_glyphs = end - start; + _engines[which]->draw( p, x, y, engine, &si2, textFlags ); + + // reset the high byte for all glyphs and advance to the next sub-string + const int hi = which << 8; + for ( i = start; i < end; ++i ) { + glyphs[i] = hi | glyphs[i]; + x += advances[i]; + } + + // change engine + start = end; + which = e; + } + + // set the high byte to zero + if (which != 0) { + for ( i = start; i < end; ++i ) + glyphs[i] = glyphs[i] & 0xff; + } + // draw the text + TQScriptItem si2 = *si; + si2.glyph_data_offset = si->glyph_data_offset + start; + si2.num_glyphs = end - start; + _engines[which]->draw( p, x, y, engine, &si2, textFlags ); + + // reset the high byte for all glyphs + if (which != 0) { + const int hi = which << 8; + for ( i = start; i < end; ++i ) + glyphs[i] = hi | glyphs[i]; + } +} + +glyph_metrics_t TQFontEngineLatinXLFD::boundingBox( const glyph_t *glyphs_const, + const advance_t *advances, + const qoffset_t *offsets, + int numGlyphs ) +{ + if ( numGlyphs <= 0 ) return glyph_metrics_t(); + + glyph_metrics_t overall; + + glyph_t *glyphs = (glyph_t *) glyphs_const; + int which = glyphs[0] >> 8; + if (which > 0x20) + which = 0; + + int start = 0; + int end, i; + for ( end = 0; end < numGlyphs; ++end ) { + int e = glyphs[end] >> 8; + if (e > 0x20) + e = 0; + if ( e == which ) continue; + + // set the high byte to zero + if (which != 0) { + for ( i = start; i < end; ++i ) + glyphs[i] = glyphs[i] & 0xff; + } + + // merge the bounding box for this run + const glyph_metrics_t gm = + _engines[which]->boundingBox( glyphs + start, + advances + start, + offsets + start, + end - start ); + + overall.x = TQMIN( overall.x, gm.x ); + overall.y = TQMIN( overall.y, gm.y ); + overall.width = overall.xoff + gm.width; + overall.height = TQMAX( overall.height + overall.y, gm.height + gm.y ) - + TQMIN( overall.y, gm.y ); + overall.xoff += gm.xoff; + overall.yoff += gm.yoff; + + // reset the high byte for all glyphs + if (which != 0) { + const int hi = which << 8; + for ( i = start; i < end; ++i ) + glyphs[i] = hi | glyphs[i]; + } + + // change engine + start = end; + which = e; + } + + // set the high byte to zero + if (which != 0) { + for ( i = start; i < end; ++i ) + glyphs[i] = glyphs[i] & 0xff; + } + + // merge the bounding box for this run + const glyph_metrics_t gm = + _engines[which]->boundingBox( glyphs + start, + advances + start, + offsets + start, + end - start ); + + overall.x = TQMIN( overall.x, gm.x ); + overall.y = TQMIN( overall.y, gm.y ); + overall.width = overall.xoff + gm.width; + overall.height = TQMAX( overall.height + overall.y, gm.height + gm.y ) - + TQMIN( overall.y, gm.y ); + overall.xoff += gm.xoff; + overall.yoff += gm.yoff; + + // reset the high byte for all glyphs + if (which != 0) { + const int hi = which << 8; + for ( i = start; i < end; ++i ) + glyphs[i] = hi | glyphs[i]; + } + + return overall; +} + +glyph_metrics_t TQFontEngineLatinXLFD::boundingBox( glyph_t glyph ) +{ + int engine = glyph >> 8; + if (engine > 0x20) + engine = 0; + Q_ASSERT( engine < _count ); + return _engines[engine]->boundingBox( engine > 0 ? glyph & 0xff : glyph ); +} + +int TQFontEngineLatinXLFD::ascent() const +{ + return _engines[0]->ascent(); +} + +int TQFontEngineLatinXLFD::descent() const +{ + return _engines[0]->descent(); +} + +int TQFontEngineLatinXLFD::leading() const +{ + return _engines[0]->leading(); +} + +int TQFontEngineLatinXLFD::maxCharWidth() const +{ + return _engines[0]->maxCharWidth(); +} + +int TQFontEngineLatinXLFD::minLeftBearing() const +{ + return _engines[0]->minLeftBearing(); +} + +int TQFontEngineLatinXLFD::minRightBearing() const +{ + return _engines[0]->minRightBearing(); +} + +const char *TQFontEngineLatinXLFD::name() const +{ + return _engines[0]->name(); +} + +bool TQFontEngineLatinXLFD::canRender( const TQChar *string, int len ) +{ + bool all = TRUE; + int i; + for ( i = 0; i < len; ++i ) { + if ( string[i].unicode() >= 0x200 || + glyphIndices[string[i].unicode()] == 0 ) { + if (string[i].unicode() != 0x20ac || euroIndex == 0) + all = FALSE; + break; + } + } + + if ( all ) + return TRUE; + + all = TRUE; + for ( i = 0; i < len; ++i ) { + if ( string[i].unicode() >= 0x200 ) { + if (string[i].unicode() == 0x20ac) { + if (euroIndex) + continue; + + findEngine(string[i]); + if (euroIndex) + continue; + } + all = FALSE; + break; + } + if ( glyphIndices[string[i].unicode()] != 0 ) continue; + + findEngine( string[i] ); + if ( glyphIndices[string[i].unicode()] == 0 ) { + all = FALSE; + break; + } + } + + return all; +} + +void TQFontEngineLatinXLFD::setScale( double scale ) +{ + int i; + for ( i = 0; i < _count; ++i ) + _engines[i]->setScale( scale ); + unsigned short chars[0x200]; + for ( i = 0; i < 0x200; ++i ) + chars[i] = i; + int glyphCount = 0x200; + _engines[0]->stringToCMap( (const TQChar *)chars, 0x200, + glyphIndices, glyphAdvances, &glyphCount, FALSE ); +} + + +// ------------------------------------------------------------------ +// Xft cont engine +// ------------------------------------------------------------------ +// #define FONTENGINE_DEBUG + +#ifndef QT_NO_XFTFREETYPE +class Q_HackPaintDevice : public TQPaintDevice +{ +public: + inline Q_HackPaintDevice() : TQPaintDevice( 0 ) {} + inline XftDraw *xftDrawHandle() const { + return (XftDraw *)rendhd; + } + +}; + +#ifdef QT_XFT2 +static inline void getGlyphInfo( XGlyphInfo *xgi, XftFont *font, int glyph ) +{ + FT_UInt x = glyph; + XftGlyphExtents( TQPaintDevice::x11AppDisplay(), font, &x, 1, xgi ); +} +#else +static inline XftFontStruct *getFontStruct( XftFont *font ) +{ + if (font->core) + return 0; + return font->u.ft.font; +} + +static inline void getGlyphInfo(XGlyphInfo *xgi, XftFont *font, int glyph) +{ + + XftTextExtents32(TQPaintDevice::x11AppDisplay(), font, (XftChar32 *) &glyph, 1, xgi); +} +#endif // QT_XFT2 + +static inline FT_Face lockFTFace( XftFont *font ) +{ +#ifdef QT_XFT2 + return XftLockFace( font ); +#else + if (font->core) return 0; + return font->u.ft.font->face; +#endif // QT_XFT2 +} + +static inline void unlockFTFace( XftFont *font ) +{ +#ifdef QT_XFT2 + XftUnlockFace( font ); +#else + Q_UNUSED( font ); +#endif // QT_XFT2 +} + + + +TQFontEngineXft::TQFontEngineXft( XftFont *font, XftPattern *pattern, int cmap ) + : _font( font ), _pattern( pattern ), _openType( 0 ), _cmap( cmap ) +{ + _face = lockFTFace( _font ); + +#ifndef QT_XFT2 + XftFontStruct *xftfs = getFontStruct( _font ); + if ( xftfs ) { + // dirty hack: we set the charmap in the Xftfreetype to -1, so + // XftFreetype assumes no encoding and really draws glyph + // indices. The FT_Face still has the Unicode encoding to we + // can convert from Unicode to glyph index + xftfs->charmap = -1; + } +#else + _cmap = -1; + // Xft maps Unicode and adobe roman for us. + for (int i = 0; i < _face->num_charmaps; ++i) { + FT_CharMap cm = _face->charmaps[i]; +// qDebug("font has charmap %x", cm->encoding); + if (cm->encoding == ft_encoding_adobe_custom + || cm->encoding == ft_encoding_symbol) { +// qDebug("font has adobe custom or ms symbol charmap"); + _cmap = i; + break; + } + } +#endif // QT_XFT2 + + + cache_cost = _font->height * _font->max_advance_width * + ( _face ? _face->num_glyphs : 1024 ); + + // if the Xft font is not antialiased, it uses bitmaps instead of + // 8-bit alpha maps... adjust the cache_cost to reflect this + Bool antialiased = TRUE; + if ( XftPatternGetBool( pattern, XFT_ANTIALIAS, + 0, &antialiased ) == XftResultMatch && + ! antialiased ) { + cache_cost /= 8; + } + lbearing = SHRT_MIN; + rbearing = SHRT_MIN; + + memset( widthCache, 0, sizeof(widthCache) ); + memset( cmapCache, 0, sizeof(cmapCache) ); +} + +TQFontEngineXft::~TQFontEngineXft() +{ + delete _openType; + unlockFTFace( _font ); + + XftFontClose( TQPaintDevice::x11AppDisplay(),_font ); + XftPatternDestroy( _pattern ); + _font = 0; + _pattern = 0; + TransformedFont *trf = transformed_fonts; + while ( trf ) { + XftFontClose( TQPaintDevice::x11AppDisplay(), trf->xft_font ); + TransformedFont *tmp = trf; + trf = trf->next; + delete tmp; + } +} + +#ifdef QT_XFT2 +static glyph_t getAdobeCharIndex(XftFont *font, int cmap, uint ucs4) +{ + FT_Face _face = XftLockFace( font ); + FT_Set_Charmap(_face, _face->charmaps[cmap]); + glyph_t g = FT_Get_Char_Index(_face, ucs4); + XftUnlockFace(font); + return g; +} +#endif + +TQFontEngine::Error TQFontEngineXft::stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const +{ + if ( *nglyphs < len ) { + *nglyphs = len; + return OutOfMemory; + } + +#ifdef QT_XFT2 + if (_cmap != -1) { + for ( int i = 0; i < len; ++i ) { + unsigned short uc = str[i].unicode(); + if (mirrored) + uc = ::mirroredChar(str[i]).unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + glyph_t glyph = XftCharIndex(0, _font, uc); + if (!glyph) + glyph = getAdobeCharIndex(_font, _cmap, uc); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((TQFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } else if ( mirrored ) { + for ( int i = 0; i < len; ++i ) { + unsigned short uc = ::mirroredChar(str[i]).unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + if (uc == 0xa0) + uc = 0x20; + glyph_t glyph = XftCharIndex(0, _font, uc); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((TQFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } else { + for ( int i = 0; i < len; ++i ) { + unsigned short uc = str[i].unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + if (uc == 0xa0) + uc = 0x20; + glyph_t glyph = XftCharIndex(0, _font, uc); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((TQFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } + + if ( advances ) { + for ( int i = 0; i < len; i++ ) { + FT_UInt glyph = *(glyphs + i); + advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0; + if ( !advances[i] ) { + XGlyphInfo gi; + XftGlyphExtents( TQPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi ); + advances[i] = gi.xOff; + if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 ) + ((TQFontEngineXft *)this)->widthCache[glyph] = gi.xOff; + } + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } + } +#else + if ( !_face ) { + if ( mirrored ) { + for ( int i = 0; i < len; i++ ) + glyphs[i] = ::mirroredChar(str[i]).unicode(); + } else { + for ( int i = 0; i < len; i++ ) + glyphs[i] = str[i].unicode(); + } + } else { + if ( _cmap == 1 ) { + // symbol font + for ( int i = 0; i < len; i++ ) { + unsigned short uc = str[i].unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + glyph_t glyph = FT_Get_Char_Index( _face, uc ); + if(!glyph && uc < 0x100) + glyph = FT_Get_Char_Index( _face, uc+0xf000 ); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((TQFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } else if ( mirrored ) { + for ( int i = 0; i < len; i++ ) { + unsigned short uc = ::mirroredChar(str[i]).unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + glyph_t glyph = FT_Get_Char_Index( _face, uc ); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((TQFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } else { + for ( int i = 0; i < len; i++ ) { + unsigned short uc = str[i].unicode(); + glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0; + if ( !glyphs[i] ) { + glyph_t glyph = FT_Get_Char_Index( _face, uc ); + glyphs[i] = glyph; + if ( uc < cmapCacheSize ) + ((TQFontEngineXft *)this)->cmapCache[uc] = glyph; + } + } + } + } + + if ( advances ) { + for ( int i = 0; i < len; i++ ) { + XftChar16 glyph = *(glyphs + i); + advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0; + if ( !advances[i] ) { + XGlyphInfo gi; + XftTextExtents16(TQPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi); + advances[i] = gi.xOff; + if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 ) + ((TQFontEngineXft *)this)->widthCache[glyph] = gi.xOff; + } + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } + } +#endif // QT_XFT2 + + *nglyphs = len; + return NoError; +} + + +void TQFontEngineXft::recalcAdvances( int len, glyph_t *glyphs, advance_t *advances ) +{ + +#ifdef QT_XFT2 + for ( int i = 0; i < len; i++ ) { + FT_UInt glyph = *(glyphs + i); + advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0; + if ( !advances[i] ) { + XGlyphInfo gi; + XftGlyphExtents( TQPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi ); + advances[i] = gi.xOff; + if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 ) + ((TQFontEngineXft *)this)->widthCache[glyph] = gi.xOff; + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } + } +#else + for ( int i = 0; i < len; i++ ) { + XftChar16 glyph = *(glyphs + i); + advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0; + if ( !advances[i] ) { + XGlyphInfo gi; + XftTextExtents16(TQPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi); + advances[i] = gi.xOff; + if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 ) + ((TQFontEngineXft *)this)->widthCache[glyph] = gi.xOff; + } + } + if ( _scale != 1. ) { + for ( int i = 0; i < len; i++ ) + advances[i] = qRound(advances[i]*_scale); + } +#endif // QT_XFT2 +} + +//#define FONTENGINE_DEBUG +void TQFontEngineXft::draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags ) +{ + if ( !si->num_glyphs ) + return; + + Display *dpy = TQPaintDevice::x11AppDisplay(); + + int xorig = x; + int yorig = y; + + GlyphAttributes *glyphAttributes = engine->glyphAttributes( si ); + + XftFont *fnt = _font; + bool transform = FALSE; + if ( p->txop >= TQPainter::TxScale || p->rop != TQt::CopyROP || _scale < 0.9999 || _scale > 1.001) { + bool can_scale = (_face->face_flags & FT_FACE_FLAG_SCALABLE) && p->rop == TQt::CopyROP; + double size = (p->m11()*p->m22() - p->m12()*p->m21())*_scale*_scale*fontDef.pixelSize*fontDef.pixelSize; + if (size > 256*256 || _scale < .9999 || _scale > 1.001) + can_scale = FALSE; + if (!can_scale) { + // font doesn't support transformations, need to do it by hand + float tmp = _scale; + _scale = 1.; + drawScaled(x, y, engine, si, textFlags, dpy, p->gc, p->device(), this, p->xmat, tmp); + _scale = tmp; + return; + } + + XftMatrix *mat = 0; + XftPatternGetMatrix( _pattern, XFT_MATRIX, 0, &mat ); + XftMatrix m2; + m2.xx = p->m11()*_scale; + m2.xy = -p->m21()*_scale; + m2.yx = -p->m12()*_scale; + m2.yy = p->m22()*_scale; + + // check if we have it cached + TransformedFont *trf = transformed_fonts; + TransformedFont *prev = 0; + int i = 0; + while ( trf ) { + if ( trf->xx == (float)m2.xx && + trf->xy == (float)m2.xy && + trf->yx == (float)m2.yx && + trf->yy == (float)m2.yy ) + break; + TransformedFont *tmp = trf; + trf = trf->next; + if (i > 10) { + XftFontClose( TQPaintDevice::x11AppDisplay(), tmp->xft_font ); + delete tmp; + prev->next = trf; + } else { + prev = tmp; + } + ++i; + } + if ( trf ) { + if ( prev ) { + // move to beginning of list + prev->next = trf->next; + trf->next = transformed_fonts; + transformed_fonts = trf; + } + fnt = trf->xft_font; + } else { + if ( mat ) + XftMatrixMultiply( &m2, &m2, mat ); + + XftPattern *pattern = XftPatternDuplicate( _pattern ); + XftPatternDel( pattern, XFT_MATRIX ); + XftPatternAddMatrix( pattern, XFT_MATRIX, &m2 ); + + fnt = XftFontOpenPattern( dpy, pattern ); +#ifndef QT_XFT2 + XftFontStruct *xftfs = getFontStruct( fnt ); + if ( xftfs ) { + // dirty hack: we set the charmap in the Xftfreetype to -1, so + // XftFreetype assumes no encoding and really draws glyph + // indices. The FT_Face still has the Unicode encoding to we + // can convert from Unicode to glyph index + xftfs->charmap = -1; + } +#endif // QT_XFT2 + TransformedFont *trf = new TransformedFont; + trf->xx = (float)m2.xx; + trf->xy = (float)m2.xy; + trf->yx = (float)m2.yx; + trf->yy = (float)m2.yy; + trf->xft_font = fnt; + trf->next = transformed_fonts; + transformed_fonts = trf; + } + transform = TRUE; + } else if ( p->txop == TQPainter::TxTranslate ) { + p->map( x, y, &x, &y ); + } + + glyph_t *glyphs = engine->glyphs( si ); + advance_t *advances = engine->advances( si ); + qoffset_t *offsets = engine->offsets( si ); + + const TQColor &pen = p->cpen.color(); + XftDraw *draw = ((Q_HackPaintDevice *)p->pdev)->xftDrawHandle(); + + XftColor col; + col.color.red = pen.red () | pen.red() << 8; + col.color.green = pen.green () | pen.green() << 8; + col.color.blue = pen.blue () | pen.blue() << 8; + col.color.alpha = 0xffff; + col.pixel = pen.pixel(); +#ifdef FONTENGINE_DEBUG + qDebug("===== drawing %d glyphs reverse=%s ======", si->num_glyphs, si->analysis.bidiLevel % 2?"TRUE":"FALSE" ); + p->save(); + p->setBrush( TQt::white ); + glyph_metrics_t ci = boundingBox( glyphs, advances, offsets, si->num_glyphs ); + p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height ); + p->drawRect( x + ci.x, y + 100 + ci.y, ci.width, ci.height ); + qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height ); + p->restore(); + int yp = y; + int xp = x; +#endif + + if ( textFlags != 0 ) + drawLines( p, this, yorig, xorig, si->width, textFlags ); + + + if ( si->isSpace ) + return; + + if ( transform || si->hasPositioning ) { + if ( si->analysis.bidiLevel % 2 ) { + int i = si->num_glyphs; + while( i-- ) { + int xp = x + offsets[i].x; + int yp = y + offsets[i].y; + if ( transform ) + p->map( xp, yp, &xp, &yp ); +#ifdef QT_XFT2 + FT_UInt glyph = *(glyphs + i); + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XftDrawGlyphs( draw, &col, fnt, xp, yp, &glyph, 1 ); +#else + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XftDrawString16( draw, &col, fnt, xp, yp, (XftChar16 *) (glyphs+i), 1); +#endif // QT_XFT2 +#ifdef FONTENGINE_DEBUG + glyph_metrics_t gi = boundingBox( glyphs[i] ); + p->drawRect( x+offsets[i].x+gi.x, y+offsets[i].y+100+gi.y, gi.width, gi.height ); + p->drawLine( x+offsets[i].x, y + 150 + 5*i , x+offsets[i].x+advances[i], y + 150 + 5*i ); + p->drawLine( x+offsets[i].x, y + 152 + 5*i , x+offsets[i].x+gi.xoff, y + 152 + 5*i ); + qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=%d", i, gi.x, gi.y, gi.width, gi.height, + gi.xoff, gi.yoff, offsets[i].x, offsets[i].y, advances[i]); +#endif + x += advances[i]; + } + } else { + int i = 0; + while ( i < si->num_glyphs ) { + int xp = x + offsets[i].x; + int yp = y + offsets[i].y; + if ( transform ) + p->map( xp, yp, &xp, &yp ); +#ifdef QT_XFT2 + FT_UInt glyph = *(glyphs + i); + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XftDrawGlyphs( draw, &col, fnt, xp, yp, &glyph, 1 ); +#else + if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN) + XftDrawString16( draw, &col, fnt, xp, yp, (XftChar16 *) (glyphs+i), 1 ); +#endif // QT_XFT2 + // qDebug("advance = %d/%d", adv.x, adv.y ); + x += advances[i]; + i++; + } + } + } else { + // Xft has real trouble drawing the glyphs on their own. + // Drawing them as one string increases performance significantly. +#ifdef QT_XFT2 + // #### we should use a different method anyways on Xft2 + FT_UInt g[64]; + int gl = 0; + while (gl < si->num_glyphs) { + int toDraw = TQMIN(64, si->num_glyphs-gl); + int adv = 0; + if ( si->analysis.bidiLevel % 2 ) { + for ( int i = 0; i < toDraw; i++ ) { + g[i] = glyphs[si->num_glyphs-1-(gl+i)]; + adv += advances[si->num_glyphs-1-(gl+i)]; + } + } else { + for ( int i = 0; i < toDraw; i++ ) { + g[i] = glyphs[gl+i]; + adv += advances[gl+i]; + } + } + if (x + adv < SHRT_MAX && x > SHRT_MIN) + XftDrawGlyphs( draw, &col, fnt, x, y, g, toDraw ); + gl += toDraw; + x += adv; + } +#else + XftChar16 g[64]; + int gl = 0; + while (gl < si->num_glyphs) { + int toDraw = TQMIN(64, si->num_glyphs-gl); + int adv = 0; + if ( si->analysis.bidiLevel % 2 ) { + for ( int i = 0; i < toDraw; i++ ) { + g[i] = glyphs[si->num_glyphs-1-(gl+i)]; + adv += advances[si->num_glyphs-1-(gl+i)]; + } + } else { + for ( int i = 0; i < toDraw; i++ ) { + g[i] = glyphs[gl+i]; + adv += advances[gl+i]; + } + } + if (x + adv < SHRT_MAX && x > SHRT_MIN) + XftDrawString16( draw, &col, fnt, x, y, g, toDraw ); + gl += toDraw; + x += adv; + } +#endif // QT_XFT2 + } + +#ifdef FONTENGINE_DEBUG + if ( !si->analysis.bidiLevel % 2 ) { + x = xp; + y = yp; + p->save(); + p->setPen( TQt::red ); + for ( int i = 0; i < si->num_glyphs; i++ ) { + glyph_metrics_t ci = boundingBox( glyphs[i] ); + p->drawRect( x + ci.x + offsets[i].x, y + 100 + ci.y + offsets[i].y, ci.width, ci.height ); + qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=%d", i, ci.x, ci.y, ci.width, ci.height, + ci.xoff, ci.yoff, offsets[i].x, offsets[i].y, advances[i]); + x += advances[i]; + } + p->restore(); + } +#endif +} + +glyph_metrics_t TQFontEngineXft::boundingBox( const glyph_t *glyphs, const advance_t *advances, const qoffset_t *offsets, int numGlyphs ) +{ + XGlyphInfo xgi; + + glyph_metrics_t overall; + int ymax = 0; + int xmax = 0; + if (_scale != 1) { + for (int i = 0; i < numGlyphs; i++) { + getGlyphInfo( &xgi, _font, glyphs[i] ); + int x = overall.xoff + offsets[i].x - xgi.x; + int y = overall.yoff + offsets[i].y - xgi.y; + overall.x = TQMIN( overall.x, x ); + overall.y = TQMIN( overall.y, y ); + xmax = TQMAX( xmax, x + xgi.width ); + ymax = TQMAX( ymax, y + xgi.height ); + overall.xoff += qRound(advances[i]/_scale); + } + overall.x = qRound(overall.x * _scale); + overall.y = qRound(overall.y * _scale); + overall.xoff = qRound(overall.xoff * _scale); + overall.yoff = qRound(overall.yoff * _scale); + xmax = qRound(xmax * _scale); + ymax = qRound(ymax * _scale); + } else { + for (int i = 0; i < numGlyphs; i++) { + getGlyphInfo( &xgi, _font, glyphs[i] ); + int x = overall.xoff + offsets[i].x - xgi.x; + int y = overall.yoff + offsets[i].y - xgi.y; + overall.x = TQMIN( overall.x, x ); + overall.y = TQMIN( overall.y, y ); + xmax = TQMAX( xmax, x + xgi.width ); + ymax = TQMAX( ymax, y + xgi.height ); + overall.xoff += advances[i]; + } + } + overall.height = ymax - overall.y; + overall.width = xmax - overall.x; + return overall; +} + +glyph_metrics_t TQFontEngineXft::boundingBox( glyph_t glyph ) +{ + XGlyphInfo xgi; + getGlyphInfo( &xgi, _font, glyph ); + glyph_metrics_t gm = glyph_metrics_t( -xgi.x, -xgi.y, xgi.width, xgi.height, xgi.xOff, -xgi.yOff ); + if ( _scale != 1. ) { + gm.x = qRound(gm.x * _scale); + gm.y = qRound(gm.y * _scale); + gm.height = qRound(gm.height * _scale); + gm.width = qRound(gm.width * _scale); + gm.xoff = qRound(gm.xoff * _scale); + gm.yoff = qRound(gm.yoff * _scale); + } + return gm; +} + + + +int TQFontEngineXft::ascent() const +{ + return qRound(_font->ascent*_scale); +} + +int TQFontEngineXft::descent() const +{ + return qRound((_font->descent-1)*_scale); +} + +// #### use Freetype to determine this +int TQFontEngineXft::leading() const +{ + int l = qRound(TQMIN( _font->height - (_font->ascent + _font->descent), + ((_font->ascent + _font->descent) >> 4)*_scale )); + return (l > 0) ? l : 1; +} + +// #### use Freetype to determine this +int TQFontEngineXft::lineThickness() const +{ + // ad hoc algorithm + int score = fontDef.weight * fontDef.pixelSize; + int lw = score / 700; + + // looks better with thicker line for small pointsizes + if ( lw < 2 && score >= 1050 ) lw = 2; + if ( lw == 0 ) lw = 1; + + return lw; +} + +// #### use Freetype to determine this +int TQFontEngineXft::underlinePosition() const +{ + int pos = ( ( lineThickness() * 2 ) + 3 ) / 6; + return pos ? pos : 1; +} + +int TQFontEngineXft::maxCharWidth() const +{ + return qRound(_font->max_advance_width*_scale); +} + +static const ushort char_table[] = { + 40, + 67, + 70, + 75, + 86, + 88, + 89, + 91, + 102, + 114, + 124, + 127, + 205, + 645, + 884, + 922, + 1070, + 12386 +}; + +static const int char_table_entries = sizeof(char_table)/sizeof(ushort); + + +int TQFontEngineXft::minLeftBearing() const +{ + if ( lbearing == SHRT_MIN ) + minRightBearing(); // calculates both + + return lbearing; +} + +int TQFontEngineXft::minRightBearing() const +{ + if ( rbearing == SHRT_MIN ) { + TQFontEngineXft *that = (TQFontEngineXft *)this; + that->lbearing = that->rbearing = 0; + TQChar *ch = (TQChar *)char_table; + glyph_t glyphs[char_table_entries]; + int ng = char_table_entries; + stringToCMap(ch, char_table_entries, glyphs, 0, &ng, FALSE); + while (--ng) { + if (glyphs[ng]) { + glyph_metrics_t gi = that->boundingBox( glyphs[ng] ); + if (gi.xoff) { + that->lbearing = TQMIN(lbearing, gi.x); + that->rbearing = TQMIN(rbearing, gi.xoff - gi.x - gi.width); + } + } + } + } + + return rbearing; +} + +int TQFontEngineXft::cmap() const +{ + return _cmap; +} + +const char *TQFontEngineXft::name() const +{ + return "xft"; +} + +void TQFontEngineXft::setScale( double scale ) +{ + _scale = scale; +} + +bool TQFontEngineXft::canRender( const TQChar *string, int len ) +{ + bool allExist = TRUE; + +#ifdef QT_XFT2 + if (_cmap != -1) { + for ( int i = 0; i < len; i++ ) { + if (!XftCharExists(0, _font, string[i].unicode()) + && getAdobeCharIndex(_font, _cmap, string[i].unicode()) == 0) { + allExist = FALSE; + break; + } + } + } else { + for ( int i = 0; i < len; i++ ) { + if (!XftCharExists(0, _font, string[i].unicode())) { + allExist = FALSE; + break; + } + } + } +#else + glyph_t glyphs[256]; + int nglyphs = 255; + glyph_t *g = glyphs; + if ( stringToCMap( string, len, g, 0, &nglyphs, FALSE ) == OutOfMemory ) { + g = (glyph_t *)malloc( nglyphs*sizeof(glyph_t) ); + stringToCMap( string, len, g, 0, &nglyphs, FALSE ); + } + + for ( int i = 0; i < nglyphs; i++ ) { + if ( !XftGlyphExists(TQPaintDevice::x11AppDisplay(), _font, g[i]) ) { + allExist = FALSE; + break; + } + } + + if ( g != glyphs ) + free( g ); +#endif // QT_XFT2 + + return allExist; +} + +TQOpenType *TQFontEngineXft::openType() const +{ +// qDebug("openTypeIface requested!"); + if ( _openType ) + return _openType; + + if ( !_face || ! FT_IS_SFNT( _face ) ) + return 0; + + TQFontEngineXft *that = (TQFontEngineXft *)this; + that->_openType = new TQOpenType(that); + return _openType; +} + + +TQFontEngine::Type TQFontEngineXft::type() const +{ + return Xft; +} +#endif + + +// -------------------------------------------------------------------------------------------------------------------- +// Open type support +// -------------------------------------------------------------------------------------------------------------------- + +#ifndef QT_NO_XFTFREETYPE + +#include "qscriptengine_p.h" + +//#define OT_DEBUG + +#ifdef OT_DEBUG +static inline char *tag_to_string(FT_ULong tag) +{ + static char string[5]; + string[0] = (tag >> 24)&0xff; + string[1] = (tag >> 16)&0xff; + string[2] = (tag >> 8)&0xff; + string[3] = tag&0xff; + string[4] = 0; + return string; +} +#endif + +#define DefaultLangSys 0xffff +#define DefaultScript FT_MAKE_TAG('D', 'F', 'L', 'T') + +enum { + RetquiresGsub = 1, + RetquiresGpos = 2 +}; + +struct OTScripts { + unsigned int tag; + int flags; +}; + +static const OTScripts ot_scripts [] = { +// // European Alphabetic Scripts +// Latin, + { FT_MAKE_TAG( 'l', 'a', 't', 'n' ), 0 }, +// Greek, + { FT_MAKE_TAG( 'g', 'r', 'e', 'k' ), 0 }, +// Cyrillic, + { FT_MAKE_TAG( 'c', 'y', 'r', 'l' ), 0 }, +// Armenian, + { FT_MAKE_TAG( 'a', 'r', 'm', 'n' ), 0 }, +// Georgian, + { FT_MAKE_TAG( 'g', 'e', 'o', 'r' ), 0 }, +// Runic, + { FT_MAKE_TAG( 'r', 'u', 'n', 'r' ), 0 }, +// Ogham, + { FT_MAKE_TAG( 'o', 'g', 'a', 'm' ), 0 }, +// SpacingModifiers, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// CombiningMarks, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, + +// // Middle Eastern Scripts +// Hebrew, + { FT_MAKE_TAG( 'h', 'e', 'b', 'r' ), 1 }, +// Arabic, + { FT_MAKE_TAG( 'a', 'r', 'a', 'b' ), 1 }, +// Syriac, + { FT_MAKE_TAG( 's', 'y', 'r', 'c' ), 1 }, +// Thaana, + { FT_MAKE_TAG( 't', 'h', 'a', 'a' ), 1 }, + +// // South and Southeast Asian Scripts +// Devanagari, + { FT_MAKE_TAG( 'd', 'e', 'v', 'a' ), 1 }, +// Bengali, + { FT_MAKE_TAG( 'b', 'e', 'n', 'g' ), 1 }, +// Gurmukhi, + { FT_MAKE_TAG( 'g', 'u', 'r', 'u' ), 1 }, +// Gujarati, + { FT_MAKE_TAG( 'g', 'u', 'j', 'r' ), 1 }, +// Oriya, + { FT_MAKE_TAG( 'o', 'r', 'y', 'a' ), 1 }, +// Tamil, + { FT_MAKE_TAG( 't', 'a', 'm', 'l' ), 1 }, +// Telugu, + { FT_MAKE_TAG( 't', 'e', 'l', 'u' ), 1 }, +// Kannada, + { FT_MAKE_TAG( 'k', 'n', 'd', 'a' ), 1 }, +// Malayalam, + { FT_MAKE_TAG( 'm', 'l', 'y', 'm' ), 1 }, +// Sinhala, + // ### could not find any OT specs on this + { FT_MAKE_TAG( 's', 'i', 'n', 'h' ), 1 }, +// Thai, + { FT_MAKE_TAG( 't', 'h', 'a', 'i' ), 1 }, +// Lao, + { FT_MAKE_TAG( 'l', 'a', 'o', ' ' ), 1 }, +// Tibetan, + { FT_MAKE_TAG( 't', 'i', 'b', 't' ), 1 }, +// Myanmar, + { FT_MAKE_TAG( 'm', 'y', 'm', 'r' ), 1 }, +// Khmer, + { FT_MAKE_TAG( 'k', 'h', 'm', 'r' ), 1 }, + +// // East Asian Scripts +// Han, + { FT_MAKE_TAG( 'h', 'a', 'n', 'i' ), 0 }, +// Hiragana, + { FT_MAKE_TAG( 'k', 'a', 'n', 'a' ), 0 }, +// Katakana, + { FT_MAKE_TAG( 'k', 'a', 'n', 'a' ), 0 }, +// Hangul, + { FT_MAKE_TAG( 'h', 'a', 'n', 'g' ), 1 }, +// Bopomofo, + { FT_MAKE_TAG( 'b', 'o', 'p', 'o' ), 0 }, +// Yi, + { FT_MAKE_TAG( 'y', 'i', ' ', ' ' ), 0 }, + +// // Additional Scripts +// Ethiopic, + { FT_MAKE_TAG( 'e', 't', 'h', 'i' ), 0 }, +// Cherokee, + { FT_MAKE_TAG( 'c', 'h', 'e', 'r' ), 0 }, +// CanadianAboriginal, + { FT_MAKE_TAG( 'c', 'a', 'n', 's' ), 0 }, +// Mongolian, + { FT_MAKE_TAG( 'm', 'o', 'n', 'g' ), 0 }, +// // Symbols +// CurrencySymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// LetterlikeSymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// NumberForms, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// MathematicalOperators, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// TechnicalSymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// GeometricSymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// MiscellaneousSymbols, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// EnclosedAndSquare, + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, +// Braille, + { FT_MAKE_TAG( 'b', 'r', 'a', 'i' ), 0 }, +// Unicode, should be used + { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 } + // ### where are these? +// { FT_MAKE_TAG( 'b', 'y', 'z', 'm' ), 0 }, +// { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }, + // ### Hangul Jamo +// { FT_MAKE_TAG( 'j', 'a', 'm', 'o' ), 0 }, +}; + +TQOpenType::TQOpenType(TQFontEngineXft *fe) + : fontEngine(fe), gdef(0), gsub(0), gpos(0), current_script(0) +{ + face = fe->face(); + otl_buffer_new(face->memory, &otl_buffer); + tmpAttributes = 0; + tmpLogClusters = 0; + + FT_Error error; + if ((error = TT_Load_GDEF_Table(face, &gdef))) { +#ifdef OT_DEBUG + qDebug("error loading gdef table: %d", error); +#endif + gdef = 0; + } + + if ((error = TT_Load_GSUB_Table(face, &gsub, gdef))) { + gsub = 0; +#ifdef OT_DEBUG + if (error != FT_Err_Table_Missing) { + qDebug("error loading gsub table: %d", error); + } else { + qDebug("face doesn't have a gsub table"); + } +#endif + } + + if ((error = TT_Load_GPOS_Table(face, &gpos, gdef))) { + gpos = 0; +#ifdef OT_DEBUG + qDebug("error loading gpos table: %d", error); +#endif + } + + for (uint i = 0; i < TQFont::NScripts; ++i) + supported_scripts[i] = checkScript(i); +} + +TQOpenType::~TQOpenType() +{ + if (gpos) + TT_Done_GPOS_Table(gpos); + if (gsub) + TT_Done_GSUB_Table(gsub); + if (gdef) + TT_Done_GDEF_Table(gdef); + if (otl_buffer) + otl_buffer_free(otl_buffer); + if (tmpAttributes) + free(tmpAttributes); + if (tmpLogClusters) + free(tmpLogClusters); +} + +bool TQOpenType::checkScript(unsigned int script) +{ + assert(script < TQFont::NScripts); + + uint tag = ot_scripts[script].tag; + int retquirements = ot_scripts[script].flags; + + if (retquirements & RetquiresGsub) { + if (!gsub) + return FALSE; + + FT_UShort script_index; + FT_Error error = TT_GSUB_Select_Script(gsub, tag, &script_index); + if (error) { +#ifdef OT_DEBUG + qDebug("could not select script %d in GSub table: %d", (int)script, error); +#endif + return FALSE; + } + } + + if (retquirements & RetquiresGpos) { + if (!gpos) + return FALSE; + + FT_UShort script_index; + FT_Error error = TT_GPOS_Select_Script(gpos, script, &script_index); + if (error) { +#ifdef OT_DEBUG + qDebug("could not select script in gpos table: %d", error); +#endif + return FALSE; + } + + } + return TRUE; +} + + +void TQOpenType::selectScript(unsigned int script, const Features *features) +{ + if (current_script == script) + return; + + assert(script < TQFont::NScripts); + // find script in our list of supported scripts. + uint tag = ot_scripts[script].tag; + + if (gsub && features) { +#ifdef OT_DEBUG + { + TTO_FeatureList featurelist = gsub->FeatureList; + int numfeatures = featurelist.FeatureCount; + qDebug("gsub table has %d features", numfeatures); + for(int i = 0; i < numfeatures; i++) { + TTO_FeatureRecord *r = featurelist.FeatureRecord + i; + qDebug(" feature '%s'", tag_to_string(r->FeatureTag)); + } + } +#endif + TT_GSUB_Clear_Features(gsub); + FT_UShort script_index; + FT_Error error = TT_GSUB_Select_Script(gsub, tag, &script_index); + if (!error) { +#ifdef OT_DEBUG + qDebug("script %s has script index %d", tag_to_string(script), script_index); +#endif + while (features->tag) { + FT_UShort feature_index; + error = TT_GSUB_Select_Feature(gsub, features->tag, script_index, 0xffff, &feature_index); + if (!error) { +#ifdef OT_DEBUG + qDebug(" adding feature %s", tag_to_string(features->tag)); +#endif + TT_GSUB_Add_Feature(gsub, feature_index, features->property); + } + ++features; + } + } + } + + if (gpos) { + TT_GPOS_Clear_Features(gpos); + FT_UShort script_index; + FT_Error error = TT_GPOS_Select_Script(gpos, tag, &script_index); + if (!error) { +#ifdef OT_DEBUG + { + TTO_FeatureList featurelist = gpos->FeatureList; + int numfeatures = featurelist.FeatureCount; + qDebug("gpos table has %d features", numfeatures); + for(int i = 0; i < numfeatures; i++) { + TTO_FeatureRecord *r = featurelist.FeatureRecord + i; + FT_UShort feature_index; + TT_GPOS_Select_Feature(gpos, r->FeatureTag, script_index, 0xffff, &feature_index); + qDebug(" feature '%s'", tag_to_string(r->FeatureTag)); + } + } +#endif + FT_ULong *feature_tag_list; + error = TT_GPOS_Query_Features(gpos, script_index, 0xffff, &feature_tag_list); + if (!error) { + while (*feature_tag_list) { + FT_UShort feature_index; + error = TT_GPOS_Select_Feature(gpos, *feature_tag_list, script_index, 0xffff, &feature_index); + if (!error) + TT_GPOS_Add_Feature(gpos, feature_index, PositioningProperties); + ++feature_tag_list; + } + } + } + } + + current_script = script; +} + +#ifdef OT_DEBUG +static void dump_string(OTL_Buffer buffer) +{ + for (uint i = 0; i < buffer->in_length; ++i) { + qDebug(" %x: cluster=%d", buffer->in_string[i].gindex, buffer->in_string[i].cluster); + } +} +#endif + +extern void qt_heuristicPosition(TQShaperItem *item); + +bool TQOpenType::shape(TQShaperItem *item, const unsigned int *properties) +{ + length = item->num_glyphs; + + otl_buffer_clear(otl_buffer); + + tmpAttributes = (GlyphAttributes *) realloc(tmpAttributes, length*sizeof(GlyphAttributes)); + tmpLogClusters = (unsigned int *) realloc(tmpLogClusters, length*sizeof(unsigned int)); + for (int i = 0; i < length; ++i) { + otl_buffer_add_glyph(otl_buffer, item->glyphs[i], properties ? properties[i] : 0, i); + tmpAttributes[i] = item->attributes[i]; + tmpLogClusters[i] = item->log_clusters[i]; + } + +#ifdef OT_DEBUG + qDebug("-----------------------------------------"); +// qDebug("log clusters before shaping:"); +// for (int j = 0; j < length; j++) +// qDebug(" log[%d] = %d", j, item->log_clusters[j]); + qDebug("original glyphs: %p", item->glyphs); + for (int i = 0; i < length; ++i) + qDebug(" glyph=%4x", otl_buffer->in_string[i].gindex); +// dump_string(otl_buffer); +#endif + + loadFlags = FT_LOAD_DEFAULT; + + if (gsub) { + uint error = TT_GSUB_Apply_String(gsub, otl_buffer); + if (error && error != TTO_Err_Not_Covered) + return false; + } + +#ifdef OT_DEBUG +// qDebug("log clusters before shaping:"); +// for (int j = 0; j < length; j++) +// qDebug(" log[%d] = %d", j, item->log_clusters[j]); + qDebug("shaped glyphs:"); + for (int i = 0; i < length; ++i) + qDebug(" glyph=%4x", otl_buffer->in_string[i].gindex); + qDebug("-----------------------------------------"); +// dump_string(otl_buffer); +#endif + + return true; +} + +bool TQOpenType::positionAndAdd(TQShaperItem *item, bool doLogClusters) +{ + if (gpos) { +#ifdef Q_WS_X11 + Q_ASSERT(fontEngine->type() == TQFontEngine::Xft); + face = lockFTFace(static_cast(fontEngine)->font()); +#endif + memset(otl_buffer->positions, 0, otl_buffer->in_length*sizeof(OTL_PositionRec)); + // #### check that passing "FALSE,FALSE" is correct + TT_GPOS_Apply_String(face, gpos, loadFlags, otl_buffer, FALSE, FALSE); +#ifdef Q_WS_X11 + unlockFTFace(static_cast(fontEngine)->font()); +#endif + } + + // make sure we have enough space to write everything back + if (item->num_glyphs < (int)otl_buffer->in_length) { + item->num_glyphs = otl_buffer->in_length; + return FALSE; + } + + for (unsigned int i = 0; i < otl_buffer->in_length; ++i) { + item->glyphs[i] = otl_buffer->in_string[i].gindex; + item->attributes[i] = tmpAttributes[otl_buffer->in_string[i].cluster]; + if (i && otl_buffer->in_string[i].cluster == otl_buffer->in_string[i-1].cluster) + item->attributes[i].clusterStart = FALSE; + } + item->num_glyphs = otl_buffer->in_length; + + if (doLogClusters) { + // we can't do this for indic, as we pass the stuf in syllables and it's easier to do it in the shaper. + unsigned short *logClusters = item->log_clusters; + int clusterStart = 0; + int oldCi = 0; + for (unsigned int i = 0; i < otl_buffer->in_length; ++i) { + int ci = otl_buffer->in_string[i].cluster; + // qDebug(" ci[%d] = %d mark=%d, cmb=%d, cs=%d", + // i, ci, glyphAttributes[i].mark, glyphAttributes[i].combiningClass, glyphAttributes[i].clusterStart); + if (!item->attributes[i].mark && item->attributes[i].clusterStart && ci != oldCi) { + for (int j = oldCi; j < ci; j++) + logClusters[j] = clusterStart; + clusterStart = i; + oldCi = ci; + } + } + for (int j = oldCi; j < length; j++) + logClusters[j] = clusterStart; + } + + // calulate the advances for the shaped glyphs +// qDebug("unpositioned: "); + static_cast(item->font)->recalcAdvances(item->num_glyphs, item->glyphs, item->advances); + + // positioning code: + if (gpos) { + float scale = item->font->scale(); + OTL_Position positions = otl_buffer->positions; + +// qDebug("positioned glyphs:"); + for (unsigned int i = 0; i < otl_buffer->in_length; i++) { +// qDebug(" %d:\t orig advance: (%d/%d)\tadv=(%d/%d)\tpos=(%d/%d)\tback=%d\tnew_advance=%d", i, +// glyphs[i].advance.x.toInt(), glyphs[i].advance.y.toInt(), +// (int)(positions[i].x_advance >> 6), (int)(positions[i].y_advance >> 6), +// (int)(positions[i].x_pos >> 6), (int)(positions[i].y_pos >> 6), +// positions[i].back, positions[i].new_advance); + // ###### fix the case where we have y advances. How do we handle this in Uniscribe????? + if (positions[i].new_advance) { + item->advances[i] = item->flags & TQTextEngine::RightToLeft + ? -qRound((positions[i].x_advance >> 6)*scale) + : qRound((positions[i].x_advance >> 6)*scale); + } else { + item->advances[i] += item->flags & TQTextEngine::RightToLeft + ? -qRound((positions[i].x_advance >> 6)*scale) + : qRound((positions[i].x_advance >> 6)*scale); + } + item->offsets[i].x = qRound((positions[i].x_pos >> 6)*scale); + item->offsets[i].y = -qRound((positions[i].y_pos >> 6)*scale); + int back = positions[i].back; + if (item->flags & TQTextEngine::RightToLeft) { + while (back--) { + item->offsets[i].x -= item->advances[i-back]; + } + } else { + while (back) { + item->offsets[i].x -= item->advances[i-back]; + --back; + } + } +// qDebug(" ->\tadv=%d\tpos=(%d/%d)", +// glyphs[i].advance.x.toInt(), glyphs[i].offset.x.toInt(), glyphs[i].offset.y.toInt()); + } + item->has_positioning = TRUE; + } else { + qt_heuristicPosition(item); + } + +#ifdef OT_DEBUG +// if (doLogClusters) { +// qDebug("log clusters after shaping:"); +// for (int j = 0; j < length; j++) +// qDebug(" log[%d] = %d", j, item->log_clusters[j]); +// } + qDebug("final glyphs:"); + for (int i = 0; i < (int)otl_buffer->in_length; ++i) + qDebug(" glyph=%4x char_index=%d mark: %d cmp: %d, clusterStart: %d advance=%d offset=%d/%d", + item->glyphs[i], otl_buffer->in_string[i].cluster, item->attributes[i].mark, + item->attributes[i].combiningClass, item->attributes[i].clusterStart, + item->advances[i], + item->offsets[i].x, item->offsets[i].y); + qDebug("-----------------------------------------"); +#endif + return TRUE; +} + +#endif diff --git a/src/kernel/qfontinfo.h b/src/kernel/qfontinfo.h new file mode 100644 index 000000000..ce733e59f --- /dev/null +++ b/src/kernel/qfontinfo.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Definition of TQFontInfo class +** +** Created : 950131 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQFONTINFO_H +#define TQFONTINFO_H + +#ifndef QT_H +#include "qfont.h" +#endif // QT_H + + +class Q_EXPORT TQFontInfo +{ +public: + TQFontInfo( const TQFont & ); + TQFontInfo( const TQFont &, TQFont::Script ); + TQFontInfo( const TQFontInfo & ); + ~TQFontInfo(); + + TQFontInfo &operator=( const TQFontInfo & ); + + TQString family() const; + int pixelSize() const; + int pointSize() const; + bool italic() const; + int weight() const; + bool bold() const; + bool underline() const; + bool overline() const; + bool strikeOut() const; + bool fixedPitch() const; + TQFont::StyleHint styleHint() const; + bool rawMode() const; + + bool exactMatch() const; + + +private: + TQFontInfo( const TQPainter * ); + + TQFontPrivate *d; + TQPainter *painter; + int fscript; + + friend class TQWidget; + friend class TQPainter; +}; + + +inline bool TQFontInfo::bold() const +{ return weight() > TQFont::Normal; } + + +#endif // TQFONTINFO_H diff --git a/src/kernel/qfontmetrics.h b/src/kernel/qfontmetrics.h new file mode 100644 index 000000000..e407aa46d --- /dev/null +++ b/src/kernel/qfontmetrics.h @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** Definition of TQFontMetrics class +** +** Created : 940514 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQFONTMETRICS_H +#define TQFONTMETRICS_H + +#ifndef QT_H +#include "qfont.h" +#include "qrect.h" +#endif // QT_H + +#ifdef Q_WS_QWS +class TQFontEngine; +#endif + +class TQTextCodec; +class TQTextParag; + +class Q_EXPORT TQFontMetrics +{ +public: + TQFontMetrics( const TQFont & ); + TQFontMetrics( const TQFont &, TQFont::Script ); + TQFontMetrics( const TQFontMetrics & ); + ~TQFontMetrics(); + + TQFontMetrics &operator=( const TQFontMetrics & ); + + int ascent() const; + int descent() const; + int height() const; + int leading() const; + int lineSpacing() const; + int minLeftBearing() const; + int minRightBearing() const; + int maxWidth() const; + + bool inFont(TQChar) const; + + int leftBearing(TQChar) const; + int rightBearing(TQChar) const; + int width( const TQString &, int len = -1 ) const; + + int width( TQChar ) const; +#ifndef QT_NO_COMPAT + int width( char c ) const { return width( (TQChar) c ); } +#endif + + int charWidth( const TQString &str, int pos ) const; + TQRect boundingRect( const TQString &, int len = -1 ) const; + TQRect boundingRect( TQChar ) const; + TQRect boundingRect( int x, int y, int w, int h, int flags, + const TQString& str, int len=-1, int tabstops=0, + int *tabarray=0, TQTextParag **intern=0 ) const; + TQSize size( int flags, + const TQString& str, int len=-1, int tabstops=0, + int *tabarray=0, TQTextParag **intern=0 ) const; + + int underlinePos() const; + int overlinePos() const; + int strikeOutPos() const; + int lineWidth() const; + +private: + TQFontMetrics( const TQPainter * ); + + friend class TQWidget; + friend class TQPainter; + friend class TQTextFormat; +#if defined( Q_WS_MAC ) + friend class TQFontPrivate; +#endif + + TQFontPrivate *d; + TQPainter *painter; + int fscript; +}; + + +#endif // TQFONTMETRICS_H diff --git a/src/kernel/qgif.h b/src/kernel/qgif.h new file mode 100644 index 000000000..6e9e9fc74 --- /dev/null +++ b/src/kernel/qgif.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** To enable built-in reading of GIF images in TQt, change the definition +** below to "#define QT_BUILTIN_GIF_READER 1". +** +** To disable built-in reading of GIF images in TQt, change the definition +** below to "#define QT_BUILTIN_GIF_READER 0". +** +** WARNING: +** A separate license from Unisys may be retquired to use the gif +** reader. See http://www.unisys.com/about__unisys/lzw/ +** for information from Unisys +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQGIF_H +#define TQGIF_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + +#ifndef QT_BUILTIN_GIF_READER +#define QT_BUILTIN_GIF_READER 0 +#endif + +bool qt_builtin_gif_reader(); + +#endif // TQGIF_H diff --git a/src/kernel/qgplugin.cpp b/src/kernel/qgplugin.cpp new file mode 100644 index 000000000..79110a871 --- /dev/null +++ b/src/kernel/qgplugin.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** ... +** +** Copyright (C) 2001-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qgplugin.h" + +#ifndef QT_NO_COMPONENT + +#include + +TQGPlugin::TQGPlugin() + : _iface( 0 ) +{ +} + +TQGPlugin::TQGPlugin( TQUnknownInterface *i ) + : _iface( i ) +{ +} + +TQGPlugin::~TQGPlugin() +{ +} + +TQUnknownInterface* TQGPlugin::iface() +{ + Q_ASSERT( _iface ); + TQUnknownInterface *i; + _iface->queryInterface( IID_QUnknown, &i ); + return i; +} + +void TQGPlugin::setIface( TQUnknownInterface *iface ) +{ + _iface = iface; +} + +#endif // QT_NO_COMPONENT diff --git a/src/kernel/qgplugin.h b/src/kernel/qgplugin.h new file mode 100644 index 000000000..075c411f0 --- /dev/null +++ b/src/kernel/qgplugin.h @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** ... +** +** Copyright (C) 2001-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQGPLUGIN_H +#define TQGPLUGIN_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of a number of TQt sources files. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_COMPONENT + +#ifndef Q_EXTERN_C +#ifdef __cplusplus +#define Q_EXTERN_C extern "C" +#else +#define Q_EXTERN_C extern +#endif +#endif + +#ifndef Q_EXPORT_PLUGIN +#if defined(QT_THREAD_SUPPORT) +#define QT_THREADED_BUILD 1 +#define Q_PLUGIN_FLAGS_STRING "11" +#else +#define QT_THREADED_BUILD 0 +#define Q_PLUGIN_FLAGS_STRING "01" +#endif + +// this is duplicated at Q_UCM_VERIFICATION_DATA in qcom_p.h +// NOTE: if you change pattern, you MUST change the pattern in +// qcomlibrary.cpp as well. changing the pattern will break all +// backwards compatibility as well (no old plugins will be loaded). +#ifndef Q_PLUGIN_VERIFICATION_DATA +# define Q_PLUGIN_VERIFICATION_DATA \ + static const char *qt_ucm_verification_data = \ + "pattern=""QT_UCM_VERIFICATION_DATA""\n" \ + "version="QT_VERSION_STR"\n" \ + "flags="Q_PLUGIN_FLAGS_STRING"\n" \ + "buildkey="QT_BUILD_KEY"\0"; +#endif // Q_PLUGIN_VERIFICATION_DATA + +#define Q_PLUGIN_INSTANTIATE( IMPLEMENTATION ) \ + { \ + IMPLEMENTATION *i = new IMPLEMENTATION; \ + return i->iface(); \ + } + +# ifdef Q_WS_WIN +# ifdef Q_CC_BOR +# define Q_EXPORT_PLUGIN(PLUGIN) \ + Q_PLUGIN_VERIFICATION_DATA \ + Q_EXTERN_C __declspec(dllexport) \ + const char * __stdcall qt_ucm_query_verification_data() \ + { return qt_ucm_verification_data; } \ + Q_EXTERN_C __declspec(dllexport) TQUnknownInterface* \ + __stdcall ucm_instantiate() \ + Q_PLUGIN_INSTANTIATE( PLUGIN ) +# else +# define Q_EXPORT_PLUGIN(PLUGIN) \ + Q_PLUGIN_VERIFICATION_DATA \ + Q_EXTERN_C __declspec(dllexport) \ + const char *qt_ucm_query_verification_data() \ + { return qt_ucm_verification_data; } \ + Q_EXTERN_C __declspec(dllexport) TQUnknownInterface* ucm_instantiate() \ + Q_PLUGIN_INSTANTIATE( PLUGIN ) +# endif +# else +# define Q_EXPORT_PLUGIN(PLUGIN) \ + Q_PLUGIN_VERIFICATION_DATA \ + Q_EXTERN_C \ + const char *qt_ucm_query_verification_data() \ + { return qt_ucm_verification_data; } \ + Q_EXTERN_C TQUnknownInterface* ucm_instantiate() \ + Q_PLUGIN_INSTANTIATE( PLUGIN ) +# endif + +#endif + +struct TQUnknownInterface; + +class Q_EXPORT TQGPlugin : public TQObject +{ + Q_OBJECT +public: + TQGPlugin( TQUnknownInterface *i ); + ~TQGPlugin(); + + TQUnknownInterface* iface(); + void setIface( TQUnknownInterface *iface ); + +private: + TQGPlugin(); + TQUnknownInterface* _iface; +}; + +#endif // QT_NO_COMPONENT + +#endif // TQGPLUGIN_H diff --git a/src/kernel/qguardedptr.cpp b/src/kernel/qguardedptr.cpp new file mode 100644 index 000000000..66355ca9a --- /dev/null +++ b/src/kernel/qguardedptr.cpp @@ -0,0 +1,226 @@ +/**************************************************************************** +** +** Implementation of TQGuardedPtr class +** +** Created : 990929 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qguardedptr.h" + +/*! + \class TQGuardedPtr qguardedptr.h + \brief The TQGuardedPtr class is a template class that provides guarded pointers to TQObjects. + + \ingroup objectmodel + \mainclass + + A guarded pointer, \c{TQGuardedPtr}, behaves like a normal C++ + pointer \c{X*}, except that it is automatically set to 0 when + the referenced object is destroyed (unlike normal C++ pointers, + which become "dangling pointers" in such cases). \c X must be a + subclass of TQObject. + + Guarded pointers are useful whenever you need to store a pointer + to a TQObject that is owned by someone else and therefore might be + destroyed while you still hold a reference to it. You can safely + test the pointer for validity. + + Example: + \code + TQGuardedPtr label = new TQLabel( 0, "label" ); + label->setText( "I like guarded pointers" ); + + delete (TQLabel*) label; // simulate somebody destroying the label + + if ( label) + label->show(); + else + qDebug("The label has been destroyed"); + \endcode + + The program will output \c{The label has been destroyed} rather + than dereferencing an invalid address in \c label->show(). + + The functions and operators available with a TQGuardedPtr are the + same as those available with a normal unguarded pointer, except + the pointer arithmetic operators (++, --, -, and +), which are + normally used only with arrays of objects. Use them like normal + pointers and you will not need to read this class documentation. + + For creating guarded pointers, you can construct or assign to them + from an X* or from another guarded pointer of the same type. You + can compare them with each other using operator==() and + operator!=(), or test for 0 with isNull(). And you can dereference + them using either the \c *x or the \c x->member notation. + + A guarded pointer will automatically cast to an X*, so you can + freely mix guarded and unguarded pointers. This means that if you + have a TQGuardedPtr, you can pass it to a function that + retquires a TQWidget*. For this reason, it is of little value to + declare functions to take a TQGuardedPtr as a parameter; just use + normal pointers. Use a TQGuardedPtr when you are storing a pointer + over time. + + Note again that class \e X must inherit TQObject, or a compilation + or link error will result. +*/ + +/*! + \fn TQGuardedPtr::TQGuardedPtr() + + Constructs a 0 guarded pointer. + + \sa isNull() +*/ + +/*! + \fn TQGuardedPtr::TQGuardedPtr( T* p ) + + Constructs a guarded pointer that points to same object as \a p + points to. +*/ + +/*! + \fn TQGuardedPtr::TQGuardedPtr(const TQGuardedPtr &p) + + Copy one guarded pointer from another. The constructed guarded + pointer points to the same object that \a p points to (which may + be 0). +*/ + +/*! + \fn TQGuardedPtr::~TQGuardedPtr() + + Destroys the guarded pointer. Just like a normal pointer, + destroying a guarded pointer does \e not destroy the object being + pointed to. +*/ + +/*! + \fn TQGuardedPtr& TQGuardedPtr::operator=(const TQGuardedPtr &p) + + Assignment operator. This guarded pointer then points to the same + object as \a p points to. +*/ + +/*! + \overload TQGuardedPtr & TQGuardedPtr::operator=(T* p) + + Assignment operator. This guarded pointer then points to the same + object as \a p points to. +*/ + +/*! + \fn bool TQGuardedPtr::operator==( const TQGuardedPtr &p ) const + + Equality operator; implements traditional pointer semantics. + Returns TRUE if both \a p and this guarded pointer are 0, or if + both \a p and this pointer point to the same object; otherwise + returns FALSE. + + \sa operator!=() +*/ + +/*! + \fn bool TQGuardedPtr::operator!= ( const TQGuardedPtr& p ) const + + Inequality operator; implements pointer semantics, the negation of + operator==(). Returns TRUE if \a p and this guarded pointer are + not pointing to the same object; otherwise returns FALSE. +*/ + +/*! + \fn bool TQGuardedPtr::isNull() const + + Returns \c TRUE if the referenced object has been destroyed or if + there is no referenced object; otherwise returns FALSE. +*/ + +/*! + \fn T* TQGuardedPtr::operator->() const + + Overloaded arrow operator; implements pointer semantics. Just use + this operator as you would with a normal C++ pointer. +*/ + +/*! + \fn T& TQGuardedPtr::operator*() const + + Dereference operator; implements pointer semantics. Just use this + operator as you would with a normal C++ pointer. +*/ + +/*! + \fn TQGuardedPtr::operator T*() const + + Cast operator; implements pointer semantics. Because of this + function you can pass a TQGuardedPtr\ to a function where an X* + is retquired. +*/ + + +/* Internal classes */ + + +TQGuardedPtrPrivate::TQGuardedPtrPrivate( TQObject* o) + : TQObject(0, "_ptrpriv" ), obj( o ) +{ + if ( obj ) + connect( obj, SIGNAL( destroyed() ), this, SLOT( objectDestroyed() ) ); +} + + +TQGuardedPtrPrivate::~TQGuardedPtrPrivate() +{ +} + +void TQGuardedPtrPrivate::reconnect( TQObject *o ) +{ + if ( obj == o ) + return; + if ( obj ) + disconnect( obj, SIGNAL( destroyed() ), + this, SLOT( objectDestroyed() ) ); + obj = o; + if ( obj ) + connect( obj, SIGNAL( destroyed() ), + this, SLOT( objectDestroyed() ) ); +} + +void TQGuardedPtrPrivate::objectDestroyed() +{ + obj = 0; +} diff --git a/src/kernel/qguardedptr.h b/src/kernel/qguardedptr.h new file mode 100644 index 000000000..2b66c6d4c --- /dev/null +++ b/src/kernel/qguardedptr.h @@ -0,0 +1,144 @@ +/**************************************************************************** +** +** Definition of TQGuardedPtr class +** +** Created : 990929 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQGUARDEDPTR_H +#define TQGUARDEDPTR_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +// ### 4.0: rename to something without Private in it. Not really internal. +class Q_EXPORT TQGuardedPtrPrivate : public TQObject, public TQShared +{ + Q_OBJECT +public: + TQGuardedPtrPrivate( TQObject* ); + ~TQGuardedPtrPrivate(); + + TQObject* object() const; + void reconnect( TQObject* ); + +private slots: + void objectDestroyed(); + +private: + TQObject* obj; +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQGuardedPtrPrivate( const TQGuardedPtrPrivate & ); + TQGuardedPtrPrivate &operator=( const TQGuardedPtrPrivate & ); +#endif +}; + +template +class TQGuardedPtr +{ +public: + TQGuardedPtr() : priv( new TQGuardedPtrPrivate( 0 ) ) {} + + TQGuardedPtr( T* o) { + priv = new TQGuardedPtrPrivate( (TQObject*)o ); + } + + TQGuardedPtr(const TQGuardedPtr &p) { + priv = p.priv; + ref(); + } + + ~TQGuardedPtr() { deref(); } + + TQGuardedPtr &operator=(const TQGuardedPtr &p) { + if ( priv != p.priv ) { + deref(); + priv = p.priv; + ref(); + } + return *this; + } + + TQGuardedPtr &operator=(T* o) { + if ( priv && priv->count == 1 ) { + priv->reconnect( (TQObject*)o ); + } else { + deref(); + priv = new TQGuardedPtrPrivate( (TQObject*)o ); + } + return *this; + } + + bool operator==( const TQGuardedPtr &p ) const { + return (T*)(*this) == (T*) p; + } + + bool operator!= ( const TQGuardedPtr& p ) const { + return !( *this == p ); + } + + bool isNull() const { return !priv || !priv->object(); } + + T* operator->() const { return (T*)(priv?priv->object():0); } + + T& operator*() const { return *((T*)(priv?priv->object():0)); } + + operator T*() const { return (T*)(priv?priv->object():0); } + +private: + + void ref() { if (priv) priv->ref(); } + + void deref() { + if ( priv && priv->deref() ) + delete priv; + } + + TQGuardedPtrPrivate* priv; +}; + + + + +inline TQObject* TQGuardedPtrPrivate::object() const +{ + return obj; +} + +#define Q_DEFINED_QGUARDEDPTR +#include "qwinexport.h" +#endif diff --git a/src/kernel/qiconset.cpp b/src/kernel/qiconset.cpp new file mode 100644 index 000000000..34dd620ef --- /dev/null +++ b/src/kernel/qiconset.cpp @@ -0,0 +1,949 @@ +/**************************************************************************** +** +** Implementation of TQIconSet class +** +** Created : 980318 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qiconset.h" + +#ifndef QT_NO_ICONSET + +#include "qapplication.h" +#include "qbitmap.h" +#include "qcleanuphandler.h" +#include "qimage.h" +#include "qpainter.h" + +enum { NumSizes = 2, NumModes = 3, NumStates = 2 }; + +static TQIconFactory *defaultFac = 0; +static TQSingleCleanupHandler q_cleanup_icon_factory; + +static short widths[2] = { 22, 32 }; +static short heights[2] = { 22, 32 }; + +enum TQIconSetIconOrigin { + SuppliedFileName, // 'fileName' contains the name of the file + SuppliedPixmap, // 'pixmap' is a pointer to the user-supplied pixmap + Manufactured, // 'pixmap' is a factory-generated pixmap (or 0) + Generated // 'pixmap' is a TQIconSet-generated pixmap (or 0) +}; + +struct TQIconSetIcon +{ + TQIconSetIconOrigin origin; + union { + TQString *fileName; + TQPixmap *pixmap; + }; + + TQIconSetIcon() : origin( Generated ) { pixmap = 0; } + TQIconSetIcon( const TQIconSetIcon& other ) + : origin( Generated ) { + pixmap = 0; + operator=( other ); + } + ~TQIconSetIcon() { + if ( origin == SuppliedFileName ) { + delete fileName; + } else { + delete pixmap; + } + } + + TQIconSetIcon& operator=( const TQIconSetIcon& other ); + + void clearCached() { + if ( pixmap && (origin == Manufactured || origin == Generated) ) { + origin = Generated; + delete pixmap; + pixmap = 0; + } + } +}; + +TQIconSetIcon& TQIconSetIcon::operator=( const TQIconSetIcon& other ) +{ + TQPixmap *oldPixmap = 0; + TQString *oldFileName = 0; + if ( origin == SuppliedFileName ) { + oldFileName = fileName; + } else { + oldPixmap = pixmap; + } + + origin = other.origin; + if ( other.origin == SuppliedFileName ) { + fileName = new TQString( *other.fileName ); + } else { + if ( other.pixmap ) { + pixmap = new TQPixmap( *other.pixmap ); + } else { + pixmap = 0; + } + } + delete oldPixmap; + delete oldFileName; + return *this; +} + +class TQIconSetPrivate : public TQShared +{ +public: + TQIconSetIcon icons[NumSizes][NumModes][NumStates]; + TQPixmap defaultPix; + TQIconFactory *factory; + + TQIconSetPrivate() : factory( 0 ) { } + TQIconSetPrivate( const TQIconSetPrivate& other ) : TQShared() { + count = 1; + for ( int i = 0; i < NumSizes; i++ ) { + for ( int j = 0; j < NumModes; j++ ) { + for ( int k = 0; k < NumStates; k++ ) { + icons[i][j][k] = other.icons[i][j][k]; + } + } + } + defaultPix = other.defaultPix; + factory = other.factory; + if ( factory ) + factory->ref(); + } + ~TQIconSetPrivate() { + setFactory( 0 ); + } + + TQIconSetIcon *icon( const TQIconSet *iconSet, TQIconSet::Size size, + TQIconSet::Mode mode, TQIconSet::State state ); + void setFactory( TQIconFactory *newFactory ) { + if ( newFactory ) + newFactory->ref(); + if ( factory && factory->deref() && factory->autoDelete() ) + delete factory; + factory = newFactory; + } + + Q_DUMMY_COMPARISON_OPERATOR( TQIconSetPrivate ) +}; + +TQIconSetIcon *TQIconSetPrivate::icon( const TQIconSet *iconSet, + TQIconSet::Size size, TQIconSet::Mode mode, + TQIconSet::State state ) +{ + TQIconSetIcon *ik = &icons[(int) size - 1][(int) mode][(int) state]; + + if ( iconSet ) { + if ( ik->origin == SuppliedFileName ) { + TQPixmap *newPixmap = new TQPixmap( *ik->fileName ); + delete ik->fileName; + + if ( newPixmap->isNull() ) { + delete newPixmap; + ik->origin = Generated; + ik->pixmap = 0; + } else { + ik->origin = SuppliedPixmap; + ik->pixmap = newPixmap; + } + } + + if ( !ik->pixmap && ik->origin == Generated ) { + TQIconFactory *f = factory; + if ( !f ) + f = defaultFac; + + if ( f ) { + /* + We set 'origin' to Manufactured half a second too + early to prevent recursive calls to this function. + (This can happen if createPixmap() calls + TQIconSet::pixmap(), which in turn calls this + function.) + */ + ik->origin = Manufactured; + ik->pixmap = f->createPixmap( *iconSet, size, mode, state ); + if ( !ik->pixmap ) + ik->origin = Generated; + } + } + } + return ik; +} + +/*! \class TQIconSet + + \brief The TQIconSet class provides a set of icons with different + styles and sizes. + + \ingroup graphics + \ingroup images + \ingroup shared + \mainclass + + A TQIconSet can generate smaller, larger, active, and disabled pixmaps + from the set of icons it is given. Such pixmaps are used by + TQToolButton, TQHeader, TQPopupMenu, etc. to show an icon representing a + particular action. + + The simplest use of TQIconSet is to create one from a TQPixmap and then + use it, allowing TQt to work out all the retquired icon styles and + sizes. For example: + + \code + TQToolButton *but = new TQToolButton( TQIconSet( TQPixmap("open.xpm") ), ... ); + \endcode + + Using whichever pixmaps you specify as a base, TQIconSet provides a + set of six icons, each with a \l Size and a \l Mode: Small Normal, + Small Disabled, Small Active, Large Normal, Large Disabled, and + Large Active. + + An additional set of six icons can be provided for widgets that have + an "On" or "Off" state, like checkable menu items or toggleable + toolbuttons. If you provide pixmaps for the "On" state, but not for + the "Off" state, the TQIconSet will provide the "Off" pixmaps. You may + specify icons for both states in you wish. + + You can set any of the icons using setPixmap(). + + When you retrieve a pixmap using pixmap(Size, Mode, State), + TQIconSet will return the icon that has been set or previously + generated for that size, mode and state combination. If none is + available, TQIconSet will ask the icon factory. If the icon factory + cannot provide any (the default), TQIconSet generates a pixmap based + on the pixmaps it has been given and returns it. + + The \c Disabled appearance is computed using an algorithm that + produces results very similar to those used in Microsoft Windows + 95. The \c Active appearance is identical to the \c Normal + appearance unless you use setPixmap() to set it to something + special. + + When scaling icons, TQIconSet uses \link TQImage::smoothScale() + smooth scaling\endlink, which can partially blend the color component + of pixmaps. If the results look poor, the best solution + is to supply pixmaps in both large and small sizes. + + You can use the static function setIconSize() to set the preferred + size of the generated large/small icons. The default small size is + 22 x 22, while the default large size is 32 x 32. These sizes only + affect generated icons. + + The isGenerated() function returns TRUE if an icon was generated by + TQIconSet or by a factory; clearGenerated() clears all cached + pixmaps. + + \section1 Making Classes that Use TQIconSet + + If you write your own widgets that have an option to set a small + pixmap, consider allowing a TQIconSet to be set for that pixmap. The + TQt class TQToolButton is an example of such a widget. + + Provide a method to set a TQIconSet, and when you draw the icon, choose + whichever icon is appropriate for the current state of your widget. + For example: + \code + void MyWidget::drawIcon( TQPainter* p, TQPoint pos ) + { + p->drawPixmap( pos, icons->pixmap( + TQIconSet::Small, + isEnabled() ? TQIconSet::Normal : + TQIconSet::Disabled, + isEnabled() ? TQIconSet::On : + TQIconSet::Off)); + } + \endcode + + You might also make use of the \c Active mode, perhaps making your + widget \c Active when the mouse is over the widget (see \l + TQWidget::enterEvent()), while the mouse is pressed pending the + release that will activate the function, or when it is the currently + selected item. If the widget can be toggled, the "On" mode might be + used to draw a different icon. + + \img iconset.png TQIconSet + + \sa TQIconFactory TQPixmap TQMainWindow::setUsesBigPixmaps() + \link guibooks.html#fowler GUI Design Handbook: Iconic Label \endlink +*/ + + +/*! + \enum TQIconSet::Size + + This enum type describes the size at which a pixmap is intended to be + used. + The currently defined sizes are: + + \value Automatic The size of the pixmap is determined from its + pixel size. This is a useful default. + \value Small The pixmap is the smaller of two. + \value Large The pixmap is the larger of two. + + If a Small pixmap is not set by TQIconSet::setPixmap(), the Large + pixmap will be automatically scaled down to the size of a small pixmap + to generate the Small pixmap when retquired. Similarly, a Small pixmap + will be automatically scaled up to generate a Large pixmap. The + preferred sizes for large/small generated icons can be set using + setIconSize(). + + \sa setIconSize() iconSize() setPixmap() pixmap() TQMainWindow::setUsesBigPixmaps() +*/ + +/*! + \enum TQIconSet::Mode + + This enum type describes the mode for which a pixmap is intended to be + used. + The currently defined modes are: + + \value Normal + Display the pixmap when the user is + not interacting with the icon, but the + functionality represented by the icon is available. + \value Disabled + Display the pixmap when the + functionality represented by the icon is not available. + \value Active + Display the pixmap when the + functionality represented by the icon is available and + the user is interacting with the icon, for example, moving the + mouse over it or clicking it. +*/ + +/*! + \enum TQIconSet::State + + This enum describes the state for which a pixmap is intended to be + used. The \e state can be: + + \value Off Display the pixmap when the widget is in an "off" state + \value On Display the pixmap when the widget is in an "on" state + + \sa setPixmap() pixmap() +*/ + +/*! + Constructs a null icon set. + + \sa setPixmap(), reset() +*/ +TQIconSet::TQIconSet() + : d( 0 ) +{ +} + +/*! + Constructs an icon set for which the Normal pixmap is \a pixmap, + which is assumed to be of size \a size. + + The default for \a size is \c Automatic, which means that TQIconSet + will determine whether the pixmap is Small or Large from its pixel + size. Pixmaps less than the width of a small generated icon are + considered to be Small. You can use setIconSize() to set the + preferred size of a generated icon. + + \sa setIconSize() reset() +*/ +TQIconSet::TQIconSet( const TQPixmap& pixmap, Size size ) + : d( 0 ) +{ + reset( pixmap, size ); +} + +/*! Creates an iconset which uses the pixmap \a smallPix for for + displaying a small icon, and the pixmap \a largePix for displaying a + large icon. +*/ +TQIconSet::TQIconSet( const TQPixmap& smallPix, const TQPixmap& largePix ) + : d( 0 ) +{ + reset( smallPix, Small ); + reset( largePix, Large ); +} + +/*! + Constructs a copy of \a other. This is very fast. +*/ +TQIconSet::TQIconSet( const TQIconSet& other ) + : d( other.d ) +{ + if ( d ) + d->ref(); +} + +/*! + Destroys the icon set and frees any allocated resources. +*/ +TQIconSet::~TQIconSet() +{ + if ( d && d->deref() ) + delete d; +} + +/*! + Sets this icon set to use pixmap \a pixmap for the Normal pixmap, + assuming it to be of size \a size. + + This is equivalent to assigning TQIconSet(\a pixmap, \a size) to this + icon set. + + This function does nothing if \a pixmap is a null pixmap. +*/ +void TQIconSet::reset( const TQPixmap& pixmap, Size size ) +{ + if ( pixmap.isNull() ) + return; + + detach(); + normalize( size, pixmap.size() ); + setPixmap( pixmap, size, Normal ); + d->defaultPix = pixmap; + d->setFactory( 0 ); +} + +/*! + Sets this icon set to provide pixmap \a pixmap for size \a size, mode \a + mode and state \a state. The icon set may also use \a pixmap for + generating other pixmaps if they are not explicitly set. + + The \a size can be one of Automatic, Large or Small. If Automatic is + used, TQIconSet will determine if the pixmap is Small or Large from its + pixel size. + + Pixmaps less than the width of a small generated icon are + considered to be Small. You can use setIconSize() to set the preferred + size of a generated icon. + + This function does nothing if \a pixmap is a null pixmap. + + \sa reset() +*/ +void TQIconSet::setPixmap( const TQPixmap& pixmap, Size size, Mode mode, + State state ) +{ + if ( pixmap.isNull() ) + return; + + normalize( size, pixmap.size() ); + + detach(); + clearGenerated(); + + TQIconSetIcon *icon = d->icon( 0, size, mode, state ); + if ( icon->origin == SuppliedFileName ) { + delete icon->fileName; + icon->pixmap = 0; + } + icon->origin = SuppliedPixmap; + if ( icon->pixmap == 0 ) { + icon->pixmap = new TQPixmap( pixmap ); + } else { + *icon->pixmap = pixmap; + } +} + +/*! + \overload + + The pixmap is loaded from \a fileName when it becomes necessary. +*/ +void TQIconSet::setPixmap( const TQString& fileName, Size size, Mode mode, + State state ) +{ + if ( size == Automatic ) { + setPixmap( TQPixmap(fileName), size, mode, state ); + } else { + detach(); + clearGenerated(); + + TQIconSetIcon *icon = d->icon( 0, size, mode, state ); + if ( icon->origin == SuppliedFileName ) { + *icon->fileName = fileName; + } else { + delete icon->pixmap; + icon->fileName = new TQString( fileName ); + icon->origin = SuppliedFileName; + } + } +} + +/*! + Returns a pixmap with size \a size, mode \a mode and state \a + state, generating one if necessary. Generated pixmaps are cached. +*/ +TQPixmap TQIconSet::pixmap( Size size, Mode mode, State state ) const +{ + if ( !d ) { + if ( defaultFac ) { + TQIconSet *that = (TQIconSet *) this; + that->detach(); + } else { + return TQPixmap(); + } + } + + if ( size == Automatic ) + size = Small; + + TQIconSetIcon *icon = d->icon( this, size, mode, state ); + if ( icon->pixmap ) + return *icon->pixmap; + if ( icon->origin == Manufactured ) { + /* + This can only occur during the half a second's time when + the icon is being manufactured. If TQIconFactory somehow + tries to access the pixmap it's supposed to be creating, it + will get a null pixmap. + */ + return TQPixmap(); + } + + if ( mode == Active ) + return pixmap( size, Normal, state ); + + Size otherSize = ( size == Large ) ? Small : Large; + TQIconSetIcon *otherSizeIcon = d->icon( this, otherSize, mode, state ); + + if ( state == Off ) { + if ( mode == Disabled && + d->icon(this, size, Normal, Off)->origin != Generated ) { + icon->pixmap = createDisabled( size, Off ); + } else if ( otherSizeIcon->origin != Generated ) { + icon->pixmap = createScaled( size, otherSizeIcon->pixmap ); + } else if ( mode == Disabled ) { + icon->pixmap = createDisabled( size, Off ); + } else if ( !d->defaultPix.isNull() ) { + icon->pixmap = new TQPixmap( d->defaultPix ); + } else { + /* + No icons are available for { TRUE, Normal, Off } and + { FALSE, Normal, Off }. Try the other 10 combinaisons, + best ones first. + */ + const int N = 10; + static const struct { + bool sameSize; + Mode mode; + State state; + } tryList[N] = { + { TRUE, Active, Off }, + { TRUE, Normal, On }, + { TRUE, Active, On }, + { FALSE, Active, Off }, + { FALSE, Normal, On }, + { FALSE, Active, On }, + { TRUE, Disabled, Off }, + { TRUE, Disabled, On }, + { FALSE, Disabled, Off }, + { FALSE, Disabled, On } + }; + + for ( int i = 0; i < N; i++ ) { + bool sameSize = tryList[i].sameSize; + TQIconSetIcon *tryIcon = + d->icon( this, sameSize ? size : otherSize, + tryList[i].mode, tryList[i].state ); + if ( tryIcon->origin != Generated ) { + if ( sameSize ) { + if ( tryIcon->pixmap ) + icon->pixmap = new TQPixmap( *tryIcon->pixmap ); + } else { + icon->pixmap = createScaled( size, tryIcon->pixmap ); + } + break; + } + } + } + } else { /* ( state == On ) */ + if ( mode == Normal ) { + if ( otherSizeIcon->origin != Generated ) { + icon->pixmap = createScaled( size, otherSizeIcon->pixmap ); + } else { + icon->pixmap = new TQPixmap( pixmap(size, mode, Off) ); + } + } else { /* ( mode == Disabled ) */ + TQIconSetIcon *offIcon = d->icon( this, size, mode, Off ); + TQIconSetIcon *otherSizeOffIcon = d->icon( this, otherSize, mode, + Off ); + + if ( offIcon->origin != Generated ) { + if ( offIcon->pixmap ) + icon->pixmap = new TQPixmap( *offIcon->pixmap ); + } else if ( d->icon(this, size, Normal, On)->origin != Generated ) { + icon->pixmap = createDisabled( size, On ); + } else if ( otherSizeIcon->origin != Generated ) { + icon->pixmap = createScaled( size, otherSizeIcon->pixmap ); + } else if ( otherSizeOffIcon->origin != Generated ) { + icon->pixmap = createScaled( size, otherSizeOffIcon->pixmap ); + } else { + icon->pixmap = createDisabled( size, On ); + } + } + } + if ( icon->pixmap ) { + return *icon->pixmap; + } else { + return TQPixmap(); + } +} + +/*! \overload + \obsolete + + This is the same as pixmap(\a size, \a enabled, \a state). +*/ +TQPixmap TQIconSet::pixmap( Size size, bool enabled, State state ) const +{ + return pixmap( size, enabled ? Normal : Disabled, state ); +} + +/*! + \overload + + Returns the pixmap originally provided to the constructor or to + reset(). This is the Normal pixmap of unspecified Size. + + \sa reset() +*/ +TQPixmap TQIconSet::pixmap() const +{ + if ( !d ) + return TQPixmap(); + return d->defaultPix; +} + +/*! + Returns TRUE if the pixmap with size \a size, mode \a mode and + state \a state is generated from other pixmaps; otherwise returns + FALSE. + + A pixmap obtained from a TQIconFactory is considered non-generated. +*/ +bool TQIconSet::isGenerated( Size size, Mode mode, State state ) const +{ + if ( !d ) + return TRUE; + return d->icon( this, size, mode, state )->origin == Generated; +} + +/*! + Clears all cached pixmaps, including those obtained from an + eventual TQIconFactory. +*/ +void TQIconSet::clearGenerated() +{ + if ( !d ) + return; + + for ( int i = 0; i < NumSizes; i++ ) { + for ( int j = 0; j < NumModes; j++ ) { + for ( int k = 0; k < NumStates; k++ ) { + d->icons[i][j][k].clearCached(); + } + } + } +} + +/*! + Installs \a factory as the icon factory for this iconset. The + icon factory is used to generates pixmaps not set by the user. + + If no icon factory is installed, TQIconFactory::defaultFactory() + is used. +*/ +void TQIconSet::installIconFactory( TQIconFactory *factory ) +{ + detach(); + d->setFactory( factory ); +} + +/*! + Returns TRUE if the icon set is empty; otherwise returns FALSE. +*/ +bool TQIconSet::isNull() const +{ + return !d; +} + +/*! + Detaches this icon set from others with which it may share data. + + You will never need to call this function; other TQIconSet functions + call it as necessary. +*/ +void TQIconSet::detach() +{ + if ( !d ) { + d = new TQIconSetPrivate; + return; + } + if ( d->count != 1 ) { + d->deref(); + d = new TQIconSetPrivate( *d ); + } +} + +/*! + Assigns \a other to this icon set and returns a reference to this + icon set. + + \sa detach() +*/ +TQIconSet& TQIconSet::operator=( const TQIconSet& other ) +{ + if ( other.d ) + other.d->ref(); + + if ( d && d->deref() ) + delete d; + d = other.d; + return *this; +} + +/*! + Set the preferred size for all small or large icons that are + generated after this call. If \a which is Small, sets the preferred + size of small generated icons to \a size. Similarly, if \a which is + Large, sets the preferred size of large generated icons to \a size. + + Note that cached icons will not be regenerated, so it is recommended + that you set the preferred icon sizes before generating any icon sets. + Also note that the preferred icon sizes will be ignored for icon sets + that have been created using both small and large pixmaps. + + \sa iconSize() +*/ +void TQIconSet::setIconSize( Size which, const TQSize& size ) +{ + widths[(int) which - 1] = size.width(); + heights[(int) which - 1] = size.height(); +} + +/*! + If \a which is Small, returns the preferred size of a small + generated icon; if \a which is Large, returns the preferred size + of a large generated icon. + + \sa setIconSize() +*/ +const TQSize& TQIconSet::iconSize( Size which ) +{ + // ### replace 'const TQSize&' with TQSize in TQt 4 and simply this code + static TQSize size; + size = TQSize( widths[(int) which - 1], heights[(int) which - 1] ); + return size; +} + +void TQIconSet::normalize( Size& which, const TQSize& pixSize ) +{ + if ( which == Automatic ) + which = pixSize.width() > iconSize( Small ).width() ? Large : Small; +} + +/*! + Returns a new pixmap that is a copy of \a suppliedPix, scaled to + the icon size \a size. +*/ +TQPixmap *TQIconSet::createScaled( Size size, const TQPixmap *suppliedPix ) const +{ + if ( !suppliedPix || suppliedPix->isNull() ) + return 0; + + TQImage img = suppliedPix->convertToImage(); + TQSize imgSize = iconSize( size ); + if ( size == Small ) { + imgSize = imgSize.boundedTo( img.size() ); + } else { + imgSize = imgSize.expandedTo( img.size() ); + } + img = img.smoothScale( imgSize ); + + TQPixmap *pixmap = new TQPixmap( img ); + if ( !pixmap->mask() ) { + TQBitmap mask; + mask.convertFromImage( img.createHeuristicMask(), + TQt::MonoOnly | TQt::ThresholdDither ); + pixmap->setMask( mask ); + } + return pixmap; +} + +/*! + Returns a new pixmap that has a 'disabled' look, taking as its + base the iconset's icon with size \a size and state \a state. +*/ +TQPixmap *TQIconSet::createDisabled( Size size, State state ) const +{ + TQPixmap normalPix = pixmap( size, Normal, state ); + if ( normalPix.isNull() ) + return 0; + + TQImage img; + TQPixmap *pixmap = 0; + TQBitmap normalMask; + if ( normalPix.mask() ) { + normalMask = *normalPix.mask(); + } else { + img = normalPix.convertToImage(); + normalMask.convertFromImage( img.createHeuristicMask(), + TQt::MonoOnly | TQt::ThresholdDither ); + } + + pixmap = new TQPixmap( normalPix.width() + 1, + normalPix.height() + 1 ); + const TQColorGroup &dis = TQApplication::palette().disabled(); + pixmap->fill( dis.background() ); + + TQPainter painter; + painter.begin( pixmap ); + painter.setPen( dis.base() ); + painter.drawPixmap( 1, 1, normalMask ); + painter.setPen( dis.foreground() ); + painter.drawPixmap( 0, 0, normalMask ); + painter.end(); + + if ( !normalMask.mask() ) + normalMask.setMask( normalMask ); + + TQBitmap mask( pixmap->size() ); + mask.fill( TQt::color0 ); + painter.begin( &mask ); + painter.drawPixmap( 0, 0, normalMask ); + painter.drawPixmap( 1, 1, normalMask ); + painter.end(); + pixmap->setMask( mask ); + return pixmap; +} + +/*! \class TQIconFactory + \ingroup advanced + \brief The TQIconFactory class is used to create pixmaps for a TQIconSet. + + By reimplementing createPixmap(), you can override TQIconSet's + default algorithm for computing pixmaps not supplied by the user. + + Call setAutoDelete(TRUE) if you want the factory to automatically + delete itself when it is no longer needed by TQIconSet. + + \sa TQIconSet +*/ + +/*! + Constructs an icon factory. +*/ +TQIconFactory::TQIconFactory() + : autoDel( 0 ) +{ + count = 0; +} + +/*! + Destroys the object and frees any allocated resources. +*/ +TQIconFactory::~TQIconFactory() +{ +} + +/*! + Ceates a pixmap for \a iconSet with a certain \a size, \a mode, and + \a state. Returns 0 if the default TQIconSet algorithm should be + used to create a pixmap that wasn't supplied by the user. + + It is the caller's responsibility to delete the returned pixmap. + + The default implementation always returns 0. +*/ +TQPixmap *TQIconFactory::createPixmap( const TQIconSet& /* iconSet */, + TQIconSet::Size /* size */, + TQIconSet::Mode /* mode */, + TQIconSet::State /* state */ ) +{ + return 0; +} + +/*! + \fn void TQIconFactory::setAutoDelete( bool autoDelete ) + + If \a autoDelete is TRUE, sets the icon factory to automatically + delete itself when it is no longer referenced by any TQIconSet and + isn't the default factory. If \a autoDelete is FALSE (the default) + auto-deletion is disabled. + + \sa autoDelete(), defaultFactory() +*/ + +/*! + \fn bool TQIconFactory::autoDelete() const + + Returns TRUE if auto-deletion is enabled; otherwise returns FALSE. + + \sa setAutoDelete() +*/ + +/*! + Returns the default icon factory. + + \sa installDefaultFactory() +*/ +TQIconFactory *TQIconFactory::defaultFactory() +{ + if ( !defaultFac ) { + defaultFac = new TQIconFactory; + defaultFac->setAutoDelete( TRUE ); + defaultFac->ref(); + q_cleanup_icon_factory.set( &defaultFac ); + } + return defaultFac; +} + +/*! + Replaces the default icon factory with \a factory. +*/ +void TQIconFactory::installDefaultFactory( TQIconFactory *factory ) +{ + if ( !factory ) + return; + + factory->ref(); + if ( defaultFac && defaultFac->deref() && defaultFac->autoDelete() ) + delete defaultFac; + defaultFac = factory; + q_cleanup_icon_factory.set( &defaultFac ); +} + +#endif // QT_NO_ICONSET diff --git a/src/kernel/qiconset.h b/src/kernel/qiconset.h new file mode 100644 index 000000000..db9d6135c --- /dev/null +++ b/src/kernel/qiconset.h @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Definition of TQIconSet class +** +** Created : 980318 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQICONSET_H +#define TQICONSET_H + +#ifndef QT_H +#include "qobject.h" +#include "qpixmap.h" +#endif // QT_H + +#ifndef QT_NO_ICONSET + +class TQIconFactory; +class TQIconSetPrivate; + +// ### Remove all 'virtual' functions in TQIconSet (but not TQIconFactory) in TQt 4.0 +class Q_EXPORT TQIconSet +{ +public: + // the implementation makes assumptions about the value of these + enum Size { Automatic, Small, Large }; + enum Mode { Normal, Disabled, Active }; + enum State { On, Off }; + + TQIconSet(); + TQIconSet( const TQPixmap& pixmap, Size size = Automatic ); + TQIconSet( const TQPixmap& smallPix, const TQPixmap& largePix ); + TQIconSet( const TQIconSet& other ); + virtual ~TQIconSet(); + + void reset( const TQPixmap& pixmap, Size size ); + + virtual void setPixmap( const TQPixmap& pixmap, Size size, + Mode mode = Normal, State state = Off ); + virtual void setPixmap( const TQString& fileName, Size size, + Mode mode = Normal, State state = Off ); + TQPixmap pixmap( Size size, Mode mode, State state = Off ) const; + TQPixmap pixmap( Size size, bool enabled, State state = Off ) const; + TQPixmap pixmap() const; + bool isGenerated( Size size, Mode mode, State state = Off ) const; + void clearGenerated(); + void installIconFactory( TQIconFactory *factory ); + + bool isNull() const; + + void detach(); + + TQIconSet& operator=( const TQIconSet& other ); + + // static functions + static void setIconSize( Size which, const TQSize& size ); + static const TQSize& iconSize( Size which ); + +#ifndef Q_QDOC + Q_DUMMY_COMPARISON_OPERATOR(TQIconSet) +#endif + +private: + void normalize( Size& which, const TQSize& pixSize ); + TQPixmap *createScaled( Size size, const TQPixmap *suppliedPix ) const; + TQPixmap *createDisabled( Size size, State state ) const; + + TQIconSetPrivate *d; +}; + +class Q_EXPORT TQIconFactory : private TQShared +{ +public: + TQIconFactory(); + virtual ~TQIconFactory(); + + virtual TQPixmap *createPixmap( const TQIconSet& iconSet, TQIconSet::Size size, + TQIconSet::Mode mode, TQIconSet::State state ); + void setAutoDelete( bool autoDelete ) { autoDel = autoDelete; } + bool autoDelete() const { return autoDel; } + + static TQIconFactory *defaultFactory(); + static void installDefaultFactory( TQIconFactory *factory ); + +private: +#if defined(Q_DISABLE_COPY) + TQIconFactory( const TQIconFactory & ); + TQIconFactory &operator=( const TQIconFactory & ); +#endif + + friend class TQIconSet; + friend class TQIconSetPrivate; + + uint autoDel : 1; + uint unused : 31; +}; + +#endif // QT_NO_ICONSET +#endif diff --git a/src/kernel/qimage.cpp b/src/kernel/qimage.cpp new file mode 100644 index 000000000..f26f24c00 --- /dev/null +++ b/src/kernel/qimage.cpp @@ -0,0 +1,6497 @@ +/**************************************************************************** +** +** Implementation of TQImage and TQImageIO classes +** +** Created : 950207 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qimage.h" +#include "qregexp.h" +#include "qfile.h" +#include "qdatastream.h" +#include "qtextstream.h" +#include "qbuffer.h" +#include "qptrlist.h" +#include "qasyncimageio.h" +#include "qpngio.h" +#include "qmngio.h" +#include "qjpegio.h" +#include "qmap.h" +#include +#include "qimageformatinterface_p.h" +#include "qwmatrix.h" +#include "qapplication.h" +#include "qmime.h" +#include "qdragobject.h" +#include +#include + +#ifdef Q_WS_QWS +#include "qgfx_qws.h" +#endif + +// 16bpp images on supported on TQt/Embedded +#if !defined( Q_WS_QWS ) && !defined(QT_NO_IMAGE_16_BIT) +#define QT_NO_IMAGE_16_BIT +#endif + + +/*! + \class TQImage + \brief The TQImage class provides a hardware-independent pixmap + representation with direct access to the pixel data. + + \ingroup images + \ingroup graphics + \ingroup shared + \mainclass + + It is one of the two classes TQt provides for dealing with images, + the other being TQPixmap. TQImage is designed and optimized for I/O + and for direct pixel access/manipulation. TQPixmap is designed and + optimized for drawing. There are (slow) functions to convert + between TQImage and TQPixmap: TQPixmap::convertToImage() and + TQPixmap::convertFromImage(). + + An image has the parameters \link width() width\endlink, \link + height() height\endlink and \link depth() depth\endlink (bits per + pixel, bpp), a color table and the actual \link bits() + pixels\endlink. TQImage supports 1-bpp, 8-bpp and 32-bpp image + data. 1-bpp and 8-bpp images use a color lookup table; the pixel + value is a color table index. + + 32-bpp images encode an RGB value in 24 bits and ignore the color + table. The most significant byte is used for the \link + setAlphaBuffer() alpha buffer\endlink. + + An entry in the color table is an RGB triplet encoded as a \c + uint. Use the \link ::qRed() qRed()\endlink, \link ::qGreen() + qGreen()\endlink and \link ::qBlue() qBlue()\endlink functions (\c + qcolor.h) to access the components, and \link ::qRgb() + qRgb\endlink to make an RGB triplet (see the TQColor class + documentation). + + 1-bpp (monochrome) images have a color table with a most two + colors. There are two different formats: big endian (MSB first) or + little endian (LSB first) bit order. To access a single bit you + will must do some bit shifts: + + \code + TQImage image; + // sets bit at (x,y) to 1 + if ( image.bitOrder() == TQImage::LittleEndian ) + *(image.scanLine(y) + (x >> 3)) |= 1 << (x & 7); + else + *(image.scanLine(y) + (x >> 3)) |= 1 << (7 - (x & 7)); + \endcode + + If this looks complicated, it might be a good idea to convert the + 1-bpp image to an 8-bpp image using convertDepth(). + + 8-bpp images are much easier to work with than 1-bpp images + because they have a single byte per pixel: + + \code + TQImage image; + // set entry 19 in the color table to yellow + image.setColor( 19, qRgb(255,255,0) ); + // set 8 bit pixel at (x,y) to value yellow (in color table) + *(image.scanLine(y) + x) = 19; + \endcode + + 32-bpp images ignore the color table; instead, each pixel contains + the RGB triplet. 24 bits contain the RGB value; the most + significant byte is reserved for the alpha buffer. + + \code + TQImage image; + // sets 32 bit pixel at (x,y) to yellow. + uint *p = (uint *)image.scanLine(y) + x; + *p = qRgb(255,255,0); + \endcode + + On TQt/Embedded, scanlines are aligned to the pixel depth and may + be padded to any degree, while on all other platforms, the + scanlines are 32-bit aligned for all depths. The constructor + taking a \c{uchar*} argument always expects 32-bit aligned data. + On TQt/Embedded, an additional constructor allows the number of + bytes-per-line to be specified. + + TQImage supports a variety of methods for getting information about + the image, for example, colorTable(), allGray(), isGrayscale(), + bitOrder(), bytesPerLine(), depth(), dotsPerMeterX() and + dotsPerMeterY(), hasAlphaBuffer(), numBytes(), numColors(), and + width() and height(). + + Pixel colors are retrieved with pixel() and set with setPixel(). + + TQImage also supports a number of functions for creating a new + image that is a transformed version of the original. For example, + copy(), convertBitOrder(), convertDepth(), createAlphaMask(), + createHeuristicMask(), mirror(), scale(), smoothScale(), swapRGB() + and xForm(). There are also functions for changing attributes of + an image in-place, for example, setAlphaBuffer(), setColor(), + setDotsPerMeterX() and setDotsPerMeterY() and setNumColors(). + + Images can be loaded and saved in the supported formats. Images + are saved to a file with save(). Images are loaded from a file + with load() (or in the constructor) or from an array of data with + loadFromData(). The lists of supported formats are available from + inputFormatList() and outputFormatList(). + + Strings of text may be added to images using setText(). + + The TQImage class uses explicit \link shclass.html sharing\endlink, + similar to that used by TQMemArray. + + New image formats can be added as \link plugins-howto.html + plugins\endlink. + + \sa TQImageIO TQPixmap \link shclass.html Shared Classes\endlink +*/ + + +/*! + \enum TQImage::Endian + + This enum type is used to describe the endianness of the CPU and + graphics hardware. + + \value IgnoreEndian Endianness does not matter. Useful for some + operations that are independent of endianness. + \value BigEndian Network byte order, as on SPARC and Motorola CPUs. + \value LittleEndian PC/Alpha byte order. +*/ + +/*! + \enum TQt::ImageConversionFlags + + The conversion flag is a bitwise-OR of the following values. The + options marked "(default)" are set if no other values from the + list are included (since the defaults are zero): + + Color/Mono preference (ignored for TQBitmap) + \value AutoColor (default) - If the image has \link + TQImage::depth() depth\endlink 1 and contains only + black and white pixels, the pixmap becomes monochrome. + \value ColorOnly The pixmap is dithered/converted to the + \link TQPixmap::defaultDepth() native display depth\endlink. + \value MonoOnly The pixmap becomes monochrome. If necessary, + it is dithered using the chosen dithering algorithm. + + Dithering mode preference for RGB channels + \value DiffuseDither (default) - A high-quality dither. + \value OrderedDither A faster, more ordered dither. + \value ThresholdDither No dithering; closest color is used. + + Dithering mode preference for alpha channel + \value ThresholdAlphaDither (default) - No dithering. + \value OrderedAlphaDither A faster, more ordered dither. + \value DiffuseAlphaDither A high-quality dither. + \value NoAlpha Not supported. + + Color matching versus dithering preference + \value PreferDither (default when converting to a pixmap) - Always dither + 32-bit images when the image is converted to 8 bits. + \value AvoidDither (default when converting for the purpose of saving to + file) - Dither 32-bit images only if the image has more than 256 + colors and it is being converted to 8 bits. + \value AutoDither Not supported. + + The following are not values that are used directly, but masks for + the above classes: + \value ColorMode_Mask Mask for the color mode. + \value Dither_Mask Mask for the dithering mode for RGB channels. + \value AlphaDither_Mask Mask for the dithering mode for the alpha channel. + \value DitherMode_Mask Mask for the mode that determines the preference of + color matching versus dithering. + + Using 0 as the conversion flag sets all the default options. +*/ + +#if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0 >= 50190001) +#pragma message disable narrowptr +#endif + +#ifndef QT_NO_IMAGE_TEXT +class TQImageDataMisc { +public: + TQImageDataMisc() { } + TQImageDataMisc( const TQImageDataMisc& o ) : + text_lang(o.text_lang) { } + + TQImageDataMisc& operator=(const TQImageDataMisc& o) + { + text_lang = o.text_lang; + return *this; + } + TQValueList list() + { + return text_lang.keys(); + } + + TQStringList languages() + { + TQStringList r; + TQMap::Iterator it = text_lang.begin(); + for ( ; it != text_lang.end(); ++it ) { + r.remove( it.key().lang ); + r.append( it.key().lang ); + } + return r; + } + TQStringList keys() + { + TQStringList r; + TQMap::Iterator it = text_lang.begin(); + for ( ; it != text_lang.end(); ++it ) { + r.remove( it.key().key ); + r.append( it.key().key ); + } + return r; + } + + TQMap text_lang; +}; +#endif // QT_NO_IMAGE_TEXT + + + +/***************************************************************************** + TQImage member functions + *****************************************************************************/ + +// table to flip bits +static const uchar bitflip[256] = { + /* + open OUT, "| fmt"; + for $i (0..255) { + print OUT (($i >> 7) & 0x01) | (($i >> 5) & 0x02) | + (($i >> 3) & 0x04) | (($i >> 1) & 0x08) | + (($i << 7) & 0x80) | (($i << 5) & 0x40) | + (($i << 3) & 0x20) | (($i << 1) & 0x10), ", "; + } + close OUT; + */ + 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, + 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, + 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, + 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, + 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, + 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, + 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, + 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, + 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, + 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, + 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, + 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, + 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, + 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, + 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, + 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255 +}; + +const uchar *qt_get_bitflip_array() // called from TQPixmap code +{ + return bitflip; +} + + +/*! + Constructs a null image. + + \sa isNull() +*/ + +TQImage::TQImage() +{ + init(); +} + +/*! + Constructs an image with \a w width, \a h height, \a depth bits + per pixel, \a numColors colors and bit order \a bitOrder. + + Using this constructor is the same as first constructing a null + image and then calling the create() function. + + \sa create() +*/ + +TQImage::TQImage( int w, int h, int depth, int numColors, Endian bitOrder ) +{ + init(); + create( w, h, depth, numColors, bitOrder ); +} + +/*! + Constructs an image with size \a size pixels, depth \a depth bits, + \a numColors and \a bitOrder endianness. + + Using this constructor is the same as first constructing a null + image and then calling the create() function. + + \sa create() +*/ +TQImage::TQImage( const TQSize& size, int depth, int numColors, Endian bitOrder ) +{ + init(); + create( size, depth, numColors, bitOrder ); +} + +#ifndef QT_NO_IMAGEIO +/*! + Constructs an image and tries to load the image from the file \a + fileName. + + If \a format is specified, the loader attempts to read the image + using the specified format. If \a format is not specified (which + is the default), the loader reads a few bytes from the header to + guess the file format. + + If the loading of the image failed, this object is a \link + isNull() null\endlink image. + + The TQImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa load() isNull() TQImageIO +*/ + +TQImage::TQImage( const TQString &fileName, const char* format ) +{ + init(); + load( fileName, format ); +} + +#ifndef QT_NO_IMAGEIO_XPM +// helper +static void read_xpm_image_or_array( TQImageIO *, const char * const *, TQImage & ); +#endif +/*! + Constructs an image from \a xpm, which must be a valid XPM image. + + Errors are silently ignored. + + Note that it's possible to squeeze the XPM variable a little bit + by using an unusual declaration: + + \code + static const char * const start_xpm[]={ + "16 15 8 1", + "a c #cec6bd", + .... + \endcode + + The extra \c const makes the entire definition read-only, which is + slightly more efficient (e.g. when the code is in a shared + library) and ROMable when the application is to be stored in ROM. +*/ + +TQImage::TQImage( const char * const xpm[] ) +{ + init(); +#ifndef QT_NO_IMAGEIO_XPM + read_xpm_image_or_array( 0, xpm, *this ); +#else + // We use a qFatal rather than disabling the whole function, as this + // constructor may be ambiguous. + qFatal("XPM not supported"); +#endif +} + +/*! + Constructs an image from the binary data \a array. It tries to + guess the file format. + + If the loading of the image failed, this object is a \link + isNull() null\endlink image. + + \sa loadFromData() isNull() imageFormat() +*/ +TQImage::TQImage( const TQByteArray &array ) +{ + init(); + loadFromData(array); +} +#endif //QT_NO_IMAGEIO + + +/*! + Constructs a \link shclass.html shallow copy\endlink of \a image. +*/ + +TQImage::TQImage( const TQImage &image ) +{ + data = image.data; + data->ref(); +} + +/*! + Constructs an image \a w pixels wide, \a h pixels high with a + color depth of \a depth, that uses an existing memory buffer, \a + yourdata. The buffer must remain valid throughout the life of the + TQImage. The image does not delete the buffer at destruction. + + If \a colortable is 0, a color table sufficient for \a numColors + will be allocated (and destructed later). + + Note that \a yourdata must be 32-bit aligned. + + The endianness is given in \a bitOrder. +*/ +TQImage::TQImage( uchar* yourdata, int w, int h, int depth, + TQRgb* colortable, int numColors, + Endian bitOrder ) +{ + init(); + int bpl = ((w*depth+31)/32)*4; // bytes per scanline + if ( w <= 0 || h <= 0 || depth <= 0 || numColors < 0 + || INT_MAX / sizeof(uchar *) < uint(h) + || INT_MAX / uint(depth) < uint(w) + || bpl <= 0 + || INT_MAX / uint(bpl) < uint(h) ) + return; // invalid parameter(s) + data->w = w; + data->h = h; + data->d = depth; + data->ncols = depth != 32 ? numColors : 0; + if ( !yourdata ) + return; // Image header info can be saved without needing to allocate memory. + data->nbytes = bpl*h; + if ( colortable || !data->ncols ) { + data->ctbl = colortable; + data->ctbl_mine = FALSE; + } else { + // calloc since we realloc, etc. later (ick) + data->ctbl = (TQRgb*)calloc( data->ncols*sizeof(TQRgb), data->ncols ); + Q_CHECK_PTR(data->ctbl); + data->ctbl_mine = TRUE; + } + uchar** jt = (uchar**)malloc(h*sizeof(uchar*)); + Q_CHECK_PTR(jt); + for (int j=0; jbits = jt; + data->bitordr = bitOrder; +} + +#ifdef Q_WS_QWS + +/*! + Constructs an image that uses an existing memory buffer. The + buffer must remain valid for the life of the TQImage. The image + does not delete the buffer at destruction. The buffer is passed as + \a yourdata. The image's width is \a w and its height is \a h. The + color depth is \a depth. \a bpl specifies the number of bytes per + line. + + If \a colortable is 0, a color table sufficient for \a numColors + will be allocated (and destructed later). + + The endianness is specified by \a bitOrder. + + \warning This constructor is only available on TQt/Embedded. +*/ +TQImage::TQImage( uchar* yourdata, int w, int h, int depth, + int bpl, TQRgb* colortable, int numColors, + Endian bitOrder ) +{ + init(); + if ( !yourdata || w <= 0 || h <= 0 || depth <= 0 || numColors < 0 + || INT_MAX / sizeof(uchar *) < uint(h) + || INT_MAX / uint(bpl) < uint(h) + ) + return; // invalid parameter(s) + data->w = w; + data->h = h; + data->d = depth; + data->ncols = numColors; + data->nbytes = bpl * h; + if ( colortable || !numColors ) { + data->ctbl = colortable; + data->ctbl_mine = FALSE; + } else { + // calloc since we realloc, etc. later (ick) + data->ctbl = (TQRgb*)calloc( numColors*sizeof(TQRgb), numColors ); + Q_CHECK_PTR(data->ctbl); + data->ctbl_mine = TRUE; + } + uchar** jt = (uchar**)malloc(h*sizeof(uchar*)); + Q_CHECK_PTR(jt); + for (int j=0; jbits = jt; + data->bitordr = bitOrder; +} +#endif // Q_WS_QWS + +/*! + Destroys the image and cleans up. +*/ + +TQImage::~TQImage() +{ + if ( data && data->deref() ) { + reset(); + delete data; + } +} + + + + +/*! Convenience function. Gets the data associated with the absolute + name \a abs_name from the default mime source factory and decodes it + to an image. + + \sa TQMimeSourceFactory, TQImage::fromMimeSource(), TQImageDrag::decode() +*/ +#ifndef QT_NO_MIME +TQImage TQImage::fromMimeSource( const TQString &abs_name ) +{ + const TQMimeSource *m = TQMimeSourceFactory::defaultFactory()->data( abs_name ); + if ( !m ) { +#if defined(QT_CHECK_STATE) + qWarning("TQImage::fromMimeSource: Cannot find image \"%s\" in the mime source factory", abs_name.latin1() ); +#endif + return TQImage(); + } + TQImage img; + TQImageDrag::decode( m, img ); + return img; +} +#endif + + +/*! + Assigns a \link shclass.html shallow copy\endlink of \a image to + this image and returns a reference to this image. + + \sa copy() +*/ + +TQImage &TQImage::operator=( const TQImage &image ) +{ + image.data->ref(); // avoid 'x = x' + if ( data->deref() ) { + reset(); + delete data; + } + data = image.data; + return *this; +} + +/*! + \overload + + Sets the image bits to the \a pixmap contents and returns a + reference to the image. + + If the image shares data with other images, it will first + dereference the shared data. + + Makes a call to TQPixmap::convertToImage(). +*/ + +TQImage &TQImage::operator=( const TQPixmap &pixmap ) +{ + *this = pixmap.convertToImage(); + return *this; +} + +/*! + Detaches from shared image data and makes sure that this image is + the only one referring to the data. + + If multiple images share common data, this image makes a copy of + the data and detaches itself from the sharing mechanism. + Nothing is done if there is just a single reference. + + \sa copy() +*/ + +void TQImage::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + +/*! + Returns a \link shclass.html deep copy\endlink of the image. + + \sa detach() +*/ + +TQImage TQImage::copy() const +{ + if ( isNull() ) { + // maintain the fields of invalid TQImages when copied + return TQImage( 0, width(), height(), depth(), colorTable(), numColors(), bitOrder() ); + } else { + TQImage image; + image.create( width(), height(), depth(), numColors(), bitOrder() ); +#ifdef Q_WS_QWS + // TQt/Embedded can create images with non-default bpl + // make sure we don't crash. + if ( image.numBytes() != numBytes() ) + for ( int i = 0; i < height(); i++ ) + memcpy( image.scanLine(i), scanLine(i), image.bytesPerLine() ); + else +#endif + memcpy( image.bits(), bits(), numBytes() ); + memcpy( image.colorTable(), colorTable(), numColors() * sizeof(TQRgb) ); + image.setAlphaBuffer( hasAlphaBuffer() ); + image.data->dpmx = dotsPerMeterX(); + image.data->dpmy = dotsPerMeterY(); + image.data->offset = offset(); +#ifndef QT_NO_IMAGE_TEXT + if ( data->misc ) { + image.data->misc = new TQImageDataMisc; + *image.data->misc = misc(); + } +#endif + return image; + } +} + +/*! + \overload + + Returns a \link shclass.html deep copy\endlink of a sub-area of + the image. + + The returned image is always \a w by \a h pixels in size, and is + copied from position \a x, \a y in this image. In areas beyond + this image pixels are filled with pixel 0. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + \sa bitBlt() TQt::ImageConversionFlags +*/ + +TQImage TQImage::copy(int x, int y, int w, int h, int conversion_flags) const +{ + int dx = 0; + int dy = 0; + if ( w <= 0 || h <= 0 ) return TQImage(); // Nothing to copy + + TQImage image( w, h, depth(), numColors(), bitOrder() ); + + if ( x < 0 || y < 0 || x + w > width() || y + h > height() ) { + // bitBlt will not cover entire image - clear it. + // ### should deal with each side separately for efficiency + image.fill(0); + if ( x < 0 ) { + dx = -x; + x = 0; + } + if ( y < 0 ) { + dy = -y; + y = 0; + } + } + + bool has_alpha = hasAlphaBuffer(); + if ( has_alpha ) { + // alpha channel should be only copied, not used by bitBlt(), and + // this is mutable, we will restore the image state before returning + TQImage *that = (TQImage *) this; + that->setAlphaBuffer( FALSE ); + } + memcpy( image.colorTable(), colorTable(), numColors()*sizeof(TQRgb) ); + bitBlt( &image, dx, dy, this, x, y, -1, -1, conversion_flags ); + if ( has_alpha ) { + // restore image state + TQImage *that = (TQImage *) this; + that->setAlphaBuffer( TRUE ); + } + image.setAlphaBuffer(hasAlphaBuffer()); + image.data->dpmx = dotsPerMeterX(); + image.data->dpmy = dotsPerMeterY(); + image.data->offset = offset(); +#ifndef QT_NO_IMAGE_TEXT + if ( data->misc ) { + image.data->misc = new TQImageDataMisc; + *image.data->misc = misc(); + } +#endif + return image; +} + +/*! + \overload TQImage TQImage::copy(const TQRect& r) const + + Returns a \link shclass.html deep copy\endlink of a sub-area of + the image. + + The returned image always has the size of the rectangle \a r. In + areas beyond this image pixels are filled with pixel 0. +*/ + +/*! + \fn bool TQImage::isNull() const + + Returns TRUE if it is a null image; otherwise returns FALSE. + + A null image has all parameters set to zero and no allocated data. +*/ + + +/*! + \fn int TQImage::width() const + + Returns the width of the image. + + \sa height() size() rect() +*/ + +/*! + \fn int TQImage::height() const + + Returns the height of the image. + + \sa width() size() rect() +*/ + +/*! + \fn TQSize TQImage::size() const + + Returns the size of the image, i.e. its width and height. + + \sa width() height() rect() +*/ + +/*! + \fn TQRect TQImage::rect() const + + Returns the enclosing rectangle (0, 0, width(), height()) of the + image. + + \sa width() height() size() +*/ + +/*! + \fn int TQImage::depth() const + + Returns the depth of the image. + + The image depth is the number of bits used to encode a single + pixel, also called bits per pixel (bpp) or bit planes of an image. + + The supported depths are 1, 8, 16 (TQt/Embedded only) and 32. + + \sa convertDepth() +*/ + +/*! + \fn int TQImage::numColors() const + + Returns the size of the color table for the image. + + Notice that numColors() returns 0 for 16-bpp (TQt/Embedded only) + and 32-bpp images because these images do not use color tables, + but instead encode pixel values as RGB triplets. + + \sa setNumColors() colorTable() +*/ + +/*! + \fn TQImage::Endian TQImage::bitOrder() const + + Returns the bit order for the image. + + If it is a 1-bpp image, this function returns either + TQImage::BigEndian or TQImage::LittleEndian. + + If it is not a 1-bpp image, this function returns + TQImage::IgnoreEndian. + + \sa depth() +*/ + +/*! + \fn uchar **TQImage::jumpTable() const + + Returns a pointer to the scanline pointer table. + + This is the beginning of the data block for the image. + + \sa bits() scanLine() +*/ + +/*! + \fn TQRgb *TQImage::colorTable() const + + Returns a pointer to the color table. + + \sa numColors() +*/ + +/*! + \fn int TQImage::numBytes() const + + Returns the number of bytes occupied by the image data. + + \sa bytesPerLine() bits() +*/ + +/*! + \fn int TQImage::bytesPerLine() const + + Returns the number of bytes per image scanline. This is equivalent + to numBytes()/height(). + + \sa numBytes() scanLine() +*/ + +/*! + \fn TQRgb TQImage::color( int i ) const + + Returns the color in the color table at index \a i. The first + color is at index 0. + + A color value is an RGB triplet. Use the \link ::qRed() + qRed()\endlink, \link ::qGreen() qGreen()\endlink and \link + ::qBlue() qBlue()\endlink functions (defined in \c qcolor.h) to + get the color value components. + + \sa setColor() numColors() TQColor +*/ + +/*! + \fn void TQImage::setColor( int i, TQRgb c ) + + Sets a color in the color table at index \a i to \a c. + + A color value is an RGB triplet. Use the \link ::qRgb() + qRgb()\endlink function (defined in \c qcolor.h) to make RGB + triplets. + + \sa color() setNumColors() numColors() +*/ + +/*! + \fn uchar *TQImage::scanLine( int i ) const + + Returns a pointer to the pixel data at the scanline with index \a + i. The first scanline is at index 0. + + The scanline data is aligned on a 32-bit boundary. + + \warning If you are accessing 32-bpp image data, cast the returned + pointer to \c{TQRgb*} (TQRgb has a 32-bit size) and use it to + read/write the pixel value. You cannot use the \c{uchar*} pointer + directly, because the pixel format depends on the byte order on + the underlying platform. Hint: use \link ::qRed() qRed()\endlink, + \link ::qGreen() qGreen()\endlink and \link ::qBlue() + qBlue()\endlink, etc. (qcolor.h) to access the pixels. + + \warning If you are accessing 16-bpp image data, you must handle + endianness yourself. (TQt/Embedded only) + + \sa bytesPerLine() bits() jumpTable() +*/ + +/*! + \fn uchar *TQImage::bits() const + + Returns a pointer to the first pixel data. This is equivalent to + scanLine(0). + + \sa numBytes() scanLine() jumpTable() +*/ + + + +void TQImage::warningIndexRange( const char *func, int i ) +{ +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::%s: Index %d out of range", func, i ); +#else + Q_UNUSED( func ) + Q_UNUSED( i ) +#endif +} + + +/*! + Resets all image parameters and deallocates the image data. +*/ + +void TQImage::reset() +{ + freeBits(); + setNumColors( 0 ); +#ifndef QT_NO_IMAGE_TEXT + delete data->misc; +#endif + reinit(); +} + + +/*! + Fills the entire image with the pixel value \a pixel. + + If the \link depth() depth\endlink of this image is 1, only the + lowest bit is used. If you say fill(0), fill(2), etc., the image + is filled with 0s. If you say fill(1), fill(3), etc., the image is + filled with 1s. If the depth is 8, the lowest 8 bits are used. + + If the depth is 32 and the image has no alpha buffer, the \a pixel + value is written to each pixel in the image. If the image has an + alpha buffer, only the 24 RGB bits are set and the upper 8 bits + (alpha value) are left unchanged. + + Note: TQImage::pixel() returns the color of the pixel at the given + coordinates; TQColor::pixel() returns the pixel value of the + underlying window system (essentially an index value), so normally + you will want to use TQImage::pixel() to use a color from an + existing image or TQColor::rgb() to use a specific color. + + \sa invertPixels() depth() hasAlphaBuffer() create() +*/ + +void TQImage::fill( uint pixel ) +{ + if ( depth() == 1 || depth() == 8 ) { + if ( depth() == 1 ) { + if ( pixel & 1 ) + pixel = 0xffffffff; + else + pixel = 0; + } else { + uint c = pixel & 0xff; + pixel = c | ((c << 8) & 0xff00) | ((c << 16) & 0xff0000) | + ((c << 24) & 0xff000000); + } + int bpl = bytesPerLine(); + for ( int i=0; i // needed for systemBitOrder +#include +#include +#if defined(Q_OS_WIN32) +#undef open +#undef close +#undef read +#undef write +#endif +#endif + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +/*! + Determines the bit order of the display hardware. Returns + TQImage::LittleEndian (LSB first) or TQImage::BigEndian (MSB first). + + \sa systemByteOrder() +*/ + +TQImage::Endian TQImage::systemBitOrder() +{ +#if defined(Q_WS_X11) + return BitmapBitOrder(qt_xdisplay()) == MSBFirst ? BigEndian :LittleEndian; +#else + return BigEndian; +#endif +} + + +/*! + Resizes the color table to \a numColors colors. + + If the color table is expanded all the extra colors will be set to + black (RGB 0,0,0). + + \sa numColors() color() setColor() colorTable() +*/ + +void TQImage::setNumColors( int numColors ) +{ + if ( numColors == data->ncols ) + return; + if ( numColors == 0 ) { // use no color table + if ( data->ctbl ) { + if ( data->ctbl_mine ) + free( data->ctbl ); + else + data->ctbl_mine = TRUE; + data->ctbl = 0; + } + data->ncols = 0; + return; + } + if ( data->ctbl && data->ctbl_mine ) { // already has color table + data->ctbl = (TQRgb*)realloc( data->ctbl, numColors*sizeof(TQRgb) ); + if ( data->ctbl && numColors > data->ncols ) + memset( (char *)&data->ctbl[data->ncols], 0, + (numColors-data->ncols)*sizeof(TQRgb) ); + } else { // create new color table + data->ctbl = (TQRgb*)calloc( numColors*sizeof(TQRgb), 1 ); + Q_CHECK_PTR(data->ctbl); + data->ctbl_mine = TRUE; + } + data->ncols = data->ctbl == 0 ? 0 : numColors; +} + + +/*! + \fn bool TQImage::hasAlphaBuffer() const + + Returns TRUE if alpha buffer mode is enabled; otherwise returns + FALSE. + + \sa setAlphaBuffer() +*/ + +/*! + Enables alpha buffer mode if \a enable is TRUE, otherwise disables + it. The default setting is disabled. + + An 8-bpp image has 8-bit pixels. A pixel is an index into the + \link color() color table\endlink, which contains 32-bit color + values. In a 32-bpp image, the 32-bit pixels are the color values. + + This 32-bit value is encoded as follows: The lower 24 bits are + used for the red, green, and blue components. The upper 8 bits + contain the alpha component. + + The alpha component specifies the transparency of a pixel. 0 means + completely transparent and 255 means opaque. The alpha component + is ignored if you do not enable alpha buffer mode. + + The alpha buffer is used to set a mask when a TQImage is translated + to a TQPixmap. + + \sa hasAlphaBuffer() createAlphaMask() +*/ + +void TQImage::setAlphaBuffer( bool enable ) +{ + data->alpha = enable; +} + + +/*! + Sets the image \a width, \a height, \a depth, its number of colors + (in \a numColors), and bit order. Returns TRUE if successful, or + FALSE if the parameters are incorrect or if memory cannot be + allocated. + + The \a width and \a height is limited to 32767. \a depth must be + 1, 8, or 32. If \a depth is 1, \a bitOrder must be set to + either TQImage::LittleEndian or TQImage::BigEndian. For other depths + \a bitOrder must be TQImage::IgnoreEndian. + + This function allocates a color table and a buffer for the image + data. The image data is not initialized. + + The image buffer is allocated as a single block that consists of a + table of \link scanLine() scanline\endlink pointers (jumpTable()) + and the image data (bits()). + + \sa fill() width() height() depth() numColors() bitOrder() + jumpTable() scanLine() bits() bytesPerLine() numBytes() +*/ + +bool TQImage::create( int width, int height, int depth, int numColors, + Endian bitOrder ) +{ + reset(); // reset old data + if ( width <= 0 || height <= 0 || depth <= 0 || numColors < 0 ) + return FALSE; // invalid parameter(s) + if ( depth == 1 && bitOrder == IgnoreEndian ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::create: Bit order is retquired for 1 bpp images" ); +#endif + return FALSE; + } + if ( depth != 1 ) + bitOrder = IgnoreEndian; + +#if defined(QT_CHECK_RANGE) + if ( depth == 24 ) + qWarning( "TQImage::create: 24-bpp images no longer supported, " + "use 32-bpp instead" ); +#endif + switch ( depth ) { + case 1: + case 8: +#ifndef QT_NO_IMAGE_16_BIT + case 16: +#endif +#ifndef QT_NO_IMAGE_TRUECOLOR + case 32: +#endif + break; + default: // invalid depth + return FALSE; + } + + if ( depth == 32 ) + numColors = 0; + setNumColors( numColors ); + if ( data->ncols != numColors ) // could not alloc color table + return FALSE; + + if ( INT_MAX / uint(depth) < uint(width) ) { // sanity check for potential overflow + setNumColors( 0 ); + return FALSE; + } +// TQt/Embedded doesn't waste memory on unnecessary padding. +#ifdef Q_WS_QWS + const int bpl = (width*depth+7)/8; // bytes per scanline + const int pad = 0; +#else + const int bpl = ((width*depth+31)/32)*4; // bytes per scanline + // #### WWA: shouldn't this be (width*depth+7)/8: + const int pad = bpl - (width*depth)/8; // pad with zeros +#endif + if ( INT_MAX / uint(bpl) < uint(height) + || bpl < 0 + || INT_MAX / sizeof(uchar *) < uint(height) ) { // sanity check for potential overflow + setNumColors( 0 ); + return FALSE; + } + int nbytes = bpl*height; // image size + int ptbl = height*sizeof(uchar*); // pointer table size + int size = nbytes + ptbl; // total size of data block + uchar **p = (uchar **)malloc( size ); // alloc image bits + Q_CHECK_PTR(p); + if ( !p ) { // no memory + setNumColors( 0 ); + return FALSE; + } + data->w = width; + data->h = height; + data->d = depth; + data->nbytes = nbytes; + data->bitordr = bitOrder; + data->bits = p; // set image pointer + //uchar *d = (uchar*)p + ptbl; // setup scanline pointers + uchar *d = (uchar*)(p + height); // setup scanline pointers + while ( height-- ) { + *p++ = d; + if ( pad ) + memset( d+bpl-pad, 0, pad ); + d += bpl; + } + return TRUE; +} + +/*! + \overload bool TQImage::create( const TQSize&, int depth, int numColors, Endian bitOrder ) +*/ +bool TQImage::create( const TQSize& size, int depth, int numColors, + TQImage::Endian bitOrder ) +{ + return create(size.width(), size.height(), depth, numColors, bitOrder); +} + +/*! + \internal + Initializes the image data structure. +*/ + +void TQImage::init() +{ + data = new TQImageData; + Q_CHECK_PTR( data ); + reinit(); +} + +void TQImage::reinit() +{ + data->w = data->h = data->d = data->ncols = 0; + data->nbytes = 0; + data->ctbl = 0; + data->bits = 0; + data->bitordr = TQImage::IgnoreEndian; + data->alpha = FALSE; +#ifndef QT_NO_IMAGE_TEXT + data->misc = 0; +#endif + data->dpmx = 0; + data->dpmy = 0; + data->offset = TQPoint(0,0); +} + +/*! + \internal + Deallocates the image data and sets the bits pointer to 0. +*/ + +void TQImage::freeBits() +{ + if ( data->bits ) { // dealloc image bits + free( data->bits ); + data->bits = 0; + } +} + + +/***************************************************************************** + Internal routines for converting image depth. + *****************************************************************************/ + +// +// convert_32_to_8: Converts a 32 bits depth (true color) to an 8 bit +// image with a colormap. If the 32 bit image has more than 256 colors, +// we convert the red,green and blue bytes into a single byte encoded +// as 6 shades of each of red, green and blue. +// +// if dithering is needed, only 1 color at most is available for alpha. +// +#ifndef QT_NO_IMAGE_TRUECOLOR +struct TQRgbMap { + TQRgbMap() : rgb(0xffffffff) { } + bool used() const { return rgb!=0xffffffff; } + uchar pix; + TQRgb rgb; +}; + +static bool convert_32_to_8( const TQImage *src, TQImage *dst, int conversion_flags, TQRgb* palette=0, int palette_count=0 ) +{ + register TQRgb *p; + uchar *b; + bool do_quant = FALSE; + int y, x; + + if ( !dst->create(src->width(), src->height(), 8, 256) ) + return FALSE; + + const int tablesize = 997; // prime + TQRgbMap table[tablesize]; + int pix=0; + TQRgb amask = src->hasAlphaBuffer() ? 0xffffffff : 0x00ffffff; + if ( src->hasAlphaBuffer() ) + dst->setAlphaBuffer(TRUE); + + if ( palette ) { + // Preload palette into table. + + p = palette; + // Almost same code as pixel insertion below + while ( palette_count-- > 0 ) { + // Find in table... + int hash = (*p & amask) % tablesize; + for (;;) { + if ( table[hash].used() ) { + if ( table[hash].rgb == (*p & amask) ) { + // Found previous insertion - use it + break; + } else { + // Keep searching... + if (++hash == tablesize) hash = 0; + } + } else { + // Cannot be in table + Q_ASSERT ( pix != 256 ); // too many colors + // Insert into table at this unused position + dst->setColor( pix, (*p & amask) ); + table[hash].pix = pix++; + table[hash].rgb = *p & amask; + break; + } + } + p++; + } + } + + if ( (conversion_flags & TQt::DitherMode_Mask) == TQt::PreferDither ) { + do_quant = TRUE; + } else { + for ( y=0; yheight(); y++ ) { // check if <= 256 colors + p = (TQRgb *)src->scanLine(y); + b = dst->scanLine(y); + x = src->width(); + while ( x-- ) { + // Find in table... + int hash = (*p & amask) % tablesize; + for (;;) { + if ( table[hash].used() ) { + if ( table[hash].rgb == (*p & amask) ) { + // Found previous insertion - use it + break; + } else { + // Keep searching... + if (++hash == tablesize) hash = 0; + } + } else { + // Cannot be in table + if ( pix == 256 ) { // too many colors + do_quant = TRUE; + // Break right out + x = 0; + y = src->height(); + } else { + // Insert into table at this unused position + dst->setColor( pix, (*p & amask) ); + table[hash].pix = pix++; + table[hash].rgb = (*p & amask); + } + break; + } + } + *b++ = table[hash].pix; // May occur once incorrectly + p++; + } + } + } + int ncols = do_quant ? 256 : pix; + + static uint bm[16][16]; + static int init=0; + if (!init) { + // Build a Bayer Matrix for dithering + + init = 1; + int n, i, j; + + bm[0][0]=0; + + for (n=1; n<16; n*=2) { + for (i=0; isetNumColors( ncols ); + + if ( do_quant ) { // quantization needed + +#define MAX_R 5 +#define MAX_G 5 +#define MAX_B 5 +#define INDEXOF(r,g,b) (((r)*(MAX_G+1)+(g))*(MAX_B+1)+(b)) + + int rc, gc, bc; + + for ( rc=0; rc<=MAX_R; rc++ ) // build 6x6x6 color cube + for ( gc=0; gc<=MAX_G; gc++ ) + for ( bc=0; bc<=MAX_B; bc++ ) { + dst->setColor( INDEXOF(rc,gc,bc), + (amask&0xff000000) + | qRgb( rc*255/MAX_R, gc*255/MAX_G, bc*255/MAX_B ) ); + } + + int sw = src->width(); + + int* line1[3]; + int* line2[3]; + int* pv[3]; + if ( ( conversion_flags & TQt::Dither_Mask ) == TQt::DiffuseDither ) { + line1[0] = new int[src->width()]; + line2[0] = new int[src->width()]; + line1[1] = new int[src->width()]; + line2[1] = new int[src->width()]; + line1[2] = new int[src->width()]; + line2[2] = new int[src->width()]; + pv[0] = new int[sw]; + pv[1] = new int[sw]; + pv[2] = new int[sw]; + } + + for ( y=0; y < src->height(); y++ ) { + p = (TQRgb *)src->scanLine(y); + b = dst->scanLine(y); + TQRgb *end = p + sw; + + // perform quantization + if ( ( conversion_flags & TQt::Dither_Mask ) == TQt::ThresholdDither ) { +#define DITHER(p,m) ((uchar) ((p * (m) + 127) / 255)) + while ( p < end ) { + rc = qRed( *p ); + gc = qGreen( *p ); + bc = qBlue( *p ); + + *b++ = + INDEXOF( + DITHER(rc, MAX_R), + DITHER(gc, MAX_G), + DITHER(bc, MAX_B) + ); + + p++; + } +#undef DITHER + } else if ( ( conversion_flags & TQt::Dither_Mask ) == TQt::OrderedDither ) { +#define DITHER(p,d,m) ((uchar) ((((256 * (m) + (m) + 1)) * (p) + (d)) / 65536 )) + + int x = 0; + while ( p < end ) { + uint d = bm[y&15][x&15]; + + rc = qRed( *p ); + gc = qGreen( *p ); + bc = qBlue( *p ); + + *b++ = + INDEXOF( + DITHER(rc, d, MAX_R), + DITHER(gc, d, MAX_G), + DITHER(bc, d, MAX_B) + ); + + p++; + x++; + } +#undef DITHER + } else { // Diffuse + int endian = (TQImage::systemByteOrder() == TQImage::BigEndian); + int x; + uchar* q = src->scanLine(y); + uchar* q2 = src->scanLine(y+1 < src->height() ? y + 1 : 0); + for (int chan = 0; chan < 3; chan++) { + b = dst->scanLine(y); + int *l1 = (y&1) ? line2[chan] : line1[chan]; + int *l2 = (y&1) ? line1[chan] : line2[chan]; + if ( y == 0 ) { + for (int i=0; iheight() ) { + for (int i=0; i>4; + l2[x+1] += err>>4; + } + l2[x]+=(err*5)>>4; + if (x>1) + l2[x-1]+=(err*3)>>4; + } + } else { + for (x=sw; x-->0; ) { + int pix = TQMAX(TQMIN(5, (l1[x] * 5 + 128)/ 255), 0); + int err = l1[x] - pix * 255 / 5; + pv[chan][x] = pix; + + // Spread the error around... + if ( x > 0 ) { + l1[x-1] += (err*7)>>4; + l2[x-1] += err>>4; + } + l2[x]+=(err*5)>>4; + if (x+1 < sw) + l2[x+1]+=(err*3)>>4; + } + } + } + if (endian) { + for (x=0; xhasAlphaBuffer() ) { + const int trans = 216; + dst->setColor(trans, 0x00000000); // transparent + TQImage mask = src->createAlphaMask(conversion_flags); + uchar* m; + for ( y=0; y < src->height(); y++ ) { + uchar bit = 0x80; + m = mask.scanLine(y); + b = dst->scanLine(y); + int w = src->width(); + for ( x = 0; x>= 1)) { + bit = 0x80; + while ( xcreate(src->width(), src->height(), 32) ) + return FALSE; // create failed + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + for ( int y=0; yheight(); y++ ) { // for each scan line... + register uint *p = (uint *)dst->scanLine(y); + uchar *b = src->scanLine(y); + uint *end = p + dst->width(); + while ( p < end ) + *p++ = src->color(*b++); + } + return TRUE; +} + + +static bool convert_1_to_32( const TQImage *src, TQImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 32) ) + return FALSE; // could not create + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + for ( int y=0; yheight(); y++ ) { // for each scan line... + register uint *p = (uint *)dst->scanLine(y); + uchar *b = src->scanLine(y); + int x; + if ( src->bitOrder() == TQImage::BigEndian ) { + for ( x=0; xwidth(); x++ ) { + *p++ = src->color( (*b >> (7 - (x & 7))) & 1 ); + if ( (x & 7) == 7 ) + b++; + } + } else { + for ( x=0; xwidth(); x++ ) { + *p++ = src->color( (*b >> (x & 7)) & 1 ); + if ( (x & 7) == 7 ) + b++; + } + } + } + return TRUE; +} +#endif // QT_NO_IMAGE_TRUECOLOR + +static bool convert_1_to_8( const TQImage *src, TQImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 8, 2) ) + return FALSE; // something failed + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + if (src->numColors() >= 2) { + dst->setColor( 0, src->color(0) ); // copy color table + dst->setColor( 1, src->color(1) ); + } else { + // Unlikely, but they do exist + if (src->numColors() >= 1) + dst->setColor( 0, src->color(0) ); + else + dst->setColor( 0, 0xffffffff ); + dst->setColor( 1, 0xff000000 ); + } + for ( int y=0; yheight(); y++ ) { // for each scan line... + register uchar *p = dst->scanLine(y); + uchar *b = src->scanLine(y); + int x; + if ( src->bitOrder() == TQImage::BigEndian ) { + for ( x=0; xwidth(); x++ ) { + *p++ = (*b >> (7 - (x & 7))) & 1; + if ( (x & 7) == 7 ) + b++; + } + } else { + for ( x=0; xwidth(); x++ ) { + *p++ = (*b >> (x & 7)) & 1; + if ( (x & 7) == 7 ) + b++; + } + } + } + return TRUE; +} + +#ifndef QT_NO_IMAGE_DITHER_TO_1 +// +// dither_to_1: Uses selected dithering algorithm. +// + +static bool dither_to_1( const TQImage *src, TQImage *dst, + int conversion_flags, bool fromalpha ) +{ + if ( !dst->create(src->width(), src->height(), 1, 2, TQImage::BigEndian) ) + return FALSE; // something failed + + enum { Threshold, Ordered, Diffuse } dithermode; + + if ( fromalpha ) { + if ( ( conversion_flags & TQt::AlphaDither_Mask ) == TQt::DiffuseAlphaDither ) + dithermode = Diffuse; + else if ( ( conversion_flags & TQt::AlphaDither_Mask ) == TQt::OrderedAlphaDither ) + dithermode = Ordered; + else + dithermode = Threshold; + } else { + if ( ( conversion_flags & TQt::Dither_Mask ) == TQt::ThresholdDither ) + dithermode = Threshold; + else if ( ( conversion_flags & TQt::Dither_Mask ) == TQt::OrderedDither ) + dithermode = Ordered; + else + dithermode = Diffuse; + } + + dst->setColor( 0, qRgb(255, 255, 255) ); + dst->setColor( 1, qRgb( 0, 0, 0) ); + int w = src->width(); + int h = src->height(); + int d = src->depth(); + uchar gray[256]; // gray map for 8 bit images + bool use_gray = d == 8; + if ( use_gray ) { // make gray map + if ( fromalpha ) { + // Alpha 0x00 -> 0 pixels (white) + // Alpha 0xFF -> 1 pixels (black) + for ( int i=0; inumColors(); i++ ) + gray[i] = (255 - (src->color(i) >> 24)); + } else { + // Pixel 0x00 -> 1 pixels (black) + // Pixel 0xFF -> 0 pixels (white) + for ( int i=0; inumColors(); i++ ) + gray[i] = qGray( src->color(i) & 0x00ffffff ); + } + } + + switch ( dithermode ) { + case Diffuse: { + int *line1 = new int[w]; + int *line2 = new int[w]; + int bmwidth = (w+7)/8; + if ( !(line1 && line2) ) + return FALSE; + register uchar *p; + uchar *end; + int *b1, *b2; + int wbytes = w * (d/8); + p = src->bits(); + end = p + wbytes; + b2 = line2; + if ( use_gray ) { // 8 bit image + while ( p < end ) + *b2++ = gray[*p++]; +#ifndef QT_NO_IMAGE_TRUECOLOR + } else { // 32 bit image + if ( fromalpha ) { + while ( p < end ) { + *b2++ = 255 - (*(uint*)p >> 24); + p += 4; + } + } else { + while ( p < end ) { + *b2++ = qGray(*(uint*)p); + p += 4; + } + } +#endif + } + int x, y; + for ( y=0; yscanLine(y+1); + end = p + wbytes; + b2 = line2; + if ( use_gray ) { // 8 bit image + while ( p < end ) + *b2++ = gray[*p++]; +#ifndef QT_NO_IMAGE_TRUECOLOR + } else { // 24 bit image + if ( fromalpha ) { + while ( p < end ) { + *b2++ = 255 - (*(uint*)p >> 24); + p += 4; + } + } else { + while ( p < end ) { + *b2++ = qGray(*(uint*)p); + p += 4; + } + } +#endif + } + } + + int err; + p = dst->scanLine( y ); + memset( p, 0, bmwidth ); + b1 = line1; + b2 = line2; + int bit = 7; + for ( x=1; x<=w; x++ ) { + if ( *b1 < 128 ) { // black pixel + err = *b1++; + *p |= 1 << bit; + } else { // white pixel + err = *b1++ - 255; + } + if ( bit == 0 ) { + p++; + bit = 7; + } else { + bit--; + } + if ( x < w ) + *b1 += (err*7)>>4; // spread error to right pixel + if ( not_last_line ) { + b2[0] += (err*5)>>4; // pixel below + if ( x > 1 ) + b2[-1] += (err*3)>>4; // pixel below left + if ( x < w ) + b2[1] += err>>4; // pixel below right + } + b2++; + } + } + delete [] line1; + delete [] line2; + } break; + case Ordered: { + static uint bm[16][16]; + static int init=0; + if (!init) { + // Build a Bayer Matrix for dithering + + init = 1; + int n, i, j; + + bm[0][0]=0; + + for (n=1; n<16; n*=2) { + for (i=0; ifill( 0 ); + uchar** mline = dst->jumpTable(); +#ifndef QT_NO_IMAGE_TRUECOLOR + if ( d == 32 ) { + uint** line = (uint**)src->jumpTable(); + for ( int i=0; i> 24) >= bm[j++&15][i&15] ) + *m |= 1 << bit; + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } else { + while ( p < end ) { + if ( (uint)qGray(*p++) < bm[j++&15][i&15] ) + *m |= 1 << bit; + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } + } + } else +#endif // QT_NO_IMAGE_TRUECOLOR + /* ( d == 8 ) */ { + uchar** line = src->jumpTable(); + for ( int i=0; ifill( 0 ); + uchar** mline = dst->jumpTable(); +#ifndef QT_NO_IMAGE_TRUECOLOR + if ( d == 32 ) { + uint** line = (uint**)src->jumpTable(); + for ( int i=0; i> 24) >= 128 ) + *m |= 1 << bit; // Set mask "on" + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } else { + while ( p < end ) { + if ( qGray(*p++) < 128 ) + *m |= 1 << bit; // Set pixel "black" + if ( bit == 0 ) { + m++; + bit = 7; + } else { + bit--; + } + } + } + } + } else +#endif //QT_NO_IMAGE_TRUECOLOR + if ( d == 8 ) { + uchar** line = src->jumpTable(); + for ( int i=0; i> 11; + int g=(c & 0x07e0) >> 6; //green/2 + int b=(c & 0x001f); + return r == g && g == b; +} + + +static bool convert_16_to_32( const TQImage *src, TQImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 32) ) + return FALSE; // create failed + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + for ( int y=0; yheight(); y++ ) { // for each scan line... + register uint *p = (uint *)dst->scanLine(y); + ushort *s = (ushort*)src->scanLine(y); + uint *end = p + dst->width(); + while ( p < end ) + *p++ = qt_conv16ToRgb( *s++ ); + } + return TRUE; +} + + +static bool convert_32_to_16( const TQImage *src, TQImage *dst ) +{ + if ( !dst->create(src->width(), src->height(), 16) ) + return FALSE; // create failed + dst->setAlphaBuffer( src->hasAlphaBuffer() ); + for ( int y=0; yheight(); y++ ) { // for each scan line... + register ushort *p = (ushort *)dst->scanLine(y); + uint *s = (uint*)src->scanLine(y); + ushort *end = p + dst->width(); + while ( p < end ) + *p++ = qt_convRgbTo16( *s++ ); + } + return TRUE; +} + + +#endif + +/*! + Converts the depth (bpp) of the image to \a depth and returns the + converted image. The original image is not changed. + + The \a depth argument must be 1, 8, 16 (TQt/Embedded only) or 32. + + Returns \c *this if \a depth is equal to the image depth, or a + \link isNull() null\endlink image if this image cannot be + converted. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + \sa TQt::ImageConversionFlags depth() isNull() +*/ + +TQImage TQImage::convertDepth( int depth, int conversion_flags ) const +{ + TQImage image; + if ( data->d == depth ) + image = *this; // no conversion +#ifndef QT_NO_IMAGE_DITHER_TO_1 + else if ( (data->d == 8 || data->d == 32) && depth == 1 ) // dither + dither_to_1( this, &image, conversion_flags, FALSE ); +#endif +#ifndef QT_NO_IMAGE_TRUECOLOR + else if ( data->d == 32 && depth == 8 ) // 32 -> 8 + convert_32_to_8( this, &image, conversion_flags ); + else if ( data->d == 8 && depth == 32 ) // 8 -> 32 + convert_8_to_32( this, &image ); +#endif + else if ( data->d == 1 && depth == 8 ) // 1 -> 8 + convert_1_to_8( this, &image ); +#ifndef QT_NO_IMAGE_TRUECOLOR + else if ( data->d == 1 && depth == 32 ) // 1 -> 32 + convert_1_to_32( this, &image ); +#endif +#ifndef QT_NO_IMAGE_16_BIT + else if ( data->d == 16 && depth != 16 ) { + TQImage tmp; + convert_16_to_32( this, &tmp ); + image = tmp.convertDepth( depth, conversion_flags ); + } else if ( data->d != 16 && depth == 16 ) { + TQImage tmp = convertDepth( 32, conversion_flags ); + convert_32_to_16( &tmp, &image ); + } +#endif + else { +#if defined(QT_CHECK_RANGE) + if ( isNull() ) + qWarning( "TQImage::convertDepth: Image is a null image" ); + else + qWarning( "TQImage::convertDepth: Depth %d not supported", depth ); +#endif + } + return image; +} + +/*! + \overload +*/ + +TQImage TQImage::convertDepth( int depth ) const +{ + return convertDepth( depth, 0 ); +} + +/*! + Returns TRUE if ( \a x, \a y ) is a valid coordinate in the image; + otherwise returns FALSE. + + \sa width() height() pixelIndex() +*/ + +bool TQImage::valid( int x, int y ) const +{ + return x >= 0 && x < width() + && y >= 0 && y < height(); +} + +/*! + Returns the pixel index at the given coordinates. + + If (\a x, \a y) is not \link valid() valid\endlink, or if the + image is not a paletted image (depth() \> 8), the results are + undefined. + + \sa valid() depth() +*/ + +int TQImage::pixelIndex( int x, int y ) const +{ +#if defined(QT_CHECK_RANGE) + if ( x < 0 || x >= width() ) { + qWarning( "TQImage::pixel: x=%d out of range", x ); + return -12345; + } +#endif + uchar * s = scanLine( y ); + switch( depth() ) { + case 1: + if ( bitOrder() == TQImage::LittleEndian ) + return (*(s + (x >> 3)) >> (x & 7)) & 1; + else + return (*(s + (x >> 3)) >> (7- (x & 7))) & 1; + case 8: + return (int)s[x]; +#ifndef QT_NO_IMAGE_TRUECOLOR +#ifndef QT_NO_IMAGE_16_BIT + case 16: +#endif + case 32: +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::pixelIndex: Not applicable for %d-bpp images " + "(no palette)", depth() ); +#endif + return 0; +#endif //QT_NO_IMAGE_TRUECOLOR + } + return 0; +} + + +/*! + Returns the color of the pixel at the coordinates (\a x, \a y). + + If (\a x, \a y) is not \link valid() on the image\endlink, the + results are undefined. + + \sa setPixel() qRed() qGreen() qBlue() valid() +*/ + +TQRgb TQImage::pixel( int x, int y ) const +{ +#if defined(QT_CHECK_RANGE) + if ( x < 0 || x >= width() ) { + qWarning( "TQImage::pixel: x=%d out of range", x ); + return 12345; + } +#endif + uchar * s = scanLine( y ); + switch( depth() ) { + case 1: + if ( bitOrder() == TQImage::LittleEndian ) + return color( (*(s + (x >> 3)) >> (x & 7)) & 1 ); + else + return color( (*(s + (x >> 3)) >> (7- (x & 7))) & 1 ); + case 8: + return color( (int)s[x] ); +#ifndef QT_NO_IMAGE_16_BIT + case 16: + return qt_conv16ToRgb(((ushort*)s)[x]); +#endif +#ifndef QT_NO_IMAGE_TRUECOLOR + case 32: + return ((TQRgb*)s)[x]; +#endif + default: + return 100367; + } +} + + +/*! + Sets the pixel index or color at the coordinates (\a x, \a y) to + \a index_or_rgb. + + If (\a x, \a y) is not \link valid() valid\endlink, the result is + undefined. + + If the image is a paletted image (depth() \<= 8) and \a + index_or_rgb \>= numColors(), the result is undefined. + + \sa pixelIndex() pixel() qRgb() qRgba() valid() +*/ + +void TQImage::setPixel( int x, int y, uint index_or_rgb ) +{ + if ( x < 0 || x >= width() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::setPixel: x=%d out of range", x ); +#endif + return; + } + if ( depth() == 1 ) { + uchar * s = scanLine( y ); + if ( index_or_rgb > 1) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::setPixel: index=%d out of range", + index_or_rgb ); +#endif + } else if ( bitOrder() == TQImage::LittleEndian ) { + if (index_or_rgb==0) + *(s + (x >> 3)) &= ~(1 << (x & 7)); + else + *(s + (x >> 3)) |= (1 << (x & 7)); + } else { + if (index_or_rgb==0) + *(s + (x >> 3)) &= ~(1 << (7-(x & 7))); + else + *(s + (x >> 3)) |= (1 << (7-(x & 7))); + } + } else if ( depth() == 8 ) { + if (index_or_rgb > (uint)numColors()) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::setPixel: index=%d out of range", + index_or_rgb ); +#endif + return; + } + uchar * s = scanLine( y ); + s[x] = index_or_rgb; +#ifndef QT_NO_IMAGE_16_BIT + } else if ( depth() == 16 ) { + ushort * s = (ushort*)scanLine( y ); + s[x] = qt_convRgbTo16(index_or_rgb); +#endif +#ifndef QT_NO_IMAGE_TRUECOLOR + } else if ( depth() == 32 ) { + TQRgb * s = (TQRgb*)scanLine( y ); + s[x] = index_or_rgb; +#endif + } +} + + +/*! + Converts the bit order of the image to \a bitOrder and returns the + converted image. The original image is not changed. + + Returns \c *this if the \a bitOrder is equal to the image bit + order, or a \link isNull() null\endlink image if this image cannot + be converted. + + \sa bitOrder() systemBitOrder() isNull() +*/ + +TQImage TQImage::convertBitOrder( Endian bitOrder ) const +{ + if ( isNull() || data->d != 1 || // invalid argument(s) + !(bitOrder == BigEndian || bitOrder == LittleEndian) ) { + TQImage nullImage; + return nullImage; + } + if ( data->bitordr == bitOrder ) // nothing to do + return copy(); + + TQImage image( data->w, data->h, 1, data->ncols, bitOrder ); + + int bpl = (width() + 7) / 8; + for ( int y = 0; y < data->h; y++ ) { + register uchar *p = jumpTable()[y]; + uchar *end = p + bpl; + uchar *b = image.jumpTable()[y]; + while ( p < end ) + *b++ = bitflip[*p++]; + } + memcpy( image.colorTable(), colorTable(), numColors()*sizeof(TQRgb) ); + return image; +} + +// ### Candidate (renamed) for qcolor.h +static +bool isGray(TQRgb c) +{ + return qRed(c) == qGreen(c) + && qRed(c) == qBlue(c); +} + +/*! + Returns TRUE if all the colors in the image are shades of gray + (i.e. their red, green and blue components are equal); otherwise + returns FALSE. + + This function is slow for large 16-bit (TQt/Embedded only) and 32-bit images. + + \sa isGrayscale() +*/ +bool TQImage::allGray() const +{ +#ifndef QT_NO_IMAGE_TRUECOLOR + if (depth()==32) { + int p = width()*height(); + TQRgb* b = (TQRgb*)bits(); + while (p--) + if (!isGray(*b++)) + return FALSE; +#ifndef QT_NO_IMAGE_16_BIT + } else if (depth()==16) { + int p = width()*height(); + ushort* b = (ushort*)bits(); + while (p--) + if (!is16BitGray(*b++)) + return FALSE; +#endif + } else +#endif //QT_NO_IMAGE_TRUECOLOR + { + if (!data->ctbl) return TRUE; + for (int i=0; ictbl[i])) + return FALSE; + } + return TRUE; +} + +/*! + For 16-bit (TQt/Embedded only) and 32-bit images, this function is + equivalent to allGray(). + + For 8-bpp images, this function returns TRUE if color(i) is + TQRgb(i,i,i) for all indices of the color table; otherwise returns + FALSE. + + \sa allGray() depth() +*/ +bool TQImage::isGrayscale() const +{ + switch (depth()) { +#ifndef QT_NO_IMAGE_TRUECOLOR + case 32: +#ifndef QT_NO_IMAGE_16_BIT + case 16: +#endif + return allGray(); +#endif //QT_NO_IMAGE_TRUECOLOR + case 8: { + for (int i=0; ictbl[i] != qRgb(i,i,i)) + return FALSE; + return TRUE; + } + } + return FALSE; +} + +#ifndef QT_NO_IMAGE_SMOOTHSCALE +static +void pnmscale(const TQImage& src, TQImage& dst) +{ + TQRgb* xelrow = 0; + TQRgb* tempxelrow = 0; + register TQRgb* xP; + register TQRgb* nxP; + int rows, cols, rowsread, newrows, newcols; + register int row, col, needtoreadrow; + const uchar maxval = 255; + double xscale, yscale; + long sxscale, syscale; + register long fracrowtofill, fracrowleft; + long* as; + long* rs; + long* gs; + long* bs; + int rowswritten = 0; + + cols = src.width(); + rows = src.height(); + newcols = dst.width(); + newrows = dst.height(); + + long SCALE; + long HALFSCALE; + + if (cols > 4096) + { + SCALE = 4096; + HALFSCALE = 2048; + } + else + { + int fac = 4096; + + while (cols * fac > 4096) + { + fac /= 2; + } + + SCALE = fac * cols; + HALFSCALE = fac * cols / 2; + } + + xscale = (double) newcols / (double) cols; + yscale = (double) newrows / (double) rows; + + sxscale = (long)(xscale * SCALE); + syscale = (long)(yscale * SCALE); + + if ( newrows != rows ) /* shortcut Y scaling if possible */ + tempxelrow = new TQRgb[cols]; + + if ( src.hasAlphaBuffer() ) { + dst.setAlphaBuffer(TRUE); + as = new long[cols]; + for ( col = 0; col < cols; ++col ) + as[col] = HALFSCALE; + } else { + as = 0; + } + rs = new long[cols]; + gs = new long[cols]; + bs = new long[cols]; + rowsread = 0; + fracrowleft = syscale; + needtoreadrow = 1; + for ( col = 0; col < cols; ++col ) + rs[col] = gs[col] = bs[col] = HALFSCALE; + fracrowtofill = SCALE; + + for ( row = 0; row < newrows; ++row ) { + /* First scale Y from xelrow into tempxelrow. */ + if ( newrows == rows ) { + /* shortcut Y scaling if possible */ + tempxelrow = xelrow = (TQRgb*)src.scanLine(rowsread++); + } else { + while ( fracrowleft < fracrowtofill ) { + if ( needtoreadrow && rowsread < rows ) + xelrow = (TQRgb*)src.scanLine(rowsread++); + for ( col = 0, xP = xelrow; col < cols; ++col, ++xP ) { + if (as) { + as[col] += fracrowleft * qAlpha( *xP ); + rs[col] += fracrowleft * qRed( *xP ) * qAlpha( *xP ) / 255; + gs[col] += fracrowleft * qGreen( *xP ) * qAlpha( *xP ) / 255; + bs[col] += fracrowleft * qBlue( *xP ) * qAlpha( *xP ) / 255; + } else { + rs[col] += fracrowleft * qRed( *xP ); + gs[col] += fracrowleft * qGreen( *xP ); + bs[col] += fracrowleft * qBlue( *xP ); + } + } + fracrowtofill -= fracrowleft; + fracrowleft = syscale; + needtoreadrow = 1; + } + /* Now fracrowleft is >= fracrowtofill, so we can produce a row. */ + if ( needtoreadrow && rowsread < rows ) { + xelrow = (TQRgb*)src.scanLine(rowsread++); + needtoreadrow = 0; + } + register long a=0; + for ( col = 0, xP = xelrow, nxP = tempxelrow; + col < cols; ++col, ++xP, ++nxP ) + { + register long r, g, b; + + if ( as ) { + r = rs[col] + fracrowtofill * qRed( *xP ) * qAlpha( *xP ) / 255; + g = gs[col] + fracrowtofill * qGreen( *xP ) * qAlpha( *xP ) / 255; + b = bs[col] + fracrowtofill * qBlue( *xP ) * qAlpha( *xP ) / 255; + a = as[col] + fracrowtofill * qAlpha( *xP ); + if ( a ) { + r = r * 255 / a * SCALE; + g = g * 255 / a * SCALE; + b = b * 255 / a * SCALE; + } + } else { + r = rs[col] + fracrowtofill * qRed( *xP ); + g = gs[col] + fracrowtofill * qGreen( *xP ); + b = bs[col] + fracrowtofill * qBlue( *xP ); + } + r /= SCALE; + if ( r > maxval ) r = maxval; + g /= SCALE; + if ( g > maxval ) g = maxval; + b /= SCALE; + if ( b > maxval ) b = maxval; + if ( as ) { + a /= SCALE; + if ( a > maxval ) a = maxval; + *nxP = qRgba( (int)r, (int)g, (int)b, (int)a ); + as[col] = HALFSCALE; + } else { + *nxP = qRgb( (int)r, (int)g, (int)b ); + } + rs[col] = gs[col] = bs[col] = HALFSCALE; + } + fracrowleft -= fracrowtofill; + if ( fracrowleft == 0 ) { + fracrowleft = syscale; + needtoreadrow = 1; + } + fracrowtofill = SCALE; + } + + /* Now scale X from tempxelrow into dst and write it out. */ + if ( newcols == cols ) { + /* shortcut X scaling if possible */ + memcpy(dst.scanLine(rowswritten++), tempxelrow, newcols*4); + } else { + register long a, r, g, b; + register long fraccoltofill, fraccolleft = 0; + register int needcol; + + nxP = (TQRgb*)dst.scanLine(rowswritten++); + fraccoltofill = SCALE; + a = r = g = b = HALFSCALE; + needcol = 0; + for ( col = 0, xP = tempxelrow; col < cols; ++col, ++xP ) { + fraccolleft = sxscale; + while ( fraccolleft >= fraccoltofill ) { + if ( needcol ) { + ++nxP; + a = r = g = b = HALFSCALE; + } + if ( as ) { + r += fraccoltofill * qRed( *xP ) * qAlpha( *xP ) / 255; + g += fraccoltofill * qGreen( *xP ) * qAlpha( *xP ) / 255; + b += fraccoltofill * qBlue( *xP ) * qAlpha( *xP ) / 255; + a += fraccoltofill * qAlpha( *xP ); + if ( a ) { + r = r * 255 / a * SCALE; + g = g * 255 / a * SCALE; + b = b * 255 / a * SCALE; + } + } else { + r += fraccoltofill * qRed( *xP ); + g += fraccoltofill * qGreen( *xP ); + b += fraccoltofill * qBlue( *xP ); + } + r /= SCALE; + if ( r > maxval ) r = maxval; + g /= SCALE; + if ( g > maxval ) g = maxval; + b /= SCALE; + if ( b > maxval ) b = maxval; + if (as) { + a /= SCALE; + if ( a > maxval ) a = maxval; + *nxP = qRgba( (int)r, (int)g, (int)b, (int)a ); + } else { + *nxP = qRgb( (int)r, (int)g, (int)b ); + } + fraccolleft -= fraccoltofill; + fraccoltofill = SCALE; + needcol = 1; + } + if ( fraccolleft > 0 ) { + if ( needcol ) { + ++nxP; + a = r = g = b = HALFSCALE; + needcol = 0; + } + if (as) { + a += fraccolleft * qAlpha( *xP ); + r += fraccolleft * qRed( *xP ) * qAlpha( *xP ) / 255; + g += fraccolleft * qGreen( *xP ) * qAlpha( *xP ) / 255; + b += fraccolleft * qBlue( *xP ) * qAlpha( *xP ) / 255; + } else { + r += fraccolleft * qRed( *xP ); + g += fraccolleft * qGreen( *xP ); + b += fraccolleft * qBlue( *xP ); + } + fraccoltofill -= fraccolleft; + } + } + if ( fraccoltofill > 0 ) { + --xP; + if (as) { + a += fraccolleft * qAlpha( *xP ); + r += fraccoltofill * qRed( *xP ) * qAlpha( *xP ) / 255; + g += fraccoltofill * qGreen( *xP ) * qAlpha( *xP ) / 255; + b += fraccoltofill * qBlue( *xP ) * qAlpha( *xP ) / 255; + if ( a ) { + r = r * 255 / a * SCALE; + g = g * 255 / a * SCALE; + b = b * 255 / a * SCALE; + } + } else { + r += fraccoltofill * qRed( *xP ); + g += fraccoltofill * qGreen( *xP ); + b += fraccoltofill * qBlue( *xP ); + } + } + if ( ! needcol ) { + r /= SCALE; + if ( r > maxval ) r = maxval; + g /= SCALE; + if ( g > maxval ) g = maxval; + b /= SCALE; + if ( b > maxval ) b = maxval; + if (as) { + a /= SCALE; + if ( a > maxval ) a = maxval; + *nxP = qRgba( (int)r, (int)g, (int)b, (int)a ); + } else { + *nxP = qRgb( (int)r, (int)g, (int)b ); + } + } + } + } + + if ( newrows != rows && tempxelrow )// Robust, tempxelrow might be 0 1 day + delete [] tempxelrow; + if ( as ) // Avoid purify complaint + delete [] as; + if ( rs ) // Robust, rs might be 0 one day + delete [] rs; + if ( gs ) // Robust, gs might be 0 one day + delete [] gs; + if ( bs ) // Robust, bs might be 0 one day + delete [] bs; +} +#endif + +/*! + \enum TQImage::ScaleMode + + The functions scale() and smoothScale() use different modes for + scaling the image. The purpose of these modes is to retain the + ratio of the image if this is retquired. + + \img scaling.png + + \value ScaleFree The image is scaled freely: the resulting image + fits exactly into the specified size; the ratio will not + necessarily be preserved. + \value ScaleMin The ratio of the image is preserved and the + resulting image is guaranteed to fit into the specified size + (it is as large as possible within these constraints) - the + image might be smaller than the requested size. + \value ScaleMax The ratio of the image is preserved and the + resulting image fills the whole specified rectangle (it is as + small as possible within these constraints) - the image might + be larger than the requested size. +*/ + +#ifndef QT_NO_IMAGE_SMOOTHSCALE +/*! + Returns a smoothly scaled copy of the image. The returned image + has a size of width \a w by height \a h pixels if \a mode is \c + ScaleFree. The modes \c ScaleMin and \c ScaleMax may be used to + preserve the ratio of the image: if \a mode is \c ScaleMin, the + returned image is guaranteed to fit into the rectangle specified + by \a w and \a h (it is as large as possible within the + constraints); if \a mode is \c ScaleMax, the returned image fits + at least into the specified rectangle (it is a small as possible + within the constraints). + + For 32-bpp images and 1-bpp/8-bpp color images the result will be + 32-bpp, whereas \link allGray() all-gray \endlink images + (including black-and-white 1-bpp) will produce 8-bit \link + isGrayscale() grayscale \endlink images with the palette spanning + 256 grays from black to white. + + This function uses code based on pnmscale.c by Jef Poskanzer. + + pnmscale.c - read a portable anymap and scale it + + \legalese + + Copyright (C) 1989, 1991 by Jef Poskanzer. + + Permission to use, copy, modify, and distribute this software and + its documentation for any purpose and without fee is hereby + granted, provided that the above copyright notice appear in all + copies and that both that copyright notice and this permission + notice appear in supporting documentation. This software is + provided "as is" without express or implied warranty. + + \sa scale() mirror() +*/ +TQImage TQImage::smoothScale( int w, int h, ScaleMode mode ) const +{ + return smoothScale( TQSize( w, h ), mode ); +} +#endif + +#ifndef QT_NO_IMAGE_SMOOTHSCALE +/*! + \overload + + The requested size of the image is \a s. +*/ +TQImage TQImage::smoothScale( const TQSize& s, ScaleMode mode ) const +{ + if ( isNull() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::smoothScale: Image is a null image" ); +#endif + return copy(); + } + + TQSize newSize = size(); + newSize.scale( s, (TQSize::ScaleMode)mode ); // ### remove cast in TQt 4.0 + if ( newSize == size() ) + return copy(); + + if ( depth() == 32 ) { + TQImage img( newSize, 32 ); + // 32-bpp to 32-bpp + pnmscale( *this, img ); + return img; + } else if ( depth() != 16 && allGray() && !hasAlphaBuffer() ) { + // Inefficient + return convertDepth(32).smoothScale(newSize, mode).convertDepth(8); + } else { + // Inefficient + return convertDepth(32).smoothScale(newSize, mode); + } +} +#endif + +/*! + Returns a copy of the image scaled to a rectangle of width \a w + and height \a h according to the ScaleMode \a mode. + + \list + \i If \a mode is \c ScaleFree, the image is scaled to (\a w, + \a h). + \i If \a mode is \c ScaleMin, the image is scaled to a rectangle + as large as possible inside (\a w, \a h), preserving the aspect + ratio. + \i If \a mode is \c ScaleMax, the image is scaled to a rectangle + as small as possible outside (\a w, \a h), preserving the aspect + ratio. + \endlist + + If either the width \a w or the height \a h is 0 or negative, this + function returns a \link isNull() null\endlink image. + + This function uses a simple, fast algorithm. If you need better + quality, use smoothScale() instead. + + \sa scaleWidth() scaleHeight() smoothScale() xForm() +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +TQImage TQImage::scale( int w, int h, ScaleMode mode ) const +{ + return scale( TQSize( w, h ), mode ); +} +#endif + +/*! + \overload + + The requested size of the image is \a s. +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +TQImage TQImage::scale( const TQSize& s, ScaleMode mode ) const +{ + if ( isNull() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::scale: Image is a null image" ); +#endif + return copy(); + } + if ( s.isEmpty() ) + return TQImage(); + + TQSize newSize = size(); + newSize.scale( s, (TQSize::ScaleMode)mode ); // ### remove cast in TQt 4.0 + if ( newSize == size() ) + return copy(); + + TQImage img; + TQWMatrix wm; + wm.scale( (double)newSize.width() / width(), (double)newSize.height() / height() ); + img = xForm( wm ); + // ### I should test and resize the image if it has not the right size +// if ( img.width() != newSize.width() || img.height() != newSize.height() ) +// img.resize( newSize.width(), newSize.height() ); + return img; +} +#endif + +/*! + Returns a scaled copy of the image. The returned image has a width + of \a w pixels. This function automatically calculates the height + of the image so that the ratio of the image is preserved. + + If \a w is 0 or negative a \link isNull() null\endlink image is + returned. + + \sa scale() scaleHeight() smoothScale() xForm() +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +TQImage TQImage::scaleWidth( int w ) const +{ + if ( isNull() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::scaleWidth: Image is a null image" ); +#endif + return copy(); + } + if ( w <= 0 ) + return TQImage(); + + TQWMatrix wm; + double factor = (double) w / width(); + wm.scale( factor, factor ); + return xForm( wm ); +} +#endif + +/*! + Returns a scaled copy of the image. The returned image has a + height of \a h pixels. This function automatically calculates the + width of the image so that the ratio of the image is preserved. + + If \a h is 0 or negative a \link isNull() null\endlink image is + returned. + + \sa scale() scaleWidth() smoothScale() xForm() +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +TQImage TQImage::scaleHeight( int h ) const +{ + if ( isNull() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage::scaleHeight: Image is a null image" ); +#endif + return copy(); + } + if ( h <= 0 ) + return TQImage(); + + TQWMatrix wm; + double factor = (double) h / height(); + wm.scale( factor, factor ); + return xForm( wm ); +} +#endif + + +/*! + Returns a copy of the image that is transformed using the + transformation matrix, \a matrix. + + The transformation \a matrix is internally adjusted to compensate + for unwanted translation, i.e. xForm() returns the smallest image + that contains all the transformed points of the original image. + + \sa scale() TQPixmap::xForm() TQPixmap::trueMatrix() TQWMatrix +*/ +#ifndef QT_NO_IMAGE_TRANSFORMATION +TQImage TQImage::xForm( const TQWMatrix &matrix ) const +{ + // This function uses the same algorithm as (and steals tquite some + // code from) TQPixmap::xForm(). + + if ( isNull() ) + return copy(); + + if ( depth() == 16 ) { + // inefficient + return convertDepth( 32 ).xForm( matrix ); + } + + // source image data + int ws = width(); + int hs = height(); + int sbpl = bytesPerLine(); + uchar *sptr = bits(); + + // target image data + int wd; + int hd; + + int bpp = depth(); + + // compute size of target image + TQWMatrix mat = TQPixmap::trueMatrix( matrix, ws, hs ); + if ( mat.m12() == 0.0F && mat.m21() == 0.0F ) { + if ( mat.m11() == 1.0F && mat.m22() == 1.0F ) // identity matrix + return copy(); + hd = qRound( mat.m22() * hs ); + wd = qRound( mat.m11() * ws ); + hd = TQABS( hd ); + wd = TQABS( wd ); + } else { // rotation or shearing + TQPointArray a( TQRect(0, 0, ws, hs) ); + a = mat.map( a ); + TQRect r = a.boundingRect().normalize(); + wd = r.width(); + hd = r.height(); + } + + bool invertible; + mat = mat.invert( &invertible ); // invert matrix + if ( hd == 0 || wd == 0 || !invertible ) // error, return null image + return TQImage(); + + // create target image (some of the code is from TQImage::copy()) + TQImage dImage( wd, hd, depth(), numColors(), bitOrder() ); + + // If the image allocation failed, we need to gracefully abort. + if (dImage.isNull()) + return dImage; + + memcpy( dImage.colorTable(), colorTable(), numColors()*sizeof(TQRgb) ); + dImage.setAlphaBuffer( hasAlphaBuffer() ); + dImage.data->dpmx = dotsPerMeterX(); + dImage.data->dpmy = dotsPerMeterY(); + + switch ( bpp ) { + // initizialize the data + case 1: + memset( dImage.bits(), 0, dImage.numBytes() ); + break; + case 8: + if ( dImage.data->ncols < 256 ) { + // colors are left in the color table, so pick that one as transparent + dImage.setNumColors( dImage.data->ncols+1 ); + dImage.setColor( dImage.data->ncols-1, 0x00 ); + memset( dImage.bits(), dImage.data->ncols-1, dImage.numBytes() ); + } else { + memset( dImage.bits(), 0, dImage.numBytes() ); + } + break; + case 16: + memset( dImage.bits(), 0xff, dImage.numBytes() ); + break; + case 32: + memset( dImage.bits(), 0x00, dImage.numBytes() ); + break; + } + + int type; + if ( bitOrder() == BigEndian ) + type = QT_XFORM_TYPE_MSBFIRST; + else + type = QT_XFORM_TYPE_LSBFIRST; + int dbpl = dImage.bytesPerLine(); + qt_xForm_helper( mat, 0, type, bpp, dImage.bits(), dbpl, 0, hd, sptr, sbpl, + ws, hs ); + return dImage; +} +#endif + +/*! + Builds and returns a 1-bpp mask from the alpha buffer in this + image. Returns a \link isNull() null\endlink image if \link + setAlphaBuffer() alpha buffer mode\endlink is disabled. + + See TQPixmap::convertFromImage() for a description of the \a + conversion_flags argument. + + The returned image has little-endian bit order, which you can + convert to big-endianness using convertBitOrder(). + + \sa createHeuristicMask() hasAlphaBuffer() setAlphaBuffer() +*/ +#ifndef QT_NO_IMAGE_DITHER_TO_1 +TQImage TQImage::createAlphaMask( int conversion_flags ) const +{ + if ( conversion_flags == 1 ) { + // Old code is passing "TRUE". + conversion_flags = TQt::DiffuseAlphaDither; + } + + if ( isNull() || !hasAlphaBuffer() ) + return TQImage(); + + if ( depth() == 1 ) { + // A monochrome pixmap, with alpha channels on those two colors. + // Pretty unlikely, so use less efficient solution. + return convertDepth(8, conversion_flags) + .createAlphaMask( conversion_flags ); + } + + TQImage mask1; + dither_to_1( this, &mask1, conversion_flags, TRUE ); + return mask1; +} +#endif + +#ifndef QT_NO_IMAGE_HEURISTIC_MASK +/*! + Creates and returns a 1-bpp heuristic mask for this image. It + works by selecting a color from one of the corners, then chipping + away pixels of that color starting at all the edges. + + The four corners vote for which color is to be masked away. In + case of a draw (this generally means that this function is not + applicable to the image), the result is arbitrary. + + The returned image has little-endian bit order, which you can + convert to big-endianness using convertBitOrder(). + + If \a clipTight is TRUE the mask is just large enough to cover the + pixels; otherwise, the mask is larger than the data pixels. + + This function disregards the \link hasAlphaBuffer() alpha buffer + \endlink. + + \sa createAlphaMask() +*/ + +TQImage TQImage::createHeuristicMask( bool clipTight ) const +{ + if ( isNull() ) { + TQImage nullImage; + return nullImage; + } + if ( depth() != 32 ) { + TQImage img32 = convertDepth(32); + return img32.createHeuristicMask(clipTight); + } + +#define PIX(x,y) (*((TQRgb*)scanLine(y)+x) & 0x00ffffff) + + int w = width(); + int h = height(); + TQImage m(w, h, 1, 2, TQImage::LittleEndian); + m.setColor( 0, 0xffffff ); + m.setColor( 1, 0 ); + m.fill( 0xff ); + + TQRgb background = PIX(0,0); + if ( background != PIX(w-1,0) && + background != PIX(0,h-1) && + background != PIX(w-1,h-1) ) { + background = PIX(w-1,0); + if ( background != PIX(w-1,h-1) && + background != PIX(0,h-1) && + PIX(0,h-1) == PIX(w-1,h-1) ) { + background = PIX(w-1,h-1); + } + } + + int x,y; + bool done = FALSE; + uchar *ypp, *ypc, *ypn; + while( !done ) { + done = TRUE; + ypn = m.scanLine(0); + ypc = 0; + for ( y = 0; y < h; y++ ) { + ypp = ypc; + ypc = ypn; + ypn = (y == h-1) ? 0 : m.scanLine(y+1); + TQRgb *p = (TQRgb *)scanLine(y); + for ( x = 0; x < w; x++ ) { + // slowness here - it's possible to do six of these tests + // together in one go. oh well. + if ( ( x == 0 || y == 0 || x == w-1 || y == h-1 || + !(*(ypc + ((x-1) >> 3)) & (1 << ((x-1) & 7))) || + !(*(ypc + ((x+1) >> 3)) & (1 << ((x+1) & 7))) || + !(*(ypp + (x >> 3)) & (1 << (x & 7))) || + !(*(ypn + (x >> 3)) & (1 << (x & 7))) ) && + ( (*(ypc + (x >> 3)) & (1 << (x & 7))) ) && + ( (*p & 0x00ffffff) == background ) ) { + done = FALSE; + *(ypc + (x >> 3)) &= ~(1 << (x & 7)); + } + p++; + } + } + } + + if ( !clipTight ) { + ypn = m.scanLine(0); + ypc = 0; + for ( y = 0; y < h; y++ ) { + ypp = ypc; + ypc = ypn; + ypn = (y == h-1) ? 0 : m.scanLine(y+1); + TQRgb *p = (TQRgb *)scanLine(y); + for ( x = 0; x < w; x++ ) { + if ( (*p & 0x00ffffff) != background ) { + if ( x > 0 ) + *(ypc + ((x-1) >> 3)) |= (1 << ((x-1) & 7)); + if ( x < w-1 ) + *(ypc + ((x+1) >> 3)) |= (1 << ((x+1) & 7)); + if ( y > 0 ) + *(ypp + (x >> 3)) |= (1 << (x & 7)); + if ( y < h-1 ) + *(ypn + (x >> 3)) |= (1 << (x & 7)); + } + p++; + } + } + } + +#undef PIX + + return m; +} +#endif //QT_NO_IMAGE_HEURISTIC_MASK + +#ifndef QT_NO_IMAGE_MIRROR +/* + This code is contributed by Philipp Lang, + GeneriCom Software Germany (www.generi.com) + under the terms of the TQPL, Version 1.0 +*/ + +/*! + \overload + + Returns a mirror of the image, mirrored in the horizontal and/or + the vertical direction depending on whether \a horizontal and \a + vertical are set to TRUE or FALSE. The original image is not + changed. + + \sa smoothScale() +*/ +TQImage TQImage::mirror(bool horizontal, bool vertical) const +{ + int w = width(); + int h = height(); + if ( (w <= 1 && h <= 1) || (!horizontal && !vertical) ) + return copy(); + + // Create result image, copy colormap + TQImage result(w, h, depth(), numColors(), bitOrder()); + memcpy(result.colorTable(), colorTable(), numColors()*sizeof(TQRgb)); + result.setAlphaBuffer(hasAlphaBuffer()); + + if (depth() == 1) + w = (w+7)/8; + int dxi = horizontal ? -1 : 1; + int dxs = horizontal ? w-1 : 0; + int dyi = vertical ? -1 : 1; + int dy = vertical ? h-1: 0; + + // 1 bit, 8 bit + if (depth() == 1 || depth() == 8) { + for (int sy = 0; sy < h; sy++, dy += dyi) { + Q_UINT8* ssl = (Q_UINT8*)(data->bits[sy]); + Q_UINT8* dsl = (Q_UINT8*)(result.data->bits[dy]); + int dx = dxs; + for (int sx = 0; sx < w; sx++, dx += dxi) + dsl[dx] = ssl[sx]; + } + } +#ifndef QT_NO_IMAGE_TRUECOLOR +#ifndef QT_NO_IMAGE_16_BIT + // 16 bit + else if (depth() == 16) { + for (int sy = 0; sy < h; sy++, dy += dyi) { + Q_UINT16* ssl = (Q_UINT16*)(data->bits[sy]); + Q_UINT16* dsl = (Q_UINT16*)(result.data->bits[dy]); + int dx = dxs; + for (int sx = 0; sx < w; sx++, dx += dxi) + dsl[dx] = ssl[sx]; + } + } +#endif + // 32 bit + else if (depth() == 32) { + for (int sy = 0; sy < h; sy++, dy += dyi) { + Q_UINT32* ssl = (Q_UINT32*)(data->bits[sy]); + Q_UINT32* dsl = (Q_UINT32*)(result.data->bits[dy]); + int dx = dxs; + for (int sx = 0; sx < w; sx++, dx += dxi) + dsl[dx] = ssl[sx]; + } + } +#endif + + // special handling of 1 bit images for horizontal mirroring + if (horizontal && depth() == 1) { + int shift = width() % 8; + for (int y = h-1; y >= 0; y--) { + Q_UINT8* a0 = (Q_UINT8*)(result.data->bits[y]); + // Swap bytes + Q_UINT8* a = a0+dxs; + while (a >= a0) { + *a = bitflip[*a]; + a--; + } + // Shift bits if unaligned + if (shift != 0) { + a = a0+dxs; + Q_UINT8 c = 0; + if (bitOrder() == TQImage::LittleEndian) { + while (a >= a0) { + Q_UINT8 nc = *a << shift; + *a = (*a >> (8-shift)) | c; + --a; + c = nc; + } + } else { + while (a >= a0) { + Q_UINT8 nc = *a >> shift; + *a = (*a << (8-shift)) | c; + --a; + c = nc; + } + } + } + } + } + + return result; +} + +/*! + Returns a TQImage which is a vertically mirrored copy of this + image. The original TQImage is not changed. +*/ + +TQImage TQImage::mirror() const +{ + return mirror(FALSE,TRUE); +} +#endif //QT_NO_IMAGE_MIRROR + +/*! + Returns a TQImage in which the values of the red and blue + components of all pixels have been swapped, effectively converting + an RGB image to a BGR image. The original TQImage is not changed. +*/ + +TQImage TQImage::swapRGB() const +{ + TQImage res = copy(); + if ( !isNull() ) { +#ifndef QT_NO_IMAGE_TRUECOLOR + if ( depth() == 32 ) { + for ( int i=0; i < height(); i++ ) { + uint *p = (uint*)scanLine( i ); + uint *q = (uint*)res.scanLine( i ); + uint *end = p + width(); + while ( p < end ) { + *q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) | + (*p & 0xff00ff00); + p++; + q++; + } + } +#ifndef QT_NO_IMAGE_16_BIT + } else if ( depth() == 16 ) { + qWarning( "TQImage::swapRGB not implemented for 16bpp" ); +#endif + } else +#endif //QT_NO_IMAGE_TRUECOLOR + { + uint* p = (uint*)colorTable(); + uint* q = (uint*)res.colorTable(); + if ( p && q ) { + for ( int i=0; i < numColors(); i++ ) { + *q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) | + (*p & 0xff00ff00); + p++; + q++; + } + } + } + } + return res; +} + +#ifndef QT_NO_IMAGEIO +/*! + Returns a string that specifies the image format of the file \a + fileName, or 0 if the file cannot be read or if the format is not + recognized. + + The TQImageIO documentation lists the guaranteed supported image + formats, or use TQImage::inputFormats() and TQImage::outputFormats() + to get lists that include the installed formats. + + \sa load() save() +*/ + +const char* TQImage::imageFormat( const TQString &fileName ) +{ + return TQImageIO::imageFormat( fileName ); +} + +/*! + Returns a list of image formats that are supported for image + input. + + \sa outputFormats() inputFormatList() TQImageIO +*/ +TQStrList TQImage::inputFormats() +{ + return TQImageIO::inputFormats(); +} +#ifndef QT_NO_STRINGLIST +/*! + Returns a list of image formats that are supported for image + input. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + TQStringList list = myImage.inputFormatList(); + TQStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa outputFormatList() inputFormats() TQImageIO +*/ +TQStringList TQImage::inputFormatList() +{ + return TQStringList::fromStrList(TQImageIO::inputFormats()); +} + + +/*! + Returns a list of image formats that are supported for image + output. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + TQStringList list = myImage.outputFormatList(); + TQStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa inputFormatList() outputFormats() TQImageIO +*/ +TQStringList TQImage::outputFormatList() +{ + return TQStringList::fromStrList(TQImageIO::outputFormats()); +} +#endif //QT_NO_STRINGLIST + +/*! + Returns a list of image formats that are supported for image + output. + + \sa inputFormats() outputFormatList() TQImageIO +*/ +TQStrList TQImage::outputFormats() +{ + return TQImageIO::outputFormats(); +} + + +/*! + Loads an image from the file \a fileName. Returns TRUE if the + image was successfully loaded; otherwise returns FALSE. + + If \a format is specified, the loader attempts to read the image + using the specified format. If \a format is not specified (which + is the default), the loader reads a few bytes from the header to + guess the file format. + + The TQImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa loadFromData() save() imageFormat() TQPixmap::load() TQImageIO +*/ + +bool TQImage::load( const TQString &fileName, const char* format ) +{ + TQImageIO io( fileName, format ); + bool result = io.read(); + if ( result ) + operator=( io.image() ); + return result; +} + +/*! + Loads an image from the first \a len bytes of binary data in \a + buf. Returns TRUE if the image was successfully loaded; otherwise + returns FALSE. + + If \a format is specified, the loader attempts to read the image + using the specified format. If \a format is not specified (which + is the default), the loader reads a few bytes from the header to + guess the file format. + + The TQImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa load() save() imageFormat() TQPixmap::loadFromData() TQImageIO +*/ + +bool TQImage::loadFromData( const uchar *buf, uint len, const char *format ) +{ + TQByteArray a; + a.setRawData( (char *)buf, len ); + TQBuffer b( a ); + b.open( IO_ReadOnly ); + TQImageIO io( &b, format ); + bool result = io.read(); + b.close(); + a.resetRawData( (char *)buf, len ); + if ( result ) + operator=( io.image() ); + return result; +} + +/*! + \overload + + Loads an image from the TQByteArray \a buf. +*/ +bool TQImage::loadFromData( TQByteArray buf, const char *format ) +{ + return loadFromData( (const uchar *)(buf.data()), buf.size(), format ); +} + +/*! + Saves the image to the file \a fileName, using the image file + format \a format and a quality factor of \a quality. \a quality + must be in the range 0..100 or -1. Specify 0 to obtain small + compressed files, 100 for large uncompressed files, and -1 (the + default) to use the default settings. + + Returns TRUE if the image was successfully saved; otherwise + returns FALSE. + + \sa load() loadFromData() imageFormat() TQPixmap::save() TQImageIO +*/ + +bool TQImage::save( const TQString &fileName, const char* format, int quality ) const +{ + if ( isNull() ) + return FALSE; // nothing to save + TQImageIO io( fileName, format ); + return doImageIO( &io, quality ); +} + +/*! + \overload + + This function writes a TQImage to the TQIODevice, \a device. This + can be used, for example, to save an image directly into a + TQByteArray: + \code + TQImage image; + TQByteArray ba; + TQBuffer buffer( ba ); + buffer.open( IO_WriteOnly ); + image.save( &buffer, "PNG" ); // writes image into ba in PNG format + \endcode +*/ + +bool TQImage::save( TQIODevice* device, const char* format, int quality ) const +{ + if ( isNull() ) + return FALSE; // nothing to save + TQImageIO io( device, format ); + return doImageIO( &io, quality ); +} + +/* \internal +*/ + +bool TQImage::doImageIO( TQImageIO* io, int quality ) const +{ + if ( !io ) + return FALSE; + io->setImage( *this ); +#if defined(QT_CHECK_RANGE) + if ( quality > 100 || quality < -1 ) + qWarning( "TQPixmap::save: quality out of range [-1,100]" ); +#endif + if ( quality >= 0 ) + io->setQuality( TQMIN(quality,100) ); + return io->write(); +} +#endif //QT_NO_IMAGEIO + +/***************************************************************************** + TQImage stream functions + *****************************************************************************/ +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +/*! + \relates TQImage + + Writes the image \a image to the stream \a s as a PNG image, or as a + BMP image if the stream's version is 1. + + Note that writing the stream to a file will not produce a valid image file. + + \sa TQImage::save() + \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQImage &image ) +{ + if ( s.version() >= 5 ) { + if ( image.isNull() ) { + s << (Q_INT32) 0; // null image marker + return s; + } else { + s << (Q_INT32) 1; + // continue ... + } + } + TQImageIO io; + io.setIODevice( s.device() ); + if ( s.version() == 1 ) + io.setFormat( "BMP" ); + else + io.setFormat( "PNG" ); + + io.setImage( image ); + io.write(); + return s; +} + +/*! + \relates TQImage + + Reads an image from the stream \a s and stores it in \a image. + + \sa TQImage::load() + \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQImage &image ) +{ + if ( s.version() >= 5 ) { + Q_INT32 nullMarker; + s >> nullMarker; + if ( !nullMarker ) { + image = TQImage(); // null image + return s; + } + } + TQImageIO io( s.device(), 0 ); + if ( io.read() ) + image = io.image(); + return s; +} +#endif + +/***************************************************************************** + Standard image io handlers (defined below) + *****************************************************************************/ + +// standard image io handlers (defined below) +#ifndef QT_NO_IMAGEIO_BMP +static void read_bmp_image( TQImageIO * ); +static void write_bmp_image( TQImageIO * ); +#endif +#ifndef QT_NO_IMAGEIO_PPM +static void read_pbm_image( TQImageIO * ); +static void write_pbm_image( TQImageIO * ); +#endif +#ifndef QT_NO_IMAGEIO_XBM +static void read_xbm_image( TQImageIO * ); +static void write_xbm_image( TQImageIO * ); +#endif +#ifndef QT_NO_IMAGEIO_XPM +static void read_xpm_image( TQImageIO * ); +static void write_xpm_image( TQImageIO * ); +#endif + +#ifndef QT_NO_ASYNC_IMAGE_IO +static void read_async_image( TQImageIO * ); // Not in table of handlers +#endif + +/***************************************************************************** + Misc. utility functions + *****************************************************************************/ +#if !defined(QT_NO_IMAGEIO_XPM) || !defined(QT_NO_IMAGEIO_XBM) +static TQString fbname( const TQString &fileName ) // get file basename (sort of) +{ + TQString s = fileName; + if ( !s.isEmpty() ) { + int i; + if ( (i = s.findRev('/')) >= 0 ) + s = s.mid( i ); + if ( (i = s.findRev('\\')) >= 0 ) + s = s.mid( i ); + TQRegExp r( TQString::fromLatin1("[a-zA-Z][a-zA-Z0-9_]*") ); + int p = r.search( s ); + if ( p == -1 ) + s.truncate( 0 ); + else + s = s.mid( p, r.matchedLength() ); + } + if ( s.isEmpty() ) + s = TQString::fromLatin1( "dummy" ); + return s; +} +#endif + +#ifndef QT_NO_IMAGEIO_BMP +static void swapPixel01( TQImage *image ) // 1-bpp: swap 0 and 1 pixels +{ + int i; + if ( image->depth() == 1 && image->numColors() == 2 ) { + register uint *p = (uint *)image->bits(); + int nbytes = image->numBytes(); + for ( i=0; icolor(0); // swap color 0 and 1 + image->setColor( 0, image->color(1) ); + image->setColor( 1, t ); + } +} +#endif + + +/***************************************************************************** + TQImageIO member functions + *****************************************************************************/ + +/*! + \class TQImageIO qimage.h + + \brief The TQImageIO class contains parameters for loading and + saving images. + + \ingroup images + \ingroup graphics + \ingroup io + + TQImageIO contains a TQIODevice object that is used for image data + I/O. The programmer can install new image file formats in addition + to those that TQt provides. + + TQt currently supports the following image file formats: PNG, BMP, + XBM, XPM and PNM. It may also support JPEG, MNG and GIF, if + specially configured during compilation. The different PNM formats + are: PBM (P1 or P4), PGM (P2 or P5), and PPM (P3 or P6). + + You don't normally need to use this class; TQPixmap::load(), + TQPixmap::save(), and TQImage contain sufficient functionality. + + For image files that contain sequences of images, only the first + is read. See TQMovie for loading multiple images. + + PBM, PGM, and PPM format \e output is always in the more condensed + raw format. PPM and PGM files with more than 256 levels of + intensity are scaled down when reading. + + \warning If you are in a country which recognizes software patents + and in which Unisys holds a patent on LZW compression and/or + decompression and you want to use GIF, Unisys may retquire you to + license the technology. Such countries include Canada, Japan, the + USA, France, Germany, Italy and the UK. + + GIF support may be removed completely in a future version of TQt. + We recommend using the PNG format. + + \sa TQImage TQPixmap TQFile TQMovie +*/ + +#ifndef QT_NO_IMAGEIO +struct TQImageIOData +{ + const char *parameters; + int quality; + float gamma; +}; + +/*! + Constructs a TQImageIO object with all parameters set to zero. +*/ + +TQImageIO::TQImageIO() +{ + init(); +} + +/*! + Constructs a TQImageIO object with the I/O device \a ioDevice and a + \a format tag. +*/ + +TQImageIO::TQImageIO( TQIODevice *ioDevice, const char *format ) + : frmt(format) +{ + init(); + iodev = ioDevice; +} + +/*! + Constructs a TQImageIO object with the file name \a fileName and a + \a format tag. +*/ + +TQImageIO::TQImageIO( const TQString &fileName, const char* format ) + : frmt(format), fname(fileName) +{ + init(); +} + +/*! + Contains initialization common to all TQImageIO constructors. +*/ + +void TQImageIO::init() +{ + d = new TQImageIOData(); + d->parameters = 0; + d->quality = -1; // default quality of the current format + d->gamma=0.0f; + iostat = 0; + iodev = 0; +} + +/*! + Destroys the object and all related data. +*/ + +TQImageIO::~TQImageIO() +{ + if ( d->parameters ) + delete [] (char*)d->parameters; + delete d; +} + + +/***************************************************************************** + TQImageIO image handler functions + *****************************************************************************/ + +class TQImageHandler +{ +public: + TQImageHandler( const char *f, const char *h, const TQCString& fl, + image_io_handler r, image_io_handler w ); + TQCString format; // image format + TQRegExp header; // image header pattern + enum TMode { Untranslated=0, TranslateIn, TranslateInOut } text_mode; + image_io_handler read_image; // image read function + image_io_handler write_image; // image write function + bool obsolete; // support not "published" +}; + +TQImageHandler::TQImageHandler( const char *f, const char *h, const TQCString& fl, + image_io_handler r, image_io_handler w ) + : format(f), header(TQString::fromLatin1(h)) +{ + text_mode = Untranslated; + if ( fl.contains('t') ) + text_mode = TranslateIn; + else if ( fl.contains('T') ) + text_mode = TranslateInOut; + obsolete = fl.contains('O'); + read_image = r; + write_image = w; +} + +typedef TQPtrList TQIHList;// list of image handlers +static TQIHList *imageHandlers = 0; +#ifndef QT_NO_COMPONENT +static TQPluginManager *plugin_manager = 0; +#else +static void *plugin_manager = 0; +#endif + +void qt_init_image_plugins() +{ +#ifndef QT_NO_COMPONENT + if ( plugin_manager ) + return; + + plugin_manager = new TQPluginManager( IID_QImageFormat, TQApplication::libraryPaths(), "/imageformats" ); + + TQStringList features = plugin_manager->featureList(); + TQStringList::Iterator it = features.begin(); + while ( it != features.end() ) { + TQString str = *it; + ++it; + TQInterfacePtr iface; + plugin_manager->queryInterface( str, &iface ); + if ( iface ) + iface->installIOHandler( str ); + } +#endif +} + +static void cleanup() +{ + // make sure that image handlers are delete before plugin manager + delete imageHandlers; + imageHandlers = 0; +#ifndef QT_NO_COMPONENT + delete plugin_manager; + plugin_manager = 0; +#endif +} + +void qt_init_image_handlers() // initialize image handlers +{ + if ( !imageHandlers ) { + imageHandlers = new TQIHList; + Q_CHECK_PTR( imageHandlers ); + imageHandlers->setAutoDelete( TRUE ); + qAddPostRoutine( cleanup ); +#ifndef QT_NO_IMAGEIO_BMP + TQImageIO::defineIOHandler( "BMP", "^BM", 0, + read_bmp_image, write_bmp_image ); +#endif +#ifndef QT_NO_IMAGEIO_PPM + TQImageIO::defineIOHandler( "PBM", "^P1", "t", + read_pbm_image, write_pbm_image ); + TQImageIO::defineIOHandler( "PBMRAW", "^P4", "O", + read_pbm_image, write_pbm_image ); + TQImageIO::defineIOHandler( "PGM", "^P2", "t", + read_pbm_image, write_pbm_image ); + TQImageIO::defineIOHandler( "PGMRAW", "^P5", "O", + read_pbm_image, write_pbm_image ); + TQImageIO::defineIOHandler( "PPM", "^P3", "t", + read_pbm_image, write_pbm_image ); + TQImageIO::defineIOHandler( "PPMRAW", "^P6", "O", + read_pbm_image, write_pbm_image ); +#endif +#ifndef QT_NO_IMAGEIO_XBM + TQImageIO::defineIOHandler( "XBM", "^((/\\*(?!.XPM.\\*/))|#define)", "T", + read_xbm_image, write_xbm_image ); +#endif +#ifndef QT_NO_IMAGEIO_XPM + TQImageIO::defineIOHandler( "XPM", "/\\*.XPM.\\*/", "T", + read_xpm_image, write_xpm_image ); +#endif +#ifndef QT_NO_IMAGEIO_MNG + qInitMngIO(); +#endif +#ifndef QT_NO_IMAGEIO_PNG + qInitPngIO(); +#endif +#ifndef QT_NO_IMAGEIO_JPEG + qInitJpegIO(); +#endif + } +} + +static TQImageHandler *get_image_handler( const char *format ) +{ // get pointer to handler + qt_init_image_handlers(); + qt_init_image_plugins(); + register TQImageHandler *p = imageHandlers->first(); + while ( p ) { // traverse list + if ( p->format == format ) + return p; + p = imageHandlers->next(); + } + return 0; // no such handler +} + + +/*! + Defines an image I/O handler for the image format called \a + format, which is recognized using the \link qregexp.html#details + regular expression\endlink \a header, read using \a readImage and + written using \a writeImage. + + \a flags is a string of single-character flags for this format. + The only flag defined currently is T (upper case), so the only + legal value for \a flags are "T" and the empty string. The "T" + flag means that the image file is a text file, and TQt should treat + all newline conventions as equivalent. (XPM files and some PPM + files are text files for example.) + + \a format is used to select a handler to write a TQImage; \a header + is used to select a handler to read an image file. + + If \a readImage is a null pointer, the TQImageIO will not be able + to read images in \a format. If \a writeImage is a null pointer, + the TQImageIO will not be able to write images in \a format. If + both are null, the TQImageIO object is valid but useless. + + Example: + \code + void readGIF( TQImageIO *image ) + { + // read the image using the image->ioDevice() + } + + void writeGIF( TQImageIO *image ) + { + // write the image using the image->ioDevice() + } + + // add the GIF image handler + + TQImageIO::defineIOHandler( "GIF", + "^GIF[0-9][0-9][a-z]", + 0, + readGIF, + writeGIF ); + \endcode + + Before the regex test, all the 0 bytes in the file header are + converted to 1 bytes. This is done because when TQt was + ASCII-based, TQRegExp could not handle 0 bytes in strings. + + The regexp is only applied on the first 14 bytes of the file. + + Note that TQt assumes that there is only one handler per format; if + two handlers support the same format, TQt will choose one + arbitrarily. It is not possible to have one handler support + reading, and another support writing. +*/ + +void TQImageIO::defineIOHandler( const char *format, + const char *header, + const char *flags, + image_io_handler readImage, + image_io_handler writeImage ) +{ + qt_init_image_handlers(); + TQImageHandler *p; + p = new TQImageHandler( format, header, flags, + readImage, writeImage ); + Q_CHECK_PTR( p ); + imageHandlers->insert( 0, p ); +} + + +/***************************************************************************** + TQImageIO normal member functions + *****************************************************************************/ + +/*! + \fn const TQImage &TQImageIO::image() const + + Returns the image currently set. + + \sa setImage() +*/ + +/*! + \fn int TQImageIO::status() const + + Returns the image's IO status. A non-zero value indicates an + error, whereas 0 means that the IO operation was successful. + + \sa setStatus() +*/ + +/*! + \fn const char *TQImageIO::format() const + + Returns the image format string or 0 if no format has been + explicitly set. +*/ + +/*! + \fn TQIODevice *TQImageIO::ioDevice() const + + Returns the IO device currently set. + + \sa setIODevice() +*/ + +/*! + \fn TQString TQImageIO::fileName() const + + Returns the file name currently set. + + \sa setFileName() +*/ + +/*! + \fn TQString TQImageIO::description() const + + Returns the image description string. + + \sa setDescription() +*/ + + +/*! + Sets the image to \a image. + + \sa image() +*/ + +void TQImageIO::setImage( const TQImage &image ) +{ + im = image; +} + +/*! + Sets the image IO status to \a status. A non-zero value indicates + an error, whereas 0 means that the IO operation was successful. + + \sa status() +*/ + +void TQImageIO::setStatus( int status ) +{ + iostat = status; +} + +/*! + Sets the image format to \a format for the image to be read or + written. + + It is necessary to specify a format before writing an image, but + it is not necessary to specify a format before reading an image. + + If no format has been set, TQt guesses the image format before + reading it. If a format is set the image will only be read if it + has that format. + + \sa read() write() format() +*/ + +void TQImageIO::setFormat( const char *format ) +{ + frmt = format; +} + +/*! + Sets the IO device to be used for reading or writing an image. + + Setting the IO device allows images to be read/written to any + block-oriented TQIODevice. + + If \a ioDevice is not null, this IO device will override file name + settings. + + \sa setFileName() +*/ + +void TQImageIO::setIODevice( TQIODevice *ioDevice ) +{ + iodev = ioDevice; +} + +/*! + Sets the name of the file to read or write an image from to \a + fileName. + + \sa setIODevice() +*/ + +void TQImageIO::setFileName( const TQString &fileName ) +{ + fname = fileName; +} + +/*! + Returns the quality of the written image, related to the + compression ratio. + + \sa setQuality() TQImage::save() +*/ + +int TQImageIO::quality() const +{ + return d->quality; +} + +/*! + Sets the quality of the written image to \a q, related to the + compression ratio. + + \a q must be in the range -1..100. Specify 0 to obtain small + compressed files, 100 for large uncompressed files. (-1 signifies + the default compression.) + + \sa quality() TQImage::save() +*/ + +void TQImageIO::setQuality( int q ) +{ + d->quality = q; +} + +/*! + Returns the image's parameters string. + + \sa setParameters() +*/ + +const char *TQImageIO::parameters() const +{ + return d->parameters; +} + +/*! + Sets the image's parameter string to \a parameters. This is for + image handlers that retquire special parameters. + + Although the current image formats supported by TQt ignore the + parameters string, it may be used in future extensions or by + contributions (for example, JPEG). + + \sa parameters() +*/ + +void TQImageIO::setParameters( const char *parameters ) +{ + if ( d && d->parameters ) + delete [] (char*)d->parameters; + d->parameters = qstrdup( parameters ); +} + +/*! + Sets the gamma value at which the image will be viewed to \a + gamma. If the image format stores a gamma value for which the + image is intended to be used, then this setting will be used to + modify the image. Setting to 0.0 will disable gamma correction + (i.e. any specification in the file will be ignored). + + The default value is 0.0. + + \sa gamma() +*/ +void TQImageIO::setGamma( float gamma ) +{ + d->gamma=gamma; +} + +/*! + Returns the gamma value at which the image will be viewed. + + \sa setGamma() +*/ +float TQImageIO::gamma() const +{ + return d->gamma; +} + +/*! + Sets the image description string for image handlers that support + image descriptions to \a description. + + Currently, no image format supported by TQt uses the description + string. +*/ + +void TQImageIO::setDescription( const TQString &description ) +{ + descr = description; +} + + +/*! + Returns a string that specifies the image format of the file \a + fileName, or null if the file cannot be read or if the format is + not recognized. +*/ + +const char* TQImageIO::imageFormat( const TQString &fileName ) +{ + TQFile file( fileName ); + if ( !file.open(IO_ReadOnly) ) + return 0; + const char* format = imageFormat( &file ); + file.close(); + return format; +} + +/*! + \overload + + Returns a string that specifies the image format of the image read + from IO device \a d, or 0 if the device cannot be read or if the + format is not recognized. + + Make sure that \a d is at the right position in the device (for + example, at the beginning of the file). + + \sa TQIODevice::at() +*/ + +const char *TQImageIO::imageFormat( TQIODevice *d ) +{ + // if you change this change the documentation for defineIOHandler() + const int buflen = 14; + + char buf[buflen]; + char buf2[buflen]; + qt_init_image_handlers(); + qt_init_image_plugins(); + int pos = d->at(); // save position + int rdlen = d->readBlock( buf, buflen ); // read a few bytes + + if ( rdlen != buflen ) + return 0; + + memcpy( buf2, buf, buflen ); + + const char* format = 0; + for ( int n = 0; n < rdlen; n++ ) + if ( buf[n] == '\0' ) + buf[n] = '\001'; + if ( d->status() == IO_Ok && rdlen > 0 ) { + buf[rdlen - 1] = '\0'; + TQString bufStr = TQString::fromLatin1(buf); + TQImageHandler *p = imageHandlers->first(); + int bestMatch = -1; + while ( p ) { + if ( p->read_image && p->header.search(bufStr) != -1 ) { + // try match with header if a read function is available + if (p->header.matchedLength() > bestMatch) { + // keep looking for best match + format = p->format; + bestMatch = p->header.matchedLength(); + } + } + p = imageHandlers->next(); + } + } + d->at( pos ); // restore position +#ifndef QT_NO_ASYNC_IMAGE_IO + if ( !format ) + format = TQImageDecoder::formatName( (uchar*)buf2, rdlen ); +#endif + + return format; +} + +/*! + Returns a sorted list of image formats that are supported for + image input. +*/ +TQStrList TQImageIO::inputFormats() +{ + TQStrList result; + + qt_init_image_handlers(); + qt_init_image_plugins(); + +#ifndef QT_NO_ASYNC_IMAGE_IO + // Include asynchronous loaders first. + result = TQImageDecoder::inputFormats(); +#endif + + TQImageHandler *p = imageHandlers->first(); + while ( p ) { + if ( p->read_image + && !p->obsolete + && !result.contains(p->format) ) + { + result.inSort(p->format); + } + p = imageHandlers->next(); + } + + return result; +} + +/*! + Returns a sorted list of image formats that are supported for + image output. +*/ +TQStrList TQImageIO::outputFormats() +{ + TQStrList result; + + qt_init_image_handlers(); + qt_init_image_plugins(); + + // Include asynchronous writers (!) first. + // (None) + + TQImageHandler *p = imageHandlers->first(); + while ( p ) { + if ( p->write_image + && !p->obsolete + && !result.contains(p->format) ) + { + result.inSort(p->format); + } + p = imageHandlers->next(); + } + + return result; +} + + + +/*! + Reads an image into memory and returns TRUE if the image was + successfully read; otherwise returns FALSE. + + Before reading an image you must set an IO device or a file name. + If both an IO device and a file name have been set, the IO device + will be used. + + Setting the image file format string is optional. + + Note that this function does \e not set the \link format() + format\endlink used to read the image. If you need that + information, use the imageFormat() static functions. + + Example: + + \code + TQImageIO iio; + TQPixmap pixmap; + iio.setFileName( "vegeburger.bmp" ); + if ( image.read() ) // ok + pixmap = iio.image(); // convert to pixmap + \endcode + + \sa setIODevice() setFileName() setFormat() write() TQPixmap::load() +*/ + +bool TQImageIO::read() +{ + TQFile file; + const char *image_format; + TQImageHandler *h; + + if ( iodev ) { // read from io device + // ok, already open + } else if ( !fname.isEmpty() ) { // read from file + file.setName( fname ); + if ( !file.open(IO_ReadOnly) ) + return FALSE; // cannot open file + iodev = &file; + } else { // no file name or io device + return FALSE; + } + if (frmt.isEmpty()) { + // Try to guess format + image_format = imageFormat( iodev ); // get image format + if ( !image_format ) { + if ( file.isOpen() ) { // unknown format + file.close(); + iodev = 0; + } + return FALSE; + } + } else { + image_format = frmt; + } + + h = get_image_handler( image_format ); + if ( file.isOpen() ) { +#if !defined(Q_OS_UNIX) + if ( h && h->text_mode ) { // reopen in translated mode + file.close(); + file.open( IO_ReadOnly | IO_Translate ); + } + else +#endif + file.at( 0 ); // position to start + } + iostat = 1; // assume error + + if ( h && h->read_image ) { + (*h->read_image)( this ); + } +#ifndef QT_NO_ASYNC_IMAGE_IO + else { + // Format name, but no handler - must be an asychronous reader + read_async_image( this ); + } +#endif + + if ( file.isOpen() ) { // image was read using file + file.close(); + iodev = 0; + } + return iostat == 0; // image successfully read? +} + + +/*! + Writes an image to an IO device and returns TRUE if the image was + successfully written; otherwise returns FALSE. + + Before writing an image you must set an IO device or a file name. + If both an IO device and a file name have been set, the IO device + will be used. + + The image will be written using the specified image format. + + Example: + \code + TQImageIO iio; + TQImage im; + im = pixmap; // convert to image + iio.setImage( im ); + iio.setFileName( "vegeburger.bmp" ); + iio.setFormat( "BMP" ); + if ( iio.write() ) + // returned TRUE if written successfully + \endcode + + \sa setIODevice() setFileName() setFormat() read() TQPixmap::save() +*/ + +bool TQImageIO::write() +{ + if ( frmt.isEmpty() ) + return FALSE; + TQImageHandler *h = get_image_handler( frmt ); + if ( !h && !plugin_manager) { + qt_init_image_plugins(); + h = get_image_handler( frmt ); + } + if ( !h || !h->write_image ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImageIO::write: No such image format handler: %s", + format() ); +#endif + return FALSE; + } + TQFile file; + if ( !iodev && !fname.isEmpty() ) { + file.setName( fname ); + bool translate = h->text_mode==TQImageHandler::TranslateInOut; + int fmode = translate ? IO_WriteOnly|IO_Translate : IO_WriteOnly; + if ( !file.open(fmode) ) // couldn't create file + return FALSE; + iodev = &file; + } + iostat = 1; + (*h->write_image)( this ); + if ( file.isOpen() ) { // image was written using file + file.close(); + iodev = 0; + } + return iostat == 0; // image successfully written? +} +#endif //QT_NO_IMAGEIO + +#ifndef QT_NO_IMAGEIO_BMP + +/***************************************************************************** + BMP (DIB) image read/write functions + *****************************************************************************/ + +const int BMP_FILEHDR_SIZE = 14; // size of BMP_FILEHDR data + +struct BMP_FILEHDR { // BMP file header + char bfType[2]; // "BM" + Q_INT32 bfSize; // size of file + Q_INT16 bfReserved1; + Q_INT16 bfReserved2; + Q_INT32 bfOffBits; // pointer to the pixmap bits +}; + +TQDataStream &operator>>( TQDataStream &s, BMP_FILEHDR &bf ) +{ // read file header + s.readRawBytes( bf.bfType, 2 ); + s >> bf.bfSize >> bf.bfReserved1 >> bf.bfReserved2 >> bf.bfOffBits; + return s; +} + +TQDataStream &operator<<( TQDataStream &s, const BMP_FILEHDR &bf ) +{ // write file header + s.writeRawBytes( bf.bfType, 2 ); + s << bf.bfSize << bf.bfReserved1 << bf.bfReserved2 << bf.bfOffBits; + return s; +} + + +const int BMP_OLD = 12; // old Windows/OS2 BMP size +const int BMP_WIN = 40; // new Windows BMP size +const int BMP_OS2 = 64; // new OS/2 BMP size + +const int BMP_RGB = 0; // no compression +const int BMP_RLE8 = 1; // run-length encoded, 8 bits +const int BMP_RLE4 = 2; // run-length encoded, 4 bits +const int BMP_BITFIELDS = 3; // RGB values encoded in data as bit-fields + +struct BMP_INFOHDR { // BMP information header + Q_INT32 biSize; // size of this struct + Q_INT32 biWidth; // pixmap width + Q_INT32 biHeight; // pixmap height + Q_INT16 biPlanes; // should be 1 + Q_INT16 biBitCount; // number of bits per pixel + Q_INT32 biCompression; // compression method + Q_INT32 biSizeImage; // size of image + Q_INT32 biXPelsPerMeter; // horizontal resolution + Q_INT32 biYPelsPerMeter; // vertical resolution + Q_INT32 biClrUsed; // number of colors used + Q_INT32 biClrImportant; // number of important colors +}; + + +TQDataStream &operator>>( TQDataStream &s, BMP_INFOHDR &bi ) +{ + s >> bi.biSize; + if ( bi.biSize == BMP_WIN || bi.biSize == BMP_OS2 ) { + s >> bi.biWidth >> bi.biHeight >> bi.biPlanes >> bi.biBitCount; + s >> bi.biCompression >> bi.biSizeImage; + s >> bi.biXPelsPerMeter >> bi.biYPelsPerMeter; + s >> bi.biClrUsed >> bi.biClrImportant; + } + else { // probably old Windows format + Q_INT16 w, h; + s >> w >> h >> bi.biPlanes >> bi.biBitCount; + bi.biWidth = w; + bi.biHeight = h; + bi.biCompression = BMP_RGB; // no compression + bi.biSizeImage = 0; + bi.biXPelsPerMeter = bi.biYPelsPerMeter = 0; + bi.biClrUsed = bi.biClrImportant = 0; + } + return s; +} + +TQDataStream &operator<<( TQDataStream &s, const BMP_INFOHDR &bi ) +{ + s << bi.biSize; + s << bi.biWidth << bi.biHeight; + s << bi.biPlanes; + s << bi.biBitCount; + s << bi.biCompression; + s << bi.biSizeImage; + s << bi.biXPelsPerMeter << bi.biYPelsPerMeter; + s << bi.biClrUsed << bi.biClrImportant; + return s; +} + +static +int calc_shift(int mask) +{ + int result = 0; + while (!(mask & 1)) { + result++; + mask >>= 1; + } + return result; +} + +static +bool read_dib( TQDataStream& s, int offset, int startpos, TQImage& image ) +{ + BMP_INFOHDR bi; + TQIODevice* d = s.device(); + + s >> bi; // read BMP info header + if ( d->atEnd() ) // end of stream/file + return FALSE; +#if 0 + qDebug( "offset...........%d", offset ); + qDebug( "startpos.........%d", startpos ); + qDebug( "biSize...........%d", bi.biSize ); + qDebug( "biWidth..........%d", bi.biWidth ); + qDebug( "biHeight.........%d", bi.biHeight ); + qDebug( "biPlanes.........%d", bi.biPlanes ); + qDebug( "biBitCount.......%d", bi.biBitCount ); + qDebug( "biCompression....%d", bi.biCompression ); + qDebug( "biSizeImage......%d", bi.biSizeImage ); + qDebug( "biXPelsPerMeter..%d", bi.biXPelsPerMeter ); + qDebug( "biYPelsPerMeter..%d", bi.biYPelsPerMeter ); + qDebug( "biClrUsed........%d", bi.biClrUsed ); + qDebug( "biClrImportant...%d", bi.biClrImportant ); +#endif + int w = bi.biWidth, h = bi.biHeight, nbits = bi.biBitCount; + int t = bi.biSize, comp = bi.biCompression; + int red_mask, green_mask, blue_mask; + int red_shift = 0; + int green_shift = 0; + int blue_shift = 0; + int red_scale = 0; + int green_scale = 0; + int blue_scale = 0; + + if ( !(nbits == 1 || nbits == 4 || nbits == 8 || nbits == 16 || nbits == 24 || nbits == 32) || + bi.biPlanes != 1 || comp > BMP_BITFIELDS ) + return FALSE; // weird BMP image + if ( !(comp == BMP_RGB || (nbits == 4 && comp == BMP_RLE4) || + (nbits == 8 && comp == BMP_RLE8) || ((nbits == 16 || nbits == 32) && comp == BMP_BITFIELDS)) ) + return FALSE; // weird compression type + + int ncols; + int depth; + switch ( nbits ) { + case 32: + case 24: + case 16: + depth = 32; + break; + case 8: + case 4: + depth = 8; + break; + default: + depth = 1; + } + if ( depth == 32 ) // there's no colormap + ncols = 0; + else // # colors used + ncols = bi.biClrUsed ? bi.biClrUsed : 1 << nbits; + + image.create( w, h, depth, ncols, nbits == 1 ? + TQImage::BigEndian : TQImage::IgnoreEndian ); + if ( image.isNull() ) // could not create image + return FALSE; + + image.setDotsPerMeterX( bi.biXPelsPerMeter ); + image.setDotsPerMeterY( bi.biYPelsPerMeter ); + + d->at( startpos + BMP_FILEHDR_SIZE + bi.biSize ); // goto start of colormap + + if ( ncols > 0 ) { // read color table + uchar rgb[4]; + int rgb_len = t == BMP_OLD ? 3 : 4; + for ( int i=0; ireadBlock( (char *)rgb, rgb_len ) != rgb_len ) + return FALSE; + image.setColor( i, qRgb(rgb[2],rgb[1],rgb[0]) ); + if ( d->atEnd() ) // truncated file + return FALSE; + } + } else if (comp == BMP_BITFIELDS && (nbits == 16 || nbits == 32)) { + if ( (Q_ULONG)d->readBlock( (char *)&red_mask, sizeof(red_mask) ) != sizeof(red_mask) ) + return FALSE; + if ( (Q_ULONG)d->readBlock( (char *)&green_mask, sizeof(green_mask) ) != sizeof(green_mask) ) + return FALSE; + if ( (Q_ULONG)d->readBlock( (char *)&blue_mask, sizeof(blue_mask) ) != sizeof(blue_mask) ) + return FALSE; + red_shift = calc_shift(red_mask); + red_scale = 256 / ((red_mask >> red_shift) + 1); + green_shift = calc_shift(green_mask); + green_scale = 256 / ((green_mask >> green_shift) + 1); + blue_shift = calc_shift(blue_mask); + blue_scale = 256 / ((blue_mask >> blue_shift) + 1); + } else if (comp == BMP_RGB && (nbits == 24 || nbits == 32)) { + blue_mask = 0x000000ff; + green_mask = 0x0000ff00; + red_mask = 0x00ff0000; + blue_shift = 0; + green_shift = 8; + red_shift = 16; + blue_scale = green_scale = red_scale = 1; + } else if (comp == BMP_RGB && nbits == 16) // don't support RGB values for 15/16 bpp + return FALSE; + + // offset can be bogus, be careful + if (offset>=0 && startpos + offset > (Q_LONG)d->at() ) + d->at( startpos + offset ); // start of image data + + int bpl = image.bytesPerLine(); +#ifdef Q_WS_QWS + // + // Guess the number of bytes-per-line if we don't know how much + // image data is in the file (bogus image ?). + // + int bmpbpl = bi.biSizeImage > 0 ? + bi.biSizeImage / bi.biHeight : + (d->size() - offset) / bi.biHeight; + int pad = bmpbpl-bpl; +#endif + uchar **line = image.jumpTable(); + + if ( nbits == 1 ) { // 1 bit BMP image + while ( --h >= 0 ) { + if ( d->readBlock((char*)line[h],bpl) != bpl ) + break; +#ifdef Q_WS_QWS + if ( pad > 0 ) + d->at(d->at()+pad); +#endif + } + if ( ncols == 2 && qGray(image.color(0)) < qGray(image.color(1)) ) + swapPixel01( &image ); // pixel 0 is white! + } + + else if ( nbits == 4 ) { // 4 bit BMP image + int buflen = ((w+7)/8)*4; + uchar *buf = new uchar[buflen]; + Q_CHECK_PTR( buf ); + if ( comp == BMP_RLE4 ) { // run length compression + int x=0, y=0, b, c, i; + register uchar *p = line[h-1]; + uchar *endp = line[h-1]+w; + while ( y < h ) { + if ( (b=d->getch()) == EOF ) + break; + if ( b == 0 ) { // escape code + switch ( (b=d->getch()) ) { + case 0: // end of line + x = 0; + y++; + p = line[h-y-1]; + break; + case 1: // end of image + case EOF: // end of file + y = h; // exit loop + break; + case 2: // delta (jump) + x += d->getch(); + y += d->getch(); + + // Protection + if ( (uint)x >= (uint)w ) + x = w-1; + if ( (uint)y >= (uint)h ) + y = h-1; + + p = line[h-y-1] + x; + break; + default: // absolute mode + // Protection + if ( p + b > endp ) + b = endp-p; + + i = (c = b)/2; + while ( i-- ) { + b = d->getch(); + *p++ = b >> 4; + *p++ = b & 0x0f; + } + if ( c & 1 ) + *p++ = d->getch() >> 4; + if ( (((c & 3) + 1) & 2) == 2 ) + d->getch(); // align on word boundary + x += c; + } + } else { // encoded mode + // Protection + if ( p + b > endp ) + b = endp-p; + + i = (c = b)/2; + b = d->getch(); // 2 pixels to be repeated + while ( i-- ) { + *p++ = b >> 4; + *p++ = b & 0x0f; + } + if ( c & 1 ) + *p++ = b >> 4; + x += c; + } + } + } else if ( comp == BMP_RGB ) { // no compression + while ( --h >= 0 ) { + if ( d->readBlock((char*)buf,buflen) != buflen ) + break; + register uchar *p = line[h]; + uchar *b = buf; + for ( int i=0; i> 4; + *p++ = *b++ & 0x0f; + } + if ( w & 1 ) // the last nibble + *p = *b >> 4; + } + } + delete [] buf; + } + + else if ( nbits == 8 ) { // 8 bit BMP image + if ( comp == BMP_RLE8 ) { // run length compression + int x=0, y=0, b; + register uchar *p = line[h-1]; + const uchar *endp = line[h-1]+w; + while ( y < h ) { + if ( (b=d->getch()) == EOF ) + break; + if ( b == 0 ) { // escape code + switch ( (b=d->getch()) ) { + case 0: // end of line + x = 0; + y++; + p = line[h-y-1]; + break; + case 1: // end of image + case EOF: // end of file + y = h; // exit loop + break; + case 2: // delta (jump) + x += d->getch(); + y += d->getch(); + + // Protection + if ( (uint)x >= (uint)w ) + x = w-1; + if ( (uint)y >= (uint)h ) + y = h-1; + + p = line[h-y-1] + x; + break; + default: // absolute mode + // Protection + if ( p + b > endp ) + b = endp-p; + + if ( d->readBlock( (char *)p, b ) != b ) + return FALSE; + if ( (b & 1) == 1 ) + d->getch(); // align on word boundary + x += b; + p += b; + } + } else { // encoded mode + // Protection + if ( p + b > endp ) + b = endp-p; + + memset( p, d->getch(), b ); // repeat pixel + x += b; + p += b; + } + } + } else if ( comp == BMP_RGB ) { // uncompressed + while ( --h >= 0 ) { + if ( d->readBlock((char *)line[h],bpl) != bpl ) + break; +#ifdef Q_WS_QWS + if ( pad > 0 ) + d->at(d->at()+pad); +#endif + } + } + } + + else if ( nbits == 16 || nbits == 24 || nbits == 32 ) { // 16,24,32 bit BMP image + register TQRgb *p; + TQRgb *end; + uchar *buf24 = new uchar[bpl]; + int bpl24 = ((w*nbits+31)/32)*4; + uchar *b; + int c; + + while ( --h >= 0 ) { + p = (TQRgb *)line[h]; + end = p + w; + if ( d->readBlock( (char *)buf24,bpl24) != bpl24 ) + break; + b = buf24; + while ( p < end ) { + c = *(uchar*)b | (*(uchar*)(b+1)<<8); + if (nbits != 16) + c |= *(uchar*)(b+2)<<16; + *p++ = qRgb(((c & red_mask) >> red_shift) * red_scale, + ((c & green_mask) >> green_shift) * green_scale, + ((c & blue_mask) >> blue_shift) * blue_scale); + b += nbits/8; + } + } + delete[] buf24; + } + + return TRUE; +} + +bool qt_read_dib( TQDataStream& s, TQImage& image ) +{ + return read_dib(s,-1,-BMP_FILEHDR_SIZE,image); +} + + +static void read_bmp_image( TQImageIO *iio ) +{ + TQIODevice *d = iio->ioDevice(); + TQDataStream s( d ); + BMP_FILEHDR bf; + int startpos = d->at(); + + s.setByteOrder( TQDataStream::LittleEndian );// Intel byte order + s >> bf; // read BMP file header + + if ( qstrncmp(bf.bfType,"BM",2) != 0 ) // not a BMP image + return; + + TQImage image; + if (read_dib( s, bf.bfOffBits, startpos, image )) { + iio->setImage( image ); + iio->setStatus( 0 ); // image ok + } +} + +bool qt_write_dib( TQDataStream& s, TQImage image ) +{ + int nbits; + int bpl_bmp; + int bpl = image.bytesPerLine(); + + TQIODevice* d = s.device(); + + if ( image.depth() == 8 && image.numColors() <= 16 ) { + bpl_bmp = (((bpl+1)/2+3)/4)*4; + nbits = 4; + } else if ( image.depth() == 32 ) { + bpl_bmp = ((image.width()*24+31)/32)*4; + nbits = 24; +#ifdef Q_WS_QWS + } else if ( image.depth() == 1 || image.depth() == 8 ) { + // TQt/E doesn't word align. + bpl_bmp = ((image.width()*image.depth()+31)/32)*4; + nbits = image.depth(); +#endif + } else { + bpl_bmp = bpl; + nbits = image.depth(); + } + + BMP_INFOHDR bi; + bi.biSize = BMP_WIN; // build info header + bi.biWidth = image.width(); + bi.biHeight = image.height(); + bi.biPlanes = 1; + bi.biBitCount = nbits; + bi.biCompression = BMP_RGB; + bi.biSizeImage = bpl_bmp*image.height(); + bi.biXPelsPerMeter = image.dotsPerMeterX() ? image.dotsPerMeterX() + : 2834; // 72 dpi default + bi.biYPelsPerMeter = image.dotsPerMeterY() ? image.dotsPerMeterY() : 2834; + bi.biClrUsed = image.numColors(); + bi.biClrImportant = image.numColors(); + s << bi; // write info header + + if ( image.depth() != 32 ) { // write color table + uchar *color_table = new uchar[4*image.numColors()]; + uchar *rgb = color_table; + TQRgb *c = image.colorTable(); + for ( int i=0; iwriteBlock( (char *)color_table, 4*image.numColors() ); + delete [] color_table; + } + + if ( image.depth() == 1 && image.bitOrder() != TQImage::BigEndian ) + image = image.convertBitOrder( TQImage::BigEndian ); + + int y; + + if ( nbits == 1 || nbits == 8 ) { // direct output +#ifdef Q_WS_QWS + // TQt/E doesn't word align. + int pad = bpl_bmp - bpl; + char padding[4]; +#endif + for ( y=image.height()-1; y>=0; y-- ) { + d->writeBlock( (char*)image.scanLine(y), bpl ); +#ifdef Q_WS_QWS + d->writeBlock( padding, pad ); +#endif + } + return TRUE; + } + + uchar *buf = new uchar[bpl_bmp]; + uchar *b, *end; + register uchar *p; + + memset( buf, 0, bpl_bmp ); + for ( y=image.height()-1; y>=0; y-- ) { // write the image bits + if ( nbits == 4 ) { // convert 8 -> 4 bits + p = image.scanLine(y); + b = buf; + end = b + image.width()/2; + while ( b < end ) { + *b++ = (*p << 4) | (*(p+1) & 0x0f); + p += 2; + } + if ( image.width() & 1 ) + *b = *p << 4; + } else { // 32 bits + TQRgb *p = (TQRgb *)image.scanLine( y ); + TQRgb *end = p + image.width(); + b = buf; + while ( p < end ) { + *b++ = qBlue(*p); + *b++ = qGreen(*p); + *b++ = qRed(*p); + p++; + } + } + if ( bpl_bmp != d->writeBlock( (char*)buf, bpl_bmp ) ) { + delete[] buf; + return FALSE; + } + } + delete[] buf; + return TRUE; +} + + +static void write_bmp_image( TQImageIO *iio ) +{ + TQIODevice *d = iio->ioDevice(); + TQImage image = iio->image(); + TQDataStream s( d ); + BMP_FILEHDR bf; + int bpl_bmp; + int bpl = image.bytesPerLine(); + + // Code partially repeated in qt_write_dib + if ( image.depth() == 8 && image.numColors() <= 16 ) { + bpl_bmp = (((bpl+1)/2+3)/4)*4; + } else if ( image.depth() == 32 ) { + bpl_bmp = ((image.width()*24+31)/32)*4; + } else { + bpl_bmp = bpl; + } + + iio->setStatus( 0 ); + s.setByteOrder( TQDataStream::LittleEndian );// Intel byte order + strncpy( bf.bfType, "BM", 2 ); // build file header + bf.bfReserved1 = bf.bfReserved2 = 0; // reserved, should be zero + bf.bfOffBits = BMP_FILEHDR_SIZE + BMP_WIN + image.numColors()*4; + bf.bfSize = bf.bfOffBits + bpl_bmp*image.height(); + s << bf; // write file header + + if ( !qt_write_dib( s, image ) ) + iio->setStatus( 1 ); + +} + +#endif // QT_NO_IMAGEIO_BMP + +#ifndef QT_NO_IMAGEIO_PPM + +/***************************************************************************** + PBM/PGM/PPM (ASCII and RAW) image read/write functions + *****************************************************************************/ + +static int read_pbm_int( TQIODevice *d ) +{ + int c; + int val = -1; + bool digit; + const int buflen = 100; + char buf[buflen]; + for ( ;; ) { + if ( (c=d->getch()) == EOF ) // end of file + break; + digit = isdigit( (uchar) c ); + if ( val != -1 ) { + if ( digit ) { + val = 10*val + c - '0'; + continue; + } else { + if ( c == '#' ) // comment + d->readLine( buf, buflen ); + break; + } + } + if ( digit ) // first digit + val = c - '0'; + else if ( isspace((uchar) c) ) + continue; + else if ( c == '#' ) + d->readLine( buf, buflen ); + else + break; + } + return val; +} + +static void read_pbm_image( TQImageIO *iio ) // read PBM image data +{ + const int buflen = 300; + char buf[buflen]; + TQIODevice *d = iio->ioDevice(); + int w, h, nbits, mcc, y; + int pbm_bpl; + char type; + bool raw; + TQImage image; + + if ( d->readBlock( buf, 3 ) != 3 ) // read P[1-6] + return; + if ( !(buf[0] == 'P' && isdigit((uchar) buf[1]) && isspace((uchar) buf[2])) ) + return; + switch ( (type=buf[1]) ) { + case '1': // ascii PBM + case '4': // raw PBM + nbits = 1; + break; + case '2': // ascii PGM + case '5': // raw PGM + nbits = 8; + break; + case '3': // ascii PPM + case '6': // raw PPM + nbits = 32; + break; + default: + return; + } + raw = type >= '4'; + w = read_pbm_int( d ); // get image width + h = read_pbm_int( d ); // get image height + if ( nbits == 1 ) + mcc = 1; // ignore max color component + else + mcc = read_pbm_int( d ); // get max color component + if ( w <= 0 || w > 32767 || h <= 0 || h > 32767 || mcc <= 0 ) + return; // weird P.M image + + int maxc = mcc; + if ( maxc > 255 ) + maxc = 255; + image.create( w, h, nbits, 0, + nbits == 1 ? TQImage::BigEndian : TQImage::IgnoreEndian ); + if ( image.isNull() ) + return; + + pbm_bpl = (nbits*w+7)/8; // bytes per scanline in PBM + + if ( raw ) { // read raw data + if ( nbits == 32 ) { // type 6 + pbm_bpl = 3*w; + uchar *buf24 = new uchar[pbm_bpl], *b; + TQRgb *p; + TQRgb *end; + for ( y=0; yreadBlock( (char *)buf24, pbm_bpl ) != pbm_bpl ) { + delete[] buf24; + return; + } + p = (TQRgb *)image.scanLine( y ); + end = p + w; + b = buf24; + while ( p < end ) { + *p++ = qRgb(b[0],b[1],b[2]); + b += 3; + } + } + delete[] buf24; + } else { // type 4,5 + for ( y=0; yreadBlock( (char *)image.scanLine(y), pbm_bpl ) + != pbm_bpl ) + return; + } + } + } else { // read ascii data + register uchar *p; + int n; + for ( y=0; ysetImage( image ); + iio->setStatus( 0 ); // image ok +} + + +static void write_pbm_image( TQImageIO *iio ) +{ + TQIODevice* out = iio->ioDevice(); + TQCString str; + + TQImage image = iio->image(); + TQCString format = iio->format(); + format = format.left(3); // ignore RAW part + bool gray = format == "PGM"; + + if ( format == "PBM" ) { + image = image.convertDepth(1); + } else if ( image.depth() == 1 ) { + image = image.convertDepth(8); + } + + if ( image.depth() == 1 && image.numColors() == 2 ) { + if ( qGray(image.color(0)) < qGray(image.color(1)) ) { + // 0=dark/black, 1=light/white - invert + image.detach(); + for ( int y=0; ywriteBlock(str, str.length()) != str.length()) { + iio->setStatus(1); + return; + } + w = (w+7)/8; + for (uint y=0; ywriteBlock((char*)line, w) ) { + iio->setStatus(1); + return; + } + } + } + break; + + case 8: { + str.insert(1, gray ? '5' : '6'); + str.append("255\n"); + if ((uint)out->writeBlock(str, str.length()) != str.length()) { + iio->setStatus(1); + return; + } + TQRgb *color = image.colorTable(); + uint bpl = w*(gray ? 1 : 3); + uchar *buf = new uchar[bpl]; + for (uint y=0; ywriteBlock((char*)buf, bpl) ) { + iio->setStatus(1); + return; + } + } + delete [] buf; + } + break; + + case 32: { + str.insert(1, gray ? '5' : '6'); + str.append("255\n"); + if ((uint)out->writeBlock(str, str.length()) != str.length()) { + iio->setStatus(1); + return; + } + uint bpl = w*(gray ? 1 : 3); + uchar *buf = new uchar[bpl]; + for (uint y=0; ywriteBlock((char*)buf, bpl) ) { + iio->setStatus(1); + return; + } + } + delete [] buf; + } + } + + iio->setStatus(0); +} + +#endif // QT_NO_IMAGEIO_PPM + +#ifndef QT_NO_ASYNC_IMAGE_IO + +class TQImageIOFrameGrabber : public TQImageConsumer { +public: + TQImageIOFrameGrabber() : framecount(0) { } + + TQImageDecoder *decoder; + int framecount; + + void changed(const TQRect&) { } + void end() { } + void frameDone(const TQPoint&, const TQRect&) { framecount++; } + void frameDone() { framecount++; } + void setLooping(int) { } + void setFramePeriod(int) { } + void setSize(int, int) { } +}; + +static void read_async_image( TQImageIO *iio ) +{ + const int buf_len = 2048; + uchar buffer[buf_len]; + TQIODevice *d = iio->ioDevice(); + TQImageIOFrameGrabber* consumer = new TQImageIOFrameGrabber(); + TQImageDecoder *decoder = new TQImageDecoder(consumer); + consumer->decoder = decoder; + int startAt = d->at(); + int totLen = 0; + + for (;;) { + int length = d->readBlock((char*)buffer, buf_len); + if ( length <= 0 ) { + iio->setStatus(length); + break; + } + uchar* b = buffer; + int r = -1; + while (length > 0 && consumer->framecount==0) { + r = decoder->decode(b, length); + if ( r <= 0 ) break; + b += r; + totLen += r; + length -= r; + } + if ( consumer->framecount ) { + // Stopped after first frame + if ( d->isDirectAccess() ) + d->at( startAt + totLen ); + else { + // ### We have (probably) read too much from the stream into + // the buffer, and there is no way to put it back! + } + iio->setImage(decoder->image()); + iio->setStatus(0); + break; + } + if ( r <= 0 ) { + iio->setStatus(r); + break; + } + } + + consumer->decoder = 0; + delete decoder; + delete consumer; +} + +#endif // QT_NO_ASYNC_IMAGE_IO + +#ifndef QT_NO_IMAGEIO_XBM + +/***************************************************************************** + X bitmap image read/write functions + *****************************************************************************/ + +static inline int hex2byte( register char *p ) +{ + return ( (isdigit((uchar) *p) ? *p - '0' : toupper((uchar) *p) - 'A' + 10) << 4 ) | + ( isdigit((uchar) *(p+1)) ? *(p+1) - '0' : toupper((uchar) *(p+1)) - 'A' + 10 ); +} + +static void read_xbm_image( TQImageIO *iio ) +{ + const int buflen = 300; + char buf[buflen]; + TQRegExp r1, r2; + TQIODevice *d = iio->ioDevice(); + int w=-1, h=-1; + TQImage image; + + r1 = TQString::fromLatin1("^#define[ \t]+[a-zA-Z0-9._]+[ \t]+"); + r2 = TQString::fromLatin1("[0-9]+"); + d->readLine( buf, buflen ); // "#define .._width " + + while (!d->atEnd() && buf[0] != '#') //skip leading comment, if any + d->readLine( buf, buflen ); + + TQString sbuf; + sbuf = TQString::fromLatin1(buf); + + if ( r1.search(sbuf) == 0 && + r2.search(sbuf, r1.matchedLength()) == r1.matchedLength() ) + w = atoi( &buf[r1.matchedLength()] ); + + d->readLine( buf, buflen ); // "#define .._height " + sbuf = TQString::fromLatin1(buf); + + if ( r1.search(sbuf) == 0 && + r2.search(sbuf, r1.matchedLength()) == r1.matchedLength() ) + h = atoi( &buf[r1.matchedLength()] ); + + if ( w <= 0 || w > 32767 || h <= 0 || h > 32767 ) + return; // format error + + for ( ;; ) { // scan for data + if ( d->readLine(buf, buflen) <= 0 ) // end of file + return; + if ( strstr(buf,"0x") != 0 ) // does line contain data? + break; + } + + image.create( w, h, 1, 2, TQImage::LittleEndian ); + if ( image.isNull() ) + return; + + image.setColor( 0, qRgb(255,255,255) ); // white + image.setColor( 1, qRgb(0,0,0) ); // black + + int x = 0, y = 0; + uchar *b = image.scanLine(0); + char *p = strstr( buf, "0x" ); + w = (w+7)/8; // byte width + + while ( y < h ) { // for all encoded bytes... + if ( p ) { // p = "0x.." + *b++ = hex2byte(p+2); + p += 2; + if ( ++x == w && ++y < h ) { + b = image.scanLine(y); + x = 0; + } + p = strstr( p, "0x" ); + } else { // read another line + if ( d->readLine(buf,buflen) <= 0 ) // EOF ==> truncated image + break; + p = strstr( buf, "0x" ); + } + } + + iio->setImage( image ); + iio->setStatus( 0 ); // image ok +} + + +static void write_xbm_image( TQImageIO *iio ) +{ + TQIODevice *d = iio->ioDevice(); + TQImage image = iio->image(); + int w = image.width(); + int h = image.height(); + int i; + TQString s = fbname(iio->fileName()); // get file base name + char *buf = new char[s.length() + 100]; + + sprintf( buf, "#define %s_width %d\n", s.ascii(), w ); + d->writeBlock( buf, qstrlen(buf) ); + sprintf( buf, "#define %s_height %d\n", s.ascii(), h ); + d->writeBlock( buf, qstrlen(buf) ); + sprintf( buf, "static char %s_bits[] = {\n ", s.ascii() ); + d->writeBlock( buf, qstrlen(buf) ); + + iio->setStatus( 0 ); + + if ( image.depth() != 1 ) + image = image.convertDepth( 1 ); // dither + if ( image.bitOrder() != TQImage::LittleEndian ) + image = image.convertBitOrder( TQImage::LittleEndian ); + + bool invert = qGray(image.color(0)) < qGray(image.color(1)); + char hexrep[16]; + for ( i=0; i<10; i++ ) + hexrep[i] = '0' + i; + for ( i=10; i<16; i++ ) + hexrep[i] = 'a' -10 + i; + if ( invert ) { + char t; + for ( i=0; i<8; i++ ) { + t = hexrep[15-i]; + hexrep[15-i] = hexrep[i]; + hexrep[i] = t; + } + } + int bcnt = 0; + register char *p = buf; + int bpl = (w+7)/8; + for (int y = 0; y < h; ++y) { + uchar *b = image.scanLine(y); + for (i = 0; i < bpl; ++i) { + *p++ = '0'; *p++ = 'x'; + *p++ = hexrep[*b >> 4]; + *p++ = hexrep[*b++ & 0xf]; + + if ( i < bpl - 1 || y < h - 1 ) { + *p++ = ','; + if ( ++bcnt > 14 ) { + *p++ = '\n'; + *p++ = ' '; + *p = '\0'; + if ( (int)qstrlen(buf) != d->writeBlock( buf, qstrlen(buf) ) ) { + iio->setStatus( 1 ); + delete [] buf; + return; + } + p = buf; + bcnt = 0; + } + } + } + } + strcpy( p, " };\n" ); + if ( (int)qstrlen(buf) != d->writeBlock( buf, qstrlen(buf) ) ) + iio->setStatus( 1 ); + delete [] buf; +} + +#endif // QT_NO_IMAGEIO_XBM + + +#ifndef QT_NO_IMAGEIO_XPM + +/***************************************************************************** + XPM image read/write functions + *****************************************************************************/ + + +// Skip until ", read until the next ", return the rest in *buf +// Returns FALSE on error, TRUE on success + +static bool read_xpm_string( TQCString &buf, TQIODevice *d, + const char * const *source, int &index ) +{ + if ( source ) { + buf = source[index++]; + return TRUE; + } + + if ( buf.size() < 69 ) //# just an approximation + buf.resize( 123 ); + + buf[0] = '\0'; + int c; + int i; + while ( (c=d->getch()) != EOF && c != '"' ) { } + if ( c == EOF ) { + return FALSE; + } + i = 0; + while ( (c=d->getch()) != EOF && c != '"' ) { + if ( i == (int)buf.size() ) + buf.resize( i*2+42 ); + buf[i++] = c; + } + if ( c == EOF ) { + return FALSE; + } + + if ( i == (int)buf.size() ) // always use a 0 terminator + buf.resize( i+1 ); + buf[i] = '\0'; + return TRUE; +} + + + +static int nextColorSpec(const TQCString & buf) +{ + int i = buf.find(" c "); + if (i < 0) + i = buf.find(" g "); + if (i < 0) + i = buf.find(" g4 "); + if (i < 0) + i = buf.find(" m "); + if (i < 0) + i = buf.find(" s "); + return i; +} + +// +// INTERNAL +// +// Reads an .xpm from either the TQImageIO or from the TQString *. +// One of the two HAS to be 0, the other one is used. +// + +static void read_xpm_image_or_array( TQImageIO * iio, const char * const * source, + TQImage & image) +{ + TQCString buf; + TQIODevice *d = 0; + buf.resize( 200 ); + + int i, cpp, ncols, w, h, index = 0; + + if ( iio ) { + iio->setStatus( 1 ); + d = iio ? iio->ioDevice() : 0; + d->readLine( buf.data(), buf.size() ); // "/* XPM */" + TQRegExp r( TQString::fromLatin1("/\\*.XPM.\\*/") ); + if ( buf.find(r) == -1 ) + return; // bad magic + } else if ( !source ) { + return; + } + + if ( !read_xpm_string( buf, d, source, index ) ) + return; + + if ( sscanf( buf, "%d %d %d %d", &w, &h, &ncols, &cpp ) < 4 ) + return; // < 4 numbers parsed + + if ( cpp > 15 ) + return; + + if ( ncols > 256 ) { + image.create( w, h, 32 ); + } else { + image.create( w, h, 8, ncols ); + } + + if (image.isNull()) + return; + + TQMap colorMap; + int currentColor; + + for( currentColor=0; currentColor < ncols; ++currentColor ) { + if ( !read_xpm_string( buf, d, source, index ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage: XPM color specification missing"); +#endif + return; + } + TQString index; + index = buf.left( cpp ); + buf = buf.mid( cpp ).simplifyWhiteSpace().lower(); + buf.prepend( " " ); + i = nextColorSpec(buf); + if ( i < 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQImage: XPM color specification is missing: %s", buf.data()); +#endif + return; // no c/g/g4/m/s specification at all + } + buf = buf.mid( i+3 ); + // Strip any other colorspec + int end = nextColorSpec(buf); + if (end != -1) + buf.truncate(end); + buf = buf.stripWhiteSpace(); + if ( buf == "none" ) { + image.setAlphaBuffer( TRUE ); + int transparentColor = currentColor; + if ( image.depth() == 8 ) { + image.setColor( transparentColor, + RGB_MASK & qRgb(198,198,198) ); + colorMap.insert( index, transparentColor ); + } else { + TQRgb rgb = RGB_MASK & qRgb(198,198,198); + colorMap.insert( index, rgb ); + } + } else { + if ( ((buf.length()-1) % 3) && (buf[0] == '#') ) { + buf.truncate (((buf.length()-1) / 4 * 3) + 1); // remove alpha channel left by imagemagick + } + TQColor c( buf.data() ); + if ( image.depth() == 8 ) { + image.setColor( currentColor, 0xff000000 | c.rgb() ); + colorMap.insert( index, currentColor ); + } else { + TQRgb rgb = 0xff000000 | c.rgb(); + colorMap.insert( index, rgb ); + } + } + } + + // Read pixels + for( int y=0; ysetImage( image ); + iio->setStatus( 0 ); // image ok + } +} + + +static void read_xpm_image( TQImageIO * iio ) +{ + TQImage i; + (void)read_xpm_image_or_array( iio, 0, i ); + return; +} + + +static const char* xpm_color_name( int cpp, int index ) +{ + static char returnable[5]; + static const char code[] = ".#abcdefghijklmnopqrstuvwxyzABCD" + "EFGHIJKLMNOPTQRSTUVWXYZ0123456789"; + // cpp is limited to 4 and index is limited to 64^cpp + if ( cpp > 1 ) { + if ( cpp > 2 ) { + if ( cpp > 3 ) { + returnable[3] = code[index % 64]; + index /= 64; + } else + returnable[3] = '\0'; + returnable[2] = code[index % 64]; + index /= 64; + } else + returnable[2] = '\0'; + // the following 4 lines are a joke! + if ( index == 0 ) + index = 64*44+21; + else if ( index == 64*44+21 ) + index = 0; + returnable[1] = code[index % 64]; + index /= 64; + } else + returnable[1] = '\0'; + returnable[0] = code[index]; + + return returnable; +} + + +// write XPM image data +static void write_xpm_image( TQImageIO * iio ) +{ + if ( iio ) + iio->setStatus( 1 ); + else + return; + + // ### 8-bit case could be made faster + TQImage image; + if ( iio->image().depth() != 32 ) + image = iio->image().convertDepth( 32 ); + else + image = iio->image(); + + TQMap colorMap; + + int w = image.width(), h = image.height(), ncolors = 0; + int x, y; + + // build color table + for( y=0; y k; k *= 64 ) { + ++cpp; + // limit to 4 characters per pixel + // 64^4 colors is enough for a 4096x4096 image + if ( cpp > 4) + break; + } + + TQString line; + + // write header + TQTextStream s( iio->ioDevice() ); + s << "/* XPM */" << endl + << "static char *" << fbname(iio->fileName()) << "[]={" << endl + << "\"" << w << " " << h << " " << ncolors << " " << cpp << "\""; + + // write palette + TQMap::Iterator c = colorMap.begin(); + while ( c != colorMap.end() ) { + TQRgb color = c.key(); + if ( image.hasAlphaBuffer() && color == (color & RGB_MASK) ) + line.sprintf( "\"%s c None\"", + xpm_color_name(cpp, *c) ); + else + line.sprintf( "\"%s c #%02x%02x%02x\"", + xpm_color_name(cpp, *c), + qRed(color), + qGreen(color), + qBlue(color) ); + ++c; + s << "," << endl << line; + } + + // write pixels, limit to 4 characters per pixel + line.truncate( cpp*w ); + for( y=0; y 1 ) { + line[cc++] = chars[1]; + if ( cpp > 2 ) { + line[cc++] = chars[2]; + if ( cpp > 3 ) { + line[cc++] = chars[3]; + } + } + } + } + s << "," << endl << "\"" << line << "\""; + } + s << "};" << endl; + + iio->setStatus( 0 ); +} + +#endif // QT_NO_IMAGEIO_XPM + +/*! + Returns an image with depth \a d, using the \a palette_count + colors pointed to by \a palette. If \a d is 1 or 8, the returned + image will have its color table ordered the same as \a palette. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + Note: currently no closest-color search is made. If colors are + found that are not in the palette, the palette may not be used at + all. This result should not be considered valid because it may + change in future implementations. + + Currently inefficient for non-32-bit images. + + \sa TQt::ImageConversionFlags +*/ +#ifndef QT_NO_IMAGE_TRUECOLOR +TQImage TQImage::convertDepthWithPalette( int d, TQRgb* palette, int palette_count, int conversion_flags ) const +{ + if ( depth() == 1 ) { + return convertDepth( 8, conversion_flags ) + .convertDepthWithPalette( d, palette, palette_count, conversion_flags ); + } else if ( depth() == 8 ) { + // ### this could be easily made more efficient + return convertDepth( 32, conversion_flags ) + .convertDepthWithPalette( d, palette, palette_count, conversion_flags ); + } else { + TQImage result; + convert_32_to_8( this, &result, + (conversion_flags&~TQt::DitherMode_Mask) | TQt::AvoidDither, + palette, palette_count ); + return result.convertDepth( d ); + } +} +#endif +static +bool +haveSamePalette(const TQImage& a, const TQImage& b) +{ + if (a.depth() != b.depth()) return FALSE; + if (a.numColors() != b.numColors()) return FALSE; + TQRgb* ca = a.colorTable(); + TQRgb* cb = b.colorTable(); + for (int i=a.numColors(); i--; ) { + if (*ca++ != *cb++) return FALSE; + } + return TRUE; +} + +/*! + \relates TQImage + + Copies a block of pixels from \a src to \a dst. The pixels + copied from source (src) are converted according to + \a conversion_flags if it is incompatible with the destination + (\a dst). + + \a sx, \a sy is the top-left pixel in \a src, \a dx, \a dy + is the top-left position in \a dst and \a sw, \a sh is the + size of the copied block. + + The copying is clipped if areas outside \a src or \a dst are + specified. + + If \a sw is -1, it is adjusted to src->width(). Similarly, if \a + sh is -1, it is adjusted to src->height(). + + Currently inefficient for non 32-bit images. +*/ +void bitBlt( TQImage* dst, int dx, int dy, const TQImage* src, + int sx, int sy, int sw, int sh, int conversion_flags ) +{ + // Parameter correction + if ( sw < 0 ) sw = src->width(); + if ( sh < 0 ) sh = src->height(); + if ( sx < 0 ) { dx -= sx; sw += sx; sx = 0; } + if ( sy < 0 ) { dy -= sy; sh += sy; sy = 0; } + if ( dx < 0 ) { sx -= dx; sw += dx; dx = 0; } + if ( dy < 0 ) { sy -= dy; sh += dy; dy = 0; } + if ( sx + sw > src->width() ) sw = src->width() - sx; + if ( sy + sh > src->height() ) sh = src->height() - sy; + if ( dx + sw > dst->width() ) sw = dst->width() - dx; + if ( dy + sh > dst->height() ) sh = dst->height() - dy; + if ( sw <= 0 || sh <= 0 ) return; // Nothing left to copy + if ( (dst->data == src->data) && dx==sx && dy==sy ) return; // Same pixels + + // "Easy" to copy if both same depth and one of: + // - 32 bit + // - 8 bit, identical palette + // - 1 bit, identical palette and byte-aligned area + // + if ( haveSamePalette(*dst,*src) + && ( dst->depth() != 1 || + !( (dx&7) || (sx&7) || + ((sw&7) && (sx+sw < src->width()) || + (dx+sw < dst->width()) ) ) ) ) + { + // easy to copy + } else if ( dst->depth() != 32 ) { +#ifndef QT_NO_IMAGE_TRUECOLOR + + TQImage dstconv = dst->convertDepth( 32 ); + bitBlt( &dstconv, dx, dy, src, sx, sy, sw, sh, + (conversion_flags&~TQt::DitherMode_Mask) | TQt::AvoidDither ); + *dst = dstconv.convertDepthWithPalette( dst->depth(), + dst->colorTable(), dst->numColors() ); +#endif + return; + } + + // Now assume palette can be ignored + + if ( dst->depth() != src->depth() ) { + if ( sw == src->width() && sh == src->height() || dst->depth()==32 ) { + TQImage srcconv = src->convertDepth( dst->depth(), conversion_flags ); + bitBlt( dst, dx, dy, &srcconv, sx, sy, sw, sh, conversion_flags ); + } else { + TQImage srcconv = src->copy( sx, sy, sw, sh ); // ie. bitBlt + bitBlt( dst, dx, dy, &srcconv, 0, 0, sw, sh, conversion_flags ); + } + return; + } + + // Now assume both are the same depth. + + // Now assume both are 32-bit or 8-bit with compatible palettes. + + // "Easy" + + switch ( dst->depth() ) { + case 1: + { + uchar* d = dst->scanLine(dy) + dx/8; + uchar* s = src->scanLine(sy) + sx/8; + const int bw = (sw+7)/8; + if ( bw < 64 ) { + // Trust ourselves + const int dd = dst->bytesPerLine() - bw; + const int ds = src->bytesPerLine() - bw; + while ( sh-- ) { + for ( int t=bw; t--; ) + *d++ = *s++; + d += dd; + s += ds; + } + } else { + // Trust libc + const int dd = dst->bytesPerLine(); + const int ds = src->bytesPerLine(); + while ( sh-- ) { + memcpy( d, s, bw ); + d += dd; + s += ds; + } + } + } + break; + case 8: + { + uchar* d = dst->scanLine(dy) + dx; + uchar* s = src->scanLine(sy) + sx; + if ( sw < 64 ) { + // Trust ourselves + const int dd = dst->bytesPerLine() - sw; + const int ds = src->bytesPerLine() - sw; + while ( sh-- ) { + for ( int t=sw; t--; ) + *d++ = *s++; + d += dd; + s += ds; + } + } else { + // Trust libc + const int dd = dst->bytesPerLine(); + const int ds = src->bytesPerLine(); + while ( sh-- ) { + memcpy( d, s, sw ); + d += dd; + s += ds; + } + } + } + break; +#ifndef QT_NO_IMAGE_TRUECOLOR + case 32: + if ( src->hasAlphaBuffer() ) { + TQRgb* d = (TQRgb*)dst->scanLine(dy) + dx; + TQRgb* s = (TQRgb*)src->scanLine(sy) + sx; + const int dd = dst->width() - sw; + const int ds = src->width() - sw; + while ( sh-- ) { + for ( int t=sw; t--; ) { + unsigned char a = qAlpha(*s); + if ( a == 255 ) + *d++ = *s++; + else if ( a == 0 ) + ++d,++s; // nothing + else { + unsigned char r = ((qRed(*s)-qRed(*d)) * a) / 256 + qRed(*d); + unsigned char g = ((qGreen(*s)-qGreen(*d)) * a) / 256 + qGreen(*d); + unsigned char b = ((qBlue(*s)-qBlue(*d)) * a) / 256 + qBlue(*d); + a = TQMAX(qAlpha(*d),a); // alternatives... + *d++ = qRgba(r,g,b,a); + ++s; + } + } + d += dd; + s += ds; + } + } else { + TQRgb* d = (TQRgb*)dst->scanLine(dy) + dx; + TQRgb* s = (TQRgb*)src->scanLine(sy) + sx; + if ( sw < 64 ) { + // Trust ourselves + const int dd = dst->width() - sw; + const int ds = src->width() - sw; + while ( sh-- ) { + for ( int t=sw; t--; ) + *d++ = *s++; + d += dd; + s += ds; + } + } else { + // Trust libc + const int dd = dst->width(); + const int ds = src->width(); + const int b = sw*sizeof(TQRgb); + while ( sh-- ) { + memcpy( d, s, b ); + d += dd; + s += ds; + } + } + } + break; +#endif // QT_NO_IMAGE_TRUECOLOR + } +} + + +/*! + Returns TRUE if this image and image \a i have the same contents; + otherwise returns FALSE. The comparison can be slow, unless there + is some obvious difference, such as different widths, in which + case the function will return tquickly. + + \sa operator=() +*/ + +bool TQImage::operator==( const TQImage & i ) const +{ + // same object, or shared? + if ( i.data == data ) + return TRUE; + // obviously different stuff? + if ( i.data->h != data->h || + i.data->w != data->w ) + return FALSE; + // not equal if one has alphabuffer and the other does not + if ( i.hasAlphaBuffer() != hasAlphaBuffer() ) + return FALSE; + // that was the fast bit... + TQImage i1 = convertDepth( 32 ); + TQImage i2 = i.convertDepth( 32 ); + int l; + // if no alpha buffer used, there might still be junk in the + // alpha bits; thus, we can't do memcmp-style comparison of scanlines + if ( !hasAlphaBuffer() ) { + int m; + TQRgb *i1line; + TQRgb *i2line; + for( l=0; l < data->h; l++ ) { + i1line = (uint *)i1.scanLine( l ); + i2line = (uint *)i2.scanLine( l ); + // compare pixels of scanline individually + for ( m=0; m < data->w; m++ ) + if ( (i1line[m] ^ i2line[m]) & 0x00FFFFFF ) + return FALSE; + } + } else { + // yay, we can do fast binary comparison on entire scanlines + for( l=0; l < data->h; l++ ) + if ( memcmp( i1.scanLine( l ), i2.scanLine( l ), 4*data->w ) ) + return FALSE; + } + return TRUE; +} + + +/*! + Returns TRUE if this image and image \a i have different contents; + otherwise returns FALSE. The comparison can be slow, unless there + is some obvious difference, such as different widths, in which + case the function will return tquickly. + + \sa operator=() +*/ + +bool TQImage::operator!=( const TQImage & i ) const +{ + return !(*this == i); +} + + + + +/*! + \fn int TQImage::dotsPerMeterX() const + + Returns the number of pixels that fit horizontally in a physical + meter. This and dotsPerMeterY() define the intended scale and + aspect ratio of the image. + + \sa setDotsPerMeterX() +*/ + +/*! + \fn int TQImage::dotsPerMeterY() const + + Returns the number of pixels that fit vertically in a physical + meter. This and dotsPerMeterX() define the intended scale and + aspect ratio of the image. + + \sa setDotsPerMeterY() +*/ + +/*! + Sets the value returned by dotsPerMeterX() to \a x. +*/ +void TQImage::setDotsPerMeterX(int x) +{ + data->dpmx = x; +} + +/*! + Sets the value returned by dotsPerMeterY() to \a y. +*/ +void TQImage::setDotsPerMeterY(int y) +{ + data->dpmy = y; +} + +/*! + \fn TQPoint TQImage::offset() const + + Returns the number of pixels by which the image is intended to be + offset by when positioning relative to other images. +*/ + +/*! + Sets the value returned by offset() to \a p. +*/ +void TQImage::setOffset(const TQPoint& p) +{ + data->offset = p; +} +#ifndef QT_NO_IMAGE_TEXT +/*! + \internal + + Returns the internal TQImageDataMisc object. This object will be + created if it doesn't already exist. +*/ +TQImageDataMisc& TQImage::misc() const +{ + if ( !data->misc ) + data->misc = new TQImageDataMisc; + return *data->misc; +} + +/*! + Returns the string recorded for the keyword \a key in language \a + lang, or in a default language if \a lang is 0. +*/ +TQString TQImage::text(const char* key, const char* lang) const +{ + TQImageTextKeyLang x(key,lang); + return misc().text_lang[x]; +} + +/*! + \overload + + Returns the string recorded for the keyword and language \a kl. +*/ +TQString TQImage::text(const TQImageTextKeyLang& kl) const +{ + return misc().text_lang[kl]; +} + +/*! + Returns the language identifiers for which some texts are + recorded. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + TQStringList list = myImage.textLanguages(); + TQStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa textList() text() setText() textKeys() +*/ +TQStringList TQImage::textLanguages() const +{ + if ( !data->misc ) + return TQStringList(); + return misc().languages(); +} + +/*! + Returns the keywords for which some texts are recorded. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + TQStringList list = myImage.textKeys(); + TQStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa textList() text() setText() textLanguages() +*/ +TQStringList TQImage::textKeys() const +{ + if ( !data->misc ) + return TQStringList(); + return misc().keys(); +} + +/*! + Returns a list of TQImageTextKeyLang objects that enumerate all the + texts key/language pairs set by setText() for this image. + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + TQValueList list = myImage.textList(); + TQValueList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode +*/ +TQValueList TQImage::textList() const +{ + if ( !data->misc ) + return TQValueList(); + return misc().list(); +} + +/*! + Records string \a s for the keyword \a key. The \a key should be a + portable keyword recognizable by other software - some suggested + values can be found in \link + http://www.libpng.org/pub/png/spec/1.2/png-1.2-pdg.html#C.Anc-text + the PNG specification \endlink. \a s can be any text. \a lang + should specify the language code (see + \link http://www.rfc-editor.org/rfc/rfc1766.txt RFC 1766 \endlink) or 0. +*/ +void TQImage::setText(const char* key, const char* lang, const TQString& s) +{ + TQImageTextKeyLang x(key,lang); + misc().text_lang.replace(x,s); +} + +#endif // QT_NO_IMAGE_TEXT + +#ifdef Q_WS_QWS +/*! + \internal +*/ +TQGfx * TQImage::graphicsContext() +{ + TQGfx * ret=0; + if(depth()) { + int w = qt_screen->mapToDevice( TQSize(width(),height()) ).width(); + int h = qt_screen->mapToDevice( TQSize(width(),height()) ).height(); + ret=TQGfx::createGfx(depth(),bits(),w,h,bytesPerLine()); + } else { + qDebug("Trying to create image for null depth"); + return 0; + } + if(depth()<=8) { + TQRgb * tmp=colorTable(); + int nc=numColors(); + if(tmp==0) { + static TQRgb table[2] = { qRgb(255,255,255), qRgb(0,0,0) }; + tmp=table; + nc=2; + } + ret->setClut(tmp,nc); + } + return ret; +} + +#endif diff --git a/src/kernel/qimage.h b/src/kernel/qimage.h new file mode 100644 index 000000000..89533522a --- /dev/null +++ b/src/kernel/qimage.h @@ -0,0 +1,425 @@ +/**************************************************************************** +** +** Definition of TQImage and TQImageIO classes +** +** Created : 950207 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQIMAGE_H +#define TQIMAGE_H + +#ifndef QT_H +#include "qpixmap.h" +#include "qstrlist.h" +#include "qstringlist.h" +#endif // QT_H + +class TQImageDataMisc; // internal +#ifndef QT_NO_IMAGE_TEXT +class Q_EXPORT TQImageTextKeyLang { +public: + TQImageTextKeyLang(const char* k, const char* l) : key(k), lang(l) { } + TQImageTextKeyLang() { } + + TQCString key; + TQCString lang; + + bool operator< (const TQImageTextKeyLang& other) const + { return key < other.key || key==other.key && lang < other.lang; } + bool operator== (const TQImageTextKeyLang& other) const + { return key==other.key && lang==other.lang; } +}; +#endif //QT_NO_IMAGE_TEXT + + +class Q_EXPORT TQImage +{ +public: + enum Endian { IgnoreEndian, BigEndian, LittleEndian }; + + TQImage(); + TQImage( int width, int height, int depth, int numColors=0, + Endian bitOrder=IgnoreEndian ); + TQImage( const TQSize&, int depth, int numColors=0, + Endian bitOrder=IgnoreEndian ); +#ifndef QT_NO_IMAGEIO + TQImage( const TQString &fileName, const char* format=0 ); + TQImage( const char * const xpm[] ); + TQImage( const TQByteArray &data ); +#endif + TQImage( uchar* data, int w, int h, int depth, + TQRgb* colortable, int numColors, + Endian bitOrder ); +#ifdef Q_WS_QWS + TQImage( uchar* data, int w, int h, int depth, int pbl, + TQRgb* colortable, int numColors, + Endian bitOrder ); +#endif + TQImage( const TQImage & ); + ~TQImage(); + + TQImage &operator=( const TQImage & ); + TQImage &operator=( const TQPixmap & ); + bool operator==( const TQImage & ) const; + bool operator!=( const TQImage & ) const; + void detach(); + TQImage copy() const; + TQImage copy(int x, int y, int w, int h, int conversion_flags=0) const; + TQImage copy(const TQRect&) const; +#ifndef QT_NO_MIME + static TQImage fromMimeSource( const TQString& abs_name ); +#endif + bool isNull() const { return data->bits == 0; } + + int width() const { return data->w; } + int height() const { return data->h; } + TQSize size() const { return TQSize(data->w,data->h); } + TQRect rect() const { return TQRect(0,0,data->w,data->h); } + int depth() const { return data->d; } + int numColors() const { return data->ncols; } + Endian bitOrder() const { return (Endian) data->bitordr; } + + TQRgb color( int i ) const; + void setColor( int i, TQRgb c ); + void setNumColors( int ); + + bool hasAlphaBuffer() const; + void setAlphaBuffer( bool ); + + bool allGray() const; + bool isGrayscale() const; + + uchar *bits() const; + uchar *scanLine( int ) const; + uchar **jumpTable() const; + TQRgb *colorTable() const; + int numBytes() const; + int bytesPerLine() const; + +#ifdef Q_WS_QWS + TQGfx * graphicsContext(); +#endif + + bool create( int width, int height, int depth, int numColors=0, + Endian bitOrder=IgnoreEndian ); + bool create( const TQSize&, int depth, int numColors=0, + Endian bitOrder=IgnoreEndian ); + void reset(); + + void fill( uint pixel ); + void invertPixels( bool invertAlpha = TRUE ); + + TQImage convertDepth( int ) const; +#ifndef QT_NO_IMAGE_TRUECOLOR + TQImage convertDepthWithPalette( int, TQRgb* p, int pc, int cf=0 ) const; +#endif + TQImage convertDepth( int, int conversion_flags ) const; + TQImage convertBitOrder( Endian ) const; + + enum ScaleMode { + ScaleFree, + ScaleMin, + ScaleMax + }; +#ifndef QT_NO_IMAGE_SMOOTHSCALE + TQImage smoothScale( int w, int h, ScaleMode mode=ScaleFree ) const; + TQImage smoothScale( const TQSize& s, ScaleMode mode=ScaleFree ) const; +#endif +#ifndef QT_NO_IMAGE_TRANSFORMATION + TQImage scale( int w, int h, ScaleMode mode=ScaleFree ) const; + TQImage scale( const TQSize& s, ScaleMode mode=ScaleFree ) const; + TQImage scaleWidth( int w ) const; + TQImage scaleHeight( int h ) const; + TQImage xForm( const TQWMatrix &matrix ) const; +#endif + +#ifndef QT_NO_IMAGE_DITHER_TO_1 + TQImage createAlphaMask( int conversion_flags=0 ) const; +#endif +#ifndef QT_NO_IMAGE_HEURISTIC_MASK + TQImage createHeuristicMask( bool clipTight=TRUE ) const; +#endif +#ifndef QT_NO_IMAGE_MIRROR + TQImage mirror() const; + TQImage mirror(bool horizontally, bool vertically) const; +#endif + TQImage swapRGB() const; + + static Endian systemBitOrder(); + static Endian systemByteOrder(); + +#ifndef QT_NO_IMAGEIO + static const char* imageFormat( const TQString &fileName ); + static TQStrList inputFormats(); + static TQStrList outputFormats(); +#ifndef QT_NO_STRINGLIST + static TQStringList inputFormatList(); + static TQStringList outputFormatList(); +#endif + bool load( const TQString &fileName, const char* format=0 ); + bool loadFromData( const uchar *buf, uint len, + const char *format=0 ); + bool loadFromData( TQByteArray data, const char* format=0 ); + bool save( const TQString &fileName, const char* format, + int quality=-1 ) const; + bool save( TQIODevice * device, const char* format, + int quality=-1 ) const; +#endif //QT_NO_IMAGEIO + + bool valid( int x, int y ) const; + int pixelIndex( int x, int y ) const; + TQRgb pixel( int x, int y ) const; + void setPixel( int x, int y, uint index_or_rgb ); + + // Auxiliary data + int dotsPerMeterX() const; + int dotsPerMeterY() const; + void setDotsPerMeterX(int); + void setDotsPerMeterY(int); + TQPoint offset() const; + void setOffset(const TQPoint&); +#ifndef QT_NO_IMAGE_TEXT + TQValueList textList() const; + TQStringList textLanguages() const; + TQStringList textKeys() const; + TQString text(const char* key, const char* lang=0) const; + TQString text(const TQImageTextKeyLang&) const; + void setText(const char* key, const char* lang, const TQString&); +#endif +private: + void init(); + void reinit(); + void freeBits(); + static void warningIndexRange( const char *, int ); + + struct TQImageData : public TQShared { // internal image data + int w; // image width + int h; // image height + int d; // image depth + int ncols; // number of colors + int nbytes; // number of bytes data + int bitordr; // bit order (1 bit depth) + TQRgb *ctbl; // color table + uchar **bits; // image data + bool alpha; // alpha buffer + int dpmx; // dots per meter X (or 0) + int dpmy; // dots per meter Y (or 0) + TQPoint offset; // offset in pixels +#ifndef QT_NO_IMAGE_TEXT + TQImageDataMisc* misc; // less common stuff +#endif + bool ctbl_mine; // this allocated ctbl + } *data; +#ifndef QT_NO_IMAGE_TEXT + TQImageDataMisc& misc() const; +#endif +#ifndef QT_NO_IMAGEIO + bool doImageIO( TQImageIO* io, int quality ) const; +#endif + friend Q_EXPORT void bitBlt( TQImage* dst, int dx, int dy, + const TQImage* src, int sx, int sy, + int sw, int sh, int conversion_flags ); +}; + + +// TQImage stream functions + +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQImage & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQImage & ); +#endif + +#ifndef QT_NO_IMAGEIO +class TQIODevice; +typedef void (*image_io_handler)( TQImageIO * ); // image IO handler + + +struct TQImageIOData; + + +class Q_EXPORT TQImageIO +{ +public: + TQImageIO(); + TQImageIO( TQIODevice *ioDevice, const char *format ); + TQImageIO( const TQString &fileName, const char* format ); + ~TQImageIO(); + + + const TQImage &image() const { return im; } + int status() const { return iostat; } + const char *format() const { return frmt; } + TQIODevice *ioDevice() const { return iodev; } + TQString fileName() const { return fname; } + int quality() const; + TQString description() const { return descr; } + const char *parameters() const; + float gamma() const; + + void setImage( const TQImage & ); + void setStatus( int ); + void setFormat( const char * ); + void setIODevice( TQIODevice * ); + void setFileName( const TQString & ); + void setQuality( int ); + void setDescription( const TQString & ); + void setParameters( const char * ); + void setGamma( float ); + + bool read(); + bool write(); + + static const char* imageFormat( const TQString &fileName ); + static const char *imageFormat( TQIODevice * ); + static TQStrList inputFormats(); + static TQStrList outputFormats(); + + static void defineIOHandler( const char *format, + const char *header, + const char *flags, + image_io_handler read_image, + image_io_handler write_image ); + +private: + void init(); + + TQImage im; // image + int iostat; // IO status + TQCString frmt; // image format + TQIODevice *iodev; // IO device + TQString fname; // file name + char *params; // image parameters //### change to TQImageIOData *d in 3.0 + TQString descr; // image description + TQImageIOData *d; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQImageIO( const TQImageIO & ); + TQImageIO &operator=( const TQImageIO & ); +#endif +}; + +#endif //QT_NO_IMAGEIO + +Q_EXPORT void bitBlt( TQImage* dst, int dx, int dy, const TQImage* src, + int sx=0, int sy=0, int sw=-1, int sh=-1, + int conversion_flags=0 ); + + +/***************************************************************************** + TQImage member functions + *****************************************************************************/ + +inline bool TQImage::hasAlphaBuffer() const +{ + return data->alpha; +} + +inline uchar *TQImage::bits() const +{ + return data->bits ? data->bits[0] : 0; +} + +inline uchar **TQImage::jumpTable() const +{ + return data->bits; +} + +inline TQRgb *TQImage::colorTable() const +{ + return data->ctbl; +} + +inline int TQImage::numBytes() const +{ + return data->nbytes; +} + +inline int TQImage::bytesPerLine() const +{ + return data->h ? data->nbytes/data->h : 0; +} + +inline TQImage TQImage::copy(const TQRect& r) const +{ + return copy(r.x(), r.y(), r.width(), r.height()); +} + +inline TQRgb TQImage::color( int i ) const +{ +#if defined(QT_CHECK_RANGE) + if ( i >= data->ncols ) + warningIndexRange( "color", i ); +#endif + return data->ctbl ? data->ctbl[i] : (TQRgb)-1; +} + +inline void TQImage::setColor( int i, TQRgb c ) +{ +#if defined(QT_CHECK_RANGE) + if ( i >= data->ncols ) + warningIndexRange( "setColor", i ); +#endif + if ( data->ctbl ) + data->ctbl[i] = c; +} + +inline uchar *TQImage::scanLine( int i ) const +{ +#if defined(QT_CHECK_RANGE) + if ( i >= data->h ) + warningIndexRange( "scanLine", i ); +#endif + return data->bits ? data->bits[i] : 0; +} + +inline int TQImage::dotsPerMeterX() const +{ + return data->dpmx; +} + +inline int TQImage::dotsPerMeterY() const +{ + return data->dpmy; +} + +inline TQPoint TQImage::offset() const +{ + return data->offset; +} + + +#endif // TQIMAGE_H diff --git a/src/kernel/qimageformatinterface_p.h b/src/kernel/qimageformatinterface_p.h new file mode 100644 index 000000000..e71078974 --- /dev/null +++ b/src/kernel/qimageformatinterface_p.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** ... +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQIMAGEFORMATINTERFACE_P_H +#define TQIMAGEFORMATINTERFACE_P_H + +#ifndef QT_H +#include +#endif // QT_H + +#if __GNUC__ - 0 > 3 +#pragma GCC system_header +#endif + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of internal files. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_NO_COMPONENT + +// {04903F05-54B1-4726-A849-FB5CB097CA87} +#ifndef IID_QImageFormat +#define IID_QImageFormat TQUuid( 0x04903f05, 0x54b1, 0x4726, 0xa8, 0x49, 0xfb, 0x5c, 0xb0, 0x97, 0xca, 0x87 ) +#endif + +class TQImage; + +struct Q_EXPORT TQImageFormatInterface : public TQFeatureListInterface +{ + virtual TQRESULT loadImage( const TQString &format, const TQString &filename, TQImage * ) = 0; + virtual TQRESULT saveImage( const TQString &format, const TQString &filename, const TQImage & ) = 0; + + virtual TQRESULT installIOHandler( const TQString & ) = 0; +}; + +#endif // QT_NO_COMPONENT + +#endif // TQIMAGEFORMATINTERFACE_P_H diff --git a/src/kernel/qimageformatplugin.cpp b/src/kernel/qimageformatplugin.cpp new file mode 100644 index 000000000..2e1d28939 --- /dev/null +++ b/src/kernel/qimageformatplugin.cpp @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** ... +** +** Copyright (C) 2001-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qimageformatplugin.h" +#ifndef QT_NO_IMAGEFORMATPLUGIN +#include "qimageformatinterface_p.h" +#include "qimage.h" + +/*! + \class TQImageFormatPlugin qimageformatplugin.h + \brief The TQImageFormatPlugin class provides an abstract base for custom image format plugins. + + \ingroup plugins + + The image format plugin is a simple plugin interface that makes + it easy to create custom image formats that can be used + transparently by applications. + + Writing an image format plugin is achieved by subclassing this + base class, reimplementing the pure virtual functions keys() and + installIOHandler(), and exporting the class with the + Q_EXPORT_PLUGIN macro. See the \link plugins-howto.html Plugins + documentation\endlink for details. +*/ + +/*! + \fn TQStringList TQImageFormatPlugin::keys() const + + Returns the list of image formats this plugin supports. + + \sa installIOHandler() +*/ + + +/*! + \fn bool TQImageFormatPlugin::installIOHandler( const TQString &format ) + + Installs a TQImageIO image I/O handler for the image format \a + format. + + \sa keys() +*/ + +class TQImageFormatPluginPrivate : public TQImageFormatInterface +{ +public: + TQImageFormatPluginPrivate( TQImageFormatPlugin *p ) + : plugin( p ) + { + } + virtual ~TQImageFormatPluginPrivate(); + + TQRESULT queryInterface( const TQUuid &iid, TQUnknownInterface **iface ); + Q_REFCOUNT; + + TQStringList featureList() const; + + TQRESULT loadImage( const TQString &format, const TQString &filename, TQImage * ); + TQRESULT saveImage( const TQString &format, const TQString &filename, const TQImage & ); + + TQRESULT installIOHandler( const TQString & ); + +private: + TQImageFormatPlugin *plugin; +}; + +TQImageFormatPluginPrivate::~TQImageFormatPluginPrivate() +{ + delete plugin; +} + +TQRESULT TQImageFormatPluginPrivate::queryInterface( const TQUuid &iid, TQUnknownInterface **iface ) +{ + *iface = 0; + + if ( iid == IID_QUnknown ) + *iface = this; + else if ( iid == IID_QFeatureList ) + *iface = this; + else if ( iid == IID_QImageFormat ) + *iface = this; + else + return TQE_NOINTERFACE; + + (*iface)->addRef(); + return TQS_OK; +} + +TQStringList TQImageFormatPluginPrivate::featureList() const +{ + return plugin->keys(); +} + +TQRESULT TQImageFormatPluginPrivate::loadImage( const TQString &format, const TQString &filename, TQImage *image ) +{ + return plugin->loadImage( format, filename, image ) ? TQS_FALSE : TQS_OK; +} + +TQRESULT TQImageFormatPluginPrivate::saveImage( const TQString &format, const TQString &filename, const TQImage &image ) +{ + return plugin->saveImage( format, filename, image ) ? TQS_FALSE : TQS_OK; +} + +TQRESULT TQImageFormatPluginPrivate::installIOHandler( const TQString &format ) +{ + return plugin->installIOHandler( format ) ? TQS_FALSE : TQS_OK; +} + +/*! + Constructs an image format plugin. This is invoked automatically + by the Q_EXPORT_PLUGIN macro. +*/ +TQImageFormatPlugin::TQImageFormatPlugin() + : TQGPlugin( d = new TQImageFormatPluginPrivate( this ) ) +{ +} + +/*! + Destroys the image format plugin. + + You never have to call this explicitly. TQt destroys a plugin + automatically when it is no longer used. +*/ +TQImageFormatPlugin::~TQImageFormatPlugin() +{ +} + + +/*!\internal + */ +bool TQImageFormatPlugin::loadImage( const TQString &format, const TQString &filename, TQImage *image ) +{ + Q_UNUSED( format ) + Q_UNUSED( filename ) + Q_UNUSED( image ) + return FALSE; +} + +/*! \internal + */ +bool TQImageFormatPlugin::saveImage( const TQString &format, const TQString &filename, const TQImage &image ) +{ + Q_UNUSED( format ) + Q_UNUSED( filename ) + Q_UNUSED( image ) + return FALSE; +} + +#endif // QT_NO_IMAGEFORMATPLUGIN diff --git a/src/kernel/qimageformatplugin.h b/src/kernel/qimageformatplugin.h new file mode 100644 index 000000000..02d925704 --- /dev/null +++ b/src/kernel/qimageformatplugin.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Definition of ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQIMAGEFORMATPLUGIN_H +#define TQIMAGEFORMATPLUGIN_H + +#ifndef QT_H +#include "qgplugin.h" +#include "qstringlist.h" +#endif // QT_H + +#ifndef QT_NO_IMAGEFORMATPLUGIN +class TQImageFormat; +class TQImageFormatPluginPrivate; + +class Q_EXPORT TQImageFormatPlugin : public TQGPlugin +{ + Q_OBJECT +public: + TQImageFormatPlugin(); + ~TQImageFormatPlugin(); + + virtual TQStringList keys() const = 0; + virtual bool loadImage( const TQString &format, const TQString &filename, TQImage *image ); + virtual bool saveImage( const TQString &format, const TQString &filename, const TQImage &image ); + virtual bool installIOHandler( const TQString &format ) = 0; + +private: + TQImageFormatPluginPrivate *d; +}; +#endif // QT_NO_IMAGEFORMATPLUGIN +#endif // TQIMAGEFORMATPLUGIN_H diff --git a/src/kernel/qinputcontext.cpp b/src/kernel/qinputcontext.cpp new file mode 100644 index 000000000..3d50405ea --- /dev/null +++ b/src/kernel/qinputcontext.cpp @@ -0,0 +1,856 @@ +/**************************************************************************** +** $Id: qinputcontext.cpp,v 1.6 2004/06/22 06:47:30 daisuke Exp $ +** +** Implementation of TQInputContext class +** +** Copyright (C) 2000-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 sales@trolltech.com 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 info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +//#define QT_NO_IM_PREEDIT_RELOCATION + +#include "qinputcontext.h" + +#ifndef QT_NO_IM + +#include "qplatformdefs.h" + +#include "qapplication.h" +#include "qwidget.h" +#include "qpopupmenu.h" + +#include +#include + +class TQInputContextPrivate +{ +public: + TQInputContextPrivate() + : holderWidget( 0 ), composingWidget( 0 ), hasFocus( FALSE ), + isComposing( FALSE ) +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + , preeditString( TQString::null ), + cursorPosition( -1 ), selLength ( 0 ) +#endif + {} + + TQWidget *holderWidget; // widget to which TQInputContext instance belongs. + TQWidget *composingWidget; + bool hasFocus; + bool isComposing; + + void updateComposingState( const TQString &text, + int newCursorPosition, int newSelLength ) { +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + preeditString = text; + cursorPosition = newCursorPosition; + selLength = newSelLength; +#endif + } + + void resetComposingState() { + isComposing = FALSE; +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + preeditString = TQString::null; + cursorPosition = -1; + selLength = 0; +#endif + } + +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + TQString preeditString; + int cursorPosition; + int selLength; +#endif +}; + + +// UPDATED COMMENT RETQUIRED -- 2004-07-08 YamaKen +/*! + \class TQInputContext qinputcontext.h + \brief The TQInputContext class abstracts the input method dependent data and composing state. + + \ingroup i18n + + An input method is responsible to input complex text that cannot + be inputted via simple keymap. It converts a sequence of input + events (typically key events) into a text string through the input + method specific converting process. The class of the processes are + widely ranging from simple finite state machine to complex text + translator that pools a whole paragraph of a text with text + editing capability to perform grammar and semantic analysis. + + To abstract such different input method specific intermediate + information, TQt offers the TQInputContext as base class. The + concept is well known as 'input context' in the input method + domain. an input context is created for a text widget in response + to a demand. It is ensured that an input context is prepared for + an input method before input to a text widget. + + Multiple input contexts that is belonging to a single input method + may concurrently coexist. Suppose multi-window text editor. Each + text widget of window A and B holds different TQInputContext + instance which contains different state information such as + partially composed text. + + \section1 Groups of functions: + + \table + \header \i Context \i Functions + + \row \i Receiving information \i + x11FilterEvent(), + filterEvent(), + setMicroFocus(), + mouseHandler() + + \row \i Sending back composed text \i + sendIMEvent(), + + \row \i State change notification \i + setFocus(), + unsetFocus(), + reset() + + \row \i Context information \i + identifierName(), + language(), + font(), + isComposing(), + + \endtable + + + \section1 Sharing input context between text widgets + + Any input context can be shared between several text widgets to + reduce resource consumption. In ideal case, each text widgets + should be allocated dedicated input context. But some complex + input contexts retquire slightly heavy resource such as 100 + kilobytes of memory. It prevents tquite many text widgets from + being used concurrently. + + To resolve such problem, we can share an input context. There is + one 'input context holder widget' per text widgets that shares + identical input context. In this model, the holder widget owns the + shared input context. Other text widgets access the input context + via TQApplication::locateICHolderWidget(). But the access + convention is transparently hidden into TQWidget, so developers are + not retquired to aware of it. + + What developer should know is only the mapping function + TQApplication::locateICHolderWidget(). It accepts a widget as + argument and returns its holder widget. Default implementation + returns the top-level widget of the widget as reasonable + assumption. But some applications should reimplement the function + to fit application specific usability. See + TQApplication::locateICHolderWidget() for further information. + + + \section1 Preedit preservation + + As described above, input contexts have wide variety of amount of + the state information in accordance with belonging input + method. It is ranging from 2-3 keystrokes of sequence in + deterministic input methods to hundreds of keystrokes with + semantic text refinement in complex input methods such as ordinary + Japanese input method. The difference retquires the different reset + policies in losing input focus. + + The former simple input method case, users will prefer resetting + the context to back to the neutral state when something + happened. Suppose a web browsing. The user scroll the page by + scrollbar after he or she has typed a half of the valid key + sequence into a text widget. In the case, the input context should + be reset in losing focus when he or she has dragged the + scrollbar. He or she will be confused if the input context is + still preserved until focused back to the text widget because he + or she will restart typing with first key of the sequence as a + habitual operation. + + On the other hand, we should choose completely different policy + for the latter complex input method case. Suppose same situation + as above but he or she is using a complex input method. In the + case, he or she will be angry if the input context has been lost + when he or she has dragged the scrollbar because the input context + contained a valuably composed text made up by considerable input + cost. So we should not reset the input context in the case. And + the input context should be preserved until focused back to the + text widget. This behavior is named as 'preedit preservation'. + + The two policies can be switched by calling or not calling reset() + in unsetFocus(). Default implementation of unsetFocus() calls + reset() to fit the simple input methods. The implementation is + expressed as 'preedit preservation is disabled'. + + + \section1 Preedit relocation + + Although the most case of the preedit preservation problem for + complex input methods is resolved as described above, there is a + special case. Suppose the case that matches all of the following + conditions. + + \list + + \i a input focus has been moved from a text widget to another text + widget directly + + \i the input context is shared between the two text widgets + + \i preedit preservation is enabled for the input context + + \endlist + + In the case, there are the following two retquirements that + contradicts each other. The input context sharing causes it. + + \list + + \i the input context has to be reset to prepare to input to the + newly focused text widget + + \i the input context has to be preserved until focused back to the + previous text widget + + \endlist + + A intrinsic feature named 'preedit relocation' is available to + compromise the retquirements. If the feature is enabled for the + input context, it is simply moved to the new text widget with the + preedit string. The user continues the input on the new text + widget, or relocate it to another text widget. The preedit of + previous text widget is automatically cleared to back to the + neutral state of the widget. + + This strange behavior is just a compromise. As described in + previous section, complex input method user should not be exposed + to the risk losing the input context because it contains valuable + long text made up with considerable input cost. The user will + immediately focus back to the previous text widget to continue the + input in the correct text widget if the preedit relocation + occurred. The feature is mainly existing as safety. + + The feature properly works even if the focus is moved as + following. Input method developers are not retquired to be aware of + the relocation protocol since TQInputContext transparently handles + it. + + a text widget -> a non-text widget -> another text widget + + To enable the preedit relocation feature, the input context class + have to reimplement isPreeditRelocationEnabled() as returns TRUE. + The implementation retquires that the preedit preservation is also + enabled since preedit relocation is a special case of the preedit + preservation. If the preedit relocation is disabled, the input + context is simply reset in the relocation case. + + + \section1 Input context instanciation + \section1 Input method switching + + \section1 Text widget implementor's guide + + Add following code fragment into createPopupMenu() to add input + method dependent submenus. + + \code + #ifndef QT_NO_IM + TQInputContext *qic = getInputContext(); + if ( qic ) + qic->addMenusTo( popup ); + #endif + \endcode + + \sa TQInputContextPlugin, TQInputContextFactory, TQApplication::locateICHolderWidget(), TQApplication::defaultInputMethod() +*/ + + +/*! + Constructs an input context. + + holderWidget is set immediately after this constructor has been + returned on the X11 platform. +*/ +TQInputContext::TQInputContext( TQObject *parent ) + : TQObject( parent ) +{ + d = new TQInputContextPrivate; +} + + +/*! + Destroys the input context. +*/ +TQInputContext::~TQInputContext() +{ + delete d; +} + +#if defined(Q_WS_X11) +/*! + \internal + Returns the owner of this input context. Ordinary input methods + should not call this function directly to keep platform + independence and flexible configuration possibility. + + The return value may differ from focusWidget() if the input + context is shared between several text widgets. + + \sa setHolderWidget(), focusWidget() +*/ +TQWidget *TQInputContext::holderWidget() const +{ + return d->holderWidget; +} + +/*! + \internal + Sets the owner of this input context. Ordinary input methods + must not call this function directly. + + \sa holderWidget() +*/ +void TQInputContext::setHolderWidget( TQWidget *w ) +{ + d->holderWidget = w; +} + +/*! + \internal + Returns the widget that has an input focus for this input + context. Ordinary input methods should not call this function + directly to keep platform independence and flexible configuration + possibility. + + The return value may differ from holderWidget() if the input + context is shared between several text widgets. + + \sa setFocusWidget(), holderWidget() +*/ +TQWidget *TQInputContext::focusWidget() const +{ + return d->hasFocus ? d->composingWidget : 0; +} + + +/*! + \internal + Sets the widget that has an input focus for this input + context. Ordinary input methods must not call this function + directly. + + \sa focusWidget() +*/ +void TQInputContext::setFocusWidget( TQWidget *w ) +{ + if ( w ) { + bool isFocusingBack = ( w == d->composingWidget ); + bool isPreeditRelocation = ( ! isFocusingBack && isComposing() && + d->composingWidget ); + // invoke sendIMEventInternal() rather than sendIMEvent() to + // avoid altering the composing state + if ( isPreeditRelocation == TRUE ) { + // clear preedit of previously focused text + // widget. preserved preedit may be exist even if + // isPreeditRelocationEnabled() == FALSE. + sendIMEventInternal( TQEvent::IMEnd ); + } + d->composingWidget = w; // changes recipient of TQIMEvent + if ( isPreeditRelocation == TRUE ) { +#if !defined(QT_NO_IM_PREEDIT_RELOCATION) + if ( isPreeditRelocationEnabled() ) { + // copy preedit state to the widget that gaining focus + sendIMEventInternal( TQEvent::IMStart ); + sendIMEventInternal( TQEvent::IMCompose, d->preeditString, + d->cursorPosition, d->selLength ); + } else +#endif + { + // reset input context when the shared context has + // focused on another text widget + reset(); + } + } + } + d->hasFocus = w ? TRUE : FALSE; +} + + +/*! + \internal + This function is called from TQWidget to keep input state + consistency. Ordinary input method must not call this function + directly. +*/ +void TQInputContext::releaseComposingWidget( TQWidget *w ) +{ + if ( d->composingWidget == w ) { + d->composingWidget = 0; + d->hasFocus = FALSE; + } +} +#endif // Q_WS_X11 + +/*! + \internal + This function can be reimplemented in a subclass as returning TRUE + if you want making your input method enable the preedit + relocation. See the description for preedit relocation of + TQInputContext. + + /sa TQInputContext +*/ +bool TQInputContext::isPreeditRelocationEnabled() +{ + return FALSE; +} + +/*! + This function indicates whether IMStart event had been sent to the + text widget. It is ensured that an input context can send IMCompose + or IMEnd event safely if this function returned TRUE. + + The state is automatically being tracked through sendIMEvent(). + + \sa sendIMEvent() +*/ +bool TQInputContext::isComposing() const +{ + return d->isComposing; +} + + +/*! + This function can be reimplemented in a subclass to filter input + events. + + Return TRUE if the \a event has been consumed. Otherwise, the + unfiltered \a event will be forwarded to widgets as ordinary + way. Although the input events have accept() and ignore() + methods, leave it untouched. + + \a event is currently restricted to TQKeyEvent. But some input + method related events such as TQWheelEvent or TQTabletEvent may be + added in future. + + The filtering opportunity is always given to the input context as + soon as possible. It has to be taken place before any other key + event consumers such as eventfilters and accelerators because some + input methods retquire tquite various key combination and + sequences. It often conflicts with accelerators and so on, so we + must give the input context the filtering opportunity first to + ensure all input methods work properly regardless of application + design. + + Ordinary input methods retquire discrete key events to work + properly, so TQt's key compression is always disabled for any input + contexts. + + \sa TQKeyEvent, x11FilterEvent() +*/ +bool TQInputContext::filterEvent( const TQEvent *event ) +{ + return FALSE; +} + + +/*! + \fn void TQInputContext::deletionRequested() + + Emit this signal when a fatal error has been caused in the input + context. The input context will be deleted by the owner which is + usually the holder widget. +*/ + +/*! + \fn void TQInputContext::imEventGenerated( TQObject *receiver, TQIMEvent *e ) + + \internal + This signal is emitted when the user has sent a TQIMEvent through + sendIMEvent(). Ordinary input methods should not emit this signal + directly. + + \a receiver is a platform dependent destination of the \a e. + + \sa TQIMEvent, sendIMEvent(), sendIMEventInternal(), +*/ + +/*! + \internal + Sends a TQIMEvent to the client via imEventGenerated() + signal. Ordinary input method should not call this function + directly. + + \sa TQIMEvent, TQIMComposeEvent, sendIMEvent(), imEventGenerated() +*/ +void TQInputContext::sendIMEventInternal( TQEvent::Type type, + const TQString &text, + int cursorPosition, int selLength ) +{ + TQObject *receiver = 0; + TQIMEvent *event = 0; + +#if defined(Q_WS_X11) + receiver = d->composingWidget; +#elif defined(Q_WS_QWS) + // just a placeholder +#endif + if ( ! receiver ) + return; + + if ( type == TQEvent::IMStart ) { + qDebug( "sending IMStart with %d chars to %p", + text.length(), receiver ); + event = new TQIMEvent( type, text, cursorPosition ); + } else if ( type == TQEvent::IMEnd ) { + qDebug( "sending IMEnd with %d chars to %p, text=%s", + text.length(), receiver, (const char*)text.local8Bit() ); + event = new TQIMEvent( type, text, cursorPosition ); + } else if ( type == TQEvent::IMCompose ) { + qDebug( "sending IMCompose to %p with %d chars, cpos=%d, sellen=%d, text=%s", + receiver, text.length(), cursorPosition, selLength, + (const char*)text.local8Bit() ); + event = new TQIMComposeEvent( type, text, cursorPosition, selLength ); + } + + if ( event ) + emit imEventGenerated( receiver, event ); +} + + +/*! + Call this function to send TQIMEvent to the text widget. This + function constructs a TQIMEvent based on the arguments and send it + to the appropriate widget. Ordinary input method should not + reimplement this function. + + \a type is either \c TQEvent::IMStart or \c TQEvent::IMCompose or \c + TQEvent::IMEnd. You have to send a \c TQEvent::IMStart to start + composing, then send several \c TQEvent::IMCompose to update the + preedit of the widget, and finalize the composition with sending + \c TQEvent::IMEnd. + + \c TQEvent::IMStart should always be sent without arguments as: + \code + sendIMEvent( TQEvent::IMStart ) + \endcode + + And \c TQEvent::IMCompose can be sent without cursor: + \code + sendIMEvent( TQEvent::IMCompose, TQString( "a text" ) ) + \endcode + + Or optionally with cursor with \a cursorPosition: + \code + sendIMEvent( TQEvent::IMCompose, TQString( "a text with cursor" ), 12 ) + \endcode + Note that \a cursorPosition also specifies microfocus position. + + Or optionally with selection text: + \code + sendIMEvent( TQEvent::IMCompose, TQString( "a text with selection" ), 12, 9 ) + \endcode + \a cursorPosition and \a selLength must be within the \a text. The + \a cursorPosition also specifies microfocus position in the case: + + \c TQEvent::IMEnd can be sent without arguments to terminate the + composition with null string: + \code + sendIMEvent( TQEvent::IMEnd ) + \endcode + + Or optionally accepts \a text to commit a string: + \code + sendIMEvent( TQEvent::IMEnd, TQString( "a text" ) ) + \endcode + + \sa TQIMEvent, TQIMComposeEvent, setMicroFocus() +*/ +void TQInputContext::sendIMEvent( TQEvent::Type type, const TQString &text, + int cursorPosition, int selLength ) +{ +#if defined(Q_WS_X11) + if ( !focusWidget() ) + return; +#endif + + if ( type == TQEvent::IMStart ) { + sendIMEventInternal( type, text, cursorPosition, selLength ); + d->isComposing = TRUE; + } else if ( type == TQEvent::IMEnd ) { + d->resetComposingState(); + sendIMEventInternal( type, text, cursorPosition, selLength ); + } else if ( type == TQEvent::IMCompose ) { + d->updateComposingState( text, cursorPosition, selLength ); + sendIMEventInternal( type, text, cursorPosition, selLength ); + } +} + + +/*! + This function can be reimplemented in a subclass to detect + that the input context has been focused on. + + The input context will receive input events through + x11FilterEvent() and filterEvent() after setFocus() until + unsetFocus() has been called. + + an input context is ensured that setFocus() is called exactly once + until unsetFocus() has been called even if preedit relocation has + occurred. This means that an input focus will survive between + several widgets that sharing the input context. + + On the X11 platform, focusWidget is already set before this + function has been called. + + \sa unsetFocus() +*/ +void TQInputContext::setFocus() +{ +} + + +/*! + This function can be reimplemented in a subclass to detect + that the input context has lost the focus. + + an input context is ensured that unsetFocus() is not called during + preedit relocation. This means that an input focus will survive + between several widgets that sharing the input context. + + Default implementation that calls reset() is sufficient for simple + input methods. You can override this function to alter the + behavior. For example, most Japanese input contexts should not be + reset on losing focus. The context sometimes contains a whole + paragraph and has minutes of lifetime different to ephemeral one + in other languages. The piled input context should be survived + until focused again since Japanese user naturally expects so. + + On the X11 platform, focusWidget is valid until this function has + been returned. + + \sa setFocus() +*/ +void TQInputContext::unsetFocus() +{ + reset(); +} + + +/*! + This function can be implemented in a subclass to handle + microfocus changes. + + 'microfocus' stands for the input method focus point in the + preedit (XIM "spot" point) for complex language input handling. It + can be used to place auxiliary GUI widgets such as candidate + selection window. + + \a x, \a y, \a w and \a h represents the position and size of the + cursor in the preedit string. \a f is the font on the location of + the cursor. +*/ +void TQInputContext::setMicroFocus( int x, int y, int w, int h, TQFont *f ) +{ +} + + +/*! + This function can be reimplemented in a subclass to handle mouse + presses/releases/doubleclicks/moves within the preedit text. You + can use the function to implement mouse-oriented user interface + such as text selection or popup menu for candidate selection. + + The parameter \a x is the offset within the string that was sent + with the IMCompose event. The alteration boundary of \a x is + ensured as character boundary of preedit string accurately. + + \a type is either \c TQEvent::MouseButtonPress or \c + TQEvent::MouseButtonRelease or \c TQEvent::MouseButtonDblClick or \c + TQEvent::MouseButtonMove. Refer \a button and \a state to determine + what operation has performed. + + The method interface is imported from + TQWSInputMethod::mouseHandler() of TQt/Embedded 2.3.7 and extended + for desktop system. + */ +void TQInputContext::mouseHandler( int x, TQEvent::Type type, + TQt::ButtonState button, + TQt::ButtonState state ) +{ + // Default behavior for simple ephemeral input contexts. Some + // complex input contexts should not be reset here. + if ( type == TQEvent::MouseButtonPress || + type == TQEvent::MouseButtonDblClick ) + reset(); +} + + +/*! + Returns the font of the current input widget + */ +TQFont TQInputContext::font() const +{ + if ( !focusWidget() ) + return TQApplication::font(); //### absolutely last resort + + return focusWidget()->font(); +} + + +/*! + This function can be reimplemented in a subclass to reset the + state of the input method. + + This function is called by several widgets to reset input + state. For example, a text widget call this function before + inserting a text to make widget ready to accept a text. + + Default implementation is sufficient for simple input method. You + can override this function to reset external input method engines + in complex input method. In the case, call TQInputContext::reset() + to ensure proper termination of inputting. + + You must not send any TQIMEvent except empty IMEnd event using + TQInputContext::reset() at reimplemented reset(). It will break + input state consistency. +*/ +void TQInputContext::reset() +{ + if ( isComposing() ) + sendIMEvent( TQEvent::IMEnd ); +} + + +/*! + This function must be implemented in any subclasses to return the + identifier name of the input method. + + Return value is the name to identify and specify input methods for + the input method switching mechanism and so on. The name has to be + consistent with TQInputContextPlugin::keys(). The name has to + consist of ASCII characters only. + + There are two different names with different responsibility in the + input method domain. This function returns one of them. Another + name is called 'display name' that stands for the name for + endusers appeared in a menu and so on. + + \sa TQInputContextPlugin::keys(), TQInputContextPlugin::displayName() +*/ +TQString TQInputContext::identifierName() +{ + return ""; +} + + +/*! + This function must be implemented in any subclasses to return a + language code (e.g. "zh_CN", "zh_TW", "zh_HK", "ja", "ko", ...) + of the input context. If the input context can handle multiple + languages, return the currently used one. The name has to be + consistent with TQInputContextPlugin::language(). + + This information will be used by language tagging feature in + TQIMEvent. It is retquired to distinguish unified han characters + correctly. It enables proper font and character code + handling. Suppose CJK-awared multilingual web browser + (that automatically modifies fonts in CJK-mixed text) and XML editor + (that automatically inserts lang attr). + + \sa TQInputContextPlugin::language() +*/ +TQString TQInputContext::language() +{ + return ""; +} + + +#if (QT_VERSION-0 >= 0x040000) +/*! + This is a preliminary interface for TQt4 + */ +TQList TQInputContext::actions() +{ +} +#else +/*! + This function can be reimplemented in a subclass to provide input + method dependent popup menus. Return 0 if the menus are + unnecessary. + + Ownership of the object and children are transferred to the + caller, and the result must not be called + setAutoDelete(). TQInputContextMenu::title is used for label text + of the popup menu as submenu. + + \sa addMenusTo() +*/ +TQPtrList *TQInputContext::menus() +{ + return 0; +} +#endif + +/*! + Appends input method dependent submenus into \a popup. A separator + is also inserted into \a popup if \a action is InsertSeparator. + + This is an utility function only for convenience in limited + situation. This function is used by input context owner such as + text widgets to add the submenus to its own context menu. If you + want to insert the submenus in more flexible way, use + TQInputContext::menus() manually. \a popup is not restricted to + context menu of a text widget. For example, the owner may be a + input method menu of TQtopia taskbar in TQt/Embedded platform. + + \sa menus(), TQInputContextMenu::Action +*/ +void TQInputContext::addMenusTo( TQPopupMenu *popup, TQInputContextMenu::Action action ) +{ + if ( ! popup ) + return; + + TQPtrList *imMenus = menus(); + if ( imMenus ) { + if ( action == TQInputContextMenu::InsertSeparator ) + popup->insertSeparator(); + for ( TQPtrList::Iterator it = imMenus->begin(); + it != imMenus->end(); + ++it ) { + TQInputContextMenu *imMenu = *it; + popup->insertItem( imMenu->title, imMenu->popup ); + } + imMenus->clear(); + delete imMenus; + } +} + +#endif //Q_NO_IM diff --git a/src/kernel/qinputcontext.h b/src/kernel/qinputcontext.h new file mode 100644 index 000000000..e06624e17 --- /dev/null +++ b/src/kernel/qinputcontext.h @@ -0,0 +1,143 @@ +/**************************************************************************** +** $Id: qinputcontext.h,v 1.8 2004/06/22 06:47:30 daisuke Exp $ +** +** Definition of TQInputContext +** +** Copyright (C) 1992-2002 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 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 sales@trolltech.com 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 info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#ifndef TQINPUTCONTEXT_H +#define TQINPUTCONTEXT_H + +#ifndef QT_NO_IM + +#ifndef QT_H +#include "qobject.h" +#include "qglobal.h" +#include "qevent.h" +#include "qstring.h" +#if (QT_VERSION-0 >= 0x040000) +#include "qlist.h" +#include "qaction.h" +#else +#include "qptrlist.h" +#endif +#endif + +class TQWidget; +class TQFont; +class TQPopupMenu; +class TQInputContextPrivate; + + +struct TQInputContextMenu { + enum Action { + NoSeparator, + InsertSeparator + }; +#if !(QT_VERSION-0 >= 0x040000) + TQString title; + TQPopupMenu *popup; +#endif +}; + + +class TQInputContext : public TQObject +{ + Q_OBJECT +public: + TQInputContext( TQObject *parent = 0 ); + virtual ~TQInputContext(); + + virtual TQString identifierName(); + virtual TQString language(); + +#if defined(Q_WS_X11) + virtual bool x11FilterEvent( TQWidget *keywidget, XEvent *event ); +#endif // Q_WS_X11 + virtual bool filterEvent( const TQEvent *event ); + virtual void reset(); + + virtual void setFocus(); + virtual void unsetFocus(); + virtual void setMicroFocus( int x, int y, int w, int h, TQFont *f = 0 ); + virtual void mouseHandler( int x, TQEvent::Type type, + TQt::ButtonState button, TQt::ButtonState state ); + virtual TQFont font() const; + virtual bool isComposing() const; + virtual bool isPreeditRelocationEnabled(); + +#if (QT_VERSION-0 >= 0x040000) + virtual TQList actions(); + void addActionsTo( TQMenu *menu, TQInputContextMenu::Action action = TQInputContextMenu::InsertSeparator ); +#else + virtual TQPtrList *menus(); + void addMenusTo( TQPopupMenu *popup, TQInputContextMenu::Action action = TQInputContextMenu::InsertSeparator ); +#endif + +#if defined(Q_WS_X11) + // these functions are not recommended for ordinary use + virtual TQWidget *focusWidget() const; + virtual TQWidget *holderWidget() const; + + // these functions must not be used by ordinary input method + virtual void setFocusWidget( TQWidget *w ); + virtual void setHolderWidget( TQWidget *w ); + virtual void releaseComposingWidget( TQWidget *w ); +#endif + +signals: + void deletionRequested(); + void imEventGenerated( TQObject *receiver, TQIMEvent *e ); + +protected: + virtual void sendIMEvent( TQEvent::Type type, + const TQString &text = TQString::null, + int cursorPosition = -1, int selLength = 0 ); + +private: + void sendIMEventInternal( TQEvent::Type type, + const TQString &text = TQString::null, + int cursorPosition = -1, int selLength = 0 ); + + TQInputContextPrivate *d; + + friend class TQWidget; + friend class TQInputContextFactory; + +private: // Disabled copy constructor and operator= + TQInputContext( const TQInputContext & ); + TQInputContext &operator=( const TQInputContext & ); + +}; + +#endif //Q_NO_IM + +#endif // TQINPUTCONTEXT_H diff --git a/src/kernel/qinputcontext_p.h b/src/kernel/qinputcontext_p.h new file mode 100644 index 000000000..1064702c3 --- /dev/null +++ b/src/kernel/qinputcontext_p.h @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Definition of ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQINPUTCONTEXT_P_H +#define TQINPUTCONTEXT_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of internal files. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// +// + +#include "qglobal.h" + +class TQKeyEvent; +class TQWidget; +class TQFont; +class TQString; + + +#ifdef Q_WS_X11 +#include "qarray.h" +#include "qwindowdefs.h" +#include "qt_x11_p.h" +#endif + +#ifdef Q_WS_WIN +#include "qt_windows.h" +#endif + +#ifdef Q_WS_QWS +class TQWSIMEvent; +#endif + +class TQInputContext +{ +public: +#ifdef Q_WS_X11 + TQInputContext(TQWidget *); // should be a toplevel widget + ~TQInputContext(); + + void setFocus(); + void setComposePosition(int, int); + void setComposeArea(int, int, int, int); + void reset(); + + int lookupString(XKeyEvent *, TQCString &, KeySym *, Status *) const; + void setXFontSet(const TQFont &); + + void *ic; + TQString text; + TQWidget *focusWidget; + bool composing; + TQFont font; + XFontSet fontset; + TQMemArray selectedChars; +#endif // Q_WS_X11 + +#ifdef Q_WS_QWS + static void translateIMEvent( TQWSIMEvent *, TQWidget * ); + static void reset(); +private: + static TQWidget* focusWidget; + static TQString* composition; +#endif //Q_WS_QWS + +#ifdef Q_WS_WIN + static void init(); + static void shutdown(); + + static void TranslateMessage( const MSG *msg); + static LRESULT DefWindowProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ); + + static void setFont( const TQWidget *w, const TQFont & ); + static void setFocusHint( int x, int y, int w, int h, const TQWidget *widget ); + static bool startComposition(); + static bool endComposition( TQWidget *fw = 0 ); + static bool composition( LPARAM lparam ); + + static void accept( TQWidget *fw = 0 ); + static void enable( TQWidget *w, bool b ); +#endif +}; + +#endif // TQINPUTCONTEXT_P_H diff --git a/src/kernel/qinputcontext_x11.cpp b/src/kernel/qinputcontext_x11.cpp new file mode 100644 index 000000000..2f9fd18e4 --- /dev/null +++ b/src/kernel/qinputcontext_x11.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Implementation of TQInputContext class +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qinputcontext.h" + +#ifndef QT_NO_IM + +#include "qplatformdefs.h" + +#include "qapplication.h" +#include "qwidget.h" + +#include "qt_x11_p.h" + +/*! + This function may be overridden only if input method is depending + on X11 and you need raw XEvent. Otherwise, this function must not. + + This function is designed to filter raw key events for XIM, but + other input methods may use this to implement some special + features such as distinguishing Shift_L and Shift_R. + + Return TRUE if the \a event has been consumed. Otherwise, the + unfiltered \a event will be translated into TQEvent and forwarded + to filterEvent(). Filtering at both x11FilterEvent() and + filterEvent() in single input method is allowed. + + \a keywidget is a client widget into which a text is inputted. \a + event is inputted XEvent. + + \sa filterEvent() +*/ +bool TQInputContext::x11FilterEvent( TQWidget *keywidget, XEvent *event ) +{ + return FALSE; +} + +#endif //Q_NO_IM diff --git a/src/kernel/qinternal.cpp b/src/kernel/qinternal.cpp new file mode 100644 index 000000000..21a4e8864 --- /dev/null +++ b/src/kernel/qinternal.cpp @@ -0,0 +1,787 @@ +/**************************************************************************** +** +** Implementation of some internal classes +** +** Created : 010427 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "private/qinternal_p.h" +#include "qwidget.h" +#include "qpixmap.h" +#include "qpainter.h" +#include "qcleanuphandler.h" + +// Modern systems (year 2011) have very large screens in excess of 1000 pixels wide +// Some may even have screens in excess of 4000 pixels wide(!) +// Prevent drawing artifacts on such screens +#define USE_HUGE_QT_SHARED_DOUBLE_BUFFERS 1 + +static TQPixmap* qdb_shared_pixmap = 0; +static TQPixmap *qdb_force_pixmap = 0; +static TQSharedDoubleBuffer* qdb_owner = 0; + +TQCleanupHandler qdb_pixmap_cleanup; + +#ifdef Q_WS_MACX +bool TQSharedDoubleBuffer::dblbufr = FALSE; +#else +bool TQSharedDoubleBuffer::dblbufr = TRUE; +#endif + + +/* + hardLimitWidth/Height: if >= 0, the maximum number of pixels that + get double buffered. + + sharedLimitWidth/Height: if >= 0, the maximum number of pixels the + shared double buffer can keep. + + For x with sharedLimitSize < x <= hardLimitSize, temporary buffers + are constructed. + */ +static const int hardLimitWidth = -1; +static const int hardLimitHeight = -1; +#if defined( Q_WS_QWS ) || defined( Q_WS_MAC9 ) +// Small in TQt/Embedded / Mac9 - 5K on 32bpp +static const int sharedLimitWidth = 64; +static const int sharedLimitHeight = 20; +#else +#ifdef USE_HUGE_QT_SHARED_DOUBLE_BUFFERS +// 24M on 32bpp +static const int sharedLimitWidth = 6400; +static const int sharedLimitHeight = 1000; +#else +// 240K on 32bpp +static const int sharedLimitWidth = 640; +static const int sharedLimitHeight = 100; +#endif +#endif + +// ******************************************************************* +// TQSharedDoubleBufferCleaner declaration and implementation +// ******************************************************************* + +/* \internal + This class is responsible for cleaning up the pixmaps created by the + TQSharedDoubleBuffer class. When TQSharedDoubleBuffer creates a + pixmap larger than the shared limits, this class deletes it after a + specified amount of time. + + When the large pixmap is created/used, you must call start(). If the + large pixmap is ever deleted, you must call stop(). The start() + method always restarts the timer, so if the large pixmap is + constantly in use, the timer will never fire, and the pixmap will + not be constantly created and destroyed. +*/ + +static const int shared_double_buffer_cleanup_timeout = 30000; // 30 seconds + +// declaration + +class TQSharedDoubleBufferCleaner : public TQObject +{ +public: + TQSharedDoubleBufferCleaner( void ); + + void start( void ); + void stop( void ); + + void doCleanup( void ); + + bool event( TQEvent *e ); + +private: + int timer_id; +}; + +// implementation + +/* \internal + Creates a TQSharedDoubleBufferCleaner object. The timer is not + started when creating the object. +*/ +TQSharedDoubleBufferCleaner::TQSharedDoubleBufferCleaner( void ) + : TQObject( 0, "internal shared double buffer cleanup object" ), + timer_id( -1 ) +{ +} + +/* \internal + Starts the cleanup timer. Any previously running timer is stopped. +*/ +void TQSharedDoubleBufferCleaner::start( void ) +{ + stop(); + timer_id = startTimer( shared_double_buffer_cleanup_timeout ); +} + +/* \internal + Stops the cleanup timer, if it is running. +*/ +void TQSharedDoubleBufferCleaner::stop( void ) +{ + if ( timer_id != -1 ) + killTimer( timer_id ); + timer_id = -1; +} + +/* \internal + */ +void TQSharedDoubleBufferCleaner::doCleanup( void ) +{ + qdb_pixmap_cleanup.remove( &qdb_force_pixmap ); + delete qdb_force_pixmap; + qdb_force_pixmap = 0; +} + +/* \internal + Event handler reimplementation. Calls doCleanup() when the timer + fires. +*/ +bool TQSharedDoubleBufferCleaner::event( TQEvent *e ) +{ + if ( e->type() != TQEvent::Timer ) + return FALSE; + + TQTimerEvent *event = (TQTimerEvent *) e; + if ( event->timerId() == timer_id ) { + doCleanup(); + stop(); + } +#ifdef QT_CHECK_STATE + else { + qWarning( "TQSharedDoubleBufferCleaner::event: invalid timer event received." ); + return FALSE; + } +#endif // QT_CHECK_STATE + + return TRUE; +} + +// static instance +static TQSharedDoubleBufferCleaner *static_cleaner = 0; +TQSingleCleanupHandler cleanup_static_cleaner; + +inline static TQSharedDoubleBufferCleaner *staticCleaner() +{ + if ( ! static_cleaner ) { + static_cleaner = new TQSharedDoubleBufferCleaner(); + cleanup_static_cleaner.set( &static_cleaner ); + } + return static_cleaner; +} + + +// ******************************************************************* +// TQSharedDoubleBuffer implementation +// ******************************************************************* + +/* \internal + \enum DoubleBufferFlags + + \value InitBG initialize the background of the double buffer. + + \value Force disable shared buffer size limits. + + \value Default InitBG and Force are used by default. +*/ + +/* \internal + \enum DoubleBufferState + + \value Active indicates that the buffer may be used. + + \value BufferActive indicates that painting with painter() will be + double buffered. + + \value ExternalPainter indicates that painter() will return a + painter that was not created by TQSharedDoubleBuffer. +*/ + +/* \internal + \class TQSharedDoubleBuffer + + This class provides a single, reusable double buffer. This class + is used internally by TQt widgets that need double buffering, which + prevents each individual widget form creating a double buffering + pixmap. + + Using a single pixmap double buffer and sharing it across all + widgets is nicer on window system resources. +*/ + +/* \internal + Creates a TQSharedDoubleBuffer with flags \f. + + \sa DoubleBufferFlags +*/ +TQSharedDoubleBuffer::TQSharedDoubleBuffer( DBFlags f ) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ +} + +/* \internal + Creates a TQSharedDoubleBuffer with flags \f. The \a widget, \a x, + \a y, \a w and \a h arguments are passed to begin(). + + \sa DoubleBufferFlags begin() +*/ +TQSharedDoubleBuffer::TQSharedDoubleBuffer( TQWidget* widget, + int x, int y, int w, int h, + DBFlags f ) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ + begin( widget, x, y, w, h ); +} + +/* \internal + Creates a TQSharedDoubleBuffer with flags \f. The \a painter, \a x, + \a y, \a w and \a h arguments are passed to begin(). + + \sa DoubleBufferFlags begin() +*/ +TQSharedDoubleBuffer::TQSharedDoubleBuffer( TQPainter* painter, + int x, int y, int w, int h, + DBFlags f) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ + begin( painter, x, y, w, h ); +} + +/* \internal + Creates a TQSharedDoubleBuffer with flags \f. The \a widget and + \a r arguments are passed to begin(). + + \sa DoubleBufferFlags begin() +*/ +TQSharedDoubleBuffer::TQSharedDoubleBuffer( TQWidget *widget, const TQRect &r, DBFlags f ) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ + begin( widget, r ); +} + +/* \internal + Creates a TQSharedDoubleBuffer with flags \f. The \a painter and + \a r arguments are passed to begin(). + + \sa DoubleBufferFlags begin() +*/ +TQSharedDoubleBuffer::TQSharedDoubleBuffer( TQPainter *painter, const TQRect &r, DBFlags f ) + : wid( 0 ), rx( 0 ), ry( 0 ), rw( 0 ), rh( 0 ), flags( f ), state( 0 ), + p( 0 ), external_p( 0 ), pix( 0 ) +{ + begin( painter, r ); +} + +/* \internal + Destructs the TQSharedDoubleBuffer and calls end() if the buffer is + active. + + \sa isActive() end() +*/ +TQSharedDoubleBuffer::~TQSharedDoubleBuffer() +{ + if ( isActive() ) + end(); +} + +/* \internal + Starts double buffered painting in the area specified by \a x, + \a y, \a w and \a h on \a painter. Painting should be done using the + TQPainter returned by TQSharedDoubleBuffer::painter(). + + The double buffered area will be updated when calling end(). + + \sa painter() isActive() end() +*/ +bool TQSharedDoubleBuffer::begin( TQPainter* painter, int x, int y, int w, int h ) +{ + if ( isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQSharedDoubleBuffer::begin: Buffer is already active." + "\n\tYou must end() the buffer before a second begin()" ); +#endif // QT_CHECK_STATE + return FALSE; + } + + external_p = painter; + + if ( painter->device()->devType() == TQInternal::Widget ) + return begin( (TQWidget *) painter->device(), x, y, w, h ); + + state = Active; + + rx = x; + ry = y; + rw = w; + rh = h; + + if ( ( pix = getPixmap() ) ) { +#ifdef Q_WS_X11 + if ( painter->device()->x11Screen() != pix->x11Screen() ) + pix->x11SetScreen( painter->device()->x11Screen() ); + TQPixmap::x11SetDefaultScreen( pix->x11Screen() ); +#endif // Q_WS_X11 + + state |= BufferActive; + p = new TQPainter( pix ); + if ( p->isActive() ) { + p->setPen( external_p->pen() ); + p->setBackgroundColor( external_p->backgroundColor() ); + p->setFont( external_p->font() ); + } + } else { + state |= ExternalPainter; + p = external_p; + } + + return TRUE; +} + +/* \internal + + + Starts double buffered painting in the area specified by \a x, + \a y, \a w and \a h on \a widget. Painting should be done using the + TQPainter returned by TQSharedDoubleBuffer::painter(). + + The double buffered area will be updated when calling end(). + + \sa painter() isActive() end() +*/ +bool TQSharedDoubleBuffer::begin( TQWidget* widget, int x, int y, int w, int h ) +{ + if ( isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQSharedDoubleBuffer::begin: Buffer is already active." + "\n\tYou must end() the buffer before a second begin()" ); +#endif // QT_CHECK_STATE + return FALSE; + } + + state = Active; + + wid = widget; + rx = x; + ry = y; + rw = w <= 0 ? wid->width() : w; + rh = h <= 0 ? wid->height() : h; + + if ( ( pix = getPixmap() ) ) { +#ifdef Q_WS_X11 + if ( wid->x11Screen() != pix->x11Screen() ) + pix->x11SetScreen( wid->x11Screen() ); + TQPixmap::x11SetDefaultScreen( pix->x11Screen() ); +#endif // Q_WS_X11 + + state |= BufferActive; + if ( flags & InitBG ) { + pix->fill( wid, rx, ry ); + } + p = new TQPainter( pix, wid ); + // newly created painters should be translated to the origin + // of the widget, so that paint methods can draw onto the double + // buffered painter in widget coordinates. + p->setBrushOrigin( -rx, -ry ); + p->translate( -rx, -ry ); + } else { + if ( external_p ) { + state |= ExternalPainter; + p = external_p; + } else { + p = new TQPainter( wid ); + } + + if ( flags & InitBG ) { + wid->erase( rx, ry, rw, rh ); + } + } + return TRUE; +} + +/* \internal + Ends double buffered painting. The contents of the shared double + buffer pixmap are drawn onto the destination by calling flush(), + and ownership of the shared double buffer pixmap is released. + + \sa begin() flush() +*/ +bool TQSharedDoubleBuffer::end() +{ + if ( ! isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQSharedDoubleBuffer::end: Buffer is not active." + "\n\tYou must call begin() before calling end()." ); +#endif // QT_CHECK_STATE + return FALSE; + } + + if ( ! ( state & ExternalPainter ) ) { + p->end(); + delete p; + } + + flush(); + + if ( pix ) { + releasePixmap(); + } + + wid = 0; + rx = ry = rw = rh = 0; + // do not reset flags! + state = 0; + + p = external_p = 0; + pix = 0; + + return TRUE; +} + +/* \internal + Paints the contents of the shared double buffer pixmap onto the + destination. The destination is determined from the arguments + based to begin(). + + Note: You should not need to call this function, since it is called + from end(). + + \sa begin() end() +*/ +void TQSharedDoubleBuffer::flush() +{ + if ( ! isActive() || ! ( state & BufferActive ) ) + return; + + if ( external_p ) + external_p->drawPixmap( rx, ry, *pix, 0, 0, rw, rh ); + else if ( wid && wid->isVisible() ) + bitBlt( wid, rx, ry, pix, 0, 0, rw, rh ); +} + +/* \internal + Atquire ownership of the shared double buffer pixmap, subject to the + following conditions: + + \list 1 + \i double buffering is enabled globally. + \i the shared double buffer pixmap is not in use. + \i the size specified in begin() is valid, and within limits. + \endlist + + If all of these conditions are met, then this TQSharedDoubleBuffer + object becomes the owner of the shared double buffer pixmap. The + shared double buffer pixmap is resize if necessary, and this + function returns a pointer to the pixmap. Ownership must later be + relintquished by calling releasePixmap(). + + If none of the above conditions are met, this function returns + zero. + + \sa releasePixmap() +*/ +TQPixmap *TQSharedDoubleBuffer::getPixmap() +{ + if ( isDisabled() ) { + // double buffering disabled globally + return 0; + } + + if ( qdb_owner ) { + // shared pixmap already in use + return 0; + } + + if ( rw <= 0 || rh <= 0 || + ( hardLimitWidth > 0 && rw >= hardLimitWidth ) || + ( hardLimitHeight > 0 && rh >= hardLimitHeight ) ) { + // invalid size, or hard limit reached + return 0; + } + + if ( rw >= sharedLimitWidth || rh >= sharedLimitHeight ) { + if ( flags & Force ) { +#ifdef USE_HUGE_QT_SHARED_DOUBLE_BUFFERS + rw = TQMIN(rw, 16000); + rh = TQMIN(rh, 16000); +#else + rw = TQMIN(rw, 8000); + rh = TQMIN(rh, 8000); +#endif + // need to create a big pixmap and start the cleaner + if ( ! qdb_force_pixmap ) { + qdb_force_pixmap = new TQPixmap( rw, rh ); + qdb_pixmap_cleanup.add( &qdb_force_pixmap ); + } else if ( qdb_force_pixmap->width () < rw || + qdb_force_pixmap->height() < rh ) { + qdb_force_pixmap->resize( rw, rh ); + } + qdb_owner = this; + staticCleaner()->start(); + return qdb_force_pixmap; + } + + // size is outside shared limit + return 0; + } + + if ( ! qdb_shared_pixmap ) { + qdb_shared_pixmap = new TQPixmap( rw, rh ); + qdb_pixmap_cleanup.add( &qdb_shared_pixmap ); + } else if ( qdb_shared_pixmap->width() < rw || + qdb_shared_pixmap->height() < rh ) { + qdb_shared_pixmap->resize( rw, rh ); + } + qdb_owner = this; + return qdb_shared_pixmap; +} + +/* \internal + Releases ownership of the shared double buffer pixmap. + + \sa getPixmap() +*/ +void TQSharedDoubleBuffer::releasePixmap() +{ + if ( qdb_owner != this ) { + // sanity check + +#ifdef QT_CHECK_STATE + qWarning( "TQSharedDoubleBuffer::releasePixmap: internal error." + "\n\t%p does not own shared pixmap, %p does.", + (void*)this, (void*)qdb_owner ); +#endif // QT_CHECK_STATE + + return; + } + + qdb_owner = 0; +} + +/* \internal + \fn bool TQSharedDoubleBuffer::isDisabled() + + Returns TRUE if double buffering is disabled globally, FALSE otherwise. +*/ + +/* \internal + \fn void TQSharedDoubleBuffer::setDisabled( bool off ) + + Disables global double buffering \a off is TRUE, otherwise global + double buffering is enabled. +*/ + +/* \internal + Deletes the shared double buffer pixmap. You should not need to + call this function, since it is called from the TQApplication + destructor. +*/ +void TQSharedDoubleBuffer::cleanup() +{ + qdb_pixmap_cleanup.remove( &qdb_shared_pixmap ); + qdb_pixmap_cleanup.remove( &qdb_force_pixmap ); + delete qdb_shared_pixmap; + delete qdb_force_pixmap; + qdb_shared_pixmap = 0; + qdb_force_pixmap = 0; + qdb_owner = 0; +} + +/* \internal + \fn bool TQSharedDoubleBuffer::begin( TQWidget *widget, const TQRect &r ) + \overload +*/ + +/* \internal + \fn bool TQSharedDoubleBuffer::begin( TQPainter *painter, const TQRect &r ) + \overload +*/ + +/* \internal + \fn TQPainter *TQSharedDoubleBuffer::painter() const + + Returns the active painter on the double buffered area, + or zero if double buffered painting is not active. +*/ + +/* \internal + \fn bool TQSharedDoubleBuffer::isActive() const + + Returns TRUE if double buffered painting is active, FALSE otherwise. +*/ + +/* \internal + \fn bool TQSharedDoubleBuffer::isBuffered() const + + Returns TRUE if painting is double buffered, FALSE otherwise. +*/ + + +// ******************************************************************* +// TQMembuf declaration and implementation +// ******************************************************************* + +/* \internal + This class implements an efficient buffering of data that is often used by + asynchronous IO classes like TQSocket, TQHttp and TQProcess. +*/ + +TQMembuf::TQMembuf() : _size(0), _index(0) +{ + buf = new TQPtrList; + buf->setAutoDelete( TRUE ); +} + +TQMembuf::~TQMembuf() +{ + delete buf; +} + +/*! \internal + This function consumes \a nbytes bytes of data from the + buffer and copies it into \a sink. If \a sink is a 0 pointer + the data goes into the nirvana. +*/ +bool TQMembuf::consumeBytes( Q_ULONG nbytes, char *sink ) +{ + if ( nbytes <= 0 || nbytes > _size ) + return FALSE; + _size -= nbytes; + for ( ;; ) { + TQByteArray *a = buf->first(); + if ( _index + nbytes >= a->size() ) { + // Here we skip the whole byte array and get the next later + int len = a->size() - _index; + if ( sink ) { + memcpy( sink, a->data()+_index, len ); + sink += len; + } + nbytes -= len; + buf->remove(); + _index = 0; + if ( nbytes == 0 ) + break; + } else { + // Here we skip only a part of the first byte array + if ( sink ) + memcpy( sink, a->data()+_index, nbytes ); + _index += nbytes; + break; + } + } + return TRUE; +} + +/*! \internal + Scans for any occurrence of '\n' in the buffer. If \a store + is not 0 the text up to the first '\n' (or terminating 0) is + written to \a store, and a terminating 0 is appended to \a store + if necessary. Returns TRUE if a '\n' was found; otherwise returns + FALSE. +*/ +bool TQMembuf::scanNewline( TQByteArray *store ) +{ + if ( _size == 0 ) + return FALSE; + int i = 0; // index into 'store' + TQByteArray *a = 0; + char *p; + int n; + for ( ;; ) { + if ( !a ) { + a = buf->first(); + if ( !a || a->size() == 0 ) + return FALSE; + p = a->data() + _index; + n = a->size() - _index; + } else { + a = buf->next(); + if ( !a || a->size() == 0 ) + return FALSE; + p = a->data(); + n = a->size(); + } + if ( store ) { + while ( n-- > 0 ) { + *(store->data()+i) = *p; + if ( ++i == (int)store->size() ) + store->resize( store->size() < 256 + ? 1024 : store->size()*4 ); + switch ( *p ) { + case '\0': + store->resize( i ); + return FALSE; + case '\n': + *(store->data()+i) = '\0'; + store->resize( i ); + return TRUE; + } + p++; + } + } else { + while ( n-- > 0 ) { + switch ( *p++ ) { + case '\0': + return FALSE; + case '\n': + return TRUE; + } + } + } + } +} + +int TQMembuf::ungetch( int ch ) +{ + if ( buf->isEmpty() || _index==0 ) { + // we need a new TQByteArray + TQByteArray *ba = new TQByteArray( 1 ); + buf->insert( 0, ba ); + _size++; + ba->at( 0 ) = ch; + } else { + // we can reuse a place in the buffer + TQByteArray *ba = buf->first(); + _index--; + _size++; + ba->at( _index ) = ch; + } + return ch; +} diff --git a/src/kernel/qinternal_p.h b/src/kernel/qinternal_p.h new file mode 100644 index 000000000..f40dd6df5 --- /dev/null +++ b/src/kernel/qinternal_p.h @@ -0,0 +1,213 @@ +/**************************************************************************** +** +** Definition of some shared interal classes +** +** Created : 010427 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQINTERNAL_P_H +#define TQINTERNAL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of a number of TQt sources files. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// +// +#ifndef QT_H +#include "qnamespace.h" +#include "qrect.h" +#include "qptrlist.h" +#include "qcstring.h" +#include "qiodevice.h" +#endif // QT_H + +class TQWidget; +class TQPainter; +class TQPixmap; + +class Q_EXPORT TQSharedDoubleBuffer +{ +public: + enum DoubleBufferFlags { + NoFlags = 0x00, + InitBG = 0x01, + Force = 0x02, + Default = InitBG | Force + }; + typedef uint DBFlags; + + TQSharedDoubleBuffer( DBFlags f = Default ); + TQSharedDoubleBuffer( TQWidget* widget, + int x = 0, int y = 0, int w = -1, int h = -1, + DBFlags f = Default ); + TQSharedDoubleBuffer( TQPainter* painter, + int x = 0, int y = 0, int w = -1, int h = -1, + DBFlags f = Default ); + TQSharedDoubleBuffer( TQWidget *widget, const TQRect &r, DBFlags f = Default ); + TQSharedDoubleBuffer( TQPainter *painter, const TQRect &r, DBFlags f = Default ); + ~TQSharedDoubleBuffer(); + + bool begin( TQWidget* widget, int x = 0, int y = 0, int w = -1, int h = -1 ); + bool begin( TQPainter* painter, int x = 0, int y = 0, int w = -1, int h = -1); + bool begin( TQWidget* widget, const TQRect &r ); + bool begin( TQPainter* painter, const TQRect &r ); + bool end(); + + TQPainter* painter() const; + + bool isActive() const; + bool isBuffered() const; + void flush(); + + static bool isDisabled() { return !dblbufr; } + static void setDisabled( bool off ) { dblbufr = !off; } + + static void cleanup(); + +private: + enum DoubleBufferState { + Active = 0x0100, + BufferActive = 0x0200, + ExternalPainter = 0x0400 + }; + typedef uint DBState; + + TQPixmap *getPixmap(); + void releasePixmap(); + + TQWidget *wid; + int rx, ry, rw, rh; + DBFlags flags; + DBState state; + + TQPainter *p, *external_p; + TQPixmap *pix; + + static bool dblbufr; +}; + +inline bool TQSharedDoubleBuffer::begin( TQWidget* widget, const TQRect &r ) +{ return begin( widget, r.x(), r.y(), r.width(), r.height() ); } + +inline bool TQSharedDoubleBuffer::begin( TQPainter *painter, const TQRect &r ) +{ return begin( painter, r.x(), r.y(), r.width(), r.height() ); } + +inline TQPainter* TQSharedDoubleBuffer::painter() const +{ return p; } + +inline bool TQSharedDoubleBuffer::isActive() const +{ return ( state & Active ); } + +inline bool TQSharedDoubleBuffer::isBuffered() const +{ return ( state & BufferActive ); } + + +class TQVirtualDestructor { +public: + virtual ~TQVirtualDestructor() {} +}; + +template +class TQAutoDeleter : public TQVirtualDestructor { +public: + TQAutoDeleter( T* p ) : ptr( p ) {} + ~TQAutoDeleter() { delete ptr; } + T* data() const { return ptr; } +private: + T* ptr; +}; + +template +T* qAutoDeleterData( TQAutoDeleter* ad ) +{ + if ( !ad ) + return 0; + return ad->data(); +} + +template +TQAutoDeleter* qAutoDeleter( T* p ) +{ + return new TQAutoDeleter( p ); +} + +class Q_EXPORT TQMembuf +{ +public: + TQMembuf(); + ~TQMembuf(); + + void append( TQByteArray *ba ); + void clear(); + + bool consumeBytes( Q_ULONG nbytes, char *sink ); + TQByteArray readAll(); + bool scanNewline( TQByteArray *store ); + bool canReadLine() const; + + int ungetch( int ch ); + + TQIODevice::Offset size() const; + +private: + + TQPtrList *buf; + TQIODevice::Offset _size; + TQIODevice::Offset _index; +}; + +inline void TQMembuf::append( TQByteArray *ba ) +{ buf->append( ba ); _size += ba->size(); } + +inline void TQMembuf::clear() +{ buf->clear(); _size=0; _index=0; } + +inline TQByteArray TQMembuf::readAll() +{ TQByteArray ba(_size); consumeBytes(_size,ba.data()); return ba; } + +inline bool TQMembuf::canReadLine() const +{ return ((TQMembuf*)this)->scanNewline( 0 ); } + +inline TQIODevice::Offset TQMembuf::size() const +{ return _size; } + +#endif // TQINTERNAL_P_H diff --git a/src/kernel/qjpegio.cpp b/src/kernel/qjpegio.cpp new file mode 100644 index 000000000..0abdd1942 --- /dev/null +++ b/src/kernel/qjpegio.cpp @@ -0,0 +1,599 @@ +/**************************************************************************** +** +** Implementation of JPEG TQImage IOHandler +** +** Created : 990521 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef QT_CLEAN_NAMESPACE +#define QT_CLEAN_NAMESPACE +#endif + +#include "qimage.h" + +#ifndef QT_NO_IMAGEIO_JPEG + +#include "qiodevice.h" +#include "qjpegio.h" + +#include // jpeglib needs this to be pre-included +#include + + +// including jpeglib.h seems to be a little messy +extern "C" { +#define XMD_H // shut JPEGlib up +#if defined(Q_OS_UNIXWARE) +# define HAVE_BOOLEAN // libjpeg under Unixware seems to need this +#endif +#include +#ifdef const +# undef const // remove crazy C hackery in jconfig.h +#endif +} + + +struct my_error_mgr : public jpeg_error_mgr { + jmp_buf setjmp_buffer; +}; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static +void my_error_exit (j_common_ptr cinfo) +{ + my_error_mgr* myerr = (my_error_mgr*) cinfo->err; + char buffer[JMSG_LENGTH_MAX]; + (*cinfo->err->format_message)(cinfo, buffer); + qWarning(buffer); + longjmp(myerr->setjmp_buffer, 1); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +static const int max_buf = 4096; + +struct my_jpeg_source_mgr : public jpeg_source_mgr { + // Nothing dynamic - cannot rely on destruction over longjump + TQImageIO* iio; + JOCTET buffer[max_buf]; + +public: + my_jpeg_source_mgr(TQImageIO* iio); +}; + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static +void qt_init_source(j_decompress_ptr) +{ +} + +static +boolean qt_fill_input_buffer(j_decompress_ptr cinfo) +{ + int num_read; + my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; + TQIODevice* dev = src->iio->ioDevice(); + src->next_input_byte = src->buffer; + num_read = dev->readBlock((char*)src->buffer, max_buf); + if ( num_read <= 0 ) { + // Insert a fake EOI marker - as per jpeglib recommendation + src->buffer[0] = (JOCTET) 0xFF; + src->buffer[1] = (JOCTET) JPEG_EOI; + src->bytes_in_buffer = 2; + } else { + src->bytes_in_buffer = num_read; + } +#if defined(Q_OS_UNIXWARE) + return B_TRUE; +#else + return TRUE; +#endif +} + +static +void qt_skip_input_data(j_decompress_ptr cinfo, long num_bytes) +{ + my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; + + // `dumb' implementation from jpeglib + + /* Just a dumb implementation for now. Could use fseek() except + * it doesn't work on pipes. Not clear that being smart is worth + * any trouble anyway --- large skips are infrequent. + */ + if (num_bytes > 0) { + while (num_bytes > (long) src->bytes_in_buffer) { + num_bytes -= (long) src->bytes_in_buffer; + (void) qt_fill_input_buffer(cinfo); + /* note we assume that qt_fill_input_buffer will never return FALSE, + * so suspension need not be handled. + */ + } + src->next_input_byte += (size_t) num_bytes; + src->bytes_in_buffer -= (size_t) num_bytes; + } +} + +static +void qt_term_source(j_decompress_ptr) +{ +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +inline my_jpeg_source_mgr::my_jpeg_source_mgr(TQImageIO* iioptr) +{ + jpeg_source_mgr::init_source = qt_init_source; + jpeg_source_mgr::fill_input_buffer = qt_fill_input_buffer; + jpeg_source_mgr::skip_input_data = qt_skip_input_data; + jpeg_source_mgr::resync_to_restart = jpeg_resync_to_restart; + jpeg_source_mgr::term_source = qt_term_source; + iio = iioptr; + bytes_in_buffer = 0; + next_input_byte = buffer; +} + + +static +void scaleSize( int &reqW, int &reqH, int imgW, int imgH, TQImage::ScaleMode mode ) +{ + if ( mode == TQImage::ScaleFree ) + return; + int t1 = imgW * reqH; + int t2 = reqW * imgH; + if (( mode == TQImage::ScaleMin && (t1 > t2) ) || ( mode == TQImage::ScaleMax && (t1 < t2) )) + reqH = t2 / imgW; + else + reqW = t1 / imgH; +} + + +static +void read_jpeg_image(TQImageIO* iio) +{ + TQImage image; + + struct jpeg_decompress_struct cinfo; + + struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(iio); + struct my_error_mgr jerr; + + cinfo.err = jpeg_std_error(&jerr); + jerr.error_exit = my_error_exit; + + jpeg_create_decompress(&cinfo); + + cinfo.src = iod_src; + + if (!setjmp(jerr.setjmp_buffer)) { +#if defined(Q_OS_UNIXWARE) + (void) jpeg_read_header(&cinfo, B_TRUE); +#else + (void) jpeg_read_header(&cinfo, TRUE); +#endif + + (void) jpeg_start_decompress(&cinfo); + + TQString params = iio->parameters(); + params.simplifyWhiteSpace(); + int sWidth = 0, sHeight = 0; + char sModeStr[1024] = ""; + TQImage::ScaleMode sMode; + + if ( params.contains( "GetHeaderInformation" ) ) { + + // Create TQImage's without allocating the data + if ( cinfo.output_components == 3 || cinfo.output_components == 4) { + image = TQImage( NULL, cinfo.output_width, cinfo.output_height, 32, NULL, 0, TQImage::IgnoreEndian ); + } else if ( cinfo.output_components == 1 ) { + image = TQImage( NULL, cinfo.output_width, cinfo.output_height, 8, NULL, 0, TQImage::IgnoreEndian ); + } else { + // Unsupported format + } + + + } else if ( params.contains( "Scale" ) ) { + sscanf( params.latin1(), "Scale( %i, %i, %1023s )", + &sWidth, &sHeight, sModeStr ); + + TQString sModeTQStr( sModeStr ); + if ( sModeTQStr == "ScaleFree" ) { + sMode = TQImage::ScaleFree; + } else if ( sModeTQStr == "ScaleMin" ) { + sMode = TQImage::ScaleMin; + } else if ( sModeTQStr == "ScaleMax" ) { + sMode = TQImage::ScaleMax; + } else { + qDebug("read_jpeg_image: invalid scale mode \"%s\", see TQImage::ScaleMode documentation", sModeStr); + sMode = TQImage::ScaleFree; + } + +// qDebug( "Parameters ask to scale the image to %i x %i ScaleMode: %s", sWidth, sHeight, sModeStr ); + scaleSize( sWidth, sHeight, cinfo.output_width, cinfo.output_height, sMode ); +// qDebug( "Scaling the jpeg to %i x %i", sWidth, sHeight, sModeStr ); + + bool created = FALSE; + if ( cinfo.output_components == 3 || cinfo.output_components == 4) { + created = image.create( sWidth, sHeight, 32 ); + } else if ( cinfo.output_components == 1 ) { + created = image.create( sWidth, sHeight, 8, 256 ); + for (int i=0; i<256; i++) + image.setColor(i, qRgb(i,i,i)); + } else { + // Unsupported format + } + if (!created) + image = TQImage(); + + if (!image.isNull()) { + TQImage tmpImage( cinfo.output_width, 1, 32 ); + uchar** inLines = tmpImage.jumpTable(); + uchar** outLines = image.jumpTable(); + while (cinfo.output_scanline < cinfo.output_height) { + int outputLine = sHeight * cinfo.output_scanline / cinfo.output_height; + (void) jpeg_read_scanlines(&cinfo, inLines, 1); + if ( cinfo.output_components == 3 ) { + uchar *in = inLines[0]; + TQRgb *out = (TQRgb*)outLines[outputLine]; + for (uint i=0; i32 bpp. + for (uint j=0; jsetImage(image); + iio->setStatus(image.isNull()); + } + + jpeg_destroy_decompress(&cinfo); + delete iod_src; +} + + +struct my_jpeg_destination_mgr : public jpeg_destination_mgr { + // Nothing dynamic - cannot rely on destruction over longjump + TQImageIO* iio; + JOCTET buffer[max_buf]; + +public: + my_jpeg_destination_mgr(TQImageIO*); +}; + + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static +void qt_init_destination(j_compress_ptr) +{ +} + +static +void qt_exit_on_error(j_compress_ptr cinfo, TQIODevice* dev) +{ + if (dev->status() == IO_Ok) { + return; + } else { + // cinfo->err->msg_code = JERR_FILE_WRITE; + (*cinfo->err->error_exit)((j_common_ptr)cinfo); + } +} + +static +boolean qt_empty_output_buffer(j_compress_ptr cinfo) +{ + my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; + TQIODevice* dev = dest->iio->ioDevice(); + + if ( dev->writeBlock( (char*)dest->buffer, max_buf ) != max_buf ) + qt_exit_on_error(cinfo, dev); + + dest->next_output_byte = dest->buffer; + dest->free_in_buffer = max_buf; + +#if defined(Q_OS_UNIXWARE) + return B_TRUE; +#else + return TRUE; +#endif +} + +static +void qt_term_destination(j_compress_ptr cinfo) +{ + my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; + TQIODevice* dev = dest->iio->ioDevice(); + Q_LONG n = max_buf - dest->free_in_buffer; + + if ( dev->writeBlock( (char*)dest->buffer, n ) != n ) + qt_exit_on_error(cinfo, dev); + + dev->flush(); + + qt_exit_on_error(cinfo, dev); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +inline +my_jpeg_destination_mgr::my_jpeg_destination_mgr(TQImageIO* iioptr) +{ + jpeg_destination_mgr::init_destination = qt_init_destination; + jpeg_destination_mgr::empty_output_buffer = qt_empty_output_buffer; + jpeg_destination_mgr::term_destination = qt_term_destination; + iio = iioptr; + next_output_byte = buffer; + free_in_buffer = max_buf; +} + + +static +void write_jpeg_image(TQImageIO* iio) +{ + TQImage image = iio->image(); + + struct jpeg_compress_struct cinfo; + JSAMPROW row_pointer[1]; + row_pointer[0] = 0; + + struct my_jpeg_destination_mgr *iod_dest = new my_jpeg_destination_mgr(iio); + struct my_error_mgr jerr; + + cinfo.err = jpeg_std_error(&jerr); + + jerr.error_exit = my_error_exit; + + if (!setjmp(jerr.setjmp_buffer)) { + jpeg_create_compress(&cinfo); + + cinfo.dest = iod_dest; + + cinfo.image_width = image.width(); + cinfo.image_height = image.height(); + + TQRgb* cmap=0; + bool gray=FALSE; + switch ( image.depth() ) { + case 1: + case 8: + cmap = image.colorTable(); + gray = TRUE; + int i; + for (i=image.numColors(); gray && i--; ) { + gray = gray & ( qRed(cmap[i]) == qGreen(cmap[i]) && + qRed(cmap[i]) == qBlue(cmap[i]) ); + } + cinfo.input_components = gray ? 1 : 3; + cinfo.in_color_space = gray ? JCS_GRAYSCALE : JCS_RGB; + break; + case 32: + cinfo.input_components = 3; + cinfo.in_color_space = JCS_RGB; + } + + jpeg_set_defaults(&cinfo); + + float diffInch = TQABS(image.dotsPerMeterX()*2.54/100. - qRound(image.dotsPerMeterX()*2.54/100.)) + + TQABS(image.dotsPerMeterY()*2.54/100. - qRound(image.dotsPerMeterY()*2.54/100.)); + float diffCm = (TQABS(image.dotsPerMeterX()/100. - qRound(image.dotsPerMeterX()/100.)) + + TQABS(image.dotsPerMeterY()/100. - qRound(image.dotsPerMeterY()/100.)))*2.54; + if (diffInch < diffCm) { + cinfo.density_unit = 1; // dots/inch + cinfo.X_density = qRound(image.dotsPerMeterX()*2.54/100.); + cinfo.Y_density = qRound(image.dotsPerMeterY()*2.54/100.); + } else { + cinfo.density_unit = 2; // dots/cm + cinfo.X_density = (image.dotsPerMeterX()+50) / 100; + cinfo.Y_density = (image.dotsPerMeterY()+50) / 100; + } + + int quality = iio->quality() >= 0 ? TQMIN(iio->quality(),100) : 75; +#if defined(Q_OS_UNIXWARE) + jpeg_set_quality(&cinfo, quality, B_TRUE /* limit to baseline-JPEG values */); + jpeg_start_compress(&cinfo, B_TRUE); +#else + jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); + jpeg_start_compress(&cinfo, TRUE); +#endif + + row_pointer[0] = new uchar[cinfo.image_width*cinfo.input_components]; + int w = cinfo.image_width; + while (cinfo.next_scanline < cinfo.image_height) { + uchar *row = row_pointer[0]; + switch ( image.depth() ) { + case 1: + if (gray) { + uchar* data = image.scanLine(cinfo.next_scanline); + if ( image.bitOrder() == TQImage::LittleEndian ) { + for (int i=0; i> 3)) & (1 << (i & 7))); + row[i] = qRed(cmap[bit]); + } + } else { + for (int i=0; i> 3)) & (1 << (7 -(i & 7)))); + row[i] = qRed(cmap[bit]); + } + } + } else { + uchar* data = image.scanLine(cinfo.next_scanline); + if ( image.bitOrder() == TQImage::LittleEndian ) { + for (int i=0; i> 3)) & (1 << (i & 7))); + *row++ = qRed(cmap[bit]); + *row++ = qGreen(cmap[bit]); + *row++ = qBlue(cmap[bit]); + } + } else { + for (int i=0; i> 3)) & (1 << (7 -(i & 7)))); + *row++ = qRed(cmap[bit]); + *row++ = qGreen(cmap[bit]); + *row++ = qBlue(cmap[bit]); + } + } + } + break; + case 8: + if (gray) { + uchar* pix = image.scanLine(cinfo.next_scanline); + for (int i=0; isetStatus(0); + } + + delete iod_dest; + delete [] row_pointer[0]; +} + +void qInitJpegIO() +{ + // Not much to go on - just 3 bytes: 0xFF, M_SOI, 0xFF + // Even the third is not strictly specified as retquired. + TQImageIO::defineIOHandler("JPEG", "^\377\330\377", 0, read_jpeg_image, write_jpeg_image); +} + +#endif diff --git a/src/kernel/qjpegio.h b/src/kernel/qjpegio.h new file mode 100644 index 000000000..410dc0b24 --- /dev/null +++ b/src/kernel/qjpegio.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Definition of JPEG TQImage IOHandler +** +** Created : 970621 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQJPEGIO_H +#define TQJPEGIO_H + +#include "qglobal.h" + +#ifndef QT_NO_IMAGEIO_JPEG + +void qInitJpegIO(); + +#endif // QT_NO_IMAGEIO_JPEG + +#endif // TQJPEGIO_H diff --git a/src/kernel/qkeycode.h b/src/kernel/qkeycode.h new file mode 100644 index 000000000..381cb95ed --- /dev/null +++ b/src/kernel/qkeycode.h @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Definition of keyboard codes +** +** Created : 931030 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQKEYCODE_H +#define TQKEYCODE_H + +#ifndef QT_H +#include "qnamespace.h" +#endif // QT_H + +// all key codes are now in the TQt namespace class + +#endif // TQKEYCODE_H diff --git a/src/kernel/qkeysequence.cpp b/src/kernel/qkeysequence.cpp new file mode 100644 index 000000000..589568733 --- /dev/null +++ b/src/kernel/qkeysequence.cpp @@ -0,0 +1,731 @@ +/**************************************************************************** +** +** Implementation of TQKeySequence class +** +** Created : 0108007 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qkeysequence.h" + +#ifndef QT_NO_ACCEL + +#include "qaccel.h" +#include "qshared.h" +#include "qvaluelist.h" +#ifndef QT_NO_REGEXP +# include "qregexp.h" +#endif + +#ifdef Q_WS_MAC +#define TQMAC_CTRL (TQString(TQChar(0x2318))) +#define TQMAC_META (TQString(TQChar(0x2303))) +#define TQMAC_ALT (TQString(TQChar(0x2325))) +#define TQMAC_SHIFT (TQString(TQChar(0x21E7))) +#endif + +/*! + \class TQKeySequence qkeysequence.h + \brief The TQKeySequence class encapsulates a key sequence as used + by accelerators. + + \ingroup misc + + A key sequence consists of up to four keyboard codes, each + optionally combined with modifiers, e.g. \c SHIFT, \c CTRL, \c + ALT, \c META, or \c UNICODE_ACCEL. For example, \c{CTRL + Key_P} + might be a sequence used as a shortcut for printing a document. + The key codes are listed in \c{qnamespace.h}. As an alternative, + use \c UNICODE_ACCEL with the unicode code point of the character. + For example, \c{UNICODE_ACCEL + 'A'} gives the same key sequence + as \c Key_A. + + Key sequences can be constructed either from an integer key code, + or from a human readable translatable string such as + "Ctrl+X,Alt+Space". A key sequence can be cast to a TQString to + obtain a human readable translated version of the sequence. + Translations are done in the "TQAccel" context. + + \sa TQAccel +*/ + +/*! + \enum TQt::SequenceMatch + + \value NoMatch Sequences have nothing in common + \value PartialMatch Sequences match partially, but are not complete + \value Identical Sequences do not differ +*/ + +static struct { + int key; + const char* name; +} keyname[] = { + { TQt::Key_Space, QT_TRANSLATE_NOOP( "TQAccel", "Space" ) }, + { TQt::Key_Escape, QT_TRANSLATE_NOOP( "TQAccel", "Esc" ) }, + { TQt::Key_Tab, QT_TRANSLATE_NOOP( "TQAccel", "Tab" ) }, + { TQt::Key_Backtab, QT_TRANSLATE_NOOP( "TQAccel", "Backtab" ) }, + { TQt::Key_Backspace, QT_TRANSLATE_NOOP( "TQAccel", "Backspace" ) }, + { TQt::Key_Return, QT_TRANSLATE_NOOP( "TQAccel", "Return" ) }, + { TQt::Key_Enter, QT_TRANSLATE_NOOP( "TQAccel", "Enter" ) }, + { TQt::Key_Insert, QT_TRANSLATE_NOOP( "TQAccel", "Ins" ) }, + { TQt::Key_Delete, QT_TRANSLATE_NOOP( "TQAccel", "Del" ) }, + { TQt::Key_Pause, QT_TRANSLATE_NOOP( "TQAccel", "Pause" ) }, + { TQt::Key_Print, QT_TRANSLATE_NOOP( "TQAccel", "Print" ) }, + { TQt::Key_SysReq, QT_TRANSLATE_NOOP( "TQAccel", "SysReq" ) }, + { TQt::Key_Home, QT_TRANSLATE_NOOP( "TQAccel", "Home" ) }, + { TQt::Key_End, QT_TRANSLATE_NOOP( "TQAccel", "End" ) }, + { TQt::Key_Left, QT_TRANSLATE_NOOP( "TQAccel", "Left" ) }, + { TQt::Key_Up, QT_TRANSLATE_NOOP( "TQAccel", "Up" ) }, + { TQt::Key_Right, QT_TRANSLATE_NOOP( "TQAccel", "Right" ) }, + { TQt::Key_Down, QT_TRANSLATE_NOOP( "TQAccel", "Down" ) }, + { TQt::Key_Prior, QT_TRANSLATE_NOOP( "TQAccel", "PgUp" ) }, + { TQt::Key_Next, QT_TRANSLATE_NOOP( "TQAccel", "PgDown" ) }, + { TQt::Key_CapsLock, QT_TRANSLATE_NOOP( "TQAccel", "CapsLock" ) }, + { TQt::Key_NumLock, QT_TRANSLATE_NOOP( "TQAccel", "NumLock" ) }, + { TQt::Key_ScrollLock, QT_TRANSLATE_NOOP( "TQAccel", "ScrollLock" ) }, + { TQt::Key_Menu, QT_TRANSLATE_NOOP( "TQAccel", "Menu" ) }, + { TQt::Key_Help, QT_TRANSLATE_NOOP( "TQAccel", "Help" ) }, + + // Multimedia keys + { TQt::Key_Back, QT_TRANSLATE_NOOP( "TQAccel", "Back" ) }, + { TQt::Key_Forward, QT_TRANSLATE_NOOP( "TQAccel", "Forward" ) }, + { TQt::Key_Stop, QT_TRANSLATE_NOOP( "TQAccel", "Stop" ) }, + { TQt::Key_Refresh, QT_TRANSLATE_NOOP( "TQAccel", "Refresh" ) }, + { TQt::Key_VolumeDown, QT_TRANSLATE_NOOP( "TQAccel", "Volume Down" ) }, + { TQt::Key_VolumeMute, QT_TRANSLATE_NOOP( "TQAccel", "Volume Mute" ) }, + { TQt::Key_VolumeUp, QT_TRANSLATE_NOOP( "TQAccel", "Volume Up" ) }, + { TQt::Key_BassBoost, QT_TRANSLATE_NOOP( "TQAccel", "Bass Boost" ) }, + { TQt::Key_BassUp, QT_TRANSLATE_NOOP( "TQAccel", "Bass Up" ) }, + { TQt::Key_BassDown, QT_TRANSLATE_NOOP( "TQAccel", "Bass Down" ) }, + { TQt::Key_TrebleUp, QT_TRANSLATE_NOOP( "TQAccel", "Treble Up" ) }, + { TQt::Key_TrebleDown, QT_TRANSLATE_NOOP( "TQAccel", "Treble Down" ) }, + { TQt::Key_MediaPlay, QT_TRANSLATE_NOOP( "TQAccel", "Media Play" ) }, + { TQt::Key_MediaStop, QT_TRANSLATE_NOOP( "TQAccel", "Media Stop" ) }, + { TQt::Key_MediaPrev, QT_TRANSLATE_NOOP( "TQAccel", "Media Previous" ) }, + { TQt::Key_MediaNext, QT_TRANSLATE_NOOP( "TQAccel", "Media Next" ) }, + { TQt::Key_MediaRecord, QT_TRANSLATE_NOOP( "TQAccel", "Media Record" ) }, + { TQt::Key_HomePage, QT_TRANSLATE_NOOP( "TQAccel", "Home" ) }, + { TQt::Key_Favorites, QT_TRANSLATE_NOOP( "TQAccel", "Favorites" ) }, + { TQt::Key_Search, QT_TRANSLATE_NOOP( "TQAccel", "Search" ) }, + { TQt::Key_Standby, QT_TRANSLATE_NOOP( "TQAccel", "Standby" ) }, + { TQt::Key_OpenUrl, QT_TRANSLATE_NOOP( "TQAccel", "Open URL" ) }, + { TQt::Key_LaunchMail, QT_TRANSLATE_NOOP( "TQAccel", "Launch Mail" ) }, + { TQt::Key_LaunchMedia, QT_TRANSLATE_NOOP( "TQAccel", "Launch Media" ) }, + { TQt::Key_Launch0, QT_TRANSLATE_NOOP( "TQAccel", "Launch (0)" ) }, + { TQt::Key_Launch1, QT_TRANSLATE_NOOP( "TQAccel", "Launch (1)" ) }, + { TQt::Key_Launch2, QT_TRANSLATE_NOOP( "TQAccel", "Launch (2)" ) }, + { TQt::Key_Launch3, QT_TRANSLATE_NOOP( "TQAccel", "Launch (3)" ) }, + { TQt::Key_Launch4, QT_TRANSLATE_NOOP( "TQAccel", "Launch (4)" ) }, + { TQt::Key_Launch5, QT_TRANSLATE_NOOP( "TQAccel", "Launch (5)" ) }, + { TQt::Key_Launch6, QT_TRANSLATE_NOOP( "TQAccel", "Launch (6)" ) }, + { TQt::Key_Launch7, QT_TRANSLATE_NOOP( "TQAccel", "Launch (7)" ) }, + { TQt::Key_Launch8, QT_TRANSLATE_NOOP( "TQAccel", "Launch (8)" ) }, + { TQt::Key_Launch9, QT_TRANSLATE_NOOP( "TQAccel", "Launch (9)" ) }, + { TQt::Key_LaunchA, QT_TRANSLATE_NOOP( "TQAccel", "Launch (A)" ) }, + { TQt::Key_LaunchB, QT_TRANSLATE_NOOP( "TQAccel", "Launch (B)" ) }, + { TQt::Key_LaunchC, QT_TRANSLATE_NOOP( "TQAccel", "Launch (C)" ) }, + { TQt::Key_LaunchD, QT_TRANSLATE_NOOP( "TQAccel", "Launch (D)" ) }, + { TQt::Key_LaunchE, QT_TRANSLATE_NOOP( "TQAccel", "Launch (E)" ) }, + { TQt::Key_LaunchF, QT_TRANSLATE_NOOP( "TQAccel", "Launch (F)" ) }, + + // -------------------------------------------------------------- + // More consistent namings + { TQt::Key_Print, QT_TRANSLATE_NOOP( "TQAccel", "Print Screen" ) }, + { TQt::Key_Prior, QT_TRANSLATE_NOOP( "TQAccel", "Page Up" ) }, + { TQt::Key_Next, QT_TRANSLATE_NOOP( "TQAccel", "Page Down" ) }, + { TQt::Key_CapsLock, QT_TRANSLATE_NOOP( "TQAccel", "Caps Lock" ) }, + { TQt::Key_NumLock, QT_TRANSLATE_NOOP( "TQAccel", "Num Lock" ) }, + { TQt::Key_NumLock, QT_TRANSLATE_NOOP( "TQAccel", "Number Lock" ) }, + { TQt::Key_ScrollLock, QT_TRANSLATE_NOOP( "TQAccel", "Scroll Lock" ) }, + { TQt::Key_Insert, QT_TRANSLATE_NOOP( "TQAccel", "Insert" ) }, + { TQt::Key_Delete, QT_TRANSLATE_NOOP( "TQAccel", "Delete" ) }, + { TQt::Key_Escape, QT_TRANSLATE_NOOP( "TQAccel", "Escape" ) }, + { TQt::Key_SysReq, QT_TRANSLATE_NOOP( "TQAccel", "System Request" ) }, + + { 0, 0 } +}; + + +class TQKeySequencePrivate : public TQShared +{ +public: + inline TQKeySequencePrivate() + { + key[0] = key[1] = key[2] = key[3] = 0; + } + inline TQKeySequencePrivate( TQKeySequencePrivate *copy ) + { + key[0] = copy->key[0]; + key[1] = copy->key[1]; + key[2] = copy->key[2]; + key[3] = copy->key[3]; + } + int key[4]; +}; + + +/*! + Constructs an empty key sequence. +*/ +TQKeySequence::TQKeySequence() +{ + d = new TQKeySequencePrivate(); + Q_CHECK_PTR( d ); +} + +/*! + Creates a key sequence from the string \a key. For example + "Ctrl+O" gives CTRL+UNICODE_ACCEL+'O'. The strings "Ctrl", + "Shift", "Alt" and "Meta" are recognized, as well as their + translated equivalents in the "TQAccel" context (using + TQObject::tr()). + + Multiple key codes (up to four) may be entered by separating them + with commas, e.g. "Alt+X,Ctrl+S,Q". + + This contructor is typically used with \link TQObject::tr() tr + \endlink(), so that accelerator keys can be replaced in + translations: + + \code + TQPopupMenu *file = new TQPopupMenu( this ); + file->insertItem( tr("&Open..."), this, SLOT(open()), + TQKeySequence( tr("Ctrl+O", "File|Open") ) ); + \endcode + + Note the \c "File|Open" translator comment. It is by no means + necessary, but it provides some context for the human translator. +*/ +TQKeySequence::TQKeySequence( const TQString& key ) +{ + d = new TQKeySequencePrivate(); + Q_CHECK_PTR( d ); + assign( key ); +} + + +// ### BCI: Merge with constructor below for 4.0 +/*! + Constructs a key sequence that has a single \a key. + + The key codes are listed in \c{qnamespace.h} and can be + combined with modifiers, e.g. with \c SHIFT, \c CTRL, \c + ALT, \c META or \c UNICODE_ACCEL. +*/ +TQKeySequence::TQKeySequence( int key ) +{ + d = new TQKeySequencePrivate(); + Q_CHECK_PTR( d ); + d->key[0] = key; +} + +/*! + Constructs a key sequence with up to 4 keys \a k1, \a k2, + \a k3 and \a k4. + + The key codes are listed in \c{qnamespace.h} and can be + combined with modifiers, e.g. with \c SHIFT, \c CTRL, \c + ALT, \c META or \c UNICODE_ACCEL. +*/ +TQKeySequence::TQKeySequence( int k1, int k2, int k3, int k4 ) +{ + d = new TQKeySequencePrivate(); + Q_CHECK_PTR( d ); + d->key[0] = k1; + d->key[1] = k2; + d->key[2] = k3; + d->key[3] = k4; +} + +/*! + Copy constructor. Makes a copy of \a keysequence. + */ +TQKeySequence::TQKeySequence( const TQKeySequence& keysequence ) + : d( keysequence.d ) +{ + d->ref(); +} + + +/*! + Destroys the key sequence. + */ +TQKeySequence::~TQKeySequence() +{ + if ( d->deref() ) + delete d; +} + +/*! + \internal + KeySequences should never be modified, but rather just created. + Internally though we do need to modify to keep pace in event + delivery. +*/ + +void TQKeySequence::setKey( int key, int index ) +{ +#ifdef QT_CHECK_STATE + if ( 0 > index && 4 < index ) { + qWarning( "TQKeySequence::setKey: index %u out of range", index ); + return; + } +#endif // QT_CHECK_STATE + + if ( 1 < d->count ) { + TQKeySequencePrivate *newd = new TQKeySequencePrivate( d ); + d->deref(); + d = newd; + } + d->key[index] = key; +} + +/*! + Returns the number of keys in the key sequence. + The maximum is 4. + */ +uint TQKeySequence::count() const +{ + if ( ! d->key[0] ) + return 0; + if ( ! d->key[1] ) + return 1; + if ( ! d->key[2] ) + return 2; + if ( ! d->key[3] ) + return 3; + return 4; +} + + +/*! + Returns TRUE if the key sequence is empty; otherwise returns + FALSE. +*/ +bool TQKeySequence::isEmpty() const +{ + return !d->key[0]; +} + + +/*! + Adds the string \a keyseq to the key sequence. \a keyseq may + contain up to four key codes, provided they are seperated by a + comma, e.g. "Alt+X,Ctrl+S,Z"). Returns the number of key codes + added. +*/ +int TQKeySequence::assign( TQString keyseq ) +{ + TQString part; + int n = 0; + int p = 0, diff = 0; + + // Run through the whole string, but stop + // if we have 4 keys before the end. + while ( keyseq.length() && n < 4 ) { + // We MUST use something to seperate each sequence, and space + // does not cut it, since some of the key names have space + // in them.. (Let's hope no one translate with a comma in it:) + p = keyseq.find( ',' ); + if ( -1 != p ) { + if ( ',' == keyseq[p+1] ) // e.g. 'Ctrl+,, Shift+,,' + p++; + if ( ' ' == keyseq[p+1] ) { // Space after comma + diff = 1; + p++; + } else if ( '\0' == keyseq[p+1] ) { // Last comma 'Ctrl+,' + p = -1; + } else { + diff = 0; + } + } + part = keyseq.left( -1==p?keyseq.length():p-diff ); + keyseq = keyseq.right( -1==p?0:keyseq.length() - ( p + 1 ) ); + d->key[n] = decodeString( part ); + n++; + } + return n; +} + +struct ModifKeyName { + ModifKeyName() { } + ModifKeyName(int q, TQString n) : qt_key(q), name(n) { } + int qt_key; + TQString name; +}; + +/*! + Constructs a single key from the string \str. + */ +int TQKeySequence::decodeString( const TQString& str ) +{ + int ret = 0; + TQString accel = str; + + TQValueList modifs; +#ifdef TQMAC_CTRL + modifs << ModifKeyName( CTRL, TQMAC_CTRL ); +#endif +#ifdef TQMAC_ALT + modifs << ModifKeyName( ALT, TQMAC_ALT ); +#endif +#ifdef TQMAC_META + modifs << ModifKeyName( META, TQMAC_META ); +#endif +#ifdef TQMAC_SHIFT + modifs << ModifKeyName( SHIFT, TQMAC_SHIFT ); +#endif + modifs << ModifKeyName( CTRL, "ctrl+" ) << ModifKeyName( CTRL, TQAccel::tr("Ctrl").lower().append('+') ); + modifs << ModifKeyName( SHIFT, "shift+" ) << ModifKeyName( SHIFT, TQAccel::tr("Shift").lower().append('+') ); + modifs << ModifKeyName( ALT, "alt+" ) << ModifKeyName( ALT, TQAccel::tr("Alt").lower().append('+') ); + modifs << ModifKeyName( META, "meta+" ) << ModifKeyName( ALT, TQAccel::tr("Meta").lower().append('+') ); + TQString sl = accel.lower(); + for( TQValueList::iterator it = modifs.begin(); it != modifs.end(); ++it ) { + if ( sl.contains( (*it).name ) ) { + ret |= (*it).qt_key; +#ifndef QT_NO_REGEXP + accel.remove( TQRegExp(TQRegExp::escape((*it).name), FALSE) ); +#else + accel.remove( (*it).name ); +#endif + sl = accel.lower(); + } + } + + int p = accel.findRev( '+', str.length() - 2 ); // -2 so that Ctrl++ works + if( p > 0 ) + accel = accel.mid( p + 1 ); + + int fnum = 0; + if ( accel.length() == 1 ) { + char ltr = accel[0].upper().latin1(); + // We can only upper A-Z without problems. + if ( ltr < (char)Key_A || ltr > (char)Key_Z ) + ret |= accel[0].unicode(); + else + ret |= accel[0].upper().unicode(); + ret |= UNICODE_ACCEL; + } else if ( accel[0] == 'F' && (fnum = accel.mid(1).toInt()) && (fnum >= 1) && (fnum <= 35) ) { + ret |= Key_F1 + fnum - 1; + } else { + // Check through translation table for the correct key name + // ...or fall back on english table. + bool found = FALSE; + for ( int tran = 0; tran < 2; tran++ ) { + for ( int i = 0; keyname[i].name; i++ ) { + if ( tran ? accel == TQAccel::tr(keyname[i].name) + : accel == keyname[i].name ) { + ret |= keyname[i].key; + found = TRUE; + break; + } + } + if(found) + break; + } + } + return ret; +} + + +/*! + Creates an accelerator string for \a key. For example, + CTRL+Key_O gives "Ctrl+O". The strings, "Ctrl", "Shift", etc. are + translated (using TQObject::tr()) in the "TQAccel" context. + */ +TQString TQKeySequence::encodeString( int key ) +{ + TQString s; +#if defined(Q_OS_MAC) && !defined(TQWS) + // On MAC the order is Meta, Alt, Shift, Control. + if ( (key & META) == META ) + s += TQMAC_META; + if ( (key & ALT) == ALT ) + s += TQMAC_ALT; + if ( (key & SHIFT) == SHIFT ) + s += TQMAC_SHIFT; + if ( (key & CTRL) == CTRL ) + s += TQMAC_CTRL; +#else + // On other systems the order is Meta, Control, Alt, Shift + if ( (key & META) == META ) + s += TQAccel::tr( "Meta" ); + if ( (key & CTRL) == CTRL ) { + if ( !s.isEmpty() ) + s += TQAccel::tr( "+" ); + s += TQAccel::tr( "Ctrl" ); + } + if ( (key & ALT) == ALT ) { + if ( !s.isEmpty() ) + s += TQAccel::tr( "+" ); + s += TQAccel::tr( "Alt" ); + } + if ( (key & SHIFT) == SHIFT ) { + if ( !s.isEmpty() ) + s += TQAccel::tr( "+" ); + s += TQAccel::tr( "Shift" ); + } +#endif + + + key &= ~(SHIFT | CTRL | ALT | META ); + TQString p; + + if ( (key & UNICODE_ACCEL) == UNICODE_ACCEL ) { + // Note: This character should NOT be upper()'ed, since + // the encoded string should indicate EXACTLY what the + // key represents! Hence a 'Ctrl+Shift+c' is posible to + // represent, but is clearly impossible to trigger... + p = TQChar(key & 0xffff); + } else if ( key >= Key_F1 && key <= Key_F35 ) { + p = TQAccel::tr( "F%1" ).arg(key - Key_F1 + 1); + } else if ( key > Key_Space && key <= Key_AsciiTilde ) { + p.sprintf( "%c", key ); + } else { + int i=0; + while (keyname[i].name) { + if ( key == keyname[i].key ) { + p = TQAccel::tr(keyname[i].name); + break; + } + ++i; + } + // If we can't find the actual translatable keyname, + // fall back on the unicode representation of it... + // Or else characters like Key_aring may not get displayed + // ( Really depends on you locale ) + if ( !keyname[i].name ) + // Note: This character should NOT be upper()'ed, see above! + p = TQChar(key & 0xffff); + } + +#ifndef Q_OS_MAC + if ( !s.isEmpty() ) + s += TQAccel::tr( "+" ); +#endif + + s += p; + return s; +} + +/*! + Matches the sequence with \a seq. Returns \c TQt::Identical if + successful, \c TQt::PartialMatch for matching but incomplete \a seq, + and \c TQt::NoMatch if the sequences have nothing in common. + Returns \c TQt::NoMatch if \a seq is shorter. +*/ +TQt::SequenceMatch TQKeySequence::matches( const TQKeySequence& seq ) const +{ + uint userN = count(), + seqN = seq.count(); + + if ( userN > seqN ) + return NoMatch; + + // If equal in length, we have a potential Identical sequence, + // else we already know it can only be partial. + SequenceMatch match = ( userN == seqN ? Identical : PartialMatch ); + + for ( uint i = 0; i < userN; i++ ) { + int userKey = (*this)[i], + sequenceKey = seq[i]; + + if ( (userKey & ~TQt::UNICODE_ACCEL) != + (sequenceKey & ~TQt::UNICODE_ACCEL) ) + return NoMatch; + } + return match; +} + + +/*! + Creates an accelerator string for the key sequence. + For instance CTRL+Key_O gives "Ctrl+O". If the key sequence has + multiple key codes they are returned comma-separated, e.g. + "Alt+X, Ctrl+Y, Z". The strings, "Ctrl", "Shift", etc. are + translated (using TQObject::tr()) in the "TQAccel" scope. If the key + sequence has no keys, TQString::null is returned. + + On Mac OS X, the string returned resembles the sequence that is shown in + the menubar. +*/ +TQKeySequence::operator TQString() const +{ + int end = count(); + if ( !end ) return TQString::null; + + TQString complete; + int i = 0; + while ( i < end ) { + complete += encodeString( d->key[i] ); + i++; + if ( i != end) + complete += ", "; + } + return complete; +} + + +/*! + \obsolete + For backward compatibility: returns the first keycode + as integer. If the key sequence is empty, 0 is returned. + */ +TQKeySequence::operator int () const +{ + if ( 1 <= count() ) + return d->key[0]; + return 0; +} + + +/*! + Returns a reference to the element at position \a index in the key + sequence. This can only be used to read an element. + */ +int TQKeySequence::operator[]( uint index ) const +{ +#ifdef QT_CHECK_STATE + if ( index > 4 ) { + qWarning( "TQKeySequence::operator[]: index %u out of range", index ); + return 0; + } +#endif // QT_CHECK_STATE + return d->key[index]; +} + + +/*! + Assignment operator. Assigns \a keysequence to this + object. + */ +TQKeySequence &TQKeySequence::operator=( const TQKeySequence & keysequence ) +{ + keysequence.d->ref(); + if ( d->deref() ) + delete d; + d = keysequence.d; + return *this; +} + + +/*! + Returns TRUE if \a keysequence is equal to this key + sequence; otherwise returns FALSE. + */ + + +bool TQKeySequence::operator==( const TQKeySequence& keysequence ) const +{ + return ( (d->key[0]&~UNICODE_ACCEL) == (keysequence.d->key[0]&~UNICODE_ACCEL) && + (d->key[1]&~UNICODE_ACCEL) == (keysequence.d->key[1]&~UNICODE_ACCEL) && + (d->key[2]&~UNICODE_ACCEL) == (keysequence.d->key[2]&~UNICODE_ACCEL) && + (d->key[3]&~UNICODE_ACCEL) == (keysequence.d->key[3]&~UNICODE_ACCEL) ); +} + + +/*! + Returns TRUE if \a keysequence is not equal to this key sequence; + otherwise returns FALSE. +*/ +bool TQKeySequence::operator!= ( const TQKeySequence& keysequence ) const +{ + TQKeySequence *that = (TQKeySequence*)this; + return !( (*that) == keysequence ); +} + + +/***************************************************************************** + TQKeySequence stream functions + *****************************************************************************/ +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +/*! + \relates TQKeySequence + + Writes the key sequence \a keysequence to the stream \a s. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ +TQDataStream &operator<<( TQDataStream &s, const TQKeySequence &keysequence ) +{ + TQValueList list; + list += keysequence.d->key[0]; + list += keysequence.d->key[1]; + list += keysequence.d->key[2]; + list += keysequence.d->key[3]; + s << list; + + return s; +} + + +/*! + \relates TQKeySequence + + Reads a key sequence from the stream \a s into the key sequence \a + keysequence. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ +TQDataStream &operator>>( TQDataStream &s, TQKeySequence &keysequence ) +{ + TQValueList list; + s >> list; + +#ifdef QT_CHECK_STATE + if ( 1 != list.count() && 4 != list.count() ) { + qWarning( "Invalid TQKeySequence data in the datastream." ); + return s; + } +#endif + + if ( 1 == list.count() ) { + keysequence.d->key[0] = *list.at( 0 ); + keysequence.d->key[1] = + keysequence.d->key[2] = + keysequence.d->key[3] = 0; + } else { + keysequence.d->key[0] = *list.at( 0 ); + keysequence.d->key[1] = *list.at( 1 ); + keysequence.d->key[2] = *list.at( 2 ); + keysequence.d->key[3] = *list.at( 3 ); + } + return s; +} + +#endif //QT_NO_DATASTREAM + +#endif //QT_NO_ACCEL diff --git a/src/kernel/qkeysequence.h b/src/kernel/qkeysequence.h new file mode 100644 index 000000000..2ea11f4c5 --- /dev/null +++ b/src/kernel/qkeysequence.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Definition of TQKeySequence class +** +** Created : 0108007 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQKEYSETQUENCE_H +#define TQKEYSETQUENCE_H + +#ifndef QT_H +#ifndef QT_H +#include "qnamespace.h" +#include "qstring.h" +#endif // QT_H +#endif + +#ifndef QT_NO_ACCEL + +/***************************************************************************** + TQKeySequence stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +class TQKeySequence; +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQKeySequence & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQKeySequence & ); +#endif + +class TQKeySequencePrivate; + +class Q_EXPORT TQKeySequence : public TQt +{ +public: + TQKeySequence(); + TQKeySequence( const TQString& key ); + TQKeySequence( int key ); + TQKeySequence( int k1, int k2, int k3 = 0, int k4 = 0 ); + TQKeySequence( const TQKeySequence & ); + ~TQKeySequence(); + + uint count() const; + bool isEmpty() const; + TQt::SequenceMatch matches( const TQKeySequence & ) const; + + operator TQString() const; + operator int () const; + int operator[]( uint ) const; + TQKeySequence &operator=( const TQKeySequence & ); + bool operator==( const TQKeySequence& ) const; + bool operator!= ( const TQKeySequence& ) const; + +private: + static int decodeString( const TQString & ); + static TQString encodeString( int ); + int assign( TQString ); + void setKey( int key, int index ); + + TQKeySequencePrivate* d; + + friend Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQKeySequence & ); + friend Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQKeySequence & ); + friend class TQAccelManager; +}; + +#else + +class Q_EXPORT TQKeySequence : public TQt +{ +public: + TQKeySequence() {} + TQKeySequence( int ) {} +}; + +#endif //QT_NO_ACCEL + +#endif diff --git a/src/kernel/qlayout.cpp b/src/kernel/qlayout.cpp new file mode 100644 index 000000000..6add8a3c3 --- /dev/null +++ b/src/kernel/qlayout.cpp @@ -0,0 +1,2605 @@ +/**************************************************************************** +** +** Implementation of layout classes +** +** Created : 960416 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qlayout.h" + +#ifndef QT_NO_LAYOUT + +#include "qapplication.h" +#include "qwidget.h" +#include "qptrlist.h" +#include "qsizepolicy.h" + +#include "qlayoutengine_p.h" + +/* + Three internal classes related to TQGridLayout: (1) TQGridBox is a + TQLayoutItem with (row, column) information; (2) TQGridMultiBox is a + TQGridBox with (torow, tocolumn) information; (3) TQGridLayoutData is + the internal representation of a TQGridLayout. +*/ + +class TQGridBox +{ +public: + TQGridBox( TQLayoutItem *lit ) { item_ = lit; } + + TQGridBox( TQWidget *wid ) { item_ = new TQWidgetItem( wid ); } + TQGridBox( int w, int h, TQSizePolicy::SizeType hData = TQSizePolicy::Minimum, + TQSizePolicy::SizeType vData = TQSizePolicy::Minimum ) + { item_ = new TQSpacerItem( w, h, hData, vData ); } + ~TQGridBox() { delete item_; } + + TQSize sizeHint() const { return item_->sizeHint(); } + TQSize minimumSize() const { return item_->minimumSize(); } + TQSize maximumSize() const { return item_->maximumSize(); } + TQSizePolicy::ExpandData expanding() const { return item_->expanding(); } + bool isEmpty() const { return item_->isEmpty(); } + + bool hasHeightForWidth() const { return item_->hasHeightForWidth(); } + int heightForWidth( int w ) const { return item_->heightForWidth(w); } + + void setAlignment( int a ) { item_->setAlignment( a ); } + void setGeometry( const TQRect &r ) { item_->setGeometry( r ); } + int alignment() const { return item_->alignment(); } + TQLayoutItem *item() { return item_; } + TQLayoutItem *takeItem() { TQLayoutItem *i = item_; item_ = 0; return i; } + + int hStretch() { return item_->widget() ? + item_->widget()->sizePolicy().horStretch() : 0; } + int vStretch() { return item_->widget() ? + item_->widget()->sizePolicy().verStretch() : 0; } + +private: + friend class TQGridLayoutData; + friend class TQGridLayoutDataIterator; + + TQLayoutItem *item_; + int row, col; +}; + +class TQGridMultiBox +{ +public: + TQGridMultiBox( TQGridBox *box, int toRow, int toCol ) + : box_( box ), torow( toRow ), tocol( toCol ) { } + ~TQGridMultiBox() { delete box_; } + TQGridBox *box() { return box_; } + TQLayoutItem *takeItem() { return box_->takeItem(); } + +private: + friend class TQGridLayoutData; + friend class TQGridLayoutDataIterator; + + TQGridBox *box_; + int torow, tocol; +}; + +class TQGridLayoutData +{ +public: + TQGridLayoutData(); + TQGridLayoutData( int nRows, int nCols ); + ~TQGridLayoutData(); + + void add( TQGridBox*, int row, int col ); + void add( TQGridBox*, int row1, int row2, int col1, int col2 ); + TQSize sizeHint( int ) const; + TQSize minimumSize( int ) const; + TQSize maximumSize( int ) const; + + TQSizePolicy::ExpandData expanding( int spacing ); + + void distribute( TQRect, int ); + inline int numRows() const { return rr; } + inline int numCols() const { return cc; } + inline void expand( int rows, int cols ) + { setSize( TQMAX(rows, rr), TQMAX(cols, cc) ); } + inline void setRowStretch( int r, int s ) + { expand( r + 1, 0 ); rStretch[r] = s; setDirty(); } + inline void setColStretch( int c, int s ) + { expand( 0, c + 1 ); cStretch[c] = s; setDirty(); } + inline int rowStretch( int r ) const { return rStretch[r]; } + inline int colStretch( int c ) const { return cStretch[c]; } + inline void setRowSpacing( int r, int s ) + { expand( r + 1, 0 ); rSpacing[r] = s; setDirty(); } + inline void setColSpacing( int c, int s ) + { expand( 0, c + 1 ); cSpacing[c] = s; setDirty(); } + inline int rowSpacing( int r ) const { return rSpacing[r]; } + inline int colSpacing( int c ) const { return cSpacing[c]; } + + inline void setReversed( bool r, bool c ) { hReversed = c; vReversed = r; } + inline bool horReversed() const { return hReversed; } + inline bool verReversed() const { return vReversed; } + inline void setDirty() { needRecalc = TRUE; hfw_width = -1; } + inline bool isDirty() const { return needRecalc; } + bool hasHeightForWidth( int space ); + int heightForWidth( int, int, int ); + int minimumHeightForWidth( int, int, int ); + + bool findWidget( TQWidget* w, int *row, int *col ); + + inline void getNextPos( int &row, int &col ) { row = nextR; col = nextC; } + inline uint count() const + { return things.count() + ( multi ? multi->count() : 0 ); } + TQRect cellGeometry( int row, int col ) const; + +private: + void setNextPosAfter( int r, int c ); + void recalcHFW( int w, int s ); + void addHfwData ( TQGridBox *box, int width ); + void init(); + TQSize findSize( TQCOORD TQLayoutStruct::*, int ) const; + void addData( TQGridBox *b, bool r = TRUE, bool c = TRUE ); + void setSize( int rows, int cols ); + void setupLayoutData( int space ); + void setupHfwLayoutData( int space ); + + int rr; + int cc; + TQMemArray rowData; + TQMemArray colData; + TQMemArray *hfwData; + TQMemArray rStretch; + TQMemArray cStretch; + TQMemArray rSpacing; + TQMemArray cSpacing; + TQPtrList things; + TQPtrList *multi; + + int hfw_width; + int hfw_height; + int hfw_minheight; + int nextR; + int nextC; + + uint hReversed : 1; + uint vReversed : 1; + uint needRecalc : 1; + uint has_hfw : 1; + uint addVertical : 1; + + friend class TQGridLayoutDataIterator; +}; + +TQGridLayoutData::TQGridLayoutData() +{ + init(); +} + +TQGridLayoutData::TQGridLayoutData( int nRows, int nCols ) + : rowData( 0 ), colData( 0 ) +{ + init(); + if ( nRows < 0 ) { + nRows = 1; + addVertical = FALSE; + } + if ( nCols < 0 ) { + nCols = 1; + addVertical = TRUE; + } + setSize( nRows, nCols ); +} + +TQGridLayoutData::~TQGridLayoutData() +{ + // must be cleared while the data is still in a stable state + things.clear(); + + delete multi; + delete hfwData; +} + +void TQGridLayoutData::init() +{ + addVertical = FALSE; + setDirty(); + multi = 0; + rr = cc = 0; + nextR = nextC = 0; + hfwData = 0; + things.setAutoDelete( TRUE ); + hReversed = FALSE; + vReversed = FALSE; +} + +bool TQGridLayoutData::hasHeightForWidth( int spacing ) +{ + setupLayoutData( spacing ); + return has_hfw; +} + +/* + Assumes that setupLayoutData() has been called, and that + qGeomCalc() has filled in colData with appropriate values. +*/ +void TQGridLayoutData::recalcHFW( int w, int spacing ) +{ + /* + Go through all children, using colData and heightForWidth() + and put the results in hfw_rowData. + */ + if ( !hfwData ) + hfwData = new TQMemArray( rr ); + setupHfwLayoutData( spacing ); + TQMemArray &rData = *hfwData; + + int h = 0; + int mh = 0; + int n = 0; + for ( int r = 0; r < rr; r++ ) { + h += rData[r].sizeHint; + mh += rData[r].minimumSize; + if ( !rData[r].empty ) + n++; + } + if ( n ) { + h += ( n - 1 ) * spacing; + mh += ( n - 1 ) * spacing; + } + + hfw_width = w; + hfw_height = TQMIN( TQLAYOUTSIZE_MAX, h ); + hfw_minheight = TQMIN( TQLAYOUTSIZE_MAX, mh ); +} + +int TQGridLayoutData::heightForWidth( int w, int margin, int spacing ) +{ + setupLayoutData( spacing ); + if ( !has_hfw ) + return -1; + if ( w + 2*margin != hfw_width ) { + qGeomCalc( colData, 0, cc, 0, w+2*margin, spacing ); + recalcHFW( w+2*margin, spacing ); + } + return hfw_height + 2*margin; +} + +int TQGridLayoutData::minimumHeightForWidth( int w, int margin, int spacing ) +{ + (void) heightForWidth( w, margin, spacing ); + return has_hfw ? (hfw_minheight + 2*margin) : -1; +} + +bool TQGridLayoutData::findWidget( TQWidget* w, int *row, int *col ) +{ + TQPtrListIterator it( things ); + TQGridBox * box; + while ( (box = it.current()) != 0 ) { + ++it; + if ( box->item()->widget() == w ) { + if ( row ) + *row = box->row; + if ( col ) + *col = box->col; + return TRUE; + } + } + if ( multi ) { + TQPtrListIterator it( *multi ); + TQGridMultiBox * mbox; + while ( (mbox = it.current()) != 0 ) { + ++it; + box = mbox->box(); + if ( box->item()->widget() == w ) { + if ( row ) + *row = box->row; + if ( col ) + *col = box->col; + return TRUE; + } + + } + } + return FALSE; +} + +TQSize TQGridLayoutData::findSize( TQCOORD TQLayoutStruct::*size, int spacer ) const +{ + TQGridLayoutData *that = (TQGridLayoutData *)this; + that->setupLayoutData( spacer ); + + int w = 0; + int h = 0; + int n = 0; + for ( int r = 0; r < rr; r++ ) { + h = h + rowData[r].*size; + if ( !rowData[r].empty ) + n++; + } + if ( n ) + h += ( n - 1 ) * spacer; + n = 0; + for ( int c = 0; c < cc; c++ ) { + w = w + colData[c].*size; + if ( !colData[c].empty ) + n++; + } + if ( n ) + w += ( n - 1 ) * spacer; + w = TQMIN( TQLAYOUTSIZE_MAX, w ); + h = TQMIN( TQLAYOUTSIZE_MAX, h ); + + return TQSize( w, h ); +} + +TQSizePolicy::ExpandData TQGridLayoutData::expanding( int spacing ) +{ + setupLayoutData( spacing ); + int ret = 0; + + for ( int r = 0; r < rr; r++ ) { + if ( rowData[r].expansive ) { + ret |= (int) TQSizePolicy::Vertically; + break; + } + } + for ( int c = 0; c < cc; c++ ) { + if ( colData[c].expansive ) { + ret |= (int) TQSizePolicy::Horizontally; + break; + } + } + return (TQSizePolicy::ExpandData) ret; +} + +TQSize TQGridLayoutData::sizeHint( int spacer ) const +{ + return findSize( &TQLayoutStruct::sizeHint, spacer ); +} + +TQSize TQGridLayoutData::maximumSize( int spacer ) const +{ + return findSize( &TQLayoutStruct::maximumSize, spacer ); +} + +TQSize TQGridLayoutData::minimumSize( int spacer ) const +{ + return findSize( &TQLayoutStruct::minimumSize, spacer ); +} + +void TQGridLayoutData::setSize( int r, int c ) +{ + if ( (int)rowData.size() < r ) { + int newR = TQMAX( r, rr * 2 ); + rowData.resize( newR ); + rStretch.resize( newR ); + rSpacing.resize( newR ); + for ( int i = rr; i < newR; i++ ) { + rowData[i].init(); + rStretch[i] = 0; + rSpacing[i] = 0; + } + } + if ( (int)colData.size() < c ) { + int newC = TQMAX( c, cc * 2 ); + colData.resize( newC ); + cStretch.resize( newC ); + cSpacing.resize( newC ); + for ( int i = cc; i < newC; i++ ) { + colData[i].init(); + cStretch[i] = 0; + cSpacing[i] = 0; + } + } + + if ( hfwData && (int)hfwData->size() < r ) { + delete hfwData; + hfwData = 0; + hfw_width = -1; + } + rr = r; + cc = c; +} + +void TQGridLayoutData::setNextPosAfter( int row, int col ) +{ + if ( addVertical ) { + if ( col > nextC || col == nextC && row >= nextR ) { + nextR = row + 1; + nextC = col; + if ( nextR >= rr ) { + nextR = 0; + nextC++; + } + } + } else { + if ( row > nextR || row == nextR && col >= nextC ) { + nextR = row; + nextC = col + 1; + if ( nextC >= cc ) { + nextC = 0; + nextR++; + } + } + } +} + +void TQGridLayoutData::add( TQGridBox *box, int row, int col ) +{ + expand( row+1, col+1 ); + box->row = row; + box->col = col; + things.append( box ); + setDirty(); + setNextPosAfter( row, col ); +} + +void TQGridLayoutData::add( TQGridBox *box, int row1, int row2, int col1, + int col2 ) +{ +#ifdef QT_CHECK_RANGE + if ( row2 >= 0 && row2 < row1 ) + qWarning( "TQGridLayout: Multi-cell fromRow greater than toRow" ); + if ( col2 >= 0 && col2 < col1 ) + qWarning( "TQGridLayout: Multi-cell fromCol greater than toCol" ); +#endif + if ( row1 == row2 && col1 == col2 ) { + add( box, row1, col1 ); + return; + } + expand( row2+1, col2+1 ); + box->row = row1; + box->col = col1; + TQGridMultiBox *mbox = new TQGridMultiBox( box, row2, col2 ); + if ( !multi ) { + multi = new TQPtrList; + multi->setAutoDelete( TRUE ); + } + multi->append( mbox ); + setDirty(); + if ( col2 < 0 ) + col2 = cc - 1; + + setNextPosAfter( row2, col2 ); +} + +void TQGridLayoutData::addData( TQGridBox *box, bool r, bool c ) +{ + TQSize hint = box->sizeHint(); + TQSize minS = box->minimumSize(); + TQSize maxS = box->maximumSize(); + + if ( c ) { + if ( !cStretch[box->col] ) + colData[box->col].stretch = TQMAX( colData[box->col].stretch, + box->hStretch() ); + colData[box->col].sizeHint = TQMAX( hint.width(), + colData[box->col].sizeHint ); + colData[box->col].minimumSize = TQMAX( minS.width(), + colData[box->col].minimumSize ); + + qMaxExpCalc( colData[box->col].maximumSize, colData[box->col].expansive, + maxS.width(), + box->expanding() & TQSizePolicy::Horizontally ); + } + if ( r ) { + if ( !rStretch[box->row] ) + rowData[box->row].stretch = TQMAX( rowData[box->row].stretch, + box->vStretch() ); + rowData[box->row].sizeHint = TQMAX( hint.height(), + rowData[box->row].sizeHint ); + rowData[box->row].minimumSize = TQMAX( minS.height(), + rowData[box->row].minimumSize ); + + qMaxExpCalc( rowData[box->row].maximumSize, rowData[box->row].expansive, + maxS.height(), + box->expanding() & TQSizePolicy::Vertically ); + } + if ( box->isEmpty() ) { + if ( box->item()->widget() != 0 ) { + /* + Hidden widgets should behave exactly the same as if + there were no widgets at all in the cell. We could have + TQWidgetItem::maximumSize() to return + TQSize(TQLAYOUTSIZE_MAX, TQLAYOUTSIZE_MAX). However, that + value is not suitable for TQBoxLayouts. So let's fix it + here. + */ + if ( c ) + colData[box->col].maximumSize = TQLAYOUTSIZE_MAX; + if ( r ) + rowData[box->row].maximumSize = TQLAYOUTSIZE_MAX; + } + } else { + // Empty boxes (i.e. spacers) do not get borders. This is + // hacky, but compatible. + if ( c ) + colData[box->col].empty = FALSE; + if ( r ) + rowData[box->row].empty = FALSE; + } +} + +static void distributeMultiBox( TQMemArray &chain, int spacing, + int start, int end, + int minSize, int sizeHint, + TQMemArray &stretchArray, int stretch ) +{ + int i; + int w = 0; + int wh = 0; + int max = 0; + for ( i = start; i <= end; i++ ) { + w += chain[i].minimumSize; + wh += chain[i].sizeHint; + max += chain[i].maximumSize; + chain[i].empty = FALSE; + if ( stretchArray[i] == 0 ) + chain[i].stretch = TQMAX(chain[i].stretch,stretch); + } + w += spacing * ( end - start ); + wh += spacing * ( end - start ); + max += spacing * ( end - start ); + + if ( max < minSize ) { // implies w < minSize + /* + We must increase the maximum size of at least one of the + items. qGeomCalc() will put the extra space in between the + items. We must recover that extra space and put it + somewhere. It does not really matter where, since the user + can always specify stretch factors and avoid this code. + */ + qGeomCalc( chain, start, end - start + 1, 0, minSize, spacing ); + int pos = 0; + for ( i = start; i <= end; i++ ) { + int nextPos = ( i == end ) ? minSize - 1 : chain[i + 1].pos; + int realSize = nextPos - pos; + if ( i != end ) + realSize -= spacing; + if ( chain[i].minimumSize < realSize ) + chain[i].minimumSize = realSize; + if ( chain[i].maximumSize < chain[i].minimumSize ) + chain[i].maximumSize = chain[i].minimumSize; + pos = nextPos; + } + } else if ( w < minSize ) { + qGeomCalc( chain, start, end - start + 1, 0, minSize, spacing ); + for ( i = start; i <= end; i++ ) { + if ( chain[i].minimumSize < chain[i].size ) + chain[i].minimumSize = chain[i].size; + } + } + + if ( wh < sizeHint ) { + qGeomCalc( chain, start, end - start + 1, 0, sizeHint, spacing ); + for ( i = start; i <= end; i++ ) { + if ( chain[i].sizeHint < chain[i].size ) + chain[i].sizeHint = chain[i].size; + } + } +} + +//#define QT_LAYOUT_DISABLE_CACHING + +void TQGridLayoutData::setupLayoutData( int spacing ) +{ +#ifndef QT_LAYOUT_DISABLE_CACHING + if ( !needRecalc ) + return; +#endif + has_hfw = FALSE; + int i; + + for ( i = 0; i < rr; i++ ) + rowData[i].init( rStretch[i], rSpacing[i] ); + for ( i = 0; i < cc; i++ ) + colData[i].init( cStretch[i], cSpacing[i] ); + + TQPtrListIterator it( things ); + TQGridBox * box; + while ( (box = it.current()) != 0 ) { + ++it; + addData( box ); + has_hfw = has_hfw || box->item()->hasHeightForWidth(); + } + + if ( multi ) { + TQPtrListIterator it( *multi ); + TQGridMultiBox * mbox; + while ( (mbox = it.current()) != 0 ) { + ++it; + TQGridBox *box = mbox->box(); + int r1 = box->row; + int c1 = box->col; + int r2 = mbox->torow; + int c2 = mbox->tocol; + if ( r2 < 0 ) + r2 = rr - 1; + if ( c2 < 0 ) + c2 = cc - 1; + + TQSize hint = box->sizeHint(); + TQSize min = box->minimumSize(); + if ( box->hasHeightForWidth() ) + has_hfw = TRUE; + + if ( r1 == r2 ) { + addData( box, TRUE, FALSE ); + } else { + distributeMultiBox( rowData, spacing, r1, r2, + min.height(), hint.height(), + rStretch, box->vStretch() ); + } + if ( c1 == c2 ) { + addData( box, FALSE, TRUE ); + } else { + distributeMultiBox( colData, spacing, c1, c2, + min.width(), hint.width(), + cStretch, box->hStretch() ); + } + } + } + for ( i = 0; i < rr; i++ ) + rowData[i].expansive = rowData[i].expansive || rowData[i].stretch > 0; + for ( i = 0; i < cc; i++ ) + colData[i].expansive = colData[i].expansive || colData[i].stretch > 0; + + needRecalc = FALSE; +} + +void TQGridLayoutData::addHfwData( TQGridBox *box, int width ) +{ + TQMemArray &rData = *hfwData; + if ( box->hasHeightForWidth() ) { + int hint = box->heightForWidth( width ); + rData[box->row].sizeHint = TQMAX( hint, rData[box->row].sizeHint ); + rData[box->row].minimumSize = TQMAX( hint, rData[box->row].minimumSize ); + } else { + TQSize hint = box->sizeHint(); + TQSize minS = box->minimumSize(); + rData[box->row].sizeHint = TQMAX( hint.height(), + rData[box->row].sizeHint ); + rData[box->row].minimumSize = TQMAX( minS.height(), + rData[box->row].minimumSize ); + } +} + +/* + Similar to setupLayoutData(), but uses heightForWidth(colData) + instead of sizeHint(). Assumes that setupLayoutData() and + qGeomCalc(colData) has been called. +*/ +void TQGridLayoutData::setupHfwLayoutData( int spacing ) +{ + TQMemArray &rData = *hfwData; + int i; + for ( i = 0; i < rr; i++ ) { + rData[i] = rowData[i]; + rData[i].minimumSize = rData[i].sizeHint = 0; + } + TQPtrListIterator it( things ); + TQGridBox * box; + while ( (box=it.current()) != 0 ) { + ++it; + addHfwData( box, colData[box->col].size ); + } + if ( multi ) { + TQPtrListIterator it( *multi ); + TQGridMultiBox * mbox; + while ( (mbox=it.current()) != 0 ) { + ++it; + TQGridBox *box = mbox->box(); + int r1 = box->row; + int c1 = box->col; + int r2 = mbox->torow; + int c2 = mbox->tocol; + if ( r2 < 0 ) + r2 = rr-1; + if ( c2 < 0 ) + c2 = cc-1; + int w = colData[c2].pos + colData[c2].size - colData[c1].pos; + if ( r1 == r2 ) { + addHfwData( box, w ); + } else { + TQSize hint = box->sizeHint(); + TQSize min = box->minimumSize(); + if ( box->hasHeightForWidth() ) { + int hfwh = box->heightForWidth( w ); + if ( hfwh > hint.height() ) + hint.setHeight( hfwh ); + if ( hfwh > min.height() ) + min.setHeight( hfwh ); + } + distributeMultiBox( rData, spacing, r1, r2, + min.height(), hint.height(), + rStretch, box->vStretch() ); + } + } + } + for ( i = 0; i < rr; i++ ) + rData[i].expansive = rData[i].expansive || rData[i].stretch > 0; +} + +void TQGridLayoutData::distribute( TQRect r, int spacing ) +{ + bool visualHReversed = hReversed; + if ( TQApplication::reverseLayout() ) + visualHReversed = !visualHReversed; + + setupLayoutData( spacing ); + + qGeomCalc( colData, 0, cc, r.x(), r.width(), spacing ); + TQMemArray *rDataPtr; + if ( has_hfw ) { + recalcHFW( r.width(), spacing ); + qGeomCalc( *hfwData, 0, rr, r.y(), r.height(), spacing ); + rDataPtr = hfwData; + } else { + qGeomCalc( rowData, 0, rr, r.y(), r.height(), spacing ); + rDataPtr = &rowData; + } + TQMemArray &rData = *rDataPtr; + + TQPtrListIterator it( things ); + TQGridBox * box; + while ( (box=it.current()) != 0 ) { + ++it; + int x = colData[box->col].pos; + int y = rData[box->row].pos; + int w = colData[box->col].size; + int h = rData[box->row].size; + if ( visualHReversed ) + x = r.left() + r.right() - x - w + 1; + if ( vReversed ) + y = r.top() + r.bottom() - y - h + 1; + box->setGeometry( TQRect( x, y, w, h ) ); + } + if ( multi ) { + TQPtrListIterator it( *multi ); + TQGridMultiBox * mbox; + while ( (mbox=it.current()) != 0 ) { + ++it; + TQGridBox *box = mbox->box(); + int r2 = mbox->torow; + int c2 = mbox->tocol; + if ( r2 < 0 ) + r2 = rr-1; + if ( c2 < 0 ) + c2 = cc-1; + + int x = colData[box->col].pos; + int y = rData[box->row].pos; + int x2p = colData[c2].pos + colData[c2].size; // x2+1 + int y2p = rData[r2].pos + rData[r2].size; // y2+1 + int w = x2p - x; + int h = y2p - y; + // this code is copied from above: + if ( visualHReversed ) + x = r.left() + r.right() - x - w + 1; + if ( vReversed ) + y = r.top() + r.bottom() - y - h + 1; + box->setGeometry( TQRect( x, y, w, h ) ); + } + } +} + +TQRect TQGridLayoutData::cellGeometry( int row, int col ) const +{ + if ( row < 0 || row >= rr || col < 0 || col >= cc ) + return TQRect(); + + const TQMemArray *rDataPtr; + if ( has_hfw ) + rDataPtr = hfwData; + else + rDataPtr = &rowData; + return TQRect( colData[col].pos, (*rDataPtr)[row].pos, + colData[col].size, (*rDataPtr)[row].size ); +} + +class TQGridLayoutDataIterator : public TQGLayoutIterator +{ +public: + TQGridLayoutDataIterator( TQGridLayoutData *d ); + uint count() const { return data->count(); } + TQLayoutItem *current() { + if ( multi ) { + if ( !data->multi || idx >= (int)data->multi->count() ) + return 0; + return data->multi->at( idx )->box()->item(); + } else { + if ( idx >= (int)data->things.count() ) + return 0; + return data->things.at( idx )->item(); + } + } + void toFirst() { + multi = data->things.isEmpty(); + idx = 0; + } + TQLayoutItem *next() { + idx++; + if ( !multi && idx >= (int)data->things.count() ) { + multi = TRUE; + idx = 0; + } + return current(); + } + TQLayoutItem *takeCurrent() { + TQLayoutItem *item = 0; + if ( multi ) { + if ( !data->multi || idx >= (int)data->multi->count() ) + return 0; + TQGridMultiBox *b = data->multi->take( idx ); + item = b->takeItem(); + delete b; + } else { + if ( idx >= (int)data->things.count() ) + return 0; + TQGridBox *b = data->things.take( idx ); + item = b->takeItem(); + delete b; + } + return item; + } + +private: + TQGridLayoutData *data; + bool multi; + int idx; +}; + +inline TQGridLayoutDataIterator::TQGridLayoutDataIterator( TQGridLayoutData *d ) + : data( d ) +{ + toFirst(); +} + +/*! + \class TQGridLayout + + \brief The TQGridLayout class lays out widgets in a grid. + + \ingroup geomanagement + \ingroup appearance + \mainclass + + TQGridLayout takes the space made available to it (by its parent + layout or by the mainWidget()), divides it up into rows and + columns, and puts each widget it manages into the correct cell. + + Columns and rows behave identically; we will discuss columns, but + there are equivalent functions for rows. + + Each column has a minimum width and a stretch factor. The minimum + width is the greatest of that set using addColSpacing() and the + minimum width of each widget in that column. The stretch factor is + set using setColStretch() and determines how much of the available + space the column will get over and above its necessary minimum. + + Normally, each managed widget or layout is put into a cell of its + own using addWidget(), addLayout() or by the \link + TQLayout::setAutoAdd() auto-add facility\endlink. It is also + possible for a widget to occupy multiple cells using + addMultiCellWidget(). If you do this, TQGridLayout will guess how + to distribute the size over the columns/rows (based on the stretch + factors). + + To remove a widget from a layout, call remove(). Calling + TQWidget::hide() on a widget also effectively removes the widget + from the layout until TQWidget::show() is called. + + This illustration shows a fragment of a dialog with a five-column, + three-row grid (the grid is shown overlaid in magenta): + + \img gridlayout.png + + Columns 0, 2 and 4 in this dialog fragment are made up of a + TQLabel, a TQLineEdit, and a TQListBox. Columns 1 and 3 are + placeholders made with addColSpacing(). Row 0 consists of three + TQLabel objects, row 1 of three TQLineEdit objects and row 2 of + three TQListBox objects. We used placeholder columns (1 and 3) to + get the right amount of space between the columns. + + Note that the columns and rows are not equally wide or tall. If + you want two columns to have the same width, you must set their + minimum widths and stretch factors to be the same yourself. You do + this using addColSpacing() and setColStretch(). + + If the TQGridLayout is not the top-level layout (i.e. does not + manage all of the widget's area and children), you must add it to + its parent layout when you create it, but before you do anything + with it. The normal way to add a layout is by calling + parentLayout-\>addLayout(). + + Once you have added your layout you can start putting widgets and + other layouts into the cells of your grid layout using + addWidget(), addLayout() and addMultiCellWidget(). + + TQGridLayout also includes two margin widths: the border and the + spacing. The border is the width of the reserved space along each + of the TQGridLayout's four sides. The spacing is the width of the + automatically allocated spacing between neighboring boxes. + + Both the border and the spacing are parameters of the constructor + and default to 0. + + \sa TQGrid, \link layout.html Layout Overview \endlink +*/ + +/*! + \enum TQGridLayout::Corner + + This enum identifies which corner is the origin (0, 0) of the + layout. + + \value TopLeft the top-left corner + \value TopRight the top-right corner + \value BottomLeft the bottom-left corner + \value BottomRight the bottom-right corner +*/ + +/*! + Constructs a new TQGridLayout with \a nRows rows, \a nCols columns + and parent widget, \a parent. \a parent may not be 0. The grid + layout is called \a name. + + \a margin is the number of pixels between the edge of the widget + and its managed children. \a space is the default number of pixels + between cells. If \a space is -1, the value of \a margin is used. +*/ +TQGridLayout::TQGridLayout( TQWidget *parent, int nRows, int nCols, int margin, + int space, const char *name ) + : TQLayout( parent, margin, space, name ) +{ + init( nRows, nCols ); +} + +/*! + Constructs a new grid that is placed inside \a parentLayout with + \a nRows rows and \a nCols columns. If \a spacing is -1, this + TQGridLayout inherits its parent's spacing(); otherwise \a spacing + is used. The grid layout is called \a name. + + This grid is placed according to \a parentLayout's default + placement rules. +*/ +TQGridLayout::TQGridLayout( TQLayout *parentLayout, int nRows, int nCols, + int spacing, const char *name ) + : TQLayout( parentLayout, spacing, name ) +{ + init( nRows, nCols ); +} + +/*! + Constructs a new grid with \a nRows rows and \a nCols columns. If + \a spacing is -1, this TQGridLayout inherits its parent's + spacing(); otherwise \a spacing is used. The grid layout is called + \a name. + + You must insert this grid into another layout. You can insert + widgets and layouts into this layout at any time, but laying out + will not be performed before this is inserted into another layout. +*/ +TQGridLayout::TQGridLayout( int nRows, int nCols, + int spacing, const char *name ) + : TQLayout( spacing, name ) +{ + init( nRows, nCols ); +} + +/*! + Destroys the grid layout. Geometry management is terminated if + this is a top-level grid. + + The layout's widgets aren't destroyed. +*/ +TQGridLayout::~TQGridLayout() +{ + delete data; +} + +/*! + Returns the number of rows in this grid. +*/ +int TQGridLayout::numRows() const +{ + return data->numRows(); +} + +/*! + Returns the number of columns in this grid. +*/ +int TQGridLayout::numCols() const +{ + return data->numCols(); +} + +/*! + Returns the preferred size of this grid. +*/ +TQSize TQGridLayout::sizeHint() const +{ + return data->sizeHint( spacing() ) + TQSize( 2 * margin(), 2 * margin() ); +} + +/*! + Returns the minimum size needed by this grid. +*/ +TQSize TQGridLayout::minimumSize() const +{ + return data->minimumSize( spacing() ) + TQSize( 2 * margin(), 2 * margin() ); +} + +/*! + Returns the maximum size needed by this grid. +*/ +TQSize TQGridLayout::maximumSize() const +{ + TQSize s = data->maximumSize( spacing() ) + + TQSize( 2 * margin(), 2 * margin() ); + s = s.boundedTo( TQSize(TQLAYOUTSIZE_MAX, TQLAYOUTSIZE_MAX) ); + if ( alignment() & TQt::AlignHorizontal_Mask ) + s.setWidth( TQLAYOUTSIZE_MAX ); + if ( alignment() & TQt::AlignVertical_Mask ) + s.setHeight( TQLAYOUTSIZE_MAX ); + return s; +} + +/*! + Returns TRUE if this layout's preferred height depends on its + width; otherwise returns FALSE. +*/ +bool TQGridLayout::hasHeightForWidth() const +{ + return ((TQGridLayout*)this)->data->hasHeightForWidth( spacing() ); +} + +/*! + Returns the layout's preferred height when it is \a w pixels wide. +*/ +int TQGridLayout::heightForWidth( int w ) const +{ + TQGridLayout *that = (TQGridLayout*)this; + return that->data->heightForWidth( w, margin(), spacing() ); +} + +/*! \internal */ +int TQGridLayout::minimumHeightForWidth( int w ) const +{ + TQGridLayout *that = (TQGridLayout*)this; + return that->data->minimumHeightForWidth( w, margin(), spacing() ); +} + +/*! + Searches for widget \a w in this layout (not including child + layouts). If \a w is found, it sets \c \a row and \c \a col to + the row and column and returns TRUE; otherwise returns FALSE. + + Note: if a widget spans multiple rows/columns, the top-left cell + is returned. +*/ +bool TQGridLayout::findWidget( TQWidget* w, int *row, int *col ) +{ + return data->findWidget( w, row, col ); +} + +/*! + Resizes managed widgets within the rectangle \a r. +*/ +void TQGridLayout::setGeometry( const TQRect &r ) +{ + if ( data->isDirty() || r != geometry() ) { + TQLayout::setGeometry( r ); + TQRect cr = alignment() ? alignmentRect( r ) : r; + TQRect s( cr.x() + margin(), cr.y() + margin(), + cr.width() - 2 * margin(), cr.height() - 2 * margin() ); + data->distribute( s, spacing() ); + } +} + +/*! + Returns the geometry of the cell with row \a row and column \a col + in the grid. Returns an invalid rectangle if \a row or \a col is + outside the grid. + + \warning in the current version of TQt this function does not + return valid results until setGeometry() has been called, i.e. + after the mainWidget() is visible. +*/ +TQRect TQGridLayout::cellGeometry( int row, int col ) const +{ + return data->cellGeometry( row, col ); +} + +/*! + Expands this grid so that it will have \a nRows rows and \a nCols + columns. Will not shrink the grid. You should not need to call + this function because TQGridLayout expands automatically as new + items are inserted. +*/ +void TQGridLayout::expand( int nRows, int nCols ) +{ + data->expand( nRows, nCols ); +} + +/*! + Sets up the grid. +*/ +void TQGridLayout::init( int nRows, int nCols ) +{ + setSupportsMargin( TRUE ); + data = new TQGridLayoutData( nRows, nCols ); +} + +/*! + \overload + + Adds \a item to the next free position of this layout. +*/ +void TQGridLayout::addItem( TQLayoutItem *item ) +{ + int r, c; + data->getNextPos( r, c ); + add( item, r, c ); +} + +/*! + Adds \a item at position \a row, \a col. The layout takes + ownership of the \a item. +*/ +void TQGridLayout::addItem( TQLayoutItem *item, int row, int col ) +{ + add( item, row, col ); +} + +/*! + Adds \a item at position \a row, \a col. The layout takes + ownership of the \a item. +*/ +void TQGridLayout::add( TQLayoutItem *item, int row, int col ) +{ + TQGridBox *box = new TQGridBox( item ); + data->add( box, row, col ); +} + +/*! + Adds the \a item to the cell grid, spanning multiple rows/columns. + + The cell will span from \a fromRow, \a fromCol to \a toRow, \a + toCol. Alignment is specified by \a alignment, which is a bitwise + OR of \l TQt::AlignmentFlags values. The default alignment is 0, + which means that the widget fills the entire cell. +*/ +void TQGridLayout::addMultiCell( TQLayoutItem *item, int fromRow, int toRow, + int fromCol, int toCol, int alignment ) +{ + TQGridBox *b = new TQGridBox( item ); + b->setAlignment( alignment ); + data->add( b, fromRow, toRow, fromCol, toCol ); +} + +/* + Returns TRUE if the widget \a w can be added to the layout \a l; + otherwise returns FALSE. +*/ +static bool checkWidget( TQLayout *l, TQWidget *w ) +{ + if ( !w ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQLayout: Cannot add null widget to %s/%s", l->className(), + l->name() ); +#endif + return FALSE; + } + if ( w->parentWidget() != l->mainWidget() && l->mainWidget() ) { +#if defined(QT_CHECK_STATE) + if ( w->parentWidget() ) + qWarning( "TQLayout: Adding %s/%s (child of %s/%s) to layout for " + "%s/%s", w->className(), w->name(), + w->parentWidget()->className(), w->parentWidget()->name(), + l->mainWidget()->className(), l->mainWidget()->name() ); + else + qWarning( "TQLayout: Adding %s/%s (top-level widget) to layout for" + " %s/%s", w->className(), w->name(), + l->mainWidget()->className(), l->mainWidget()->name() ); +#endif + return FALSE; + } + return TRUE; +} + +/*! + Adds the widget \a w to the cell grid at \a row, \a col. The + top-left position is (0, 0) by default. + + Alignment is specified by \a alignment, which is a bitwise OR of + \l TQt::AlignmentFlags values. The default alignment is 0, which + means that the widget fills the entire cell. + + \list + \i You should not call this if you have enabled the + \link TQLayout::setAutoAdd() auto-add facility of the layout\endlink. + + \i From TQt 3.0, the \a alignment parameter is interpreted more + aggressively than in previous versions of TQt. A non-default + alignment now indicates that the widget should not grow to fill + the available space, but should be sized according to sizeHint(). + \endlist + + \sa addMultiCellWidget() +*/ +void TQGridLayout::addWidget( TQWidget *w, int row, int col, int alignment ) +{ + if ( !checkWidget( this, w ) ) + return; + if ( row < 0 || col < 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQGridLayout: Cannot add %s/%s to %s/%s at row %d col %d", + w->className(), w->name(), className(), name(), row, col ); +#endif + return; + } + TQWidgetItem *b = new TQWidgetItem( w ); + b->setAlignment( alignment ); + add( b, row, col ); +} + +/*! + Adds the widget \a w to the cell grid, spanning multiple + rows/columns. The cell will span from \a fromRow, \a fromCol to \a + toRow, \a toCol. + + Alignment is specified by \a alignment, which is a bitwise OR of + \l TQt::AlignmentFlags values. The default alignment is 0, which + means that the widget fills the entire cell. + + A non-zero alignment indicates that the widget should not grow to + fill the available space but should be sized according to + sizeHint(). + + \sa addWidget() +*/ +void TQGridLayout::addMultiCellWidget( TQWidget *w, int fromRow, int toRow, + int fromCol, int toCol, int alignment ) +{ + TQGridBox *b = new TQGridBox( w ); + b->setAlignment( alignment ); + data->add( b, fromRow, toRow, fromCol, toCol ); +} + +/*! + Places the \a layout at position (\a row, \a col) in the grid. The + top-left position is (0, 0). + + \a layout becomes a child of the grid layout. + + When a layout is constructed with another layout as its parent, + you don't need to call addLayout(); the child layout is + automatically added to the parent layout as it is constructed. + + \sa addMultiCellLayout() +*/ +void TQGridLayout::addLayout( TQLayout *layout, int row, int col ) +{ + addChildLayout( layout ); + add( layout, row, col ); +} + +/*! + Adds the layout \a layout to the cell grid, spanning multiple + rows/columns. The cell will span from \a fromRow, \a fromCol to \a + toRow, \a toCol. + + Alignment is specified by \a alignment, which is a bitwise OR of + \l TQt::AlignmentFlags values. The default alignment is 0, which + means that the widget fills the entire cell. + + A non-zero alignment indicates that the layout should not grow to + fill the available space but should be sized according to + sizeHint(). + + \a layout becomes a child of the grid layout. + + \sa addLayout() +*/ +void TQGridLayout::addMultiCellLayout( TQLayout *layout, int fromRow, int toRow, + int fromCol, int toCol, int alignment ) +{ + addChildLayout( layout ); + TQGridBox *b = new TQGridBox( layout ); + b->setAlignment( alignment ); + data->add( b, fromRow, toRow, fromCol, toCol ); +} + +/*! + Sets the stretch factor of row \a row to \a stretch. The first row + is number 0. + + The stretch factor is relative to the other rows in this grid. + Rows with a higher stretch factor take more of the available + space. + + The default stretch factor is 0. If the stretch factor is 0 and no + other row in this table can grow at all, the row may still grow. + + \sa rowStretch(), setRowSpacing(), setColStretch() +*/ +void TQGridLayout::setRowStretch( int row, int stretch ) +{ + data->setRowStretch( row, stretch ); +} + +/*! + Returns the stretch factor for row \a row. + + \sa setRowStretch() +*/ +int TQGridLayout::rowStretch( int row ) const +{ + return data->rowStretch( row ); +} + +/*! + Returns the stretch factor for column \a col. + + \sa setColStretch() +*/ +int TQGridLayout::colStretch( int col ) const +{ + return data->colStretch( col ); +} + +/*! + Sets the stretch factor of column \a col to \a stretch. The first + column is number 0. + + The stretch factor is relative to the other columns in this grid. + Columns with a higher stretch factor take more of the available + space. + + The default stretch factor is 0. If the stretch factor is 0 and no + other column in this table can grow at all, the column may still + grow. + + \sa colStretch(), addColSpacing(), setRowStretch() +*/ +void TQGridLayout::setColStretch( int col, int stretch ) +{ + data->setColStretch( col, stretch ); +} + +#if QT_VERSION >= 0x040000 +#error "Make add{Row,Col}Spacing() inline QT_NO_COMPAT functions defined in terms of set{Row,Col}Spacing()" +#endif + +/*! + \obsolete + + Sets the minimum height of row \a row to \a minsize pixels. + + Use setRowSpacing() instead. +*/ +void TQGridLayout::addRowSpacing( int row, int minsize ) +{ + TQLayoutItem *b = new TQSpacerItem( 0, minsize ); + add( b, row, 0 ); +} + +/*! + \obsolete + + Sets the minimum width of column \a col to \a minsize pixels. + + Use setColSpacing() instead. +*/ +void TQGridLayout::addColSpacing( int col, int minsize ) +{ + TQLayoutItem *b = new TQSpacerItem( minsize, 0 ); + add( b, 0, col ); +} + +/*! + Sets the minimum height of row \a row to \a minSize pixels. + + \sa rowSpacing(), setColSpacing() +*/ +void TQGridLayout::setRowSpacing( int row, int minSize ) +{ + data->setRowSpacing( row, minSize ); +} + +/*! + Returns the row spacing for row \a row. + + \sa setRowSpacing() +*/ +int TQGridLayout::rowSpacing( int row ) const +{ + return data->rowSpacing( row ); +} + +/*! + Sets the minimum width of column \a col to \a minSize pixels. + + \sa colSpacing(), setRowSpacing() +*/ +void TQGridLayout::setColSpacing( int col, int minSize ) +{ + data->setColSpacing( col, minSize ); +} + +/*! + Returns the column spacing for column \a col. + + \sa setColSpacing() +*/ +int TQGridLayout::colSpacing( int col ) const +{ + return data->colSpacing( col ); +} + +/*! + Returns whether this layout can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions. +*/ +TQSizePolicy::ExpandData TQGridLayout::expanding() const +{ + return data->expanding( spacing() ); +} + +/*! + Sets the grid's origin corner, i.e. position (0, 0), to \a c. +*/ +void TQGridLayout::setOrigin( Corner c ) +{ + data->setReversed( c == BottomLeft || c == BottomRight, + c == TopRight || c == BottomRight ); +} + +/*! + Returns the corner that's used for the grid's origin, i.e. for + position (0, 0). +*/ +TQGridLayout::Corner TQGridLayout::origin() const +{ + if ( data->horReversed() ) { + return data->verReversed() ? BottomRight : TopRight; + } else { + return data->verReversed() ? BottomLeft : TopLeft; + } +} + +/*! + Resets cached information. +*/ +void TQGridLayout::invalidate() +{ + TQLayout::invalidate(); + TQLayout::setGeometry( TQRect() ); // for binary compatibility (?) + data->setDirty(); +} + +/*! \reimp */ +TQLayoutIterator TQGridLayout::iterator() +{ + return TQLayoutIterator( new TQGridLayoutDataIterator(data) ); +} + +struct TQBoxLayoutItem +{ + TQBoxLayoutItem( TQLayoutItem *it, int stretch_ = 0 ) + : item( it ), stretch( stretch_ ), magic( FALSE ) { } + ~TQBoxLayoutItem() { delete item; } + + int hfw( int w ) { + if ( item->hasHeightForWidth() ) { + return item->heightForWidth( w ); + } else { + return item->sizeHint().height(); + } + } + int mhfw( int w ) { + if ( item->hasHeightForWidth() ) { + return item->heightForWidth( w ); + } else { + return item->minimumSize().height(); + } + } + int hStretch() { + if ( stretch == 0 && item->widget() ) { + return item->widget()->sizePolicy().horStretch(); + } else { + return stretch; + } + } + int vStretch() { + if ( stretch == 0 && item->widget() ) { + return item->widget()->sizePolicy().verStretch(); + } else { + return stretch; + } + } + + TQLayoutItem *item; + int stretch; + bool magic; +}; + +class TQBoxLayoutData +{ +public: + TQBoxLayoutData() : geomArray( 0 ), hfwWidth( -1 ), dirty( TRUE ) + { list.setAutoDelete( TRUE ); } + + ~TQBoxLayoutData() { delete geomArray; } + void setDirty() { + delete geomArray; + geomArray = 0; + hfwWidth = -1; + hfwHeight = -1; + dirty = TRUE; + } + + TQPtrList list; + TQMemArray *geomArray; + int hfwWidth; + int hfwHeight; + int hfwMinHeight; + TQSize sizeHint; + TQSize minSize; + TQSize maxSize; + TQSizePolicy::ExpandData expanding; + uint hasHfw : 1; + uint dirty : 1; +}; + +class TQBoxLayoutIterator : public TQGLayoutIterator +{ +public: + TQBoxLayoutIterator( TQBoxLayoutData *d ) : data( d ), idx( 0 ) {} + TQLayoutItem *current() { + if ( idx >= int(data->list.count()) ) + return 0; + return data->list.at(idx)->item; + } + TQLayoutItem *next() { + idx++; + return current(); + } + TQLayoutItem *takeCurrent() { + TQLayoutItem *item = 0; + + TQBoxLayoutItem *b = data->list.take( idx ); + if ( b ) { + item = b->item; + b->item = 0; + delete b; + } + data->setDirty(); + return item; + } + +private: + TQBoxLayoutData *data; + int idx; +}; + +/*! + \class TQBoxLayout + + \brief The TQBoxLayout class lines up child widgets horizontally or + vertically. + + \ingroup geomanagement + \ingroup appearance + + TQBoxLayout takes the space it gets (from its parent layout or from + the mainWidget()), divides it up into a row of boxes, and makes + each managed widget fill one box. + + \img qhbox-m.png Horizontal box with five child widgets + + If the TQBoxLayout's orientation is \c Horizontal the boxes are + placed in a row, with suitable sizes. Each widget (or other box) + will get at least its minimum size and at most its maximum size. + Any excess space is shared according to the stretch factors (more + about that below). + + \img qvbox-m.png Vertical box with five child widgets + + If the TQBoxLayout's orientation is \c Vertical, the boxes are + placed in a column, again with suitable sizes. + + The easiest way to create a TQBoxLayout is to use one of the + convenience classes, e.g. TQHBoxLayout (for \c Horizontal boxes) or + TQVBoxLayout (for \c Vertical boxes). You can also use the + TQBoxLayout constructor directly, specifying its direction as \c + LeftToRight, \c Down, \c RightToLeft or \c Up. + + If the TQBoxLayout is not the top-level layout (i.e. it is not + managing all of the widget's area and children), you must add it + to its parent layout before you can do anything with it. The + normal way to add a layout is by calling + parentLayout-\>addLayout(). + + Once you have done this, you can add boxes to the TQBoxLayout using + one of four functions: + + \list + \i addWidget() to add a widget to the TQBoxLayout and set the + widget's stretch factor. (The stretch factor is along the row of + boxes.) + + \i addSpacing() to create an empty box; this is one of the + functions you use to create nice and spacious dialogs. See below + for ways to set margins. + + \i addStretch() to create an empty, stretchable box. + + \i addLayout() to add a box containing another TQLayout to the row + and set that layout's stretch factor. + \endlist + + Use insertWidget(), insertSpacing(), insertStretch() or + insertLayout() to insert a box at a specified position in the + layout. + + TQBoxLayout also includes two margin widths: + + \list + \i setMargin() sets the width of the outer border. This is the width + of the reserved space along each of the TQBoxLayout's four sides. + \i setSpacing() sets the width between neighboring boxes. (You + can use addSpacing() to get more space at a particular spot.) + \endlist + + The margin defaults to 0. The spacing defaults to the same as the + margin width for a top-level layout, or to the same as the parent + layout. Both are parameters to the constructor. + + To remove a widget from a layout, call remove(). Calling + TQWidget::hide() on a widget also effectively removes the widget + from the layout until TQWidget::show() is called. + + You will almost always want to use TQVBoxLayout and TQHBoxLayout + rather than TQBoxLayout because of their convenient constructors. + + \sa TQGrid \link layout.html Layout Overview \endlink +*/ + +/*! + \enum TQBoxLayout::Direction + + This type is used to determine the direction of a box layout. + + \value LeftToRight Horizontal, from left to right + \value RightToLeft Horizontal, from right to left + \value TopToBottom Vertical, from top to bottom + \value Down The same as \c TopToBottom + \value BottomToTop Vertical, from bottom to top + \value Up The same as \c BottomToTop +*/ + +static inline bool horz( TQBoxLayout::Direction dir ) +{ + return dir == TQBoxLayout::RightToLeft || dir == TQBoxLayout::LeftToRight; +} + +/*! + Constructs a new TQBoxLayout with direction \a d and main widget \a + parent. \a parent may not be 0. + + The \a margin is the number of pixels between the edge of the + widget and its managed children. The \a spacing is the default + number of pixels between neighboring children. If \a spacing is -1 + the value of \a margin is used for \a spacing. + + \a name is the internal object name. + + \sa direction() +*/ +TQBoxLayout::TQBoxLayout( TQWidget *parent, Direction d, + int margin, int spacing, const char *name ) + : TQLayout( parent, margin, spacing, name ) +{ + data = new TQBoxLayoutData; + dir = d; + setSupportsMargin( TRUE ); +} + +/*! + Constructs a new TQBoxLayout called \a name, with direction \a d, + and inserts it into \a parentLayout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, the layout will inherit its + parent's spacing(). +*/ +TQBoxLayout::TQBoxLayout( TQLayout *parentLayout, Direction d, int spacing, + const char *name ) + : TQLayout( parentLayout, spacing, name ) +{ + data = new TQBoxLayoutData; + dir = d; + setSupportsMargin( TRUE ); +} + +/*! + Constructs a new TQBoxLayout called \a name, with direction \a d. + + If \a spacing is -1, the layout will inherit its parent's + spacing(); otherwise \a spacing is used. + + You must insert this box into another layout. +*/ +TQBoxLayout::TQBoxLayout( Direction d, int spacing, const char *name ) + : TQLayout( spacing, name ) +{ + data = new TQBoxLayoutData; + dir = d; + setSupportsMargin( TRUE ); +} + +/*! + Destroys this box layout. + + The layout's widgets aren't destroyed. +*/ +TQBoxLayout::~TQBoxLayout() +{ + delete data; +} + +/*! + Returns the preferred size of this box layout. +*/ +TQSize TQBoxLayout::sizeHint() const +{ + if ( data->dirty ) { + TQBoxLayout *that = (TQBoxLayout*)this; + that->setupGeom(); + } + return data->sizeHint + TQSize( 2 * margin(), 2 * margin() ); +} + +/*! + Returns the minimum size needed by this box layout. +*/ +TQSize TQBoxLayout::minimumSize() const +{ + if ( data->dirty ) { + TQBoxLayout *that = (TQBoxLayout*)this; + that->setupGeom(); + } + return data->minSize + TQSize( 2 * margin(), 2 * margin() ); +} + +/*! + Returns the maximum size needed by this box layout. +*/ +TQSize TQBoxLayout::maximumSize() const +{ + if ( data->dirty ) { + TQBoxLayout *that = (TQBoxLayout*)this; + that->setupGeom(); + } + TQSize s = ( data->maxSize + TQSize(2 * margin(), 2 * margin()) ) + .boundedTo(TQSize(TQLAYOUTSIZE_MAX, TQLAYOUTSIZE_MAX)); + if ( alignment() & TQt::AlignHorizontal_Mask ) + s.setWidth( TQLAYOUTSIZE_MAX ); + if ( alignment() & TQt::AlignVertical_Mask ) + s.setHeight( TQLAYOUTSIZE_MAX ); + return s; +} + +/*! + Returns TRUE if this layout's preferred height depends on its width; + otherwise returns FALSE. +*/ +bool TQBoxLayout::hasHeightForWidth() const +{ + if ( data->dirty ) { + TQBoxLayout *that = (TQBoxLayout*)this; + that->setupGeom(); + } + return data->hasHfw; +} + +/*! + Returns the layout's preferred height when it is \a w pixels wide. +*/ +int TQBoxLayout::heightForWidth( int w ) const +{ + if ( !hasHeightForWidth() ) + return -1; + w -= 2 * margin(); + if ( w != data->hfwWidth ) { + TQBoxLayout *that = (TQBoxLayout*)this; + that->calcHfw( w ); + } + return data->hfwHeight + 2 * margin(); +} + +/*! \internal */ +int TQBoxLayout::minimumHeightForWidth( int w ) const +{ + (void) heightForWidth( w ); + return data->hasHfw ? (data->hfwMinHeight + 2 * margin() ) : -1; +} + +/*! + Resets cached information. +*/ +void TQBoxLayout::invalidate() +{ + TQLayout::invalidate(); + data->setDirty(); +} + +/*! + \reimp +*/ +TQLayoutIterator TQBoxLayout::iterator() +{ + return TQLayoutIterator( new TQBoxLayoutIterator(data) ); +} + +/*! + Returns whether this layout can make use of more space than + sizeHint(). A value of \c Vertical or \c Horizontal means that it wants + to grow in only one dimension, whereas \c BothDirections means that + it wants to grow in both dimensions. +*/ +TQSizePolicy::ExpandData TQBoxLayout::expanding() const +{ + if ( data->dirty ) { + TQBoxLayout *that = (TQBoxLayout*)this; + that->setupGeom(); + } + return data->expanding; +} + +/*! + Resizes managed widgets within the rectangle \a r. +*/ +void TQBoxLayout::setGeometry( const TQRect &r ) +{ + if ( !data->geomArray || r != geometry() ) { + TQLayout::setGeometry( r ); + if ( !data->geomArray ) + setupGeom(); + TQRect cr = alignment() ? alignmentRect( r ) : r; + TQRect s( cr.x() + margin(), cr.y() + margin(), + cr.width() - 2 * margin(), cr.height() - 2 * margin() ); + + TQMemArray a = *data->geomArray; + int pos = horz( dir ) ? s.x() : s.y(); + int space = horz( dir ) ? s.width() : s.height(); + int n = a.count(); + if ( data->hasHfw && !horz(dir) ) { + for ( int i = 0; i < n; i++ ) { + TQBoxLayoutItem *box = data->list.at( i ); + if ( box->item->hasHeightForWidth() ) + a[i].sizeHint = a[i].minimumSize = + box->item->heightForWidth( s.width() ); + } + } + + Direction visualDir = dir; + if ( TQApplication::reverseLayout() ) { + if ( dir == LeftToRight ) + visualDir = RightToLeft; + else if ( dir == RightToLeft ) + visualDir = LeftToRight; + } + + qGeomCalc( a, 0, n, pos, space, spacing() ); + for ( int i = 0; i < n; i++ ) { + TQBoxLayoutItem *box = data->list.at( i ); + + switch ( visualDir ) { + case LeftToRight: + box->item->setGeometry( TQRect(a[i].pos, s.y(), + a[i].size, s.height()) ); + break; + case RightToLeft: + box->item->setGeometry( TQRect(s.left() + s.right() + - a[i].pos - a[i].size + 1, s.y(), + a[i].size, s.height()) ); + break; + case TopToBottom: + box->item->setGeometry( TQRect(s.x(), a[i].pos, + s.width(), a[i].size) ); + break; + case BottomToTop: + box->item->setGeometry( TQRect(s.x(), s.top() + s.bottom() + - a[i].pos - a[i].size + 1, + s.width(), a[i].size) ); + } + } + } +} + +/*! + Adds \a item to the end of this box layout. +*/ +void TQBoxLayout::addItem( TQLayoutItem *item ) +{ + TQBoxLayoutItem *it = new TQBoxLayoutItem( item ); + data->list.append( it ); + invalidate(); +} + +/*! + Inserts \a item into this box layout at position \a index. If \a + index is negative, the item is added at the end. + + \warning Does not call TQLayout::insertChildLayout() if \a item is + a TQLayout. + + \sa addItem(), findWidget() +*/ +void TQBoxLayout::insertItem( int index, TQLayoutItem *item ) +{ + if ( index < 0 ) // append + index = data->list.count(); + + TQBoxLayoutItem *it = new TQBoxLayoutItem( item ); + data->list.insert( index, it ); + invalidate(); +} + +/*! + Inserts a non-stretchable space at position \a index, with size \a + size. If \a index is negative the space is added at the end. + + The box layout has default margin and spacing. This function adds + additional space. + + \sa insertStretch() +*/ +void TQBoxLayout::insertSpacing( int index, int size ) +{ + if ( index < 0 ) // append + index = data->list.count(); + + // hack in TQGridLayoutData: spacers do not get insideSpacing + TQLayoutItem *b; + if ( horz( dir ) ) + b = new TQSpacerItem( size, 0, TQSizePolicy::Fixed, + TQSizePolicy::Minimum ); + else + b = new TQSpacerItem( 0, size, TQSizePolicy::Minimum, + TQSizePolicy::Fixed ); + + TQBoxLayoutItem *it = new TQBoxLayoutItem( b ); + it->magic = TRUE; + data->list.insert( index, it ); + invalidate(); +} + +/*! + Inserts a stretchable space at position \a index, with zero + minimum size and stretch factor \a stretch. If \a index is + negative the space is added at the end. + + \sa insertSpacing() +*/ +void TQBoxLayout::insertStretch( int index, int stretch ) +{ + if ( index < 0 ) // append + index = data->list.count(); + + // hack in TQGridLayoutData: spacers do not get insideSpacing + TQLayoutItem *b; + if ( horz( dir ) ) + b = new TQSpacerItem( 0, 0, TQSizePolicy::Expanding, + TQSizePolicy::Minimum ); + else + b = new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, + TQSizePolicy::Expanding ); + + TQBoxLayoutItem *it = new TQBoxLayoutItem( b, stretch ); + it->magic = TRUE; + data->list.insert( index, it ); + invalidate(); +} + +/*! + Inserts \a layout at position \a index, with stretch factor \a + stretch. If \a index is negative, the layout is added at the end. + + \a layout becomes a child of the box layout. + + \sa setAutoAdd(), insertWidget(), insertSpacing() +*/ +void TQBoxLayout::insertLayout( int index, TQLayout *layout, int stretch ) +{ + if ( index < 0 ) // append + index = data->list.count(); + + addChildLayout( layout ); + TQBoxLayoutItem *it = new TQBoxLayoutItem( layout, stretch ); + data->list.insert( index, it ); + invalidate(); +} + +/*! + Inserts \a widget at position \a index, with stretch factor \a + stretch and alignment \a alignment. If \a index is negative, the + widget is added at the end. + + The stretch factor applies only in the \link direction() direction + \endlink of the TQBoxLayout, and is relative to the other boxes and + widgets in this TQBoxLayout. Widgets and boxes with higher stretch + factors grow more. + + If the stretch factor is 0 and nothing else in the TQBoxLayout has + a stretch factor greater than zero, the space is distributed + according to the TQWidget:sizePolicy() of each widget that's + involved. + + Alignment is specified by \a alignment, which is a bitwise OR of + \l TQt::AlignmentFlags values. The default alignment is 0, which + means that the widget fills the entire cell. + + From TQt 3.0, the \a alignment parameter is interpreted more + aggressively than in previous versions of TQt. A non-default + alignment now indicates that the widget should not grow to fill + the available space, but should be sized according to sizeHint(). + + \sa setAutoAdd(), insertLayout(), insertSpacing() +*/ +void TQBoxLayout::insertWidget( int index, TQWidget *widget, int stretch, + int alignment ) +{ + if ( !checkWidget(this, widget) ) + return; + + if ( index < 0 ) // append + index = data->list.count(); + + TQWidgetItem *b = new TQWidgetItem( widget ); + b->setAlignment( alignment ); + TQBoxLayoutItem *it = new TQBoxLayoutItem( b, stretch ); + data->list.insert( index, it ); + invalidate(); +} + +/*! + Adds a non-stretchable space with size \a size to the end of this + box layout. TQBoxLayout provides default margin and spacing. This + function adds additional space. + + \sa insertSpacing(), addStretch() +*/ +void TQBoxLayout::addSpacing( int size ) +{ + insertSpacing( -1, size ); +} + +/*! + Adds a stretchable space with zero minimum size and stretch factor + \a stretch to the end of this box layout. + + \sa addSpacing() +*/ +void TQBoxLayout::addStretch( int stretch ) +{ + insertStretch( -1, stretch ); +} + +/*! + Adds \a widget to the end of this box layout, with a stretch + factor of \a stretch and alignment \a alignment. + + The stretch factor applies only in the \link direction() direction + \endlink of the TQBoxLayout, and is relative to the other boxes and + widgets in this TQBoxLayout. Widgets and boxes with higher stretch + factors grow more. + + If the stretch factor is 0 and nothing else in the TQBoxLayout has + a stretch factor greater than zero, the space is distributed + according to the TQWidget:sizePolicy() of each widget that's + involved. + + Alignment is specified by \a alignment which is a bitwise OR of \l + TQt::AlignmentFlags values. The default alignment is 0, which means + that the widget fills the entire cell. + + From TQt 3.0, the \a alignment parameter is interpreted more + aggressively than in previous versions of TQt. A non-default + alignment now indicates that the widget should not grow to fill + the available space, but should be sized according to sizeHint(). + + \sa insertWidget(), setAutoAdd(), addLayout(), addSpacing() +*/ +void TQBoxLayout::addWidget( TQWidget *widget, int stretch, + int alignment ) +{ + insertWidget( -1, widget, stretch, alignment ); +} + +/*! + Adds \a layout to the end of the box, with serial stretch factor + \a stretch. + + When a layout is constructed with another layout as its parent, + you don't need to call addLayout(); the child layout is + automatically added to the parent layout as it is constructed. + + \sa insertLayout(), setAutoAdd(), addWidget(), addSpacing() +*/ +void TQBoxLayout::addLayout( TQLayout *layout, int stretch ) +{ + insertLayout( -1, layout, stretch ); +} + +/*! + Limits the perpendicular dimension of the box (e.g. height if the + box is LeftToRight) to a minimum of \a size. Other constraints may + increase the limit. +*/ +void TQBoxLayout::addStrut( int size ) +{ + TQLayoutItem *b; + if ( horz( dir ) ) + b = new TQSpacerItem( 0, size, TQSizePolicy::Fixed, + TQSizePolicy::Minimum ); + else + b = new TQSpacerItem( size, 0, TQSizePolicy::Minimum, + TQSizePolicy::Fixed ); + + TQBoxLayoutItem *it = new TQBoxLayoutItem( b ); + it->magic = TRUE; + data->list.append( it ); + invalidate(); +} + +/*! + Searches for widget \a w in this layout (not including child + layouts). + + Returns the index of \a w, or -1 if \a w is not found. +*/ +int TQBoxLayout::findWidget( TQWidget* w ) +{ + const int n = data->list.count(); + for ( int i = 0; i < n; i++ ) { + if ( data->list.at(i)->item->widget() == w ) + return i; + } + return -1; +} + +/*! + Sets the stretch factor for widget \a w to \a stretch and returns + TRUE if \a w is found in this layout (not including child + layouts); otherwise returns FALSE. +*/ +bool TQBoxLayout::setStretchFactor( TQWidget *w, int stretch ) +{ + TQPtrListIterator it( data->list ); + TQBoxLayoutItem *box; + while ( (box=it.current()) != 0 ) { + ++it; + if ( box->item->widget() == w ) { + box->stretch = stretch; + invalidate(); + return TRUE; + } + } + return FALSE; +} + +/*! + \overload + + Sets the stretch factor for the layout \a l to \a stretch and + returns TRUE if \a l is found in this layout (not including child + layouts); otherwise returns FALSE. +*/ +bool TQBoxLayout::setStretchFactor( TQLayout *l, int stretch ) +{ + TQPtrListIterator it( data->list ); + TQBoxLayoutItem *box; + while ( (box=it.current()) != 0 ) { + ++it; + if ( box->item->layout() == l ) { + box->stretch = stretch; + invalidate(); + return TRUE; + } + } + return FALSE; +} + +/*! + Sets the direction of this layout to \a direction. +*/ +void TQBoxLayout::setDirection( Direction direction ) +{ + if ( dir == direction ) + return; + if ( horz(dir) != horz(direction) ) { + //swap around the spacers (the "magic" bits) + //#### a bit yucky, knows too much. + //#### probably best to add access functions to spacerItem + //#### or even a TQSpacerItem::flip() + TQPtrListIterator it( data->list ); + TQBoxLayoutItem *box; + while ( (box=it.current()) != 0 ) { + ++it; + if ( box->magic ) { + TQSpacerItem *sp = box->item->spacerItem(); + if ( sp ) { + if ( sp->expanding() == TQSizePolicy::NoDirection ) { + //spacing or strut + TQSize s = sp->sizeHint(); + sp->changeSize( s.height(), s.width(), + horz(direction) ? TQSizePolicy::Fixed:TQSizePolicy::Minimum, + horz(direction) ? TQSizePolicy::Minimum:TQSizePolicy::Fixed ); + + } else { + //stretch + if ( horz(direction) ) + sp->changeSize( 0, 0, TQSizePolicy::Expanding, + TQSizePolicy::Minimum ); + else + sp->changeSize( 0, 0, TQSizePolicy::Minimum, + TQSizePolicy::Expanding ); + } + } + } + } + } + dir = direction; + invalidate(); + if ( mainWidget() ) { + TQEvent *lh = new TQEvent( TQEvent::LayoutHint ); + TQApplication::postEvent( mainWidget(), lh ); + } + +} + +/* + Initializes the data structure needed by qGeomCalc and + recalculates max/min and size hint. +*/ +void TQBoxLayout::setupGeom() +{ + if ( !data->dirty ) + return; + + int maxw = horz( dir ) ? 0 : TQLAYOUTSIZE_MAX; + int maxh = horz( dir ) ? TQLAYOUTSIZE_MAX : 0; + int minw = 0; + int minh = 0; + int hintw = 0; + int hinth = 0; + + bool horexp = FALSE; + bool verexp = FALSE; + + data->hasHfw = FALSE; + + delete data->geomArray; + int n = data->list.count(); + data->geomArray = new TQMemArray( n ); + TQMemArray& a = *data->geomArray; + + bool first = TRUE; + for ( int i = 0; i < n; i++ ) { + TQBoxLayoutItem *box = data->list.at( i ); + TQSize max = box->item->maximumSize(); + TQSize min = box->item->minimumSize(); + TQSize hint = box->item->sizeHint(); + TQSizePolicy::ExpandData exp = box->item->expanding(); + bool empty = box->item->isEmpty(); + // space before non-empties, except the first: + int space = ( empty || first ) ? 0 : spacing(); + bool ignore = empty && box->item->widget(); // ignore hidden widgets + + if ( horz(dir) ) { + bool expand = exp & TQSizePolicy::Horizontally || box->stretch > 0; + horexp = horexp || expand; + maxw += max.width() + space; + minw += min.width() + space; + hintw += hint.width() + space; + if ( !ignore ) + qMaxExpCalc( maxh, verexp, + max.height(), exp & TQSizePolicy::Vertically ); + minh = TQMAX( minh, min.height() ); + hinth = TQMAX( hinth, hint.height() ); + + a[i].sizeHint = hint.width(); + a[i].maximumSize = max.width(); + a[i].minimumSize = min.width(); + a[i].expansive = expand; + a[i].stretch = box->stretch ? box->stretch : box->hStretch(); + } else { + bool expand = ( exp & TQSizePolicy::Vertically || box->stretch > 0 ); + verexp = verexp || expand; + maxh += max.height() + space; + minh += min.height() + space; + hinth += hint.height() + space; + if ( !ignore ) + qMaxExpCalc( maxw, horexp, + max.width(), exp & TQSizePolicy::Horizontally ); + minw = TQMAX( minw, min.width() ); + hintw = TQMAX( hintw, hint.width() ); + + a[i].sizeHint = hint.height(); + a[i].maximumSize = max.height(); + a[i].minimumSize = min.height(); + a[i].expansive = expand; + a[i].stretch = box->stretch ? box->stretch : box->vStretch(); + } + + a[i].empty = empty; + data->hasHfw = data->hasHfw || box->item->hasHeightForWidth(); + first = first && empty; + } + + data->minSize = TQSize( minw, minh ); + data->maxSize = TQSize( maxw, maxh ).expandedTo( data->minSize ); + + data->expanding = (TQSizePolicy::ExpandData) + ( (horexp ? TQSizePolicy::Horizontally : 0) + | (verexp ? TQSizePolicy::Vertically : 0) ); + + data->sizeHint = TQSize( hintw, hinth ) + .expandedTo( data->minSize ) + .boundedTo( data->maxSize ); + + data->dirty = FALSE; +} + +/* + Calculates and stores the preferred height given the width \a w. +*/ +void TQBoxLayout::calcHfw( int w ) +{ + int h = 0; + int mh = 0; + + if ( horz(dir) ) { + TQMemArray &a = *data->geomArray; + int n = a.count(); + qGeomCalc( a, 0, n, 0, w, spacing() ); + for ( int i = 0; i < n; i++ ) { + TQBoxLayoutItem *box = data->list.at(i); + h = TQMAX( h, box->hfw(a[i].size) ); + mh = TQMAX( mh, box->mhfw(a[i].size) ); + } + } else { + TQPtrListIterator it( data->list ); + TQBoxLayoutItem *box; + bool first = TRUE; + while ( (box = it.current()) != 0 ) { + ++it; + bool empty = box->item->isEmpty(); + h += box->hfw( w ); + mh += box->mhfw( w ); + if ( !first && !empty ) { + h += spacing(); + mh += spacing(); + } + first = first && empty; + } + } + data->hfwWidth = w; + data->hfwHeight = h; + data->hfwMinHeight = mh; +} + +/*! + \fn TQBoxLayout::Direction TQBoxLayout::direction() const + + Returns the direction of the box. addWidget() and addSpacing() + work in this direction; the stretch stretches in this direction. + + \sa TQBoxLayout::Direction addWidget() addSpacing() +*/ + +/*! + \class TQHBoxLayout + \brief The TQHBoxLayout class lines up widgets horizontally. + + \ingroup geomanagement + \ingroup appearance + \mainclass + + This class is used to construct horizontal box layout objects. See + \l TQBoxLayout for more details. + + The simplest use of the class is like this: + \code + TQBoxLayout * l = new TQHBoxLayout( widget ); + l->setAutoAdd( TRUE ); + new TQSomeWidget( widget ); + new TQSomeOtherWidget( widget ); + new TQAnotherWidget( widget ); + \endcode + + or like this: + \code + TQBoxLayout * l = new TQHBoxLayout( widget ); + l->addWidget( existingChildOfWidget ); + l->addWidget( anotherChildOfWidget ); + \endcode + + \img qhboxlayout.png TQHBox + + \sa TQVBoxLayout TQGridLayout + \link layout.html the Layout overview \endlink +*/ + +/*! + Constructs a new top-level horizontal box called \a name, with + parent \a parent. + + The \a margin is the number of pixels between the edge of the + widget and its managed children. The \a spacing is the default + number of pixels between neighboring children. If \a spacing is -1 + the value of \a margin is used for \a spacing. +*/ +TQHBoxLayout::TQHBoxLayout( TQWidget *parent, int margin, + int spacing, const char *name ) + : TQBoxLayout( parent, LeftToRight, margin, spacing, name ) +{ +} + +/*! + Constructs a new horizontal box called name \a name and adds it to + \a parentLayout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, this TQHBoxLayout will inherit its + parent's spacing(). +*/ +TQHBoxLayout::TQHBoxLayout( TQLayout *parentLayout, int spacing, + const char *name ) + : TQBoxLayout( parentLayout, LeftToRight, spacing, name ) +{ +} + +/*! + Constructs a new horizontal box called name \a name. You must add + it to another layout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, this TQHBoxLayout will inherit its + parent's spacing(). +*/ +TQHBoxLayout::TQHBoxLayout( int spacing, const char *name ) + : TQBoxLayout( LeftToRight, spacing, name ) +{ +} + +/*! + Destroys this box layout. + + The layout's widgets aren't destroyed. +*/ +TQHBoxLayout::~TQHBoxLayout() +{ +} + +/*! + \class TQVBoxLayout + + \brief The TQVBoxLayout class lines up widgets vertically. + + \ingroup geomanagement + \ingroup appearance + \mainclass + + This class is used to construct vertical box layout objects. See + TQBoxLayout for more details. + + The simplest use of the class is like this: + \code + TQBoxLayout * l = new TQVBoxLayout( widget ); + l->addWidget( aWidget ); + l->addWidget( anotherWidget ); + \endcode + + \img qvboxlayout.png TQVBox + + \sa TQHBoxLayout TQGridLayout \link layout.html the Layout overview \endlink +*/ + +/*! + Constructs a new top-level vertical box called \a name, with + parent \a parent. + + The \a margin is the number of pixels between the edge of the + widget and its managed children. The \a spacing is the default + number of pixels between neighboring children. If \a spacing is -1 + the value of \a margin is used for \a spacing. +*/ +TQVBoxLayout::TQVBoxLayout( TQWidget *parent, int margin, int spacing, + const char *name ) + : TQBoxLayout( parent, TopToBottom, margin, spacing, name ) +{ + +} + +/*! + Constructs a new vertical box called name \a name and adds it to + \a parentLayout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, this TQVBoxLayout will inherit its + parent's spacing(). +*/ +TQVBoxLayout::TQVBoxLayout( TQLayout *parentLayout, int spacing, + const char *name ) + : TQBoxLayout( parentLayout, TopToBottom, spacing, name ) +{ +} + +/*! + Constructs a new vertical box called name \a name. You must add + it to another layout. + + The \a spacing is the default number of pixels between neighboring + children. If \a spacing is -1, this TQVBoxLayout will inherit its + parent's spacing(). +*/ +TQVBoxLayout::TQVBoxLayout( int spacing, const char *name ) + : TQBoxLayout( TopToBottom, spacing, name ) +{ +} + +/*! + Destroys this box layout. + + The layout's widgets aren't destroyed. +*/ +TQVBoxLayout::~TQVBoxLayout() +{ +} + +TQBoxLayout *TQBoxLayout::createTmpCopy() +{ + TQBoxLayout *bl = new TQBoxLayout( direction() ); + delete bl->data; + bl->data = data; + return bl; +} + +#endif // QT_NO_LAYOUT diff --git a/src/kernel/qlayout.h b/src/kernel/qlayout.h new file mode 100644 index 000000000..aa0e58415 --- /dev/null +++ b/src/kernel/qlayout.h @@ -0,0 +1,473 @@ +/**************************************************************************** +** +** Definition of layout classes +** +** Created : 960416 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQLAYOUT_H +#define TQLAYOUT_H + +#ifndef QT_H +#include "qobject.h" +#include "qsizepolicy.h" +#include "qwidget.h" +#endif // QT_H + +#include + +#ifndef QT_NO_LAYOUT + +#if 0 +Q_OBJECT +#endif + +static const int TQLAYOUTSIZE_MAX = INT_MAX/256/16; + +class TQGridLayoutBox; +class TQGridLayoutData; +class TQLayout; +class TQLayoutItem; +struct TQLayoutData; +class TQMenuBar; +class TQSpacerItem; +class TQWidget; + +class Q_EXPORT TQGLayoutIterator : public TQShared +{ +public: + virtual ~TQGLayoutIterator(); + virtual TQLayoutItem *next() = 0; + virtual TQLayoutItem *current() = 0; + virtual TQLayoutItem *takeCurrent() = 0; +}; + +class Q_EXPORT TQLayoutIterator +{ +public: + TQLayoutIterator( TQGLayoutIterator *i ) : it( i ) { } + TQLayoutIterator( const TQLayoutIterator &i ) : it( i.it ) { + if ( it ) + it->ref(); + } + ~TQLayoutIterator() { if ( it && it->deref() ) delete it; } + TQLayoutIterator &operator=( const TQLayoutIterator &i ) { + if ( i.it ) + i.it->ref(); + if ( it && it->deref() ) + delete it; + it = i.it; + return *this; + } + TQLayoutItem *operator++() { return it ? it->next() : 0; } + TQLayoutItem *current() { return it ? it->current() : 0; } + TQLayoutItem *takeCurrent() { return it ? it->takeCurrent() : 0; } + void deleteCurrent(); + +private: + TQGLayoutIterator *it; +}; + +class Q_EXPORT TQLayoutItem +{ +public: + TQLayoutItem( int alignment = 0 ) : align( alignment ) { } + virtual ~TQLayoutItem(); + virtual TQSize sizeHint() const = 0; + virtual TQSize minimumSize() const = 0; + virtual TQSize maximumSize() const = 0; + virtual TQSizePolicy::ExpandData expanding() const = 0; + virtual void setGeometry( const TQRect& ) = 0; + virtual TQRect geometry() const = 0; + virtual bool isEmpty() const = 0; + virtual bool hasHeightForWidth() const; + virtual int heightForWidth( int ) const; + // ### add minimumHeightForWidth( int ) in TQt 4.0 + virtual void invalidate(); + + virtual TQWidget *widget(); + virtual TQLayoutIterator iterator(); + virtual TQLayout *layout(); + virtual TQSpacerItem *spacerItem(); + + int alignment() const { return align; } + virtual void setAlignment( int a ); + +protected: + int align; +}; + +class Q_EXPORT TQSpacerItem : public TQLayoutItem +{ +public: + TQSpacerItem( int w, int h, + TQSizePolicy::SizeType hData = TQSizePolicy::Minimum, + TQSizePolicy::SizeType vData = TQSizePolicy::Minimum ) + : width( w ), height( h ), sizeP( hData, vData ) { } + void changeSize( int w, int h, + TQSizePolicy::SizeType hData = TQSizePolicy::Minimum, + TQSizePolicy::SizeType vData = TQSizePolicy::Minimum ); + TQSize sizeHint() const; + TQSize minimumSize() const; + TQSize maximumSize() const; + TQSizePolicy::ExpandData expanding() const; + bool isEmpty() const; + void setGeometry( const TQRect& ); + TQRect geometry() const; + TQSpacerItem *spacerItem(); + +private: + int width; + int height; + TQSizePolicy sizeP; + TQRect rect; +}; + +class Q_EXPORT TQWidgetItem : public TQLayoutItem +{ +public: + TQWidgetItem( TQWidget *w ) : wid( w ) { } + TQSize sizeHint() const; + TQSize minimumSize() const; + TQSize maximumSize() const; + TQSizePolicy::ExpandData expanding() const; + bool isEmpty() const; + void setGeometry( const TQRect& ); + TQRect geometry() const; + virtual TQWidget *widget(); + + bool hasHeightForWidth() const; + int heightForWidth( int ) const; + +private: + TQWidget *wid; +}; + +class Q_EXPORT TQLayout : public TQObject, public TQLayoutItem +{ + Q_OBJECT + Q_ENUMS( ResizeMode ) + Q_PROPERTY( int margin READ margin WRITE setMargin ) + Q_PROPERTY( int spacing READ spacing WRITE setSpacing ) + Q_PROPERTY( ResizeMode resizeMode READ resizeMode WRITE setResizeMode ) + +public: + // ### TQt 4.0: put 'Auto' first in enum + enum ResizeMode { FreeResize, Minimum, Fixed, Auto }; + + TQLayout( TQWidget *parent, int margin = 0, int spacing = -1, + const char *name = 0 ); + TQLayout( TQLayout *parentLayout, int spacing = -1, const char *name = 0 ); + TQLayout( int spacing = -1, const char *name = 0 ); + ~TQLayout(); + + int margin() const { return outsideBorder; } + int spacing() const { return insideSpacing; } + + virtual void setMargin( int ); + virtual void setSpacing( int ); + + int defaultBorder() const { return insideSpacing; } + void freeze( int w, int h ); + void freeze() { setResizeMode( Fixed ); } + + void setResizeMode( ResizeMode ); + ResizeMode resizeMode() const; + +#ifndef QT_NO_MENUBAR + virtual void setMenuBar( TQMenuBar *w ); + TQMenuBar *menuBar() const { return menubar; } +#endif + + TQWidget *mainWidget(); + bool isTopLevel() const { return topLevel; } + + virtual void setAutoAdd( bool ); + bool autoAdd() const { return autoNewChild; } + + void invalidate(); + TQRect geometry() const; + bool activate(); + + void add( TQWidget *w ) { addItem( new TQWidgetItem(w) ); } + virtual void addItem( TQLayoutItem * ) = 0; + + void remove( TQWidget *w ); + void removeItem( TQLayoutItem * ); + + TQSizePolicy::ExpandData expanding() const; + TQSize minimumSize() const; + TQSize maximumSize() const; + void setGeometry( const TQRect& ) = 0; + TQLayoutIterator iterator() = 0; + bool isEmpty() const; + + int totalHeightForWidth( int w ) const; + TQSize totalMinimumSize() const; + TQSize totalMaximumSize() const; + TQSize totalSizeHint() const; + TQLayout *layout(); + + bool supportsMargin() const { return marginImpl; } + + void setEnabled( bool ); + bool isEnabled() const; + +protected: + bool eventFilter( TQObject *, TQEvent * ); + void childEvent( TQChildEvent *e ); + void addChildLayout( TQLayout *l ); + void deleteAllItems(); + + void setSupportsMargin( bool ); + TQRect alignmentRect( const TQRect& ) const; + +private: + void setWidgetLayout( TQWidget *, TQLayout * ); + void init(); + int insideSpacing; + int outsideBorder; + uint topLevel : 1; + uint enabled : 1; + uint autoNewChild : 1; + uint frozen : 1; + uint activated : 1; + uint marginImpl : 1; + uint autoMinimum : 1; + uint autoResizeMode : 1; + TQRect rect; + TQLayoutData *extraData; +#ifndef QT_NO_MENUBAR + TQMenuBar *menubar; +#endif + +private: +#if defined(Q_DISABLE_COPY) + TQLayout( const TQLayout & ); + TQLayout &operator=( const TQLayout & ); +#endif + + static void propagateSpacing( TQLayout *layout ); +}; + +inline void TQLayoutIterator::deleteCurrent() +{ + delete takeCurrent(); +} + +class Q_EXPORT TQGridLayout : public TQLayout +{ + Q_OBJECT +public: + TQGridLayout( TQWidget *parent, int nRows = 1, int nCols = 1, int border = 0, + int spacing = -1, const char *name = 0 ); + TQGridLayout( int nRows = 1, int nCols = 1, int spacing = -1, + const char *name = 0 ); + TQGridLayout( TQLayout *parentLayout, int nRows = 1, int nCols = 1, + int spacing = -1, const char *name = 0 ); + ~TQGridLayout(); + + TQSize sizeHint() const; + TQSize minimumSize() const; + TQSize maximumSize() const; + + // ### remove 'virtual' in 4.0 (or add 'virtual' to set{Row,Col}Spacing()) + virtual void setRowStretch( int row, int stretch ); + virtual void setColStretch( int col, int stretch ); + int rowStretch( int row ) const; + int colStretch( int col ) const; + + void setRowSpacing( int row, int minSize ); + void setColSpacing( int col, int minSize ); + int rowSpacing( int row ) const; + int colSpacing( int col ) const; + + int numRows() const; + int numCols() const; + TQRect cellGeometry( int row, int col ) const; + + bool hasHeightForWidth() const; + int heightForWidth( int ) const; + int minimumHeightForWidth( int ) const; + + TQSizePolicy::ExpandData expanding() const; + void invalidate(); + + void addItem( TQLayoutItem * ); + void addItem( TQLayoutItem *item, int row, int col ); + void addMultiCell( TQLayoutItem *, int fromRow, int toRow, + int fromCol, int toCol, int align = 0 ); + + void addWidget( TQWidget *, int row, int col, int align = 0 ); + void addMultiCellWidget( TQWidget *, int fromRow, int toRow, + int fromCol, int toCol, int align = 0 ); + void addLayout( TQLayout *layout, int row, int col); + void addMultiCellLayout( TQLayout *layout, int fromRow, int toRow, + int fromCol, int toCol, int align = 0 ); + void addRowSpacing( int row, int minsize ); + void addColSpacing( int col, int minsize ); + + void expand( int rows, int cols ); + + enum Corner { TopLeft, TopRight, BottomLeft, BottomRight }; + void setOrigin( Corner ); + Corner origin() const; + TQLayoutIterator iterator(); + void setGeometry( const TQRect& ); + +protected: + bool findWidget( TQWidget* w, int *r, int *c ); + void add( TQLayoutItem*, int row, int col ); + +private: +#if defined(Q_DISABLE_COPY) + TQGridLayout( const TQGridLayout & ); + TQGridLayout &operator=( const TQGridLayout & ); +#endif + + void init( int rows, int cols ); + TQGridLayoutData *data; +}; + +class TQBoxLayoutData; +class TQDockWindow; + +class Q_EXPORT TQBoxLayout : public TQLayout +{ + Q_OBJECT +public: + enum Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop, + Down = TopToBottom, Up = BottomToTop }; + + TQBoxLayout( TQWidget *parent, Direction, int border = 0, int spacing = -1, + const char *name = 0 ); + TQBoxLayout( TQLayout *parentLayout, Direction, int spacing = -1, + const char *name = 0 ); + TQBoxLayout( Direction, int spacing = -1, const char *name = 0 ); + ~TQBoxLayout(); + + void addItem( TQLayoutItem * ); + + Direction direction() const { return dir; } + void setDirection( Direction ); + + void addSpacing( int size ); + void addStretch( int stretch = 0 ); + void addWidget( TQWidget *, int stretch = 0, int alignment = 0 ); + void addLayout( TQLayout *layout, int stretch = 0 ); + void addStrut( int ); + + void insertSpacing( int index, int size ); + void insertStretch( int index, int stretch = 0 ); + void insertWidget( int index, TQWidget *widget, int stretch = 0, + int alignment = 0 ); + void insertLayout( int index, TQLayout *layout, int stretch = 0 ); + + bool setStretchFactor( TQWidget*, int stretch ); + bool setStretchFactor( TQLayout *l, int stretch ); + + TQSize sizeHint() const; + TQSize minimumSize() const; + TQSize maximumSize() const; + + bool hasHeightForWidth() const; + int heightForWidth( int ) const; + int minimumHeightForWidth( int ) const; + + TQSizePolicy::ExpandData expanding() const; + void invalidate(); + TQLayoutIterator iterator(); + void setGeometry( const TQRect& ); + + int findWidget( TQWidget* w ); + +protected: + void insertItem( int index, TQLayoutItem * ); + +private: + friend class TQDockWindow; +#if defined(Q_DISABLE_COPY) + TQBoxLayout( const TQBoxLayout & ); + TQBoxLayout &operator=( const TQBoxLayout & ); +#endif + + void setupGeom(); + void calcHfw( int ); + TQBoxLayoutData *data; + Direction dir; + TQBoxLayout *createTmpCopy(); +}; + +class Q_EXPORT TQHBoxLayout : public TQBoxLayout +{ + Q_OBJECT +public: + TQHBoxLayout( TQWidget *parent, int border = 0, + int spacing = -1, const char *name = 0 ); + TQHBoxLayout( TQLayout *parentLayout, + int spacing = -1, const char *name = 0 ); + TQHBoxLayout( int spacing = -1, const char *name = 0 ); + + ~TQHBoxLayout(); + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQHBoxLayout( const TQHBoxLayout & ); + TQHBoxLayout &operator=( const TQHBoxLayout & ); +#endif +}; + +class Q_EXPORT TQVBoxLayout : public TQBoxLayout +{ + Q_OBJECT +public: + TQVBoxLayout( TQWidget *parent, int border = 0, + int spacing = -1, const char *name = 0 ); + TQVBoxLayout( TQLayout *parentLayout, + int spacing = -1, const char *name = 0 ); + TQVBoxLayout( int spacing = -1, const char *name = 0 ); + + ~TQVBoxLayout(); + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQVBoxLayout( const TQVBoxLayout & ); + TQVBoxLayout &operator=( const TQVBoxLayout & ); +#endif +}; + +#endif // QT_NO_LAYOUT +#endif // TQLAYOUT_H diff --git a/src/kernel/qlayoutengine.cpp b/src/kernel/qlayoutengine.cpp new file mode 100644 index 000000000..a295b278a --- /dev/null +++ b/src/kernel/qlayoutengine.cpp @@ -0,0 +1,322 @@ +/**************************************************************************** +** +** Implementation of TQLayout functionality +** +** Created : 981231 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qlayout.h" +#include "private/qlayoutengine_p.h" + +#ifndef QT_NO_LAYOUT + +static inline int toFixed( int i ) { return i * 256; } +static inline int fRound( int i ) { + return ( i % 256 < 128 ) ? i / 256 : 1 + i / 256; +} + +/* + This is the main workhorse of the TQGridLayout. It portions out + available space to the chain's children. + + The calculation is done in fixed point: "fixed" variables are + scaled by a factor of 256. + + If the layout runs "backwards" (i.e. RightToLeft or Up) the layout + is computed mirror-reversed, and it's the caller's responsibility + do reverse the values before use. + + chain contains input and output parameters describing the geometry. + count is the count of items in the chain; pos and space give the + interval (relative to parentWidget topLeft). +*/ +Q_EXPORT void qGeomCalc( TQMemArray &chain, int start, int count, + int pos, int space, int spacer ) +{ + typedef int fixed; + int cHint = 0; + int cMin = 0; + int cMax = 0; + int sumStretch = 0; + int spacerCount = 0; + + bool wannaGrow = FALSE; // anyone who really wants to grow? + // bool canShrink = FALSE; // anyone who could be persuaded to shrink? + + int i; + for ( i = start; i < start + count; i++ ) { + chain[i].done = FALSE; + cHint += chain[i].smartSizeHint(); + cMin += chain[i].minimumSize; + cMax += chain[i].maximumSize; + sumStretch += chain[i].stretch; + if ( !chain[i].empty ) + spacerCount++; + wannaGrow = wannaGrow || chain[i].expansive || chain[i].stretch > 0; + } + + int extraspace = 0; + if ( spacerCount ) + spacerCount--; // only spacers between things + if ( space < cMin + spacerCount * spacer ) { + for ( i = start; i < start+count; i++ ) { + chain[i].size = chain[i].minimumSize; + chain[i].done = TRUE; + } + } else if ( space < cHint + spacerCount*spacer ) { + /* + Less space than smartSizeHint(), but more than minimumSize. + Currently take space equally from each, as in TQt 2.x. + Commented-out lines will give more space to stretchier + items. + */ + int n = count; + int space_left = space - spacerCount*spacer; + int overdraft = cHint - space_left; + + // first give to the fixed ones: + for ( i = start; i < start + count; i++ ) { + if ( !chain[i].done + && chain[i].minimumSize >= chain[i].smartSizeHint() ) { + chain[i].size = chain[i].smartSizeHint(); + chain[i].done = TRUE; + space_left -= chain[i].smartSizeHint(); + // sumStretch -= chain[i].stretch; + n--; + } + } + bool finished = n == 0; + while ( !finished ) { + finished = TRUE; + fixed fp_over = toFixed( overdraft ); + fixed fp_w = 0; + + for ( i = start; i < start+count; i++ ) { + if ( chain[i].done ) + continue; + // if ( sumStretch <= 0 ) + fp_w += fp_over / n; + // else + // fp_w += (fp_over * chain[i].stretch) / sumStretch; + int w = fRound( fp_w ); + chain[i].size = chain[i].smartSizeHint() - w; + fp_w -= toFixed( w ); // give the difference to the next + if ( chain[i].size < chain[i].minimumSize ) { + chain[i].done = TRUE; + chain[i].size = chain[i].minimumSize; + finished = FALSE; + overdraft -= ( chain[i].smartSizeHint() + - chain[i].minimumSize ); + // sumStretch -= chain[i].stretch; + n--; + break; + } + } + } + } else { // extra space + int n = count; + int space_left = space - spacerCount*spacer; + // first give to the fixed ones, and handle non-expansiveness + for ( i = start; i < start + count; i++ ) { + if ( !chain[i].done + && (chain[i].maximumSize <= chain[i].smartSizeHint() + || (wannaGrow && !chain[i].expansive && chain[i].stretch == 0)) ) { + chain[i].size = chain[i].smartSizeHint(); + chain[i].done = TRUE; + space_left -= chain[i].smartSizeHint(); + sumStretch -= chain[i].stretch; + n--; + } + } + extraspace = space_left; + + /* + Do a trial distribution and calculate how much it is off. + If there are more deficit pixels than surplus pixels, give + the minimum size items what they need, and repeat. + Otherwise give to the maximum size items, and repeat. + + Paul Olav Tvete has a wonderful mathematical proof of the + correctness of this principle, but unfortunately this + comment is too small to contain it. + */ + int surplus, deficit; + do { + surplus = deficit = 0; + fixed fp_space = toFixed( space_left ); + fixed fp_w = 0; + for ( i = start; i < start+count; i++ ) { + if ( chain[i].done ) + continue; + extraspace = 0; + if ( sumStretch <= 0 ) + fp_w += fp_space / n; + else + fp_w += (fp_space * chain[i].stretch) / sumStretch; + int w = fRound( fp_w ); + chain[i].size = w; + fp_w -= toFixed( w ); // give the difference to the next + if ( w < chain[i].smartSizeHint() ) { + deficit += chain[i].smartSizeHint() - w; + } else if ( w > chain[i].maximumSize ) { + surplus += w - chain[i].maximumSize; + } + } + if ( deficit > 0 && surplus <= deficit ) { + // give to the ones that have too little + for ( i = start; i < start+count; i++ ) { + if ( !chain[i].done && + chain[i].size < chain[i].smartSizeHint() ) { + chain[i].size = chain[i].smartSizeHint(); + chain[i].done = TRUE; + space_left -= chain[i].smartSizeHint(); + sumStretch -= chain[i].stretch; + n--; + } + } + } + if ( surplus > 0 && surplus >= deficit ) { + // take from the ones that have too much + for ( i = start; i < start+count; i++ ) { + if ( !chain[i].done && + chain[i].size > chain[i].maximumSize ) { + chain[i].size = chain[i].maximumSize; + chain[i].done = TRUE; + space_left -= chain[i].maximumSize; + sumStretch -= chain[i].stretch; + n--; + } + } + } + } while ( n > 0 && surplus != deficit ); + if ( n == 0 ) + extraspace = space_left; + } + + /* + As a last resort, we distribute the unwanted space equally + among the spacers (counting the start and end of the chain). We + could, but don't, attempt a sub-pixel allocation of the extra + space. + */ + int extra = extraspace / ( spacerCount + 2 ); + int p = pos + extra; + for ( i = start; i < start+count; i++ ) { + chain[i].pos = p; + p = p + chain[i].size; + if ( !chain[i].empty ) + p += spacer+extra; + } +} + +Q_EXPORT TQSize qSmartMinSize( const TQWidgetItem *i ) +{ + TQWidget *w = ((TQWidgetItem *)i)->widget(); + + TQSize s( 0, 0 ); + if ( w->layout() ) { + s = w->layout()->totalMinimumSize(); + } else { + TQSize sh; + + if ( w->sizePolicy().horData() != TQSizePolicy::Ignored ) { + if ( w->sizePolicy().mayShrinkHorizontally() ) { + s.setWidth( w->minimumSizeHint().width() ); + } else { + sh = w->sizeHint(); + s.setWidth( sh.width() ); + } + } + + if ( w->sizePolicy().verData() != TQSizePolicy::Ignored ) { + if ( w->sizePolicy().mayShrinkVertically() ) { + s.setHeight( w->minimumSizeHint().height() ); + } else { + s.setHeight( sh.isValid() ? sh.height() + : w->sizeHint().height() ); + } + } + } + s = s.boundedTo( w->maximumSize() ); + TQSize min = w->minimumSize(); + if ( min.width() > 0 ) + s.setWidth( min.width() ); + if ( min.height() > 0 ) + s.setHeight( min.height() ); + + if ( i->hasHeightForWidth() && min.height() == 0 && min.width() > 0 ) + s.setHeight( i->heightForWidth(s.width()) ); + + s = s.expandedTo( TQSize(1, 1) ); + return s; +} + +Q_EXPORT TQSize qSmartMinSize( TQWidget *w ) +{ + TQWidgetItem item( w ); + return qSmartMinSize( &item ); +} + +Q_EXPORT TQSize qSmartMaxSize( const TQWidgetItem *i, int align ) +{ + TQWidget *w = ( (TQWidgetItem*)i )->widget(); + if ( align & TQt::AlignHorizontal_Mask && align & TQt::AlignVertical_Mask ) + return TQSize( TQLAYOUTSIZE_MAX, TQLAYOUTSIZE_MAX ); + TQSize s = w->maximumSize(); + if ( s.width() == TQWIDGETSIZE_MAX && !(align & TQt::AlignHorizontal_Mask) ) + if ( !w->sizePolicy().mayGrowHorizontally() ) + s.setWidth( w->sizeHint().width() ); + + if ( s.height() == TQWIDGETSIZE_MAX && !(align & TQt::AlignVertical_Mask) ) + if ( !w->sizePolicy().mayGrowVertically() ) + s.setHeight( w->sizeHint().height() ); + + s = s.expandedTo( w->minimumSize() ); + + if ( align & TQt::AlignHorizontal_Mask ) + s.setWidth( TQLAYOUTSIZE_MAX ); + if ( align & TQt::AlignVertical_Mask ) + s.setHeight( TQLAYOUTSIZE_MAX ); + return s; +} + +Q_EXPORT TQSize qSmartMaxSize( TQWidget *w, int align ) +{ + TQWidgetItem item( w ); + return qSmartMaxSize( &item, align ); +} + +#endif // QT_NO_LAYOUT diff --git a/src/kernel/qlayoutengine_p.h b/src/kernel/qlayoutengine_p.h new file mode 100644 index 000000000..d0cd72d7a --- /dev/null +++ b/src/kernel/qlayoutengine_p.h @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Internal header file. +** +** Created : 981027 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQLAYOUTENGINE_P_H +#define TQLAYOUTENGINE_P_H + +#ifndef TQLAYOUT_H + #error "Need to include qlayout.h before including qlayoutengine_p.h" +#endif + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of qlayout.cpp, qlayoutengine.cpp, qmainwindow.cpp and qsplitter.cpp. +// This header file may change from version to version without notice, +// or even be removed. +// +// We mean it. +// +// + + +#ifndef QT_H +#include "qabstractlayout.h" +#endif // QT_H + +#ifndef QT_NO_LAYOUT + +struct TQLayoutStruct +{ + inline void init( int stretchFactor = 0, int spacing = 0 ) { + stretch = stretchFactor; + minimumSize = sizeHint = spacing; + maximumSize = TQLAYOUTSIZE_MAX; + expansive = FALSE; + empty = TRUE; + } + + TQCOORD smartSizeHint() { + return ( stretch > 0 ) ? minimumSize : sizeHint; + } + + // parameters + int stretch; + TQCOORD sizeHint; + TQCOORD maximumSize; + TQCOORD minimumSize; + bool expansive; + bool empty; + + // temporary storage + bool done; + + // result + int pos; + int size; +}; + + +Q_EXPORT void qGeomCalc( TQMemArray &chain, int start, int count, + int pos, int space, int spacer ); +Q_EXPORT TQSize qSmartMinSize( const TQWidgetItem *i ); +Q_EXPORT TQSize qSmartMinSize( TQWidget *w ); +Q_EXPORT TQSize qSmartMaxSize( const TQWidgetItem *i, int align = 0 ); +Q_EXPORT TQSize qSmartMaxSize( TQWidget *w, int align = 0 ); + + +/* + Modify total maximum (max) and total expansion (exp) + when adding boxmax/boxexp. + + Expansive boxes win over non-expansive boxes. +*/ +static inline void qMaxExpCalc( TQCOORD & max, bool &exp, + TQCOORD boxmax, bool boxexp ) +{ + if ( exp ) { + if ( boxexp ) + max = TQMAX( max, boxmax ); + } else { + if ( boxexp ) + max = boxmax; + else + max = TQMIN( max, boxmax ); + } + exp = exp || boxexp; +} + +#endif //QT_NO_LAYOUT +#endif diff --git a/src/kernel/qlocalfs.cpp b/src/kernel/qlocalfs.cpp new file mode 100644 index 000000000..1333f9874 --- /dev/null +++ b/src/kernel/qlocalfs.cpp @@ -0,0 +1,407 @@ +/**************************************************************************** +** +** Implementation of TQLocalFs class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qlocalfs.h" + +#ifndef QT_NO_NETWORKPROTOCOL + +#include "qfileinfo.h" +#include "qfile.h" +#include "qurlinfo.h" +#include "qapplication.h" +#include "qurloperator.h" +#include "qguardedptr.h" + +//#define TQLOCALFS_DEBUG + + +/*! + \class TQLocalFs qlocalfs.h + \brief The TQLocalFs class is an implementation of a + TQNetworkProtocol that works on the local file system. +\if defined(commercial) + It is part of the TQt Enterprise Edition. +\endif + + \module network + + \ingroup io + + This class is derived from TQNetworkProtocol. TQLocalFs is not + normally used directly, but rather through a TQUrlOperator, for + example: + \code + TQUrlOperator op( "file:///tmp" ); + op.listChildren(); // Asks the server to provide a directory listing + \endcode + + This code will only work if the TQLocalFs class is registered; to + register the class, you must call qInitNetworkProtocols() before + using a TQUrlOperator with TQLocalFs. + + If you really need to use TQLocalFs directly, don't forget + to set its TQUrlOperator with setUrl(). + + \sa \link network.html TQt Network Documentation \endlink TQNetworkProtocol, TQUrlOperator +*/ + +/*! + Constructor. +*/ + +TQLocalFs::TQLocalFs() + : TQNetworkProtocol() +{ +} + +static int convertPermissions(TQFileInfo *fi) +{ + int p = 0; + if ( fi->permission( TQFileInfo::ReadOwner ) ) + p |= TQUrlInfo::ReadOwner; + if ( fi->permission( TQFileInfo::WriteOwner ) ) + p |= TQUrlInfo::WriteOwner; + if ( fi->permission( TQFileInfo::ExeOwner ) ) + p |= TQUrlInfo::ExeOwner; + if ( fi->permission( TQFileInfo::ReadGroup ) ) + p |= TQUrlInfo::ReadGroup; + if ( fi->permission( TQFileInfo::WriteGroup ) ) + p |= TQUrlInfo::WriteGroup; + if ( fi->permission( TQFileInfo::ExeGroup ) ) + p |= TQUrlInfo::ExeGroup; + if ( fi->permission( TQFileInfo::ReadOther ) ) + p |= TQUrlInfo::ReadOther; + if ( fi->permission( TQFileInfo::WriteOther ) ) + p |= TQUrlInfo::WriteOther; + if ( fi->permission( TQFileInfo::ExeOther ) ) + p |= TQUrlInfo::ExeOther; + return p; +} + +/*! + \reimp +*/ + +void TQLocalFs::operationListChildren( TQNetworkOperation *op ) +{ +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: operationListChildren" ); +#endif + op->setState( StInProgress ); + + dir = TQDir( url()->path() ); + dir.setNameFilter( url()->nameFilter() ); + dir.setMatchAllDirs( TRUE ); + if ( !dir.isReadable() ) { + TQString msg = tr( "Could not read directory\n%1" ).arg( url()->path() ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrListChildren ); + emit finished( op ); + return; + } + + const TQFileInfoList *filist = dir.entryInfoList( TQDir::All | TQDir::Hidden | TQDir::System ); + if ( !filist ) { + TQString msg = tr( "Could not read directory\n%1" ).arg( url()->path() ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrListChildren ); + emit finished( op ); + return; + } + + emit start( op ); + + TQFileInfoListIterator it( *filist ); + TQFileInfo *fi; + TQValueList infos; + while ( ( fi = it.current() ) != 0 ) { + ++it; + infos << TQUrlInfo( fi->fileName(), convertPermissions(fi), fi->owner(), fi->group(), + fi->size(), fi->lastModified(), fi->lastRead(), fi->isDir(), fi->isFile(), + fi->isSymLink(), fi->isWritable(), fi->isReadable(), fi->isExecutable() ); + } + emit newChildren( infos, op ); + op->setState( StDone ); + emit finished( op ); +} + +/*! + \reimp +*/ + +void TQLocalFs::operationMkDir( TQNetworkOperation *op ) +{ +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: operationMkDir" ); +#endif + op->setState( StInProgress ); + TQString dirname = op->arg( 0 ); + + dir = TQDir( url()->path() ); + if ( dir.mkdir( dirname ) ) { + TQFileInfo fi( dir, dirname ); + TQUrlInfo inf( fi.fileName(), convertPermissions(&fi), fi.owner(), fi.group(), + fi.size(), fi.lastModified(), fi.lastRead(), fi.isDir(), fi.isFile(), + fi.isSymLink(), fi.isWritable(), fi.isReadable(), fi.isExecutable() ); + emit newChild( inf, op ); + op->setState( StDone ); + emit createdDirectory( inf, op ); + emit finished( op ); + } else { + TQString msg = tr( "Could not create directory\n%1" ).arg( dirname ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrMkDir ); + emit finished( op ); + } +} + +/*! + \reimp +*/ + +void TQLocalFs::operationRemove( TQNetworkOperation *op ) +{ +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: operationRemove" ); +#endif + op->setState( StInProgress ); + TQString name = TQUrl( op->arg( 0 ) ).path(); + bool deleted = FALSE; + + dir = TQDir( url()->path() ); + + TQFileInfo fi( dir, name ); + if ( fi.isDir() ) { + if ( dir.rmdir( name ) ) + deleted = TRUE; + } + + if ( deleted || dir.remove( name ) ) { + op->setState( StDone ); + emit removed( op ); + emit finished( op ); + } else { + TQString msg = tr( "Could not remove file or directory\n%1" ).arg( name ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrRemove ); + emit finished( op ); + } +} + +/*! + \reimp +*/ + +void TQLocalFs::operationRename( TQNetworkOperation *op ) +{ +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: operationRename" ); +#endif + op->setState( StInProgress ); + TQString oldname = op->arg( 0 ); + TQString newname = op->arg( 1 ); + + dir = TQDir( url()->path() ); + if ( dir.rename( oldname, newname ) ) { + op->setState( StDone ); + emit itemChanged( op ); + emit finished( op ); + } else { + TQString msg = tr( "Could not rename\n%1\nto\n%2" ).arg( oldname ).arg( newname ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrRename ); + emit finished( op ); + } +} + +/*! + \reimp +*/ + +void TQLocalFs::operationGet( TQNetworkOperation *op ) +{ +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: operationGet" ); +#endif + op->setState( StInProgress ); + TQString from = TQUrl( op->arg( 0 ) ).path(); + + TQFile f( from ); + if ( !f.open( IO_ReadOnly ) ) { +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: could not open %s", from.latin1() ); +#endif + TQString msg = tr( "Could not open\n%1" ).arg( from ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrGet ); + emit finished( op ); + return; + } + + TQByteArray s; + emit dataTransferProgress( 0, f.size(), op ); + if ( f.size() != 0 ) { + int blockSize = calcBlockSize( f.size() ); + if ( (int)f.size() < blockSize ) { + s.resize( f.size() ); + f.readBlock( s.data(), f.size() ); + emit data( s, op ); + emit dataTransferProgress( f.size(), f.size(), op ); +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: got all %d bytes at once", f.size() ); +#endif + } else { + s.resize( blockSize ); + int remaining = f.size(); + TQGuardedPtr that = this; + while ( that && remaining > 0 ) { + if ( operationInProgress() != op ) + return; + if ( remaining >= blockSize ) { + f.readBlock( s.data(), blockSize ); + emit data( s, op ); + emit dataTransferProgress( f.size() - remaining, f.size(), op ); + remaining -= blockSize; + } else { + s.resize( remaining ); + f.readBlock( s.data(), remaining ); + emit data( s, op ); + emit dataTransferProgress( f.size() - remaining, f.size(), op ); + remaining -= remaining; + } + qApp->processEvents(); + } + if (!that) + return; +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: got all %d bytes step by step", f.size() ); +#endif + emit dataTransferProgress( f.size(), f.size(), op ); + } + } + op->setState( StDone ); + f.close(); + emit finished( op ); +} + +/*! + \reimp +*/ + +void TQLocalFs::operationPut( TQNetworkOperation *op ) +{ +#ifdef TQLOCALFS_DEBUG + qDebug( "TQLocalFs: operationPut" ); +#endif + op->setState( StInProgress ); + TQString to = TQUrl( op->arg( 0 ) ).path(); + + TQFile f( to ); + if ( !f.open( IO_WriteOnly ) ) { + TQString msg = tr( "Could not write\n%1" ).arg( to ); + op->setState( StFailed ); + op->setProtocolDetail( msg ); + op->setErrorCode( (int)ErrPut ); + emit finished( op ); + return; + } + + TQByteArray ba( op->rawArg( 1 ) ); + emit dataTransferProgress( 0, ba.size(), op ); + int blockSize = calcBlockSize( ba.size() ); + if ( (int)ba.size() < blockSize ) { + f.writeBlock( ba.data(), ba.size() ); + emit dataTransferProgress( ba.size(), ba.size(), op ); + } else { + int i = 0; + while ( i + blockSize < (int)ba.size() - 1 ) { + if ( operationInProgress() != op ) + return; + f.writeBlock( &ba.data()[ i ], blockSize ); + f.flush(); + emit dataTransferProgress( i + blockSize, ba.size(), op ); + i += blockSize; + TQGuardedPtr that = this; + qApp->processEvents(); + if (!that) + return; + } + if ( i < (int)ba.size() - 1 ) + f.writeBlock( &ba.data()[ i ], ba.size() - i ); + emit dataTransferProgress( ba.size(), ba.size(), op ); + } + op->setState( StDone ); + f.close(); + emit finished( op ); +} + +/*! + \reimp +*/ + +int TQLocalFs::supportedOperations() const +{ + return OpListChildren | OpMkDir | OpRemove | OpRename | OpGet | OpPut; +} + +/*! + \internal +*/ + +int TQLocalFs::calcBlockSize( int totalSize ) const +{ + if ( totalSize == 0 ) + return 1024; + int s = totalSize / 100; + // we want a block size between 1KB and 1MB + if ( s < 1024 ) + s = 1024; + if ( s > 1048576 ) + s = 1048576; + return s; +} + +#endif // QT_NO_NETWORKPROTOCOL diff --git a/src/kernel/qlocalfs.h b/src/kernel/qlocalfs.h new file mode 100644 index 000000000..88001d6e7 --- /dev/null +++ b/src/kernel/qlocalfs.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Definition of TQLocalFs class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQLOCALFS_H +#define TQLOCALFS_H + +#ifndef QT_H +#include "qnetworkprotocol.h" +#include "qdir.h" +#endif // QT_H + +#ifndef QT_NO_NETWORKPROTOCOL + +class Q_EXPORT TQLocalFs : public TQNetworkProtocol +{ + Q_OBJECT + +public: + TQLocalFs(); + virtual int supportedOperations() const; + +protected: + virtual void operationListChildren( TQNetworkOperation *op ); + virtual void operationMkDir( TQNetworkOperation *op ); + virtual void operationRemove( TQNetworkOperation *op ); + virtual void operationRename( TQNetworkOperation *op ); + virtual void operationGet( TQNetworkOperation *op ); + virtual void operationPut( TQNetworkOperation *op ); + +private: + int calcBlockSize( int totalSize ) const; + TQDir dir; + +}; + +#endif // QT_NO_NETWORKPROTOCOL + +#endif // TQLOCALFS_H diff --git a/src/kernel/qlock.cpp b/src/kernel/qlock.cpp new file mode 100644 index 000000000..d12c692f6 --- /dev/null +++ b/src/kernel/qlock.cpp @@ -0,0 +1,297 @@ +/**************************************************************************** +** +** Definition of TQLock class. This manages interprocess locking +** +** Created : 20000406 +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** Licensees holding valid TQt Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qlock_p.h" + +#ifndef QT_NO_QWS_MULTIPROCESS + +#include +#include +#if defined(Q_OS_MACX) +#define Q_NO_SEMAPHORE +#include +#include +#else +#include +#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) \ + || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_NETBSD) || defined(Q_OS_BSDI) +/* union semun is defined by including */ +#else +/* according to X/OPEN we have to define it ourselves */ +union semun { + int val; /* value for SETVAL */ + struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ + unsigned short *array; /* array for GETALL, SETALL */ +}; +#endif +#endif +#include +#include +#include + +#define MAX_LOCKS 200 // maximum simultaneous read locks + +class TQLockData +{ +public: +#ifdef Q_NO_SEMAPHORE + TQCString file; +#endif + int id; + int count; + bool owned; +}; + +#endif + +/*! + \class TQLock qlock_p.h + \brief The TQLock class is a wrapper for a System V shared semaphore. + + \ingroup qws + \ingroup io + + \internal + + It is used by TQt/Embedded for synchronizing access to the graphics + card and shared memory region between processes. +*/ + +/*! + \enum TQLock::Type + + \value Read + \value Write +*/ + +/*! + \fn TQLock::TQLock( const TQString &filename, char id, bool create ) + + Creates a lock. \a filename is the file path of the Unix-domain + socket the TQt/Embedded client is using. \a id is the name of the + particular lock to be created on that socket. If \a create is TRUE + the lock is to be created (as the TQt/Embedded server does); if \a + create is FALSE the lock should exist already (as the TQt/Embedded + client expects). +*/ + +TQLock::TQLock( const TQString &filename, char id, bool create ) +{ +#ifndef QT_NO_QWS_MULTIPROCESS + data = new TQLockData; + data->count = 0; +#ifdef Q_NO_SEMAPHORE + data->file = TQString(filename+id).local8Bit(); + for(int x = 0; x < 2; x++) { + data->id = open(data->file, O_RDWR | (x ? O_CREAT : 0), S_IRWXU); + if(data->id != -1 || !create) { + data->owned = x; + break; + } + } +#else + key_t semkey = ftok(filename, id); + data->id = semget(semkey,0,0); + data->owned = create; + if ( create ) { + semun arg; arg.val = 0; + if ( data->id != -1 ) + semctl(data->id,0,IPC_RMID,arg); + data->id = semget(semkey,1,IPC_CREAT|0600); + arg.val = MAX_LOCKS; + semctl(data->id,0,SETVAL,arg); + } +#endif + if ( data->id == -1 ) { + qWarning( "Cannot %s semaphore %s \'%c\'", + create ? "create" : "get", filename.latin1(), id ); + qDebug("Error %d %s\n",errno,strerror(errno)); + } +#endif +} + +/*! + \fn TQLock::~TQLock() + + Destroys a lock +*/ + +TQLock::~TQLock() +{ +#ifndef QT_NO_QWS_MULTIPROCESS + if ( locked() ) + unlock(); +#ifdef Q_NO_SEMAPHORE + if(isValid()) { + close(data->id); + if( data->owned ) + unlink( data->file ); + } +#else + if(data->owned) { + semun arg; arg.val = 0; + semctl( data->id, 0, IPC_RMID, arg ); + } +#endif + delete data; +#endif +} + +/*! + \fn bool TQLock::isValid() const + + Returns TRUE if the lock constructor was succesful; returns FALSE if + the lock could not be created or was not available to connect to. +*/ + +bool TQLock::isValid() const +{ +#ifndef QT_NO_QWS_MULTIPROCESS + return (data->id != -1); +#else + return TRUE; +#endif +} + +/*! + Locks the semaphore with a lock of type \a t. Locks can either be + \c Read or \c Write. If a lock is \c Read, attempts by other + processes to obtain \c Read locks will succeed, and \c Write + attempts will block until the lock is unlocked. If locked as \c + Write, all attempts to lock by other processes will block until + the lock is unlocked. Locks are stacked: i.e. a given TQLock can be + locked multiple times by the same process without blocking, and + will only be unlocked after a corresponding number of unlock() + calls. +*/ + +void TQLock::lock( Type t ) +{ +#ifndef QT_NO_QWS_MULTIPROCESS + if ( !data->count ) { +#ifdef Q_NO_SEMAPHORE + int op = LOCK_SH; + if(t == Write) + op = LOCK_EX; + for( int rv=1; rv; ) { + rv = flock(data->id, op); + if (rv == -1 && errno != EINTR) + qDebug("Semop lock failure %s",strerror(errno)); + } +#else + sembuf sops; + sops.sem_num = 0; + sops.sem_flg = SEM_UNDO; + + if ( t == Write ) { + sops.sem_op = -MAX_LOCKS; + type = Write; + } else { + sops.sem_op = -1; + type = Read; + } + + int rv; + do { + rv = semop(data->id,&sops,1); + if (rv == -1 && errno != EINTR) + qDebug("Semop lock failure %s",strerror(errno)); + } while ( rv == -1 && errno == EINTR ); +#endif + } + data->count++; +#endif +} + +/*! + \fn void TQLock::unlock() + + Unlocks the semaphore. If other processes were blocking waiting to + lock() the semaphore, one of them will wake up and succeed in + lock()ing. +*/ + +void TQLock::unlock() +{ +#ifndef QT_NO_QWS_MULTIPROCESS + if( data->count ) { + data->count--; + if( !data->count ) { +#ifdef Q_NO_SEMAPHORE + for( int rv=1; rv; ) { + rv = flock(data->id, LOCK_UN); + if (rv == -1 && errno != EINTR) + qDebug("Semop lock failure %s",strerror(errno)); + } +#else + sembuf sops; + sops.sem_num = 0; + sops.sem_op = 1; + sops.sem_flg = SEM_UNDO; + if ( type == Write ) + sops.sem_op = MAX_LOCKS; + + int rv; + do { + rv = semop(data->id,&sops,1); + if (rv == -1 && errno != EINTR) + qDebug("Semop unlock failure %s",strerror(errno)); + } while ( rv == -1 && errno == EINTR ); +#endif + } + } else { + qDebug("Unlock without corresponding lock"); + } +#endif +} + +/*! + \fn bool TQLock::locked() const + + Returns TRUE if the lock is currently held by the current process; + otherwise returns FALSE. +*/ + +bool TQLock::locked() const +{ +#ifndef QT_NO_QWS_MULTIPROCESS + return (data->count > 0); +#else + return FALSE; +#endif +} diff --git a/src/kernel/qlock_p.h b/src/kernel/qlock_p.h new file mode 100644 index 000000000..2d46f7dc0 --- /dev/null +++ b/src/kernel/qlock_p.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Definition of TQLock class. This manages interprocess locking +** +** Created : 20000406 +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** Licensees holding valid TQt Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQLOCK_P_H +#define TQLOCK_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. This header file may +// change from version to version without notice, or even be +// removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qstring.h" +#endif // QT_H + +class TQLockData; + +class TQLock +{ +public: + TQLock( const TQString &filename, char id, bool create = FALSE ); + ~TQLock(); + + enum Type { Read, Write }; + + bool isValid() const; + void lock( Type type ); + void unlock(); + bool locked() const; + +private: + Type type; + TQLockData *data; +}; + + +// Nice class for ensuring the lock is released. +// Just create one on the stack and the lock is automatically released +// when TQLockHolder is destructed. +class TQLockHolder +{ +public: + TQLockHolder( TQLock *l, TQLock::Type type ) : qlock(l) { + qlock->lock( type ); + } + ~TQLockHolder() { if ( locked() ) qlock->unlock(); } + + void lock( TQLock::Type type ) { qlock->lock( type ); } + void unlock() { qlock->unlock(); } + bool locked() const { return qlock->locked(); } + +private: + TQLock *qlock; +}; + +#endif + diff --git a/src/kernel/qmetaobject.cpp b/src/kernel/qmetaobject.cpp new file mode 100644 index 000000000..706d5779e --- /dev/null +++ b/src/kernel/qmetaobject.cpp @@ -0,0 +1,1251 @@ +/**************************************************************************** +** +** Implementation of TQMetaObject class +** +** Created : 930419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qmetaobject.h" +#include "qasciidict.h" + +#ifdef QT_THREAD_SUPPORT +#include +#endif // QT_THREAD_SUPPORT + +/*! + \class TQMetaData qmetaobject.h + \reentrant + + \brief The TQMetaData class provides information about a member function that is known to the meta object system. + + \internal + + The struct consists of three members, \e name, \e method and \e access: + + \code + const char *name; // - member name + const TQUMethod* method; // - detailed method description + enum Access { Private, Protected, Public }; + Access access; // - access permission + \endcode + */ + +/*! + \class TQClassInfo qmetaobject.h + + \brief The TQClassInfo class provides a struct that stores some basic information about a single class. + + \internal + + The class information is a simple \e name - \e value pair: + + \code + const char* name; + const char* value; + \endcode + + */ + + +/*! + \class TQMetaObject qmetaobject.h + \brief The TQMetaObject class contains meta information about TQt objects. + + \ingroup objectmodel + + The Meta Object System in TQt is responsible for the signals and + slots inter-object communication mechanism, runtime type + information and the property system. All meta information in TQt is + kept in a single instance of TQMetaObject per class. + + This class is not normally retquired for application programming. + But if you write meta applications, such as scripting engines or + GUI builders, you might find these functions useful: + \list + \i className() to get the name of a class. + \i superClassName() to get the name of the superclass. + \i inherits(), the function called by TQObject::inherits(). + \i superClass() to access the superclass's meta object. + \i numSlots(), numSignals(), slotNames(), and signalNames() to get + information about a class's signals and slots. + \i property() and propertyNames() to obtain information about a + class's properties. + \endlist + + Classes may have a list of name-value pairs of class information. + The number of pairs is returned by numClassInfo(), and values are + returned by classInfo(). + + \sa \link moc.html moc (Meta Object Compiler)\endlink + +*/ + + +/***************************************************************************** + The private object. + *****************************************************************************/ + +// extra flags from moc.y +enum Flags { + Invalid = 0x00000000, + Readable = 0x00000001, + Writable = 0x00000002, + EnumOrSet = 0x00000004, + UnresolvedEnum = 0x00000008, + StdSet = 0x00000100, + Override = 0x00000200, + NotDesignable = 0x00001000, + DesignableOverride = 0x00002000, + NotScriptable = 0x00004000, + ScriptableOverride = 0x00008000, + NotStored = 0x00010000, + StoredOverride = 0x00020000 +}; + +static TQAsciiDict *qt_metaobjects = 0; +static int qt_metaobjects_count = 0; + +class TQMetaObjectPrivate +{ +public: + TQMetaObjectPrivate() : +#ifndef QT_NO_PROPERTIES + enumData(0), numEnumData(0), + propData(0),numPropData(0), + qt_static_property(0), +#endif + classInfo(0), numClassInfo(0) {} +#ifndef QT_NO_PROPERTIES + const TQMetaEnum *enumData; + int numEnumData; + const TQMetaProperty *propData; + int numPropData; + bool (*qt_static_property)(TQObject*, int, int, TQVariant*); +#endif + const TQClassInfo *classInfo; + int numClassInfo; +}; + + +/***************************************************************************** + Internal dictionary for fast access to class members + *****************************************************************************/ + +#if defined(Q_CANNOT_DELETE_CONSTANT) +typedef TQMetaData TQConstMetaData; +#else +typedef const TQMetaData TQConstMetaData; +#endif + +class Q_EXPORT TQMemberDict : public TQAsciiDict +{ +public: + TQMemberDict( int size = 17, bool cs = TRUE, bool ck = TRUE ) : + TQAsciiDict(size,cs,ck) {} + TQMemberDict( const TQMemberDict &dict ) : TQAsciiDict(dict) {} + ~TQMemberDict() { clear(); } + TQMemberDict &operator=(const TQMemberDict &dict) + { return (TQMemberDict&)TQAsciiDict::operator=(dict); } +}; + + +/* + Calculate optimal dictionary size for n entries using prime numbers, + and assuming there are no more than 40 entries. +*/ + +static int optDictSize( int n ) +{ + if ( n < 6 ) + n = 5; + else if ( n < 10 ) + n = 11; + else if ( n < 14 ) + n = 17; + else + n = 23; + return n; +} + + +/***************************************************************************** + TQMetaObject member functions + *****************************************************************************/ + +/*!\internal + */ +TQMetaObject::TQMetaObject( const char *const class_name, TQMetaObject *super_class, + const TQMetaData *const slot_data, int n_slots, + const TQMetaData *const signal_data, int n_signals, +#ifndef QT_NO_PROPERTIES + const TQMetaProperty *const prop_data, int n_props, + const TQMetaEnum *const enum_data, int n_enums, +#endif + const TQClassInfo *const class_info, int n_info ) +{ + classname = class_name; // set meta data + superclass = super_class; + superclassname = superclass ? superclass->className() : 0; + slotDict = init( slotData = slot_data, n_slots ); + signalDict = init( signalData = signal_data, n_signals ); + + d = new TQMetaObjectPrivate; + reserved = 0; + +#ifndef QT_NO_PROPERTIES + d->propData = prop_data; + d->numPropData = n_props; + d->enumData = enum_data; + d->numEnumData = n_enums; +#endif + d->classInfo = class_info; + d->numClassInfo = n_info; + + signaloffset = superclass ? ( superclass->signalOffset() + superclass->numSignals() ) : 0; + slotoffset = superclass ? ( superclass->slotOffset() + superclass->numSlots() ) : 0; +#ifndef QT_NO_PROPERTIES + propertyoffset = superclass ? ( superclass->propertyOffset() + superclass->numProperties() ) : 0; +#endif +} + +#ifndef QT_NO_PROPERTIES +/*!\internal + */ +TQMetaObject::TQMetaObject( const char *const class_name, TQMetaObject *super_class, + const TQMetaData *const slot_data, int n_slots, + const TQMetaData *const signal_data, int n_signals, + const TQMetaProperty *const prop_data, int n_props, + const TQMetaEnum *const enum_data, int n_enums, + bool (*qt_static_property)(TQObject*, int, int, TQVariant*), + const TQClassInfo *const class_info, int n_info ) +{ + classname = class_name; // set meta data + superclass = super_class; + superclassname = superclass ? superclass->className() : 0; + slotDict = init( slotData = slot_data, n_slots ); + signalDict = init( signalData = signal_data, n_signals ); + + d = new TQMetaObjectPrivate; + reserved = 0; + + d->propData = prop_data; + d->numPropData = n_props; + d->enumData = enum_data; + d->numEnumData = n_enums; + d->qt_static_property = qt_static_property; + d->classInfo = class_info; + d->numClassInfo = n_info; + + signaloffset = superclass ? ( superclass->signalOffset() + superclass->numSignals() ) : 0; + slotoffset = superclass ? ( superclass->slotOffset() + superclass->numSlots() ) : 0; + propertyoffset = superclass ? ( superclass->propertyOffset() + superclass->numProperties() ) : 0; +} +#endif + +/*!\internal + */ +TQMetaObject::~TQMetaObject() +{ + delete slotDict; // delete dicts + delete signalDict; + delete d; +#ifdef QT_THREAD_SUPPORT + TQMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + if ( qt_metaobjects ) { + qt_metaobjects->remove( classname ); + if ( qt_metaobjects->isEmpty() ) { + delete qt_metaobjects; + qt_metaobjects = 0; + } + } + + // delete reserved; // Unused void* +} + + +/*! + \fn const char *TQMetaObject::className() const + + Returns the class name. + + \sa TQObject::className(), superClassName() +*/ + +/*! + \fn const char *TQMetaObject::superClassName() const + + Returns the class name of the superclass or 0 if there is no + superclass in the TQObject hierachy. + + \sa className() +*/ + +/*! + \fn TQMetaObject *TQMetaObject::superClass() const + + Returns the meta object of the super class or 0 if there is no + such object. +*/ + +/*! + Returns the number of slots for this class. + + If \a super is TRUE, inherited slots are included. + + \sa slotNames() +*/ +int TQMetaObject::numSlots( bool super ) const // number of slots +{ + int n = slotDict ? slotDict->count() : 0; + if ( !super || !superclass ) + return n; + return n + superclass->numSlots( super ); +} + +/*! + Returns the number of signals for this class. + + If \a super is TRUE, inherited signals are included. + + \sa signalNames() +*/ +int TQMetaObject::numSignals( bool super ) const // number of signals +{ + int n = signalDict ? signalDict->count() : 0; + if ( !super || !superclass ) + return n; + return n + superclass->numSignals( super ); +} + + +/*! \internal + + Returns the meta data of the slot with the name \a n or 0 if no + such slot exists. + + If \a super is TRUE, inherited slots are included. + */ +const TQMetaData* TQMetaObject::slot( int index, bool super ) const +{ + int idx = index - ( super ? slotOffset() : 0 ); + if ( slotDict && idx >= 0 && idx < (int) slotDict->count() ) { + return slotData + idx; + } + if ( !super || !superclass ) + return 0; + return superclass->slot( index, super ); +} + +/*! \internal + + Returns the meta data of the signal with the name \a n or 0 if no + such signal exists. + + If \a super is TRUE, inherited signals are included. + */ +const TQMetaData* TQMetaObject::signal( int index, bool super ) const +{ + int idx = index - ( super ? signalOffset() : 0 ); + if ( signalDict && idx >= 0 && idx < (int) signalDict->count() ) { + return signalData + idx; + } + if ( !super || !superclass ) + return 0; + return superclass->signal( index, super ); +} + + +/*! + \fn int TQMetaObject::signalOffset() const + + \internal + + Returns the signal offset for this metaobject. + +*/ + +/*! + \fn int TQMetaObject::propertyOffset() const + + \internal + + Returns the property offset for this metaobject. + +*/ + +/*! \internal + Returns the index of the signal with name \n or -1 if no such signal exists. + + If \a super is TRUE, inherited signals are included. +*/ +int TQMetaObject::findSignal( const char* n, bool super ) const +{ + const TQMetaObject *mo = this; + int offset = -1; + + do { + const TQMetaData *md = mo->signalDict ? mo->signalDict->find( n ) : 0; + if ( md ) { +#if defined(QT_CHECK_RANGE) + if ( offset != -1 ) { + qWarning( "TQMetaObject::findSignal:%s: Conflict with %s::%s", + className(), mo->className(), n ); + return offset; + } +#endif + offset = mo->signalOffset() + ( md - mo->signalData ); +#if !defined(QT_CHECK_RANGE) + return offset; +#endif + } + } while ( super && (mo = mo->superclass) ); + + return offset; +} + +/*! + \fn int TQMetaObject::slotOffset() const + + \internal + + Returns the slot offset for this metaobject. + +*/ + +/*! \internal + Returns the index of the slot with name \n or -1 if no such slot exists. + + If \a super is TRUE, inherited slots are included. + */ +int TQMetaObject::findSlot( const char* n, bool super ) const +{ + const TQMetaData *md = slotDict ? slotDict->find( n ) : 0; + if ( md ) + return slotOffset() + ( md - slotData ); + if ( !super || !superclass) + return -1; + return superclass->findSlot( n, super ); +} + +/*!\internal + */ +TQMetaObject *TQMetaObject::new_metaobject( const char *classname, + TQMetaObject *superclassobject, + const TQMetaData * const slot_data, int n_slots, + const TQMetaData * const signal_data, int n_signals, +#ifndef QT_NO_PROPERTIES + const TQMetaProperty * const prop_data, int n_props, + const TQMetaEnum * const enum_data, int n_enums, +#endif + const TQClassInfo * const class_info, int n_info ) +{ + return new TQMetaObject( classname, superclassobject, slot_data, n_slots, + signal_data, n_signals, +#ifndef QT_NO_PROPERTIES + prop_data, n_props, + enum_data, n_enums, +#endif + class_info, n_info ); +} + +#ifndef QT_NO_PROPERTIES +/*!\internal + */ +TQMetaObject *TQMetaObject::new_metaobject( const char *classname, + TQMetaObject *superclassobject, + const TQMetaData * const slot_data, int n_slots, + const TQMetaData * const signal_data, int n_signals, + const TQMetaProperty * const prop_data, int n_props, + const TQMetaEnum * const enum_data, int n_enums, + bool (*qt_static_property)(TQObject*, int, int, TQVariant*), + const TQClassInfo * const class_info, int n_info ) +{ + return new TQMetaObject( classname, superclassobject, slot_data, n_slots, + signal_data, n_signals, + prop_data, n_props, + enum_data, n_enums, + qt_static_property, + class_info, n_info ); +} +#endif + +/*!\internal + */ +TQMemberDict *TQMetaObject::init( const TQMetaData * data, int n ) +{ + if ( n == 0 ) // nothing, then make no dict + return 0; + TQMemberDict *dict = new TQMemberDict( optDictSize(n), TRUE, FALSE ); + Q_CHECK_PTR( dict ); + while ( n-- ) { // put all members into dict + dict->insert( data->name, data ); + data++; + } + return dict; +} + +/*! + Returns the number of items of class information available for + this class. + + If \a super is TRUE, inherited class information is included. +*/ +int TQMetaObject::numClassInfo( bool super ) const +{ + return d->numClassInfo + ((super && superclass)?superclass->numClassInfo(super):0); +} + +/*! + Returns the class information with index \a index or 0 if no such + information exists. + + If \a super is TRUE, inherited class information is included. +*/ +const TQClassInfo* TQMetaObject::classInfo( int index, bool super ) const +{ + if ( index < 0 ) + return 0; + if ( index < d->numClassInfo ) + return &(d->classInfo[ index ]); + if ( !super || !superclass ) + return 0; + return superclass->classInfo( index - d->numClassInfo, super ); +} + +/*! + \overload + Returns the class information with name \a name or 0 if no such + information exists. + + If \a super is TRUE, inherited class information is included. +*/ +const char* TQMetaObject::classInfo( const char* name, bool super ) const +{ + for( int i = 0; i < d->numClassInfo; ++i ) { + if ( qstrcmp( d->classInfo[i].name, name ) == 0 ) + return d->classInfo[i].value; + } + if ( !super || !superclass ) + return 0; + return superclass->classInfo( name, super ); +} + +#ifndef QT_NO_PROPERTIES + +/*! + Returns the number of properties for this class. + + If \a super is TRUE, inherited properties are included. + + \sa propertyNames() + */ +int TQMetaObject::numProperties( bool super ) const // number of signals +{ + int n = d->numPropData; + if ( !super || !superclass ) + return n; + return n + superclass->numProperties( super ); +} + +/*! + Returns the property meta data for the property at index \a index + or 0 if no such property exists. + + If \a super is TRUE, inherited properties are included. + + \sa propertyNames() + */ +const TQMetaProperty* TQMetaObject::property( int index, bool super ) const +{ + int idx = index - ( super ? propertyOffset() : 0 ); + if ( d->propData && idx >= 0 && idx < (int)d->numPropData ) + return d->propData + idx; + if ( !super || !superclass ) + return 0; + return superclass->property( index, super ); +} + + +/*! + Returns the index for the property with name \a name or -1 if no + such property exists. + + If \a super is TRUE, inherited properties are included. + + \sa property(), propertyNames() +*/ + +int TQMetaObject::findProperty( const char *name, bool super ) const +{ + for( int i = 0; i < d->numPropData; ++i ) { + if ( d->propData[i].isValid() && qstrcmp( d->propData[i].name(), name ) == 0 ) { + return ( super ? propertyOffset() : 0 ) + i; + } + } + if ( !super || !superclass ) + return -1; + return superclass->findProperty( name, super ); +} + +/*! \internal + + Returns the index for the property \a prop + or -1 if the property can not be found. + + If \a super is TRUE, inherited properties are included. + + \sa property(), propertyNames() +*/ + +int TQMetaObject::indexOfProperty( const TQMetaProperty* prop, bool super ) const +{ + if ( *prop->meta == this ) + return ( super ? propertyOffset() : 0 ) + ( prop - d->propData); + if ( !super || !superclass ) + return -1; + return superclass->indexOfProperty( prop, super ); +} + +/*!\internal + + Returns the parent property of property \a p or 0, if the property + cannot be resolved. + + \a p has to be contained in this meta object +*/ + +const TQMetaProperty* TQMetaObject::resolveProperty( const TQMetaProperty* p ) const +{ + if ( !superclass ) + return 0; + return superclass->property( superclass->findProperty( p->n, TRUE ), TRUE ); +} + +/*!\internal + + \overload + + The version of resolveProperty that is used by moc generated code +*/ + +int TQMetaObject::resolveProperty( int index ) const +{ + if ( !superclass ) + return -1; + const TQMetaProperty* p = d->propData + ( index - propertyOffset() ); + return superclass->findProperty( p->n, TRUE ); +} + + +/*! + Returns a list with the names of all this class's properties. + + If \a super is TRUE, inherited properties are included. + + \sa property() +*/ +TQStrList TQMetaObject::propertyNames( bool super ) const +{ + TQStrList l( FALSE ); + + if ( superclass && super ) { + TQStrList sl = superclass->propertyNames( super ); + for ( TQStrListIterator slit( sl ); slit.current(); ++slit ) + l.append( slit.current() ); + } + + for( int i = 0; i < d->numPropData; ++i ) { + if ( d->propData[i].isValid() ) + l.append( d->propData[i].name() ); + } + + return l; +} + +/*! + Returns a list with the names of all this class's signals. + + If \a super is TRUE, inherited signals are included. +*/ +TQStrList TQMetaObject::signalNames( bool super ) const +{ + TQStrList l( FALSE ); + int n = numSignals( super ); + for( int i = 0; i < n; ++i ) { + l.append( signal(i, super)->name ); + } + return l; +} + +/*! + Returns a list with the names of all this class's slots. + + If \a super is TRUE, inherited slots are included. + + \sa numSlots() +*/ +TQStrList TQMetaObject::slotNames( bool super ) const +{ + TQStrList l( FALSE ); + int n = numSlots( super ); + for( int i = 0; i < n; ++i ) + l.append( slot( i, super)->name ); + return l; +} + +/*!\internal + + */ + +int TQMetaObject::numEnumerators( bool super ) const +{ + int n = 0; + if ( superclass && super ) + n += superclass->numEnumerators( super ); + return n + d->numEnumData; +} + +/*!\internal + + */ +TQStrList TQMetaObject::enumeratorNames( bool super ) const +{ + TQStrList l( FALSE ); + + if ( superclass && super ) { + TQStrList sl = superclass->enumeratorNames( super ); + for ( TQStrListIterator slit( sl ); slit.current(); ++slit ) + l.append( slit.current() ); + } + + for( int i = 0; i < d->numEnumData; ++i ) { + if ( d->enumData[i].items ) + l.append( d->enumData[i].name ); + } + + return l; +} + +/*!\internal + */ +const TQMetaEnum* TQMetaObject::enumerator( const char* name, bool super ) const +{ + for( int i = 0; i < d->numEnumData; ++i ) + if ( qstrcmp( d->enumData[i].name, name ) == 0 ) + return &(d->enumData[i]); + if ( !super || !superclass ) + return 0; + return superclass->enumerator( name, super ); +} + +#endif // QT_NO_PROPERTIES + + +/*! + Returns TRUE if this class inherits \a clname within the meta + object inheritance chain; otherwise returns FALSE. + + (A class is considered to inherit itself.) +*/ +bool TQMetaObject::inherits( const char* clname ) const +{ + const TQMetaObject *meta = this; + while ( meta ) { + if ( qstrcmp(clname, meta->className()) == 0 ) + return TRUE; + meta = meta->superclass; + } + return FALSE; +} + +/*! \internal */ + +TQMetaObject *TQMetaObject::metaObject( const char *class_name ) +{ + if ( !qt_metaobjects ) + return 0; +#ifdef QT_THREAD_SUPPORT + TQMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + TQtStaticMetaObjectFunction func = (TQtStaticMetaObjectFunction)qt_metaobjects->find( class_name ); + if ( func ) + return func(); + return 0; +} + +/*! \internal */ +bool TQMetaObject::hasMetaObject( const char *class_name ) +{ + if ( !qt_metaobjects ) + return FALSE; +#ifdef QT_THREAD_SUPPORT + TQMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + return !!qt_metaobjects->find( class_name ); +} + +#ifndef QT_NO_PROPERTIES +/*! \internal + +### this functions will go away. It exists purely for the sake of meta +### object code generated with TQt 3.1.0 +*/ +bool TQMetaObject::qt_static_property( TQObject* o, int id, int f, TQVariant* v) +{ + if ( d->qt_static_property ) + return d->qt_static_property( o, id, f, v ); + else if ( o ) // compatibility + return o->qt_property( id, f, v ); + else if ( superclass ) + return superclass->qt_static_property( o, id, f, v ); + switch ( f ) { + case 3: case 4: case 5: + return TRUE; + default: + return FALSE; + } +} + + +/*! + \class TQMetaProperty qmetaobject.h + + \brief The TQMetaProperty class stores meta data about a property. + + \ingroup objectmodel + + Property meta data includes type(), name(), and whether a property + is writable(), designable() and stored(). + + The functions isSetType(), isEnumType() and enumKeys() provide + further information about a property's type. The conversion + functions keyToValue(), valueToKey(), keysToValue() and + valueToKeys() allow conversion between the integer representation + of an enumeration or set value and its literal representation. + + Actual property values are set and received through TQObject's set + and get functions. See TQObject::setProperty() and + TQObject::property() for details. + + You receive meta property data through an object's meta object. + See TQMetaObject::property() and TQMetaObject::propertyNames() for + details. +*/ + +/*! + Returns the possible enumeration keys if this property is an + enumeration type (or a set type). + + \sa isEnumType() +*/ +TQStrList TQMetaProperty::enumKeys() const +{ + TQStrList l( FALSE ); + const TQMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return l; + if ( ed != 0 ) { + for( uint i = 0; i < ed->count; ++i ) { + uint j = 0; + while ( j < i && + ed->items[j].value != ed->items[i].value ) + ++j; + if ( i == j ) + l.append( ed->items[i].key ); + } + } + return l; +} + +/*! + Converts the enumeration key \a key to its integer value. + + For set types, use keysToValue(). + + \sa valueToKey(), isSetType(), keysToValue() +*/ +int TQMetaProperty::keyToValue( const char* key ) const +{ + const TQMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return -1; + for ( uint i = 0; i < ed->count; ++i ) { + if ( !qstrcmp( key, ed->items[i].key) ) + return ed->items[i].value; + } + return -1; +} + +/*! + Converts the enumeration value \a value to its literal key. + + For set types, use valueToKeys(). + + \sa valueToKey(), isSetType(), valueToKeys() +*/ +const char* TQMetaProperty::valueToKey( int value ) const +{ + const TQMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return 0; + for ( uint i = 0; i < ed->count; ++i ) { + if ( value == ed->items[i].value ) + return ed->items[i].key ; + } + return 0; +} + +/*! + Converts the list of keys \a keys to their combined (OR-ed) + integer value. + + \sa isSetType(), valueToKey(), keysToValue() +*/ +int TQMetaProperty::keysToValue( const TQStrList& keys ) const +{ + const TQMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return -1; + int value = 0; + for ( TQStrListIterator it( keys ); it.current(); ++it ) { + uint i; + for( i = ed->count; i > 0; --i ) { + if ( !qstrcmp( it.current(), ed->items[i-1].key) ) { + value |= ed->items[i-1].value; + break; + } + } + if ( i == 0 ) + value |= -1; + } + return value; +} + +/*! + Converts the set value \a value to a list of keys. + + \sa isSetType(), valueToKey(), valueToKeys() +*/ +TQStrList TQMetaProperty::valueToKeys( int value ) const +{ + TQStrList keys; + const TQMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + if ( !ed ) + return keys; + + int v = value; + for( uint i = ed->count; i > 0; --i ) { + int k = ed->items[i-1].value; + if ( ( k != 0 && (v & k) == k ) || ( k == value) ) { + v = v & ~k; + keys.append( ed->items[i-1].key ); + } + } + return keys; +} + +bool TQMetaProperty::writable() const +{ + if ( !testFlags( Override ) || testFlags( Writable ) ) + return testFlags( Writable ); + const TQMetaObject* mo = (*meta); + const TQMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->writable() : FALSE; +} + +/*!\internal + */ +bool TQMetaProperty::stdSet() const +{ + if ( !testFlags( Override ) || testFlags( Writable ) ) + return testFlags( StdSet ); + const TQMetaObject* mo = (*meta); + const TQMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->stdSet() : FALSE; +} + +/*!\internal + */ +int TQMetaProperty::id() const +{ + return _id < 0 ? (*meta)->indexOfProperty( this, TRUE ) : _id; +} + +/*! \internal +*/ +void TQMetaProperty::clear() +{ + t = n = 0; + meta = 0; + enumData = 0; + _id = -1; + flags = 0; +} + +bool TQMetaProperty::isValid() const +{ + if ( testFlags( UnresolvedEnum ) ) { + if ( !enumData && (!meta || !(*meta)->enumerator( t, TRUE ) ) ) + return FALSE; + } + if ( !testFlags( Override ) || testFlags( Readable ) ) + return testFlags( Readable ); + const TQMetaObject* mo = (*meta); + const TQMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->isValid() : FALSE; +} + +bool TQMetaProperty::isSetType() const +{ + const TQMetaEnum* ed = enumData; + if ( !enumData && meta ) + ed = (*meta)->enumerator( t, TRUE ); + return ( ed != 0 && ed->set ); +} + +bool TQMetaProperty::isEnumType() const +{ + return testFlags( EnumOrSet ); +} + + + +/*! + \fn const char* TQMetaProperty::type() const + + Returns the type of the property. +*/ + +/*! + \fn const char* TQMetaProperty::name() const + + Returns the name of the property. +*/ + +/*! + \fn bool TQMetaProperty::writable() const + + Returns TRUE if the property is writable; otherwise returns FALSE. + +*/ + +/*! \fn bool TQMetaProperty::isValid() const + + \internal + + Returns whether the property is valid. +*/ + +/*! + \fn bool TQMetaProperty::isEnumType() const + + Returns TRUE if the property's type is an enumeration value; + otherwise returns FALSE. + + \sa isSetType(), enumKeys() +*/ + +/*! + \fn bool TQMetaProperty::isSetType() const + + Returns TRUE if the property's type is an enumeration value that + is used as set, i.e. if the enumeration values can be OR-ed + together; otherwise returns FALSE. A set type is implicitly also + an enum type. + + \sa isEnumType(), enumKeys() +*/ + + +/*! Returns TRUE if the property is designable for object \a o; + otherwise returns FALSE. + + If no object \a o is given, the function returns a static + approximation. + */ +bool TQMetaProperty::designable( TQObject* o ) const +{ + if ( !isValid() || !writable() ) + return FALSE; + if ( o ) { + int idx = _id >= 0 ? _id : (*meta)->indexOfProperty( this, TRUE ); + return idx >= 0 && o->qt_property( idx, 3, 0 ); + } + if ( testFlags( DesignableOverride ) ) { + const TQMetaObject* mo = (*meta); + const TQMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->designable() : FALSE; + } + return !testFlags( NotDesignable ); +} + +/*! + Returns TRUE if the property is scriptable for object \a o; + otherwise returns FALSE. + + If no object \a o is given, the function returns a static + approximation. + */ +bool TQMetaProperty::scriptable( TQObject* o ) const +{ + if ( o ) { + int idx = _id >= 0 ? _id : (*meta)->indexOfProperty( this, TRUE ); + return idx >= 0 && o->qt_property( idx, 4, 0 ); + } + if ( testFlags( ScriptableOverride ) ) { + const TQMetaObject* mo = (*meta); + const TQMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->scriptable() : FALSE; + } + return !testFlags( NotScriptable ); +} + +/*! + Returns TRUE if the property shall be stored for object \a o; + otherwise returns FALSE. + + If no object \a o is given, the function returns a static + approximation. + */ +bool TQMetaProperty::stored( TQObject* o ) const +{ + if ( !isValid() || !writable() ) + return FALSE; + if ( o ) { + int idx = _id >= 0 ? _id : (*meta)->indexOfProperty( this, TRUE ); + return idx >= 0 && o->qt_property( idx, 5, 0 ); + } + if ( testFlags( StoredOverride ) ) { + const TQMetaObject* mo = (*meta); + const TQMetaProperty* parent = mo->resolveProperty( this ); + return parent ? parent->stored() : FALSE; + } + return !testFlags( NotStored ); +} + + +/*! + Tries to reset the property for object \a o with a reset method. + On success, returns TRUE; otherwise returns FALSE. + + Reset methods are optional, usually only a few properties support + them. +*/ +bool TQMetaProperty::reset( TQObject* o ) const +{ + if ( !o ) + return FALSE; + int idx = _id >= 0 ? _id : (*meta)->indexOfProperty( this, TRUE ); + if ( idx < 0 ) + return 0; + return o->qt_property( idx, 2, 0 ); +} + + +/*! \enum TQMetaProperty::Flags + + \internal +*/ + +#endif // QT_NO_PROPERTIES + +/* + * TQMetaObjectCleanUp is used as static global object in the moc-generated cpp + * files and deletes the TQMetaObject provided with setMetaObject. It sets the + * TQObject reference to the metaObj to NULL when it is destroyed. + */ +TQMetaObjectCleanUp::TQMetaObjectCleanUp( const char *mo_name, TQtStaticMetaObjectFunction func ) + : metaObject( 0 ) +{ +#ifdef QT_THREAD_SUPPORT + TQMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + if ( !qt_metaobjects ) + qt_metaobjects = new TQAsciiDict( 257 ); + qt_metaobjects->insert( mo_name, (void*)func ); + + qt_metaobjects_count++; +} + +TQMetaObjectCleanUp::TQMetaObjectCleanUp() + : metaObject( 0 ) +{ +} + +/*! \fn bool TQMetaProperty::testFlags( uint f ) const + \internal +*/ + +TQMetaObjectCleanUp::~TQMetaObjectCleanUp() +{ +#ifdef QT_THREAD_SUPPORT + TQMutexLocker( qt_global_mutexpool ? + qt_global_mutexpool->get( &qt_metaobjects ) : 0 ); +#endif // QT_THREAD_SUPPORT + if ( !--qt_metaobjects_count ) { + delete qt_metaobjects; + qt_metaobjects = 0; + } + if ( metaObject ) { + delete *metaObject; + *metaObject = 0; + metaObject = 0; + } +} + +void TQMetaObjectCleanUp::setMetaObject( TQMetaObject *&mo ) +{ +#if defined(QT_CHECK_RANGE) + if ( metaObject ) + qWarning( "TQMetaObjectCleanUp::setMetaObject: Double use of TQMetaObjectCleanUp!" ); +#endif + metaObject = &mo; +} diff --git a/src/kernel/qmetaobject.h b/src/kernel/qmetaobject.h new file mode 100644 index 000000000..0f0616892 --- /dev/null +++ b/src/kernel/qmetaobject.h @@ -0,0 +1,286 @@ +/**************************************************************************** +** +** Definition of TQMetaObject class +** +** Created : 930419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQMETAOBJECT_H +#define TQMETAOBJECT_H + +#ifndef QT_H +#include "qconnection.h" +#include "qstrlist.h" +#endif // QT_H + +#ifndef Q_MOC_OUTPUT_REVISION +#define Q_MOC_OUTPUT_REVISION 26 +#endif + +class TQObject; +struct TQUMethod; +class TQMetaObjectPrivate; + +struct TQMetaData // - member function meta data +{ // for signal and slots + const char *name; // - member name + const TQUMethod* method; // - detailed method description + enum Access { Private, Protected, Public }; + Access access; // - access permission +}; + +#ifndef QT_NO_PROPERTIES +struct TQMetaEnum // enumerator meta data +{ // for properties + const char *name; // - enumerator name + uint count; // - number of values + struct Item // - a name/value pair + { + const char *key; + int value; + }; + const Item *items; // - the name/value pairs + bool set; // whether enum has to be treated as a set +}; +#endif + +#ifndef QT_NO_PROPERTIES + +class Q_EXPORT TQMetaProperty // property meta data +{ +public: + const char* type() const { return t; } // type of the property + const char* name() const { return n; } // name of the property + + bool writable() const; + bool isValid() const; + + bool isSetType() const; + bool isEnumType() const; + TQStrList enumKeys() const; // enumeration names + + int keyToValue( const char* key ) const; // enum and set conversion functions + const char* valueToKey( int value ) const; + int keysToValue( const TQStrList& keys ) const; + TQStrList valueToKeys( int value ) const; + + bool designable( TQObject* = 0 ) const; + bool scriptable( TQObject* = 0 ) const; + bool stored( TQObject* = 0 ) const; + + bool reset( TQObject* ) const; + + const char* t; // internal + const char* n; // internal + + enum Flags { + Invalid = 0x00000000, + Readable = 0x00000001, + Writable = 0x00000002, + EnumOrSet = 0x00000004, + UnresolvedEnum = 0x00000008, + StdSet = 0x00000100, + Override = 0x00000200 + }; + + uint flags; // internal + bool testFlags( uint f ) const; // internal + bool stdSet() const; // internal + int id() const; // internal + + TQMetaObject** meta; // internal + + const TQMetaEnum* enumData; // internal + int _id; // internal + void clear(); // internal +}; + +inline bool TQMetaProperty::testFlags( uint f ) const +{ return (flags & (uint)f) != (uint)0; } + +#endif // QT_NO_PROPERTIES + +struct TQClassInfo // class info meta data +{ + const char* name; // - name of the info + const char* value; // - value of the info +}; + +class Q_EXPORT TQMetaObject // meta object class +{ +public: + TQMetaObject( const char * const class_name, TQMetaObject *superclass, + const TQMetaData * const slot_data, int n_slots, + const TQMetaData * const signal_data, int n_signals, +#ifndef QT_NO_PROPERTIES + const TQMetaProperty *const prop_data, int n_props, + const TQMetaEnum *const enum_data, int n_enums, +#endif + const TQClassInfo *const class_info, int n_info ); + +#ifndef QT_NO_PROPERTIES + TQMetaObject( const char * const class_name, TQMetaObject *superclass, + const TQMetaData * const slot_data, int n_slots, + const TQMetaData * const signal_data, int n_signals, + const TQMetaProperty *const prop_data, int n_props, + const TQMetaEnum *const enum_data, int n_enums, + bool (*qt_static_property)(TQObject*, int, int, TQVariant*), + const TQClassInfo *const class_info, int n_info ); +#endif + + + virtual ~TQMetaObject(); + + const char *className() const { return classname; } + const char *superClassName() const { return superclassname; } + + TQMetaObject *superClass() const { return superclass; } + + bool inherits( const char* clname ) const; + + int numSlots( bool super = FALSE ) const; + int numSignals( bool super = FALSE ) const; + + int findSlot( const char *, bool super = FALSE ) const; + int findSignal( const char *, bool super = FALSE ) const; + + const TQMetaData *slot( int index, bool super = FALSE ) const; + const TQMetaData *signal( int index, bool super = FALSE ) const; + + TQStrList slotNames( bool super = FALSE ) const; + TQStrList signalNames( bool super = FALSE ) const; + + int slotOffset() const; + int signalOffset() const; + int propertyOffset() const; + + int numClassInfo( bool super = FALSE ) const; + const TQClassInfo *classInfo( int index, bool super = FALSE ) const; + const char *classInfo( const char* name, bool super = FALSE ) const; + +#ifndef QT_NO_PROPERTIES + const TQMetaProperty *property( int index, bool super = FALSE ) const; + int findProperty( const char *name, bool super = FALSE ) const; + int indexOfProperty( const TQMetaProperty*, bool super = FALSE ) const; + const TQMetaProperty* resolveProperty( const TQMetaProperty* ) const; + int resolveProperty( int ) const; + TQStrList propertyNames( bool super = FALSE ) const; + int numProperties( bool super = FALSE ) const; +#endif + + // static wrappers around constructors, necessary to work around a + // Windows-DLL limitation: objects can only be deleted within a + // DLL if they were actually created within that DLL. + static TQMetaObject *new_metaobject( const char *, TQMetaObject *, + const TQMetaData *const, int, + const TQMetaData *const, int, +#ifndef QT_NO_PROPERTIES + const TQMetaProperty *const prop_data, int n_props, + const TQMetaEnum *const enum_data, int n_enums, +#endif + const TQClassInfo *const class_info, int n_info ); +#ifndef QT_NO_PROPERTIES + static TQMetaObject *new_metaobject( const char *, TQMetaObject *, + const TQMetaData *const, int, + const TQMetaData *const, int, + const TQMetaProperty *const prop_data, int n_props, + const TQMetaEnum *const enum_data, int n_enums, + bool (*qt_static_property)(TQObject*, int, int, TQVariant*), + const TQClassInfo *const class_info, int n_info ); + TQStrList enumeratorNames( bool super = FALSE ) const; + int numEnumerators( bool super = FALSE ) const; + const TQMetaEnum *enumerator( const char* name, bool super = FALSE ) const; +#endif + + static TQMetaObject *metaObject( const char *class_name ); + static bool hasMetaObject( const char *class_name ); + +private: + TQMemberDict *init( const TQMetaData *, int ); + + const char *classname; // class name + const char *superclassname; // super class name + TQMetaObject *superclass; // super class meta object + TQMetaObjectPrivate *d; // private data for... + void *reserved; // ...binary compatibility + const TQMetaData *slotData; // slot meta data + TQMemberDict *slotDict; // slot dictionary + const TQMetaData *signalData; // signal meta data + TQMemberDict *signalDict; // signal dictionary + int signaloffset; + int slotoffset; +#ifndef QT_NO_PROPERTIES + int propertyoffset; +public: + bool qt_static_property( TQObject* o, int id, int f, TQVariant* v); +private: + friend class TQMetaProperty; +#endif + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQMetaObject( const TQMetaObject & ); + TQMetaObject &operator=( const TQMetaObject & ); +#endif +}; + +inline int TQMetaObject::slotOffset() const +{ return slotoffset; } + +inline int TQMetaObject::signalOffset() const +{ return signaloffset; } + +#ifndef QT_NO_PROPERTIES +inline int TQMetaObject::propertyOffset() const +{ return propertyoffset; } +#endif + +typedef TQMetaObject *(*TQtStaticMetaObjectFunction)(); + +class Q_EXPORT TQMetaObjectCleanUp +{ +public: + TQMetaObjectCleanUp( const char *mo_name, TQtStaticMetaObjectFunction ); + TQMetaObjectCleanUp(); + ~TQMetaObjectCleanUp(); + + void setMetaObject( TQMetaObject *&mo ); + +private: + TQMetaObject **metaObject; +}; + +#endif // TQMETAOBJECT_H diff --git a/src/kernel/qmime.cpp b/src/kernel/qmime.cpp new file mode 100644 index 000000000..0944bf7f6 --- /dev/null +++ b/src/kernel/qmime.cpp @@ -0,0 +1,618 @@ +/**************************************************************************** +** +** Implementation of MIME support +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qmime.h" + +#ifndef QT_NO_MIME + +#include "qmap.h" +#include "qstringlist.h" +#include "qfileinfo.h" +#include "qdir.h" +#include "qdragobject.h" +#include "qcleanuphandler.h" +#include "qapplication.h" // ### for now +#include "qclipboard.h" // ### for now + +/*! + \class TQMimeSource qmime.h + \brief The TQMimeSource class is an abstraction of objects which provide formatted data of a certain MIME type. + + \ingroup io + \ingroup draganddrop + \ingroup misc + + \link dnd.html Drag-and-drop\endlink and + \link TQClipboard clipboard\endlink use this abstraction. + + \sa \link http://www.isi.edu/in-notes/iana/assignments/media-types/ + IANA list of MIME media types\endlink +*/ + +static int qt_mime_serial_number = 0; +static TQMimeSourceFactory* defaultfactory = 0; +static TQSingleCleanupHandler qmime_cleanup_factory; + +/*! + Constructs a mime source and assigns a globally unique serial + number to it. + + \sa serialNumber() +*/ + +TQMimeSource::TQMimeSource() +{ + ser_no = qt_mime_serial_number++; + cacheType = NoCache; +} + +/*! + \fn int TQMimeSource::serialNumber() const + + Returns the mime source's globally unique serial number. +*/ + + +void TQMimeSource::clearCache() +{ + if ( cacheType == Text ) { + delete cache.txt.str; + delete cache.txt.subtype; + cache.txt.str = 0; + cache.txt.subtype = 0; + } else if ( cacheType == Graphics ) { + delete cache.gfx.img; + delete cache.gfx.pix; + cache.gfx.img = 0; + cache.gfx.pix = 0; + } + cacheType = NoCache; +} + +/*! + Provided to ensure that subclasses destroy themselves correctly. +*/ +TQMimeSource::~TQMimeSource() +{ +#ifndef QT_NO_CLIPBOARD + extern void qt_clipboard_cleanup_mime_source(TQMimeSource *); + qt_clipboard_cleanup_mime_source(this); +#endif + clearCache(); +} + +/*! + \fn TQByteArray TQMimeSource::encodedData(const char*) const + + Returns the encoded data of this object in the specified MIME + format. + + Subclasses must reimplement this function. +*/ + + + +/*! + Returns TRUE if the object can provide the data in format \a + mimeType; otherwise returns FALSE. + + If you inherit from TQMimeSource, for consistency reasons it is + better to implement the more abstract canDecode() functions such + as TQTextDrag::canDecode() and TQImageDrag::canDecode(). +*/ +bool TQMimeSource::provides(const char* mimeType) const +{ + const char* fmt; + for (int i=0; (fmt = format(i)); i++) { + if ( !qstricmp(mimeType,fmt) ) + return TRUE; + } + return FALSE; +} + + +/*! + \fn const char * TQMimeSource::format(int i) const + + Returns the \a{i}-th supported MIME format, or 0. +*/ + + + +class TQMimeSourceFactoryData { +public: + TQMimeSourceFactoryData() : + last(0) + { + } + + ~TQMimeSourceFactoryData() + { + TQMap::Iterator it = stored.begin(); + while ( it != stored.end() ) { + delete *it; + ++it; + } + delete last; + } + + TQMap stored; + TQMap extensions; + TQStringList path; + TQMimeSource* last; + TQPtrList factories; +}; + + +/*! + \class TQMimeSourceFactory qmime.h + \brief The TQMimeSourceFactory class is an extensible provider of mime-typed data. + + \ingroup io + \ingroup environment + + A TQMimeSourceFactory provides an abstract interface to a + collection of information. Each piece of information is + represented by a TQMimeSource object which can be examined and + converted to concrete data types by functions such as + TQImageDrag::canDecode() and TQImageDrag::decode(). + + The base TQMimeSourceFactory can be used in two ways: as an + abstraction of a collection of files or as specifically stored + data. For it to access files, call setFilePath() before accessing + data. For stored data, call setData() for each item (there are + also convenience functions, e.g. setText(), setImage() and + setPixmap(), that simply call setData() with appropriate + parameters). + + The rich text widgets, TQTextEdit and TQTextBrowser, use + TQMimeSourceFactory to resolve references such as images or links + within rich text documents. They either access the default factory + (see \l{defaultFactory()}) or their own (see + \l{TQTextEdit::setMimeSourceFactory()}). Other classes that are + capable of displaying rich text (such as TQLabel, TQWhatsThis or + TQMessageBox) always use the default factory. + + A factory can also be used as a container to store data associated + with a name. This technique is useful whenever rich text contains + images that are stored in the program itself, not loaded from the + hard disk. Your program may, for example, define some image data + as: + \code + static const char* myimage_data[]={ + "...", + ... + "..."}; + \endcode + + To be able to use this image within some rich text, for example + inside a TQLabel, you must create a TQImage from the raw data and + insert it into the factory with a unique name: + \code + TQMimeSourceFactory::defaultFactory()->setImage( "myimage", TQImage(myimage_data) ); + \endcode + + Now you can create a rich text TQLabel with + + \code + TQLabel* label = new TQLabel( + "Rich text with embedded image:" + "Isn't that cute?" ); + \endcode + + When no longer needed, you can clear the data from the factory: + + \code + delete label; + TQMimeSourceFactory::defaultFactory()->setData( "myimage", 0 ); + \endcode +*/ + + +/*! + Constructs a TQMimeSourceFactory that has no file path and no + stored content. +*/ +TQMimeSourceFactory::TQMimeSourceFactory() : + d(new TQMimeSourceFactoryData) +{ + // add some reasonable defaults + setExtensionType("htm", "text/html;charset=iso8859-1"); + setExtensionType("html", "text/html;charset=iso8859-1"); + setExtensionType("txt", "text/plain"); + setExtensionType("xml", "text/xml;charset=UTF-8"); + setExtensionType("jpg", "image/jpeg"); // support misspelled jpeg files +} + +/*! + Destroys the TQMimeSourceFactory, deleting all stored content. +*/ +TQMimeSourceFactory::~TQMimeSourceFactory() +{ + if ( defaultFactory() == this ) + defaultfactory = 0; + delete d; +} + +TQMimeSource* TQMimeSourceFactory::dataInternal(const TQString& abs_name, const TQMap &extensions ) const +{ + TQMimeSource* r = 0; + TQFileInfo fi(abs_name); + if ( fi.isReadable() ) { + + // get the right mimetype + TQString e = fi.extension(FALSE); + TQCString mimetype = "application/octet-stream"; + const char* imgfmt; + if ( extensions.contains(e) ) + mimetype = extensions[e].latin1(); + else if ( ( imgfmt = TQImage::imageFormat( abs_name ) ) ) + mimetype = TQCString("image/")+TQCString(imgfmt).lower(); + + TQFile f(abs_name); + if ( f.open(IO_ReadOnly) && f.size() ) { + TQByteArray ba(f.size()); + f.readBlock(ba.data(), ba.size()); + TQStoredDrag* sr = new TQStoredDrag( mimetype ); + sr->setEncodedData( ba ); + delete d->last; + d->last = r = sr; + } + } + + // we didn't find the mime-source, so ask the default factory for + // the mime-source (this one will iterate over all installed ones) + // + // this looks dangerous, as this dataInternal() function will be + // called again when the default factory loops over all installed + // factories (including this), but the static bool looping in + // data() avoids endless recursions + if ( !r && this != defaultFactory() ) + r = (TQMimeSource*)defaultFactory()->data( abs_name ); + + return r; +} + + +/*! + Returns a reference to the data associated with \a abs_name. The + return value remains valid only until the next data() or setData() + call, so you should immediately decode the result. + + If there is no data associated with \a abs_name in the factory's + store, the factory tries to access the local filesystem. If \a + abs_name isn't an absolute file name, the factory will search for + it in all defined paths (see \l{setFilePath()}). + + The factory understands all the image formats supported by + TQImageIO. Any other mime types are determined by the file name + extension. The default settings are + \code + setExtensionType("html", "text/html;charset=iso8859-1"); + setExtensionType("htm", "text/html;charset=iso8859-1"); + setExtensionType("txt", "text/plain"); + setExtensionType("xml", "text/xml;charset=UTF-8"); + \endcode + The effect of these is that file names ending in "txt" will be + treated as text encoded in the local encoding; those ending in + "xml" will be treated as text encoded in Unicode UTF-8 encoding. + The text/html type is treated specially, since the encoding can be + specified in the html file itself. "html" or "htm" will be treated + as text encoded in the encoding specified by the html meta tag, if + none could be found, the charset of the mime type will be used. + The text subtype ("html", "plain", or "xml") does not affect the + factory, but users of the factory may behave differently. We + recommend creating "xml" files where practical. These files can be + viewed regardless of the runtime encoding and can encode any + Unicode characters without resorting to encoding definitions + inside the file. + + Any file data that is not recognized will be retrieved as a + TQMimeSource providing the "application/octet-stream" mime type, + meaning uninterpreted binary data. + + You can add further extensions or change existing ones with + subsequent calls to setExtensionType(). If the extension mechanism + is not sufficient for your problem domain, you can inherit + TQMimeSourceFactory and reimplement this function to perform some + more specialized mime-type detection. The same applies if you want + to use the mime source factory to access URL referenced data over + a network. +*/ +const TQMimeSource* TQMimeSourceFactory::data(const TQString& abs_name) const +{ + if ( d->stored.contains(abs_name) ) + return d->stored[abs_name]; + + TQMimeSource* r = 0; + TQStringList::Iterator it; + if ( abs_name[0] == '/' +#ifdef Q_WS_WIN + || ( abs_name[0] && abs_name[1] == ':' ) || abs_name.startsWith("\\\\") +#endif + ) + { + // handle absolute file names directly + r = dataInternal( abs_name, d->extensions); + } + else { // check list of paths + for ( it = d->path.begin(); !r && it != d->path.end(); ++it ) { + TQString filename = *it; + if ( filename[(int)filename.length()-1] != '/' ) + filename += '/'; + filename += abs_name; + r = dataInternal( filename, d->extensions ); + } + } + + static bool looping = FALSE; + if ( !r && this == defaultFactory() ) { + // we found no mime-source and we are the default factory, so + // we know all the other installed mime-source factories, so + // ask them + if ( !looping ) { + // to avoid endless recustions, don't enter the loop below + // if data() got called from within the loop below + looping = TRUE; + TQPtrListIterator it( d->factories ); + TQMimeSourceFactory *f; + while ( ( f = it.current() ) ) { + ++it; + if ( f == this ) + continue; + r = (TQMimeSource*)f->data( abs_name ); + if ( r ) { + looping = FALSE; + return r; + } + } + looping = FALSE; + } + } else if ( !r ) { + // we are not the default mime-source factory, so ask the + // default one for the mime-source, as this one will loop over + // all installed mime-source factories and ask these + r = (TQMimeSource*)defaultFactory()->data( abs_name ); + } + + return r; +} + +/*! + Sets the list of directories that will be searched when named data + is requested to the those given in the string list \a path. + + \sa filePath() +*/ +void TQMimeSourceFactory::setFilePath( const TQStringList& path ) +{ + d->path = path; +} + +/*! + Returns the currently set search paths. +*/ +TQStringList TQMimeSourceFactory::filePath() const +{ + return d->path; +} + +/*! + Adds another search path, \a p to the existing search paths. + + \sa setFilePath() +*/ +void TQMimeSourceFactory::addFilePath( const TQString& p ) +{ + d->path += p; +} + +/*! + Sets the mime-type to be associated with the file name extension, + \a ext to \a mimetype. This determines the mime-type for files + found via the paths set by setFilePath(). +*/ +void TQMimeSourceFactory::setExtensionType( const TQString& ext, const char* mimetype ) +{ + d->extensions.replace(ext, mimetype); +} + +/*! + Converts the absolute or relative data item name \a + abs_or_rel_name to an absolute name, interpreted within the + context (path) of the data item named \a context (this must be an + absolute name). +*/ +TQString TQMimeSourceFactory::makeAbsolute(const TQString& abs_or_rel_name, const TQString& context) const +{ + if ( context.isNull() || + !(context[0] == '/' +#ifdef Q_WS_WIN + || ( context[0] && context[1] == ':') +#endif + )) + return abs_or_rel_name; + if ( abs_or_rel_name.isEmpty() ) + return context; + TQFileInfo c( context ); + if (!c.isDir()) { + TQFileInfo r( c.dir(TRUE), abs_or_rel_name ); + return r.absFilePath(); + } else { + TQDir d(context); + TQFileInfo r(d, abs_or_rel_name); + return r.absFilePath(); + } +} + +/*! + \overload + A convenience function. See data(const TQString& abs_name). The + file name is given in \a abs_or_rel_name and the path is in \a + context. +*/ +const TQMimeSource* TQMimeSourceFactory::data(const TQString& abs_or_rel_name, const TQString& context) const +{ + const TQMimeSource* r = data(makeAbsolute(abs_or_rel_name,context)); + if ( !r && !d->path.isEmpty() ) + r = data(abs_or_rel_name); + return r; +} + + +/*! + Sets \a text to be the data item associated with the absolute name + \a abs_name. + + Equivalent to setData(abs_name, new TQTextDrag(text)). +*/ +void TQMimeSourceFactory::setText( const TQString& abs_name, const TQString& text ) +{ + setData(abs_name, new TQTextDrag(text)); +} + +/*! + Sets \a image to be the data item associated with the absolute + name \a abs_name. + + Equivalent to setData(abs_name, new TQImageDrag(image)). +*/ +void TQMimeSourceFactory::setImage( const TQString& abs_name, const TQImage& image ) +{ + setData(abs_name, new TQImageDrag(image)); +} + +/*! + Sets \a pixmap to be the data item associated with the absolute + name \a abs_name. +*/ +void TQMimeSourceFactory::setPixmap( const TQString& abs_name, const TQPixmap& pixmap ) +{ + setData(abs_name, new TQImageDrag(pixmap.convertToImage())); +} + +/*! + Sets \a data to be the data item associated with + the absolute name \a abs_name. Note that the ownership of \a data is + transferred to the factory: do not delete or access the pointer after + passing it to this function. + + Passing 0 for data removes previously stored data. +*/ +void TQMimeSourceFactory::setData( const TQString& abs_name, TQMimeSource* data ) +{ + if ( d->stored.contains(abs_name) ) + delete d->stored[abs_name]; + d->stored.replace(abs_name,data); +} + + +/*! + Returns the application-wide default mime source factory. This + factory is used by rich text rendering classes such as + TQSimpleRichText, TQWhatsThis and TQMessageBox to resolve named + references within rich text documents. It serves also as the + initial factory for the more complex render widgets, TQTextEdit and + TQTextBrowser. + + \sa setDefaultFactory() +*/ +TQMimeSourceFactory* TQMimeSourceFactory::defaultFactory() +{ + if (!defaultfactory) + { + defaultfactory = new TQMimeSourceFactory(); + qmime_cleanup_factory.set( &defaultfactory ); + } + return defaultfactory; +} + +/*! + Sets the default \a factory, destroying any previously set mime + source provider. The ownership of the factory is transferred to + TQt. + + \sa defaultFactory() +*/ +void TQMimeSourceFactory::setDefaultFactory( TQMimeSourceFactory* factory) +{ + if ( !defaultfactory ) + qmime_cleanup_factory.set( &defaultfactory ); + else if ( defaultfactory != factory ) + delete defaultfactory; + defaultfactory = factory; +} + +/*! + Sets the defaultFactory() to 0 and returns the previous one. +*/ + +TQMimeSourceFactory* TQMimeSourceFactory::takeDefaultFactory() +{ + TQMimeSourceFactory *f = defaultfactory; + defaultfactory = 0; + return f; +} + +/*! + Adds the TQMimeSourceFactory \a f to the list of available + mimesource factories. If the defaultFactory() can't resolve a + data() it iterates over the list of installed mimesource factories + until the data can be resolved. + + \sa removeFactory(); +*/ + +void TQMimeSourceFactory::addFactory( TQMimeSourceFactory *f ) +{ + TQMimeSourceFactory::defaultFactory()->d->factories.append( f ); +} + +/*! + Removes the mimesource factory \a f from the list of available + mimesource factories. + + \sa addFactory(); +*/ + +void TQMimeSourceFactory::removeFactory( TQMimeSourceFactory *f ) +{ + TQMimeSourceFactory::defaultFactory()->d->factories.removeRef( f ); +} + +#endif // QT_NO_MIME diff --git a/src/kernel/qmime.h b/src/kernel/qmime.h new file mode 100644 index 000000000..9e14d516c --- /dev/null +++ b/src/kernel/qmime.h @@ -0,0 +1,200 @@ +/**************************************************************************** +** +** Definition of mime classes +** +** Created : 981204 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQMIME_H +#define TQMIME_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qmap.h" +#endif // QT_H + +#ifndef QT_NO_MIME + +class TQImageDrag; +class TQTextDrag; + +class Q_EXPORT TQMimeSource +{ + friend class TQClipboardData; + +public: + TQMimeSource(); + virtual ~TQMimeSource(); + virtual const char* format( int n = 0 ) const = 0; + virtual bool provides( const char* ) const; + virtual TQByteArray encodedData( const char* ) const = 0; + int serialNumber() const; + +private: + int ser_no; + enum { NoCache, Text, Graphics } cacheType; + union + { + struct + { + TQString *str; + TQCString *subtype; + } txt; + struct + { + TQImage *img; + TQPixmap *pix; + } gfx; + } cache; + void clearCache(); + + // friends for caching + friend class TQImageDrag; + friend class TQTextDrag; + +}; + +inline int TQMimeSource::serialNumber() const +{ return ser_no; } + +class TQStringList; +class TQMimeSourceFactoryData; + +class Q_EXPORT TQMimeSourceFactory { +public: + TQMimeSourceFactory(); + virtual ~TQMimeSourceFactory(); + + static TQMimeSourceFactory* defaultFactory(); + static void setDefaultFactory( TQMimeSourceFactory* ); + static TQMimeSourceFactory* takeDefaultFactory(); + static void addFactory( TQMimeSourceFactory *f ); + static void removeFactory( TQMimeSourceFactory *f ); + + virtual const TQMimeSource* data(const TQString& abs_name) const; + virtual TQString makeAbsolute(const TQString& abs_or_rel_name, const TQString& context) const; + const TQMimeSource* data(const TQString& abs_or_rel_name, const TQString& context) const; + + virtual void setText( const TQString& abs_name, const TQString& text ); + virtual void setImage( const TQString& abs_name, const TQImage& im ); + virtual void setPixmap( const TQString& abs_name, const TQPixmap& pm ); + virtual void setData( const TQString& abs_name, TQMimeSource* data ); + virtual void setFilePath( const TQStringList& ); + virtual TQStringList filePath() const; + void addFilePath( const TQString& ); + virtual void setExtensionType( const TQString& ext, const char* mimetype ); + +private: + TQMimeSource *dataInternal(const TQString& abs_name, const TQMap &extensions ) const; + TQMimeSourceFactoryData* d; +}; + +#if defined(Q_WS_WIN) + +#ifndef QT_H +#include "qptrlist.h" // down here for GCC 2.7.* compatibility +#endif // QT_H + +/* + Encapsulation of conversion between MIME and Windows CLIPFORMAT. + Not need on X11, as the underlying protocol uses the MIME standard + directly. +*/ + +class Q_EXPORT TQWindowsMime { +public: + TQWindowsMime(); + virtual ~TQWindowsMime(); + + static void initialize(); + + static TQPtrList all(); + static TQWindowsMime* convertor( const char* mime, int cf ); + static const char* cfToMime(int cf); + + static int registerMimeType(const char *mime); + + virtual const char* convertorName()=0; + virtual int countCf()=0; + virtual int cf(int index)=0; + virtual bool canConvert( const char* mime, int cf )=0; + virtual const char* mimeFor(int cf)=0; + virtual int cfFor(const char* )=0; + virtual TQByteArray convertToMime( TQByteArray data, const char* mime, int cf )=0; + virtual TQByteArray convertFromMime( TQByteArray data, const char* mime, int cf )=0; +}; + +#endif +#if defined(Q_WS_MAC) + +#ifndef QT_H +#include "qptrlist.h" // down here for GCC 2.7.* compatibility +#endif // QT_H + +/* + Encapsulation of conversion between MIME and Mac flavor. + Not need on X11, as the underlying protocol uses the MIME standard + directly. +*/ + +class Q_EXPORT TQMacMime { + char type; +public: + enum TQMacMimeType { MIME_DND=0x01, MIME_CLIP=0x02, MIME_QT_CONVERTOR=0x04, MIME_ALL=MIME_DND|MIME_CLIP }; + TQMacMime(char); + virtual ~TQMacMime(); + + static void initialize(); + + static TQPtrList all(TQMacMimeType); + static TQMacMime* convertor(TQMacMimeType, const char* mime, int flav); + static const char* flavorToMime(TQMacMimeType, int flav); + + virtual const char* convertorName()=0; + virtual int countFlavors()=0; + virtual int flavor(int index)=0; + virtual bool canConvert(const char* mime, int flav)=0; + virtual const char* mimeFor(int flav)=0; + virtual int flavorFor(const char*)=0; + virtual TQByteArray convertToMime(TQValueList data, const char* mime, int flav)=0; + virtual TQValueList convertFromMime(TQByteArray data, const char* mime, int flav)=0; +}; + +#endif // Q_WS_MAC + +#endif // QT_NO_MIME + +#endif // TQMIME_H diff --git a/src/kernel/qmngio.cpp b/src/kernel/qmngio.cpp new file mode 100644 index 000000000..b4539b760 --- /dev/null +++ b/src/kernel/qmngio.cpp @@ -0,0 +1,464 @@ +/**************************************************************************** +** +** Implementation of MNG TQImage IOHandler +** +** Created : 970521 +** +** Copyright (C) 1997-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef QT_CLEAN_NAMESPACE +#define QT_CLEAN_NAMESPACE +#endif + +#include "qdatetime.h" + +#ifndef QT_NO_IMAGEIO_MNG + +#include "qimage.h" +#include "qasyncimageio.h" +#include "qiodevice.h" +#include "qmngio.h" + +// Define XMD_H prohibits the included headers of libmng.h to typedef INT32. +// This is needed for Borland with STL support, since in that case, INT32 is +// already defined by some Borland header. +#define XMD_H +#if defined(Q_OS_UNIXWARE) +# define HAVE_BOOLEAN // libjpeg under Unixware seems to need this +#endif +#include +#include + + +#ifndef QT_NO_ASYNC_IMAGE_IO + +class TQMNGFormat : public TQImageFormat { +public: + TQMNGFormat(); + virtual ~TQMNGFormat(); + + int decode(TQImage& img, TQImageConsumer* consumer, + const uchar* buffer, int length); + + bool openstream() + { + // ### We should figure out how many loops an MNG has, but for now always assume infinite. + if (consumer) + consumer->setLooping(0); + return TRUE; + } + bool closestream( ) + { + if (consumer) + consumer->end(); + return TRUE; + } + bool readdata( mng_ptr pBuf, mng_uint32 iBuflen, mng_uint32p pRead ) + { + uint m = ndata + nbuffer - ubuffer; + if ( iBuflen > m ) { + iBuflen = m; + } + *pRead = iBuflen; + uint n = nbuffer-ubuffer; + if ( iBuflen < n ) { + // enough in buffer + memcpy(pBuf, buffer+ubuffer, iBuflen); + ubuffer += iBuflen; + return TRUE; + } + if ( n ) { + // consume buffer + memcpy(pBuf, buffer+ubuffer, n ); + pBuf = (mng_ptr)((char*)pBuf + n); + iBuflen -= n; + ubuffer = nbuffer; + } + if ( iBuflen ) { + // fill from incoming data + memcpy(pBuf, data, iBuflen); + data += iBuflen; + ndata -= iBuflen; + } + return TRUE; + } + bool errorproc( mng_int32 iErrorcode, + mng_int8 /*iSeverity*/, + mng_chunkid iChunkname, + mng_uint32 /*iChunkseq*/, + mng_int32 iExtra1, + mng_int32 iExtra2, + mng_pchar zErrortext ) + { + qWarning("MNG error %d: %s; chunk %c%c%c%c; subcode %d:%d", + iErrorcode, zErrortext ? zErrortext : "", + (iChunkname>>24)&0xff, + (iChunkname>>16)&0xff, + (iChunkname>>8)&0xff, + (iChunkname>>0)&0xff, + iExtra1,iExtra2); + return TRUE; + } + bool processheader( mng_uint32 iWidth, mng_uint32 iHeight ) + { + image->create(iWidth,iHeight,32); + image->setAlphaBuffer(TRUE); + memset(image->bits(),0,iWidth*iHeight*4); + consumer->setSize(iWidth,iHeight); + mng_set_canvasstyle(handle, + TQImage::systemByteOrder() == TQImage::LittleEndian + ? MNG_CANVAS_BGRA8 : MNG_CANVAS_ARGB8 ); + return TRUE; + } + mng_ptr getcanvasline( mng_uint32 iLinenr ) + { + return image->scanLine(iLinenr); + } + mng_bool refresh( mng_uint32 x, mng_uint32 y, mng_uint32 w, mng_uint32 h ) + { + TQRect r(x,y,w,h); + consumer->changed(r); + consumer->setFramePeriod(0); + consumer->frameDone(); + return TRUE; + } + mng_uint32 gettickcount( ) + { + return timer.elapsed() - losttime; + } + bool settimer( mng_uint32 iMsecs ) + { + consumer->setFramePeriod(iMsecs); + consumer->frameDone(); + state = Time; + losingtimer.start(); + losttime -= iMsecs; + return TRUE; + } + +private: + // Animation-level information + enum { MovieStart, Time, Data, Data2 } state; + + // Image-level information + mng_handle handle; + + // For storing unused data + uchar *buffer; + uint maxbuffer; + uint nbuffer; + + // Timing + TQTime timer; + TQTime losingtimer; + int losttime; + + void enlargeBuffer(uint n) + { + if ( n > maxbuffer ) { + maxbuffer = n; + buffer = (uchar*)realloc(buffer,n); + } + } + + // Temporary locals during single data-chunk processing + const uchar* data; + uint ndata; + uint ubuffer; + TQImageConsumer* consumer; + TQImage* image; +}; + +class TQMNGFormatType : public TQImageFormatType +{ + TQImageFormat* decoderFor(const uchar* buffer, int length); + const char* formatName() const; +}; + + +/* + \class TQMNGFormat qmngio.h + \brief Incremental image decoder for MNG image format. + + \ingroup images + \ingroup graphics + + This subclass of TQImageFormat decodes MNG format images, + including animated MNGs. + + Animated MNG images are standard MNG images. The MNG standard + defines two extension chunks that are useful for animations: + +
+
gIFg - GIF-like Graphic Control Extension +
Includes frame disposal, user input flag (we ignore this), + and inter-frame delay. +
gIFx - GIF-like Application Extension +
Multi-purpose, but we just use the Netscape extension + which specifies looping. +
+ + The subimages usually contain a offset chunk (oFFs) but need not. + + The first image defines the "screen" size. Any subsequent image that + doesn't fit is clipped. + +TODO: decide on this point. gIFg gives disposal types, so it can be done. + All images paste (\e not composite, just place all-channel copying) + over the previous image to produce a subsequent frame. +*/ + +/* + \class TQMNGFormatType qasyncimageio.h + \brief Incremental image decoder for MNG image format. + + \ingroup images + \ingroup graphics + \ingroup io + + This subclass of TQImageFormatType recognizes MNG + format images, creating a TQMNGFormat when retquired. An instance + of this class is created automatically before any other factories, + so you should have no need for such objects. +*/ + +TQImageFormat* TQMNGFormatType::decoderFor( const uchar* buffer, int length ) +{ + if (length < 8) return 0; + + if (buffer[0]==138 // MNG signature + && buffer[1]=='M' + && buffer[2]=='N' + && buffer[3]=='G' + && buffer[4]==13 + && buffer[5]==10 + && buffer[6]==26 + && buffer[7]==10 + || buffer[0]==139 // JNG signature + && buffer[1]=='J' + && buffer[2]=='N' + && buffer[3]=='G' + && buffer[4]==13 + && buffer[5]==10 + && buffer[6]==26 + && buffer[7]==10 +#ifdef QT_NO_IMAGEIO_PNG // if we don't have native PNG support use libmng + || buffer[0]==137 // PNG signature + && buffer[1]=='P' + && buffer[2]=='N' + && buffer[3]=='G' + && buffer[4]==13 + && buffer[5]==10 + && buffer[6]==26 + && buffer[7]==10 +#endif + ) + return new TQMNGFormat; + return 0; +} + +const char* TQMNGFormatType::formatName() const +{ + return "MNG"; +} + + +/*! + Constructs a TQMNGFormat. +*/ +TQMNGFormat::TQMNGFormat() +{ + state = MovieStart; + handle = 0; + nbuffer = 0; + maxbuffer = 0; + buffer = 0; + losttime = 0; +} + +/* + Destroys a TQMNGFormat. +*/ +TQMNGFormat::~TQMNGFormat() +{ + // We're setting the consumer to 0 since it may have been + // deleted by read_async_image in qimage.cpp + consumer = 0; + if (handle) mng_cleanup(&handle); +} + + +// C-callback to C++-member-function conversion +// +static mng_bool openstream( mng_handle handle ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->openstream(); +} +static mng_bool closestream( mng_handle handle ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->closestream(); +} +static mng_bool readdata( mng_handle handle, mng_ptr pBuf, mng_uint32 iBuflen, mng_uint32p pRead ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->readdata(pBuf,iBuflen,pRead); +} +static mng_bool errorproc( mng_handle handle, + mng_int32 iErrorcode, + mng_int8 iSeverity, + mng_chunkid iChunkname, + mng_uint32 iChunkseq, + mng_int32 iExtra1, + mng_int32 iExtra2, + mng_pchar zErrortext ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->errorproc(iErrorcode, + iSeverity,iChunkname,iChunkseq,iExtra1,iExtra2,zErrortext); +} +static mng_bool processheader( mng_handle handle, + mng_uint32 iWidth, mng_uint32 iHeight ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->processheader(iWidth,iHeight); +} +static mng_ptr getcanvasline( mng_handle handle, mng_uint32 iLinenr ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->getcanvasline(iLinenr); +} +static mng_bool refresh( mng_handle handle, + mng_uint32 iTop, + mng_uint32 iLeft, + mng_uint32 iBottom, + mng_uint32 iRight ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->refresh(iTop,iLeft,iBottom,iRight); +} +static mng_uint32 gettickcount( mng_handle handle ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->gettickcount(); +} +static mng_bool settimer( mng_handle handle, mng_uint32 iMsecs ) +{ + return ((TQMNGFormat*)mng_get_userdata(handle))->settimer(iMsecs); +} + +static mng_ptr memalloc( mng_size_t iLen ) +{ + return calloc(1,iLen); +} +static void memfree( mng_ptr iPtr, mng_size_t /*iLen*/ ) +{ + free(iPtr); +} + +/*! + This function decodes some data into image changes. + + Returns the number of bytes consumed. +*/ +int TQMNGFormat::decode( TQImage& img, TQImageConsumer* cons, + const uchar* buf, int length ) +{ + consumer = cons; + image = &img; + + data = buf; + ndata = length; + ubuffer = 0; + + if ( state == MovieStart ) { + handle = mng_initialize( (mng_ptr)this, ::memalloc, ::memfree, 0 ); + mng_set_suspensionmode( handle, MNG_TRUE ); + mng_setcb_openstream( handle, ::openstream ); + mng_setcb_closestream( handle, ::closestream ); + mng_setcb_readdata( handle, ::readdata ); + mng_setcb_errorproc( handle, ::errorproc ); + mng_setcb_processheader( handle, ::processheader ); + mng_setcb_getcanvasline( handle, ::getcanvasline ); + mng_setcb_refresh( handle, ::refresh ); + mng_setcb_gettickcount( handle, ::gettickcount ); + mng_setcb_settimer( handle, ::settimer ); + state = Data; + mng_readdisplay(handle); + losingtimer.start(); + } + + losttime += losingtimer.elapsed(); + if ( ndata || !length ) + mng_display_resume(handle); + losingtimer.start(); + + image = 0; + + nbuffer -= ubuffer; + if ( nbuffer ) { + // Move back unused tail + memcpy(buffer,buffer+ubuffer,nbuffer); + } + if ( ndata ) { + // Not all used. + enlargeBuffer(nbuffer+ndata); + memcpy(buffer+nbuffer,data,ndata); + nbuffer += ndata; + } + + return length; +} + +static TQMNGFormatType* globalMngFormatTypeObject = 0; + +#endif // QT_NO_ASYNC_IMAGE_IO + +#ifndef QT_NO_ASYNC_IMAGE_IO +void qCleanupMngIO() +{ + if ( globalMngFormatTypeObject ) { + delete globalMngFormatTypeObject; + globalMngFormatTypeObject = 0; + } +} +#endif + +void qInitMngIO() +{ + static bool done = FALSE; + if ( !done ) { + done = TRUE; +#ifndef QT_NO_ASYNC_IMAGE_IO + globalMngFormatTypeObject = new TQMNGFormatType; + qAddPostRoutine( qCleanupMngIO ); +#endif + } +} + +#endif // QT_NO_IMAGEIO_MNG diff --git a/src/kernel/qmngio.h b/src/kernel/qmngio.h new file mode 100644 index 000000000..c0a1a8f4a --- /dev/null +++ b/src/kernel/qmngio.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Definition of MNG TQImage IOHandler +** +** Created : 970521 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQMNGIO_H +#define TQMNGIO_H + +#ifndef QT_H +#endif // QT_H + +#ifndef QT_NO_IMAGEIO_MNG + +void qInitMngIO(); + +#endif // QT_NO_IMAGEIO_MNG + +#endif // TQMNGIO_H diff --git a/src/kernel/qmotifdnd_x11.cpp b/src/kernel/qmotifdnd_x11.cpp new file mode 100644 index 000000000..8969dea4f --- /dev/null +++ b/src/kernel/qmotifdnd_x11.cpp @@ -0,0 +1,978 @@ +/**************************************************************************** +** +** Implementation of Motif Dynamic Drag and Drop class +** +** Created : 950419 +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +/* The following copyright notice pertains to the code as contributed +to Trolltech, not to Trolltech's modifications. It is replicated +in doc/dnd.doc, where the documentation system can see it. */ + +/* Copyright 1996 Daniel Dardailler. + + Permission to use, copy, modify, distribute, and sell this software + for any purpose is hereby granted without fee, provided that the above + copyright notice appear in all copies and that both that copyright + notice and this permission notice appear in supporting documentation, + and that the name of Daniel Dardailler not be used in advertising or + publicity pertaining to distribution of the software without specific, + written prior permission. Daniel Dardailler makes no representations + about the suitability of this software for any purpose. It is + provided "as is" without express or implied warranty. + + Modifications Copyright 1999 Matt Koss, under the same license as + above. +************************************************************/ + +/***********************************************************/ +/* Motif Drag&Drop Dynamic Protocol messaging API code */ +/* Only retquires Xlib layer - not MT safe */ +/* Author: Daniel Dardailler, daniel@x.org */ +/* Adapted by : Matt Koss, koss@napri.sk */ +/* Further adaptions by : Trolltech ASA */ +/***********************************************************/ + +#include "qplatformdefs.h" + +#include "qapplication.h" + +#ifndef QT_NO_DRAGANDDROP + +#include "qwidget.h" +#include "qt_x11_p.h" + +#include + + +static Atom atom_message_type, atom_receiver_info, atom_src_property_type; +static Atom atom_motif_window, atom_target_list ; + +static bool in_drop_site = FALSE; +static Window cur_window = 0; +static TQWidget *drop_widget = 0L; + +static Atom Dnd_transfer_success, Dnd_transfer_failure; + +static Atom Dnd_selection; +static Time Dnd_selection_time; + +static Atom * src_targets ; +static ushort num_src_targets ; + +extern bool qt_motifdnd_active; + +// this stuff is copied from qclipboard_x11.cpp + +extern bool qt_xclb_wait_for_event( Display *dpy, Window win, int type, + XEvent *event, int timeout ); +extern bool qt_xclb_read_property( Display *dpy, Window win, Atom property, + bool deleteProperty, + TQByteArray *buffer, int *size, Atom *type, + int *format, bool nullterm ); + +extern Atom* qt_xdnd_str_to_atom( const char *mimeType ); +extern const char* qt_xdnd_atom_to_str( Atom ); + + +// Motif definitions +#define DndVersion 1 +#define DndRevision 0 +#define DndIncludeVersion (DndVersion * 10 + DndRevision) + +/* The following values are used in the DndData structure */ + +/* protocol style */ +#define DND_DRAG_NONE 0 +#define DND_DRAG_DROP_ONLY 1 +#define DND_DRAG_DYNAMIC 5 + +/* message type */ +#define DND_TOP_LEVEL_ENTER 0 +#define DND_TOP_LEVEL_LEAVE 1 +#define DND_DRAG_MOTION 2 +#define DND_DROP_SITE_ENTER 3 +#define DND_DROP_SITE_LEAVE 4 +#define DND_DROP_START 5 +#define DND_OPERATION_CHANGED 8 + +/* operation(s) */ +#define DND_NOOP 0L +#define DND_MOVE (1L << 0) +#define DND_COPY (1L << 1) +#define DND_LINK (1L << 2) + +/* status */ +#define DND_NO_DROP_SITE 1 +#define DND_INVALID_DROP_SITE 2 +#define DND_VALID_DROP_SITE 3 + +/* completion */ +#define DND_DROP 0 +#define DND_DROP_HELP 1 +#define DND_DROP_CANCEL 2 + +#define BYTE unsigned char +#define CARD32 unsigned int +#define CARD16 unsigned short +#define INT16 signed short + +/* Client side structure used in the API */ +typedef struct { + unsigned char reason; /* message type: DND_TOP_LEVEL_ENTER, etc */ + Time time ; + unsigned char operation; + unsigned char operations; + unsigned char status; + unsigned char completion; + short x ; + short y ; + Window src_window ; + Atom property ; +} DndData ; + + +typedef struct _DndSrcProp { + BYTE byte_order ; + BYTE protocol_version ; + CARD16 target_index ; + CARD32 selection ; +} DndSrcProp ; + +typedef struct _DndReceiverProp { + BYTE byte_order ; + BYTE protocol_version ; + BYTE protocol_style ; + BYTE pad1; + CARD32 proxy_window; + CARD16 num_drop_sites ; + CARD16 pad2; + CARD32 total_size; +} DndReceiverProp ; + +/* need to use some union hack since window and property are in + different order depending on the message ... */ +typedef struct _DndTop { + CARD32 src_window; + CARD32 property; +} DndTop ; + +typedef struct _DndPot { + INT16 x; + INT16 y; + CARD32 property; + CARD32 src_window; +} DndPot ; + +typedef struct _DndMessage { + BYTE reason; + BYTE byte_order; + CARD16 flags; + CARD32 time; + union { + DndTop top ; + DndPot pot ; + } data ; +} DndMessage ; + +typedef struct { + BYTE byte_order; + BYTE protocol_version; + CARD16 num_target_lists; + CARD32 data_size; + /* then come series of CARD16,CARD32,CARD32,CARD32... */ +} DndTargets; + + +/* protocol version */ +#define DND_PROTOCOL_VERSION 0 + + +#define DND_EVENT_TYPE_MASK ((BYTE)0x80) +#define DND_EVENT_TYPE_SHIFT 7 +#define DND_CLEAR_EVENT_TYPE ((BYTE)0x7F) + +/* message_type is data[0] of the client_message + this return 1 (receiver bit up) or 0 (initiator) */ +#define DND_GET_EVENT_TYPE(message_type) \ +((char) (((message_type) & DND_EVENT_TYPE_MASK) >> DND_EVENT_TYPE_SHIFT)) + +/* event_type can be 0 (initiator) or 1 (receiver) */ +#define DND_SET_EVENT_TYPE(event_type) \ +(((BYTE)(event_type) << DND_EVENT_TYPE_SHIFT) & DND_EVENT_TYPE_MASK) + + +#define DND_OPERATION_MASK ((CARD16) 0x000F) +#define DND_OPERATION_SHIFT 0 +#define DND_STATUS_MASK ((CARD16) 0x00F0) +#define DND_STATUS_SHIFT 4 +#define DND_OPERATIONS_MASK ((CARD16) 0x0F00) +#define DND_OPERATIONS_SHIFT 8 +#define DND_COMPLETION_MASK ((CARD16) 0xF000) +#define DND_COMPLETION_SHIFT 12 + +#define DND_GET_OPERATION(flags) \ +((unsigned char) \ +(((flags) & DND_OPERATION_MASK) >> DND_OPERATION_SHIFT)) + +#define DND_SET_OPERATION(operation) \ +(((CARD16)(operation) << DND_OPERATION_SHIFT)\ +& DND_OPERATION_MASK) + +#define DND_GET_STATUS(flags) \ +((unsigned char) \ +(((flags) & DND_STATUS_MASK) >> DND_STATUS_SHIFT)) + +#define DND_SET_STATUS(status) \ +(((CARD16)(status) << DND_STATUS_SHIFT)\ +& DND_STATUS_MASK) + +#define DND_GET_OPERATIONS(flags) \ +((unsigned char) \ +(((flags) & DND_OPERATIONS_MASK) >> DND_OPERATIONS_SHIFT)) + +#define DND_SET_OPERATIONS(operation) \ +(((CARD16)(operation) << DND_OPERATIONS_SHIFT)\ +& DND_OPERATIONS_MASK) + +#define DND_GET_COMPLETION(flags) \ +((unsigned char) \ +(((flags) & DND_COMPLETION_MASK) >> DND_COMPLETION_SHIFT)) + +#define DND_SET_COMPLETION(completion) \ +(((CARD16)(completion) << DND_COMPLETION_SHIFT)\ +& DND_COMPLETION_MASK) + + +#define SWAP4BYTES(l) {\ +struct { unsigned t :32;} bit32;\ +char n, *tp = (char *) &bit32;\ +bit32.t = l;\ +n = tp[0]; tp[0] = tp[3]; tp[3] = n;\ +n = tp[1]; tp[1] = tp[2]; tp[2] = n;\ +l = bit32.t;\ +} + +#define SWAP2BYTES(s) {\ +struct { unsigned t :16; } bit16;\ +char n, *tp = (char *) &bit16;\ +bit16.t = s;\ +n = tp[0]; tp[0] = tp[1]; tp[1] = n;\ +s = bit16.t;\ +} + + +/** Private extern functions */ + +static unsigned char DndByteOrder (); + + +/***** Targets/Index stuff */ + +typedef struct { + int num_targets; + Atom *targets; +} DndTargetsTableEntryRec, * DndTargetsTableEntry; + +typedef struct { + int num_entries; + DndTargetsTableEntry entries; +} DndTargetsTableRec, * DndTargetsTable; + + +static int _DndIndexToTargets(Display * display, + int index, + Atom ** targets); + +extern void qt_x11_intern_atom( const char *, Atom * ); + +///////////////////////////////////////////////////////////////// + +void qt_x11_motifdnd_init() +{ + /* Init atoms used in the com */ + + qt_x11_intern_atom( "_MOTIF_DRAG_AND_DROP_MESSAGE", &atom_message_type ); + qt_x11_intern_atom( "_MOTIF_DRAG_INITIATOR_INFO", &atom_src_property_type ); + qt_x11_intern_atom( "_MOTIF_DRAG_RECEIVER_INFO", &atom_receiver_info ); + qt_x11_intern_atom( "_MOTIF_DRAG_WINDOW", &atom_motif_window ); + qt_x11_intern_atom( "_MOTIF_DRAG_TARGETS", &atom_target_list ); + + qt_x11_intern_atom( "XmTRANSFER_SUCCESS", &Dnd_transfer_success ); + qt_x11_intern_atom( "XmTRANSFER_FAILURE", &Dnd_transfer_failure ); + + char my_dnd_selection_name[30]; // 11-digit number should be enough + sprintf(my_dnd_selection_name, "_MY_DND_SELECTION_%d", (int)getpid()); + qt_x11_intern_atom( my_dnd_selection_name, &Dnd_selection ); +} + +static unsigned char DndByteOrder () +{ + static unsigned char byte_order = 0; + + if (!byte_order) { + unsigned int endian = 1; + byte_order = (*((char *)&endian))?'l':'B'; + } + return byte_order ; +} + + + +static void DndReadSourceProperty(Display * dpy, + Window window, Atom dnd_selection, + Atom ** targets, unsigned short * num_targets) +{ + DndSrcProp * src_prop = 0; + Atom type ; + int format ; + unsigned long bytesafter, lengthRtn; + + if ((XGetWindowProperty (dpy, window, dnd_selection, 0L, 100000L, + False, atom_src_property_type, &type, + &format, &lengthRtn, &bytesafter, + (unsigned char **) &src_prop) != Success) + || (type == None)) { + *num_targets = 0; + return ; + } + + if (src_prop->byte_order != DndByteOrder()) { + SWAP2BYTES(src_prop->target_index); + SWAP4BYTES(src_prop->selection); + } + + *num_targets = _DndIndexToTargets(dpy, src_prop->target_index, targets); + + XFree((char*)src_prop); +} + + +/* Position the _MOTIF_DRAG_RECEIVER_INFO property on the dropsite window. + Called by the receiver of the drop to indicate the + supported protocol style : dynamic, drop_only or none */ +static void DndWriteReceiverProperty(Display * dpy, Window window, + unsigned char protocol_style) +{ + DndReceiverProp receiver_prop ; + + receiver_prop.byte_order = DndByteOrder() ; + receiver_prop.protocol_version = DND_PROTOCOL_VERSION; + receiver_prop.protocol_style = protocol_style ; + receiver_prop.proxy_window = None ; + receiver_prop.num_drop_sites = 0 ; + receiver_prop.total_size = sizeof(DndReceiverProp); + + /* write the buffer to the property */ + XChangeProperty (dpy, window, atom_receiver_info, atom_receiver_info, + 8, PropModeReplace, + (unsigned char *)&receiver_prop, + sizeof(DndReceiverProp)); +} + + +/* protocol style equiv (preregister stuff really) */ +#define DND_DRAG_DROP_ONLY_ETQUIV 3 +#define DND_DRAG_DYNAMIC_ETQUIV1 2 +#define DND_DRAG_DYNAMIC_ETQUIV2 4 + + +/* Produce a client message to be sent by the caller */ +static void DndFillClientMessage(Display * dpy, Window window, + XClientMessageEvent *cm, + DndData * dnd_data, + char receiver) +{ + DndMessage * dnd_message = (DndMessage*)&cm->data.b[0] ; + + cm->display = dpy; + cm->type = ClientMessage; + cm->serial = LastKnownRequestProcessed(dpy); + cm->send_event = True; + cm->window = window; + cm->format = 8; + cm->message_type = atom_message_type ;/* _MOTIF_DRAG_AND_DROP_MESSAGE */ + + dnd_message->reason = dnd_data->reason | DND_SET_EVENT_TYPE(receiver); + + dnd_message->byte_order = DndByteOrder(); + + /* we're filling in flags with more stuff that necessary, + depending on the reason, but it doesn't matter */ + dnd_message->flags = 0 ; + dnd_message->flags |= DND_SET_STATUS(dnd_data->status) ; + dnd_message->flags |= DND_SET_OPERATION(dnd_data->operation) ; + dnd_message->flags |= DND_SET_OPERATIONS(dnd_data->operations) ; + dnd_message->flags |= DND_SET_COMPLETION(dnd_data->completion) ; + + dnd_message->time = dnd_data->time ; + + switch(dnd_data->reason) { + case DND_DROP_SITE_LEAVE: break ; + case DND_TOP_LEVEL_ENTER: + case DND_TOP_LEVEL_LEAVE: + dnd_message->data.top.src_window = dnd_data->src_window ; + dnd_message->data.top.property = dnd_data->property ; + break ; /* cannot fall thru since the byte layout is different in + both set of messages, see top and pot union stuff */ + + case DND_DRAG_MOTION: + case DND_OPERATION_CHANGED: + case DND_DROP_SITE_ENTER: + case DND_DROP_START: + dnd_message->data.pot.x = dnd_data->x ; /* mouse position */ + dnd_message->data.pot.y = dnd_data->y ; + dnd_message->data.pot.src_window = dnd_data->src_window ; + dnd_message->data.pot.property = dnd_data->property ; + break ; + default: + break ; + } + +} + +static Bool DndParseClientMessage(XClientMessageEvent *cm, DndData * dnd_data, + char * receiver) +{ + DndMessage * dnd_message = (DndMessage*)&cm->data.b[0] ; + + if (cm->message_type != atom_message_type) { + return False ; + } + + if (dnd_message->byte_order != DndByteOrder()) { + SWAP2BYTES(dnd_message->flags); + SWAP4BYTES(dnd_message->time); + } /* do the rest in the switch */ + + dnd_data->reason = dnd_message->reason ; + if (DND_GET_EVENT_TYPE(dnd_data->reason)) + *receiver = 1 ; + else + *receiver = 0 ; + dnd_data->reason &= DND_CLEAR_EVENT_TYPE ; + + dnd_data->time = dnd_message->time ; + + /* we're reading in more stuff that necessary. but who cares */ + dnd_data->status = DND_GET_STATUS(dnd_message->flags) ; + dnd_data->operation = DND_GET_OPERATION(dnd_message->flags) ; + dnd_data->operations = DND_GET_OPERATIONS(dnd_message->flags) ; + dnd_data->completion = DND_GET_COMPLETION(dnd_message->flags) ; + + switch(dnd_data->reason) { + case DND_TOP_LEVEL_ENTER: + case DND_TOP_LEVEL_LEAVE: + if (dnd_message->byte_order != DndByteOrder()) { + SWAP4BYTES(dnd_message->data.top.src_window); + SWAP4BYTES(dnd_message->data.top.property); + } + dnd_data->src_window = dnd_message->data.top.src_window ; + dnd_data->property = dnd_message->data.top.property ; + break ; /* cannot fall thru, see above comment in write msg */ + + case DND_DRAG_MOTION: + case DND_OPERATION_CHANGED: + case DND_DROP_SITE_ENTER: + case DND_DROP_START: + if (dnd_message->byte_order != DndByteOrder()) { + SWAP2BYTES(dnd_message->data.pot.x); + SWAP2BYTES(dnd_message->data.pot.y); + SWAP4BYTES(dnd_message->data.pot.property); + SWAP4BYTES(dnd_message->data.pot.src_window); + } + dnd_data->x = dnd_message->data.pot.x ; + dnd_data->y = dnd_message->data.pot.y ; + dnd_data->property = dnd_message->data.pot.property ; + dnd_data->src_window = dnd_message->data.pot.src_window ; + break ; + + case DND_DROP_SITE_LEAVE: + break; + default: + break ; + } + + return True ; +} + + +static Window MotifWindow(Display *display ) +{ + Atom type; + int format; + unsigned long size; + unsigned long bytes_after; + Window *property = 0; + Window motif_window ; + + /* this version does no caching, so it's slow: round trip each time */ + + if ((XGetWindowProperty (display, DefaultRootWindow(display), + atom_motif_window, + 0L, 100000L, False, AnyPropertyType, + &type, &format, &size, &bytes_after, + (unsigned char **) &property) == Success) && + (type != None)) { + motif_window = *property; + } else { + XSetWindowAttributes sAttributes; + + /* really, this should be done on a separate connection, + with XSetCloseDownMode (RetainPermanent), so that + others don't have to recreate it; hopefully, some real + Motif application will be around to do it */ + + sAttributes.override_redirect = True; + sAttributes.event_mask = PropertyChangeMask; + motif_window = XCreateWindow (display, + DefaultRootWindow (display), + -170, -560, 1, 1, 0, 0, + InputOnly, CopyFromParent, + (CWOverrideRedirect |CWEventMask), + &sAttributes); + XMapWindow (display, motif_window); + } + + if (property) { + XFree ((char *)property); + } + + return (motif_window); +} + + +static DndTargetsTable TargetsTable(Display *display) +{ + Atom type; + int format; + unsigned long size; + unsigned long bytes_after; + Window motif_window = MotifWindow(display) ; + DndTargets * target_prop; + DndTargetsTable targets_table ; + int i,j ; + char * target_data ; + + /* this version does no caching, so it's slow: round trip each time */ + /* ideally, register for property notify on this target_list + atom and update when necessary only */ + + if ((XGetWindowProperty (display, motif_window, + atom_target_list, 0L, 100000L, + False, atom_target_list, + &type, &format, &size, &bytes_after, + (unsigned char **) &target_prop) != Success) || + type == None) { + qWarning("TQMotifDND: cannot get property on motif window"); + return 0; + } + + if (target_prop->protocol_version != DND_PROTOCOL_VERSION) { + qWarning("TQMotifDND: protocol mismatch"); + } + + if (target_prop->byte_order != DndByteOrder()) { + /* need to swap num_target_lists and size */ + SWAP2BYTES(target_prop->num_target_lists); + SWAP4BYTES(target_prop->data_size); + } + + /* now parse DndTarget prop data in a TargetsTable */ + + targets_table = (DndTargetsTable)malloc(sizeof(DndTargetsTableRec)); + targets_table->num_entries = target_prop->num_target_lists ; + targets_table->entries = (DndTargetsTableEntry) + malloc(sizeof(DndTargetsTableEntryRec) * target_prop->num_target_lists); + + target_data = (char*)target_prop + sizeof(*target_prop) ; + + for (i = 0 ; i < targets_table->num_entries; i++) { + CARD16 num_targets ; + CARD32 atom ; + + memcpy(&num_targets, target_data, 2); + target_data += 2; + + /* potential swap needed here */ + if (target_prop->byte_order != DndByteOrder()) + SWAP2BYTES(num_targets); + + targets_table->entries[i].num_targets = num_targets ; + targets_table->entries[i].targets = (Atom *) + malloc(sizeof(Atom) * targets_table->entries[i].num_targets); + + + for (j = 0; j < num_targets; j++) { + memcpy(&atom, target_data, 4 ); + target_data += 4; + + /* another potential swap needed here */ + if (target_prop->byte_order != DndByteOrder()) + SWAP4BYTES(atom); + + targets_table->entries[i].targets[j] = (Atom) atom ; + } + } + + if (target_prop) { + XFree((char *)target_prop); + } + + return targets_table ; +} + + +static int _DndIndexToTargets(Display * display, + int index, + Atom ** targets) +{ + DndTargetsTable targets_table; + int i ; + + /* again, slow: no caching here, alloc/free each time */ + + if (!(targets_table = TargetsTable (display)) || + (index >= targets_table->num_entries)) { + return -1; + } + + /* transfer the correct target list index */ + *targets = (Atom*)malloc(sizeof(Atom)*targets_table-> + entries[index].num_targets); + memcpy((char*)*targets, + (char*)targets_table->entries[index].targets, + sizeof(Atom)*targets_table->entries[index].num_targets); + + /* free the target table and its guts */ + for (i=0 ; i < targets_table->num_entries; i++) + XFree((char*)targets_table->entries[i].targets); + + int tmp = targets_table->entries[index].num_targets; + XFree((char*)targets_table); + + return tmp; // targets_table->entries[index].num_targets; +} + + +const char *qt_motifdnd_format( int n ) +{ + if ( ! qt_motifdnd_active ) + return 0; // should not happen + + if ( n == 0 ) + return "text/plain"; + if ( n == 1 ) + return "text/uri-list"; + n -= 2; + + if ( n >= num_src_targets ) + return 0; + + Atom target = src_targets[n]; + + // duplicated from qclipboard_x11.cpp - not the best solution + static Atom xa_utf8_string = *qt_xdnd_str_to_atom( "UTF8_STRING" ); + static Atom xa_text = *qt_xdnd_str_to_atom( "TEXT" ); + static Atom xa_compound_text = *qt_xdnd_str_to_atom( "COMPOUND_TEXT" ); + + if ( target == XA_STRING ) + return "text/plain;charset=ISO-8859-1"; + if ( target == xa_utf8_string ) + return "text/plain;charset=UTF-8"; + if ( target == xa_text || + target == xa_compound_text ) + return "text/plain"; + + return qt_xdnd_atom_to_str( target ); +} + + +TQByteArray qt_motifdnd_obtain_data( const char *mimeType ) +{ + TQByteArray result; + + // try to convert the selection to the requested property + // qDebug( "trying to convert to '%s'", mimeType ); + + int n=0; + const char* f; + do { + f = qt_motifdnd_format( n ); + if ( !f ) + return result; + n++; + } while( qstricmp( mimeType, f ) ); + + // found one + Atom conversion_type; + + if ( qstrnicmp( f, "text/", 5 ) == 0 ) { + // always convert text to XA_STRING for compatibility with + // prior TQt versions + conversion_type = XA_STRING; + } else { + conversion_type = *qt_xdnd_str_to_atom( f ); + // qDebug( "found format '%s' 0x%lx '%s'", f, conversion_type, + // qt_xdnd_atom_to_str( conversion_type ) ); + } + + if ( XGetSelectionOwner( qt_xdisplay(), + Dnd_selection ) == None ) { + return result; // should never happen? + } + + TQWidget* tw = drop_widget; + if ( drop_widget->isDesktop() ) { + tw = new TQWidget; + } + + // convert selection to the appropriate type + XConvertSelection (qt_xdisplay(), Dnd_selection, conversion_type, + Dnd_selection, tw->winId(), Dnd_selection_time); + + XFlush( qt_xdisplay() ); + + XEvent xevent; + bool got=qt_xclb_wait_for_event( qt_xdisplay(), + tw->winId(), + SelectionNotify, &xevent, 5000); + if ( got ) { + Atom type; + + if ( qt_xclb_read_property( qt_xdisplay(), + tw->winId(), + Dnd_selection, TRUE, + &result, 0, &type, 0, TRUE ) ) { + } + } + + // we have to convert selection in order to indicate success to the initiator + XConvertSelection (qt_xdisplay(), Dnd_selection, Dnd_transfer_success, + Dnd_selection, tw->winId(), Dnd_selection_time); + + // wait again for SelectionNotify event + qt_xclb_wait_for_event( qt_xdisplay(), + tw->winId(), + SelectionNotify, &xevent, 5000); + + if ( drop_widget->isDesktop() ) { + delete tw; + } + + return result; +} + + +void qt_motifdnd_enable( TQWidget *widget, bool ) +{ + DndWriteReceiverProperty( widget->x11Display(), widget->winId(), + DND_DRAG_DYNAMIC); +} + + +void qt_motifdnd_handle_msg( TQWidget * /* w */ , const XEvent * xe, bool /* passive */ ) +{ + + XEvent event = *xe; + XClientMessageEvent cm ; + DndData dnd_data ; + char receiver ; + + if (!(DndParseClientMessage ((XClientMessageEvent*)&event, + &dnd_data, &receiver))) { + return; + } + + switch ( dnd_data.reason ) { + + case DND_DRAG_MOTION: + + { + /* check if in drop site, and depending on the state, + send a drop site enter or drop site leave or echo */ + + TQPoint p( dnd_data.x, dnd_data.y ); + TQWidget *c = TQApplication::widgetAt( p, TRUE ); + if (c) + p = c->mapFromGlobal(p); + + while ( c && !c->acceptDrops() && !c->isTopLevel() ) { + p = c->mapToParent( p ); + c = c->parentWidget(); + } + + TQDragMoveEvent me( p ); + TQDropEvent::Action accepted_action = TQDropEvent::Copy; + me.setAction(accepted_action); + + if ( c != 0L && c->acceptDrops() ) { + + if ( drop_widget != 0L && drop_widget->acceptDrops() && + drop_widget != c ) { + TQDragLeaveEvent e; + TQApplication::sendEvent( drop_widget, &e ); + TQDragEnterEvent de( p ); + TQApplication::sendEvent( c, &de ); + } + + drop_widget = c; + + if (!in_drop_site) { + in_drop_site = True ; + + dnd_data.reason = DND_DROP_SITE_ENTER ; + dnd_data.time = CurrentTime ; + dnd_data.operation = DND_MOVE|DND_COPY; + dnd_data.operations = DND_MOVE|DND_COPY; + + DndFillClientMessage (event.xclient.display, + cur_window, + &cm, &dnd_data, 0); + + XSendEvent(event.xbutton.display, + cur_window, False, 0, + (XEvent *)&cm) ; + + TQDragEnterEvent de( p ); + TQApplication::sendEvent( drop_widget, &de ); + if ( de.isAccepted() ) { + me.accept( de.answerRect() ); + } else { + me.ignore( de.answerRect() ); + } + + } else { + dnd_data.reason = DND_DRAG_MOTION ; + dnd_data.time = CurrentTime ; + dnd_data.operation = DND_MOVE|DND_COPY; + dnd_data.operations = DND_MOVE|DND_COPY; + + DndFillClientMessage (event.xclient.display, + cur_window, + &cm, &dnd_data, 0); + + XSendEvent(event.xbutton.display, + cur_window, False, 0, + (XEvent *)&cm) ; + + TQApplication::sendEvent( drop_widget, &me ); + } + } else { + if (in_drop_site) { + in_drop_site = False ; + + dnd_data.reason = DND_DROP_SITE_LEAVE ; + dnd_data.time = CurrentTime ; + + DndFillClientMessage (event.xclient.display, + cur_window, + &cm, &dnd_data, 0); + + XSendEvent(event.xbutton.display, + cur_window, False, 0, + (XEvent *)&cm) ; + + TQDragLeaveEvent e; + TQApplication::sendEvent( drop_widget, &e ); + } + } + } + break; + + case DND_TOP_LEVEL_ENTER: + + /* get the size of our drop site for later use */ + + cur_window = dnd_data.src_window ; + qt_motifdnd_active = TRUE; + + /* no answer needed, just read source property */ + DndReadSourceProperty (event.xclient.display, + cur_window, + dnd_data.property, + &src_targets, &num_src_targets); + break; + + case DND_TOP_LEVEL_LEAVE: + /* no need to do anything */ + break; + + case DND_OPERATION_CHANGED: + /* need to echo */ + break; + + case DND_DROP_START: + if (!in_drop_site) { + // we have to convert selection in order to indicate failure to the initiator + XConvertSelection (qt_xdisplay(), dnd_data.property, Dnd_transfer_failure, + dnd_data.property, cur_window, dnd_data.time); + TQDragLeaveEvent e; + TQApplication::sendEvent( drop_widget, &e ); + drop_widget = 0; + return; + } + + /* need to echo and then request a convert */ + dnd_data.reason = DND_DROP_START ; + + DndFillClientMessage (event.xclient.display, + drop_widget->winId(), + &cm, &dnd_data, 0); + + XSendEvent(event.xbutton.display, + cur_window, False, 0, + (XEvent *)&cm) ; + + // store selection and its time + Dnd_selection = dnd_data.property; + Dnd_selection_time = dnd_data.time; + + TQPoint p( dnd_data.x, dnd_data.y ); + TQDropEvent de( drop_widget->mapFromGlobal(p) ); + de.setAction( TQDropEvent::Copy ); + TQApplication::sendEvent( drop_widget, &de ); + + if (in_drop_site) + in_drop_site = False ; + + drop_widget = 0; + cur_window = 0; + break; + } // end of switch ( dnd_data.reason ) +} + +#endif // QT_NO_DRAGANDDROP diff --git a/src/kernel/qmovie.cpp b/src/kernel/qmovie.cpp new file mode 100644 index 000000000..ffb8d5027 --- /dev/null +++ b/src/kernel/qmovie.cpp @@ -0,0 +1,1081 @@ +/**************************************************************************** +** +** Implementation of movie classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +// #define QT_SAVE_MOVIE_HACK + +#include "qtimer.h" +#include "qpainter.h" +#include "qptrlist.h" +#include "qbitmap.h" +#include "qmovie.h" +#include "qfile.h" +#include "qbuffer.h" +#include "qobject.h" +#include "qpixmapcache.h" + +#ifndef QT_NO_MOVIE + +#ifdef Q_WS_QWS +#include "qgfx_qws.h" +#endif + +#include "qasyncio.h" +#include "qasyncimageio.h" + +#include + +/*! + \class TQMovie qmovie.h + \brief The TQMovie class provides incremental loading of animations or images, signalling as it progresses. + + \ingroup images + \ingroup graphics + \ingroup multimedia + \mainclass + + The simplest way to display a TQMovie is to use a TQLabel and + TQLabel::setMovie(). + + A TQMovie provides a TQPixmap as the framePixmap(); connections can + be made via connectResize() and connectUpdate() to receive + notification of size and pixmap changes. All decoding is driven + by the normal event-processing mechanisms. + + The movie begins playing as soon as the TQMovie is created + (actually, once control returns to the event loop). When the last + frame in the movie has been played, it may loop back to the start + if such looping is defined in the input source. + + TQMovie objects are explicitly shared. This means that a TQMovie + copied from another TQMovie will be displaying the same frame at + all times. If one shared movie pauses, all pause. To make \e + independent movies, they must be constructed separately. + + The set of data formats supported by TQMovie is determined by the + decoder factories that have been installed; the format of the + input is determined as the input is decoded. + + The supported formats are MNG (if TQt is configured with MNG + support enabled) and GIF (if TQt is configured with GIF support + enabled, see qgif.h). + + If TQt is configured to support GIF reading, we are retquired to + state that "The Graphics Interchange Format(c) is the Copyright + property of CompuServe Incorporated. GIF(sm) is a Service Mark + property of CompuServe Incorporated. + + \warning If you are in a country that recognizes software patents + and in which Unisys holds a patent on LZW compression and/or + decompression and you want to use GIF, Unisys may retquire you to + license that technology. Such countries include Canada, Japan, + the USA, France, Germany, Italy and the UK. + + GIF support may be removed completely in a future version of TQt. + We recommend using the MNG or PNG format. + + \img qmovie.png TQMovie + + \sa TQLabel::setMovie() +*/ + +/*! + \enum TQMovie::Status + + \value SourceEmpty + \value UnrecognizedFormat + \value Paused + \value EndOfFrame + \value EndOfLoop + \value EndOfMovie + \value SpeedChanged +*/ + +class TQMoviePrivate : public TQObject, public TQShared, + private TQDataSink, private TQImageConsumer +{ + Q_OBJECT + +public: // for TQMovie + + // Creates a null Private + TQMoviePrivate(); + + // NOTE: The ownership of the TQDataSource is transferred to the Private + TQMoviePrivate(TQDataSource* src, TQMovie* movie, int bufsize); + + virtual ~TQMoviePrivate(); + + bool isNull() const; + + // Initialize, possibly to the null state + void init(bool fully); + void flushBuffer(); + void updatePixmapFromImage(); + void updatePixmapFromImage(const TQPoint& off, const TQRect& area); + void showChanges(); + + // This as TQImageConsumer + void changed(const TQRect& rect); + void end(); + void preFrameDone(); //util func + void frameDone(); + void frameDone(const TQPoint&, const TQRect& rect); + void restartTimer(); + void setLooping(int l); + void setFramePeriod(int milliseconds); + void setSize(int w, int h); + + // This as TQDataSink + int readyToReceive(); + void receive(const uchar* b, int bytecount); + void eof(); + void pause(); + +signals: + void sizeChanged(const TQSize&); + void areaChanged(const TQRect&); + void dataStatus(int); + +public slots: + void refresh(); + +public: + TQMovie *that; + TQWidget * display_widget; + + TQImageDecoder *decoder; + + // Cyclic buffer + int buf_size; + uchar *buffer; + int buf_r, buf_w, buf_usage; + + int framenumber; + int frameperiod; + int speed; + TQTimer *frametimer; + int lasttimerinterval; + int loop; + bool movie_ended; + bool dirty_cache; + bool waitingForFrameTick; + int stepping; + TQRect changed_area; + TQRect valid_area; + TQDataPump *pump; + TQDataSource *source; + TQPixmap mypixmap; + TQBitmap mymask; + TQColor bg; + + int error; + bool empty; + +#ifdef QT_SAVE_MOVIE_HACK + bool save_image; + int image_number; +#endif +}; + + +TQMoviePrivate::TQMoviePrivate() +{ + dirty_cache = FALSE; + buffer = 0; + pump = 0; + source = 0; + decoder = 0; + display_widget=0; + buf_size = 0; + init(FALSE); +} + +// NOTE: The ownership of the TQDataSource is transferred to the Private +TQMoviePrivate::TQMoviePrivate(TQDataSource* src, TQMovie* movie, int bufsize) : + that(movie), + buf_size(bufsize) +{ + frametimer = new TQTimer(this); + pump = src ? new TQDataPump(src, this) : 0; + TQObject::connect(frametimer, SIGNAL(timeout()), this, SLOT(refresh())); + dirty_cache = FALSE; + source = src; + buffer = 0; + decoder = 0; + speed = 100; + display_widget=0; + init(TRUE); +} + +TQMoviePrivate::~TQMoviePrivate() +{ + if ( buffer ) // Avoid purify complaint + delete [] buffer; + delete pump; + delete decoder; + delete source; + + // Too bad.. but better be safe than sorry + if ( dirty_cache ) + TQPixmapCache::clear(); +} + +bool TQMoviePrivate::isNull() const +{ + return !buf_size; +} + +// Initialize. Only actually allocate any space if \a fully is TRUE, +// otherwise, just enough to be a valid null Private. +void TQMoviePrivate::init(bool fully) +{ +#ifdef QT_SAVE_MOVIE_HACK + save_image = TRUE; + image_number = 0; +#endif + + buf_usage = buf_r = buf_w = 0; + if ( buffer ) // Avoid purify complaint + delete [] buffer; + buffer = fully ? new uchar[buf_size] : 0; + if ( buffer ) + memset( buffer, 0, buf_size ); + + delete decoder; + decoder = fully ? new TQImageDecoder(this) : 0; + +#ifdef AVOID_OPEN_FDS + if ( source && !source->isOpen() ) + source->open(IO_ReadOnly); +#endif + + waitingForFrameTick = FALSE; + stepping = -1; + framenumber = 0; + frameperiod = -1; + if (fully) frametimer->stop(); + lasttimerinterval = -1; + changed_area.setRect(0,0,-1,-1); + valid_area = changed_area; + loop = -1; + movie_ended = FALSE; + error = 0; + empty = TRUE; +} + +void TQMoviePrivate::flushBuffer() +{ + int used; + while (buf_usage && !waitingForFrameTick && stepping != 0 && !error) { + used = decoder->decode(buffer + buf_r, TQMIN(buf_usage, buf_size - buf_r)); + if (used <= 0) { + if ( used < 0 ) { + error = 1; + emit dataStatus(TQMovie::UnrecognizedFormat); + } + break; + } + buf_r = (buf_r + used) % buf_size; + buf_usage -= used; + } + + // Some formats, like MNG, can make stuff happen without any extra data. + // Only do this if the movie hasn't ended, however or we'll never get the end of loop signal. + if (!movie_ended) { + used = decoder->decode(buffer + buf_r, 0); + if (used <= 0) { + if ( used < 0 ) { + error = 1; + emit dataStatus(TQMovie::UnrecognizedFormat); + } + } + } + + if (error) + frametimer->stop(); + maybeReady(); +} + +void TQMoviePrivate::updatePixmapFromImage() +{ + if (changed_area.isEmpty()) return; + updatePixmapFromImage(TQPoint(0,0),changed_area); +} + +void TQMoviePrivate::updatePixmapFromImage(const TQPoint& off, + const TQRect& area) +{ + // Create temporary TQImage to hold the part we want + const TQImage& gimg = decoder->image(); + TQImage img = gimg.copy(area); + +#ifdef QT_SAVE_MOVIE_HACK + if ( save_image ) { + TQString name; + name.sprintf("movie%i.ppm",image_number++); + gimg.save( name, "PPM" ); + } +#endif + + // Resize to size of image + if (mypixmap.width() != gimg.width() || mypixmap.height() != gimg.height()) + mypixmap.resize(gimg.width(), gimg.height()); + + // Convert to pixmap and paste that onto myself + TQPixmap lines; + +#ifndef QT_NO_SPRINTF + if (!(frameperiod < 0 && loop == -1)) { + // its an animation, lets see if we converted + // this frame already. + TQString key; + key.sprintf( "%08lx:%04d", ( long )this, framenumber ); + if ( !TQPixmapCache::find( key, lines ) ) { + lines.convertFromImage(img, TQt::ColorOnly); + TQPixmapCache::insert( key, lines ); + dirty_cache = TRUE; + } + } else +#endif + { + lines.convertFromImage(img, TQt::ColorOnly); + } + + if (bg.isValid()) { + TQPainter p; + p.begin(&mypixmap); + p.fillRect(area, bg); + p.drawPixmap(area, lines); + p.end(); + } else { + if (gimg.hasAlphaBuffer()) { + // Resize to size of image + if (mymask.isNull()) { + mymask.resize(gimg.width(), gimg.height()); + mymask.fill( TQt::color1 ); + } + } + mypixmap.setMask(TQBitmap()); // Remove reference to my mask + copyBlt( &mypixmap, area.left(), area.top(), + &lines, off.x(), off.y(), area.width(), area.height() ); + } + +#ifdef Q_WS_QWS + if(display_widget) { + TQGfx * mygfx=display_widget->graphicsContext(); + if(mygfx) { + double xscale,yscale; + xscale=display_widget->width(); + yscale=display_widget->height(); + xscale=xscale/((double)mypixmap.width()); + yscale=yscale/((double)mypixmap.height()); + double xh,yh; + xh=xscale*((double)area.left()); + yh=yscale*((double)area.top()); + mygfx->setSource(&mypixmap); + mygfx->setAlphaType(TQGfx::IgnoreAlpha); + mygfx->stretchBlt(0,0,display_widget->width(), + display_widget->height(),mypixmap.width(), + mypixmap.height()); + delete mygfx; + } + } +#endif +} + +void TQMoviePrivate::showChanges() +{ + if (changed_area.isValid()) { + updatePixmapFromImage(); + + valid_area = valid_area.unite(changed_area); + emit areaChanged(changed_area); + + changed_area.setWidth(-1); // make empty + } +} + +// Private as TQImageConsumer +void TQMoviePrivate::changed(const TQRect& rect) +{ + if (!frametimer->isActive()) + frametimer->start(0); + changed_area = changed_area.unite(rect); +} + +void TQMoviePrivate::end() +{ + movie_ended = TRUE; +} + +void TQMoviePrivate::preFrameDone() +{ + if (stepping > 0) { + stepping--; + if (!stepping) { + frametimer->stop(); + emit dataStatus( TQMovie::Paused ); + } + } else { + waitingForFrameTick = TRUE; + restartTimer(); + } +} +void TQMoviePrivate::frameDone() +{ + preFrameDone(); + showChanges(); + emit dataStatus(TQMovie::EndOfFrame); + framenumber++; +} +void TQMoviePrivate::frameDone(const TQPoint& p, + const TQRect& rect) +{ + preFrameDone(); + const TQImage& gimg = decoder->image(); + TQPoint point = p - gimg.offset(); + if (framenumber==0) + emit sizeChanged(gimg.size()); + valid_area = valid_area.unite(TQRect(point,rect.size())); + updatePixmapFromImage(point,rect); + emit areaChanged(TQRect(point,rect.size())); + emit dataStatus(TQMovie::EndOfFrame); + framenumber++; +} + +void TQMoviePrivate::restartTimer() +{ + if (speed > 0) { + int i = frameperiod >= 0 ? frameperiod * 100/speed : 0; + if ( i != lasttimerinterval || !frametimer->isActive() ) { + lasttimerinterval = i; + frametimer->start( i ); + } + } else { + frametimer->stop(); + } +} + +void TQMoviePrivate::setLooping(int nloops) +{ + if (loop == -1) { // Only if we don't already know how many loops! + if (source && source->rewindable()) { + source->enableRewind(TRUE); + loop = nloops; + } else { + // Cannot loop from this source + loop = -2; + } + } +} + +void TQMoviePrivate::setFramePeriod(int milliseconds) +{ + // Animation: only show complete frame + frameperiod = milliseconds; + if (stepping<0 && frameperiod >= 0) restartTimer(); +} + +void TQMoviePrivate::setSize(int w, int h) +{ + if (mypixmap.width() != w || mypixmap.height() != h) { + mypixmap.resize(w, h); + emit sizeChanged(TQSize(w, h)); + } +} + + +// Private as TQDataSink + +int TQMoviePrivate::readyToReceive() +{ + // Could pre-fill buffer, but more efficient to just leave the + // data back at the source. + return (waitingForFrameTick || !stepping || buf_usage || error) + ? 0 : buf_size; +} + +void TQMoviePrivate::receive(const uchar* b, int bytecount) +{ + if ( bytecount ) empty = FALSE; + + while (bytecount && !waitingForFrameTick && stepping != 0) { + int used = decoder->decode(b, bytecount); + if (used<=0) { + if ( used < 0 ) { + error = 1; + emit dataStatus(TQMovie::UnrecognizedFormat); + } + break; + } + b+=used; + bytecount-=used; + } + + // Append unused to buffer + while (bytecount--) { + buffer[buf_w] = *b++; + buf_w = (buf_w+1)%buf_size; + buf_usage++; + } +} + +void TQMoviePrivate::eof() +{ + if ( !movie_ended ) + return; + + if ( empty ) + emit dataStatus(TQMovie::SourceEmpty); + +#ifdef QT_SAVE_MOVIE_HACK + save_image = FALSE; +#endif + + emit dataStatus(TQMovie::EndOfLoop); + + if (loop >= 0) { + if (loop) { + loop--; + if (!loop) return; + } + delete decoder; + decoder = new TQImageDecoder(this); + source->rewind(); + framenumber = 0; + movie_ended = FALSE; + } else { + delete decoder; + decoder = 0; + if ( buffer ) // Avoid purify complaint + delete [] buffer; + buffer = 0; + emit dataStatus(TQMovie::EndOfMovie); +#ifdef AVOID_OPEN_FDS + if ( source ) + source->close(); +#endif + } +} + +void TQMoviePrivate::pause() +{ + if ( stepping ) { + stepping = 0; + frametimer->stop(); + emit dataStatus( TQMovie::Paused ); + } +} + +void TQMoviePrivate::refresh() +{ + if (!decoder) { + frametimer->stop(); + return; + } + + if (frameperiod < 0 && loop == -1) { + // Only show changes if probably not an animation + showChanges(); + } + + if (!buf_usage) { + frametimer->stop(); + } + + waitingForFrameTick = FALSE; + flushBuffer(); +} + +///////////////// End of Private ///////////////// + + + + + +/*! + Constructs a null TQMovie. The only interesting thing to do with + such a movie is to assign another movie to it. + + \sa isNull() +*/ +TQMovie::TQMovie() +{ + d = new TQMoviePrivate(); +} + +/*! + Constructs a TQMovie with an external data source. You should later + call pushData() to send incoming animation data to the movie. + + The \a bufsize argument sets the maximum amount of data the movie + will transfer from the data source per event loop. The lower this + value, the better interleaved the movie playback will be with + other event processing, but the slower the overall processing will + be. + + \sa pushData() +*/ +TQMovie::TQMovie(int bufsize) +{ + d = new TQMoviePrivate(0, this, bufsize); +} + +/*! + Returns the maximum amount of data that can currently be pushed + into the movie by a call to pushData(). This is affected by the + initial buffer size, but varies as the movie plays and data is + consumed. +*/ +int TQMovie::pushSpace() const +{ + return d->readyToReceive(); +} + +/*! + Pushes \a length bytes from \a data into the movie. \a length must + be no more than the amount returned by pushSpace() since the + previous call to pushData(). +*/ +void TQMovie::pushData(const uchar* data, int length) +{ + d->receive(data,length); +} + +#ifdef Q_WS_QWS // ##### Temporary performance experiment +/*! + \internal +*/ +void TQMovie::setDisplayWidget(TQWidget * w) +{ + d->display_widget=w; +} +#endif + +/*! + Constructs a TQMovie that reads an image sequence from the given + data source, \a src. The source must be allocated dynamically, + because TQMovie will take ownership of it and will destroy it when + the movie is destroyed. The movie starts playing as soon as event + processing continues. + + The \a bufsize argument sets the maximum amount of data the movie + will transfer from the data source per event loop. The lower this + value, the better interleaved the movie playback will be with + other event processing, but the slower the overall processing will + be. +*/ +TQMovie::TQMovie(TQDataSource* src, int bufsize) +{ + d = new TQMoviePrivate(src, this, bufsize); +} + +/*! + Constructs a TQMovie that reads an image sequence from the file, \a + fileName. + + The \a bufsize argument sets the maximum amount of data the movie + will transfer from the data source per event loop. The lower this + value, the better interleaved the movie playback will be with + other event processing, but the slower the overall processing will + be. +*/ +TQMovie::TQMovie(const TQString &fileName, int bufsize) +{ + TQFile* file = new TQFile(fileName); + if ( !fileName.isEmpty() ) + file->open(IO_ReadOnly); + d = new TQMoviePrivate(new TQIODeviceSource(file, bufsize), this, bufsize); +} + +/*! + Constructs a TQMovie that reads an image sequence from the byte + array, \a data. + + The \a bufsize argument sets the maximum amount of data the movie + will transfer from the data source per event loop. The lower this + value, the better interleaved the movie playback will be with + other event processing, but the slower the overall processing will + be. +*/ +TQMovie::TQMovie(TQByteArray data, int bufsize) +{ + TQBuffer* buffer = new TQBuffer(data); + buffer->open(IO_ReadOnly); + d = new TQMoviePrivate(new TQIODeviceSource(buffer, bufsize), this, bufsize); +} + +/*! + Constructs a movie that uses the same data as movie \a movie. + TQMovies use explicit sharing, so operations on the copy will + affect both. +*/ +TQMovie::TQMovie(const TQMovie& movie) +{ + d = movie.d; + d->ref(); +} + +/*! + Destroys the TQMovie. If this is the last reference to the data of + the movie, the data is deallocated. +*/ +TQMovie::~TQMovie() +{ + if (d->deref()) delete d; +} + +/*! + Returns TRUE if the movie is null; otherwise returns FALSE. +*/ +bool TQMovie::isNull() const +{ + return d->isNull(); +} + +/*! + Makes this movie use the same data as movie \a movie. TQMovies use + explicit sharing. +*/ +TQMovie& TQMovie::operator=(const TQMovie& movie) +{ + movie.d->ref(); + if (d->deref()) delete d; + d = movie.d; + return *this; +} + + +/*! + Sets the background color of the pixmap to \a c. If the background + color isValid(), the pixmap will never have a mask because the + background color will be used in transparent regions of the image. + + \sa backgroundColor() +*/ +void TQMovie::setBackgroundColor(const TQColor& c) +{ + d->bg = c; +} + +/*! + Returns the background color of the movie set by + setBackgroundColor(). +*/ +const TQColor& TQMovie::backgroundColor() const +{ + return d->bg; +} + +/*! + Returns the area of the pixmap for which pixels have been + generated. +*/ +const TQRect& TQMovie::getValidRect() const +{ + return d->valid_area; +} + +/*! + Returns the current frame of the movie, as a TQPixmap. It is not + generally useful to keep a copy of this pixmap. It is better to + keep a copy of the TQMovie and get the framePixmap() only when + needed for drawing. + + \sa frameImage() +*/ +const TQPixmap& TQMovie::framePixmap() const +{ + return d->mypixmap; +} + +/*! + Returns the current frame of the movie, as a TQImage. It is not + generally useful to keep a copy of this image. Also note that you + must not call this function if the movie is finished(), since by + then the image will not be available. + + \sa framePixmap() +*/ +const TQImage& TQMovie::frameImage() const +{ + return d->decoder->image(); +} + +/*! + Returns the number of steps remaining after a call to step(). If + the movie is paused, steps() returns 0. If it's running normally + or is finished, steps() returns a negative number. +*/ +int TQMovie::steps() const +{ + return d->stepping; +} + +/*! + Returns the number of times EndOfFrame has been emitted since the + start of the current loop of the movie. Thus, before any + EndOfFrame has been emitted the value will be 0; within slots + processing the first signal, frameNumber() will be 1, and so on. +*/ +int TQMovie::frameNumber() const { return d->framenumber; } + +/*! + Returns TRUE if the image is paused; otherwise returns FALSE. +*/ +bool TQMovie::paused() const +{ + return d->stepping == 0; +} + +/*! + Returns TRUE if the image is no longer playing: this happens when + all loops of all frames are complete; otherwise returns FALSE. +*/ +bool TQMovie::finished() const +{ + return !d->decoder; +} + +/*! + Returns TRUE if the image is not single-stepping, not paused, and + not finished; otherwise returns FALSE. +*/ +bool TQMovie::running() const +{ + return d->stepping<0 && d->decoder; +} + +/*! + Pauses the progress of the animation. + + \sa unpause() +*/ +void TQMovie::pause() +{ + d->pause(); +} + +/*! + Unpauses the progress of the animation. + + \sa pause() +*/ +void TQMovie::unpause() +{ + if ( d->stepping >= 0 ) { + if (d->isNull()) + return; + d->stepping = -1; + d->restartTimer(); + } +} + +/*! + \overload + + Steps forward, showing \a steps frames, and then pauses. +*/ +void TQMovie::step(int steps) +{ + if (d->isNull()) + return; + d->stepping = steps; + d->frametimer->start(0); + d->waitingForFrameTick = FALSE; // Full speed ahead! +} + +/*! + Steps forward 1 frame and then pauses. +*/ +void TQMovie::step() +{ + step(1); +} + +/*! + Rewinds the movie to the beginning. If the movie has not been + paused, it begins playing again. +*/ +void TQMovie::restart() +{ + if (d->isNull()) + return; + if (d->source->rewindable()) { + d->source->enableRewind(TRUE); + d->source->rewind(); + int s = d->stepping; + d->init(TRUE); + if ( s>0 ) + step(s); + else if ( s==0 ) + pause(); + } +} + +/*! + Returns the movie's play speed as a percentage. The default is 100 + percent. + + \sa setSpeed() +*/ +int TQMovie::speed() const +{ + return d->speed; +} + +/*! + Sets the movie's play speed as a percentage, to \a percent. This + is a percentage of the speed dictated by the input data format. + The default is 100 percent. +*/ +void TQMovie::setSpeed(int percent) +{ + int oldspeed = d->speed; + if ( oldspeed != percent && percent >= 0 ) { + d->speed = percent; + // Restart timer only if really needed + if (d->stepping < 0) { + if ( !percent || !oldspeed // To or from zero + || oldspeed*4 / percent > 4 // More than 20% slower + || percent*4 / oldspeed > 4 // More than 20% faster + ) + d->restartTimer(); + } + } +} + +/*! + Connects the \a{receiver}'s \a member of type \c{void member(const + TQSize&)} so that it is signalled when the movie changes size. + + Note that due to the explicit sharing of TQMovie objects, these + connections persist until they are explicitly disconnected with + disconnectResize() or until \e every shared copy of the movie is + deleted. +*/ +void TQMovie::connectResize(TQObject* receiver, const char *member) +{ + TQObject::connect(d, SIGNAL(sizeChanged(const TQSize&)), receiver, member); +} + +/*! + Disconnects the \a{receiver}'s \a member (or all members if \a + member is zero) that were previously connected by connectResize(). +*/ +void TQMovie::disconnectResize(TQObject* receiver, const char *member) +{ + TQObject::disconnect(d, SIGNAL(sizeChanged(const TQSize&)), receiver, member); +} + +/*! + Connects the \a{receiver}'s \a member of type \c{void member(const + TQRect&)} so that it is signalled when an area of the framePixmap() + has changed since the previous frame. + + Note that due to the explicit sharing of TQMovie objects, these + connections persist until they are explicitly disconnected with + disconnectUpdate() or until \e every shared copy of the movie is + deleted. +*/ +void TQMovie::connectUpdate(TQObject* receiver, const char *member) +{ + TQObject::connect(d, SIGNAL(areaChanged(const TQRect&)), receiver, member); +} + +/*! + Disconnects the \a{receiver}'s \a member (or all members if \q + member is zero) that were previously connected by connectUpdate(). +*/ +void TQMovie::disconnectUpdate(TQObject* receiver, const char *member) +{ + TQObject::disconnect(d, SIGNAL(areaChanged(const TQRect&)), receiver, member); +} + +/*! + Connects the \a{receiver}'s \a member, of type \c{void + member(int)} so that it is signalled when the movie changes + status. The status codes are negative for errors and positive for + information. + + \table + \header \i Status Code \i Meaning + \row \i TQMovie::SourceEmpty + \i signalled if the input cannot be read. + \row \i TQMovie::UnrecognizedFormat + \i signalled if the input data is unrecognized. + \row \i TQMovie::Paused + \i signalled when the movie is paused by a call to paused() + or by after \link step() stepping \endlink pauses. + \row \i TQMovie::EndOfFrame + \i signalled at end-of-frame after any update and Paused signals. + \row \i TQMovie::EndOfLoop + \i signalled at end-of-loop, after any update signals, + EndOfFrame - but before EndOfMovie. + \row \i TQMovie::EndOfMovie + \i signalled when the movie completes and is not about to loop. + \endtable + + More status messages may be added in the future, so a general test + for errors would test for negative. + + Note that due to the explicit sharing of TQMovie objects, these + connections persist until they are explicitly disconnected with + disconnectStatus() or until \e every shared copy of the movie is + deleted. +*/ +void TQMovie::connectStatus(TQObject* receiver, const char *member) +{ + TQObject::connect(d, SIGNAL(dataStatus(int)), receiver, member); +} + +/*! + Disconnects the \a{receiver}'s \a member (or all members if \a + member is zero) that were previously connected by connectStatus(). +*/ +void TQMovie::disconnectStatus(TQObject* receiver, const char *member) +{ + TQObject::disconnect(d, SIGNAL(dataStatus(int)), receiver, member); +} + + +#include "qmovie.moc" + +#endif // QT_NO_MOVIE diff --git a/src/kernel/qmovie.h b/src/kernel/qmovie.h new file mode 100644 index 000000000..00703d3e0 --- /dev/null +++ b/src/kernel/qmovie.h @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Definition of movie classes +** +** Created : 970617 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQMOVIE_H +#define TQMOVIE_H + +#ifndef QT_H +#include "qpixmap.h" // ### remove or keep for users' convenience? +#endif // QT_H + +#ifndef QT_NO_MOVIE + +class TQDataSource; +class TQObject; +class TQMoviePrivate; + +class Q_EXPORT TQMovie { +public: + TQMovie(); + TQMovie(int bufsize); + TQMovie(TQDataSource*, int bufsize=1024); + TQMovie(const TQString &fileName, int bufsize=1024); + TQMovie(TQByteArray data, int bufsize=1024); + TQMovie(const TQMovie&); + ~TQMovie(); + + TQMovie& operator=(const TQMovie&); + + int pushSpace() const; + void pushData(const uchar* data, int length); + + const TQColor& backgroundColor() const; + void setBackgroundColor(const TQColor&); + + const TQRect& getValidRect() const; + const TQPixmap& framePixmap() const; + const TQImage& frameImage() const; + + bool isNull() const; + + int frameNumber() const; + int steps() const; + bool paused() const; + bool finished() const; + bool running() const; + + void unpause(); + void pause(); + void step(); + void step(int); + void restart(); + + int speed() const; + void setSpeed(int); + + void connectResize(TQObject* receiver, const char *member); + void disconnectResize(TQObject* receiver, const char *member=0); + + void connectUpdate(TQObject* receiver, const char *member); + void disconnectUpdate(TQObject* receiver, const char *member=0); + +#ifdef Q_WS_QWS + // Temporary hack + void setDisplayWidget(TQWidget * w); +#endif + + enum Status { SourceEmpty=-2, + UnrecognizedFormat=-1, + Paused=1, + EndOfFrame=2, + EndOfLoop=3, + EndOfMovie=4, + SpeedChanged=5 }; + void connectStatus(TQObject* receiver, const char *member); + void disconnectStatus(TQObject* receiver, const char *member=0); + +private: + TQMoviePrivate *d; +}; + +#endif // QT_NO_MOVIE + +#endif diff --git a/src/kernel/qnamespace.h b/src/kernel/qnamespace.h new file mode 100644 index 000000000..4e830889f --- /dev/null +++ b/src/kernel/qnamespace.h @@ -0,0 +1,1008 @@ +/**************************************************************************** +** +** Definition of TQt namespace (as class for compiler compatibility) +** +** Created : 980927 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQNAMESPACE_H +#define TQNAMESPACE_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + + +class TQColor; +class TQCursor; + + +class Q_EXPORT TQt { +public: + QT_STATIC_CONST TQColor & color0; + QT_STATIC_CONST TQColor & color1; + QT_STATIC_CONST TQColor & black; + QT_STATIC_CONST TQColor & white; + QT_STATIC_CONST TQColor & darkGray; + QT_STATIC_CONST TQColor & gray; + QT_STATIC_CONST TQColor & lightGray; + QT_STATIC_CONST TQColor & red; + QT_STATIC_CONST TQColor & green; + QT_STATIC_CONST TQColor & blue; + QT_STATIC_CONST TQColor & cyan; + QT_STATIC_CONST TQColor & magenta; + QT_STATIC_CONST TQColor & yellow; + QT_STATIC_CONST TQColor & darkRed; + QT_STATIC_CONST TQColor & darkGreen; + QT_STATIC_CONST TQColor & darkBlue; + QT_STATIC_CONST TQColor & darkCyan; + QT_STATIC_CONST TQColor & darkMagenta; + QT_STATIC_CONST TQColor & darkYellow; + + // documented in qevent.cpp + enum ButtonState { // mouse/keyboard state values + NoButton = 0x0000, + LeftButton = 0x0001, + RightButton = 0x0002, + MidButton = 0x0004, + MouseButtonMask = 0x0007, + ShiftButton = 0x0100, + ControlButton = 0x0200, + AltButton = 0x0400, + MetaButton = 0x0800, + KeyButtonMask = 0x0f00, + Keypad = 0x4000 + }; + + // documented in qobject.cpp + // ideally would start at 1, as in TQSizePolicy, but that breaks other things + enum Orientation { + Horizontal = 0, + Vertical + }; + + // documented in qlistview.cpp + enum SortOrder { + Ascending, + Descending + }; + + // Text formatting flags for TQPainter::drawText and TQLabel + // the following four enums can be combined to one integer which + // is passed as textflag to drawText and qt_format_text. + + // documented in qpainter.cpp + enum AlignmentFlags { + AlignAuto = 0x0000, // text alignment + AlignLeft = 0x0001, + AlignRight = 0x0002, + AlignHCenter = 0x0004, + AlignJustify = 0x0008, + AlignHorizontal_Mask = AlignLeft | AlignRight | AlignHCenter | AlignJustify, + AlignTop = 0x0010, + AlignBottom = 0x0020, + AlignVCenter = 0x0040, + AlignVertical_Mask = AlignTop | AlignBottom | AlignVCenter, + AlignCenter = AlignVCenter | AlignHCenter + }; + + // documented in qpainter.cpp + enum TextFlags { + SingleLine = 0x0080, // misc. flags + DontClip = 0x0100, + ExpandTabs = 0x0200, + ShowPrefix = 0x0400, + WordBreak = 0x0800, + BreakAnywhere = 0x1000, +#ifndef Q_QDOC + DontPrint = 0x2000, + Underline = 0x01000000, + Overline = 0x02000000, + StrikeOut = 0x04000000, + IncludeTrailingSpaces = 0x08000000, +#endif + NoAccel = 0x4000 + }; + + // Widget flags; documented in qwidget.cpp + typedef uint WState; + + // TQWidget state flags (internal, barely documented in qwidget.cpp) + enum WidgetState { + WState_Created = 0x00000001, + WState_Disabled = 0x00000002, + WState_Visible = 0x00000004, + WState_ForceHide = 0x00000008, + WState_OwnCursor = 0x00000010, + WState_MouseTracking = 0x00000020, + WState_CompressKeys = 0x00000040, + WState_BlockUpdates = 0x00000080, + WState_InPaintEvent = 0x00000100, + WState_Reparented = 0x00000200, + WState_ConfigPending = 0x00000400, + WState_Resized = 0x00000800, + WState_AutoMask = 0x00001000, + WState_Polished = 0x00002000, + WState_DND = 0x00004000, + WState_Reserved0 = 0x00008000, + WState_FullScreen = 0x00010000, + WState_OwnSizePolicy = 0x00020000, + WState_CreatedHidden = 0x00040000, + WState_Maximized = 0x00080000, + WState_Minimized = 0x00100000, + WState_ForceDisabled = 0x00200000, + WState_Exposed = 0x00400000, + WState_HasMouse = 0x00800000 + }; + + // Widget flags2; documented in qwidget.cpp + typedef uint WFlags; + + // documented in qwidget.cpp + enum WidgetFlags { + WType_TopLevel = 0x00000001, // widget type flags + WType_Dialog = 0x00000002, + WType_Popup = 0x00000004, + WType_Desktop = 0x00000008, + WType_Mask = 0x0000000f, + + WStyle_Customize = 0x00000010, // window style flags + WStyle_NormalBorder = 0x00000020, + WStyle_DialogBorder = 0x00000040, // MS-Windows only + WStyle_NoBorder = 0x00002000, + WStyle_Title = 0x00000080, + WStyle_SysMenu = 0x00000100, + WStyle_Minimize = 0x00000200, + WStyle_Maximize = 0x00000400, + WStyle_MinMax = WStyle_Minimize | WStyle_Maximize, + WStyle_Tool = 0x00000800, + WStyle_StaysOnTop = 0x00001000, + WStyle_ContextHelp = 0x00004000, + WStyle_Reserved = 0x00008000, + WStyle_Mask = 0x0000fff0, + + WDestructiveClose = 0x00010000, // misc flags + WPaintDesktop = 0x00020000, + WPaintUnclipped = 0x00040000, + WPaintClever = 0x00080000, + WResizeNoErase = 0x00100000, // OBSOLETE + WMouseNoMask = 0x00200000, + WStaticContents = 0x00400000, + WRepaintNoErase = 0x00800000, // OBSOLETE +#if defined(Q_WS_X11) + WX11BypassWM = 0x01000000, + WWinOwnDC = 0x00000000, + WMacNoSheet = 0x00000000, + WMacDrawer = 0x00000000, +#elif defined(Q_WS_MAC) + WX11BypassWM = 0x00000000, + WWinOwnDC = 0x00000000, + WMacNoSheet = 0x01000000, + WMacDrawer = 0x20000000, +#else + WX11BypassWM = 0x00000000, + WWinOwnDC = 0x01000000, + WMacNoSheet = 0x00000000, + WMacDrawer = 0x00000000, +#endif + WGroupLeader = 0x02000000, + WShowModal = 0x04000000, + WNoMousePropagation = 0x08000000, + WSubWindow = 0x10000000, +#if defined(Q_WS_X11) + WStyle_Splash = 0x20000000, +#else + WStyle_Splash = WStyle_NoBorder | WMacNoSheet | WStyle_Tool | WWinOwnDC, +#endif + WNoAutoErase = WRepaintNoErase | WResizeNoErase +#ifndef QT_NO_COMPAT + , + WNorthWestGravity = WStaticContents, + WType_Modal = WType_Dialog | WShowModal, + WStyle_Dialog = WType_Dialog, + WStyle_NoBorderEx = WStyle_NoBorder +#endif + }; + + enum WindowState { + WindowNoState = 0x00000000, + WindowMinimized = 0x00000001, + WindowMaximized = 0x00000002, + WindowFullScreen = 0x00000004, + WindowActive = 0x00000008 + }; + + + // Image conversion flags. The unusual ordering is caused by + // compatibility and default retquirements. + // Documented in qimage.cpp + + enum ImageConversionFlags { + ColorMode_Mask = 0x00000003, + AutoColor = 0x00000000, + ColorOnly = 0x00000003, + MonoOnly = 0x00000002, + // Reserved = 0x00000001, + + AlphaDither_Mask = 0x0000000c, + ThresholdAlphaDither = 0x00000000, + OrderedAlphaDither = 0x00000004, + DiffuseAlphaDither = 0x00000008, + NoAlpha = 0x0000000c, // Not supported + + Dither_Mask = 0x00000030, + DiffuseDither = 0x00000000, + OrderedDither = 0x00000010, + ThresholdDither = 0x00000020, + // ReservedDither= 0x00000030, + + DitherMode_Mask = 0x000000c0, + AutoDither = 0x00000000, + PreferDither = 0x00000040, + AvoidDither = 0x00000080 + }; + + // documented in qpainter.cpp + enum BGMode { // background mode + TransparentMode, + OpaqueMode + }; + +#ifndef QT_NO_COMPAT + // documented in qpainter.cpp + enum PaintUnit { // paint unit + PixelUnit, + LoMetricUnit, // OBSOLETE + HiMetricUnit, // OBSOLETE + LoEnglishUnit, // OBSOLETE + HiEnglishUnit, // OBSOLETE + TwipsUnit // OBSOLETE + }; +#endif + + // documented in qstyle.cpp +#ifdef QT_NO_COMPAT + enum GUIStyle { + WindowsStyle = 1, // ### TQt 4.0: either remove the obsolete enums or clean up compat vs. + MotifStyle = 4, // ### QT_NO_COMPAT by reordering or combination into one enum. + GtkStyle = 6 // Gtk compability mode + }; +#else + enum GUIStyle { + MacStyle, // OBSOLETE + WindowsStyle, + Win3Style, // OBSOLETE + PMStyle, // OBSOLETE + MotifStyle, + GtkStyle = 6 // Gtk compability mode + }; +#endif + + // documented in qkeysequence.cpp + enum SequenceMatch { + NoMatch, + PartialMatch, + Identical + }; + + // documented in qevent.cpp + enum Modifier { // accelerator modifiers + META = 0x00100000, + SHIFT = 0x00200000, + CTRL = 0x00400000, + ALT = 0x00800000, + MODIFIER_MASK = 0x00f00000, + UNICODE_ACCEL = 0x10000000, + + ASCII_ACCEL = UNICODE_ACCEL // 1.x compat + }; + + // documented in qevent.cpp + enum Key { + Key_Escape = 0x1000, // misc keys + Key_Tab = 0x1001, + Key_Backtab = 0x1002, Key_BackTab = Key_Backtab, + Key_Backspace = 0x1003, Key_BackSpace = Key_Backspace, + Key_Return = 0x1004, + Key_Enter = 0x1005, + Key_Insert = 0x1006, + Key_Delete = 0x1007, + Key_Pause = 0x1008, + Key_Print = 0x1009, + Key_SysReq = 0x100a, + Key_Clear = 0x100b, + Key_Home = 0x1010, // cursor movement + Key_End = 0x1011, + Key_Left = 0x1012, + Key_Up = 0x1013, + Key_Right = 0x1014, + Key_Down = 0x1015, + Key_Prior = 0x1016, Key_PageUp = Key_Prior, + Key_Next = 0x1017, Key_PageDown = Key_Next, + Key_Shift = 0x1020, // modifiers + Key_Control = 0x1021, + Key_Meta = 0x1022, + Key_Alt = 0x1023, + Key_CapsLock = 0x1024, + Key_NumLock = 0x1025, + Key_ScrollLock = 0x1026, + Key_F1 = 0x1030, // function keys + Key_F2 = 0x1031, + Key_F3 = 0x1032, + Key_F4 = 0x1033, + Key_F5 = 0x1034, + Key_F6 = 0x1035, + Key_F7 = 0x1036, + Key_F8 = 0x1037, + Key_F9 = 0x1038, + Key_F10 = 0x1039, + Key_F11 = 0x103a, + Key_F12 = 0x103b, + Key_F13 = 0x103c, + Key_F14 = 0x103d, + Key_F15 = 0x103e, + Key_F16 = 0x103f, + Key_F17 = 0x1040, + Key_F18 = 0x1041, + Key_F19 = 0x1042, + Key_F20 = 0x1043, + Key_F21 = 0x1044, + Key_F22 = 0x1045, + Key_F23 = 0x1046, + Key_F24 = 0x1047, + Key_F25 = 0x1048, // F25 .. F35 only on X11 + Key_F26 = 0x1049, + Key_F27 = 0x104a, + Key_F28 = 0x104b, + Key_F29 = 0x104c, + Key_F30 = 0x104d, + Key_F31 = 0x104e, + Key_F32 = 0x104f, + Key_F33 = 0x1050, + Key_F34 = 0x1051, + Key_F35 = 0x1052, + Key_Super_L = 0x1053, // extra keys + Key_Super_R = 0x1054, + Key_Menu = 0x1055, + Key_Hyper_L = 0x1056, + Key_Hyper_R = 0x1057, + Key_Help = 0x1058, + Key_Direction_L = 0x1059, + Key_Direction_R = 0x1060, + + // International input method support (X keycode - 0xEE00, the + // definition follows TQt/Embedded 2.3.7) Only interesting if + // you are writing your own input method + + // International & multi-key character composition + Key_Multi_key = 0x1120, // Multi-key character compose + Key_Codeinput = 0x1137, + Key_SingleCandidate = 0x113c, + Key_MultipleCandidate = 0x113d, + Key_PreviousCandidate = 0x113e, + + // Misc Functions + Key_Mode_switch = 0x117e, // Character set switch + //Key_script_switch = 0x117e, // Alias for mode_switch + + // Japanese keyboard support + Key_Kanji = 0x1121, // Kanji, Kanji convert + Key_Muhenkan = 0x1122, // Cancel Conversion + //Key_Henkan_Mode = 0x1123, // Start/Stop Conversion + Key_Henkan = 0x1123, // Alias for Henkan_Mode + Key_Romaji = 0x1124, // to Romaji + Key_Hiragana = 0x1125, // to Hiragana + Key_Katakana = 0x1126, // to Katakana + Key_Hiragana_Katakana = 0x1127, // Hiragana/Katakana toggle + Key_Zenkaku = 0x1128, // to Zenkaku + Key_Hankaku = 0x1129, // to Hankaku + Key_Zenkaku_Hankaku = 0x112a, // Zenkaku/Hankaku toggle + Key_Touroku = 0x112b, // Add to Dictionary + Key_Massyo = 0x112c, // Delete from Dictionary + Key_Kana_Lock = 0x112d, // Kana Lock + Key_Kana_Shift = 0x112e, // Kana Shift + Key_Eisu_Shift = 0x112f, // Alphanumeric Shift + Key_Eisu_toggle = 0x1130, // Alphanumeric toggle + //Key_Kanji_Bangou = 0x1137, // Codeinput + //Key_Zen_Koho = 0x113d, // Multiple/All Candidate(s) + //Key_Mae_Koho = 0x113e, // Previous Candidate + + // Korean keyboard support + // + // In fact, many Korean users need only 2 keys, Key_Hangul and + // Key_Hangul_Hanja. But rest of the keys are good for future. + + Key_Hangul = 0x1131, // Hangul start/stop(toggle) + Key_Hangul_Start = 0x1132, // Hangul start + Key_Hangul_End = 0x1133, // Hangul end, English start + Key_Hangul_Hanja = 0x1134, // Start Hangul->Hanja Conversion + Key_Hangul_Jamo = 0x1135, // Hangul Jamo mode + Key_Hangul_Romaja = 0x1136, // Hangul Romaja mode + //Key_Hangul_Codeinput = 0x1137, // Hangul code input mode + Key_Hangul_Jeonja = 0x1138, // Jeonja mode + Key_Hangul_Banja = 0x1139, // Banja mode + Key_Hangul_PreHanja = 0x113a, // Pre Hanja conversion + Key_Hangul_PostHanja = 0x113b, // Post Hanja conversion + //Key_Hangul_SingleCandidate = 0x113c, // Single candidate + //Key_Hangul_MultipleCandidate = 0x113d, // Multiple candidate + //Key_Hangul_PreviousCandidate = 0x113e, // Previous candidate + Key_Hangul_Special = 0x113f, // Special symbols + //Key_Hangul_switch = 0x117e, // Alias for mode_switch + + // dead keys (X keycode - 0xED00 to avoid the conflict) + Key_Dead_Grave = 0x1250, + Key_Dead_Acute = 0x1251, + Key_Dead_Circumflex = 0x1252, + Key_Dead_Tilde = 0x1253, + Key_Dead_Macron = 0x1254, + Key_Dead_Breve = 0x1255, + Key_Dead_Abovedot = 0x1256, + Key_Dead_Diaeresis = 0x1257, + Key_Dead_Abovering = 0x1258, + Key_Dead_Doubleacute = 0x1259, + Key_Dead_Caron = 0x125a, + Key_Dead_Cedilla = 0x125b, + Key_Dead_Ogonek = 0x125c, + Key_Dead_Iota = 0x125d, + Key_Dead_Voiced_Sound = 0x125e, + Key_Dead_Semivoiced_Sound = 0x125f, + Key_Dead_Belowdot = 0x1260, + Key_Dead_Hook = 0x1261, + Key_Dead_Horn = 0x1262, + + Key_Space = 0x20, // 7 bit printable ASCII + Key_Any = Key_Space, + Key_Exclam = 0x21, + Key_QuoteDbl = 0x22, + Key_NumberSign = 0x23, + Key_Dollar = 0x24, + Key_Percent = 0x25, + Key_Ampersand = 0x26, + Key_Apostrophe = 0x27, + Key_ParenLeft = 0x28, + Key_ParenRight = 0x29, + Key_Asterisk = 0x2a, + Key_Plus = 0x2b, + Key_Comma = 0x2c, + Key_Minus = 0x2d, + Key_Period = 0x2e, + Key_Slash = 0x2f, + Key_0 = 0x30, + Key_1 = 0x31, + Key_2 = 0x32, + Key_3 = 0x33, + Key_4 = 0x34, + Key_5 = 0x35, + Key_6 = 0x36, + Key_7 = 0x37, + Key_8 = 0x38, + Key_9 = 0x39, + Key_Colon = 0x3a, + Key_Semicolon = 0x3b, + Key_Less = 0x3c, + Key_Equal = 0x3d, + Key_Greater = 0x3e, + Key_Question = 0x3f, + Key_At = 0x40, + Key_A = 0x41, + Key_B = 0x42, + Key_C = 0x43, + Key_D = 0x44, + Key_E = 0x45, + Key_F = 0x46, + Key_G = 0x47, + Key_H = 0x48, + Key_I = 0x49, + Key_J = 0x4a, + Key_K = 0x4b, + Key_L = 0x4c, + Key_M = 0x4d, + Key_N = 0x4e, + Key_O = 0x4f, + Key_P = 0x50, + Key_Q = 0x51, + Key_R = 0x52, + Key_S = 0x53, + Key_T = 0x54, + Key_U = 0x55, + Key_V = 0x56, + Key_W = 0x57, + Key_X = 0x58, + Key_Y = 0x59, + Key_Z = 0x5a, + Key_BracketLeft = 0x5b, + Key_Backslash = 0x5c, + Key_BracketRight = 0x5d, + Key_AsciiCircum = 0x5e, + Key_Underscore = 0x5f, + Key_QuoteLeft = 0x60, + Key_BraceLeft = 0x7b, + Key_Bar = 0x7c, + Key_BraceRight = 0x7d, + Key_AsciiTilde = 0x7e, + + // Latin 1 codes adapted from X: keysymdef.h,v 1.21 94/08/28 16:17:06 + // + // This is mainly for compatibility - applications and input + // methods should not use the TQt keycodes between 128 and 255, + // but should rather use the TQKeyEvent::text(). See + // TQETWidget::translateKeyEventInternal() for more details. + + Key_nobreakspace = 0x0a0, + Key_exclamdown = 0x0a1, + Key_cent = 0x0a2, + Key_sterling = 0x0a3, + Key_currency = 0x0a4, + Key_yen = 0x0a5, + Key_brokenbar = 0x0a6, + Key_section = 0x0a7, + Key_diaeresis = 0x0a8, + Key_copyright = 0x0a9, + Key_ordfeminine = 0x0aa, + Key_guillemotleft = 0x0ab, // left angle quotation mark + Key_notsign = 0x0ac, + Key_hyphen = 0x0ad, + Key_registered = 0x0ae, + Key_macron = 0x0af, + Key_degree = 0x0b0, + Key_plusminus = 0x0b1, + Key_twosuperior = 0x0b2, + Key_threesuperior = 0x0b3, + Key_acute = 0x0b4, + Key_mu = 0x0b5, + Key_paragraph = 0x0b6, + Key_periodcentered = 0x0b7, + Key_cedilla = 0x0b8, + Key_onesuperior = 0x0b9, + Key_masculine = 0x0ba, + Key_guillemotright = 0x0bb, // right angle quotation mark + Key_onequarter = 0x0bc, + Key_onehalf = 0x0bd, + Key_threequarters = 0x0be, + Key_questiondown = 0x0bf, + Key_Agrave = 0x0c0, + Key_Aacute = 0x0c1, + Key_Acircumflex = 0x0c2, + Key_Atilde = 0x0c3, + Key_Adiaeresis = 0x0c4, + Key_Aring = 0x0c5, + Key_AE = 0x0c6, + Key_Ccedilla = 0x0c7, + Key_Egrave = 0x0c8, + Key_Eacute = 0x0c9, + Key_Ecircumflex = 0x0ca, + Key_Ediaeresis = 0x0cb, + Key_Igrave = 0x0cc, + Key_Iacute = 0x0cd, + Key_Icircumflex = 0x0ce, + Key_Idiaeresis = 0x0cf, + Key_ETH = 0x0d0, + Key_Ntilde = 0x0d1, + Key_Ograve = 0x0d2, + Key_Oacute = 0x0d3, + Key_Ocircumflex = 0x0d4, + Key_Otilde = 0x0d5, + Key_Odiaeresis = 0x0d6, + Key_multiply = 0x0d7, + Key_Ooblique = 0x0d8, + Key_Ugrave = 0x0d9, + Key_Uacute = 0x0da, + Key_Ucircumflex = 0x0db, + Key_Udiaeresis = 0x0dc, + Key_Yacute = 0x0dd, + Key_THORN = 0x0de, + Key_ssharp = 0x0df, + Key_agrave = 0x0e0, + Key_aacute = 0x0e1, + Key_acircumflex = 0x0e2, + Key_atilde = 0x0e3, + Key_adiaeresis = 0x0e4, + Key_aring = 0x0e5, + Key_ae = 0x0e6, + Key_ccedilla = 0x0e7, + Key_egrave = 0x0e8, + Key_eacute = 0x0e9, + Key_ecircumflex = 0x0ea, + Key_ediaeresis = 0x0eb, + Key_igrave = 0x0ec, + Key_iacute = 0x0ed, + Key_icircumflex = 0x0ee, + Key_idiaeresis = 0x0ef, + Key_eth = 0x0f0, + Key_ntilde = 0x0f1, + Key_ograve = 0x0f2, + Key_oacute = 0x0f3, + Key_ocircumflex = 0x0f4, + Key_otilde = 0x0f5, + Key_odiaeresis = 0x0f6, + Key_division = 0x0f7, + Key_oslash = 0x0f8, + Key_ugrave = 0x0f9, + Key_uacute = 0x0fa, + Key_ucircumflex = 0x0fb, + Key_udiaeresis = 0x0fc, + Key_yacute = 0x0fd, + Key_thorn = 0x0fe, + Key_ydiaeresis = 0x0ff, + + // multimedia/internet keys - ignored by default - see TQKeyEvent c'tor + + Key_Back = 0x1061, + Key_Forward = 0x1062, + Key_Stop = 0x1063, + Key_Refresh = 0x1064, + + Key_VolumeDown = 0x1070, + Key_VolumeMute = 0x1071, + Key_VolumeUp = 0x1072, + Key_BassBoost = 0x1073, + Key_BassUp = 0x1074, + Key_BassDown = 0x1075, + Key_TrebleUp = 0x1076, + Key_TrebleDown = 0x1077, + + Key_MediaPlay = 0x1080, + Key_MediaStop = 0x1081, + Key_MediaPrev = 0x1082, + Key_MediaNext = 0x1083, + Key_MediaRecord = 0x1084, + + Key_HomePage = 0x1090, + Key_Favorites = 0x1091, + Key_Search = 0x1092, + Key_Standby = 0x1093, + Key_OpenUrl = 0x1094, + + Key_LaunchMail = 0x10a0, + Key_LaunchMedia = 0x10a1, + Key_Launch0 = 0x10a2, + Key_Launch1 = 0x10a3, + Key_Launch2 = 0x10a4, + Key_Launch3 = 0x10a5, + Key_Launch4 = 0x10a6, + Key_Launch5 = 0x10a7, + Key_Launch6 = 0x10a8, + Key_Launch7 = 0x10a9, + Key_Launch8 = 0x10aa, + Key_Launch9 = 0x10ab, + Key_LaunchA = 0x10ac, + Key_LaunchB = 0x10ad, + Key_LaunchC = 0x10ae, + Key_LaunchD = 0x10af, + Key_LaunchE = 0x10b0, + Key_LaunchF = 0x10b1, + + Key_MediaLast = 0x1fff, + + Key_unknown = 0xffff + }; + + // documented in qcommonstyle.cpp + enum ArrowType { + UpArrow, + DownArrow, + LeftArrow, + RightArrow + }; + + // documented in qpainter.cpp + enum RasterOp { // raster op mode + CopyROP, + OrROP, + XorROP, + NotAndROP, EraseROP=NotAndROP, + NotCopyROP, + NotOrROP, + NotXorROP, + AndROP, NotEraseROP=AndROP, + NotROP, + ClearROP, + SetROP, + NopROP, + AndNotROP, + OrNotROP, + NandROP, + NorROP, LastROP=NorROP + }; + + // documented in qpainter.cpp + enum PenStyle { // pen style + NoPen, + SolidLine, + DashLine, + DotLine, + DashDotLine, + DashDotDotLine, + MPenStyle = 0x0f + }; + + // documented in qpainter.cpp + enum PenCapStyle { // line endcap style + FlatCap = 0x00, + SquareCap = 0x10, + RoundCap = 0x20, + MPenCapStyle = 0x30 + }; + + // documented in qpainter.cpp + enum PenJoinStyle { // line join style + MiterJoin = 0x00, + BevelJoin = 0x40, + RoundJoin = 0x80, + MPenJoinStyle = 0xc0 + }; + + // documented in qpainter.cpp + enum BrushStyle { // brush style + NoBrush, + SolidPattern, + Dense1Pattern, + Dense2Pattern, + Dense3Pattern, + Dense4Pattern, + Dense5Pattern, + Dense6Pattern, + Dense7Pattern, + HorPattern, + VerPattern, + CrossPattern, + BDiagPattern, + FDiagPattern, + DiagCrossPattern, + CustomPattern=24 + }; + + // documented in qapplication_mac.cpp + enum MacintoshVersion { + //Unknown + MV_Unknown = 0x0000, + + //Version numbers + MV_9 = 0x0001, + MV_10_DOT_0 = 0x0002, + MV_10_DOT_1 = 0x0003, + MV_10_DOT_2 = 0x0004, + MV_10_DOT_3 = 0x0005, + MV_10_DOT_4 = 0x0006, + + //Code names + MV_CHEETAH = MV_10_DOT_0, + MV_PUMA = MV_10_DOT_1, + MV_JAGUAR = MV_10_DOT_2, + MV_PANTHER = MV_10_DOT_3, + MV_TIGER = MV_10_DOT_4 + }; + + // documented in qapplication_win.cpp + enum WindowsVersion { + WV_32s = 0x0001, + WV_95 = 0x0002, + WV_98 = 0x0003, + WV_Me = 0x0004, + WV_DOS_based = 0x000f, + + WV_NT = 0x0010, + WV_2000 = 0x0020, + WV_XP = 0x0030, + WV_2003 = 0x0040, + WV_VISTA = 0x0080, + WV_NT_based = 0x00f0, + + WV_CE = 0x0100, + WV_CENET = 0x0200, + WV_CE_based = 0x0f00 + }; + + // documented in qstyle.cpp + enum UIEffect { + UI_General, + UI_AnimateMenu, + UI_FadeMenu, + UI_AnimateCombo, + UI_AnimateTooltip, + UI_FadeTooltip, + UI_AnimateToolBox + }; + + // documented in qcursor.cpp + enum CursorShape { + ArrowCursor, + UpArrowCursor, + CrossCursor, + WaitCursor, + IbeamCursor, + SizeVerCursor, + SizeHorCursor, + SizeBDiagCursor, + SizeFDiagCursor, + SizeAllCursor, + BlankCursor, + SplitVCursor, + SplitHCursor, + PointingHandCursor, + ForbiddenCursor, + WhatsThisCursor, + BusyCursor, + LastCursor = BusyCursor, + BitmapCursor = 24 + }; + + // Global cursors + + QT_STATIC_CONST TQCursor & arrowCursor; // standard arrow cursor + QT_STATIC_CONST TQCursor & upArrowCursor; // upwards arrow + QT_STATIC_CONST TQCursor & crossCursor; // crosshair + QT_STATIC_CONST TQCursor & waitCursor; // hourglass/watch + QT_STATIC_CONST TQCursor & ibeamCursor; // ibeam/text entry + QT_STATIC_CONST TQCursor & sizeVerCursor; // vertical resize + QT_STATIC_CONST TQCursor & sizeHorCursor; // horizontal resize + QT_STATIC_CONST TQCursor & sizeBDiagCursor; // diagonal resize (/) + QT_STATIC_CONST TQCursor & sizeFDiagCursor; // diagonal resize (\) + QT_STATIC_CONST TQCursor & sizeAllCursor; // all directions resize + QT_STATIC_CONST TQCursor & blankCursor; // blank/invisible cursor + QT_STATIC_CONST TQCursor & splitVCursor; // vertical bar with left-right + // arrows + QT_STATIC_CONST TQCursor & splitHCursor; // horizontal bar with up-down + // arrows + QT_STATIC_CONST TQCursor & pointingHandCursor; // pointing hand + QT_STATIC_CONST TQCursor & forbiddenCursor; // forbidden cursor (slashed circle) + QT_STATIC_CONST TQCursor & whatsThisCursor; // arrow with a question mark + QT_STATIC_CONST TQCursor & busyCursor; // arrow with hourglass + + + enum TextFormat { + PlainText, + RichText, + AutoText, + LogText + }; + + // Documented in qtextedit.cpp + enum AnchorAttribute { + AnchorName, + AnchorHref + }; + + // Documented in qmainwindow.cpp + enum Dock { + DockUnmanaged, + DockTornOff, + DockTop, + DockBottom, + DockRight, + DockLeft, + DockMinimized +#ifndef QT_NO_COMPAT + , + Unmanaged = DockUnmanaged, + TornOff = DockTornOff, + Top = DockTop, + Bottom = DockBottom, + Right = DockRight, + Left = DockLeft, + Minimized = DockMinimized +#endif + }; + // compatibility + typedef Dock ToolBarDock; + + // documented in qdatetime.cpp + enum DateFormat { + TextDate, // default TQt + ISODate, // ISO 8601 + LocalDate // locale dependent + }; + + // documented in qdatetime.cpp + enum TimeSpec { + LocalTime, + UTC + }; + + // documented in qwidget.cpp + enum BackgroundMode { + FixedColor, + FixedPixmap, + NoBackground, + PaletteForeground, + PaletteButton, + PaletteLight, + PaletteMidlight, + PaletteDark, + PaletteMid, + PaletteText, + PaletteBrightText, + PaletteBase, + PaletteBackground, + PaletteShadow, + PaletteHighlight, + PaletteHighlightedText, + PaletteButtonText, + PaletteLink, + PaletteLinkVisited, + X11ParentRelative + }; + + typedef uint ComparisonFlags; + + // Documented in qstring.cpp + enum StringComparisonMode { + CaseSensitive = 0x00001, // 0 0001 + BeginsWith = 0x00002, // 0 0010 + EndsWith = 0x00004, // 0 0100 + Contains = 0x00008, // 0 1000 + ExactMatch = 0x00010 // 1 0000 + }; + + // Documented in qtabwidget.cpp + enum Corner { + TopLeft = 0x00000, + TopRight = 0x00001, + BottomLeft = 0x00002, + BottomRight = 0x00003 + }; + + // "handle" type for system objects. Documented as \internal in + // qapplication.cpp +#if defined(Q_WS_MAC) + typedef void * HANDLE; +#elif defined(Q_WS_WIN) + typedef void *HANDLE; +#elif defined(Q_WS_X11) + typedef unsigned long HANDLE; +#elif defined(Q_WS_QWS) + typedef void * HANDLE; +#endif +}; + + +class Q_EXPORT TQInternal { +public: + enum PaintDeviceFlags { + UndefinedDevice = 0x00, + Widget = 0x01, + Pixmap = 0x02, + Printer = 0x03, + Picture = 0x04, + System = 0x05, + DeviceTypeMask = 0x0f, + ExternalDevice = 0x10, + // used to emulate some of the behaviour different between TQt2 and TQt3 (mainly for printing) + CompatibilityMode = 0x20 + }; +}; + +#endif // TQNAMESPACE_H diff --git a/src/kernel/qnetworkprotocol.cpp b/src/kernel/qnetworkprotocol.cpp new file mode 100644 index 000000000..8f095faea --- /dev/null +++ b/src/kernel/qnetworkprotocol.cpp @@ -0,0 +1,1265 @@ +/**************************************************************************** +** +** Implementation of TQNetworkProtocol class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qnetworkprotocol.h" + +#ifndef QT_NO_NETWORKPROTOCOL + +#include "qlocalfs.h" +#include "qurloperator.h" +#include "qtimer.h" +#include "qmap.h" +#include "qptrqueue.h" + +//#define TQNETWORKPROTOCOL_DEBUG +#define NETWORK_OP_DELAY 1000 + +extern Q_EXPORT TQNetworkProtocolDict *qNetworkProtocolRegister; + +TQNetworkProtocolDict *qNetworkProtocolRegister = 0; + +class TQNetworkProtocolPrivate +{ +public: + TQNetworkProtocolPrivate( TQNetworkProtocol *p ) + { + url = 0; + opInProgress = 0; + opStartTimer = new TQTimer( p ); + removeTimer = new TQTimer( p ); + operationQueue.setAutoDelete( FALSE ); + autoDelete = FALSE; + removeInterval = 10000; + oldOps.setAutoDelete( FALSE ); + } + + ~TQNetworkProtocolPrivate() + { + removeTimer->stop(); + if ( opInProgress ) { + if ( opInProgress == operationQueue.head() ) + operationQueue.dequeue(); + opInProgress->free(); + } + while ( operationQueue.head() ) { + operationQueue.head()->free(); + operationQueue.dequeue(); + } + while ( oldOps.first() ) { + oldOps.first()->free(); + oldOps.removeFirst(); + } + delete opStartTimer; + } + + TQUrlOperator *url; + TQPtrQueue< TQNetworkOperation > operationQueue; + TQNetworkOperation *opInProgress; + TQTimer *opStartTimer, *removeTimer; + int removeInterval; + bool autoDelete; + TQPtrList< TQNetworkOperation > oldOps; +}; + +/*! + \class TQNetworkProtocol qnetworkprotocol.h + \brief The TQNetworkProtocol class provides a common API for network protocols. +\if defined(commercial) + It is part of the TQt Enterprise Edition. +\endif + + \module network + \ingroup io + \module network + \mainclass + + This is a base class which should be used for network protocols + implementations that can then be used in TQt (e.g. in the file + dialog) together with the TQUrlOperator. + + The easiest way to implement a new network protocol is to + reimplement the operation*() methods, e.g. operationGet(), etc. + Only the supported operations should be reimplemented. To specify + which operations are supported, also reimplement + supportedOperations() and return an int that is OR'd together + using the supported operations from the \l + TQNetworkProtocol::Operation enum. + + When you implement a network protocol this way, it is important to + emit the correct signals. Also, always emit the finished() signal + when an operation is done (on success \e and on failure). TQt + relies on correctly emitted finished() signals. + + For a detailed description of the TQt Network Architecture and how + to implement and use network protocols in TQt, see the \link + network.html TQt Network Documentation\endlink. +*/ + +/*! + \fn void TQNetworkProtocol::newChildren( const TQValueList &i, TQNetworkOperation *op ) + + This signal is emitted after listChildren() was called and new + children (files) have been read from the list of files. \a i holds + the information about the new children. \a op is the pointer to + the operation object which contains all the information about the + operation, including the state, etc. + + When a protocol emits this signal, TQNetworkProtocol is smart + enough to let the TQUrlOperator, which is used by the network + protocol, emit its corresponding signal. + + When implementing your own network protocol and reading children, + you usually don't read one child at once, but rather a list of + them. That's why this signal takes a list of TQUrlInfo objects. If + you prefer to read just one child at a time you can use the + convenience signal newChild(), which takes a single TQUrlInfo + object. +*/ + +/*! + \fn void TQNetworkProtocol::newChild( const TQUrlInfo &i, TQNetworkOperation *op ) + + This signal is emitted if a new child (file) has been read. + TQNetworkProtocol automatically connects it to a slot which creates + a list of TQUrlInfo objects (with just one TQUrlInfo \a i) and emits + the newChildren() signal with this list. \a op is the pointer to + the operation object which contains all the information about the + operation that has finished, including the state, etc. + + This is just a convenience signal useful for implementing your own + network protocol. In all other cases connect to the newChildren() + signal with its list of TQUrlInfo objects. +*/ + +/*! + \fn void TQNetworkProtocol::finished( TQNetworkOperation *op ) + + This signal is emitted when an operation finishes. This signal is + always emitted, for both success and failure. \a op is the pointer + to the operation object which contains all the information about + the operation, including the state, etc. Check the state and error + code of the operation object to determine whether or not the + operation was successful. + + When a protocol emits this signal, TQNetworkProtocol is smart + enough to let the TQUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void TQNetworkProtocol::start( TQNetworkOperation *op ) + + Some operations (such as listChildren()) emit this signal when + they start processing the operation. \a op is the pointer to the + operation object which contains all the information about the + operation, including the state, etc. + + When a protocol emits this signal, TQNetworkProtocol is smart + enough to let the TQUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void TQNetworkProtocol::createdDirectory( const TQUrlInfo &i, TQNetworkOperation *op ) + + This signal is emitted when mkdir() has been succesful and the + directory has been created. \a i holds the information about the + new directory. \a op is the pointer to the operation object which + contains all the information about the operation, including the + state, etc. Using op->arg( 0 ), you can get the file name of the + new directory. + + When a protocol emits this signal, TQNetworkProtocol is smart + enough to let the TQUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void TQNetworkProtocol::removed( TQNetworkOperation *op ) + + This signal is emitted when remove() has been succesful and the + file has been removed. \a op holds the file name of the removed + file in the first argument, accessible with op->arg( 0 ). \a op is + the pointer to the operation object which contains all the + information about the operation, including the state, etc. + + When a protocol emits this signal, TQNetworkProtocol is smart + enough to let the TQUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void TQNetworkProtocol::itemChanged( TQNetworkOperation *op ) + + This signal is emitted whenever a file which is a child of this + URL has been changed, e.g. by successfully calling rename(). \a op + holds the original and the new file names in the first and second + arguments, accessible with op->arg( 0 ) and op->arg( 1 ) + respectively. \a op is the pointer to the operation object which + contains all the information about the operation, including the + state, etc. + + When a protocol emits this signal, TQNetworkProtocol is smart + enough to let the TQUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void TQNetworkProtocol::data( const TQByteArray &data, + TQNetworkOperation *op ) + + This signal is emitted when new \a data has been received after + calling get() or put(). \a op holds the name of the file from + which data is retrieved or uploaded in its first argument, and the + (raw) data in its second argument. You can get them with + op->arg( 0 ) and op->rawArg( 1 ). \a op is the pointer to the + operation object, which contains all the information about the + operation, including the state, etc. + + When a protocol emits this signal, TQNetworkProtocol is smart + enough to let the TQUrlOperator (which is used by the network + protocol) emit its corresponding signal. +*/ + +/*! + \fn void TQNetworkProtocol::dataTransferProgress( int bytesDone, int bytesTotal, TQNetworkOperation *op ) + + This signal is emitted during the transfer of data (using put() or + get()). \a bytesDone is how many bytes of \a bytesTotal have been + transferred. \a bytesTotal may be -1, which means that the total + number of bytes is not known. \a op is the pointer to the + operation object which contains all the information about the + operation, including the state, etc. + + When a protocol emits this signal, TQNetworkProtocol is smart + enough to let the TQUrlOperator, which is used by the network + protocol, emit its corresponding signal. +*/ + +/*! + \fn void TQNetworkProtocol::connectionStateChanged( int state, const TQString &data ) + + This signal is emitted whenever the state of the connection of the + network protocol is changed. \a state describes the new state, + which is one of, \c ConHostFound, \c ConConnected or \c ConClosed. + \a data is a message text. +*/ + +/*! + \enum TQNetworkProtocol::State + + This enum contains the state that a TQNetworkOperation can have. + + \value StWaiting The operation is in the TQNetworkProtocol's queue + waiting to be prcessed. + + \value StInProgress The operation is being processed. + + \value StDone The operation has been processed succesfully. + + \value StFailed The operation has been processed but an error occurred. + + \value StStopped The operation has been processed but has been + stopped before it finished, and is waiting to be processed. + +*/ + +/*! + \enum TQNetworkProtocol::Operation + + This enum lists the possible operations that a network protocol + can support. supportedOperations() returns an int of these that is + OR'd together. Also, the type() of a TQNetworkOperation is always + one of these values. + + \value OpListChildren List the children of a URL, e.g. of a directory. + \value OpMkDir Create a directory. + \value OpRemove Remove a child (e.g. a file). + \value OpRename Rename a child (e.g. a file). + \value OpGet Get data from a location. + \value OpPut Put data to a location. +*/ + +/*! + \enum TQNetworkProtocol::ConnectionState + + When the connection state of a network protocol changes it emits + the signal connectionStateChanged(). The first argument is one of + the following values: + + \value ConHostFound Host has been found. + \value ConConnected Connection to the host has been established. + \value ConClosed Connection has been closed. +*/ + +/*! + \enum TQNetworkProtocol::Error + + When an operation fails (finishes unsuccessfully), the + TQNetworkOperation of the operation returns an error code which has + one of the following values: + + \value NoError No error occurred. + + \value ErrValid The URL you are operating on is not valid. + + \value ErrUnknownProtocol There is no protocol implementation + available for the protocol of the URL you are operating on (e.g. + if the protocol is http and no http implementation has been + registered). + + \value ErrUnsupported The operation is not supported by the + protocol. + + \value ErrParse The URL could not be parsed correctly. + + \value ErrLoginIncorrect You needed to login but the username + or password is wrong. + + \value ErrHostNotFound The specified host (in the URL) couldn't + be found. + + \value ErrListChildren An error occurred while listing the + children (files). + + \value ErrMkDir An error occurred when creating a directory. + + \value ErrRemove An error occurred when removing a child (file). + + \value ErrRename An error occurred when renaming a child (file). + + \value ErrGet An error occurred while getting (retrieving) data. + + \value ErrPut An error occurred while putting (uploading) data. + + \value ErrFileNotExisting A file which is needed by the operation + doesn't exist. + + \value ErrPermissionDenied Permission for doing the operation has + been denied. + + You should also use these error codes when implementing custom + network protocols. If this is not possible, you can define your own + error codes by using integer values that don't conflict with any + of these values. +*/ + +/*! + Constructor of the network protocol base class. Does some + initialization and connecting of signals and slots. +*/ + +TQNetworkProtocol::TQNetworkProtocol() + : TQObject() +{ + d = new TQNetworkProtocolPrivate( this ); + + connect( d->opStartTimer, SIGNAL( timeout() ), + this, SLOT( startOps() ) ); + connect( d->removeTimer, SIGNAL( timeout() ), + this, SLOT( removeMe() ) ); + + if ( url() ) { + connect( this, SIGNAL( data(const TQByteArray&,TQNetworkOperation*) ), + url(), SIGNAL( data(const TQByteArray&,TQNetworkOperation*) ) ); + connect( this, SIGNAL( finished(TQNetworkOperation*) ), + url(), SIGNAL( finished(TQNetworkOperation*) ) ); + connect( this, SIGNAL( start(TQNetworkOperation*) ), + url(), SIGNAL( start(TQNetworkOperation*) ) ); + connect( this, SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ), + url(), SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ) ); + connect( this, SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ), + url(), SLOT( addEntry(const TQValueList&) ) ); + connect( this, SIGNAL( createdDirectory(const TQUrlInfo&,TQNetworkOperation*) ), + url(), SIGNAL( createdDirectory(const TQUrlInfo&,TQNetworkOperation*) ) ); + connect( this, SIGNAL( removed(TQNetworkOperation*) ), + url(), SIGNAL( removed(TQNetworkOperation*) ) ); + connect( this, SIGNAL( itemChanged(TQNetworkOperation*) ), + url(), SIGNAL( itemChanged(TQNetworkOperation*) ) ); + connect( this, SIGNAL( dataTransferProgress(int,int,TQNetworkOperation*) ), + url(), SIGNAL( dataTransferProgress(int,int,TQNetworkOperation*) ) ); + connect( this, SIGNAL( connectionStateChanged(int,const TQString&) ), + url(), SIGNAL( connectionStateChanged(int,const TQString&) ) ); + } + + connect( this, SIGNAL( finished(TQNetworkOperation*) ), + this, SLOT( processNextOperation(TQNetworkOperation*) ) ); + connect( this, SIGNAL( newChild(const TQUrlInfo&,TQNetworkOperation*) ), + this, SLOT( emitNewChildren(const TQUrlInfo&,TQNetworkOperation*) ) ); + +} + +/*! + Destructor. +*/ + +TQNetworkProtocol::~TQNetworkProtocol() +{ + delete d; +} + +/*! + Sets the TQUrlOperator, on which the protocol works, to \a u. + + \sa TQUrlOperator +*/ + +void TQNetworkProtocol::setUrl( TQUrlOperator *u ) +{ + if ( url() ) { + disconnect( this, SIGNAL( data(const TQByteArray&,TQNetworkOperation*) ), + url(), SIGNAL( data(const TQByteArray&,TQNetworkOperation*) ) ); + disconnect( this, SIGNAL( finished(TQNetworkOperation*) ), + url(), SIGNAL( finished(TQNetworkOperation*) ) ); + disconnect( this, SIGNAL( start(TQNetworkOperation*) ), + url(), SIGNAL( start(TQNetworkOperation*) ) ); + disconnect( this, SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ), + url(), SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ) ); + disconnect( this, SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ), + url(), SLOT( addEntry(const TQValueList&) ) ); + disconnect( this, SIGNAL( createdDirectory(const TQUrlInfo&,TQNetworkOperation*) ), + url(), SIGNAL( createdDirectory(const TQUrlInfo&,TQNetworkOperation*) ) ); + disconnect( this, SIGNAL( removed(TQNetworkOperation*) ), + url(), SIGNAL( removed(TQNetworkOperation*) ) ); + disconnect( this, SIGNAL( itemChanged(TQNetworkOperation*) ), + url(), SIGNAL( itemChanged(TQNetworkOperation*) ) ); + disconnect( this, SIGNAL( dataTransferProgress(int,int,TQNetworkOperation*) ), + url(), SIGNAL( dataTransferProgress(int,int,TQNetworkOperation*) ) ); + disconnect( this, SIGNAL( connectionStateChanged(int,const TQString&) ), + url(), SIGNAL( connectionStateChanged(int,const TQString&) ) ); + } + + + // ### if autoDelete is TRUE, we should delete the TQUrlOperator (something + // like below; but that is not possible since it would delete this, too). + //if ( d->autoDelete && (d->url!=u) ) { + // delete d->url; // destructor deletes the network protocol + //} + d->url = u; + + if ( url() ) { + connect( this, SIGNAL( data(const TQByteArray&,TQNetworkOperation*) ), + url(), SIGNAL( data(const TQByteArray&,TQNetworkOperation*) ) ); + connect( this, SIGNAL( finished(TQNetworkOperation*) ), + url(), SIGNAL( finished(TQNetworkOperation*) ) ); + connect( this, SIGNAL( start(TQNetworkOperation*) ), + url(), SIGNAL( start(TQNetworkOperation*) ) ); + connect( this, SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ), + url(), SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ) ); + connect( this, SIGNAL( newChildren(const TQValueList&,TQNetworkOperation*) ), + url(), SLOT( addEntry(const TQValueList&) ) ); + connect( this, SIGNAL( createdDirectory(const TQUrlInfo&,TQNetworkOperation*) ), + url(), SIGNAL( createdDirectory(const TQUrlInfo&,TQNetworkOperation*) ) ); + connect( this, SIGNAL( removed(TQNetworkOperation*) ), + url(), SIGNAL( removed(TQNetworkOperation*) ) ); + connect( this, SIGNAL( itemChanged(TQNetworkOperation*) ), + url(), SIGNAL( itemChanged(TQNetworkOperation*) ) ); + connect( this, SIGNAL( dataTransferProgress(int,int,TQNetworkOperation*) ), + url(), SIGNAL( dataTransferProgress(int,int,TQNetworkOperation*) ) ); + connect( this, SIGNAL( connectionStateChanged(int,const TQString&) ), + url(), SIGNAL( connectionStateChanged(int,const TQString&) ) ); + } + + if ( !d->opInProgress && !d->operationQueue.isEmpty() ) + d->opStartTimer->start( 0, TRUE ); +} + +/*! + For processing operations the network protocol base class calls + this method tquite often. This should be reimplemented by new + network protocols. It should return TRUE if the connection is OK + (open); otherwise it should return FALSE. If the connection is not + open the protocol should open it. + + If the connection can't be opened (e.g. because you already tried + but the host couldn't be found), set the state of \a op to + TQNetworkProtocol::StFailed and emit the finished() signal with + this TQNetworkOperation as argument. + + \a op is the operation that needs an open connection. +*/ + +bool TQNetworkProtocol::checkConnection( TQNetworkOperation * ) +{ + return TRUE; +} + +/*! + Returns an int that is OR'd together using the enum values of + \l{TQNetworkProtocol::Operation}, which describes which operations + are supported by the network protocol. Should be reimplemented by + new network protocols. +*/ + +int TQNetworkProtocol::supportedOperations() const +{ + return 0; +} + +/*! + Adds the operation \a op to the operation queue. The operation + will be processed as soon as possible. This method returns + immediately. +*/ + +void TQNetworkProtocol::addOperation( TQNetworkOperation *op ) +{ +#ifdef TQNETWORKPROTOCOL_DEBUG + qDebug( "TQNetworkOperation: addOperation: %p %d", op, op->operation() ); +#endif + d->operationQueue.enqueue( op ); + if ( !d->opInProgress ) + d->opStartTimer->start( 0, TRUE ); +} + +/*! + Static method to register a network protocol for TQt. For example, + if you have an implementation of NNTP (called Nntp) which is + derived from TQNetworkProtocol, call: + \code + TQNetworkProtocol::registerNetworkProtocol( "nntp", new TQNetworkProtocolFactory ); + \endcode + after which your implementation is registered for future nntp + operations. + + The name of the protocol is given in \a protocol and a pointer to + the protocol factory is given in \a protocolFactory. +*/ + +void TQNetworkProtocol::registerNetworkProtocol( const TQString &protocol, + TQNetworkProtocolFactoryBase *protocolFactory ) +{ + if ( !qNetworkProtocolRegister ) { + qNetworkProtocolRegister = new TQNetworkProtocolDict; + TQNetworkProtocol::registerNetworkProtocol( "file", new TQNetworkProtocolFactory< TQLocalFs > ); + } + + qNetworkProtocolRegister->insert( protocol, protocolFactory ); +} + +/*! + Static method to get a new instance of the network protocol \a + protocol. For example, if you need to do some FTP operations, do + the following: + \code + TQFtp *ftp = TQNetworkProtocol::getNetworkProtocol( "ftp" ); + \endcode + This returns a pointer to a new instance of an ftp implementation + or null if no protocol for ftp was registered. The ownership of + the pointer is transferred to you, so you must delete it if you + don't need it anymore. + + Normally you should not work directly with network protocols, so + you will not need to call this method yourself. Instead, use + TQUrlOperator, which makes working with network protocols much more + convenient. + + \sa TQUrlOperator +*/ + +TQNetworkProtocol *TQNetworkProtocol::getNetworkProtocol( const TQString &protocol ) +{ + if ( !qNetworkProtocolRegister ) { + qNetworkProtocolRegister = new TQNetworkProtocolDict; + TQNetworkProtocol::registerNetworkProtocol( "file", new TQNetworkProtocolFactory< TQLocalFs > ); + } + + if ( protocol.isNull() ) + return 0; + + TQNetworkProtocolFactoryBase *factory = qNetworkProtocolRegister->find( protocol ); + if ( factory ) + return factory->createObject(); + + return 0; +} + +/*! + Returns TRUE if the only protocol registered is for working on the + local filesystem; returns FALSE if other network protocols are + also registered. +*/ + +bool TQNetworkProtocol::hasOnlyLocalFileSystem() +{ + if ( !qNetworkProtocolRegister ) + return FALSE; + + TQDictIterator< TQNetworkProtocolFactoryBase > it( *qNetworkProtocolRegister ); + for ( ; it.current(); ++it ) + if ( it.currentKey() != "file" ) + return FALSE; + return TRUE; +} + +/*! + \internal + Starts processing network operations. +*/ + +void TQNetworkProtocol::startOps() +{ +#ifdef TQNETWORKPROTOCOL_DEBUG + qDebug( "TQNetworkOperation: start processing operations" ); +#endif + processNextOperation( 0 ); +} + +/*! + \internal + Processes the operation \a op. It calls the + corresponding operation[something]( TQNetworkOperation * ) + methods. +*/ + +void TQNetworkProtocol::processOperation( TQNetworkOperation *op ) +{ + if ( !op ) + return; + + switch ( op->operation() ) { + case OpListChildren: + operationListChildren( op ); + break; + case OpMkDir: + operationMkDir( op ); + break; + case OpRemove: + operationRemove( op ); + break; + case OpRename: + operationRename( op ); + break; + case OpGet: + operationGet( op ); + break; + case OpPut: + operationPut( op ); + break; + } +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports listing children (files); + this method should then process this TQNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html TQt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void TQNetworkProtocol::operationListChildren( TQNetworkOperation * ) +{ +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports making directories; this + method should then process this TQNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html TQt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void TQNetworkProtocol::operationMkDir( TQNetworkOperation * ) +{ +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports removing children (files); + this method should then process this TQNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html TQt Network Documentation\endlink which is describes + in detail how to reimplement this method. You may also want to + look at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void TQNetworkProtocol::operationRemove( TQNetworkOperation * ) +{ +} + +/*! + When implementing a new newtork protocol, this method should be + reimplemented if the protocol supports renaming children (files); + this method should then process this TQNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html TQt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void TQNetworkProtocol::operationRename( TQNetworkOperation * ) +{ +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports getting data; this method + should then process the TQNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html TQt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void TQNetworkProtocol::operationGet( TQNetworkOperation * ) +{ +} + +/*! + When implementing a new network protocol, this method should be + reimplemented if the protocol supports putting (uploading) data; + this method should then process the TQNetworkOperation. + + When you reimplement this method it's very important that you emit + the correct signals at the correct time (especially the finished() + signal after processing an operation). Take a look at the \link + network.html TQt Network Documentation\endlink which describes in + detail how to reimplement this method. You may also want to look + at the example implementation in + examples/network/networkprotocol/nntp.cpp. + + \a op is the pointer to the operation object which contains all + the information on the operation that has finished, including the + state, etc. +*/ + +void TQNetworkProtocol::operationPut( TQNetworkOperation * ) +{ +} + +/*! \internal +*/ + +void TQNetworkProtocol::operationPutChunk( TQNetworkOperation * ) +{ +} + +/*! + \internal + Handles operations. Deletes the previous operation object and + tries to process the next operation. It also checks the connection state + and only processes the next operation, if the connection of the protocol + is open. Otherwise it waits until the protocol opens the connection. +*/ + +void TQNetworkProtocol::processNextOperation( TQNetworkOperation *old ) +{ +#ifdef TQNETWORKPROTOCOL_DEBUG + qDebug( "TQNetworkOperation: process next operation, old: %p", old ); +#endif + d->removeTimer->stop(); + + if ( old ) + d->oldOps.append( old ); + if ( d->opInProgress && d->opInProgress!=old ) + d->oldOps.append( d->opInProgress ); + + if ( d->operationQueue.isEmpty() ) { + d->opInProgress = 0; + if ( d->autoDelete ) + d->removeTimer->start( d->removeInterval, TRUE ); + return; + } + + TQNetworkOperation *op = d->operationQueue.head(); + + d->opInProgress = op; + + if ( !checkConnection( op ) ) { + if ( op->state() != TQNetworkProtocol::StFailed ) { + d->opStartTimer->start( 0, TRUE ); + } else { + d->operationQueue.dequeue(); + clearOperationQueue(); + emit finished( op ); + } + + return; + } + + d->opInProgress = op; + d->operationQueue.dequeue(); + processOperation( op ); +} + +/*! + Returns the TQUrlOperator on which the protocol works. +*/ + +TQUrlOperator *TQNetworkProtocol::url() const +{ + return d->url; +} + +/*! + Returns the operation, which is being processed, or 0 of no + operation is being processed at the moment. +*/ + +TQNetworkOperation *TQNetworkProtocol::operationInProgress() const +{ + return d->opInProgress; +} + +/*! + Clears the operation queue. +*/ + +void TQNetworkProtocol::clearOperationQueue() +{ + d->operationQueue.dequeue(); + d->operationQueue.setAutoDelete( TRUE ); + d->operationQueue.clear(); +} + +/*! + Stops the current operation that is being processed and clears all + waiting operations. +*/ + +void TQNetworkProtocol::stop() +{ + TQNetworkOperation *op = d->opInProgress; + clearOperationQueue(); + if ( op ) { + op->setState( StStopped ); + op->setProtocolDetail( tr( "Operation stopped by the user" ) ); + emit finished( op ); + setUrl( 0 ); + op->free(); + } +} + +/*! + Because it's sometimes hard to take care of removing network + protocol instances, TQNetworkProtocol provides an auto-delete + mechanism. If you set \a b to TRUE, the network protocol instance + is removed after it has been inactive for \a i milliseconds (i.e. + \a i milliseconds after the last operation has been processed). + If you set \a b to FALSE the auto-delete mechanism is switched + off. + + If you switch on auto-delete, the TQNetworkProtocol also deletes + its TQUrlOperator. +*/ + +void TQNetworkProtocol::setAutoDelete( bool b, int i ) +{ + d->autoDelete = b; + d->removeInterval = i; +} + +/*! + Returns TRUE if auto-deleting is enabled; otherwise returns FALSE. + + \sa TQNetworkProtocol::setAutoDelete() +*/ + +bool TQNetworkProtocol::autoDelete() const +{ + return d->autoDelete; +} + +/*! + \internal +*/ + +void TQNetworkProtocol::removeMe() +{ + if ( d->autoDelete ) { +#ifdef TQNETWORKPROTOCOL_DEBUG + qDebug( "TQNetworkOperation: autodelete of TQNetworkProtocol %p", this ); +#endif + delete d->url; // destructor deletes the network protocol + } +} + +void TQNetworkProtocol::emitNewChildren( const TQUrlInfo &i, TQNetworkOperation *op ) +{ + TQValueList lst; + lst << i; + emit newChildren( lst, op ); +} + +class TQNetworkOperationPrivate +{ +public: + TQNetworkProtocol::Operation operation; + TQNetworkProtocol::State state; + TQMap args; + TQMap rawArgs; + TQString protocolDetail; + int errorCode; + TQTimer *deleteTimer; +}; + +/*! + \class TQNetworkOperation + + \brief The TQNetworkOperation class provides common operations for network protocols. +\if defined(commercial) + It is part of the TQt Enterprise Edition. +\endif + + \module network + \ingroup io + + An object is created to describe the operation and the current + state for each operation that a network protocol should process. + + For a detailed description of the TQt Network Architecture and how + to implement and use network protocols in TQt, see the \link + network.html TQt Network Documentation\endlink. + + \sa TQNetworkProtocol +*/ + +/*! + Constructs a network operation object. \a operation is the type of + the operation, and \a arg0, \a arg1 and \a arg2 are the first + three arguments of the operation. The state is initialized to + TQNetworkProtocol::StWaiting. + + \sa TQNetworkProtocol::Operation TQNetworkProtocol::State +*/ + +TQNetworkOperation::TQNetworkOperation( TQNetworkProtocol::Operation operation, + const TQString &arg0, const TQString &arg1, + const TQString &arg2 ) +{ + d = new TQNetworkOperationPrivate; + d->deleteTimer = new TQTimer( this ); + connect( d->deleteTimer, SIGNAL( timeout() ), + this, SLOT( deleteMe() ) ); + d->operation = operation; + d->state = TQNetworkProtocol::StWaiting; + d->args[ 0 ] = arg0; + d->args[ 1 ] = arg1; + d->args[ 2 ] = arg2; + d->rawArgs[ 0 ] = TQByteArray( 0 ); + d->rawArgs[ 1 ] = TQByteArray( 0 ); + d->rawArgs[ 2 ] = TQByteArray( 0 ); + d->protocolDetail = TQString::null; + d->errorCode = (int)TQNetworkProtocol::NoError; +} + +/*! + Constructs a network operation object. \a operation is the type of + the operation, and \a arg0, \a arg1 and \a arg2 are the first + three raw data arguments of the operation. The state is + initialized to TQNetworkProtocol::StWaiting. + + \sa TQNetworkProtocol::Operation TQNetworkProtocol::State +*/ + +TQNetworkOperation::TQNetworkOperation( TQNetworkProtocol::Operation operation, + const TQByteArray &arg0, const TQByteArray &arg1, + const TQByteArray &arg2 ) +{ + d = new TQNetworkOperationPrivate; + d->deleteTimer = new TQTimer( this ); + connect( d->deleteTimer, SIGNAL( timeout() ), + this, SLOT( deleteMe() ) ); + d->operation = operation; + d->state = TQNetworkProtocol::StWaiting; + d->args[ 0 ] = TQString::null; + d->args[ 1 ] = TQString::null; + d->args[ 2 ] = TQString::null; + d->rawArgs[ 0 ] = arg0; + d->rawArgs[ 1 ] = arg1; + d->rawArgs[ 2 ] = arg2; + d->protocolDetail = TQString::null; + d->errorCode = (int)TQNetworkProtocol::NoError; +} + +/*! + Destructor. +*/ + +TQNetworkOperation::~TQNetworkOperation() +{ + delete d; +} + +/*! + Sets the \a state of the operation object. This should be done by + the network protocol during processing; at the end it should be + set to TQNetworkProtocol::StDone or TQNetworkProtocol::StFailed, + depending on success or failure. + + \sa TQNetworkProtocol::State +*/ + +void TQNetworkOperation::setState( TQNetworkProtocol::State state ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->state = state; +} + +/*! + If the operation failed, the error message can be specified as \a + detail. +*/ + +void TQNetworkOperation::setProtocolDetail( const TQString &detail ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->protocolDetail = detail; +} + +/*! + Sets the error code to \a ec. + + If the operation failed, the protocol should set an error code to + describe the error in more detail. If possible, one of the error + codes defined in TQNetworkProtocol should be used. + + \sa setProtocolDetail() TQNetworkProtocol::Error +*/ + +void TQNetworkOperation::setErrorCode( int ec ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->errorCode = ec; +} + +/*! + Sets the network operation's \a{num}-th argument to \a arg. +*/ + +void TQNetworkOperation::setArg( int num, const TQString &arg ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->args[ num ] = arg; +} + +/*! + Sets the network operation's \a{num}-th raw data argument to \a arg. +*/ + +void TQNetworkOperation::setRawArg( int num, const TQByteArray &arg ) +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + d->rawArgs[ num ] = arg; +} + +/*! + Returns the type of the operation. +*/ + +TQNetworkProtocol::Operation TQNetworkOperation::operation() const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->operation; +} + +/*! + Returns the state of the operation. You can determine whether an + operation is still waiting to be processed, is being processed, + has been processed successfully, or failed. +*/ + +TQNetworkProtocol::State TQNetworkOperation::state() const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->state; +} + +/*! + Returns the operation's \a{num}-th argument. If this argument was + not already set, an empty string is returned. +*/ + +TQString TQNetworkOperation::arg( int num ) const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->args[ num ]; +} + +/*! + Returns the operation's \a{num}-th raw data argument. If this + argument was not already set, an empty bytearray is returned. +*/ + +TQByteArray TQNetworkOperation::rawArg( int num ) const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->rawArgs[ num ]; +} + +/*! + Returns a detailed error message for the last error. This must + have been set using setProtocolDetail(). +*/ + +TQString TQNetworkOperation::protocolDetail() const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->protocolDetail; +} + +/*! + Returns the error code for the last error that occurred. +*/ + +int TQNetworkOperation::errorCode() const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->errorCode; +} + +/*! + \internal +*/ + +TQByteArray& TQNetworkOperation::raw( int num ) const +{ + if ( d->deleteTimer->isActive() ) { + d->deleteTimer->stop(); + d->deleteTimer->start( NETWORK_OP_DELAY ); + } + return d->rawArgs[ num ]; +} + +/*! + Sets this object to delete itself when it hasn't been used for one + second. + + Because TQNetworkOperation pointers are passed around a lot the + TQNetworkProtocol generally does not have enough knowledge to + delete these at the correct time. If a TQNetworkProtocol doesn't + need an operation any more it will call this function instead. + + Note: you should never need to call the method yourself. +*/ + +void TQNetworkOperation::free() +{ + d->deleteTimer->start( NETWORK_OP_DELAY ); +} + +/*! + \internal + Internal slot for auto-deletion. +*/ + +void TQNetworkOperation::deleteMe() +{ + delete this; +} + +#endif diff --git a/src/kernel/qnetworkprotocol.h b/src/kernel/qnetworkprotocol.h new file mode 100644 index 000000000..ab5998518 --- /dev/null +++ b/src/kernel/qnetworkprotocol.h @@ -0,0 +1,244 @@ +/**************************************************************************** +** +** Definition of TQNetworkProtocol class +** +** Created : 950429 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQNETWORKPROTOCOL_H +#define TQNETWORKPROTOCOL_H + +#ifndef QT_H +#include "qurlinfo.h" +#include "qstring.h" +#include "qdict.h" +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_NETWORKPROTOCOL + +#if __GNUC__ - 0 > 3 +#pragma GCC system_header +#endif + +class TQNetworkProtocol; +class TQNetworkOperation; +class TQTimer; +class TQUrlOperator; +class TQNetworkProtocolPrivate; +template class TQValueList; + +class Q_EXPORT TQNetworkProtocolFactoryBase +{ +public: + virtual TQNetworkProtocol *createObject() = 0; + +}; + +template< class T > +class TQNetworkProtocolFactory : public TQNetworkProtocolFactoryBase +{ +public: + TQNetworkProtocol *createObject() { + return new T; + } + +}; + +typedef TQDict< TQNetworkProtocolFactoryBase > TQNetworkProtocolDict; + +class Q_EXPORT TQNetworkProtocol : public TQObject +{ + Q_OBJECT + +public: + enum State { + StWaiting = 0, + StInProgress, + StDone, + StFailed, + StStopped + }; + + enum Operation { + OpListChildren = 1, + OpMkDir = 2, + OpMkdir = OpMkDir, // ### remove in 4.0 + OpRemove = 4, + OpRename = 8, + OpGet = 32, + OpPut = 64 + }; + + enum ConnectionState { + ConHostFound, + ConConnected, + ConClosed + }; + + enum Error { + // no error + NoError = 0, + // general errors + ErrValid, + ErrUnknownProtocol, + ErrUnsupported, + ErrParse, + // errors on connect + ErrLoginIncorrect, + ErrHostNotFound, + // protocol errors + ErrListChildren, + ErrListChlidren = ErrListChildren, // ### remove in 4.0 + ErrMkDir, + ErrMkdir = ErrMkDir, // ### remove in 4.0 + ErrRemove, + ErrRename, + ErrGet, + ErrPut, + ErrFileNotExisting, + ErrPermissionDenied + }; + + TQNetworkProtocol(); + virtual ~TQNetworkProtocol(); + + virtual void setUrl( TQUrlOperator *u ); + + virtual void setAutoDelete( bool b, int i = 10000 ); + bool autoDelete() const; + + static void registerNetworkProtocol( const TQString &protocol, + TQNetworkProtocolFactoryBase *protocolFactory ); + static TQNetworkProtocol *getNetworkProtocol( const TQString &protocol ); + static bool hasOnlyLocalFileSystem(); + + virtual int supportedOperations() const; + virtual void addOperation( TQNetworkOperation *op ); + + TQUrlOperator *url() const; + TQNetworkOperation *operationInProgress() const; + virtual void clearOperationQueue(); + virtual void stop(); + +signals: + void data( const TQByteArray &, TQNetworkOperation *res ); + void connectionStateChanged( int state, const TQString &data ); + void finished( TQNetworkOperation *res ); + void start( TQNetworkOperation *res ); + void newChildren( const TQValueList &, TQNetworkOperation *res ); + void newChild( const TQUrlInfo &, TQNetworkOperation *res ); + void createdDirectory( const TQUrlInfo &, TQNetworkOperation *res ); + void removed( TQNetworkOperation *res ); + void itemChanged( TQNetworkOperation *res ); + void dataTransferProgress( int bytesDone, int bytesTotal, TQNetworkOperation *res ); + +protected: + virtual void processOperation( TQNetworkOperation *op ); + virtual void operationListChildren( TQNetworkOperation *op ); + virtual void operationMkDir( TQNetworkOperation *op ); + virtual void operationRemove( TQNetworkOperation *op ); + virtual void operationRename( TQNetworkOperation *op ); + virtual void operationGet( TQNetworkOperation *op ); + virtual void operationPut( TQNetworkOperation *op ); + virtual void operationPutChunk( TQNetworkOperation *op ); + virtual bool checkConnection( TQNetworkOperation *op ); + +private: + TQNetworkProtocolPrivate *d; + +private slots: + void processNextOperation( TQNetworkOperation *old ); + void startOps(); + void emitNewChildren( const TQUrlInfo &i, TQNetworkOperation *op ); + + void removeMe(); + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQNetworkProtocol( const TQNetworkProtocol & ); + TQNetworkProtocol &operator=( const TQNetworkProtocol & ); +#endif +}; + +class TQNetworkOperationPrivate; + +class Q_EXPORT TQNetworkOperation : public TQObject +{ + Q_OBJECT + friend class TQUrlOperator; + +public: + TQNetworkOperation( TQNetworkProtocol::Operation operation, + const TQString &arg0, const TQString &arg1, + const TQString &arg2 ); + TQNetworkOperation( TQNetworkProtocol::Operation operation, + const TQByteArray &arg0, const TQByteArray &arg1, + const TQByteArray &arg2 ); + ~TQNetworkOperation(); + + void setState( TQNetworkProtocol::State state ); + void setProtocolDetail( const TQString &detail ); + void setErrorCode( int ec ); + void setArg( int num, const TQString &arg ); + void setRawArg( int num, const TQByteArray &arg ); + + TQNetworkProtocol::Operation operation() const; + TQNetworkProtocol::State state() const; + TQString arg( int num ) const; + TQByteArray rawArg( int num ) const; + TQString protocolDetail() const; + int errorCode() const; + + void free(); + +private slots: + void deleteMe(); + +private: + TQByteArray &raw( int num ) const; + TQNetworkOperationPrivate *d; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQNetworkOperation( const TQNetworkOperation & ); + TQNetworkOperation &operator=( const TQNetworkOperation & ); +#endif +}; + +#endif // QT_NO_NETWORKPROTOCOL + +#endif // TQNETWORKPROTOCOL_H diff --git a/src/kernel/qobject.cpp b/src/kernel/qobject.cpp new file mode 100644 index 000000000..242a35e69 --- /dev/null +++ b/src/kernel/qobject.cpp @@ -0,0 +1,2711 @@ +/**************************************************************************** +** +** Implementation of TQObject class +** +** Created : 930418 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qvariant.h" +#include "qapplication.h" +#include "qobject.h" +#include "qobjectlist.h" +#include "qsignalslotimp.h" +#include "qregexp.h" +#include "qmetaobject.h" +#include +#include "qucomextra_p.h" +#include "qptrvector.h" + +#ifdef QT_THREAD_SUPPORT +#include +#include +#endif + +#include + + +#ifndef QT_NO_USERDATA +class TQObjectPrivate : public TQPtrVector +{ +public: + TQObjectPrivate( uint s ) : TQPtrVector(s){ setAutoDelete( TRUE ); } +}; +#else +class TQObjectPrivate { +} +#endif + +class TQSenderObjectList : public TQObjectList, public TQShared +{ +public: + TQSenderObjectList() : currentSender( 0 ) { } + TQObject *currentSender; +}; + +/*! + \class TQt qnamespace.h + + \brief The TQt class is a namespace for miscellaneous identifiers + that need to be global-like. + + \ingroup misc + + Normally, you can ignore this class. TQObject and a few other + classes inherit it, so all the identifiers in the TQt namespace are + normally usable without qualification. + + However, you may occasionally need to say \c TQt::black instead of + just \c black, particularly in static utility functions (such as + many class factories). + +*/ + +/*! + \enum TQt::Orientation + + This type is used to signify an object's orientation. + + \value Horizontal + \value Vertical + + Orientation is used with TQScrollBar for example. +*/ + + +/*! + \class TQObject qobject.h + \brief The TQObject class is the base class of all TQt objects. + + \ingroup objectmodel + \mainclass + \reentrant + + TQObject is the heart of the \link object.html TQt object model. + \endlink The central feature in this model is a very powerful + mechanism for seamless object communication called \link + signalsandslots.html signals and slots \endlink. You can + connect a signal to a slot with connect() and destroy the + connection with disconnect(). To avoid never ending notification + loops you can temporarily block signals with blockSignals(). The + protected functions connectNotify() and disconnectNotify() make it + possible to track connections. + + TQObjects organize themselves in object trees. When you create a + TQObject with another object as parent, the object will + automatically do an insertChild() on the parent and thus show up + in the parent's children() list. The parent takes ownership of the + object i.e. it will automatically delete its children in its + destructor. You can look for an object by name and optionally type + using child() or queryList(), and get the list of tree roots using + objectTrees(). + + Every object has an object name() and can report its className() + and whether it inherits() another class in the TQObject inheritance + hierarchy. + + When an object is deleted, it emits a destroyed() signal. You can + catch this signal to avoid dangling references to TQObjects. The + TQGuardedPtr class provides an elegant way to use this feature. + + TQObjects can receive events through event() and filter the events + of other objects. See installEventFilter() and eventFilter() for + details. A convenience handler, childEvent(), can be reimplemented + to catch child events. + + Last but not least, TQObject provides the basic timer support in + TQt; see TQTimer for high-level support for timers. + + Notice that the Q_OBJECT macro is mandatory for any object that + implements signals, slots or properties. You also need to run the + \link moc.html moc program (Meta Object Compiler) \endlink on the + source file. We strongly recommend the use of this macro in \e all + subclasses of TQObject regardless of whether or not they actually + use signals, slots and properties, since failure to do so may lead + certain functions to exhibit undefined behaviour. + + All TQt widgets inherit TQObject. The convenience function + isWidgetType() returns whether an object is actually a widget. It + is much faster than inherits( "TQWidget" ). + + Some TQObject functions, e.g. children(), objectTrees() and + queryList() return a TQObjectList. A TQObjectList is a TQPtrList of + TQObjects. TQObjectLists support the same operations as TQPtrLists + and have an iterator class, TQObjectListIt. +*/ + + +// +// Remove white space from SIGNAL and SLOT names. +// Internal for TQObject::connect() and TQObject::disconnect() +// + +static inline bool isIdentChar( char x ) +{ // Avoid bug in isalnum + return x == '_' || (x >= '0' && x <= '9') || + (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'); +} + +static inline bool isSpace( char x ) +{ +#if defined(Q_CC_BOR) + /* + Borland C++ 4.5 has a weird isspace() bug. + isspace() usually works, but not here. + This implementation is sufficient for our internal use: rmWS() + */ + return (uchar) x <= 32; +#else + return isspace( (uchar) x ); +#endif +} + +static TQCString qt_rmWS( const char *s ) +{ + TQCString result( qstrlen(s)+1 ); + char *d = result.data(); + char last = 0; + while( *s && isSpace(*s) ) // skip leading space + s++; + while ( *s ) { + while ( *s && !isSpace(*s) ) + last = *d++ = *s++; + while ( *s && isSpace(*s) ) + s++; + if ( *s && isIdentChar(*s) && isIdentChar(last) ) + last = *d++ = ' '; + } + *d = '\0'; + result.truncate( (int)(d - result.data()) ); + int void_pos = result.find("(void)"); + if ( void_pos >= 0 ) + result.remove( void_pos+1, (uint)strlen("void") ); + return result; +} + + +// Event functions, implemented in qapplication_xxx.cpp + +int qStartTimer( int interval, TQObject *obj ); +bool qKillTimer( int id ); +bool qKillTimer( TQObject *obj ); + +static void removeObjFromList( TQObjectList *objList, const TQObject *obj, + bool single=FALSE ) +{ + if ( !objList ) + return; + int index = objList->findRef( obj ); + while ( index >= 0 ) { + objList->remove(); + if ( single ) + return; + index = objList->findNextRef( obj ); + } +} + + +/*! + \relates TQObject + + Returns a pointer to the object named \a name that inherits \a + type and with a given \a parent. + + Returns 0 if there is no such child. + + \code + TQListBox *c = (TQListBox *) qt_find_obj_child( myWidget, "TQListBox", + "my list box" ); + if ( c ) + c->insertItem( "another string" ); + \endcode +*/ + +void *qt_find_obj_child( TQObject *parent, const char *type, const char *name ) +{ + const TQObjectList *list = parent->children(); + if ( list ) { + TQObjectListIt it( *list ); + TQObject *obj; + while ( (obj = it.current()) ) { + ++it; + if ( qstrcmp(name,obj->name()) == 0 && + obj->inherits(type) ) + return obj; + } + } + return 0; +} + + + +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY +/* + Preliminary signal spy + */ +Q_EXPORT TQObject* qt_preliminary_signal_spy = 0; +static TQObject* qt_spy_signal_sender = 0; + +static void qt_spy_signal( TQObject* sender, int signal, TQUObject* o ) +{ + TQMetaObject* mo = sender->metaObject(); + while ( mo && signal - mo->signalOffset() < 0 ) + mo = mo->superClass(); + if ( !mo ) + return; + const TQMetaData* sigData = mo->signal( signal - mo->signalOffset() ); + if ( !sigData ) + return; + TQCString s; + mo = sender->metaObject(); + while ( mo ) { + s.sprintf( "%s_%s", mo->className(), sigData->name ); + int slot = qt_preliminary_signal_spy->metaObject()->findSlot( s, TRUE ); + if ( slot >= 0 ) { +#ifdef QT_THREAD_SUPPORT + // protect access to qt_spy_signal_sender + void * const address = &qt_spy_signal_sender; + TQMutexLocker locker( qt_global_mutexpool ? + qt_global_mutexpool->get( address ) : 0 ); +#endif // QT_THREAD_SUPPORT + + TQObject* old_sender = qt_spy_signal_sender; + qt_spy_signal_sender = sender; + qt_preliminary_signal_spy->qt_invoke( slot, o ); + qt_spy_signal_sender = old_sender; + break; + } + mo = mo->superClass(); + } +} + +/* + End Preliminary signal spy + */ +#endif // QT_NO_PRELIMINARY_SIGNAL_SPY + +static TQObjectList* object_trees = 0; + +#ifdef QT_THREAD_SUPPORT +static TQMutex *obj_trees_mutex = 0; +#endif + +static void cleanup_object_trees() +{ + delete object_trees; + object_trees = 0; +#ifdef QT_THREAD_SUPPORT + delete obj_trees_mutex; + obj_trees_mutex = 0; +#endif +} + +static void ensure_object_trees() +{ + object_trees = new TQObjectList; + qAddPostRoutine( cleanup_object_trees ); +} + +static void insert_tree( TQObject* obj ) +{ +#ifdef QT_THREAD_SUPPORT + if ( !obj_trees_mutex ) + obj_trees_mutex = new TQMutex(); + TQMutexLocker locker( obj_trees_mutex ); +#endif + if ( !object_trees ) + ensure_object_trees(); + object_trees->insert(0, obj ); +} + +static void remove_tree( TQObject* obj ) +{ + if ( object_trees ) { +#ifdef QT_THREAD_SUPPORT + TQMutexLocker locker( obj_trees_mutex ); +#endif + object_trees->removeRef( obj ); + } +} + +/*! \internal + TQt compatibility function +*/ +TQObjectList TQObject::childrenListObject() { + if (children()) return *(children()); + else return TQObjectList(); +} + +/*! \internal + TQt compatibility function +*/ +const TQObjectList TQObject::childrenListObject() const { + if (children()) return *(children()); + else return TQObjectList(); +} + +/*! \internal + TQt compatibility function +*/ +const TQObjectList TQObject::objectTreesListObject() { + if (objectTrees()) return *(objectTrees()); + else return TQObjectList(); +} + + +/***************************************************************************** + TQObject member functions + *****************************************************************************/ + +/*! + Constructs an object called \a name with parent object, \a parent. + + The parent of an object may be viewed as the object's owner. For + instance, a \link TQDialog dialog box\endlink is the parent of the + "OK" and "Cancel" buttons it contains. + + The destructor of a parent object destroys all child objects. + + Setting \a parent to 0 constructs an object with no parent. If the + object is a widget, it will become a top-level window. + + The object name is some text that can be used to identify a + TQObject. It's particularly useful in conjunction with \link + designer-manual.book TQt Designer\endlink. You can find an + object by name (and type) using child(). To find several objects + use queryList(). + + \sa parent(), name(), child(), queryList() +*/ + +TQObject::TQObject( TQObject *parent, const char *name ) + : + isSignal( FALSE ), // assume not a signal object + isWidget( FALSE ), // assume not a widget object + pendTimer( FALSE ), // no timers yet + blockSig( FALSE ), // not blocking signals + wasDeleted( FALSE ), // double-delete catcher + isTree( FALSE ), // no tree yet + objname( name ? qstrdup(name) : 0 ), // set object name + parentObj( 0 ), // no parent yet. It is set by insertChild() + childObjects( 0 ), // no children yet + connections( 0 ), // no connections yet + senderObjects( 0 ), // no signals connected yet + eventFilters( 0 ), // no filters installed + postedEvents( 0 ), // no events posted + d( 0 ) +{ + if ( !metaObj ) // will create object dict + (void) staticMetaObject(); + + if ( parent ) { // add object to parent + parent->insertChild( this ); + } else { + insert_tree( this ); + isTree = TRUE; + } +} + + +/*! + Destroys the object, deleting all its child objects. + + All signals to and from the object are automatically disconnected. + + \warning All child objects are deleted. If any of these objects + are on the stack or global, sooner or later your program will + crash. We do not recommend holding pointers to child objects from + outside the parent. If you still do, the TQObject::destroyed() + signal gives you an opportunity to detect when an object is + destroyed. + + \warning Deleting a TQObject while pending events are waiting to be + delivered can cause a crash. You must not delete the TQObject + directly from a thread that is not the GUI thread. Use the + TQObject::deleteLater() method instead, which will cause the event + loop to delete the object after all pending events have been + delivered to the object. +*/ + +TQObject::~TQObject() +{ + if ( wasDeleted ) { +#if defined(QT_DEBUG) + qWarning( "Double TQObject deletion detected." ); +#endif + return; + } + wasDeleted = 1; + blockSig = 0; // unblock signals to keep TQGuardedPtr happy + emit destroyed( this ); + emit destroyed(); + if ( objname ) + delete [] (char*)objname; + objname = 0; + if ( pendTimer ) // might be pending timers + qKillTimer( this ); + TQApplication::removePostedEvents( this ); + if ( isTree ) { + remove_tree( this ); // remove from global root list + isTree = FALSE; + } + if ( parentObj ) // remove it from parent object + parentObj->removeChild( this ); + register TQObject *obj; + if ( senderObjects ) { // disconnect from senders + TQSenderObjectList *tmp = senderObjects; + senderObjects = 0; + obj = tmp->first(); + while ( obj ) { // for all senders... + obj->disconnect( this ); + obj = tmp->next(); + } + if ( tmp->deref() ) + delete tmp; + } + if ( connections ) { // disconnect receivers + for ( int i = 0; i < (int) connections->size(); i++ ) { + TQConnectionList* clist = (*connections)[i]; // for each signal... + if ( !clist ) + continue; + register TQConnection *c; + TQConnectionListIt cit(*clist); + while( (c=cit.current()) ) { // for each connected slot... + ++cit; + if ( (obj=c->object()) ) + removeObjFromList( obj->senderObjects, this ); + } + } + delete connections; + connections = 0; + } + if ( eventFilters ) { + delete eventFilters; + eventFilters = 0; + } + if ( childObjects ) { // delete children objects + TQObjectListIt it(*childObjects); + while ( (obj=it.current()) ) { + ++it; + obj->parentObj = 0; + childObjects->removeRef( obj ); + delete obj; + } + delete childObjects; + } + + delete d; +} + + +/*! + \fn TQMetaObject *TQObject::metaObject() const + + Returns a pointer to the meta object of this object. + + A meta object contains information about a class that inherits + TQObject, e.g. class name, superclass name, properties, signals and + slots. Every class that contains the Q_OBJECT macro will also have + a meta object. + + The meta object information is retquired by the signal/slot + connection mechanism and the property system. The functions isA() + and inherits() also make use of the meta object. +*/ + +/*! + \fn const char *TQObject::className() const + + Returns the class name of this object. + + This function is generated by the \link metaobjects.html Meta + Object Compiler. \endlink + + \warning This function will return the wrong name if the class + definition lacks the Q_OBJECT macro. + + \sa name(), inherits(), isA(), isWidgetType() +*/ + +/*! + Returns TRUE if this object is an instance of the class \a clname; + otherwise returns FALSE. + + Example: + \code + TQTimer *t = new TQTimer; // TQTimer inherits TQObject + t->isA( "TQTimer" ); // returns TRUE + t->isA( "TQObject" ); // returns FALSE + \endcode + + \sa inherits() metaObject() +*/ + +bool TQObject::isA( const char *clname ) const +{ + return qstrcmp( clname, className() ) == 0; +} + +/*! + Returns TRUE if this object is an instance of a class that + inherits \a clname, and \a clname inherits TQObject; otherwise + returns FALSE. + + A class is considered to inherit itself. + + Example: + \code + TQTimer *t = new TQTimer; // TQTimer inherits TQObject + t->inherits( "TQTimer" ); // returns TRUE + t->inherits( "TQObject" ); // returns TRUE + t->inherits( "TQButton" ); // returns FALSE + + // TQScrollBar inherits TQWidget and TQRangeControl + TQScrollBar *s = new TQScrollBar( 0 ); + s->inherits( "TQWidget" ); // returns TRUE + s->inherits( "TQRangeControl" ); // returns FALSE + \endcode + + (\l TQRangeControl is not a TQObject.) + + \sa isA(), metaObject() +*/ + +bool TQObject::inherits( const char *clname ) const +{ + return metaObject()->inherits( clname ); +} + +/*! + \internal + + Returns TRUE if \a object inherits \a superClass within + the meta object inheritance chain; otherwise returns FALSE. + + \sa inherits() +*/ +void *qt_inheritedBy( TQMetaObject *superClass, const TQObject *object ) +{ + if (!object) + return 0; + register TQMetaObject *mo = object->metaObject(); + while (mo) { + if (mo == superClass) + return (void*)object; + mo = mo->superClass(); + } + return 0; +} + +/*! + \property TQObject::name + + \brief the name of this object + + You can find an object by name (and type) using child(). You can + find a set of objects with queryList(). + + The object name is set by the constructor or by the setName() + function. The object name is not very useful in the current + version of TQt, but will become increasingly important in the + future. + + If the object does not have a name, the name() function returns + "unnamed", so printf() (used in qDebug()) will not be asked to + output a null pointer. If you want a null pointer to be returned + for unnamed objects, you can call name( 0 ). + + \code + qDebug( "MyClass::setPrecision(): (%s) invalid precision %f", + name(), newPrecision ); + \endcode + + \sa className(), child(), queryList() +*/ + +const char * TQObject::name() const +{ + // If you change the name here, the builder will be broken + return objname ? objname : "unnamed"; +} + +/*! + Sets the object's name to \a name. +*/ +void TQObject::setName( const char *name ) +{ + if ( objname ) + delete [] (char*) objname; + objname = name ? qstrdup(name) : 0; +} + +/*! + \overload + + Returns the name of this object, or \a defaultName if the object + does not have a name. +*/ + +const char * TQObject::name( const char * defaultName ) const +{ + return objname ? objname : defaultName; +} + + +/*! + Searches the children and optionally grandchildren of this object, + and returns a child that is called \a objName that inherits \a + inheritsClass. If \a inheritsClass is 0 (the default), any class + matches. + + If \a recursiveSearch is TRUE (the default), child() performs a + depth-first search of the object's children. + + If there is no such object, this function returns 0. If there are + more than one, the first one found is retured; if you need all of + them, use queryList(). +*/ +TQObject* TQObject::child( const char *objName, const char *inheritsClass, + bool recursiveSearch ) +{ + const TQObjectList *list = children(); + if ( !list ) + return 0; + + bool onlyWidgets = ( inheritsClass && qstrcmp( inheritsClass, "TQWidget" ) == 0 ); + TQObjectListIt it( *list ); + TQObject *obj; + while ( ( obj = it.current() ) ) { + ++it; + if ( onlyWidgets ) { + if ( obj->isWidgetType() && ( !objName || qstrcmp( objName, obj->name() ) == 0 ) ) + break; + } else if ( ( !inheritsClass || obj->inherits(inheritsClass) ) && ( !objName || qstrcmp( objName, obj->name() ) == 0 ) ) + break; + if ( recursiveSearch && (obj = obj->child( objName, inheritsClass, recursiveSearch ) ) ) + break; + } + return obj; +} + +/*! + \fn bool TQObject::isWidgetType() const + + Returns TRUE if the object is a widget; otherwise returns FALSE. + + Calling this function is equivalent to calling + inherits("TQWidget"), except that it is much faster. +*/ + +/*! + \fn bool TQObject::highPriority() const + + Returns TRUE if the object is a high-priority object, or FALSE if + it is a standard-priority object. + + High-priority objects are placed first in TQObject's list of + children on the assumption that they will be referenced very + often. +*/ + + +/*! + This virtual function receives events to an object and should + return TRUE if the event \a e was recognized and processed. + + The event() function can be reimplemented to customize the + behavior of an object. + + \sa installEventFilter(), timerEvent(), TQApplication::sendEvent(), + TQApplication::postEvent(), TQWidget::event() +*/ + +bool TQObject::event( TQEvent *e ) +{ +#if defined(QT_CHECK_NULL) + if ( e == 0 ) + qWarning( "TQObject::event: Null events are not permitted" ); +#endif + if ( eventFilters ) { // try filters + if ( activate_filters(e) ) // stopped by a filter + return TRUE; + } + + switch ( e->type() ) { + case TQEvent::Timer: + timerEvent( (TQTimerEvent*)e ); + return TRUE; + + case TQEvent::ChildInserted: + case TQEvent::ChildRemoved: + childEvent( (TQChildEvent*)e ); + return TRUE; + + case TQEvent::DeferredDelete: + delete this; + return TRUE; + + default: + if ( e->type() >= TQEvent::User ) { + customEvent( (TQCustomEvent*) e ); + return TRUE; + } + break; + } + return FALSE; +} + +/*! + This event handler can be reimplemented in a subclass to receive + timer events for the object. + + TQTimer provides a higher-level interface to the timer + functionality, and also more general information about timers. + + \sa startTimer(), killTimer(), killTimers(), event() +*/ + +void TQObject::timerEvent( TQTimerEvent * ) +{ +} + + +/*! + This event handler can be reimplemented in a subclass to receive + child events. + + Child events are sent to objects when children are inserted or + removed. + + Note that events with TQEvent::type() \c TQEvent::ChildInserted are + posted (with \l{TQApplication::postEvent()}) to make sure that the + child's construction is completed before this function is called. + + If a child is removed immediately after it is inserted, the \c + ChildInserted event may be suppressed, but the \c ChildRemoved + event will always be sent. In such cases it is possible that there + will be a \c ChildRemoved event without a corresponding \c + ChildInserted event. + + If you change state based on \c ChildInserted events, call + TQWidget::constPolish(), or do + \code + TQApplication::sendPostedEvents( this, TQEvent::ChildInserted ); + \endcode + in functions that depend on the state. One notable example is + TQWidget::sizeHint(). + + \sa event(), TQChildEvent +*/ + +void TQObject::childEvent( TQChildEvent * ) +{ +} + +/*! + This event handler can be reimplemented in a subclass to receive + custom events. Custom events are user-defined events with a type + value at least as large as the "User" item of the \l TQEvent::Type + enum, and is typically a TQCustomEvent or TQCustomEvent subclass. + + \sa event(), TQCustomEvent +*/ +void TQObject::customEvent( TQCustomEvent * ) +{ +} + + + +/*! + Filters events if this object has been installed as an event + filter for the \a watched object. + + In your reimplementation of this function, if you want to filter + the event \a e, out, i.e. stop it being handled further, return + TRUE; otherwise return FALSE. + + Example: + \code + class MyMainWindow : public TQMainWindow + { + public: + MyMainWindow( TQWidget *parent = 0, const char *name = 0 ); + + protected: + bool eventFilter( TQObject *obj, TQEvent *ev ); + + private: + TQTextEdit *textEdit; + }; + + MyMainWindow::MyMainWindow( TQWidget *parent, const char *name ) + : TQMainWindow( parent, name ) + { + textEdit = new TQTextEdit( this ); + setCentralWidget( textEdit ); + textEdit->installEventFilter( this ); + } + + bool MyMainWindow::eventFilter( TQObject *obj, TQEvent *ev ) + { + if ( obj == textEdit ) { + if ( e->type() == TQEvent::KeyPress ) { + TQKeyEvent *k = (TQKeyEvent*)ev; + qDebug( "Ate key press %d", k->key() ); + return TRUE; + } else { + return FALSE; + } + } else { + // pass the event on to the parent class + return TQMainWindow::eventFilter( obj, ev ); + } + } + \endcode + + Notice in the example above that unhandled events are passed to + the base class's eventFilter() function, since the base class + might have reimplemented eventFilter() for its own internal + purposes. + + \warning If you delete the receiver object in this function, be + sure to return TRUE. Otherwise, TQt will forward the event to the + deleted object and the program might crash. + + \sa installEventFilter() +*/ + +bool TQObject::eventFilter( TQObject * /* watched */, TQEvent * /* e */ ) +{ + return FALSE; +} + + +/*! + \internal + Activates all event filters for this object. + This function is normally called from TQObject::event() or TQWidget::event(). +*/ + +bool TQObject::activate_filters( TQEvent *e ) +{ + if ( !eventFilters ) // no event filter + return FALSE; + TQObjectListIt it( *eventFilters ); + register TQObject *obj = it.current(); + while ( obj ) { // send to all filters + ++it; // until one returns TRUE + if ( obj->eventFilter(this,e) ) { + return TRUE; + } + obj = it.current(); + } + return FALSE; // don't do anything with it +} + + +/*! + \fn bool TQObject::signalsBlocked() const + + Returns TRUE if signals are blocked; otherwise returns FALSE. + + Signals are not blocked by default. + + \sa blockSignals() +*/ + +/*! + Blocks signals if \a block is TRUE, or unblocks signals if \a + block is FALSE. + + Emitted signals disappear into hyperspace if signals are blocked. + Note that the destroyed() signals will be emitted even if the signals + for this object have been blocked. +*/ + +void TQObject::blockSignals( bool block ) +{ + blockSig = block; +} + + +// +// The timer flag hasTimer is set when startTimer is called. +// It is not reset when killing the timer because more than +// one timer might be active. +// + +/*! + Starts a timer and returns a timer identifier, or returns zero if + it could not start a timer. + + A timer event will occur every \a interval milliseconds until + killTimer() or killTimers() is called. If \a interval is 0, then + the timer event occurs once every time there are no more window + system events to process. + + The virtual timerEvent() function is called with the TQTimerEvent + event parameter class when a timer event occurs. Reimplement this + function to get timer events. + + If multiple timers are running, the TQTimerEvent::timerId() can be + used to find out which timer was activated. + + Example: + \code + class MyObject : public TQObject + { + Q_OBJECT + public: + MyObject( TQObject *parent = 0, const char *name = 0 ); + + protected: + void timerEvent( TQTimerEvent * ); + }; + + MyObject::MyObject( TQObject *parent, const char *name ) + : TQObject( parent, name ) + { + startTimer( 50 ); // 50-millisecond timer + startTimer( 1000 ); // 1-second timer + startTimer( 60000 ); // 1-minute timer + } + + void MyObject::timerEvent( TQTimerEvent *e ) + { + qDebug( "timer event, id %d", e->timerId() ); + } + \endcode + + Note that TQTimer's accuracy depends on the underlying operating + system and hardware. Most platforms support an accuracy of 20 ms; + some provide more. If TQt is unable to deliver the requested + number of timer clicks, it will silently discard some. + + The TQTimer class provides a high-level programming interface with + one-shot timers and timer signals instead of events. + + \sa timerEvent(), killTimer(), killTimers(), TQEventLoop::awake(), + TQEventLoop::aboutToBlock() +*/ + +int TQObject::startTimer( int interval ) +{ + pendTimer = TRUE; // set timer flag + return qStartTimer( interval, (TQObject *)this ); +} + +/*! + Kills the timer with timer identifier, \a id. + + The timer identifier is returned by startTimer() when a timer + event is started. + + \sa timerEvent(), startTimer(), killTimers() +*/ + +void TQObject::killTimer( int id ) +{ + qKillTimer( id ); +} + +/*! + Kills all timers that this object has started. + + \warning Using this function can cause hard-to-find bugs: it kills + timers started by sub- and superclasses as well as those started + by you, which is often not what you want. We recommend using a + TQTimer or perhaps killTimer(). + + \sa timerEvent(), startTimer(), killTimer() +*/ + +void TQObject::killTimers() +{ + qKillTimer( this ); +} + +static void objSearch( TQObjectList *result, + TQObjectList *list, + const char *inheritsClass, + bool onlyWidgets, + const char *objName, + TQRegExp *rx, + bool recurse ) +{ + if ( !list || list->isEmpty() ) // nothing to search + return; + TQObject *obj = list->first(); + while ( obj ) { + bool ok = TRUE; + if ( onlyWidgets ) + ok = obj->isWidgetType(); + else if ( inheritsClass && !obj->inherits(inheritsClass) ) + ok = FALSE; + if ( ok ) { + if ( objName ) + ok = ( qstrcmp(objName,obj->name()) == 0 ); +#ifndef QT_NO_REGEXP + else if ( rx ) + ok = ( rx->search(TQString::fromLatin1(obj->name())) != -1 ); +#endif + } + if ( ok ) // match! + result->append( obj ); + if ( recurse && obj->children() ) + objSearch( result, (TQObjectList *)obj->children(), inheritsClass, + onlyWidgets, objName, rx, recurse ); + obj = list->next(); + } +} + +/*! + \fn TQObject *TQObject::parent() const + + Returns a pointer to the parent object. + + \sa children() +*/ + +/*! + \fn const TQObjectList *TQObject::children() const + + Returns a list of child objects, or 0 if this object has no + children. + + The TQObjectList class is defined in the \c qobjectlist.h header + file. + + The first child added is the \link TQPtrList::first() first\endlink + object in the list and the last child added is the \link + TQPtrList::last() last\endlink object in the list, i.e. new + children are appended at the end. + + Note that the list order changes when TQWidget children are \link + TQWidget::raise() raised\endlink or \link TQWidget::lower() + lowered.\endlink A widget that is raised becomes the last object + in the list, and a widget that is lowered becomes the first object + in the list. + + \sa child(), queryList(), parent(), insertChild(), removeChild() +*/ + + +/*! + Returns a pointer to the list of all object trees (their root + objects), or 0 if there are no objects. + + The TQObjectList class is defined in the \c qobjectlist.h header + file. + + The most recent root object created is the \link TQPtrList::first() + first\endlink object in the list and the first root object added + is the \link TQPtrList::last() last\endlink object in the list. + + \sa children(), parent(), insertChild(), removeChild() +*/ +const TQObjectList *TQObject::objectTrees() +{ + return object_trees; +} + + +/*! + Searches the children and optionally grandchildren of this object, + and returns a list of those objects that are named or that match + \a objName and inherit \a inheritsClass. If \a inheritsClass is 0 + (the default), all classes match. If \a objName is 0 (the + default), all object names match. + + If \a regexpMatch is TRUE (the default), \a objName is a regular + expression that the objects's names must match. The syntax is that + of a TQRegExp. If \a regexpMatch is FALSE, \a objName is a string + and object names must match it exactly. + + Note that \a inheritsClass uses single inheritance from TQObject, + the way inherits() does. According to inherits(), TQMenuBar + inherits TQWidget but not TQMenuData. This does not tquite match + reality, but is the best that can be done on the wide variety of + compilers TQt supports. + + Finally, if \a recursiveSearch is TRUE (the default), queryList() + searches \e{n}th-generation as well as first-generation children. + + If all this seems a bit complex for your needs, the simpler + child() function may be what you want. + + This somewhat contrived example disables all the buttons in this + window: + \code + TQObjectList *l = topLevelWidget()->queryList( "TQButton" ); + TQObjectListIt it( *l ); // iterate over the buttons + TQObject *obj; + + while ( (obj = it.current()) != 0 ) { + // for each found object... + ++it; + ((TQButton*)obj)->setEnabled( FALSE ); + } + delete l; // delete the list, not the objects + \endcode + + The TQObjectList class is defined in the \c qobjectlist.h header + file. + + \warning Delete the list as soon you have finished using it. The + list contains pointers that may become invalid at almost any time + without notice (as soon as the user closes a window you may have + dangling pointers, for example). + + \sa child() children(), parent(), inherits(), name(), TQRegExp +*/ + +TQObjectList *TQObject::queryList( const char *inheritsClass, + const char *objName, + bool regexpMatch, + bool recursiveSearch ) const +{ + TQObjectList *list = new TQObjectList; + Q_CHECK_PTR( list ); + bool onlyWidgets = ( inheritsClass && qstrcmp(inheritsClass, "TQWidget") == 0 ); +#ifndef QT_NO_REGEXP + if ( regexpMatch && objName ) { // regexp matching + TQRegExp rx(TQString::fromLatin1(objName)); + objSearch( list, (TQObjectList *)children(), inheritsClass, onlyWidgets, + 0, &rx, recursiveSearch ); + } else +#endif + { + objSearch( list, (TQObjectList *)children(), inheritsClass, onlyWidgets, + objName, 0, recursiveSearch ); + } + return list; +} + +/*! \internal + + Returns a list of objects/slot pairs that are connected to the + \a signal, or 0 if nothing is connected to it. +*/ + +TQConnectionList *TQObject::receivers( const char* signal ) const +{ + if ( connections && signal ) { + if ( *signal == '2' ) { // tag == 2, i.e. signal + TQCString s = qt_rmWS( signal+1 ); + return receivers( metaObject()->findSignal( (const char*)s, TRUE ) ); + } else { + return receivers( metaObject()->findSignal(signal, TRUE ) ); + } + } + return 0; +} + +/*! \internal + + Returns a list of objects/slot pairs that are connected to the + signal, or 0 if nothing is connected to it. +*/ + +TQConnectionList *TQObject::receivers( int signal ) const +{ +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY + if ( qt_preliminary_signal_spy && signal >= 0 ) { + if ( !connections ) { + TQObject* that = (TQObject*) this; + that->connections = new TQSignalVec( signal+1 ); + that->connections->setAutoDelete( TRUE ); + } + if ( !connections->at( signal ) ) { + TQConnectionList* clist = new TQConnectionList; + clist->setAutoDelete( TRUE ); + connections->insert( signal, clist ); + return clist; + } + } +#endif + if ( connections && signal >= 0 ) + return connections->at( signal ); + return 0; +} + + +/*! + Inserts an object \a obj into the list of child objects. + + \warning This function cannot be used to make one widget the child + widget of another widget. Child widgets can only be created by + setting the parent widget in the constructor or by calling + TQWidget::reparent(). + + \sa removeChild(), TQWidget::reparent() +*/ + +void TQObject::insertChild( TQObject *obj ) +{ + if ( obj->isTree ) { + remove_tree( obj ); + obj->isTree = FALSE; + } + if ( obj->parentObj && obj->parentObj != this ) { +#if defined(QT_CHECK_STATE) + if ( obj->parentObj != this && obj->isWidgetType() ) + qWarning( "TQObject::insertChild: Cannot reparent a widget, " + "use TQWidget::reparent() instead" ); +#endif + obj->parentObj->removeChild( obj ); + } + + if ( !childObjects ) { + childObjects = new TQObjectList; + Q_CHECK_PTR( childObjects ); + } else if ( obj->parentObj == this ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQObject::insertChild: Object %s::%s already in list", + obj->className(), obj->name( "unnamed" ) ); +#endif + return; + } + obj->parentObj = this; + childObjects->append( obj ); + + TQChildEvent *e = new TQChildEvent( TQEvent::ChildInserted, obj ); + TQApplication::postEvent( this, e ); +} + +/*! + Removes the child object \a obj from the list of children. + + \warning This function will not remove a child widget from the + screen. It will only remove it from the parent widget's list of + children. + + \sa insertChild(), TQWidget::reparent() +*/ + +void TQObject::removeChild( TQObject *obj ) +{ + if ( childObjects && childObjects->removeRef(obj) ) { + obj->parentObj = 0; + if ( !obj->wasDeleted ) { + insert_tree( obj ); // it's a root object now + obj->isTree = TRUE; + } + if ( childObjects->isEmpty() ) { + delete childObjects; // last child removed + childObjects = 0; // reset children list + } + + // remove events must be sent, not posted!!! + TQChildEvent ce( TQEvent::ChildRemoved, obj ); + TQApplication::sendEvent( this, &ce ); + } +} + + +/*! + \fn void TQObject::installEventFilter( const TQObject *filterObj ) + + Installs an event filter \a filterObj on this object. For example: + \code + monitoredObj->installEventFilter( filterObj ); + \endcode + + An event filter is an object that receives all events that are + sent to this object. The filter can either stop the event or + forward it to this object. The event filter \a filterObj receives + events via its eventFilter() function. The eventFilter() function + must return TRUE if the event should be filtered, (i.e. stopped); + otherwise it must return FALSE. + + If multiple event filters are installed on a single object, the + filter that was installed last is activated first. + + Here's a \c KeyPressEater class that eats the key presses of its + monitored objects: + \code + class KeyPressEater : public TQObject + { + ... + protected: + bool eventFilter( TQObject *o, TQEvent *e ); + }; + + bool KeyPressEater::eventFilter( TQObject *o, TQEvent *e ) + { + if ( e->type() == TQEvent::KeyPress ) { + // special processing for key press + TQKeyEvent *k = (TQKeyEvent *)e; + qDebug( "Ate key press %d", k->key() ); + return TRUE; // eat event + } else { + // standard event processing + return FALSE; + } + } + \endcode + + And here's how to install it on two widgets: + \code + KeyPressEater *keyPressEater = new KeyPressEater( this ); + TQPushButton *pushButton = new TQPushButton( this ); + TQListView *listView = new TQListView( this ); + + pushButton->installEventFilter( keyPressEater ); + listView->installEventFilter( keyPressEater ); + \endcode + + The TQAccel class, for example, uses this technique to intercept + accelerator key presses. + + \warning If you delete the receiver object in your eventFilter() + function, be sure to return TRUE. If you return FALSE, TQt sends + the event to the deleted object and the program will crash. + + \sa removeEventFilter(), eventFilter(), event() +*/ + +void TQObject::installEventFilter( const TQObject *obj ) +{ + if ( !obj ) + return; + if ( eventFilters ) { + int c = eventFilters->findRef( obj ); + if ( c >= 0 ) + eventFilters->take( c ); + disconnect( obj, SIGNAL(destroyed(TQObject*)), + this, SLOT(cleanupEventFilter(TQObject*)) ); + } else { + eventFilters = new TQObjectList; + Q_CHECK_PTR( eventFilters ); + } + eventFilters->insert( 0, obj ); + connect( obj, SIGNAL(destroyed(TQObject*)), this, SLOT(cleanupEventFilter(TQObject*)) ); +} + +/*! + Removes an event filter object \a obj from this object. The + request is ignored if such an event filter has not been installed. + + All event filters for this object are automatically removed when + this object is destroyed. + + It is always safe to remove an event filter, even during event + filter activation (i.e. from the eventFilter() function). + + \sa installEventFilter(), eventFilter(), event() +*/ + +void TQObject::removeEventFilter( const TQObject *obj ) +{ + if ( eventFilters && eventFilters->removeRef(obj) ) { + if ( eventFilters->isEmpty() ) { // last event filter removed + delete eventFilters; + eventFilters = 0; // reset event filter list + } + disconnect( obj, SIGNAL(destroyed(TQObject*)), + this, SLOT(cleanupEventFilter(TQObject*)) ); + } +} + + +/***************************************************************************** + Signal connection management + *****************************************************************************/ + +#if defined(QT_CHECK_RANGE) + +static bool check_signal_macro( const TQObject *sender, const char *signal, + const char *func, const char *op ) +{ + int sigcode = (int)(*signal) - '0'; + if ( sigcode != TQSIGNAL_CODE ) { + if ( sigcode == TQSLOT_CODE ) + qWarning( "TQObject::%s: Attempt to %s non-signal %s::%s", + func, op, sender->className(), signal+1 ); + else + qWarning( "TQObject::%s: Use the SIGNAL macro to %s %s::%s", + func, op, sender->className(), signal ); + return FALSE; + } + return TRUE; +} + +static bool check_member_code( int code, const TQObject *object, + const char *member, const char *func ) +{ + if ( code != TQSLOT_CODE && code != TQSIGNAL_CODE ) { + qWarning( "TQObject::%s: Use the SLOT or SIGNAL macro to " + "%s %s::%s", func, func, object->className(), member ); + return FALSE; + } + return TRUE; +} + +static void err_member_notfound( int code, const TQObject *object, + const char *member, const char *func ) +{ + const char *type = 0; + switch ( code ) { + case TQSLOT_CODE: type = "slot"; break; + case TQSIGNAL_CODE: type = "signal"; break; + } + if ( strchr(member,')') == 0 ) // common typing mistake + qWarning( "TQObject::%s: Parentheses expected, %s %s::%s", + func, type, object->className(), member ); + else + qWarning( "TQObject::%s: No such %s %s::%s", + func, type, object->className(), member ); +} + + +static void err_info_about_objects( const char * func, + const TQObject * sender, + const TQObject * receiver ) +{ + const char * a = sender->name(), * b = receiver->name(); + if ( a ) + qWarning( "TQObject::%s: (sender name: '%s')", func, a ); + if ( b ) + qWarning( "TQObject::%s: (receiver name: '%s')", func, b ); +} + +static void err_info_about_candidates( int code, + const TQMetaObject* mo, + const char* member, + const char *func ) +{ + if ( strstr(member,"const char*") ) { + // porting help + TQCString newname = member; + int p; + while ( (p=newname.find("const char*")) >= 0 ) { + newname.replace(p, 11, "const TQString&"); + } + const TQMetaData *rm = 0; + switch ( code ) { + case TQSLOT_CODE: + rm = mo->slot( mo->findSlot( newname, TRUE ), TRUE ); + break; + case TQSIGNAL_CODE: + rm = mo->signal( mo->findSignal( newname, TRUE ), TRUE ); + break; + } + if ( rm ) { + qWarning("TQObject::%s: Candidate: %s", func, newname.data()); + } + } +} + + +#endif // QT_CHECK_RANGE + + +/*! + Returns a pointer to the object that sent the signal, if called in + a slot activated by a signal; otherwise it returns 0. The pointer + is valid only during the execution of the slot that calls this + function. + + The pointer returned by this function becomes invalid if the + sender is destroyed, or if the slot is disconnected from the + sender's signal. + + \warning This function violates the object-oriented principle of + modularity. However, getting access to the sender might be useful + when many signals are connected to a single slot. The sender is + undefined if the slot is called as a normal C++ function. +*/ + +const TQObject *TQObject::sender() +{ +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY + if ( this == qt_preliminary_signal_spy ) { +# ifdef QT_THREAD_SUPPORT + // protect access to qt_spy_signal_sender + void * const address = &qt_spy_signal_sender; + TQMutexLocker locker( qt_global_mutexpool ? + qt_global_mutexpool->get( address ) : 0 ); +# endif // QT_THREAD_SUPPORT + return qt_spy_signal_sender; + } +#endif + if ( senderObjects && + senderObjects->currentSender && + /* + * currentSender may be a dangling pointer in case the object + * it was pointing to was destructed from inside a slot. Thus + * verify it still is contained inside the senderObjects list + * which gets cleaned on both destruction and disconnect. + */ + + senderObjects->findRef( senderObjects->currentSender ) != -1 ) + return senderObjects->currentSender; + return 0; +} + + +/*! + \fn void TQObject::connectNotify( const char *signal ) + + This virtual function is called when something has been connected + to \a signal in this object. + + \warning This function violates the object-oriented principle of + modularity. However, it might be useful when you need to perform + expensive initialization only if something is connected to a + signal. + + \sa connect(), disconnectNotify() +*/ + +void TQObject::connectNotify( const char * ) +{ +} + +/*! + \fn void TQObject::disconnectNotify( const char *signal ) + + This virtual function is called when something has been + disconnected from \a signal in this object. + + \warning This function violates the object-oriented principle of + modularity. However, it might be useful for optimizing access to + expensive resources. + + \sa disconnect(), connectNotify() +*/ + +void TQObject::disconnectNotify( const char * ) +{ +} + + +/*! + \fn bool TQObject::checkConnectArgs( const char *signal, const TQObject *receiver, const char *member ) + + Returns TRUE if the \a signal and the \a member arguments are + compatible; otherwise returns FALSE. (The \a receiver argument is + currently ignored.) + + \warning We recommend that you use the default implementation and + do not reimplement this function. + + \omit + TRUE: "signal()", "member()" + TRUE: "signal(a,b,c)", "member(a,b,c)" + TRUE: "signal(a,b,c)", "member(a,b)", "member(a)" etc. + FALSE: "signal(const a)", "member(a)" + FALSE: "signal(a)", "member(const a)" + FALSE: "signal(a)", "member(b)" + FALSE: "signal(a)", "member(a,b)" + \endomit +*/ + +bool TQObject::checkConnectArgs( const char *signal, + const TQObject *, + const char *member ) +{ + const char *s1 = signal; + const char *s2 = member; + while ( *s1++ != '(' ) { } // scan to first '(' + while ( *s2++ != '(' ) { } + if ( *s2 == ')' || qstrcmp(s1,s2) == 0 ) // member has no args or + return TRUE; // exact match + int s1len = qstrlen(s1); + int s2len = qstrlen(s2); + if ( s2len < s1len && qstrncmp(s1,s2,s2len-1)==0 && s1[s2len-1]==',' ) + return TRUE; // member has less args + return FALSE; +} + +/*! + Normlizes the signal or slot definition \a signalSlot by removing + unnecessary whitespace. +*/ + +TQCString TQObject::normalizeSignalSlot( const char *signalSlot ) +{ + if ( !signalSlot ) + return TQCString(); + return qt_rmWS( signalSlot ); +} + + + +/*! + \overload bool TQObject::connect( const TQObject *sender, const char *signal, const char *member ) const + + Connects \a signal from the \a sender object to this object's \a + member. + + Equivalent to: \c{TQObject::connect(sender, signal, this, member)}. + + \sa disconnect() +*/ + +/*! + Connects \a signal from the \a sender object to \a member in object + \a receiver, and returns TRUE if the connection succeeds; otherwise + returns FALSE. + + You must use the SIGNAL() and SLOT() macros when specifying the \a signal + and the \a member, for example: + \code + TQLabel *label = new TQLabel; + TQScrollBar *scroll = new TQScrollBar; + TQObject::connect( scroll, SIGNAL(valueChanged(int)), + label, SLOT(setNum(int)) ); + \endcode + + This example ensures that the label always displays the current + scroll bar value. Note that the signal and slots parameters must not + contain any variable names, only the type. E.g. the following would + not work and return FALSE: + TQObject::connect( scroll, SIGNAL(valueChanged(int v)), + label, SLOT(setNum(int v)) ); + + A signal can also be connected to another signal: + + \code + class MyWidget : public TQWidget + { + Q_OBJECT + public: + MyWidget(); + + signals: + void myUsefulSignal(); + + private: + TQPushButton *aButton; + }; + + MyWidget::MyWidget() + { + aButton = new TQPushButton( this ); + connect( aButton, SIGNAL(clicked()), SIGNAL(myUsefulSignal()) ); + } + \endcode + + In this example, the MyWidget constructor relays a signal from a + private member variable, and makes it available under a name that + relates to MyWidget. + + A signal can be connected to many slots and signals. Many signals + can be connected to one slot. + + If a signal is connected to several slots, the slots are activated + in an arbitrary order when the signal is emitted. + + The function returns TRUE if it successfully connects the signal + to the slot. It will return FALSE if it cannot create the + connection, for example, if TQObject is unable to verify the + existence of either \a signal or \a member, or if their signatures + aren't compatible. + + A signal is emitted for \e{every} connection you make, so if you + duplicate a connection, two signals will be emitted. You can + always break a connection using \c{disconnect()}. + + \sa disconnect() +*/ + +bool TQObject::connect( const TQObject *sender, const char *signal, + const TQObject *receiver, const char *member ) +{ +#if defined(QT_CHECK_NULL) + if ( sender == 0 || receiver == 0 || signal == 0 || member == 0 ) { + qWarning( "TQObject::connect: Cannot connect %s::%s to %s::%s", + sender ? sender->className() : "(null)", + signal ? signal+1 : "(null)", + receiver ? receiver->className() : "(null)", + member ? member+1 : "(null)" ); + return FALSE; + } +#endif + TQMetaObject *smeta = sender->metaObject(); + +#if defined(QT_CHECK_RANGE) + if ( !check_signal_macro( sender, signal, "connect", "bind" ) ) + return FALSE; +#endif + TQCString nw_signal(signal); // Assume already normalized + ++signal; // skip member type code + + int signal_index = smeta->findSignal( signal, TRUE ); + if ( signal_index < 0 ) { // normalize and retry + nw_signal = qt_rmWS( signal-1 ); // remove whitespace + signal = nw_signal.data()+1; // skip member type code + signal_index = smeta->findSignal( signal, TRUE ); + } + + if ( signal_index < 0 ) { // no such signal +#if defined(QT_CHECK_RANGE) + err_member_notfound( TQSIGNAL_CODE, sender, signal, "connect" ); + err_info_about_candidates( TQSIGNAL_CODE, smeta, signal, "connect" ); + err_info_about_objects( "connect", sender, receiver ); +#endif + return FALSE; + } + const TQMetaData *sm = smeta->signal( signal_index, TRUE ); + signal = sm->name; // use name from meta object + + int membcode = member[0] - '0'; // get member code + + TQObject *s = (TQObject *)sender; // we need to change them + TQObject *r = (TQObject *)receiver; // internally + +#if defined(QT_CHECK_RANGE) + if ( !check_member_code( membcode, r, member, "connect" ) ) + return FALSE; +#endif + member++; // skip code + + TQCString nw_member ; + TQMetaObject *rmeta = r->metaObject(); + int member_index = -1; + switch ( membcode ) { // get receiver member + case TQSLOT_CODE: + member_index = rmeta->findSlot( member, TRUE ); + if ( member_index < 0 ) { // normalize and retry + nw_member = qt_rmWS(member); // remove whitespace + member = nw_member; + member_index = rmeta->findSlot( member, TRUE ); + } + break; + case TQSIGNAL_CODE: + member_index = rmeta->findSignal( member, TRUE ); + if ( member_index < 0 ) { // normalize and retry + nw_member = qt_rmWS(member); // remove whitespace + member = nw_member; + member_index = rmeta->findSignal( member, TRUE ); + } + break; + } + if ( member_index < 0 ) { +#if defined(QT_CHECK_RANGE) + err_member_notfound( membcode, r, member, "connect" ); + err_info_about_candidates( membcode, rmeta, member, "connect" ); + err_info_about_objects( "connect", sender, receiver ); +#endif + return FALSE; + } +#if defined(QT_CHECK_RANGE) + if ( !s->checkConnectArgs(signal,receiver,member) ) { + qWarning( "TQObject::connect: Incompatible sender/receiver arguments" + "\n\t%s::%s --> %s::%s", + s->className(), signal, + r->className(), member ); + return FALSE; + } else { + const TQMetaData *rm = membcode == TQSLOT_CODE ? + rmeta->slot( member_index, TRUE ) : + rmeta->signal( member_index, TRUE ); + if ( rm ) { + int si = 0; + int ri = 0; + while ( si < sm->method->count && ri < rm->method->count ) { + if ( sm->method->parameters[si].inOut == TQUParameter::Out ) + si++; + else if ( rm->method->parameters[ri].inOut == TQUParameter::Out ) + ri++; + else if ( !TQUType::isEqual( sm->method->parameters[si++].type, + rm->method->parameters[ri++].type ) ) { + if ( ( TQUType::isEqual( sm->method->parameters[si-1].type, &static_QUType_ptr ) + && TQUType::isEqual( rm->method->parameters[ri-1].type, &static_QUType_varptr ) ) + || ( TQUType::isEqual( sm->method->parameters[si-1].type, &static_QUType_varptr ) + && TQUType::isEqual( rm->method->parameters[ri-1].type, &static_QUType_ptr ) ) ) + continue; // varptr got introduced in 3.1 and is binary compatible with ptr + qWarning( "TQObject::connect: Incompatible sender/receiver marshalling" + "\n\t%s::%s --> %s::%s", + s->className(), signal, + r->className(), member ); + return FALSE; + } + } + } + } +#endif + connectInternal( sender, signal_index, receiver, membcode, member_index ); + s->connectNotify( nw_signal ); + return TRUE; +} + +/*! \internal */ + +void TQObject::connectInternal( const TQObject *sender, int signal_index, const TQObject *receiver, + int membcode, int member_index ) +{ + TQObject *s = (TQObject*)sender; + TQObject *r = (TQObject*)receiver; + + if ( !s->connections ) { // create connections lookup table + s->connections = new TQSignalVec( signal_index+1 ); + Q_CHECK_PTR( s->connections ); + s->connections->setAutoDelete( TRUE ); + } + + TQConnectionList *clist = s->connections->at( signal_index ); + if ( !clist ) { // create receiver list + clist = new TQConnectionList; + Q_CHECK_PTR( clist ); + clist->setAutoDelete( TRUE ); + s->connections->insert( signal_index, clist ); + } + + TQMetaObject *rmeta = r->metaObject(); + const TQMetaData *rm = 0; + + switch ( membcode ) { // get receiver member + case TQSLOT_CODE: + rm = rmeta->slot( member_index, TRUE ); + break; + case TQSIGNAL_CODE: + rm = rmeta->signal( member_index, TRUE ); + break; + } + + TQConnection *c = new TQConnection( r, member_index, rm ? rm->name : "qt_invoke", membcode ); + Q_CHECK_PTR( c ); + clist->append( c ); + if ( !r->senderObjects ) // create list of senders + r->senderObjects = new TQSenderObjectList; + r->senderObjects->append( s ); // add sender to list +} + + +/*! + \overload bool TQObject::disconnect( const char *signal, const TQObject *receiver, const char *member ) + + Disconnects \a signal from \a member of \a receiver. + + A signal-slot connection is removed when either of the objects + involved are destroyed. +*/ + +/*! + \overload bool TQObject::disconnect( const TQObject *receiver, const char *member ) + + Disconnects all signals in this object from \a receiver's \a + member. + + A signal-slot connection is removed when either of the objects + involved are destroyed. +*/ + +/*! + Disconnects \a signal in object \a sender from \a member in object + \a receiver. + + A signal-slot connection is removed when either of the objects + involved are destroyed. + + disconnect() is typically used in three ways, as the following + examples demonstrate. + \list 1 + \i Disconnect everything connected to an object's signals: + \code + disconnect( myObject, 0, 0, 0 ); + \endcode + equivalent to the non-static overloaded function + \code + myObject->disconnect(); + \endcode + \i Disconnect everything connected to a specific signal: + \code + disconnect( myObject, SIGNAL(mySignal()), 0, 0 ); + \endcode + equivalent to the non-static overloaded function + \code + myObject->disconnect( SIGNAL(mySignal()) ); + \endcode + \i Disconnect a specific receiver: + \code + disconnect( myObject, 0, myReceiver, 0 ); + \endcode + equivalent to the non-static overloaded function + \code + myObject->disconnect( myReceiver ); + \endcode + \endlist + + 0 may be used as a wildcard, meaning "any signal", "any receiving + object", or "any slot in the receiving object", respectively. + + The \a sender may never be 0. (You cannot disconnect signals from + more than one object in a single call.) + + If \a signal is 0, it disconnects \a receiver and \a member from + any signal. If not, only the specified signal is disconnected. + + If \a receiver is 0, it disconnects anything connected to \a + signal. If not, slots in objects other than \a receiver are not + disconnected. + + If \a member is 0, it disconnects anything that is connected to \a + receiver. If not, only slots named \a member will be disconnected, + and all other slots are left alone. The \a member must be 0 if \a + receiver is left out, so you cannot disconnect a + specifically-named slot on all objects. + + \sa connect() +*/ + +bool TQObject::disconnect( const TQObject *sender, const char *signal, + const TQObject *receiver, const char *member ) +{ +#if defined(QT_CHECK_NULL) + if ( sender == 0 || (receiver == 0 && member != 0) ) { + qWarning( "TQObject::disconnect: Unexpected null parameter" ); + return FALSE; + } +#endif + if ( !sender->connections ) // no connected signals + return FALSE; + TQObject *s = (TQObject *)sender; + TQObject *r = (TQObject *)receiver; + int member_index = -1; + int membcode = -1; + TQCString nw_member; + if ( member ) { + membcode = member[0] - '0'; +#if defined(QT_CHECK_RANGE) + if ( !check_member_code( membcode, r, member, "disconnect" ) ) + return FALSE; +#endif + ++member; + TQMetaObject *rmeta = r->metaObject(); + + switch ( membcode ) { // get receiver member + case TQSLOT_CODE: + member_index = rmeta->findSlot( member, TRUE ); + if ( member_index < 0 ) { // normalize and retry + nw_member = qt_rmWS(member); // remove whitespace + member = nw_member; + member_index = rmeta->findSlot( member, TRUE ); + } + break; + case TQSIGNAL_CODE: + member_index = rmeta->findSignal( member, TRUE ); + if ( member_index < 0 ) { // normalize and retry + nw_member = qt_rmWS(member); // remove whitespace + member = nw_member; + member_index = rmeta->findSignal( member, TRUE ); + } + break; + } + if ( member_index < 0 ) { // no such member +#if defined(QT_CHECK_RANGE) + err_member_notfound( membcode, r, member, "disconnect" ); + err_info_about_candidates( membcode, rmeta, member, "connect" ); + err_info_about_objects( "disconnect", sender, receiver ); +#endif + return FALSE; + } + } + + if ( signal == 0 ) { // any/all signals + if ( disconnectInternal( s, -1, r, membcode, member_index ) ) + s->disconnectNotify( 0 ); + else + return FALSE; + } else { // specific signal +#if defined(QT_CHECK_RANGE) + if ( !check_signal_macro( s, signal, "disconnect", "unbind" ) ) + return FALSE; +#endif + TQCString nw_signal(signal); // Assume already normalized + ++signal; // skip member type code + + TQMetaObject *smeta = s->metaObject(); + if ( !smeta ) // no meta object + return FALSE; + int signal_index = smeta->findSignal( signal, TRUE ); + if ( signal_index < 0 ) { // normalize and retry + nw_signal = qt_rmWS( signal-1 ); // remove whitespace + signal = nw_signal.data()+1; // skip member type code + signal_index = smeta->findSignal( signal, TRUE ); + } + if ( signal_index < 0 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQObject::disconnect: No such signal %s::%s", + s->className(), signal ); +#endif + return FALSE; + } + + /* compatibility and safety: If a receiver has several slots + * with the same name, disconnect them all*/ + bool res = FALSE; + if ( membcode == TQSLOT_CODE && r ) { + TQMetaObject * rmeta = r->metaObject(); + do { + int mi = rmeta->findSlot( member ); + if ( mi != -1 ) + res |= disconnectInternal( s, signal_index, r, membcode, mi ); + } while ( (rmeta = rmeta->superClass()) ); + } else { + res = disconnectInternal( s, signal_index, r, membcode, member_index ); + } + if ( res ) + s->disconnectNotify( nw_signal ); + return res; + } + return TRUE; +} + +/*! \internal */ + +bool TQObject::disconnectInternal( const TQObject *sender, int signal_index, + const TQObject *receiver, int membcode, int member_index ) +{ + TQObject *s = (TQObject*)sender; + TQObject *r = (TQObject*)receiver; + + if ( !s->connections ) + return FALSE; + + bool success = FALSE; + TQConnectionList *clist; + register TQConnection *c; + if ( signal_index == -1 ) { + for ( int i = 0; i < (int) s->connections->size(); i++ ) { + clist = (*s->connections)[i]; // for all signals... + if ( !clist ) + continue; + c = clist->first(); + while ( c ) { // for all receivers... + if ( r == 0 ) { // remove all receivers + removeObjFromList( c->object()->senderObjects, s ); + success = TRUE; + c = clist->next(); + } else if ( r == c->object() && + ( member_index == -1 || + member_index == c->member() && c->memberType() == membcode ) ) { + removeObjFromList( c->object()->senderObjects, s, TRUE ); + success = TRUE; + clist->remove(); + c = clist->current(); + } else { + c = clist->next(); + } + } + if ( r == 0 ) // disconnect all receivers + s->connections->insert( i, 0 ); + } + } else { + clist = s->connections->at( signal_index ); + if ( !clist ) + return FALSE; + + c = clist->first(); + while ( c ) { // for all receivers... + if ( r == 0 ) { // remove all receivers + removeObjFromList( c->object()->senderObjects, s, TRUE ); + success = TRUE; + c = clist->next(); + } else if ( r == c->object() && + ( member_index == -1 || + member_index == c->member() && c->memberType() == membcode ) ) { + removeObjFromList( c->object()->senderObjects, s, TRUE ); + success = TRUE; + clist->remove(); + c = clist->current(); + } else { + c = clist->next(); + } + } + if ( r == 0 ) // disconnect all receivers + s->connections->insert( signal_index, 0 ); + } + return success; +} + +/*! + \fn TQObject::destroyed() + + This signal is emitted when the object is being destroyed. + + Note that the signal is emitted by the TQObject destructor, so + the object's virtual table is already degenerated at this point, + and it is not safe to call any functions on the object emitting + the signal. This signal can not be blocked. + + All the objects's children are destroyed immediately after this + signal is emitted. +*/ + +/*! + \overload TQObject::destroyed( TQObject* obj) + + This signal is emitted immediately before the object \a obj is + destroyed, and can not be blocked. + + All the objects's children are destroyed immediately after this + signal is emitted. +*/ + +/*! + Performs a deferred deletion of this object. + + Instead of an immediate deletion this function schedules a + deferred delete event for processing when TQt returns to the main + event loop. +*/ +void TQObject::deleteLater() +{ + TQApplication::postEvent( this, new TQEvent( TQEvent::DeferredDelete) ); +} + +/*! + This slot is connected to the destroyed() signal of other objects + that have installed event filters on this object. When the other + object, \a obj, is destroyed, we want to remove its event filter. +*/ + +void TQObject::cleanupEventFilter(TQObject* obj) +{ + removeEventFilter( obj ); +} + + +/*! + \fn TQString TQObject::tr( const char *sourceText, const char * comment ) + \reentrant + + Returns a translated version of \a sourceText, or \a sourceText + itself if there is no appropriate translated version. The + translation context is TQObject with \a comment (0 by default). + All TQObject subclasses using the Q_OBJECT macro automatically have + a reimplementation of this function with the subclass name as + context. + + \warning This method is reentrant only if all translators are + installed \e before calling this method. Installing or removing + translators while performing translations is not supported. Doing + so will probably result in crashes or other undesirable behavior. + + \sa trUtf8() TQApplication::translate() + \link i18n.html Internationalization with TQt\endlink +*/ + +/*! + \fn TQString TQObject::trUtf8( const char *sourceText, + const char *comment ) + \reentrant + + Returns a translated version of \a sourceText, or + TQString::fromUtf8(\a sourceText) if there is no appropriate + version. It is otherwise identical to tr(\a sourceText, \a + comment). + + \warning This method is reentrant only if all translators are + installed \e before calling this method. Installing or removing + translators while performing translations is not supported. Doing + so will probably result in crashes or other undesirable behavior. + + \sa tr() TQApplication::translate() +*/ + +static TQMetaObjectCleanUp cleanUp_TQt = TQMetaObjectCleanUp( "TQObject", &TQObject::staticMetaObject ); + +TQMetaObject* TQObject::staticTQtMetaObject() +{ + static TQMetaObject* qtMetaObject = 0; + if ( qtMetaObject ) + return qtMetaObject; + +#ifndef QT_NO_PROPERTIES + static const TQMetaEnum::Item enum_0[] = { + { "AlignLeft", (int) TQt::AlignLeft }, + { "AlignRight", (int) TQt::AlignRight }, + { "AlignHCenter", (int) TQt::AlignHCenter }, + { "AlignTop", (int) TQt::AlignTop }, + { "AlignBottom", (int) TQt::AlignBottom }, + { "AlignVCenter", (int) TQt::AlignVCenter }, + { "AlignCenter", (int) TQt::AlignCenter }, + { "AlignAuto", (int) TQt::AlignAuto }, + { "AlignJustify", (int) TQt::AlignJustify }, + { "WordBreak", (int) TQt::WordBreak } + }; + + static const TQMetaEnum::Item enum_1[] = { + { "Horizontal", (int) TQt::Horizontal }, + { "Vertical", (int) TQt::Vertical } + }; + + static const TQMetaEnum::Item enum_2[] = { + { "PlainText", (int) TQt::PlainText }, + { "RichText", (int) TQt::RichText }, + { "AutoText", (int) TQt::AutoText }, + { "LogText", (int) TQt::LogText } + }; + + static const TQMetaEnum::Item enum_3[] = { + { "NoBackground", (int) TQt::NoBackground }, + { "PaletteForeground", (int) TQt::PaletteForeground }, + { "PaletteButton", (int) TQt::PaletteButton }, + { "PaletteLight", (int) TQt::PaletteLight }, + { "PaletteMidlight", (int) TQt::PaletteMidlight }, + { "PaletteDark", (int) TQt::PaletteDark }, + { "PaletteMid", (int) TQt::PaletteMid }, + { "PaletteText", (int) TQt::PaletteText }, + { "PaletteBrightText", (int) TQt::PaletteBrightText }, + { "PaletteBase", (int) TQt::PaletteBase }, + { "PaletteBackground", (int) TQt::PaletteBackground }, + { "PaletteShadow", (int) TQt::PaletteShadow }, + { "PaletteHighlight", (int) TQt::PaletteHighlight }, + { "PaletteHighlightedText", (int) TQt::PaletteHighlightedText }, + { "PaletteButtonText", (int) TQt::PaletteButtonText }, + { "PaletteLink", (int) TQt::PaletteLink }, + { "PaletteLinkVisited", (int) TQt::PaletteLinkVisited } + }; + + static const TQMetaEnum::Item enum_4[] = { + { "TextDate", (int) TQt::TextDate }, + { "ISODate", (int) TQt::ISODate }, + { "LocalDate", (int) TQt::LocalDate } + }; + + + static const TQMetaEnum enum_tbl[] = { + { "Alignment", 10, enum_0, TRUE }, + { "Orientation", 2, enum_1, FALSE }, + { "TextFormat", 4, enum_2, FALSE }, + { "BackgroundMode", 17, enum_3, FALSE }, + { "DateFormat", 3, enum_4, FALSE } + }; +#endif + + qtMetaObject = new TQMetaObject( "TQt", 0, + 0, 0, + 0, 0, +#ifndef QT_NO_PROPERTIES + 0, 0, + enum_tbl, 5, +#endif + 0, 0 ); + cleanUp_TQt.setMetaObject( qtMetaObject ); + + return qtMetaObject; +} + +/*! + \internal + + Signal activation with the most frequently used parameter/argument + types. All other combinations are generated by the meta object + compiler. + */ +void TQObject::activate_signal( int signal ) +{ +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY + if ( qt_preliminary_signal_spy ) { + if ( !signalsBlocked() && signal >= 0 && + ( !connections || !connections->at( signal ) ) ) { + TQUObject o[1]; + qt_spy_signal( this, signal, o ); + return; + } + } +#endif + + if ( !connections || signalsBlocked() || signal < 0 ) + return; + TQConnectionList *clist = connections->at( signal ); + if ( !clist ) + return; + TQUObject o[1]; + activate_signal( clist, o ); +} + +/*! \internal */ + +void TQObject::activate_signal( TQConnectionList *clist, TQUObject *o ) +{ + if ( !clist ) + return; + +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY + if ( qt_preliminary_signal_spy ) + qt_spy_signal( this, connections->findRef( clist), o ); +#endif + + TQObject *object; + TQSenderObjectList* sol; + TQObject* oldSender = 0; + TQConnection *c; + if ( clist->count() == 1 ) { // save iterator + c = clist->first(); + object = c->object(); + sol = object->senderObjects; + if ( sol ) { + oldSender = sol->currentSender; + sol->ref(); + sol->currentSender = this; + } + if ( c->memberType() == TQSIGNAL_CODE ) + object->qt_emit( c->member(), o ); + else + object->qt_invoke( c->member(), o ); + if ( sol ) { + sol->currentSender = oldSender; + if ( sol->deref() ) + delete sol; + } + } else { + TQConnection *cd = 0; + TQConnectionListIt it(*clist); + while ( (c=it.current()) ) { + ++it; + if ( c == cd ) + continue; + cd = c; + object = c->object(); + sol = object->senderObjects; + if ( sol ) { + oldSender = sol->currentSender; + sol->ref(); + sol->currentSender = this; + } + if ( c->memberType() == TQSIGNAL_CODE ) + object->qt_emit( c->member(), o ); + else + object->qt_invoke( c->member(), o ); + if (sol ) { + sol->currentSender = oldSender; + if ( sol->deref() ) + delete sol; + } + } + } +} + +/*! + \overload void TQObject::activate_signal( int signal, int ) +*/ + +/*! + \overload void TQObject::activate_signal( int signal, double ) +*/ + +/*! + \overload void TQObject::activate_signal( int signal, TQString ) +*/ + +/*! + \fn void TQObject::activate_signal_bool( int signal, bool ) + \internal + + Like the above functions, but since bool is sometimes + only a typedef it cannot be a simple overload. +*/ + +#ifndef QT_NO_PRELIMINARY_SIGNAL_SPY +#define ACTIVATE_SIGNAL_WITH_PARAM(FNAME,TYPE) \ +void TQObject::FNAME( int signal, TYPE param ) \ +{ \ + if ( qt_preliminary_signal_spy ) { \ + if ( !signalsBlocked() && signal >= 0 && \ + ( !connections || !connections->at( signal ) ) ) { \ + TQUObject o[2]; \ + static_QUType_##TYPE.set( o+1, param ); \ + qt_spy_signal( this, signal, o ); \ + return; \ + } \ + } \ + if ( !connections || signalsBlocked() || signal < 0 ) \ + return; \ + TQConnectionList *clist = connections->at( signal ); \ + if ( !clist ) \ + return; \ + TQUObject o[2]; \ + static_QUType_##TYPE.set( o+1, param ); \ + activate_signal( clist, o ); \ +} +#else +#define ACTIVATE_SIGNAL_WITH_PARAM(FNAME,TYPE) \ +void TQObject::FNAME( int signal, TYPE param ) \ +{ \ + if ( !connections || signalsBlocked() || signal < 0 ) \ + return; \ + TQConnectionList *clist = connections->at( signal ); \ + if ( !clist ) \ + return; \ + TQUObject o[2]; \ + static_QUType_##TYPE.set( o+1, param ); \ + activate_signal( clist, o ); \ +} + +#endif +// We don't want to duplicate too much text so... + +ACTIVATE_SIGNAL_WITH_PARAM( activate_signal, int ) +ACTIVATE_SIGNAL_WITH_PARAM( activate_signal, double ) +ACTIVATE_SIGNAL_WITH_PARAM( activate_signal, TQString ) +ACTIVATE_SIGNAL_WITH_PARAM( activate_signal_bool, bool ) + + +/***************************************************************************** + TQObject debugging output routines. + *****************************************************************************/ + +static void dumpRecursive( int level, TQObject *object ) +{ +#if defined(QT_DEBUG) + if ( object ) { + TQString buf; + buf.fill( '\t', level/2 ); + if ( level % 2 ) + buf += " "; + const char *name = object->name(); + TQString flags=""; + if ( qApp->focusWidget() == object ) + flags += 'F'; + if ( object->isWidgetType() ) { + TQWidget * w = (TQWidget *)object; + if ( w->isVisible() ) { + TQString t( "<%1,%2,%3,%4>" ); + flags += t.arg(w->x()).arg(w->y()).arg(w->width()).arg(w->height()); + } else { + flags += 'I'; + } + } + qDebug( "%s%s::%s %s", (const char*)buf, object->className(), name, + flags.latin1() ); + if ( object->children() ) { + TQObjectListIt it(*object->children()); + TQObject * c; + while ( (c=it.current()) != 0 ) { + ++it; + dumpRecursive( level+1, c ); + } + } + } +#else + Q_UNUSED( level ) + Q_UNUSED( object ) +#endif +} + +/*! + Dumps a tree of children to the debug output. + + This function is useful for debugging, but does nothing if the + library has been compiled in release mode (i.e. without debugging + information). +*/ + +void TQObject::dumpObjectTree() +{ + dumpRecursive( 0, this ); +} + +/*! + Dumps information about signal connections, etc. for this object + to the debug output. + + This function is useful for debugging, but does nothing if the + library has been compiled in release mode (i.e. without debugging + information). +*/ + +void TQObject::dumpObjectInfo() +{ +#if defined(QT_DEBUG) + qDebug( "OBJECT %s::%s", className(), name( "unnamed" ) ); + int n = 0; + qDebug( " SIGNALS OUT" ); + if ( connections ) { + TQConnectionList *clist; + for ( uint i = 0; i < connections->size(); i++ ) { + if ( ( clist = connections->at( i ) ) ) { + qDebug( "\t%s", metaObject()->signal( i, TRUE )->name ); + n++; + register TQConnection *c; + TQConnectionListIt cit(*clist); + while ( (c=cit.current()) ) { + ++cit; + qDebug( "\t --> %s::%s %s", c->object()->className(), + c->object()->name( "unnamed" ), c->memberName() ); + } + } + } + } + if ( n == 0 ) + qDebug( "\t" ); + + qDebug( " SIGNALS IN" ); + n = 0; + if ( senderObjects ) { + TQObject *sender = senderObjects->first(); + while ( sender ) { + qDebug( "\t%s::%s", + sender->className(), sender->name( "unnamed" ) ); + n++; + sender = senderObjects->next(); + } + } + if ( n == 0 ) + qDebug( "\t" ); +#endif +} + +#ifndef QT_NO_PROPERTIES + +/*! + Sets the value of the object's \a name property to \a value. + + Returns TRUE if the operation was successful; otherwise returns + FALSE. + + Information about all available properties is provided through the + metaObject(). + + \sa property(), metaObject(), TQMetaObject::propertyNames(), TQMetaObject::property() +*/ +bool TQObject::setProperty( const char *name, const TQVariant& value ) +{ + if ( !value.isValid() ) + return FALSE; + + TQVariant v = value; + + TQMetaObject* meta = metaObject(); + if ( !meta ) + return FALSE; + int id = meta->findProperty( name, TRUE ); + const TQMetaProperty* p = meta->property( id, TRUE ); + if ( !p || !p->isValid() || !p->writable() ) { + qWarning( "%s::setProperty( \"%s\", value ) failed: property invalid, read-only or does not exist", + className(), name ); + return FALSE; + } + + if ( p->isEnumType() ) { + if ( v.type() == TQVariant::String || v.type() == TQVariant::CString ) { + if ( p->isSetType() ) { + TQString s = value.toString(); + // TQStrList does not support split, use TQStringList for that. + TQStringList l = TQStringList::split( '|', s ); + TQStrList keys; + for ( TQStringList::Iterator it = l.begin(); it != l.end(); ++it ) + keys.append( (*it).stripWhiteSpace().latin1() ); + v = TQVariant( p->keysToValue( keys ) ); + } else { + v = TQVariant( p->keyToValue( value.toCString().data() ) ); + } + } else if ( v.type() != TQVariant::Int && v.type() != TQVariant::UInt ) { + return FALSE; + } + return qt_property( id, 0, &v ); + } + + TQVariant::Type type = (TQVariant::Type)(p->flags >> 24); + if ( type == TQVariant::Invalid ) + type = TQVariant::nameToType( p->type() ); + if ( type != TQVariant::Invalid && !v.canCast( type ) ) + return FALSE; + return qt_property( id, 0, &v ); +} + +/*! + Returns the value of the object's \a name property. + + If no such property exists, the returned variant is invalid. + + Information about all available properties are provided through + the metaObject(). + + \sa setProperty(), TQVariant::isValid(), metaObject(), + TQMetaObject::propertyNames(), TQMetaObject::property() +*/ +TQVariant TQObject::property( const char *name ) const +{ + TQVariant v; + TQMetaObject* meta = metaObject(); + if ( !meta ) + return v; + int id = meta->findProperty( name, TRUE ); + const TQMetaProperty* p = meta->property( id, TRUE ); + if ( !p || !p->isValid() ) { + qWarning( "%s::property( \"%s\" ) failed: property invalid or does not exist", + className(), name ); + return v; + } + TQObject* that = (TQObject*) this; // moc ensures constness for the qt_property call + that->qt_property( id, 1, &v ); + return v; +} + +#endif // QT_NO_PROPERTIES + +#ifndef QT_NO_USERDATA +/*!\internal + */ +uint TQObject::registerUserData() +{ + static int user_data_registration = 0; + return user_data_registration++; +} + +/*!\internal + */ +TQObjectUserData::~TQObjectUserData() +{ +} + +/*!\internal + */ +void TQObject::setUserData( uint id, TQObjectUserData* data) +{ + if ( !d ) + d = new TQObjectPrivate( id+1 ); + if ( id >= d->size() ) + d->resize( id+1 ); + d->insert( id, data ); +} + +/*!\internal + */ +TQObjectUserData* TQObject::userData( uint id ) const +{ + if ( d && id < d->size() ) + return d->at( id ); + return 0; +} + +#endif // QT_NO_USERDATA diff --git a/src/kernel/qobject.h b/src/kernel/qobject.h new file mode 100644 index 000000000..ae5326740 --- /dev/null +++ b/src/kernel/qobject.h @@ -0,0 +1,265 @@ +/**************************************************************************** +** +** Definition of TQObject class +** +** Created : 930418 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQOBJECT_H +#define TQOBJECT_H + +#ifndef QT_H +#include "qobjectdefs.h" +#include "qwindowdefs.h" +#include "qstring.h" +#include "qevent.h" +#include "qnamespace.h" +#endif // QT_H + +#define QT_TR_NOOP(x) (x) +#define QT_TRANSLATE_NOOP(scope,x) (x) + +class TQMetaObject; +class TQVariant; +class TQMetaProperty; +class TQPostEventList; +class TQSenderObjectList; +class TQObjectPrivate; +#ifndef QT_NO_USERDATA +class TQObjectUserData; +#endif +struct TQUObject; + +class Q_EXPORT TQObject: public TQt +{ + Q_OBJECT + Q_PROPERTY( TQCString name READ name WRITE setName ) + +public: + TQObject( TQObject *parent=0, const char *name=0 ); + virtual ~TQObject(); + +#ifdef Q_QDOC + virtual const char *className() const; + static TQString tr( const char *, const char * ); + static TQString trUtf8( const char *, const char * ); + virtual TQMetaObject *metaObject() const; +#endif + + virtual bool event( TQEvent * ); + virtual bool eventFilter( TQObject *, TQEvent * ); + + bool isA( const char * ) const; + bool inherits( const char * ) const; + + const char *name() const; + const char *name( const char * defaultName ) const; + + virtual void setName( const char *name ); + bool isWidgetType() const { return isWidget; } + bool highPriority() const { return FALSE; } + + bool signalsBlocked() const { return blockSig; } + void blockSignals( bool b ); + + int startTimer( int interval ); + void killTimer( int id ); + void killTimers(); + + TQObject *child( const char *objName, const char *inheritsClass = 0, bool recursiveSearch = TRUE ); //### const in 4.0 + const TQObjectList *children() const { return childObjects; } + TQObjectList childrenListObject(); + const TQObjectList childrenListObject() const; + + static const TQObjectList *objectTrees(); + static const TQObjectList objectTreesListObject(); + + TQObjectList *queryList( const char *inheritsClass = 0, + const char *objName = 0, + bool regexpMatch = TRUE, + bool recursiveSearch = TRUE ) const; + + virtual void insertChild( TQObject * ); + virtual void removeChild( TQObject * ); + + void installEventFilter( const TQObject * ); + void removeEventFilter( const TQObject * ); + + static bool connect( const TQObject *sender, const char *signal, + const TQObject *receiver, const char *member ); + bool connect( const TQObject *sender, const char *signal, + const char *member ) const; + static bool disconnect( const TQObject *sender, const char *signal, + const TQObject *receiver, const char *member ); + bool disconnect( const char *signal=0, + const TQObject *receiver=0, const char *member=0 ); + bool disconnect( const TQObject *receiver, const char *member=0 ); + static void connectInternal( const TQObject *sender, int signal_index, + const TQObject *receiver, int membcode, int member_index ); + static bool disconnectInternal( const TQObject *sender, int signal_index, + const TQObject *receiver, int membcode, int member_index ); + + void dumpObjectTree(); + void dumpObjectInfo(); + +#ifndef QT_NO_PROPERTIES + virtual bool setProperty( const char *name, const TQVariant& value ); + virtual TQVariant property( const char *name ) const; +#endif // QT_NO_PROPERTIES +#ifdef QT_NO_TRANSLATION + static TQString tr( const char *sourceText, const char * = 0); +#ifndef QT_NO_TEXTCODEC + static TQString trUtf8( const char *sourceText, const char * = 0); +#endif +#endif //QT_NO_TRANSLATION + +#ifndef QT_NO_USERDATA + static uint registerUserData(); + void setUserData( uint id, TQObjectUserData* data); + TQObjectUserData* userData( uint id ) const; +#endif // QT_NO_USERDATA + +signals: + void destroyed(); + void destroyed( TQObject* obj ); + +public: + TQObject *parent() const { return parentObj; } + +public slots: + void deleteLater(); + +private slots: + void cleanupEventFilter( TQObject* ); + +protected: + bool activate_filters( TQEvent * ); + TQConnectionList *receivers( const char* signal ) const; + TQConnectionList *receivers( int signal ) const; + void activate_signal( int signal ); + void activate_signal( int signal, int ); + void activate_signal( int signal, double ); + void activate_signal( int signal, TQString ); + void activate_signal_bool( int signal, bool ); + void activate_signal( TQConnectionList *clist, TQUObject *o ); + + const TQObject *sender(); + + virtual void timerEvent( TQTimerEvent * ); + virtual void childEvent( TQChildEvent * ); + virtual void customEvent( TQCustomEvent * ); + + virtual void connectNotify( const char *signal ); + virtual void disconnectNotify( const char *signal ); + virtual bool checkConnectArgs( const char *signal, const TQObject *receiver, + const char *member ); + static TQCString normalizeSignalSlot( const char *signalSlot ); + +private: + uint isSignal : 1; + uint isWidget : 1; + uint pendTimer : 1; + uint blockSig : 1; + uint wasDeleted : 1; + uint isTree : 1; + + const char *objname; + TQObject *parentObj; + TQObjectList *childObjects; + TQSignalVec *connections; + TQSenderObjectList *senderObjects; + TQObjectList *eventFilters; + TQPostEventList *postedEvents; + TQObjectPrivate* d; + + static TQMetaObject* staticTQtMetaObject(); + + friend class TQApplication; + friend class TQBaseApplication; + friend class TQWidget; + friend class TQSignal; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQObject( const TQObject & ); + TQObject &operator=( const TQObject & ); +#endif +}; + + +#ifndef QT_NO_USERDATA +class Q_EXPORT TQObjectUserData { +public: + virtual ~TQObjectUserData(); +}; +#endif + + +inline bool TQObject::connect( const TQObject *sender, const char *signal, + const char *member ) const +{ + return connect( sender, signal, this, member ); +} + + +inline bool TQObject::disconnect( const char *signal, + const TQObject *receiver, const char *member ) +{ + return disconnect( this, signal, receiver, member ); +} + + +inline bool TQObject::disconnect( const TQObject *receiver, const char *member ) +{ + return disconnect( this, 0, receiver, member ); +} + + +#ifdef QT_NO_TRANSLATION +inline TQString TQObject::tr( const char *sourceText, const char * ) { + return TQString::fromLatin1( sourceText ); +} +#ifndef QT_NO_TEXTCODEC +inline TQString TQObject::trUtf8( const char *sourceText, const char * ) { + return TQString::fromUtf8( sourceText ); +} +#endif +#endif //QT_NO_TRANSLATION + + +#define Q_DEFINED_QOBJECT +#include "qwinexport.h" +#endif // TQOBJECT_H diff --git a/src/kernel/qobjectcleanuphandler.cpp b/src/kernel/qobjectcleanuphandler.cpp new file mode 100644 index 000000000..1b4ab3776 --- /dev/null +++ b/src/kernel/qobjectcleanuphandler.cpp @@ -0,0 +1,172 @@ +/**************************************************************************** +** +** Implementation of TQObjectCleanupHandler class +** +** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qobjectcleanuphandler.h" +#include "qobjectlist.h" + +/*! + \class TQObjectCleanupHandler qobjectcleanuphandler.h + \brief The TQObjectCleanupHandler class watches the lifetime of multiple TQObjects. + + \ingroup objectmodel + + A TQObjectCleanupHandler is useful whenever you need to know when a + number of \l{TQObject}s that are owned by someone else have been + deleted. This is important, for example, when referencing memory + in an application that has been allocated in a shared library. + + Example: + + \code + class FactoryComponent : public FactoryInterface, public TQLibraryInterface + { + public: + ... + + TQObject *createObject(); + + bool init(); + void cleanup(); + bool canUnload() const; + + private: + TQObjectCleanupHandler objects; + }; + + // allocate a new object, and add it to the cleanup handler + TQObject *FactoryComponent::createObject() + { + return objects.add( new TQObject() ); + } + + // TQLibraryInterface implementation + bool FactoryComponent::init() + { + return TRUE; + } + + void FactoryComponent::cleanup() + { + } + + // it is only safe to unload the library when all TQObject's have been destroyed + bool FactoryComponent::canUnload() const + { + return objects.isEmpty(); + } + \endcode +*/ + +/*! + Constructs an empty TQObjectCleanupHandler. +*/ +TQObjectCleanupHandler::TQObjectCleanupHandler() +: TQObject(), cleanupObjects( 0 ) +{ +} + +/*! + Destroys the cleanup handler. All objects in this cleanup handler + will be deleted. +*/ +TQObjectCleanupHandler::~TQObjectCleanupHandler() +{ + clear(); +} + +/*! + Adds \a object to this cleanup handler and returns the pointer to + the object. +*/ +TQObject* TQObjectCleanupHandler::add( TQObject* object ) +{ + if ( !object ) + return 0; + + if ( !cleanupObjects ) { + cleanupObjects = new TQObjectList; + cleanupObjects->setAutoDelete( TRUE ); + } + connect( object, SIGNAL(destroyed(TQObject*)), this, SLOT(objectDestroyed(TQObject*)) ); + cleanupObjects->insert( 0, object ); + return object; +} + +/*! + Removes the \a object from this cleanup handler. The object will + not be destroyed. +*/ +void TQObjectCleanupHandler::remove( TQObject *object ) +{ + if ( !cleanupObjects ) + return; + if ( cleanupObjects->findRef( object ) >= 0 ) { + (void) cleanupObjects->take(); + disconnect( object, SIGNAL(destroyed(TQObject*)), this, SLOT(objectDestroyed(TQObject*)) ); + } +} + +/*! + Returns TRUE if this cleanup handler is empty or if all objects in + this cleanup handler have been destroyed; otherwise return FALSE. +*/ +bool TQObjectCleanupHandler::isEmpty() const +{ + return cleanupObjects ? cleanupObjects->isEmpty() : TRUE; +} + +/*! + Deletes all objects in this cleanup handler. The cleanup handler + becomes empty. +*/ +void TQObjectCleanupHandler::clear() +{ + delete cleanupObjects; + cleanupObjects = 0; +} + +void TQObjectCleanupHandler::objectDestroyed( TQObject*object ) +{ + if ( cleanupObjects ) + cleanupObjects->setAutoDelete( FALSE ); + + remove( object ); + + if ( cleanupObjects ) + cleanupObjects->setAutoDelete( TRUE ); +} diff --git a/src/kernel/qobjectcleanuphandler.h b/src/kernel/qobjectcleanuphandler.h new file mode 100644 index 000000000..72a661c2b --- /dev/null +++ b/src/kernel/qobjectcleanuphandler.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Definition of ??? +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQOBJECTCLEANUPHANDLER_H +#define TQOBJECTCLEANUPHANDLER_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +class TQObjectList; + +class Q_EXPORT TQObjectCleanupHandler : public TQObject +{ + Q_OBJECT + +public: + TQObjectCleanupHandler(); + ~TQObjectCleanupHandler(); + + TQObject* add( TQObject* object ); + void remove( TQObject *object ); + bool isEmpty() const; + void clear(); + +private: + TQObjectList *cleanupObjects; + +private slots: + void objectDestroyed( TQObject * ); +}; + +#endif // TQOBJECTCLEANUPHANDLER_H diff --git a/src/kernel/qobjectdefs.h b/src/kernel/qobjectdefs.h new file mode 100644 index 000000000..335fb3805 --- /dev/null +++ b/src/kernel/qobjectdefs.h @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** Macros and definitions related to TQObject +** +** Created : 930419 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQOBJECTDEFS_H +#define TQOBJECTDEFS_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + + +#ifndef QT_NO_TRANSLATION +# ifndef QT_NO_TEXTCODEC +// full set of tr functions +# define QT_TR_FUNCTIONS \ + static TQString tr( const char *, const char * = 0 ); \ + static TQString trUtf8( const char *, const char * = 0 ); +# else +// no TQTextCodec, no utf8 +# define QT_TR_FUNCTIONS \ + static TQString tr( const char *, const char * = 0 ); +# endif +#else +// inherit the ones from TQObject +# define QT_TR_FUNCTIONS +#endif + +#ifndef QT_NO_PROPERTIES +# define QT_PROP_FUNCTIONS \ + virtual bool qt_property( int id, int f, TQVariant* v); \ + static bool qt_static_property( TQObject* , int, int, TQVariant* ); +#else +# define QT_PROP_FUNCTIONS +#endif + +// The following macros are our "extensions" to C++ +// They are used, strictly speaking, only by the moc. +struct TQUObject; + +#ifdef QT_MOC_CPP +#define slots slots +#define signals signals +#define Q_CLASSINFO( name, value ) Q_CLASSINFO( name, value ) +#define Q_PROPERTY( text ) Q_PROPERTY( text ) +#define Q_OVERRIDE( text ) Q_OVERRIDE( text ) +#define Q_ENUMS( x ) Q_ENUMS( x ) +#define Q_SETS( x ) Q_SETS( x ) + /* tmake ignore Q_OBJECT */ +#define Q_OBJECT Q_OBJECT + /* tmake ignore Q_OBJECT */ +#define Q_OBJECT_FAKE Q_OBJECT_FAKE + +#else +#define slots // slots: in class +#define signals protected // signals: in class +#ifndef QT_NO_EMIT +#define emit // emit signal +#endif +#define Q_CLASSINFO( name, value ) // class info +#define Q_PROPERTY( text ) // property +#define Q_OVERRIDE( text ) // override property +#define Q_ENUMS( x ) +#define Q_SETS( x ) + +/* tmake ignore Q_OBJECT */ +#define Q_OBJECT \ +public: \ + virtual TQMetaObject *metaObject() const { \ + return staticMetaObject(); \ + } \ + virtual const char *className() const; \ + virtual void* qt_cast( const char* ); \ + virtual bool qt_invoke( int, TQUObject* ); \ + virtual bool qt_emit( int, TQUObject* ); \ + QT_PROP_FUNCTIONS \ + static TQMetaObject* staticMetaObject(); \ + TQObject* qObject() { return (TQObject*)this; } \ + QT_TR_FUNCTIONS \ +private: \ + static TQMetaObject *metaObj; + +/* tmake ignore Q_OBJECT */ +#define Q_OBJECT_FAKE Q_OBJECT + +#endif + +// macro for naming members +#ifdef METHOD +#undef METHOD +#endif +#ifdef SLOT +#undef SLOT +#endif +#ifdef SIGNAL +#undef SIGNAL +#endif + +#if defined(_OLD_CPP_) +#define METHOD(a) "0""a" +#define SLOT(a) "1""a" +#define SIGNAL(a) "2""a" +#else +#define METHOD(a) "0"#a +#define SLOT(a) "1"#a +#define SIGNAL(a) "2"#a +#endif + +#ifndef QT_CLEAN_NAMESPACE +#define METHOD_CODE 0 // member type codes +#define SLOT_CODE 1 +#define SIGNAL_CODE 2 +#endif + +#define TQMETHOD_CODE 0 // member type codes +#define TQSLOT_CODE 1 +#define TQSIGNAL_CODE 2 + +class TQObject; +class TQMetaObject; +class TQSignal; +class TQConnection; +class TQEvent; +struct TQMetaData; +class TQConnectionList; +class TQConnectionListIt; +class TQSignalVec; +class TQObjectList; +class TQObjectListIt; +class TQMemberDict; + +Q_EXPORT void *qt_find_obj_child( TQObject *, const char *, const char * ); +#define Q_CHILD(parent,type,name) \ + ((type*)qt_find_obj_child(parent,#type,name)) + +Q_EXPORT void *qt_inheritedBy( TQMetaObject *super, const TQObject *cls ); + +template +Q_INLINE_TEMPLATES T qt_cast(const TQObject *object) +{ return (T)qt_inheritedBy( ((T)0)->staticMetaObject(), object ); } +#endif // TQOBJECTDEFS_H diff --git a/src/kernel/qobjectdict.h b/src/kernel/qobjectdict.h new file mode 100644 index 000000000..0b10e3877 --- /dev/null +++ b/src/kernel/qobjectdict.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Definition of TQObjectDictionary +** +** Created : 940807 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQOBJECTDICT_H +#define TQOBJECTDICT_H + +#ifndef QT_H +#include "qmetaobject.h" +#include "qasciidict.h" +#endif // QT_H + + +// +// The object dictionary is a collection of TQMetaObjects +// + +class Q_EXPORT TQObjectDictionary : public TQAsciiDict +{ +public: + TQObjectDictionary(int size=17,bool cs=TRUE,bool ck=TRUE) + : TQAsciiDict(size,cs,ck) {} + TQObjectDictionary( const TQObjectDictionary &dict ) + : TQAsciiDict(dict) {} + ~TQObjectDictionary() { clear(); } + TQObjectDictionary &operator=(const TQObjectDictionary &dict) + { return (TQObjectDictionary&)TQAsciiDict::operator=(dict);} +}; + +#endif // TQOBJECTDICT_H diff --git a/src/kernel/qobjectlist.h b/src/kernel/qobjectlist.h new file mode 100644 index 000000000..557bf7b69 --- /dev/null +++ b/src/kernel/qobjectlist.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Definition of TQObjectList +** +** Created : 940807 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQOBJECTLIST_H +#define TQOBJECTLIST_H + +#ifndef QT_H +#include "qobject.h" +#include "qptrlist.h" +#endif // QT_H + + +#if defined(Q_TEMPLATEDLL) +//Q_TEMPLATE_EXTERN template class Q_EXPORT TQPtrList; +//Q_TEMPLATE_EXTERN template class Q_EXPORT TQPtrListIterator; +#endif + + +class Q_EXPORT TQObjectList : public TQPtrList +{ +public: + TQObjectList() : TQPtrList() {} + TQObjectList( const TQObjectList &list ) : TQPtrList(list) {} + ~TQObjectList() { clear(); } + TQObjectList &operator=(const TQObjectList &list) + { return (TQObjectList&)TQPtrList::operator=(list); } +}; + +class Q_EXPORT TQObjectListIterator : public TQPtrListIterator +{ +public: + TQObjectListIterator( const TQObjectList &l ) + : TQPtrListIterator( l ) { } + TQObjectListIterator &operator=( const TQObjectListIterator &i ) + { return (TQObjectListIterator&) + TQPtrListIterator::operator=( i ); } +}; + +#if (QT_VERSION-0 >= 0x040000) +#if defined(Q_CC_GNU) +#warning "remove the TQObjectListIt class" +#warning "remove the typedef too, maybe" +#endif +typedef TQObjectListIterator TQObjectListIt; +#else +class Q_EXPORT TQObjectListIt : public TQPtrListIterator +{ +public: + TQObjectListIt( const TQObjectList &l ) : TQPtrListIterator(l) {} + TQObjectListIt &operator=(const TQObjectListIt &i) + { return (TQObjectListIt&)TQPtrListIterator::operator=(i); } +}; +#endif + +#endif // TQOBJECTLIST_H diff --git a/src/kernel/qpaintdevice.h b/src/kernel/qpaintdevice.h new file mode 100644 index 000000000..d419e4e43 --- /dev/null +++ b/src/kernel/qpaintdevice.h @@ -0,0 +1,421 @@ +/**************************************************************************** +** +** Definition of TQPaintDevice class +** +** Created : 940721 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPAINTDEVICE_H +#define TQPAINTDEVICE_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qrect.h" +#endif // QT_H + +#if defined(Q_WS_QWS) +class TQWSDisplay; +class TQGfx; +#endif + +class TQIODevice; +class TQString; +class TQTextItem; + + +#if defined(Q_WS_X11) +struct TQPaintDeviceX11Data; +#endif + +union TQPDevCmdParam { + int ival; + int *ivec; + TQString *str; + const TQPoint *point; + const TQRect *rect; + const TQPointArray *ptarr; + const TQPixmap *pixmap; + const TQImage *image; + const TQColor *color; + const TQFont *font; + const TQPen *pen; + const TQBrush *brush; + const TQRegion *rgn; + const TQWMatrix *matrix; + const TQTextItem *textItem; + TQIODevice *device; +}; + + + +class Q_EXPORT TQPaintDevice // device for TQPainter +{ +public: + virtual ~TQPaintDevice(); + + int devType() const; + bool isExtDev() const; + bool paintingActive() const; + + virtual void setResolution( int ); + virtual int resolution() const; + + // Windows: get device context + // X-Windows: get drawable +#if defined(Q_WS_WIN) + virtual HDC handle() const; +#elif defined(Q_WS_X11) + virtual TQt::HANDLE handle() const; + virtual TQt::HANDLE x11RenderHandle() const; +#elif defined(Q_WS_MAC) + virtual TQt::HANDLE handle() const; +#elif defined(Q_WS_QWS) + virtual TQt::HANDLE handle() const; +#endif + +#if defined(Q_WS_X11) + Display *x11Display() const; + int x11Screen() const; + int x11Depth() const; + int x11Cells() const; + TQt::HANDLE x11Colormap() const; + bool x11DefaultColormap() const; + void *x11Visual() const; + bool x11DefaultVisual() const; + + static Display *x11AppDisplay(); + static int x11AppScreen(); + + static int x11AppDpiX(); + static int x11AppDpiY(); + static void x11SetAppDpiX(int); + static void x11SetAppDpiY(int); + static int x11AppDepth(); + static int x11AppCells(); + static TQt::HANDLE x11AppRootWindow(); + static TQt::HANDLE x11AppColormap(); + static bool x11AppDefaultColormap(); + static void *x11AppVisual(); + static bool x11AppDefaultVisual(); + + // ### in 4.0, the above need to go away, the below needs to take a -1 default + // argument, signifying the default screen... + static int x11AppDepth( int screen ); + static int x11AppCells( int screen ); + static TQt::HANDLE x11AppRootWindow( int screen ); + static TQt::HANDLE x11AppColormap( int screen ); + static void *x11AppVisual( int screen ); + static bool x11AppDefaultColormap( int screen ); + static bool x11AppDefaultVisual( int screen ); + static int x11AppDpiX( int ); + static int x11AppDpiY( int ); + static void x11SetAppDpiX( int, int ); + static void x11SetAppDpiY( int, int ); +#endif + +#if defined(Q_WS_QWS) + static TQWSDisplay *qwsDisplay(); + virtual unsigned char * scanLine(int) const; + virtual int bytesPerLine() const; + virtual TQGfx * graphicsContext(bool clip_children=TRUE) const; +#endif + + enum PDevCmd { + PdcNOP = 0, // + PdcDrawPoint = 1, // point + PdcDrawFirst = PdcDrawPoint, + PdcMoveTo = 2, // point + PdcLineTo = 3, // point + PdcDrawLine = 4, // point,point + PdcDrawRect = 5, // rect + PdcDrawRoundRect = 6, // rect,ival,ival + PdcDrawEllipse = 7, // rect + PdcDrawArc = 8, // rect,ival,ival + PdcDrawPie = 9, // rect,ival,ival + PdcDrawChord = 10, // rect,ival,ival + PdcDrawLineSegments = 11, // ptarr + PdcDrawPolyline = 12, // ptarr + PdcDrawPolygon = 13, // ptarr,ival + PdcDrawCubicBezier = 14, // ptarr + PdcDrawText = 15, // point,str + PdcDrawTextFormatted = 16, // rect,ival,str + PdcDrawPixmap = 17, // rect,pixmap + PdcDrawImage = 18, // rect,image + PdcDrawText2 = 19, // point,str + PdcDrawText2Formatted = 20, // rect,ival,str + PdcDrawTextItem = 21, + PdcDrawLast = PdcDrawTextItem, + + // no painting commands below PdcDrawLast. + + PdcBegin = 30, // + PdcEnd = 31, // + PdcSave = 32, // + PdcRestore = 33, // + PdcSetdev = 34, // device - PRIVATE + PdcSetBkColor = 40, // color + PdcSetBkMode = 41, // ival + PdcSetROP = 42, // ival + PdcSetBrushOrigin = 43, // point + PdcSetFont = 45, // font + PdcSetPen = 46, // pen + PdcSetBrush = 47, // brush + PdcSetTabStops = 48, // ival + PdcSetTabArray = 49, // ival,ivec + PdcSetUnit = 50, // ival + PdcSetVXform = 51, // ival + PdcSetWindow = 52, // rect + PdcSetViewport = 53, // rect + PdcSetWXform = 54, // ival + PdcSetWMatrix = 55, // matrix,ival + PdcSaveWMatrix = 56, + PdcRestoreWMatrix = 57, + PdcSetClip = 60, // ival + PdcSetClipRegion = 61, // rgn + + PdcReservedStart = 0, // codes 0-199 are reserved + PdcReservedStop = 199 // for TQt + }; + +protected: + TQPaintDevice( uint devflags ); + +#if defined(Q_WS_WIN) + HDC hdc; // device context +#elif defined(Q_WS_X11) + TQt::HANDLE hd; // handle to drawable + TQt::HANDLE rendhd; // handle to RENDER pict + + void copyX11Data( const TQPaintDevice * ); + void cloneX11Data( const TQPaintDevice * ); + virtual void setX11Data( const TQPaintDeviceX11Data* ); + TQPaintDeviceX11Data* getX11Data( bool def=FALSE ) const; +#elif defined(Q_WS_MAC) +#if !defined( TQMAC_NO_QUARTZ ) + CGContextRef ctx; +#endif + void * hd; +#elif defined(Q_WS_QWS) + TQt::HANDLE hd; +#endif + + virtual bool cmd( int, TQPainter *, TQPDevCmdParam * ); + virtual int metric( int ) const; + virtual int fontMet( TQFont *, int, const char * = 0, int = 0 ) const; + virtual int fontInf( TQFont *, int ) const; + + ushort devFlags; // device flags + ushort painters; // refcount + + friend class TQPainter; + friend class TQPaintDeviceMetrics; +#if defined(Q_WS_MAC) +#ifndef TQMAC_NO_QUARTZ + virtual CGContextRef macCGContext(bool clipped=TRUE) const; +#endif + friend Q_EXPORT void unclippedScaledBitBlt( TQPaintDevice *, int, int, int, int, + const TQPaintDevice *, int, int, int, int, TQt::RasterOp, bool, bool ); +#else + friend Q_EXPORT void bitBlt( TQPaintDevice *, int, int, + const TQPaintDevice *, + int, int, int, int, TQt::RasterOp, bool ); +#endif +#if defined(Q_WS_X11) + friend void qt_init_internal( int *, char **, Display *, TQt::HANDLE, TQt::HANDLE ); + friend void qt_cleanup(); +#endif + +private: +#if defined(Q_WS_X11) + static Display *x_appdisplay; + static int x_appscreen; + + static int x_appdepth; + static int x_appcells; + static TQt::HANDLE x_approotwindow; + static TQt::HANDLE x_appcolormap; + static bool x_appdefcolormap; + static void *x_appvisual; + static bool x_appdefvisual; + + // ### in 4.0, remove the above, and replace with the below + static int *x_appdepth_arr; + static int *x_appcells_arr; + static TQt::HANDLE *x_approotwindow_arr; + static TQt::HANDLE *x_appcolormap_arr; + static bool *x_appdefcolormap_arr; + static void **x_appvisual_arr; + static bool *x_appdefvisual_arr; + + TQPaintDeviceX11Data* x11Data; +#endif + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQPaintDevice( const TQPaintDevice & ); + TQPaintDevice &operator=( const TQPaintDevice & ); +#endif +}; + + +Q_EXPORT +void bitBlt( TQPaintDevice *dst, int dx, int dy, + const TQPaintDevice *src, int sx=0, int sy=0, int sw=-1, int sh=-1, + TQt::RasterOp = TQt::CopyROP, bool ignoreMask=FALSE ); + +Q_EXPORT +void bitBlt( TQPaintDevice *dst, int dx, int dy, + const TQImage *src, int sx=0, int sy=0, int sw=-1, int sh=-1, + int conversion_flags=0 ); + + +#if defined(Q_WS_X11) + +struct Q_EXPORT TQPaintDeviceX11Data : public TQShared { + Display* x_display; + int x_screen; + int x_depth; + int x_cells; + TQt::HANDLE x_colormap; + bool x_defcolormap; + void* x_visual; + bool x_defvisual; +}; + +#endif + +/***************************************************************************** + Inline functions + *****************************************************************************/ + +inline int TQPaintDevice::devType() const +{ return devFlags & TQInternal::DeviceTypeMask; } + +inline bool TQPaintDevice::isExtDev() const +{ return (devFlags & TQInternal::ExternalDevice) != 0; } + +inline bool TQPaintDevice::paintingActive() const +{ return painters != 0; } + +#if defined(Q_WS_X11) +inline Display *TQPaintDevice::x11Display() const +{ return x11Data ? x11Data->x_display : x_appdisplay; } + +inline int TQPaintDevice::x11Screen() const +{ return x11Data ? x11Data->x_screen : x_appscreen; } + +inline int TQPaintDevice::x11Depth() const +{ return x11Data ? x11Data->x_depth : x_appdepth; } + +inline int TQPaintDevice::x11Cells() const +{ return x11Data ? x11Data->x_cells : x_appcells; } + +inline TQt::HANDLE TQPaintDevice::x11Colormap() const +{ return x11Data ? x11Data->x_colormap : x_appcolormap; } + +inline bool TQPaintDevice::x11DefaultColormap() const +{ return x11Data ? x11Data->x_defcolormap : x_appdefcolormap; } + +inline void *TQPaintDevice::x11Visual() const +{ return x11Data ? x11Data->x_visual : x_appvisual; } + +inline bool TQPaintDevice::x11DefaultVisual() const +{ return x11Data ? x11Data->x_defvisual : x_appdefvisual; } + +inline Display *TQPaintDevice::x11AppDisplay() +{ return x_appdisplay; } + +inline int TQPaintDevice::x11AppScreen() +{ return x_appscreen; } + +inline int TQPaintDevice::x11AppDepth( int screen ) +{ return x_appdepth_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline int TQPaintDevice::x11AppCells( int screen ) +{ return x_appcells_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline TQt::HANDLE TQPaintDevice::x11AppRootWindow( int screen ) +{ return x_approotwindow_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline TQt::HANDLE TQPaintDevice::x11AppColormap( int screen ) +{ return x_appcolormap_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline bool TQPaintDevice::x11AppDefaultColormap( int screen ) +{ return x_appdefcolormap_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline void *TQPaintDevice::x11AppVisual( int screen ) +{ return x_appvisual_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline bool TQPaintDevice::x11AppDefaultVisual( int screen ) +{ return x_appdefvisual_arr[ screen == -1 ? x_appscreen : screen ]; } + +inline int TQPaintDevice::x11AppDepth() +{ return x_appdepth; } + +inline int TQPaintDevice::x11AppCells() +{ return x_appcells; } + +inline TQt::HANDLE TQPaintDevice::x11AppRootWindow() +{ return x_approotwindow; } + +inline TQt::HANDLE TQPaintDevice::x11AppColormap() +{ return x_appcolormap; } + +inline bool TQPaintDevice::x11AppDefaultColormap() +{ return x_appdefcolormap; } + +inline void *TQPaintDevice::x11AppVisual() +{ return x_appvisual; } + +inline bool TQPaintDevice::x11AppDefaultVisual() +{ return x_appdefvisual; } + +#endif // Q_WS_X11 + + +Q_EXPORT +inline void bitBlt( TQPaintDevice *dst, const TQPoint &dp, + const TQPaintDevice *src, const TQRect &sr =TQRect(0,0,-1,-1), + TQt::RasterOp rop=TQt::CopyROP, bool ignoreMask=FALSE ) +{ + bitBlt( dst, dp.x(), dp.y(), src, sr.x(), sr.y(), sr.width(), sr.height(), + rop, ignoreMask ); +} + + + + +#endif // TQPAINTDEVICE_H diff --git a/src/kernel/qpaintdevice_x11.cpp b/src/kernel/qpaintdevice_x11.cpp new file mode 100644 index 000000000..cd521c96e --- /dev/null +++ b/src/kernel/qpaintdevice_x11.cpp @@ -0,0 +1,1168 @@ +/**************************************************************************** +** +** Implementation of TQPaintDevice class for X11 +** +** Created : 940721 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpaintdevice.h" +#include "qpaintdevicemetrics.h" +#include "qpainter.h" +#include "qwidget.h" +#include "qbitmap.h" +#include "qapplication.h" +#include "qt_x11_p.h" + + +/*! + \class TQPaintDevice qpaintdevice.h + \brief The TQPaintDevice class is the base class of objects that + can be painted. + + \ingroup graphics + \ingroup images + + A paint device is an abstraction of a two-dimensional space that + can be drawn using a TQPainter. The drawing capabilities are + implemented by the subclasses TQWidget, TQPixmap, TQPicture and + TQPrinter. + + The default coordinate system of a paint device has its origin + located at the top-left position. X increases to the right and Y + increases downward. The unit is one pixel. There are several ways + to set up a user-defined coordinate system using the painter, for + example, using TQPainter::setWorldMatrix(). + + Example (draw on a paint device): + \code + void MyWidget::paintEvent( TQPaintEvent * ) + { + TQPainter p; // our painter + p.begin( this ); // start painting the widget + p.setPen( red ); // red outline + p.setBrush( yellow ); // yellow fill + p.drawEllipse( 10, 20, 100,100 ); // 100x100 ellipse at position (10, 20) + p.end(); // painting done + } + \endcode + + The bit block transfer is an extremely useful operation for + copying pixels from one paint device to another (or to itself). It + is implemented as the global function bitBlt(). + + Example (scroll widget contents 10 pixels to the right): + \code + bitBlt( myWidget, 10, 0, myWidget ); + \endcode + + \warning TQt retquires that a TQApplication object exists before + any paint devices can be created. Paint devices access window + system resources, and these resources are not initialized before + an application object is created. +*/ + + +// +// Some global variables - these are initialized by TQColor::initialize() +// + +Display *TQPaintDevice::x_appdisplay = 0; +int TQPaintDevice::x_appscreen; + +int TQPaintDevice::x_appdepth; +int TQPaintDevice::x_appcells; +TQt::HANDLE TQPaintDevice::x_approotwindow; +TQt::HANDLE TQPaintDevice::x_appcolormap; +bool TQPaintDevice::x_appdefcolormap; +void *TQPaintDevice::x_appvisual; +bool TQPaintDevice::x_appdefvisual; + +// ### in 4.0, remove the above, and use the below +int *TQPaintDevice::x_appdepth_arr; +int *TQPaintDevice::x_appcells_arr; +TQt::HANDLE *TQPaintDevice::x_approotwindow_arr; +TQt::HANDLE *TQPaintDevice::x_appcolormap_arr; +bool *TQPaintDevice::x_appdefcolormap_arr; +void **TQPaintDevice::x_appvisual_arr; +bool *TQPaintDevice::x_appdefvisual_arr; + +/*! + \enum TQPaintDevice::PDevCmd + \internal +*/ + +/*! + Constructs a paint device with internal flags \a devflags. This + constructor can be invoked only from TQPaintDevice subclasses. +*/ + +TQPaintDevice::TQPaintDevice( uint devflags ) +{ + if ( !qApp ) { // global constructor +#if defined(QT_CHECK_STATE) + qFatal( "TQPaintDevice: Must construct a TQApplication before a " + "TQPaintDevice" ); +#endif + return; + } + devFlags = devflags; + painters = 0; + hd = 0; + rendhd = 0; + x11Data = 0; +} + +/*! + Destroys the paint device and frees window system resources. +*/ + +TQPaintDevice::~TQPaintDevice() +{ +#if defined(QT_CHECK_STATE) + if ( paintingActive() ) + qWarning( "TQPaintDevice: Cannot destroy paint device that is being " + "painted" ); +#endif + if ( x11Data && x11Data->deref() ) { + delete x11Data; + x11Data = 0; + } +} + + +/* + \internal + Makes a shallow copy of the X11-specific data of \a fromDevice, if it is not + null. Otherwise this function sets it to null. +*/ + +void TQPaintDevice::copyX11Data( const TQPaintDevice *fromDevice ) +{ + setX11Data( fromDevice ? fromDevice->x11Data : 0 ); +} + +/* + \internal + Makes a deep copy of the X11-specific data of \a fromDevice, if it is not + null. Otherwise this function sets it to null. +*/ + +void TQPaintDevice::cloneX11Data( const TQPaintDevice *fromDevice ) +{ + if ( fromDevice && fromDevice->x11Data ) { + TQPaintDeviceX11Data *d = new TQPaintDeviceX11Data; + *d = *fromDevice->x11Data; + d->count = 0; + setX11Data( d ); + } else { + setX11Data( 0 ); + } +} + +/* + \internal + Makes a shallow copy of the X11-specific data \a d and assigns it to this + class. This function increments the reference code of \a d. +*/ + +void TQPaintDevice::setX11Data( const TQPaintDeviceX11Data* d ) +{ + if ( x11Data && x11Data->deref() ) + delete x11Data; + x11Data = (TQPaintDeviceX11Data*)d; + if ( x11Data ) + x11Data->ref(); +} + + +/* + \internal + If \a def is FALSE, returns a deep copy of the x11Data, or 0 if x11Data is 0. + If \a def is TRUE, makes a TQPaintDeviceX11Data struct filled with the default + values. + + In either case the caller is responsible for deleting the returned + struct. But notice that the struct is a shared class, so other + classes might also have a reference to it. The reference count of + the returned TQPaintDeviceX11Data* is 0. +*/ + +TQPaintDeviceX11Data* TQPaintDevice::getX11Data( bool def ) const +{ + TQPaintDeviceX11Data* res = 0; + if ( def ) { + res = new TQPaintDeviceX11Data; + res->x_display = x11AppDisplay(); + res->x_screen = x11AppScreen(); + res->x_depth = x11AppDepth(); + res->x_cells = x11AppCells(); + res->x_colormap = x11Colormap(); + res->x_defcolormap = x11AppDefaultColormap(); + res->x_visual = x11AppVisual(); + res->x_defvisual = x11AppDefaultVisual(); + res->deref(); + } else if ( x11Data ) { + res = new TQPaintDeviceX11Data; + *res = *x11Data; + res->count = 0; + } + return res; +} + + +/*! + \fn int TQPaintDevice::devType() const + + \internal + + Returns the device type identifier, which is \c TQInternal::Widget + if the device is a TQWidget, \c TQInternal::Pixmap if it's a + TQPixmap, \c TQInternal::Printer if it's a TQPrinter, \c + TQInternal::Picture if it's a TQPicture or \c + TQInternal::UndefinedDevice in other cases (which should never + happen). +*/ + +/*! + \fn bool TQPaintDevice::isExtDev() const + + Returns TRUE if the device is an external paint device; otherwise + returns FALSE. + + External paint devices cannot be bitBlt()'ed from. TQPicture and + TQPrinter are external paint devices. +*/ + +/*! + Returns the window system handle of the paint device, for + low-level access. Using this function is not portable. + + The HANDLE type varies with platform; see \c qpaintdevice.h and + \c qwindowdefs.h for details. + + \sa x11Display() +*/ +TQt::HANDLE TQPaintDevice::handle() const +{ + return hd; +} + +/*! + Returns the window system handle of the paint device for XRender + support. Use of this function is not portable. This function will + return 0 if XRender support is not compiled into TQt, if the + XRender extension is not supported on the X11 display, or if the + handle could not be created. +*/ +TQt::HANDLE TQPaintDevice::x11RenderHandle() const +{ +#ifndef QT_NO_XFTFREETYPE + return rendhd ? XftDrawPicture( (XftDraw *) rendhd ) : 0; +#else + return 0; +#endif // QT_NO_XFTFREETYPE +} + + +/*! + \fn Display *TQPaintDevice::x11AppDisplay() + + Returns a pointer to the X display global to the application (X11 + only). Using this function is not portable. + + \sa handle() +*/ + +/*! + \fn int TQPaintDevice::x11AppScreen() + + Returns the screen number on the X display global to the + application (X11 only). Using this function is not portable. +*/ + +/*! + \overload + \fn int TQPaintDevice::x11AppDepth() + + Returns the depth for the default screen of the X display global + to the application (X11 only). Using this function is not + portable. + + \sa TQPixmap::defaultDepth() +*/ + +/*! + \fn int TQPaintDevice::x11AppCells() + + Returns the number of entries in the colormap for the default + screen of the X display global to the application (X11 + only). Using this function is not portable. + + \sa x11Colormap() +*/ + +/*! + \fn HANDLE TQPaintDevice::x11AppRootWindow() + + Returns the root window for the default screen of the X display + global to the applicatoin (X11 only). Using this function is not + portable. +*/ + +/*! + \fn HANDLE TQPaintDevice::x11AppColormap() + + Returns the colormap for the default screen of the X display + global to the application (X11 only). Using this function is not + portable. + + \sa x11Cells() +*/ + +/*! + \fn bool TQPaintDevice::x11AppDefaultColormap () + + Returns the default colormap for the default screen of the X + display global to the application (X11 only). Using this function + is not portable. + + \sa x11Cells() +*/ + +/*! + \fn void* TQPaintDevice::x11AppVisual () + + Returns the Visual for the default screen of the X display global + to the application (X11 only). Using this function is not + portable. +*/ + +/*! + \fn bool TQPaintDevice::x11AppDefaultVisual () + + Returns TRUE if the Visual used is the default for the default + screen of the X display global to the application (X11 only); + otherwise returns FALSE. Using this function is not portable. +*/ + +/*! + \fn int TQPaintDevice::x11AppDepth( int screen ) + + Returns the depth for screen \a screen of the X display global to + the application (X11 only). Using this function is not portable. + + \sa TQPixmap::defaultDepth() +*/ + +/*! + \overload + \fn int TQPaintDevice::x11AppCells( int screen ) + + Returns the number of entries in the colormap for screen \a screen + of the X display global to the application (X11 only). Using this + function is not portable. + + \sa x11Colormap() +*/ + +/*! + \overload + \fn HANDLE TQPaintDevice::x11AppRootWindow( int screen ) + + Returns the root window for screen \a screen of the X display + global to the applicatoin (X11 only). Using this function is not + portable. +*/ + +/*! + \overload + \fn HANDLE TQPaintDevice::x11AppColormap( int screen ) + + Returns the colormap for screen \a screen of the X display global + to the application (X11 only). Using this function is not + portable. + + \sa x11Cells() +*/ + +/*! + \overload + \fn bool TQPaintDevice::x11AppDefaultColormap( int screen ) + + Returns the default colormap for screen \a screen of the X display + global to the application (X11 only). Using this function is not + portable. + + \sa x11Cells() +*/ + +/*! + \overload + \fn void* TQPaintDevice::x11AppVisual( int screen ) + + Returns the Visual for screen \a screen of the X display global to + the application (X11 only). Using this function is not portable. +*/ + +/*! + \overload + \fn bool TQPaintDevice::x11AppDefaultVisual( int screen ) + + Returns TRUE if the Visual used is the default for screen + \a screen of the X display global to the application (X11 only); + otherwise returns FALSE. Using this function is not portable. +*/ + + +/*! + \fn Display *TQPaintDevice::x11Display() const + + Returns a pointer to the X display for the paint device (X11 + only). Using this function is not portable. + + \sa handle() +*/ + +/*! + \fn int TQPaintDevice::x11Screen () const + + Returns the screen number on the X display for the paint device + (X11 only). Using this function is not portable. +*/ + +/*! + \fn int TQPaintDevice::x11Depth () const + + Returns the depth of the X display for the paint device (X11 + only). Using this function is not portable. + + \sa TQPixmap::defaultDepth() +*/ + +/*! + \fn int TQPaintDevice::x11Cells () const + + Returns the number of entries in the colormap of the X display for + the paint device (X11 only). Using this function is not portable. + + \sa x11Colormap() +*/ + +/*! + \fn HANDLE TQPaintDevice::x11Colormap () const + + Returns the colormap of the X display for the paint device (X11 + only). Using this function is not portable. + + \sa x11Cells() +*/ + +/*! + \fn bool TQPaintDevice::x11DefaultColormap () const + + Returns the default colormap of the X display for the paint device + (X11 only). Using this function is not portable. + + \sa x11Cells() +*/ + +/*! + \fn void* TQPaintDevice::x11Visual () const + + Returns the Visual of the X display for the paint device (X11 + only). Using this function is not portable. +*/ + +/*! + \fn bool TQPaintDevice::x11DefaultVisual () const + + Returns the default Visual of the X display for the paint device + (X11 only). Using this function is not portable. +*/ + +static int *dpisX=0, *dpisY=0; +static void create_dpis() +{ + if ( dpisX ) + return; + + Display *dpy = TQPaintDevice::x11AppDisplay(); + if ( ! dpy ) + return; + + int i, screens = ScreenCount( dpy ); + dpisX = new int[ screens ]; + dpisY = new int[ screens ]; + Q_CHECK_PTR( dpisX ); + Q_CHECK_PTR( dpisY ); + for ( i = 0; i < screens; i++ ) { + dpisX[ i ] = (DisplayWidth(dpy,i) * 254 + DisplayWidthMM(dpy,i)*5) + + / (DisplayWidthMM(dpy,i)*10); + dpisY[ i ] = (DisplayHeight(dpy,i) * 254 + DisplayHeightMM(dpy,i)*5) + / (DisplayHeightMM(dpy,i)*10); + } +} + +/*! + Sets the value returned by x11AppDpiX() to \a dpi for screen + \a screen. The default is determined by the display configuration. + Changing this value will alter the scaling of fonts and many other + metrics and is not recommended. Using this function is not + portable. + + \sa x11SetAppDpiY() +*/ +void TQPaintDevice::x11SetAppDpiX(int dpi, int screen) +{ + create_dpis(); + if ( ! dpisX ) + return; + if ( screen < 0 ) + screen = TQPaintDevice::x11AppScreen(); + if ( screen > ScreenCount( TQPaintDevice::x11AppDisplay() ) ) + return; + dpisX[ screen ] = dpi; +} + +/*! + \overload + + Sets the value returned by x11AppDpiX() to \a dpi for the default + screen. The default is determined by the display configuration. + Changing this value will alter the scaling of fonts and many other + metrics and is not recommended. Using this function is not + portable. + +*/ +// ### REMOVE 4.0 +void TQPaintDevice::x11SetAppDpiX( int dpi ) +{ + TQPaintDevice::x11SetAppDpiX( dpi, -1 ); +} + +/*! + Sets the value returned by x11AppDpiY() to \a dpi for screen + \a screen. The default is determined by the display configuration. + Changing this value will alter the scaling of fonts and many other + metrics and is not recommended. Using this function is not + portable. + + \sa x11SetAppDpiX() +*/ +void TQPaintDevice::x11SetAppDpiY(int dpi, int screen) +{ + create_dpis(); + if ( ! dpisY ) + return; + if ( screen < 0 ) + screen = TQPaintDevice::x11AppScreen(); + if ( screen > ScreenCount( TQPaintDevice::x11AppDisplay() ) ) + return; + dpisY[ screen ] = dpi; +} + +/*! + \overload + + Sets the value returned by x11AppDpiY() to \a dpi for the default + screen. The default is determined by the display configuration. + Changing this value will alter the scaling of fonts and many other + metrics and is not recommended. Using this function is not + portable. +*/ +// ### REMOVE 4.0 +void TQPaintDevice::x11SetAppDpiY( int dpi ) +{ + TQPaintDevice::x11SetAppDpiY( dpi, -1 ); +} + +/*! + Returns the horizontal DPI of the X display (X11 only) for screen + \a screen. Using this function is not portable. See + TQPaintDeviceMetrics for portable access to related information. + Using this function is not portable. + + \sa x11AppDpiY(), x11SetAppDpiX(), TQPaintDeviceMetrics::logicalDpiX() +*/ +int TQPaintDevice::x11AppDpiX(int screen) +{ + create_dpis(); + if ( ! dpisX ) + return 0; + if ( screen < 0 ) + screen = TQPaintDevice::x11AppScreen(); + if ( screen > ScreenCount( TQPaintDevice::x11AppDisplay() ) ) + return 0; + return dpisX[ screen ]; +} + +/*! + \overload + + Returns the horizontal DPI of the X display (X11 only) for the + default screen. Using this function is not portable. See + TQPaintDeviceMetrics for portable access to related information. + Using this function is not portable. +*/ +int TQPaintDevice::x11AppDpiX() +{ + return TQPaintDevice::x11AppDpiX( -1 ); +} + +/*! + Returns the vertical DPI of the X11 display (X11 only) for screen + \a screen. Using this function is not portable. See + TQPaintDeviceMetrics for portable access to related information. + Using this function is not portable. + + \sa x11AppDpiX(), x11SetAppDpiY(), TQPaintDeviceMetrics::logicalDpiY() +*/ +int TQPaintDevice::x11AppDpiY( int screen ) +{ + create_dpis(); + if ( ! dpisY ) + return 0; + if ( screen < 0 ) + screen = TQPaintDevice::x11AppScreen(); + if ( screen > ScreenCount( TQPaintDevice::x11AppDisplay() ) ) + return 0; + return dpisY[ screen ]; +} + +/*! + \overload + + Returns the vertical DPI of the X11 display (X11 only) for the + default screen. Using this function is not portable. See + TQPaintDeviceMetrics for portable access to related information. + Using this function is not portable. + + \sa x11AppDpiX(), x11SetAppDpiY(), TQPaintDeviceMetrics::logicalDpiY() +*/ +int TQPaintDevice::x11AppDpiY() +{ + return TQPaintDevice::x11AppDpiY( -1 ); +} + +/*! + \fn bool TQPaintDevice::paintingActive() const + + Returns TRUE if the device is being painted, i.e. someone has + called TQPainter::begin() but not yet called TQPainter::end() for + this device; otherwise returns FALSE. + + \sa TQPainter::isActive() +*/ + +/*! + Internal virtual function that interprets drawing commands from + the painter. + + Implemented by subclasses that have no direct support for drawing + graphics (external paint devices, for example, TQPicture). +*/ + +bool TQPaintDevice::cmd( int, TQPainter *, TQPDevCmdParam * ) +{ +#if defined(QT_CHECK_STATE) + qWarning( "TQPaintDevice::cmd: Device has no command interface" ); +#endif + return FALSE; +} + +/*! + \internal + + Internal virtual function that returns paint device metrics. + + Please use the TQPaintDeviceMetrics class instead. +*/ + +int TQPaintDevice::metric( int ) const +{ +#if defined(QT_CHECK_STATE) + qWarning( "TQPaintDevice::metrics: Device has no metric information" ); +#endif + return 0; +} + +/*! + \internal + + Internal virtual function. Reserved for future use. + + Please use the TQFontMetrics class instead. +*/ + +int TQPaintDevice::fontMet( TQFont *, int, const char *, int ) const +{ + return 0; +} + +/*! + \internal + + Internal virtual function. Reserved for future use. + + Please use the TQFontInfo class instead. +*/ + +int TQPaintDevice::fontInf( TQFont *, int ) const +{ + return 0; +} + + +// +// Internal functions for simple GC caching for blt'ing masked pixmaps. +// This cache is used when the pixmap optimization is set to Normal +// and the pixmap size doesn't exceed 128x128. +// + +static bool init_mask_gc = FALSE; +static const int max_mask_gcs = 11; // suitable for hashing + +struct mask_gc { + GC gc; + int mask_no; +}; + +static mask_gc gc_vec[max_mask_gcs]; + + +static void cleanup_mask_gc() +{ + Display *dpy = TQPaintDevice::x11AppDisplay(); + init_mask_gc = FALSE; + for ( int i=0; igc || p->mask_no != mask_no ) { // not a perfect match + if ( !p->gc ) { // no GC + p->gc = XCreateGC( dpy, hd, 0, 0 ); + XSetGraphicsExposures( dpy, p->gc, False ); + } + XSetClipMask( dpy, p->gc, mask ); + p->mask_no = mask_no; + } + return p->gc; +} + + +/*! + \relates TQPaintDevice + + Copies a block of pixels from \a src to \a dst, perhaps merging + each pixel according to the \link TQt::RasterOp raster operation \endlink + \a rop. \a sx, \a sy + is the top-left pixel in \a src (0, 0) by default, \a dx, \a dy is + the top-left position in \a dst and \a sw, \a sh is the size of + the copied block (all of \a src by default). + + The most common values for \a rop are CopyROP and XorROP; the \l + TQt::RasterOp documentation defines all the possible values. + + If \a ignoreMask is FALSE (the default) and \a src is a + masked TQPixmap, the entire blit is masked by \a{src}->mask(). + + If \a src, \a dst, \a sw or \a sh is 0, bitBlt() does nothing. If + \a sw or \a sh is negative bitBlt() copies starting at \a sx (and + respectively, \a sy) and ending at the right end (respectively, + bottom) of \a src. + + \a src must be a TQWidget or TQPixmap. You cannot blit from a + TQPrinter, for example. bitBlt() does nothing if you attempt to + blit from an unsupported device. + + bitBlt() does nothing if \a src has a greater depth than \e dst. + If you need to for example, draw a 24-bit pixmap on an 8-bit + widget, you must use drawPixmap(). +*/ + +void bitBlt( TQPaintDevice *dst, int dx, int dy, + const TQPaintDevice *src, int sx, int sy, int sw, int sh, + TQt::RasterOp rop, bool ignoreMask ) +{ + if ( !src || !dst ) { +#if defined(QT_CHECK_NULL) + Q_ASSERT( src != 0 ); + Q_ASSERT( dst != 0 ); +#endif + return; + } + if ( !src->handle() || src->isExtDev() ) + return; + + TQPaintDevice *pdev = TQPainter::redirect( dst ); + if ( pdev ) + dst = pdev; + + int ts = src->devType(); // from device type + int td = dst->devType(); // to device type + Display *dpy = src->x11Display(); + + if ( sw <= 0 ) { // special width + if ( sw < 0 ) + sw = src->metric( TQPaintDeviceMetrics::PdmWidth ) - sx; + else + return; + } + if ( sh <= 0 ) { // special height + if ( sh < 0 ) + sh = src->metric( TQPaintDeviceMetrics::PdmHeight ) - sy; + else + return; + } + + if ( dst->paintingActive() && dst->isExtDev() ) { + TQPixmap *pm; // output to picture/printer + bool tmp_pm = TRUE; + if ( ts == TQInternal::Pixmap ) { + pm = (TQPixmap*)src; + if ( sx != 0 || sy != 0 || + sw != pm->width() || sh != pm->height() || ignoreMask ) { + TQPixmap *tmp = new TQPixmap( sw, sh, pm->depth() ); + bitBlt( tmp, 0, 0, pm, sx, sy, sw, sh, TQt::CopyROP, TRUE ); + if ( pm->mask() && !ignoreMask ) { + TQBitmap mask( sw, sh ); + bitBlt( &mask, 0, 0, pm->mask(), sx, sy, sw, sh, + TQt::CopyROP, TRUE ); + tmp->setMask( mask ); + } + pm = tmp; + } else { + tmp_pm = FALSE; + } + } else if ( ts == TQInternal::Widget ) {// bitBlt to temp pixmap + pm = new TQPixmap( sw, sh ); + Q_CHECK_PTR( pm ); + bitBlt( pm, 0, 0, src, sx, sy, sw, sh ); + } else { +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Cannot bitBlt from device" ); +#endif + return; + } + TQPDevCmdParam param[3]; + TQRect r(dx, dy, pm->width(), pm->height()); + param[0].rect = &r; + param[1].pixmap = pm; + dst->cmd( TQPaintDevice::PdcDrawPixmap, 0, param ); + if ( tmp_pm ) + delete pm; + return; + } + + switch ( ts ) { + case TQInternal::Widget: + case TQInternal::Pixmap: + case TQInternal::System: // OK, can blt from these + break; + default: +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Cannot bitBlt from device type %x", ts ); +#endif + return; + } + switch ( td ) { + case TQInternal::Widget: + case TQInternal::Pixmap: + case TQInternal::System: // OK, can blt to these + break; + default: +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Cannot bitBlt to device type %x", td ); +#endif + return; + } + + static const short ropCodes[] = { // ROP translation table + GXcopy, GXor, GXxor, GXandInverted, + GXcopyInverted, GXorInverted, GXequiv, GXand, + GXinvert, GXclear, GXset, GXnoop, + GXandReverse, GXorReverse, GXnand, GXnor + }; + if ( rop > TQt::LastROP ) { +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Invalid ROP code" ); +#endif + return; + } + + if ( dst->handle() == 0 ) { +#if defined(QT_CHECK_NULL) + qWarning( "bitBlt: Cannot bitBlt to device" ); +#endif + return; + } + + bool mono_src; + bool mono_dst; + bool include_inferiors = FALSE; + bool graphics_exposure = FALSE; + TQPixmap *src_pm; + TQBitmap *mask; + + if ( ts == TQInternal::Pixmap ) { + src_pm = (TQPixmap*)src; + if ( src_pm->x11Screen() != dst->x11Screen() ) + src_pm->x11SetScreen( dst->x11Screen() ); + mono_src = src_pm->depth() == 1; + mask = ignoreMask ? 0 : src_pm->data->mask; + } else { + src_pm = 0; + mono_src = FALSE; + mask = 0; + include_inferiors = ((TQWidget*)src)->testWFlags(TQt::WPaintUnclipped); + graphics_exposure = td == TQInternal::Widget; + } + if ( td == TQInternal::Pixmap ) { + if ( dst->x11Screen() != src->x11Screen() ) + ((TQPixmap*)dst)->x11SetScreen( src->x11Screen() ); + mono_dst = ((TQPixmap*)dst)->depth() == 1; + ((TQPixmap*)dst)->detach(); // changes shared pixmap + } else { + mono_dst = FALSE; + include_inferiors = include_inferiors || + ((TQWidget*)dst)->testWFlags(TQt::WPaintUnclipped); + } + + if ( mono_dst && !mono_src ) { // dest is 1-bit pixmap, source is not +#if defined(QT_CHECK_RANGE) + qWarning( "bitBlt: Incompatible destination pixmap" ); +#endif + return; + } + +#ifndef QT_NO_XRENDER + if (src_pm && !mono_src && src_pm->data->alphapm && !ignoreMask ) { + // use RENDER to do the blit + TQPixmap *alpha = src_pm->data->alphapm; + if (src->x11RenderHandle() && + alpha->x11RenderHandle() && + dst->x11RenderHandle()) { + XRenderPictureAttributes pattr; + ulong picmask = 0; + if (include_inferiors) { + pattr.subwindow_mode = IncludeInferiors; + picmask |= CPSubwindowMode; + } + if (graphics_exposure) { + pattr.graphics_exposures = TRUE; + picmask |= CPGraphicsExposure; + } + if (picmask) + XRenderChangePicture(dpy, dst->x11RenderHandle(), picmask, &pattr); + XRenderComposite(dpy, PictOpOver, src->x11RenderHandle(), + alpha->x11RenderHandle(), dst->x11RenderHandle(), + sx, sy, sx, sy, dx, dy, sw, sh); + // restore attributes + pattr.subwindow_mode = ClipByChildren; + pattr.graphics_exposures = FALSE; + if (picmask) + XRenderChangePicture(dpy, dst->x11RenderHandle(), picmask, &pattr); + return; + } + } +#endif + + GC gc; + + if ( mask && !mono_src ) { // fast masked blt + bool temp_gc = FALSE; + if ( mask->data->maskgc ) { + gc = (GC)mask->data->maskgc; // we have a premade mask GC + } else { + if ( FALSE && src_pm->optimization() == TQPixmap::NormalOptim ) { // #### cache disabled + // Compete for the global cache + gc = cache_mask_gc( dpy, dst->handle(), + mask->data->ser_no, + mask->handle() ); + } else { + // Create a new mask GC. If BestOptim, we store the mask GC + // with the mask (not at the pixmap). This way, many pixmaps + // which have a common mask will be optimized at no extra cost. + gc = XCreateGC( dpy, dst->handle(), 0, 0 ); + XSetGraphicsExposures( dpy, gc, False ); + XSetClipMask( dpy, gc, mask->handle() ); + if ( src_pm->optimization() == TQPixmap::BestOptim ) { + mask->data->maskgc = gc; + } else { + temp_gc = TRUE; + } + } + } + XSetClipOrigin( dpy, gc, dx-sx, dy-sy ); + if ( rop != TQt::CopyROP ) // use non-default ROP code + XSetFunction( dpy, gc, ropCodes[rop] ); + if ( include_inferiors ) { + XSetSubwindowMode( dpy, gc, IncludeInferiors ); + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, + dx, dy ); + XSetSubwindowMode( dpy, gc, ClipByChildren ); + } else { + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, + dx, dy ); + } + + if ( temp_gc ) // delete temporary GC + XFreeGC( dpy, gc ); + else if ( rop != TQt::CopyROP ) // restore ROP + XSetFunction( dpy, gc, GXcopy ); + return; + } + + gc = qt_xget_temp_gc( dst->x11Screen(), mono_dst ); // get a reusable GC + + if ( rop != TQt::CopyROP ) // use non-default ROP code + XSetFunction( dpy, gc, ropCodes[rop] ); + + if ( mono_src && mono_dst && src == dst ) { // dst and src are the same bitmap + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, dx, dy ); + } else if ( mono_src ) { // src is bitmap + XGCValues gcvals; + ulong valmask = GCBackground | GCForeground | GCFillStyle | + GCStipple | GCTileStipXOrigin | GCTileStipYOrigin; + if ( td == TQInternal::Widget ) { // set GC colors + TQWidget *w = (TQWidget *)dst; + gcvals.background = w->backgroundColor().pixel( dst->x11Screen() ); + gcvals.foreground = w->foregroundColor().pixel( dst->x11Screen() ); + if ( include_inferiors ) { + valmask |= GCSubwindowMode; + gcvals.subwindow_mode = IncludeInferiors; + } + } else if ( mono_dst ) { + gcvals.background = 0; + gcvals.foreground = 1; + } else { + gcvals.background = TQt::white.pixel( dst->x11Screen() ); + gcvals.foreground = TQt::black.pixel( dst->x11Screen() ); + } + + gcvals.fill_style = FillOpaqueStippled; + gcvals.stipple = src->handle(); + gcvals.ts_x_origin = dx - sx; + gcvals.ts_y_origin = dy - sy; + + bool clipmask = FALSE; + if ( mask ) { + if ( ((TQPixmap*)src)->data->selfmask ) { + gcvals.fill_style = FillStippled; + } else { + XSetClipMask( dpy, gc, mask->handle() ); + XSetClipOrigin( dpy, gc, dx-sx, dy-sy ); + clipmask = TRUE; + } + } + + XChangeGC( dpy, gc, valmask, &gcvals ); + XFillRectangle( dpy,dst->handle(), gc, dx, dy, sw, sh ); + + valmask = GCFillStyle | GCTileStipXOrigin | GCTileStipYOrigin; + gcvals.fill_style = FillSolid; + gcvals.ts_x_origin = 0; + gcvals.ts_y_origin = 0; + if ( include_inferiors ) { + valmask |= GCSubwindowMode; + gcvals.subwindow_mode = ClipByChildren; + } + XChangeGC( dpy, gc, valmask, &gcvals ); + + if ( clipmask ) { + XSetClipOrigin( dpy, gc, 0, 0 ); + XSetClipMask( dpy, gc, None ); + } + + } else { // src is pixmap/widget + + if ( graphics_exposure ) // widget to widget + XSetGraphicsExposures( dpy, gc, True ); + if ( include_inferiors ) { + XSetSubwindowMode( dpy, gc, IncludeInferiors ); + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, + dx, dy ); + XSetSubwindowMode( dpy, gc, ClipByChildren ); + } else { + XCopyArea( dpy, src->handle(), dst->handle(), gc, sx, sy, sw, sh, + dx, dy ); + } + if ( graphics_exposure ) // reset graphics exposure + XSetGraphicsExposures( dpy, gc, False ); + } + + if ( rop != TQt::CopyROP ) // restore ROP + XSetFunction( dpy, gc, GXcopy ); +} + + +/*! + \relates TQPaintDevice + + \overload void bitBlt( TQPaintDevice *dst, const TQPoint &dp, const TQPaintDevice *src, const TQRect &sr, RasterOp rop ) + + Overloaded bitBlt() with the destination point \a dp and source + rectangle \a sr. +*/ + + +/*! + \internal +*/ +// makes it possible to add a setResolution as we have in TQPrinter for all +// paintdevices without breaking bin compatibility. +void TQPaintDevice::setResolution( int ) +{ +} + +/*!\internal +*/ +int TQPaintDevice::resolution() const +{ + return metric( TQPaintDeviceMetrics::PdmDpiY ); +} diff --git a/src/kernel/qpaintdevicedefs.h b/src/kernel/qpaintdevicedefs.h new file mode 100644 index 000000000..56fe7d071 --- /dev/null +++ b/src/kernel/qpaintdevicedefs.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Definition of TQPaintDevice constants and flags +** +** Created : 940721 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPAINTDEVICEDEFS_H +#define TQPAINTDEVICEDEFS_H + +#error "this file is gone. the #defines it contained are in" +#error "q1xcompatibility.h; the functionality is in TQPaintDevice" +#error "and TQPaintDeviceMetrics." + +#endif // TQPAINTDEVICEDEFS_H diff --git a/src/kernel/qpaintdevicemetrics.cpp b/src/kernel/qpaintdevicemetrics.cpp new file mode 100644 index 000000000..a154af118 --- /dev/null +++ b/src/kernel/qpaintdevicemetrics.cpp @@ -0,0 +1,151 @@ +/**************************************************************************** +** +** Implementation of TQPaintDeviceMetrics class +** +** Created : 941109 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpaintdevicemetrics.h" + +/*! + \class TQPaintDeviceMetrics qpaintdevicemetrics.h + \brief The TQPaintDeviceMetrics class provides information about a + paint device. + + \ingroup graphics + \ingroup images + + Sometimes when drawing graphics it is necessary to obtain + information about the physical characteristics of a paint device. + This class provides the information. For example, to compute the + aspect ratio of a paint device: + + \code + TQPaintDeviceMetrics pdm( myWidget ); + double aspect = (double)pdm.widthMM() / (double)pdm.heightMM(); + \endcode + + TQPaintDeviceMetrics contains methods to provide the width and + height of a device in both pixels (width() and height()) and + millimeters (widthMM() and heightMM()), the number of colors the + device supports (numColors()), the number of bit planes (depth()), + and the resolution of the device (logicalDpiX() and + logicalDpiY()). + + It is not always possible for TQPaintDeviceMetrics to compute the + values you ask for, particularly for external devices. The + ultimate example is asking for the resolution of of a TQPrinter + that is set to "print to file": who knows what printer that file + will end up on? +*/ + +/*! + Constructs a metric for the paint device \a pd. +*/ +TQPaintDeviceMetrics::TQPaintDeviceMetrics( const TQPaintDevice *pd ) +{ + pdev = (TQPaintDevice *)pd; +} + + +/*! + \fn int TQPaintDeviceMetrics::width() const + + Returns the width of the paint device in default coordinate system + units (e.g. pixels for TQPixmap and TQWidget). +*/ + +/*! + \fn int TQPaintDeviceMetrics::height() const + + Returns the height of the paint device in default coordinate + system units (e.g. pixels for TQPixmap and TQWidget). +*/ + +/*! + \fn int TQPaintDeviceMetrics::widthMM() const + + Returns the width of the paint device, measured in millimeters. +*/ + +/*! + \fn int TQPaintDeviceMetrics::heightMM() const + + Returns the height of the paint device, measured in millimeters. +*/ + +/*! + \fn int TQPaintDeviceMetrics::numColors() const + + Returns the number of different colors available for the paint + device. Since this value is an int will not be sufficient to represent + the number of colors on 32 bit displays, in which case INT_MAX is + returned instead. +*/ + +/*! + \fn int TQPaintDeviceMetrics::depth() const + + Returns the bit depth (number of bit planes) of the paint device. +*/ + +/*! + \fn int TQPaintDeviceMetrics::logicalDpiX() const + + Returns the horizontal resolution of the device in dots per inch, + which is used when computing font sizes. For X, this is usually + the same as could be computed from widthMM(), but it varies on + Windows. +*/ + +/*! + \fn int TQPaintDeviceMetrics::logicalDpiY() const + + Returns the vertical resolution of the device in dots per inch, + which is used when computing font sizes. For X, this is usually + the same as could be computed from heightMM(), but it varies on + Windows. +*/ + +/*! + \fn int TQPaintDeviceMetrics::physicalDpiX() const + \internal +*/ +/*! + \fn int TQPaintDeviceMetrics::physicalDpiY() const + \internal +*/ + diff --git a/src/kernel/qpaintdevicemetrics.h b/src/kernel/qpaintdevicemetrics.h new file mode 100644 index 000000000..8866169ae --- /dev/null +++ b/src/kernel/qpaintdevicemetrics.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Definition of TQPaintDeviceMetrics class +** +** Created : 941109 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPAINTDEVICEMETRICS_H +#define TQPAINTDEVICEMETRICS_H + +#ifndef QT_H +#include "qpaintdevice.h" +#endif // QT_H + + +class Q_EXPORT TQPaintDeviceMetrics // paint device metrics +{ +public: + TQPaintDeviceMetrics( const TQPaintDevice * ); + + enum { + PdmWidth = 1, + PdmHeight, + PdmWidthMM, + PdmHeightMM, + PdmNumColors, + PdmDepth, + PdmDpiX, + PdmDpiY, + PdmPhysicalDpiX, + PdmPhysicalDpiY + }; + + int width() const { return (int)pdev->metric(PdmWidth); } + int height() const { return (int)pdev->metric(PdmHeight); } + int widthMM() const { return (int)pdev->metric(PdmWidthMM); } + int heightMM() const { return (int)pdev->metric(PdmHeightMM); } + int logicalDpiX() const { return (int)pdev->metric(PdmDpiX); } + int logicalDpiY() const { return (int)pdev->metric(PdmDpiY); } + int physicalDpiX()const { return (int)pdev->metric(PdmPhysicalDpiX); } + int physicalDpiY()const { return (int)pdev->metric(PdmPhysicalDpiY); } + int numColors() const { return (int)pdev->metric(PdmNumColors); } + int depth() const { return (int)pdev->metric(PdmDepth); } + +private: + TQPaintDevice *pdev; +}; + + +#endif // TQPAINTDEVICEMETRICS_H diff --git a/src/kernel/qpainter.cpp b/src/kernel/qpainter.cpp new file mode 100644 index 000000000..e95c5b8a7 --- /dev/null +++ b/src/kernel/qpainter.cpp @@ -0,0 +1,3966 @@ +/**************************************************************************** +** +** Implementation of TQPainter, TQPen and TQBrush classes +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpainter.h" +#include "qpainter_p.h" +#include "qbitmap.h" +#include "qptrstack.h" +#include "qptrdict.h" +#include "qdatastream.h" +#include "qwidget.h" +#include "qimage.h" +#include "qpaintdevicemetrics.h" +#include "qapplication.h" +#include "qrichtext_p.h" +#include "qregexp.h" +#include "qcleanuphandler.h" +#ifdef Q_WS_QWS +#include "qgfx_qws.h" +#endif +#include + +#include "qtextlayout_p.h" +#include "qfontengine_p.h" + +#ifndef QT_NO_TRANSFORMATIONS +typedef TQPtrStack TQWMatrixStack; +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +/*! + \class TQPainter qpainter.h + \brief The TQPainter class does low-level painting e.g. on widgets. + + \ingroup graphics + \ingroup images + \mainclass + + The painter provides highly optimized functions to do most of the + drawing GUI programs retquire. TQPainter can draw everything from + simple lines to complex shapes like pies and chords. It can also + draw aligned text and pixmaps. Normally, it draws in a "natural" + coordinate system, but it can also do view and world + transformation. + + The typical use of a painter is: + + \list + \i Construct a painter. + \i Set a pen, a brush etc. + \i Draw. + \i Destroy the painter. + \endlist + + Mostly, all this is done inside a paint event. (In fact, 99% of + all TQPainter use is in a reimplementation of + TQWidget::paintEvent(), and the painter is heavily optimized for + such use.) Here's one very simple example: + + \code + void SimpleExampleWidget::paintEvent() + { + TQPainter paint( this ); + paint.setPen( TQt::blue ); + paint.drawText( rect(), AlignCenter, "The Text" ); + } + \endcode + + Usage is simple, and there are many settings you can use: + + \list + + \i font() is the currently set font. If you set a font that isn't + available, TQt finds a close match. In fact font() returns what + you set using setFont() and fontInfo() returns the font actually + being used (which may be the same). + + \i brush() is the currently set brush; the color or pattern that's + used for filling e.g. circles. + + \i pen() is the currently set pen; the color or stipple that's + used for drawing lines or boundaries. + + \i backgroundMode() is \c Opaque or \c Transparent, i.e. whether + backgroundColor() is used or not. + + \i backgroundColor() only applies when backgroundMode() is Opaque + and pen() is a stipple. In that case, it describes the color of + the background pixels in the stipple. + + \i rasterOp() is how pixels drawn interact with the pixels already + there. + + \i brushOrigin() is the origin of the tiled brushes, normally the + origin of the window. + + \i viewport(), window(), worldMatrix() and many more make up the + painter's coordinate transformation system. See \link + coordsys.html The Coordinate System \endlink for an explanation of + this, or see below for a very brief overview of the functions. + + \i hasClipping() is whether the painter clips at all. (The paint + device clips, too.) If the painter clips, it clips to clipRegion(). + + \i pos() is the current position, set by moveTo() and used by + lineTo(). + + \endlist + + Note that some of these settings mirror settings in some paint + devices, e.g. TQWidget::font(). TQPainter::begin() (or the TQPainter + constructor) copies these attributes from the paint device. + Calling, for example, TQWidget::setFont() doesn't take effect until + the next time a painter begins painting on it. + + save() saves all of these settings on an internal stack, restore() + pops them back. + + The core functionality of TQPainter is drawing, and there are + functions to draw most primitives: drawPoint(), drawPoints(), + drawLine(), drawRect(), drawWinFocusRect(), drawRoundRect(), + drawEllipse(), drawArc(), drawPie(), drawChord(), + drawLineSegments(), drawPolyline(), drawPolygon(), + drawConvexPolygon() and drawCubicBezier(). All of these functions + take integer coordinates; there are no floating-point versions + since we want drawing to be as fast as possible. + + There are functions to draw pixmaps/images, namely drawPixmap(), + drawImage() and drawTiledPixmap(). drawPixmap() and drawImage() + produce the same result, except that drawPixmap() is faster + on-screen and drawImage() faster and sometimes better on TQPrinter + and TQPicture. + + Text drawing is done using drawText(), and when you need + fine-grained positioning, boundingRect() tells you where a given + drawText() command would draw. + + There is a drawPicture() function that draws the contents of an + entire TQPicture using this painter. drawPicture() is the only + function that disregards all the painter's settings: the TQPicture + has its own settings. + + Normally, the TQPainter operates on the device's own coordinate + system (usually pixels), but TQPainter has good support for + coordinate transformation. See \link coordsys.html The Coordinate + System \endlink for a more general overview and a simple example. + + The most common functions used are scale(), rotate(), translate() + and shear(), all of which operate on the worldMatrix(). + setWorldMatrix() can replace or add to the currently set + worldMatrix(). + + setViewport() sets the rectangle on which TQPainter operates. The + default is the entire device, which is usually fine, except on + printers. setWindow() sets the coordinate system, that is, the + rectangle that maps to viewport(). What's drawn inside the + window() ends up being inside the viewport(). The window's + default is the same as the viewport, and if you don't use the + transformations, they are optimized away, gaining another little + bit of speed. + + After all the coordinate transformation is done, TQPainter can clip + the drawing to an arbitrary rectangle or region. hasClipping() is + TRUE if TQPainter clips, and clipRegion() returns the clip region. + You can set it using either setClipRegion() or setClipRect(). + Note that the clipping can be slow. It's all system-dependent, + but as a rule of thumb, you can assume that drawing speed is + inversely proportional to the number of rectangles in the clip + region. + + After TQPainter's clipping, the paint device may also clip. For + example, most widgets clip away the pixels used by child widgets, + and most printers clip away an area near the edges of the paper. + This additional clipping is not reflected by the return value of + clipRegion() or hasClipping(). + + TQPainter also includes some less-used functions that are very + useful on those occasions when they're needed. + + isActive() indicates whether the painter is active. begin() (and + the most usual constructor) makes it active. end() (and the + destructor) deactivates it. If the painter is active, device() + returns the paint device on which the painter paints. + + Sometimes it is desirable to make someone else paint on an unusual + TQPaintDevice. TQPainter supports a static function to do this, + redirect(). We recommend not using it, but for some hacks it's + perfect. + + setTabStops() and setTabArray() can change where the tab stops + are, but these are very seldomly used. + + \warning Note that TQPainter does not attempt to work around + coordinate limitations in the underlying window system. Some + platforms may behave incorrectly with coordinates as small as + +/-4000. + + \headerfile qdrawutil.h + + \sa TQPaintDevice TQWidget TQPixmap TQPrinter TQPicture + \link simple-application.html Application Walkthrough \endlink + \link coordsys.html Coordinate System Overview \endlink +*/ + +/*! + \fn TQGfx * TQPainter::internalGfx() + + \internal +*/ + +/*! + \enum TQPainter::CoordinateMode + \value CoordDevice + \value CoordPainter + + \sa clipRegion() +*/ +/*! + \enum TQPainter::TextDirection + \value Auto + \value RTL right to left + \value LTR left to right + + \sa drawText() +*/ + +/*! + \enum TQt::PaintUnit + \value PixelUnit + \value LoMetricUnit \e obsolete + \value HiMetricUnit \e obsolete + \value LoEnglishUnit \e obsolete + \value HiEnglishUnit \e obsolete + \value TwipsUnit \e obsolete +*/ + +/*! + \enum TQt::BrushStyle + + \value NoBrush + \value SolidPattern + \value Dense1Pattern + \value Dense2Pattern + \value Dense3Pattern + \value Dense4Pattern + \value Dense5Pattern + \value Dense6Pattern + \value Dense7Pattern + \value HorPattern + \value VerPattern + \value CrossPattern + \value BDiagPattern + \value FDiagPattern + \value DiagCrossPattern + \value CustomPattern + + \img brush-styles.png Brush Styles + +*/ + +/*! + \enum TQt::RasterOp + + This enum type is used to describe the way things are written to + the paint device. Each bit of the \e src (what you write) + interacts with the corresponding bit of the \e dst pixel. + + \value CopyROP dst = src + \value OrROP dst = src OR dst + \value XorROP dst = src XOR dst + \value NotAndROP dst = (NOT src) AND dst + \value EraseROP an alias for \c NotAndROP + \value NotCopyROP dst = NOT src + \value NotOrROP dst = (NOT src) OR dst + \value NotXorROP dst = (NOT src) XOR dst + \value AndROP dst = src AND dst + \value NotEraseROP an alias for \c AndROP + \value NotROP dst = NOT dst + \value ClearROP dst = 0 + \value SetROP dst = 1 + \value NopROP dst = dst + \value AndNotROP dst = src AND (NOT dst) + \value OrNotROP dst = src OR (NOT dst) + \value NandROP dst = NOT (src AND dst) + \value NorROP dst = NOT (src OR dst) + + By far the most useful ones are \c CopyROP and \c XorROP. + + On TQt/Embedded, only \c CopyROP, \c XorROP, and \c NotROP are supported. +*/ + +/*! + \enum TQt::AlignmentFlags + + This enum type is used to describe alignment. It contains + horizontal and vertical flags. + + The horizontal flags are: + + \value AlignAuto Aligns according to the language. Left for most, + right for Arabic and Hebrew. + \value AlignLeft Aligns with the left edge. + \value AlignRight Aligns with the right edge. + \value AlignHCenter Centers horizontally in the available space. + \value AlignJustify Justifies the text in the available space. + Does not work for everything and may be interpreted as + AlignAuto in some cases. + + The vertical flags are: + + \value AlignTop Aligns with the top. + \value AlignBottom Aligns with the bottom. + \value AlignVCenter Centers vertically in the available space. + + You can use only one of the horizontal flags at a time. There is + one two-dimensional flag: + + \value AlignCenter Centers in both dimensions. + + You can use at most one horizontal and one vertical flag at a time. \c + AlignCenter counts as both horizontal and vertical. + + Masks: + + \value AlignHorizontal_Mask + \value AlignVertical_Mask + + Conflicting combinations of flags have undefined meanings. +*/ + +/*! + \enum TQt::TextFlags + + This enum type is used to define some modifier flags. Some of + these flags only make sense in the context of printing: + + \value SingleLine Treats all whitespace as spaces and prints just + one line. + \value DontClip If it's impossible to stay within the given bounds, + it prints outside. + \value ExpandTabs Makes the U+0009 (ASCII tab) character move to + the next tab stop. + \value ShowPrefix Displays the string "\&P" as P + (see TQButton for an example). For an ampersand, use "\&\&". + \value WordBreak Breaks lines at appropriate points, e.g. at word + boundaries. + \value BreakAnywhere Breaks lines anywhere, even within words. + \value NoAccel Same as ShowPrefix but doesn't draw the underlines. + + You can use as many modifier flags as you want, except that \c + SingleLine and \c WordBreak cannot be combined. + + Flags that are inappropriate for a given use (e.g. ShowPrefix to + TQGridLayout::addWidget()) are generally ignored. + +*/ + +/*! + \enum TQt::PenStyle + + This enum type defines the pen styles that can be drawn using + TQPainter. The styles are + + \value NoPen no line at all. For example, TQPainter::drawRect() + fills but does not draw any boundary line. + + \value SolidLine a simple line. + + \value DashLine dashes separated by a few pixels. + + \value DotLine dots separated by a few pixels. + + \value DashDotLine alternate dots and dashes. + + \value DashDotDotLine one dash, two dots, one dash, two dots. + + \value MPenStyle mask of the pen styles. + + \img pen-styles.png Pen Styles +*/ + +/*! + \enum TQt::PenCapStyle + + This enum type defines the pen cap styles supported by TQt, i.e. + the line end caps that can be drawn using TQPainter. + + \value FlatCap a square line end that does not cover the end + point of the line. + \value SquareCap a square line end that covers the end point and + extends beyond it with half the line width. + \value RoundCap a rounded line end. + \value MPenCapStyle mask of the pen cap styles. + + \img pen-cap-styles.png Pen Cap Styles +*/ + +/*! + \enum TQt::PenJoinStyle + + This enum type defines the pen join styles supported by TQt, i.e. + which joins between two connected lines can be drawn using + TQPainter. + + \value MiterJoin The outer edges of the lines are extended to + meet at an angle, and this area is filled. + \value BevelJoin The triangular notch between the two lines is filled. + \value RoundJoin A circular arc between the two lines is filled. + \value MPenJoinStyle mask of the pen join styles. + + \img pen-join-styles.png Pen Join Styles +*/ + +/*! + \enum TQt::BGMode + + Background mode + + \value TransparentMode + \value OpaqueMode +*/ + +/*! + Constructs a painter. + + Notice that all painter settings (setPen, setBrush etc.) are reset + to default values when begin() is called. + + \sa begin(), end() +*/ + +TQPainter::TQPainter() +{ + init(); +} + + +/*! + Constructs a painter that begins painting the paint device \a pd + immediately. Depending on the underlying graphic system the + painter will paint over children of the paintdevice if \a + unclipped is TRUE. + + This constructor is convenient for short-lived painters, e.g. in a + \link TQWidget::paintEvent() paint event\endlink and should be used + only once. The constructor calls begin() for you and the TQPainter + destructor automatically calls end(). + + Here's an example using begin() and end(): + \code + void MyWidget::paintEvent( TQPaintEvent * ) + { + TQPainter p; + p.begin( this ); + p.drawLine( ... ); // drawing code + p.end(); + } + \endcode + + The same example using this constructor: + \code + void MyWidget::paintEvent( TQPaintEvent * ) + { + TQPainter p( this ); + p.drawLine( ... ); // drawing code + } + \endcode + + Since the constructor cannot provide feedback when the initialization + of the painter failed you should rather use begin() and end() to paint + on external devices, e.g. printers. + + \sa begin(), end() +*/ + +TQPainter::TQPainter( const TQPaintDevice *pd, bool unclipped ) +{ + init(); + if ( begin( pd, unclipped ) ) + flags |= CtorBegin; +} + + +/*! + Constructs a painter that begins painting the paint device \a pd + immediately, with the default arguments taken from \a + copyAttributes. The painter will paint over children of the paint + device if \a unclipped is TRUE (although this is not supported on + all platforms). + + \sa begin() +*/ + +TQPainter::TQPainter( const TQPaintDevice *pd, + const TQWidget *copyAttributes, bool unclipped ) +{ + init(); + if ( begin( pd, copyAttributes, unclipped ) ) + flags |= CtorBegin; +} + + +/*! + Destroys the painter. +*/ + +TQPainter::~TQPainter() +{ + if ( isActive() ) + end(); + else + killPStack(); + if ( tabarray ) // delete tab array + delete [] tabarray; +#ifndef QT_NO_TRANSFORMATIONS + if ( wm_stack ) + delete (TQWMatrixStack *)wm_stack; +#endif + destroy(); +} + + +/*! + \overload bool TQPainter::begin( const TQPaintDevice *pd, const TQWidget *copyAttributes, bool unclipped ) + + This version opens the painter on a paint device \a pd and sets + the initial pen, background color and font from \a copyAttributes, + painting over the paint device's children when \a unclipped is + TRUE. This is equivalent to: + + \code + TQPainter p; + p.begin( pd ); + p.setPen( copyAttributes->foregroundColor() ); + p.setBackgroundColor( copyAttributes->backgroundColor() ); + p.setFont( copyAttributes->font() ); + \endcode + + This begin function is convenient for double buffering. When you + draw in a pixmap instead of directly in a widget (to later bitBlt + the pixmap into the widget) you will need to set the widget's + font etc. This function does exactly that. + + Example: + \code + void MyWidget::paintEvent( TQPaintEvent * ) + { + TQPixmap pm(size()); + TQPainter p; + p.begin(&pm, this); + // ... potentially flickering paint operation ... + p.end(); + bitBlt(this, 0, 0, &pm); + } + \endcode + + \sa end() +*/ + +bool TQPainter::begin( const TQPaintDevice *pd, const TQWidget *copyAttributes, bool unclipped ) +{ + if ( copyAttributes == 0 ) { +#if defined(QT_CHECK_NULL) + qWarning( "TQPainter::begin: The widget to copy attributes from cannot " + "be null" ); +#endif + return FALSE; + } + if ( begin( pd, unclipped ) ) { + setPen( copyAttributes->foregroundColor() ); + setBackgroundColor( copyAttributes->backgroundColor() ); + setFont( copyAttributes->font() ); + return TRUE; + } + return FALSE; +} + + +/*! + \internal + Sets or clears a pointer flag. +*/ + +void TQPainter::setf( uint b, bool v ) +{ + if ( v ) + setf( b ); + else + clearf( b ); +} + + +/*! + \fn bool TQPainter::isActive() const + + Returns TRUE if the painter is active painting, i.e. begin() has + been called and end() has not yet been called; otherwise returns + FALSE. + + \sa TQPaintDevice::paintingActive() +*/ + +/*! + \fn TQPaintDevice *TQPainter::device() const + + Returns the paint device on which this painter is currently + painting, or 0 if the painter is not active. + + \sa TQPaintDevice::paintingActive() +*/ + + +struct TQPState { // painter state + TQFont font; + TQPen pen; + TQPoint curPt; + TQBrush brush; + TQColor bgc; + uchar bgm; + uchar rop; + TQPoint bro; + TQRect wr, vr; +#ifndef QT_NO_TRANSFORMATIONS + TQWMatrix wm; +#else + int xlatex; + int xlatey; +#endif + bool vxf; + bool wxf; + TQRegion rgn; + bool clip; + int ts; + int *ta; + void* wm_stack; +}; + +//TODO lose the worldmatrix stack + +typedef TQPtrStack TQPStateStack; + + +void TQPainter::killPStack() +{ +#if defined(QT_CHECK_STATE) + if ( ps_stack && !((TQPStateStack *)ps_stack)->isEmpty() ) + qWarning( "TQPainter::killPStack: non-empty save/restore stack when " + "end() was called" ); +#endif + delete (TQPStateStack *)ps_stack; + ps_stack = 0; +} + +/*! + Saves the current painter state (pushes the state onto a stack). A + save() must be followed by a corresponding restore(). end() + unwinds the stack. + + \sa restore() +*/ + +void TQPainter::save() +{ + if ( testf(ExtDev) ) { + if ( testf(DirtyFont) ) + updateFont(); + if ( testf(DirtyPen) ) + updatePen(); + if ( testf(DirtyBrush) ) + updateBrush(); + pdev->cmd( TQPaintDevice::PdcSave, this, 0 ); + } + TQPStateStack *pss = (TQPStateStack *)ps_stack; + if ( pss == 0 ) { + pss = new TQPtrStack; + Q_CHECK_PTR( pss ); + pss->setAutoDelete( TRUE ); + ps_stack = pss; + } + TQPState *ps = new TQPState; + Q_CHECK_PTR( ps ); + ps->font = cfont; + ps->pen = cpen; + ps->curPt = pos(); + ps->brush = cbrush; + ps->bgc = bg_col; + ps->bgm = bg_mode; + ps->rop = rop; + ps->bro = bro; +#ifndef QT_NO_TRANSFORMATIONS + ps->wr = TQRect( wx, wy, ww, wh ); + ps->vr = TQRect( vx, vy, vw, vh ); + ps->wm = wxmat; + ps->vxf = testf(VxF); + ps->wxf = testf(WxF); +#else + ps->xlatex = xlatex; + ps->xlatey = xlatey; +#endif + ps->rgn = crgn; + ps->clip = testf(ClipOn); + ps->ts = tabstops; + ps->ta = tabarray; + ps->wm_stack = wm_stack; + wm_stack = 0; + pss->push( ps ); +} + +/*! + Restores the current painter state (pops a saved state off the + stack). + + \sa save() +*/ + +void TQPainter::restore() +{ + if ( testf(ExtDev) ) { + pdev->cmd( TQPaintDevice::PdcRestore, this, 0 ); + if ( pdev->devType() == TQInternal::Picture ) + block_ext = TRUE; + } + TQPStateStack *pss = (TQPStateStack *)ps_stack; + if ( pss == 0 || pss->isEmpty() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::restore: Empty stack error" ); +#endif + return; + } + TQPState *ps = pss->pop(); + bool hardRestore = testf(VolatileDC); + + if ( ps->font != cfont || hardRestore ) + setFont( ps->font ); + if ( ps->pen != cpen || hardRestore ) + setPen( ps->pen ); + if ( ps->brush != cbrush || hardRestore ) + setBrush( ps->brush ); + if ( ps->bgc != bg_col || hardRestore ) + setBackgroundColor( ps->bgc ); + if ( ps->bgm != bg_mode || hardRestore ) + setBackgroundMode( (BGMode)ps->bgm ); + if ( ps->rop != rop || hardRestore ) + setRasterOp( (RasterOp)ps->rop ); + if ( ps->bro != bro || hardRestore ) + setBrushOrigin( ps->bro ); +#ifndef QT_NO_TRANSFORMATIONS + TQRect wr( wx, wy, ww, wh ); + TQRect vr( vx, vy, vw, vh ); + if ( ps->wr != wr || hardRestore ) + setWindow( ps->wr ); + if ( ps->vr != vr || hardRestore ) + setViewport( ps->vr ); + if ( ps->wm != wxmat || hardRestore ) + setWorldMatrix( ps->wm ); + if ( ps->vxf != testf(VxF) || hardRestore ) + setViewXForm( ps->vxf ); + if ( ps->wxf != testf(WxF) || hardRestore ) + setWorldXForm( ps->wxf ); +#else + xlatex = ps->xlatex; + xlatey = ps->xlatey; + setf( VxF, xlatex || xlatey ); +#endif + if ( ps->curPt != pos() || hardRestore ) + moveTo( ps->curPt ); + if ( ps->rgn != crgn || hardRestore ) + setClipRegion( ps->rgn ); + if ( ps->clip != testf(ClipOn) || hardRestore ) + setClipping( ps->clip ); + tabstops = ps->ts; + tabarray = ps->ta; + +#ifndef QT_NO_TRANSFORMATIONS + if ( wm_stack ) + delete (TQWMatrixStack *)wm_stack; + wm_stack = ps->wm_stack; +#endif + delete ps; + block_ext = FALSE; +} + +typedef TQPtrDict TQPaintDeviceDict; +static TQPaintDeviceDict *pdev_dict = 0; + +/*! + Redirects all paint commands for a paint device, \a pdev, to + another paint device, \a replacement, unless \a replacement is 0. + If \a replacement is 0, the redirection for \a pdev is removed. + + In general, you'll probably find calling TQPixmap::grabWidget() or + TQPixmap::grabWindow() is an easier solution. +*/ + +void TQPainter::redirect( TQPaintDevice *pdev, TQPaintDevice *replacement ) +{ + if ( pdev_dict == 0 ) { + if ( replacement == 0 ) + return; + pdev_dict = new TQPaintDeviceDict; + Q_CHECK_PTR( pdev_dict ); + } +#if defined(QT_CHECK_NULL) + if ( pdev == 0 ) + qWarning( "TQPainter::redirect: The pdev argument cannot be 0" ); +#endif + if ( replacement ) { + pdev_dict->insert( pdev, replacement ); + } else { + pdev_dict->remove( pdev ); + if ( pdev_dict->count() == 0 ) { + delete pdev_dict; + pdev_dict = 0; + } + } +} + +/*! + \internal + Returns the replacement for \a pdev, or 0 if there is no replacement. +*/ +TQPaintDevice *TQPainter::redirect( TQPaintDevice *pdev ) +{ + return pdev_dict ? pdev_dict->find( pdev ) : 0; +} + +/*! + Returns the font metrics for the painter, if the painter is + active. It is not possible to obtain metrics for an inactive + painter, so the return value is undefined if the painter is not + active. + + \sa fontInfo(), isActive() +*/ + +TQFontMetrics TQPainter::fontMetrics() const +{ + if ( pdev && pdev->devType() == TQInternal::Picture ) + return TQFontMetrics( cfont ); + + return TQFontMetrics(this); +} + +/*! + Returns the font info for the painter, if the painter is active. + It is not possible to obtain font information for an inactive + painter, so the return value is undefined if the painter is not + active. + + \sa fontMetrics(), isActive() +*/ + +TQFontInfo TQPainter::fontInfo() const +{ + if ( pdev && pdev->devType() == TQInternal::Picture ) + return TQFontInfo( cfont ); + + return TQFontInfo(this); +} + + +/*! + \fn const TQPen &TQPainter::pen() const + + Returns the painter's current pen. + + \sa setPen() +*/ + +/*! + Sets a new painter pen. + + The \a pen defines how to draw lines and outlines, and it also + defines the text color. + + \sa pen() +*/ + +void TQPainter::setPen( const TQPen &pen ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setPen: Will be reset by begin()" ); +#endif + if ( cpen == pen ) + return; + cpen = pen; + updatePen(); +} + +/*! + \overload + + Sets the painter's pen to have style \a style, width 0 and black + color. + + \sa pen(), TQPen +*/ + +void TQPainter::setPen( PenStyle style ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setPen: Will be reset by begin()" ); +#endif + TQPen::TQPenData *d = cpen.data; // low level access + if ( d->style == style && d->linest == style && !d->width && d->color == TQt::black ) + return; + if ( d->count != 1 ) { + cpen.detach(); + d = cpen.data; + } + d->style = style; + d->width = 0; + d->color = TQt::black; + d->linest = style; + updatePen(); +} + +/*! + \overload + + Sets the painter's pen to have style \c SolidLine, width 0 and the + specified \a color. + + \sa pen(), TQPen +*/ + +void TQPainter::setPen( const TQColor &color ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setPen: Will be reset by begin()" ); +#endif + TQPen::TQPenData *d = cpen.data; // low level access + if ( d->color == color && !d->width && d->style == SolidLine && d->linest == SolidLine ) + return; + if ( d->count != 1 ) { + cpen.detach(); + d = cpen.data; + } + d->style = SolidLine; + d->width = 0; + d->color = color; + d->linest = SolidLine; + updatePen(); +} + +/*! + \fn const TQBrush &TQPainter::brush() const + + Returns the painter's current brush. + + \sa TQPainter::setBrush() +*/ + +/*! + \overload + + Sets the painter's brush to \a brush. + + The \a brush defines how shapes are filled. + + \sa brush() +*/ + +void TQPainter::setBrush( const TQBrush &brush ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setBrush: Will be reset by begin()" ); +#endif + if ( cbrush == brush ) + return; + cbrush = brush; + updateBrush(); +} + +/*! + Sets the painter's brush to black color and the specified \a + style. + + \sa brush(), TQBrush +*/ + +void TQPainter::setBrush( BrushStyle style ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setBrush: Will be reset by begin()" ); +#endif + TQBrush::TQBrushData *d = cbrush.data; // low level access + if ( d->style == style && d->color == TQt::black && !d->pixmap ) + return; + if ( d->count != 1 ) { + cbrush.detach(); + d = cbrush.data; + } + d->style = style; + d->color = TQt::black; + if ( d->pixmap ) { + delete d->pixmap; + d->pixmap = 0; + } + updateBrush(); +} + +/*! + \overload + + Sets the painter's brush to have style \c SolidPattern and the + specified \a color. + + \sa brush(), TQBrush +*/ + +void TQPainter::setBrush( const TQColor &color ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setBrush: Will be reset by begin()" ); +#endif + TQBrush::TQBrushData *d = cbrush.data; // low level access + if ( d->color == color && d->style == SolidPattern && !d->pixmap ) + return; + if ( d->count != 1 ) { + cbrush.detach(); + d = cbrush.data; + } + d->style = SolidPattern; + d->color = color; + if ( d->pixmap ) { + delete d->pixmap; + d->pixmap = 0; + } + updateBrush(); +} + + +/*! + \fn const TQColor &TQPainter::backgroundColor() const + + Returns the current background color. + + \sa setBackgroundColor() TQColor +*/ + +/*! + \fn BGMode TQPainter::backgroundMode() const + + Returns the current background mode. + + \sa setBackgroundMode() BGMode +*/ + +/*! + \fn RasterOp TQPainter::rasterOp() const + + Returns the current \link TQt::RasterOp raster operation \endlink. + + \sa setRasterOp() RasterOp +*/ + +/*! + \fn const TQPoint &TQPainter::brushOrigin() const + + Returns the brush origin currently set. + + \sa setBrushOrigin() +*/ + + +/*! + \fn int TQPainter::tabStops() const + + Returns the tab stop setting. + + \sa setTabStops() +*/ + +/*! + Set the tab stop width to \a ts, i.e. locates tab stops at \a ts, + 2*\a ts, 3*\a ts and so on. + + Tab stops are used when drawing formatted text with \c ExpandTabs + set. This fixed tab stop value is used only if no tab array is set + (which is the default case). + + A value of 0 (the default) implies a tabstop setting of 8 times the width of the + character 'x' in the font currently set on the painter. + + \sa tabStops(), setTabArray(), drawText(), fontMetrics() +*/ + +void TQPainter::setTabStops( int ts ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setTabStops: Will be reset by begin()" ); +#endif + tabstops = ts; + if ( isActive() && testf(ExtDev) ) { // tell extended device + TQPDevCmdParam param[1]; + param[0].ival = ts; + pdev->cmd( TQPaintDevice::PdcSetTabStops, this, param ); + } +} + +/*! + \fn int *TQPainter::tabArray() const + + Returns the currently set tab stop array. + + \sa setTabArray() +*/ + +/*! + Sets the tab stop array to \a ta. This puts tab stops at \a ta[0], + \a ta[1] and so on. The array is null-terminated. + + If both a tab array and a tab top size is set, the tab array wins. + + \sa tabArray(), setTabStops(), drawText(), fontMetrics() +*/ + +void TQPainter::setTabArray( int *ta ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setTabArray: Will be reset by begin()" ); +#endif + if ( ta != tabarray ) { + tabarraylen = 0; + if ( tabarray ) // Avoid purify complaint + delete [] tabarray; // delete old array + if ( ta ) { // tabarray = copy of 'ta' + while ( ta[tabarraylen] ) + tabarraylen++; + tabarraylen++; // and 0 terminator + tabarray = new int[tabarraylen]; // duplicate ta + memcpy( tabarray, ta, sizeof(int)*tabarraylen ); + } else { + tabarray = 0; + } + } + if ( isActive() && testf(ExtDev) ) { // tell extended device + TQPDevCmdParam param[2]; + param[0].ival = tabarraylen; + param[1].ivec = tabarray; + pdev->cmd( TQPaintDevice::PdcSetTabArray, this, param ); + } +} + + +/*! + \fn HANDLE TQPainter::handle() const + + Returns the platform-dependent handle used for drawing. Using this + function is not portable. +*/ + + +/***************************************************************************** + TQPainter xform settings + *****************************************************************************/ + +#ifndef QT_NO_TRANSFORMATIONS + +/*! + Enables view transformations if \a enable is TRUE, or disables + view transformations if \a enable is FALSE. + + \sa hasViewXForm(), setWindow(), setViewport(), setWorldMatrix(), + setWorldXForm(), xForm() +*/ + +void TQPainter::setViewXForm( bool enable ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setViewXForm: Will be reset by begin()" ); +#endif + if ( !isActive() || enable == testf(VxF) ) + return; + setf( VxF, enable ); + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + param[0].ival = enable; + pdev->cmd( TQPaintDevice::PdcSetVXform, this, param ); + } + updateXForm(); +} + +/*! + \fn bool TQPainter::hasViewXForm() const + + Returns TRUE if view transformation is enabled; otherwise returns + FALSE. + + \sa setViewXForm(), xForm() +*/ + +/*! + Returns the window rectangle. + + \sa setWindow(), setViewXForm() +*/ + +TQRect TQPainter::window() const +{ + return TQRect( wx, wy, ww, wh ); +} + +/*! + Sets the window rectangle view transformation for the painter and + enables view transformation. + + The window rectangle is part of the view transformation. The + window specifies the logical coordinate system and is specified by + the \a x, \a y, \a w width and \a h height parameters. Its sister, + the viewport(), specifies the device coordinate system. + + The default window rectangle is the same as the device's + rectangle. See the \link coordsys.html Coordinate System Overview + \endlink for an overview of coordinate transformation. + + \sa window(), setViewport(), setViewXForm(), setWorldMatrix(), + setWorldXForm() +*/ + +void TQPainter::setWindow( int x, int y, int w, int h ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setWindow: Will be reset by begin()" ); +#endif + wx = x; + wy = y; + ww = w; + wh = h; + if ( testf(ExtDev) ) { + TQRect r( x, y, w, h ); + TQPDevCmdParam param[1]; + param[0].rect = (TQRect*)&r; + pdev->cmd( TQPaintDevice::PdcSetWindow, this, param ); + } + if ( testf(VxF) ) + updateXForm(); + else + setViewXForm( TRUE ); +} + +/*! + Returns the viewport rectangle. + + \sa setViewport(), setViewXForm() +*/ + +TQRect TQPainter::viewport() const // get viewport +{ + return TQRect( vx, vy, vw, vh ); +} + +/*! + Sets the viewport rectangle view transformation for the painter + and enables view transformation. + + The viewport rectangle is part of the view transformation. The + viewport specifies the device coordinate system and is specified + by the \a x, \a y, \a w width and \a h height parameters. Its + sister, the window(), specifies the logical coordinate system. + + The default viewport rectangle is the same as the device's + rectangle. See the \link coordsys.html Coordinate System Overview + \endlink for an overview of coordinate transformation. + + \sa viewport(), setWindow(), setViewXForm(), setWorldMatrix(), + setWorldXForm(), xForm() +*/ + +void TQPainter::setViewport( int x, int y, int w, int h ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setViewport: Will be reset by begin()" ); +#endif + vx = x; + vy = y; + vw = w; + vh = h; + if ( testf(ExtDev) ) { + TQRect r( x, y, w, h ); + TQPDevCmdParam param[1]; + param[0].rect = (TQRect*)&r; + pdev->cmd( TQPaintDevice::PdcSetViewport, this, param ); + } + if ( testf(VxF) ) + updateXForm(); + else + setViewXForm( TRUE ); +} + + +/*! + Enables world transformations if \a enable is TRUE, or disables + world transformations if \a enable is FALSE. The world + transformation matrix is not changed. + + \sa setWorldMatrix(), setWindow(), setViewport(), setViewXForm(), + xForm() +*/ + +void TQPainter::setWorldXForm( bool enable ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setWorldXForm: Will be reset by begin()" ); +#endif + if ( !isActive() || enable == testf(WxF) ) + return; + setf( WxF, enable ); + if ( testf(ExtDev) && !block_ext ) { + TQPDevCmdParam param[1]; + param[0].ival = enable; + pdev->cmd( TQPaintDevice::PdcSetWXform, this, param ); + } + updateXForm(); +} + +/*! + \fn bool TQPainter::hasWorldXForm() const + + Returns TRUE if world transformation is enabled; otherwise returns + FALSE. + + \sa setWorldXForm() +*/ + +/*! + Returns the world transformation matrix. + + \sa setWorldMatrix() +*/ + +const TQWMatrix &TQPainter::worldMatrix() const +{ + return wxmat; +} + +/*! + Sets the world transformation matrix to \a m and enables world + transformation. + + If \a combine is TRUE, then \a m is combined with the current + transformation matrix, otherwise \a m replaces the current + transformation matrix. + + If \a m is the identity matrix and \a combine is FALSE, this + function calls setWorldXForm(FALSE). (The identity matrix is the + matrix where TQWMatrix::m11() and TQWMatrix::m22() are 1.0 and the + rest are 0.0.) + + World transformations are applied after the view transformations + (i.e. \link setWindow() window\endlink and \link setViewport() + viewport\endlink). + + The following functions can transform the coordinate system without using + a TQWMatrix: + \list + \i translate() + \i scale() + \i shear() + \i rotate() + \endlist + + They operate on the painter's worldMatrix() and are implemented like this: + + \code + void TQPainter::rotate( double a ) + { + TQWMatrix m; + m.rotate( a ); + setWorldMatrix( m, TRUE ); + } + \endcode + + Note that you should always use \a combine when you are drawing + into a TQPicture. Otherwise it may not be possible to replay the + picture with additional transformations. Using translate(), + scale(), etc., is safe. + + For a brief overview of coordinate transformation, see the \link + coordsys.html Coordinate System Overview. \endlink + + \sa worldMatrix() setWorldXForm() setWindow() setViewport() + setViewXForm() xForm() TQWMatrix +*/ + +void TQPainter::setWorldMatrix( const TQWMatrix &m, bool combine ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::setWorldMatrix: Will be reset by begin()" ); +#endif + return; + } + if ( combine ) + wxmat = m * wxmat; // combines + else + wxmat = m; // set new matrix + bool identity = wxmat.m11() == 1.0F && wxmat.m22() == 1.0F && + wxmat.m12() == 0.0F && wxmat.m21() == 0.0F && + wxmat.dx() == 0.0F && wxmat.dy() == 0.0F; + if ( testf(ExtDev) && !block_ext ) { + TQPDevCmdParam param[2]; + param[0].matrix = &m; + param[1].ival = combine; + pdev->cmd( TQPaintDevice::PdcSetWMatrix, this, param ); + } + if ( identity && pdev->devType() != TQInternal::Picture ) + setWorldXForm( FALSE ); + else if ( !testf(WxF) ) + setWorldXForm( TRUE ); + else + updateXForm(); +} + +/*! \obsolete + + We recommend using save() instead. +*/ + +void TQPainter::saveWorldMatrix() +{ + TQWMatrixStack *stack = (TQWMatrixStack *)wm_stack; + if ( stack == 0 ) { + stack = new TQPtrStack; + Q_CHECK_PTR( stack ); + stack->setAutoDelete( TRUE ); + wm_stack = stack; + } + + stack->push( new TQWMatrix( wxmat ) ); + +} + +/*! \obsolete + We recommend using restore() instead. +*/ + +void TQPainter::restoreWorldMatrix() +{ + TQWMatrixStack *stack = (TQWMatrixStack *)wm_stack; + if ( stack == 0 || stack->isEmpty() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::restoreWorldMatrix: Empty stack error" ); +#endif + return; + } + TQWMatrix* m = stack->pop(); + setWorldMatrix( *m ); + delete m; +} + +#endif // QT_NO_TRANSFORMATIONS + +/*! + Translates the coordinate system by \a (dx, dy). After this call, + \a (dx, dy) is added to points. + + For example, the following code draws the same point twice: + \code + void MyWidget::paintEvent() + { + TQPainter paint( this ); + + paint.drawPoint( 0, 0 ); + + paint.translate( 100.0, 40.0 ); + paint.drawPoint( -100, -40 ); + } + \endcode + + \sa scale(), shear(), rotate(), resetXForm(), setWorldMatrix(), xForm() +*/ + +void TQPainter::translate( double dx, double dy ) +{ +#ifndef QT_NO_TRANSFORMATIONS + TQWMatrix m; + m.translate( dx, dy ); + setWorldMatrix( m, TRUE ); +#else + xlatex += (int)dx; + xlatey += (int)dy; + setf( VxF, xlatex || xlatey ); +#endif +} + + +#ifndef QT_NO_TRANSFORMATIONS +/*! + Scales the coordinate system by \a (sx, sy). + + \sa translate(), shear(), rotate(), resetXForm(), setWorldMatrix(), + xForm() +*/ + +void TQPainter::scale( double sx, double sy ) +{ + TQWMatrix m; + m.scale( sx, sy ); + setWorldMatrix( m, TRUE ); +} + +/*! + Shears the coordinate system by \a (sh, sv). + + \sa translate(), scale(), rotate(), resetXForm(), setWorldMatrix(), + xForm() +*/ + +void TQPainter::shear( double sh, double sv ) +{ + TQWMatrix m; + m.shear( sv, sh ); + setWorldMatrix( m, TRUE ); +} + +/*! + Rotates the coordinate system \a a degrees counterclockwise. + + \sa translate(), scale(), shear(), resetXForm(), setWorldMatrix(), + xForm() +*/ + +void TQPainter::rotate( double a ) +{ + TQWMatrix m; + m.rotate( a ); + setWorldMatrix( m, TRUE ); +} + + +/*! + Resets any transformations that were made using translate(), scale(), + shear(), rotate(), setWorldMatrix(), setViewport() and + setWindow(). + + \sa worldMatrix(), viewport(), window() +*/ + +void TQPainter::resetXForm() +{ + if ( !isActive() ) + return; + wx = wy = vx = vy = 0; // default view origins + ww = vw = pdev->metric( TQPaintDeviceMetrics::PdmWidth ); + wh = vh = pdev->metric( TQPaintDeviceMetrics::PdmHeight ); + wxmat = TQWMatrix(); + setWorldXForm( FALSE ); + setViewXForm( FALSE ); +} + +/*! + \internal + Updates an internal integer transformation matrix. +*/ + +void TQPainter::updateXForm() +{ + TQWMatrix m; + if ( testf(VxF) ) { + double scaleW = (double)vw/(double)ww; + double scaleH = (double)vh/(double)wh; + m.setMatrix( scaleW, 0, 0, scaleH, vx - wx*scaleW, vy - wy*scaleH ); + } + if ( testf(WxF) ) { + if ( testf(VxF) ) + m = wxmat * m; + else + m = wxmat; + } + xmat = m; + + txinv = FALSE; // no inverted matrix + txop = TxNone; + if ( m12()==0.0 && m21()==0.0 && m11() >= 0.0 && m22() >= 0.0 ) { + if ( m11()==1.0 && m22()==1.0 ) { + if ( dx()!=0.0 || dy()!=0.0 ) + txop = TxTranslate; + } else { + txop = TxScale; +#if defined(Q_WS_WIN) + setf(DirtyFont); +#endif + } + } else { + txop = TxRotShear; +#if defined(Q_WS_WIN) + setf(DirtyFont); +#endif + } +} + + +/*! + \internal + Updates an internal integer inverse transformation matrix. +*/ + +void TQPainter::updateInvXForm() +{ +#if defined(QT_CHECK_STATE) + Q_ASSERT( txinv == FALSE ); +#endif + txinv = TRUE; // creating inverted matrix + bool invertible; + TQWMatrix m; + if ( testf(VxF) ) { + m.translate( vx, vy ); + m.scale( 1.0*vw/ww, 1.0*vh/wh ); + m.translate( -wx, -wy ); + } + if ( testf(WxF) ) { + if ( testf(VxF) ) + m = wxmat * m; + else + m = wxmat; + } + ixmat = m.invert( &invertible ); // invert matrix +} + +#else +void TQPainter::resetXForm() +{ + xlatex = 0; + xlatey = 0; + clearf( VxF ); +} +#endif // QT_NO_TRANSFORMATIONS + + +extern bool qt_old_transformations; + +/*! + \internal + Maps a point from logical coordinates to device coordinates. +*/ + +void TQPainter::map( int x, int y, int *rx, int *ry ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( qt_old_transformations ) { + switch ( txop ) { + case TxNone: + *rx = x; *ry = y; + break; + case TxTranslate: + // #### "Why no rounding here?", Warwick asked of Haavard. + *rx = int(x + dx()); + *ry = int(y + dy()); + break; + case TxScale: { + double tx = m11()*x + dx(); + double ty = m22()*y + dy(); + *rx = tx >= 0 ? int(tx + 0.5) : int(tx - 0.5); + *ry = ty >= 0 ? int(ty + 0.5) : int(ty - 0.5); + } break; + default: { + double tx = m11()*x + m21()*y+dx(); + double ty = m12()*x + m22()*y+dy(); + *rx = tx >= 0 ? int(tx + 0.5) : int(tx - 0.5); + *ry = ty >= 0 ? int(ty + 0.5) : int(ty - 0.5); + } break; + } + } else { + switch ( txop ) { + case TxNone: + *rx = x; + *ry = y; + break; + case TxTranslate: + *rx = qRound( x + dx() ); + *ry = qRound( y + dy() ); + break; + case TxScale: + *rx = qRound( m11()*x + dx() ); + *ry = qRound( m22()*y + dy() ); + break; + default: + *rx = qRound( m11()*x + m21()*y+dx() ); + *ry = qRound( m12()*x + m22()*y+dy() ); + break; + } + } +#else + *rx = x + xlatex; + *ry = y + xlatey; +#endif +} + +/*! + \internal + Maps a rectangle from logical coordinates to device coordinates. + This internal function does not handle rotation and/or shear. +*/ + +void TQPainter::map( int x, int y, int w, int h, + int *rx, int *ry, int *rw, int *rh ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( qt_old_transformations ) { + switch ( txop ) { + case TxNone: + *rx = x; *ry = y; + *rw = w; *rh = h; + break; + case TxTranslate: + // #### "Why no rounding here?", Warwick asked of Haavard. + *rx = int(x + dx()); + *ry = int(y + dy()); + *rw = w; *rh = h; + break; + case TxScale: { + double tx1 = m11()*x + dx(); + double ty1 = m22()*y + dy(); + double tx2 = m11()*(x + w - 1) + dx(); + double ty2 = m22()*(y + h - 1) + dy(); + *rx = qRound( tx1 ); + *ry = qRound( ty1 ); + *rw = qRound( tx2 ) - *rx + 1; + *rh = qRound( ty2 ) - *ry + 1; + } break; + default: +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::map: Internal error" ); +#endif + break; + } + } else { + switch ( txop ) { + case TxNone: + *rx = x; *ry = y; + *rw = w; *rh = h; + break; + case TxTranslate: + *rx = qRound(x + dx() ); + *ry = qRound(y + dy() ); + *rw = w; *rh = h; + break; + case TxScale: + *rx = qRound( m11()*x + dx() ); + *ry = qRound( m22()*y + dy() ); + *rw = qRound( m11()*w ); + *rh = qRound( m22()*h ); + break; + default: +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::map: Internal error" ); +#endif + break; + } + } +#else + *rx = x + xlatex; + *ry = y + xlatey; + *rw = w; *rh = h; +#endif +} + +/*! + \internal + Maps a point from device coordinates to logical coordinates. +*/ + +void TQPainter::mapInv( int x, int y, int *rx, int *ry ) const +{ +#ifndef QT_NO_TRANSFORMATIONS +#if defined(QT_CHECK_STATE) + if ( !txinv ) + qWarning( "TQPainter::mapInv: Internal error" ); +#endif + if ( qt_old_transformations ) { + double tx = im11()*x + im21()*y+idx(); + double ty = im12()*x + im22()*y+idy(); + *rx = tx >= 0 ? int(tx + 0.5) : int(tx - 0.5); + *ry = ty >= 0 ? int(ty + 0.5) : int(ty - 0.5); + } else { + *rx = qRound( im11()*x + im21()*y + idx() ); + *ry = qRound( im12()*x + im22()*y + idy() ); + } +#else + *rx = x - xlatex; + *ry = y - xlatey; +#endif +} + +/*! + \internal + Maps a rectangle from device coordinates to logical coordinates. + Cannot handle rotation and/or shear. +*/ + +void TQPainter::mapInv( int x, int y, int w, int h, + int *rx, int *ry, int *rw, int *rh ) const +{ +#ifndef QT_NO_TRANSFORMATIONS +#if defined(QT_CHECK_STATE) + if ( !txinv || txop == TxRotShear ) + qWarning( "TQPainter::mapInv: Internal error" ); +#endif + if ( qt_old_transformations ) { + double tx = im11()*x + idx(); + double ty = im22()*y + idy(); + double tw = im11()*w; + double th = im22()*h; + *rx = tx >= 0 ? int(tx + 0.5) : int(tx - 0.5); + *ry = ty >= 0 ? int(ty + 0.5) : int(ty - 0.5); + *rw = tw >= 0 ? int(tw + 0.5) : int(tw - 0.5); + *rh = th >= 0 ? int(th + 0.5) : int(th - 0.5); + } else { + *rx = qRound( im11()*x + idx() ); + *ry = qRound( im22()*y + idy() ); + *rw = qRound( im11()*w ); + *rh = qRound( im22()*h ); + } +#else + *rx = x - xlatex; + *ry = y - xlatey; + *rw = w; + *rh = h; +#endif +} + + +/*! + Returns the point \a pv transformed from model coordinates to + device coordinates. + + \sa xFormDev(), TQWMatrix::map() +*/ + +TQPoint TQPainter::xForm( const TQPoint &pv ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return pv; + int x=pv.x(), y=pv.y(); + map( x, y, &x, &y ); + return TQPoint( x, y ); +#else + return TQPoint( pv.x()+xlatex, pv.y()+xlatey ); +#endif +} + +/*! + \overload + + Returns the rectangle \a rv transformed from model coordinates to + device coordinates. + + If world transformation is enabled and rotation or shearing has + been specified, then the bounding rectangle is returned. + + \sa xFormDev(), TQWMatrix::map() +*/ + +TQRect TQPainter::xForm( const TQRect &rv ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return rv; + if ( txop == TxRotShear ) { // rotation/shear + return xmat.mapRect( rv ); + } + // Just translation/scale + int x, y, w, h; + rv.rect( &x, &y, &w, &h ); + map( x, y, w, h, &x, &y, &w, &h ); + return TQRect( x, y, w, h ); +#else + return TQRect( rv.x()+xlatex, rv.y()+xlatey, rv.width(), rv.height() ); +#endif +} + +/*! + \overload + + Returns the point array \a av transformed from model coordinates + to device coordinates. + + \sa xFormDev(), TQWMatrix::map() +*/ + +TQPointArray TQPainter::xForm( const TQPointArray &av ) const +{ + TQPointArray a = av; +#ifndef QT_NO_TRANSFORMATIONS + if ( txop != TxNone ) + { + return xmat * av; + } +#else + a.translate( xlatex, xlatey ); +#endif + return a; +} + +/*! + \overload + + Returns the point array \a av transformed from model coordinates + to device coordinates. The \a index is the first point in the + array and \a npoints denotes the number of points to be + transformed. If \a npoints is negative, all points from \a + av[index] until the last point in the array are transformed. + + The returned point array consists of the number of points that + were transformed. + + Example: + \code + TQPointArray a(10); + TQPointArray b; + b = painter.xForm(a, 2, 4); // b.size() == 4 + b = painter.xForm(a, 2, -1); // b.size() == 8 + \endcode + + \sa xFormDev(), TQWMatrix::map() +*/ + +TQPointArray TQPainter::xForm( const TQPointArray &av, int index, + int npoints ) const +{ + int lastPoint = npoints < 0 ? av.size() : index+npoints; + TQPointArray a( lastPoint-index ); + memcpy( a.data(), av.data()+index, (lastPoint-index)*sizeof( TQPoint ) ); +#ifndef QT_NO_TRANSFORMATIONS + return xmat*a; +#else + a.translate( xlatex, xlatey ); + return a; +#endif +} + +/*! + \overload + + Returns the point \a pd transformed from device coordinates to + model coordinates. + + \sa xForm(), TQWMatrix::map() +*/ + +TQPoint TQPainter::xFormDev( const TQPoint &pd ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return pd; + if ( !txinv ) { + TQPainter *that = (TQPainter*)this; // mutable + that->updateInvXForm(); + } +#endif + int x=pd.x(), y=pd.y(); + mapInv( x, y, &x, &y ); + return TQPoint( x, y ); +} + +/*! + Returns the rectangle \a rd transformed from device coordinates to + model coordinates. + + If world transformation is enabled and rotation or shearing is + used, then the bounding rectangle is returned. + + \sa xForm(), TQWMatrix::map() +*/ + +TQRect TQPainter::xFormDev( const TQRect &rd ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return rd; + if ( !txinv ) { + TQPainter *that = (TQPainter*)this; // mutable + that->updateInvXForm(); + } + if ( txop == TxRotShear ) { // rotation/shear + return ixmat.mapRect( rd ); + } +#endif + // Just translation/scale + int x, y, w, h; + rd.rect( &x, &y, &w, &h ); + mapInv( x, y, w, h, &x, &y, &w, &h ); + return TQRect( x, y, w, h ); +} + +/*! + \overload + + Returns the point array \a ad transformed from device coordinates + to model coordinates. + + \sa xForm(), TQWMatrix::map() +*/ + +TQPointArray TQPainter::xFormDev( const TQPointArray &ad ) const +{ +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return ad; + if ( !txinv ) { + TQPainter *that = (TQPainter*)this; // mutable + that->updateInvXForm(); + } + return ixmat * ad; +#else + // ### + return ad; +#endif +} + +/*! + \overload + + Returns the point array \a ad transformed from device coordinates + to model coordinates. The \a index is the first point in the array + and \a npoints denotes the number of points to be transformed. If + \a npoints is negative, all points from \a ad[index] until the + last point in the array are transformed. + + The returned point array consists of the number of points that + were transformed. + + Example: + \code + TQPointArray a(10); + TQPointArray b; + b = painter.xFormDev(a, 1, 3); // b.size() == 3 + b = painter.xFormDev(a, 1, -1); // b.size() == 9 + \endcode + + \sa xForm(), TQWMatrix::map() +*/ + +TQPointArray TQPainter::xFormDev( const TQPointArray &ad, int index, + int npoints ) const +{ + int lastPoint = npoints < 0 ? ad.size() : index+npoints; + TQPointArray a( lastPoint-index ); + memcpy( a.data(), ad.data()+index, (lastPoint-index)*sizeof( TQPoint ) ); +#ifndef QT_NO_TRANSFORMATIONS + if ( txop == TxNone ) + return a; + if ( !txinv ) { + TQPainter *that = (TQPainter*)this; // mutable + that->updateInvXForm(); + } + return ixmat * a; +#else + // ### + return a; +#endif +} + + +/*! + Fills the rectangle \a (x, y, w, h) with the \a brush. + + You can specify a TQColor as \a brush, since there is a TQBrush + constructor that takes a TQColor argument and creates a solid + pattern brush. + + \sa drawRect() +*/ + +void TQPainter::fillRect( int x, int y, int w, int h, const TQBrush &brush ) +{ + TQPen oldPen = pen(); // save pen + TQBrush oldBrush = this->brush(); // save brush + setPen( NoPen ); + setBrush( brush ); + drawRect( x, y, w, h ); // draw filled rect + setBrush( oldBrush ); // restore brush + setPen( oldPen ); // restore pen +} + + +/*! + \overload void TQPainter::setBrushOrigin( const TQPoint &p ) + + Sets the brush origin to point \a p. +*/ + +/*! + \overload void TQPainter::setWindow( const TQRect &r ) + + Sets the painter's window to rectangle \a r. +*/ + + +/*! + \overload void TQPainter::setViewport( const TQRect &r ) + + Sets the painter's viewport to rectangle \a r. +*/ + + +/*! + \fn bool TQPainter::hasClipping() const + + Returns TRUE if clipping has been set; otherwise returns FALSE. + + \sa setClipping() +*/ + +/*! + Returns the currently set clip region. Note that the clip region + is given in physical device coordinates and \e not subject to any + \link coordsys.html coordinate transformation \endlink if \a m is + equal to \c CoordDevice (the default). If \a m equals \c + CoordPainter the returned region is in model coordinates. + + \sa setClipRegion(), setClipRect(), setClipping() TQPainter::CoordinateMode +*/ +TQRegion TQPainter::clipRegion( CoordinateMode m ) const +{ + // ### FIXME in 4.0: + // If the transformation mode is CoordPainter, we should transform the + // clip region with painter transformations. + +#ifndef QT_NO_TRANSFORMATIONS + TQRegion r; + if ( m == CoordDevice ) { + r = crgn; + } else { + if ( !txinv ) { + TQPainter *that = (TQPainter*)this; // mutable + that->updateInvXForm(); + } + + r = ixmat * crgn; + } + return r; +#else + return crgn; +#endif +} + +/*! + \fn void TQPainter::setClipRect( int x, int y, int w, int h, CoordinateMode m) + + Sets the clip region to the rectangle \a x, \a y, \a w, \a h and + enables clipping. The clip mode is set to \a m. + + If \a m is \c CoordDevice (the default), the coordinates given for + the clip region are taken to be physical device coordinates and + are \e not subject to any \link coordsys.html coordinate + transformations\endlink. If \a m is \c CoordPainter, the + coordinates given for the clip region are taken to be model + coordinates. + + \sa setClipRegion(), clipRegion(), setClipping() TQPainter::CoordinateMode +*/ + +/*! + \overload void TQPainter::drawPoint( const TQPoint &p ) + + Draws the point \a p. +*/ + + +/*! + \overload void TQPainter::moveTo( const TQPoint &p ) + + Moves to the point \a p. +*/ + +/*! + \overload void TQPainter::lineTo( const TQPoint &p ) + + Draws a line to the point \a p. +*/ + +/*! + \overload void TQPainter::drawLine( const TQPoint &p1, const TQPoint &p2 ) + + Draws a line from point \a p1 to point \a p2. +*/ + +/*! + \overload void TQPainter::drawRect( const TQRect &r ) + + Draws the rectangle \a r. +*/ + +/*! + \overload void TQPainter::drawWinFocusRect( const TQRect &r ) + + Draws rectangle \a r as a window focus rectangle. +*/ + +/*! + \overload void TQPainter::drawWinFocusRect( const TQRect &r, const TQColor &bgColor ) + + Draws rectangle \a r as a window focus rectangle using background + color \a bgColor. +*/ + + +#if !defined(Q_WS_X11) && !defined(Q_WS_QWS) && !defined(Q_WS_MAC) +// The doc and X implementation of this functions is in qpainter_x11.cpp +void TQPainter::drawWinFocusRect( int, int, int, int, + bool, const TQColor & ) +{ + // do nothing, only called from X11 specific functions +} +#endif + + +/*! + \overload void TQPainter::drawRoundRect( const TQRect &r, int xRnd, int yRnd ) + + Draws a rounded rectangle \a r, rounding to the x position \a xRnd + and the y position \a yRnd on each corner. +*/ + +/*! + \overload void TQPainter::drawEllipse( const TQRect &r ) + + Draws the ellipse that fits inside rectangle \a r. +*/ + +/*! + \overload void TQPainter::drawArc( const TQRect &r, int a, int alen ) + + Draws the arc that fits inside the rectangle \a r with start angle + \a a and arc length \a alen. +*/ + +/*! + \overload void TQPainter::drawPie( const TQRect &r, int a, int alen ) + + Draws a pie segment that fits inside the rectangle \a r with start + angle \a a and arc length \a alen. +*/ + +/*! + \overload void TQPainter::drawChord( const TQRect &r, int a, int alen ) + + Draws a chord that fits inside the rectangle \a r with start angle + \a a and arc length \a alen. +*/ + +/*! + \overload void TQPainter::drawPixmap( const TQPoint &p, const TQPixmap &pm, const TQRect &sr ) + + Draws the rectangle \a sr of pixmap \a pm with its origin at point + \a p. +*/ + +/*! + \overload void TQPainter::drawPixmap( const TQPoint &p, const TQPixmap &pm ) + + Draws the pixmap \a pm with its origin at point \a p. +*/ + +void TQPainter::drawPixmap( const TQPoint &p, const TQPixmap &pm ) +{ + drawPixmap( p.x(), p.y(), pm, 0, 0, pm.width(), pm.height() ); +} + +#if !defined(QT_NO_IMAGE_SMOOTHSCALE) || !defined(QT_NO_PIXMAP_TRANSFORMATION) + +/*! + \overload + + Draws the pixmap \a pm into the rectangle \a r. The pixmap is + scaled to fit the rectangle, if image and rectangle size disagree. +*/ +void TQPainter::drawPixmap( const TQRect &r, const TQPixmap &pm ) +{ + int rw = r.width(); + int rh = r.height(); + int iw= pm.width(); + int ih = pm.height(); + if ( rw <= 0 || rh <= 0 || iw <= 0 || ih <= 0 ) + return; + bool scale = ( rw != iw || rh != ih ); + float scaleX = (float)rw/(float)iw; + float scaleY = (float)rh/(float)ih; + bool smooth = ( scaleX < 1.5 || scaleY < 1.5 ); + + if ( testf(ExtDev) ) { + TQPDevCmdParam param[2]; + param[0].rect = &r; + param[1].pixmap = ± +#if defined(Q_WS_WIN) + if ( !pdev->cmd( TQPaintDevice::PdcDrawPixmap, this, param ) || !hdc ) + return; +#elif defined(Q_WS_QWS) + pdev->cmd( TQPaintDevice::PdcDrawPixmap, this, param ); + return; +#elif defined(Q_WS_MAC) + if ( !pdev->cmd( TQPaintDevice::PdcDrawPixmap, this, param ) || !pdev->handle()) + return; +#else + if ( !pdev->cmd( TQPaintDevice::PdcDrawPixmap, this, param ) || !hd ) + return; +#endif + } + + TQPixmap pixmap = pm; + + if ( scale ) { +#ifndef QT_NO_IMAGE_SMOOTHSCALE +# ifndef QT_NO_PIXMAP_TRANSFORMATION + if ( smooth ) +# endif + { + TQImage i = pm.convertToImage(); + pixmap = TQPixmap( i.smoothScale( rw, rh ) ); + } +# ifndef QT_NO_PIXMAP_TRANSFORMATION + else +# endif +#endif +#ifndef QT_NO_PIXMAP_TRANSFORMATION + { + pixmap = pm.xForm( TQWMatrix( scaleX, 0, 0, scaleY, 0, 0 ) ); + } +#endif + } + drawPixmap( r.x(), r.y(), pixmap ); +} + +#endif + +/*! + \overload void TQPainter::drawImage( const TQPoint &, const TQImage &, const TQRect &sr, int conversionFlags = 0 ); + + Draws the rectangle \a sr from the image at the given point. +*/ + +/* + Draws at point \a p the \sr rect from image \a pm, using \a + conversionFlags if the image needs to be converted to a pixmap. + The default value for \a conversionFlags is 0; see + convertFromImage() for information about what other values do. + + This function may convert \a image to a pixmap and then draw it, if + device() is a TQPixmap or a TQWidget, or else draw it directly, if + device() is a TQPrinter or TQPicture. +*/ + +/*! + Draws at (\a x, \a y) the \a sw by \a sh area of pixels from (\a + sx, \a sy) in \a image, using \a conversionFlags if the image + needs to be converted to a pixmap. The default value for \a + conversionFlags is 0; see convertFromImage() for information about + what other values do. + + This function may convert \a image to a pixmap and then draw it, + if device() is a TQPixmap or a TQWidget, or else draw it directly, + if device() is a TQPrinter or TQPicture. + + Currently alpha masks of the image are ignored when painting on a TQPrinter. + + \sa drawPixmap() TQPixmap::convertFromImage() +*/ +void TQPainter::drawImage( int x, int y, const TQImage & image, + int sx, int sy, int sw, int sh, + int conversionFlags ) +{ +#ifdef Q_WS_QWS + //### Hackish +# ifndef QT_NO_TRANSFORMATIONS + if ( !image.isNull() && gfx && + (txop==TxNone||txop==TxTranslate) && !testf(ExtDev) ) +# else + if ( !image.isNull() && gfx && !testf(ExtDev) ) +# endif + { + if(sw<0) + sw=image.width(); + if(sh<0) + sh=image.height(); + + TQImage image2 = qt_screen->mapToDevice( image ); + + // This is a bit dubious + if(image2.depth()==1) { + image2.setNumColors( 2 ); + image2.setColor( 0, qRgb(255,255,255) ); + image2.setColor( 1, qRgb(0,0,0) ); + } + if ( image2.hasAlphaBuffer() ) + gfx->setAlphaType(TQGfx::InlineAlpha); + else + gfx->setAlphaType(TQGfx::IgnoreAlpha); + gfx->setSource(&image2); + if ( testf(VxF|WxF) ) { + map( x, y, &x, &y ); + } + gfx->blt(x,y,sw,sh,sx,sy); + return; + } +#endif + + if ( !isActive() || image.isNull() ) + return; + + // right/bottom + if ( sw < 0 ) + sw = image.width() - sx; + if ( sh < 0 ) + sh = image.height() - sy; + + // Sanity-check clipping + if ( sx < 0 ) { + x -= sx; + sw += sx; + sx = 0; + } + if ( sw + sx > image.width() ) + sw = image.width() - sx; + if ( sy < 0 ) { + y -= sy; + sh += sy; + sy = 0; + } + if ( sh + sy > image.height() ) + sh = image.height() - sy; + + if ( sw <= 0 || sh <= 0 ) + return; + + bool all = image.rect().intersect(TQRect(sx,sy,sw,sh)) == image.rect(); + TQImage subimage = all ? image : image.copy(sx,sy,sw,sh); + + if ( testf(ExtDev) ) { + TQPDevCmdParam param[2]; + TQRect r( x, y, subimage.width(), subimage.height() ); + param[0].rect = &r; + param[1].image = &subimage; +#if defined(Q_WS_WIN) + if ( !pdev->cmd( TQPaintDevice::PdcDrawImage, this, param ) || !hdc ) + return; +#elif defined (Q_WS_QWS) + pdev->cmd( TQPaintDevice::PdcDrawImage, this, param ); + return; +#elif defined(Q_WS_MAC) + if(!pdev->cmd( TQPaintDevice::PdcDrawImage, this, param ) || !pdev->handle() ) + return; +#else + if ( !pdev->cmd( TQPaintDevice::PdcDrawImage, this, param ) || !hd ) + return; +#endif + } + + TQPixmap pm; + pm.convertFromImage( subimage, conversionFlags ); + drawPixmap( x, y, pm ); +} + +/*! + \overload void TQPainter::drawImage( const TQPoint &p, const TQImage &i, int conversion_flags ) + + Draws the image \a i at point \a p. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + \sa TQt::ImageConversionFlags +*/ +void TQPainter::drawImage( const TQPoint & p, const TQImage & i, + int conversion_flags ) +{ + drawImage(p, i, i.rect(), conversion_flags); +} + +#if !defined(QT_NO_IMAGE_TRANSFORMATION) || !defined(QT_NO_IMAGE_SMOOTHSCALE) + +/*! + \overload + + Draws the image \a i into the rectangle \a r. The image will be + scaled to fit the rectangle if image and rectangle dimensions + differ. +*/ +void TQPainter::drawImage( const TQRect &r, const TQImage &i ) +{ + int rw = r.width(); + int rh = r.height(); + int iw= i.width(); + int ih = i.height(); + if ( rw <= 0 || rh <= 0 || iw <= 0 || ih <= 0 ) + return; + + if ( testf(ExtDev) ) { + TQPDevCmdParam param[2]; + param[0].rect = &r; + param[1].image = &i; +#if defined(Q_WS_WIN) + if ( !pdev->cmd( TQPaintDevice::PdcDrawImage, this, param ) || !hdc ) + return; +#elif defined(Q_WS_QWS) + pdev->cmd( TQPaintDevice::PdcDrawImage, this, param ); + return; +#elif defined(Q_WS_MAC) + if ( !pdev->cmd( TQPaintDevice::PdcDrawImage, this, param ) || !pdev->handle() ) + return; +#else + if ( !pdev->cmd( TQPaintDevice::PdcDrawImage, this, param ) || !hd ) + return; +#endif + } + + + bool scale = ( rw != iw || rh != ih ); + float scaleX = (float)rw/(float)iw; + float scaleY = (float)rh/(float)ih; + bool smooth = ( scaleX < 1.5 || scaleY < 1.5 ); + + TQImage img = scale + ? ( +#if defined(QT_NO_IMAGE_TRANSFORMATION) + i.smoothScale( rw, rh ) +#elif defined(QT_NO_IMAGE_SMOOTHSCALE) + i.scale( rw, rh ) +#else + smooth ? i.smoothScale( rw, rh ) : i.scale( rw, rh ) +#endif + ) + : i; + + drawImage( r.x(), r.y(), img ); +} + +#endif + + +void bitBlt( TQPaintDevice *dst, int dx, int dy, + const TQImage *src, int sx, int sy, int sw, int sh, + int conversion_flags ) +{ + TQPixmap tmp; + if ( sx == 0 && sy == 0 + && (sw<0 || sw==src->width()) && (sh<0 || sh==src->height()) ) + { + tmp.convertFromImage( *src, conversion_flags ); + } else { + tmp.convertFromImage( src->copy( sx, sy, sw, sh, conversion_flags), + conversion_flags ); + } + bitBlt( dst, dx, dy, &tmp ); +} + + +/*! + \overload void TQPainter::drawTiledPixmap( const TQRect &r, const TQPixmap &pm, const TQPoint &sp ) + + Draws a tiled pixmap, \a pm, inside rectangle \a r with its origin + at point \a sp. +*/ + +/*! + \overload void TQPainter::drawTiledPixmap( const TQRect &r, const TQPixmap &pm ) + + Draws a tiled pixmap, \a pm, inside rectangle \a r. +*/ + +/*! + \overload void TQPainter::fillRect( const TQRect &r, const TQBrush &brush ) + + Fills the rectangle \a r using brush \a brush. +*/ + +/*! + \fn void TQPainter::eraseRect( int x, int y, int w, int h ) + + Erases the area inside \a x, \a y, \a w, \a h. Equivalent to + \c{fillRect( x, y, w, h, backgroundColor() )}. +*/ + +/*! + \overload void TQPainter::eraseRect( const TQRect &r ) + + Erases the area inside the rectangle \a r. +*/ + +/*! + \fn TQPainter::drawText( int x, int y, const TQString &, int len = -1, TextDirection dir = Auto ) + + \overload + + Draws the given text at position \a x, \a y. If \a len is -1 (the + default) all the text is drawn, otherwise the first \a len + characters are drawn. The text's direction is given by \a dir. + + \sa TQPainter::TextDirection +*/ + +/*! + \fn void TQPainter::drawText( int x, int y, int w, int h, int flags, + const TQString&, int len = -1, TQRect *br=0, + TQTextParag **internal=0 ) + + \overload + + Draws the given text within the rectangle starting at \a x, \a y, + with width \a w and height \a h. If \a len is -1 (the default) all + the text is drawn, otherwise the first \a len characters are + drawn. The text's flags that are given in the \a flags parameter + are \l{TQt::AlignmentFlags} and \l{TQt::TextFlags} OR'd together. \a + br (if not null) is set to the actual bounding rectangle of the + output. The \a internal parameter is for internal use only. +*/ + +/*! + \fn void TQPainter::drawText( const TQPoint &, const TQString &, int len = -1, TextDirection dir = Auto ); + + \overload + + Draws the text at the given point. + + \sa TQPainter::TextDirection +*/ + +/* + Draws the text in \a s at point \a p. If \a len is -1 the entire + string is drawn, otherwise just the first \a len characters. The + text's direction is specified by \a dir. +*/ + + +/*! + \fn void TQPainter::drawText( int x, int y, const TQString &, int pos, int len, TextDirection dir = Auto ); + + \overload + + Draws the text from position \a pos, at point \a (x, y). If \a len is + -1 the entire string is drawn, otherwise just the first \a len + characters. The text's direction is specified by \a dir. +*/ + +/*! + \fn void TQPainter::drawText( const TQPoint &p, const TQString &, int pos, int len, TextDirection dir = Auto ); + + Draws the text from position \a pos, at point \a p. If \a len is + -1 the entire string is drawn, otherwise just the first \a len + characters. The text's direction is specified by \a dir. + + Note that the meaning of \e y is not the same for the two + drawText() varieties. For overloads that take a simple \e x, \e y + pair (or a point), the \e y value is the text's baseline; for + overloads that take a rectangle, \e rect.y() is the top of the + rectangle and the text is aligned within that rectangle in + accordance with the alignment flags. + + \sa TQPainter::TextDirection +*/ + +/*! + \fn void TQPainter::drawTextItem(const TQPoint &, const TQTextItem &, int) + \internal +*/ + +static inline void fix_neg_rect( int *x, int *y, int *w, int *h ) +{ + if ( *w < 0 ) { + *w = -*w + 2; + *x -= *w - 1; + } + if ( *h < 0 ) { + *h = -*h + 2; + *y -= *h - 1; + } +} +void TQPainter::fix_neg_rect( int *x, int *y, int *w, int *h ) +{ + ::fix_neg_rect(x,y,w,h); +} + +// +// The drawText function takes two special parameters; 'internal' and 'brect'. +// +// The 'internal' parameter contains a pointer to an array of encoded +// information that keeps internal geometry data. +// If the drawText function is called repeatedly to display the same text, +// it makes sense to calculate text width and linebreaks the first time, +// and use these parameters later to print the text because we save a lot of +// CPU time. +// The 'internal' parameter will not be used if it is a null pointer. +// The 'internal' parameter will be generated if it is not null, but points +// to a null pointer, i.e. internal != 0 && *internal == 0. +// The 'internal' parameter will be used if it contains a non-null pointer. +// +// If the 'brect parameter is a non-null pointer, then the bounding rectangle +// of the text will be returned in 'brect'. +// + +/*! + \overload + + Draws at most \a len characters from \a str in the rectangle \a r. + + This function draws formatted text. The \a tf text format is + really of type \l TQt::AlignmentFlags and \l TQt::TextFlags OR'd + together. + + Horizontal alignment defaults to AlignAuto and vertical alignment + defaults to AlignTop. + + \a brect (if not null) is set to the actual bounding rectangle of + the output. \a internal is, yes, internal. + + \sa boundingRect() +*/ + +void TQPainter::drawText( const TQRect &r, int tf, + const TQString& str, int len, TQRect *brect, + TQTextParag **internal ) +{ + if ( !isActive() ) + return; + if ( len < 0 ) + len = str.length(); + if ( len == 0 ) // empty string + return; + + if ( testf(DirtyFont|ExtDev) ) { + if ( testf(DirtyFont) ) + updateFont(); + if ( testf(ExtDev) && (tf & DontPrint) == 0 ) { + TQPDevCmdParam param[3]; + TQString newstr = str; + newstr.truncate( len ); + param[0].rect = &r; + param[1].ival = tf; + param[2].str = &newstr; + if ( pdev->devType() != TQInternal::Printer ) { +#if defined(Q_WS_WIN) + if ( !pdev->cmd( TQPaintDevice::PdcDrawText2Formatted, + this, param) || + !hdc ) + return; // TQPrinter wants PdcDrawText2 +#elif defined(Q_WS_QWS) + pdev->cmd( TQPaintDevice::PdcDrawText2Formatted, this, param); + return; +#elif defined(Q_WS_MAC) + if ( !pdev->cmd( TQPaintDevice::PdcDrawText2Formatted, this, param) || + !pdev->handle()) + return; // TQPrinter wants PdcDrawText2 +#else + if ( !pdev->cmd( TQPaintDevice::PdcDrawText2Formatted, + this, param) || + !hd ) + return; // TQPrinter wants PdcDrawText2 +#endif + } + } + } + + qt_format_text(font(), r, tf, str, len, brect, + tabstops, tabarray, tabarraylen, internal, this); +} + +//#define QT_FORMAT_TEXT_DEBUG + +#define TQChar_linesep TQChar(0x2028U) + +void qt_format_text( const TQFont& font, const TQRect &_r, + int tf, const TQString& str, int len, TQRect *brect, + int tabstops, int* tabarray, int tabarraylen, + TQTextParag **, TQPainter* painter ) +{ + // we need to copy r here to protect against the case (&r == brect). + TQRect r( _r ); + + bool dontclip = (tf & TQt::DontClip) == TQt::DontClip; + bool wordbreak = (tf & TQt::WordBreak) == TQt::WordBreak; + bool singleline = (tf & TQt::SingleLine) == TQt::SingleLine; + bool showprefix = (tf & TQt::ShowPrefix) == TQt::ShowPrefix; + bool noaccel = ( tf & TQt::NoAccel ) == TQt::NoAccel; + + bool isRightToLeft = str.isRightToLeft(); + if ( ( tf & TQt::AlignHorizontal_Mask ) == TQt::AlignAuto ) + tf |= isRightToLeft ? TQt::AlignRight : TQt::AlignLeft; + + bool expandtabs = ( (tf & TQt::ExpandTabs) && + ( ( (tf & TQt::AlignLeft) && !isRightToLeft ) || + ( (tf & TQt::AlignRight) && isRightToLeft ) ) ); + + if ( !painter ) + tf |= TQt::DontPrint; + + int maxUnderlines = 0; + int numUnderlines = 0; + int underlinePositionStack[32]; + int *underlinePositions = underlinePositionStack; + + TQFont fnt(painter ? (painter->pfont ? *painter->pfont : painter->cfont) : font); + TQFontMetrics fm( fnt ); + + TQString text = str; + // str.setLength() always does a deep copy, so the replacement + // code below is safe. + text.setLength( len ); + // compatible behaviour to the old implementation. Replace + // tabs by spaces + TQChar *chr = (TQChar*)text.unicode(); + const TQChar *end = chr + len; + bool haveLineSep = FALSE; + while ( chr != end ) { + if ( *chr == '\r' || ( singleline && *chr == '\n' ) ) { + *chr = ' '; + } else if ( *chr == '\n' ) { + *chr = TQChar_linesep; + haveLineSep = TRUE; + } else if ( *chr == '&' ) { + ++maxUnderlines; + } + ++chr; + } + if ( !expandtabs ) { + chr = (TQChar*)text.unicode(); + while ( chr != end ) { + if ( *chr == '\t' ) + *chr = ' '; + ++chr; + } + } else if (!tabarraylen && !tabstops) { + tabstops = fm.width('x')*8; + } + + if ( noaccel || showprefix ) { + if ( maxUnderlines > 32 ) + underlinePositions = new int[maxUnderlines]; + TQChar *cout = (TQChar*)text.unicode(); + TQChar *cin = cout; + int l = len; + while ( l ) { + if ( *cin == '&' ) { + ++cin; + --l; + if ( !l ) + break; + if ( *cin != '&' ) + underlinePositions[numUnderlines++] = cout - text.unicode(); + } + *cout = *cin; + ++cout; + ++cin; + --l; + } + uint newlen = cout - text.unicode(); + if ( newlen != text.length()) + text.setLength( newlen ); + } + + // no need to do extra work for underlines if we don't paint + if ( tf & TQt::DontPrint ) + numUnderlines = 0; + + int height = 0; + int left = r.width(); + int right = 0; + + TQTextLayout textLayout( text, fnt ); + int rb = TQMAX( 0, -fm.minRightBearing() ); + int lb = TQMAX( 0, -fm.minLeftBearing() ); + + if ( text.isEmpty() ) { + height = fm.height(); + left = right = 0; + tf |= TQPainter::DontPrint; + } else { + textLayout.beginLayout((haveLineSep || expandtabs || wordbreak) ? + TQTextLayout::MultiLine : + (tf & TQt::DontPrint) ? TQTextLayout::NoBidi : TQTextLayout::SingleLine ); + + // break underline chars into items of their own + for( int i = 0; i < numUnderlines; i++ ) { + textLayout.setBoundary( underlinePositions[i] ); + textLayout.setBoundary( underlinePositions[i]+1 ); + } + + int lineWidth = wordbreak ? TQMAX(0, r.width()-rb-lb) : INT_MAX; + if(!wordbreak) + tf |= TQt::IncludeTrailingSpaces; + + int leading = fm.leading(); + int asc = fm.ascent(); + int desc = fm.descent(); + height = -leading; + + //qDebug("\n\nbeginLayout: lw = %d, rectwidth=%d", lineWidth , r.width()); + while ( !textLayout.atEnd() ) { + height += leading; + textLayout.beginLine( lineWidth == INT_MAX ? lineWidth : lineWidth ); + //qDebug("-----beginLine( %d )-----", lineWidth ); + bool linesep = FALSE; + while ( 1 ) { + TQTextItem ti = textLayout.currentItem(); + //qDebug("item: from=%d, ch=%x", ti.from(), text.unicode()[ti.from()].unicode() ); + if ( expandtabs && ti.isTab() ) { + int tw = 0; + int x = textLayout.widthUsed(); + if ( tabarraylen ) { +// qDebug("tabarraylen=%d", tabarraylen ); + int tab = 0; + while ( tab < tabarraylen ) { + if ( tabarray[tab] > x ) { + tw = tabarray[tab] - x; + break; + } + ++tab; + } + } else { + tw = tabstops - (x % tabstops); + } + //qDebug("tw = %d", tw ); + if ( tw ) + ti.setWidth( tw ); + } + if ( ti.isObject() && text.unicode()[ti.from()] == TQChar_linesep ) + linesep = TRUE; + + if ( linesep || textLayout.addCurrentItem() != TQTextLayout::Ok || textLayout.atEnd() ) + break; + } + + int ascent = asc, descent = desc, lineLeft, lineRight; + textLayout.setLineWidth( r.width()-rb-lb ); + textLayout.endLine( 0, height, tf, &ascent, &descent, + &lineLeft, &lineRight ); + //qDebug("finalizing line: lw=%d ascent = %d, descent=%d lineleft=%d lineright=%d", lineWidth, ascent, descent,lineLeft, lineRight ); + left = TQMIN( left, lineLeft ); + right = TQMAX( right, lineRight ); + height += ascent + descent + 1; + if ( linesep ) + textLayout.nextItem(); + } + } + + int yoff = 0; + if ( tf & TQt::AlignBottom ) + yoff = r.height() - height; + else if ( tf & TQt::AlignVCenter ) + yoff = (r.height() - height)/2; + + if ( brect ) { + *brect = TQRect( r.x() + left, r.y() + yoff, right-left + lb+rb, height ); + //qDebug("br = %d %d %d/%d, left=%d, right=%d", brect->x(), brect->y(), brect->width(), brect->height(), left, right); + } + + if (!(tf & TQPainter::DontPrint)) { + bool restoreClipping = FALSE; + bool painterHasClip = FALSE; + TQRegion painterClipRegion; + if ( !dontclip ) { +#ifndef QT_NO_TRANSFORMATIONS + TQRegion reg = painter->xmat * r; +#else + TQRegion reg = r; + reg.translate( painter->xlatex, painter->xlatey ); +#endif + if ( painter->hasClipping() ) + reg &= painter->clipRegion(); + + painterHasClip = painter->hasClipping(); + painterClipRegion = painter->clipRegion(); + restoreClipping = TRUE; + painter->setClipRegion( reg ); + } else { + if ( painter->hasClipping() ){ + painterHasClip = painter->hasClipping(); + painterClipRegion = painter->clipRegion(); + restoreClipping = TRUE; + painter->setClipping( FALSE ); + } + } + + int cUlChar = 0; + int _tf = 0; + if (fnt.underline()) _tf |= TQt::Underline; + if (fnt.overline()) _tf |= TQt::Overline; + if (fnt.strikeOut()) _tf |= TQt::StrikeOut; + + //qDebug("have %d items",textLayout.numItems()); + for ( int i = 0; i < textLayout.numItems(); i++ ) { + TQTextItem ti = textLayout.itemAt( i ); + //qDebug("Item %d: from=%d, length=%d, space=%d x=%d", i, ti.from(), ti.length(), ti.isSpace(), ti.x() ); + if ( ti.isTab() || ti.isObject() ) + continue; + int textFlags = _tf; + if ( !noaccel && numUnderlines > cUlChar && ti.from() == underlinePositions[cUlChar] ) { + textFlags |= TQt::Underline; + cUlChar++; + } +#if defined(Q_WS_X11) || defined(Q_WS_QWS) + if ( painter->bg_mode == TQt::OpaqueMode ) { + int h = ti.ascent() + ti.descent() + 1; + if (ti.y() + h < height) + // don't add leading to last line + h += fm.leading(); + qt_draw_background( painter, r.x()+lb + ti.x(), r.y() + yoff + ti.y() - ti.ascent(), + ti.width(), h); + } +#endif + painter->drawTextItem( r.x()+lb, r.y() + yoff, ti, textFlags ); + } + + if ( restoreClipping ) { + painter->setClipRegion( painterClipRegion ); + painter->setClipping( painterHasClip ); + } + } + + if ( underlinePositions != underlinePositionStack ) + delete [] underlinePositions; +} + +/*! + \overload + + Returns the bounding rectangle of the aligned text that would be + printed with the corresponding drawText() function using the first + \a len characters from \a str if \a len is > -1, or the whole of + \a str if \a len is -1. The drawing, and hence the bounding + rectangle, is constrained to the rectangle \a r, or to the + rectangle retquired to draw the text, whichever is the larger. + + The \a internal parameter should not be used. + + \sa drawText(), fontMetrics(), TQFontMetrics::boundingRect(), TQt::TextFlags +*/ + +TQRect TQPainter::boundingRect( const TQRect &r, int flags, + const TQString& str, int len, TQTextParag **internal ) +{ + TQRect brect; + if ( str.isEmpty() ) + brect.setRect( r.x(),r.y(), 0,0 ); + else + drawText( r, flags | DontPrint, str, len, &brect, internal ); + return brect; +} + +/*! + \fn TQRect TQPainter::boundingRect( int x, int y, int w, int h, int flags, const TQString&, int len = -1, TQTextParag **intern=0 ); + + Returns the bounding rectangle of the aligned text that would be + printed with the corresponding drawText() function using the first + \a len characters of the string if \a len is > -1, or the whole of + the string if \a len is -1. The drawing, and hence the bounding + rectangle, is constrained to the rectangle that begins at point \a + (x, y) with width \a w and hight \a h, or to the + rectangle retquired to draw the text, whichever is the larger. + + The \a flags argument is + the bitwise OR of the following flags: + \table + \header \i Flag \i Meaning + \row \i \c AlignAuto \i aligns according to the language, usually left. + \row \i \c AlignLeft \i aligns to the left border. + \row \i \c AlignRight \i aligns to the right border. + \row \i \c AlignHCenter \i aligns horizontally centered. + \row \i \c AlignTop \i aligns to the top border. + \row \i \c AlignBottom \i aligns to the bottom border. + \row \i \c AlignVCenter \i aligns vertically centered. + \row \i \c AlignCenter \i (== \c AlignHCenter | \c AlignVCenter). + \row \i \c SingleLine \i ignores newline characters in the text. + \row \i \c ExpandTabs \i expands tabs. + \row \i \c ShowPrefix \i interprets "&x" as "x". + \row \i \c WordBreak \i breaks the text to fit the rectangle. + \endtable + + Horizontal alignment defaults to \c AlignLeft and vertical + alignment defaults to \c AlignTop. + + If several of the horizontal or several of the vertical alignment flags + are set, the resulting alignment is undefined. + + The \a intern parameter should not be used. + + \sa TQt::TextFlags +*/ + + + +/***************************************************************************** + TQPen member functions + *****************************************************************************/ + +/*! + \class TQPen qpen.h + \brief The TQPen class defines how a TQPainter should draw lines and outlines + of shapes. + + \ingroup graphics + \ingroup images + \ingroup shared + \mainclass + + A pen has a style, width, color, cap style and join style. + + The pen style defines the line type. The default pen style is \c + TQt::SolidLine. Setting the style to \c NoPen tells the painter to + not draw lines or outlines. + + When drawing 1 pixel wide diagonal lines you can either use a very + fast algorithm (specified by a line width of 0, which is the + default), or a slower but more accurate algorithm (specified by a + line width of 1). For horizontal and vertical lines a line width + of 0 is the same as a line width of 1. The cap and join style have + no effect on 0-width lines. + + The pen color defines the color of lines and text. The default + line color is black. The TQColor documentation lists predefined + colors. + + The cap style defines how the end points of lines are drawn. The + join style defines how the joins between two lines are drawn when + multiple connected lines are drawn (TQPainter::drawPolyline() + etc.). The cap and join styles only apply to wide lines, i.e. when + the width is 1 or greater. + + Use the TQBrush class to specify fill styles. + + Example: + \code + TQPainter painter; + TQPen pen( red, 2 ); // red solid line, 2 pixels wide + painter.begin( &anyPaintDevice ); // paint something + painter.setPen( pen ); // set the red, wide pen + painter.drawRect( 40,30, 200,100 ); // draw a rectangle + painter.setPen( blue ); // set blue pen, 0 pixel width + painter.drawLine( 40,30, 240,130 ); // draw a diagonal in rectangle + painter.end(); // painting done + \endcode + + See the \l TQt::PenStyle enum type for a complete list of pen + styles. + + With reference to the end points of lines, for wide (non-0-width) + pens it depends on the cap style whether the end point is drawn or + not. TQPainter will try to make sure that the end point is drawn + for 0-width pens, but this cannot be absolutely guaranteed because + the underlying drawing engine is free to use any (typically + accelerated) algorithm for drawing 0-width lines. On all tested + systems, however, the end point of at least all non-diagonal lines + are drawn. + + A pen's color(), width(), style(), capStyle() and joinStyle() can + be set in the constructor or later with setColor(), setWidth(), + setStyle(), setCapStyle() and setJoinStyle(). Pens may also be + compared and streamed. + + \img pen-styles.png Pen styles + + \sa TQPainter, TQPainter::setPen() +*/ + + +/*! + \internal + Initializes the pen. +*/ + +void TQPen::init( const TQColor &color, uint width, uint linestyle ) +{ + data = new TQPenData; + Q_CHECK_PTR( data ); + data->style = (PenStyle)(linestyle & MPenStyle); + data->width = width; + data->color = color; + data->linest = linestyle; +} + +/*! + Constructs a default black solid line pen with 0 width, which + renders lines 1 pixel wide (fast diagonals). +*/ + +TQPen::TQPen() +{ + init( TQt::black, 0, SolidLine ); // default pen +} + +/*! + Constructs a black pen with 0 width (fast diagonals) and style \a + style. + + \sa setStyle() +*/ + +TQPen::TQPen( PenStyle style ) +{ + init( TQt::black, 0, style ); +} + +/*! + Constructs a pen with the specified \a color, \a width and \a + style. + + \sa setWidth(), setStyle(), setColor() +*/ + +TQPen::TQPen( const TQColor &color, uint width, PenStyle style ) +{ + init( color, width, style ); +} + +/*! + Constructs a pen with the specified color \a cl and width \a w. + The pen style is set to \a s, the pen cap style to \a c and the + pen join style to \a j. + + A line width of 0 will produce a 1 pixel wide line using a fast + algorithm for diagonals. A line width of 1 will also produce a 1 + pixel wide line, but uses a slower more accurate algorithm for + diagonals. For horizontal and vertical lines a line width of 0 is + the same as a line width of 1. The cap and join style have no + effect on 0-width lines. + + \sa setWidth(), setStyle(), setColor() +*/ + +TQPen::TQPen( const TQColor &cl, uint w, PenStyle s, PenCapStyle c, + PenJoinStyle j ) +{ + init( cl, w, s | c | j ); +} + +/*! + Constructs a pen that is a copy of \a p. +*/ + +TQPen::TQPen( const TQPen &p ) +{ + data = p.data; + data->ref(); +} + +/*! + Destroys the pen. +*/ + +TQPen::~TQPen() +{ + if ( data->deref() ) + delete data; +} + + +/*! + Detaches from shared pen data to make sure that this pen is the + only one referring the data. + + If multiple pens share common data, this pen dereferences the data + and gets a copy of the data. Nothing is done if there is just a + single reference. +*/ + +void TQPen::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + + +/*! + Assigns \a p to this pen and returns a reference to this pen. +*/ + +TQPen &TQPen::operator=( const TQPen &p ) +{ + p.data->ref(); + if ( data->deref() ) + delete data; + data = p.data; + return *this; +} + + +/*! + Returns a \link shclass.html deep copy\endlink of the pen. +*/ + +TQPen TQPen::copy() const +{ + TQPen p( data->color, data->width, data->style, capStyle(), joinStyle() ); + return p; +} + + +/*! + \fn PenStyle TQPen::style() const + + Returns the pen style. + + \sa setStyle() +*/ + +/*! + Sets the pen style to \a s. + + See the \l TQt::PenStyle documentation for a list of all the + styles. + + \warning On Mac OS X the style setting (other than \c NoPen and \c + SolidLine) have no effect as they are not implemented by the + underlying system. + + \warning On Windows 95/98, the style setting (other than \c NoPen + and \c SolidLine) has no effect for lines with width greater than + 1. + + \sa style() +*/ + +void TQPen::setStyle( PenStyle s ) +{ + if ( data->style == s ) + return; + detach(); + data->style = s; + data->linest = (data->linest & ~MPenStyle) | s; +} + + +/*! + \fn uint TQPen::width() const + + Returns the pen width. + + \sa setWidth() +*/ + +/*! + Sets the pen width to \a w. + + A line width of 0 will produce a 1 pixel wide line using a fast + algorithm for diagonals. A line width of 1 will also produce a 1 + pixel wide line, but uses a slower more accurate algorithm for + diagonals. For horizontal and vertical lines a line width of 0 is + the same as a line width of 1. The cap and join style have no + effect on 0-width lines. + + \sa width() +*/ + +void TQPen::setWidth( uint w ) +{ + if ( data->width == w ) + return; + detach(); + data->width = w; +} + + +/*! + Returns the pen's cap style. + + \sa setCapStyle() +*/ +TQt::PenCapStyle TQPen::capStyle() const +{ + return (PenCapStyle)(data->linest & MPenCapStyle); +} + +/*! + Sets the pen's cap style to \a c. + + The default value is \c FlatCap. The cap style has no effect on + 0-width pens. + + \img pen-cap-styles.png Pen Cap Styles + + \warning On Windows 95/98 and Macintosh, the cap style setting has + no effect. Wide lines are rendered as if the cap style was \c + SquareCap. + + \sa capStyle() +*/ + +void TQPen::setCapStyle( PenCapStyle c ) +{ + if ( (data->linest & MPenCapStyle) == c ) + return; + detach(); + data->linest = (data->linest & ~MPenCapStyle) | c; +} + +/*! + Returns the pen's join style. + + \sa setJoinStyle() +*/ +TQt::PenJoinStyle TQPen::joinStyle() const +{ + return (PenJoinStyle)(data->linest & MPenJoinStyle); +} + +/*! + Sets the pen's join style to \a j. + + The default value is \c MiterJoin. The join style has no effect on + 0-width pens. + + \img pen-join-styles.png Pen Join Styles + + \warning On Windows 95/98 and Macintosh, the join style setting + has no effect. Wide lines are rendered as if the join style was \c + BevelJoin. + + \sa joinStyle() +*/ + +void TQPen::setJoinStyle( PenJoinStyle j ) +{ + if ( (data->linest & MPenJoinStyle) == j ) + return; + detach(); + data->linest = (data->linest & ~MPenJoinStyle) | j; +} + +/*! + \fn const TQColor &TQPen::color() const + + Returns the pen color. + + \sa setColor() +*/ + +/*! + Sets the pen color to \a c. + + \sa color() +*/ + +void TQPen::setColor( const TQColor &c ) +{ + detach(); + data->color = c; +} + + +/*! + \fn bool TQPen::operator!=( const TQPen &p ) const + + Returns TRUE if the pen is different from \a p; otherwise returns + FALSE. + + Two pens are different if they have different styles, widths or + colors. + + \sa operator==() +*/ + +/*! + Returns TRUE if the pen is equal to \a p; otherwise returns FALSE. + + Two pens are equal if they have equal styles, widths and colors. + + \sa operator!=() +*/ + +bool TQPen::operator==( const TQPen &p ) const +{ + return (p.data == data) || (p.data->linest == data->linest && + p.data->width == data->width && p.data->color == data->color); +} + + +/***************************************************************************** + TQPen stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates TQPen + + Writes the pen \a p to the stream \a s and returns a reference to + the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQPen &p ) +{ + // ### width() should not be restricted to 8-bit values + if ( s.version() < 3 ) + return s << (Q_UINT8)p.style() << (Q_UINT8)p.width() << p.color(); + else + return s << (Q_UINT8)( p.style() | p.capStyle() | p.joinStyle() ) + << (Q_UINT8)p.width() << p.color(); +} + +/*! + \relates TQPen + + Reads a pen from the stream \a s into \a p and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQPen &p ) +{ + Q_UINT8 style, width; + TQColor color; + s >> style; + s >> width; + s >> color; + p = TQPen( color, (uint)width, (TQt::PenStyle)style ); // owl + return s; +} +#endif //QT_NO_DATASTREAM + +/***************************************************************************** + TQBrush member functions + *****************************************************************************/ + +/*! + \class TQBrush qbrush.h + + \brief The TQBrush class defines the fill pattern of shapes drawn by a TQPainter. + + \ingroup graphics + \ingroup images + \ingroup shared + + A brush has a style and a color. One of the brush styles is a + custom pattern, which is defined by a TQPixmap. + + The brush style defines the fill pattern. The default brush style + is \c NoBrush (depending on how you construct a brush). This style + tells the painter to not fill shapes. The standard style for + filling is \c SolidPattern. + + The brush color defines the color of the fill pattern. The TQColor + documentation lists the predefined colors. + + Use the TQPen class for specifying line/outline styles. + + Example: + \code + TQPainter painter; + TQBrush brush( yellow ); // yellow solid pattern + painter.begin( &anyPaintDevice ); // paint something + painter.setBrush( brush ); // set the yellow brush + painter.setPen( NoPen ); // do not draw outline + painter.drawRect( 40,30, 200,100 ); // draw filled rectangle + painter.setBrush( NoBrush ); // do not fill + painter.setPen( black ); // set black pen, 0 pixel width + painter.drawRect( 10,10, 30,20 ); // draw rectangle outline + painter.end(); // painting done + \endcode + + See the setStyle() function for a complete list of brush styles. + + \img brush-styles.png Brush Styles + + \sa TQPainter, TQPainter::setBrush(), TQPainter::setBrushOrigin() +*/ + + +/*! + \internal + Initializes the brush. +*/ + +void TQBrush::init( const TQColor &color, BrushStyle style ) +{ + data = new TQBrushData; + Q_CHECK_PTR( data ); + data->style = style; + data->color = color; + data->pixmap = 0; +} + +/*! + Constructs a default black brush with the style \c NoBrush (will + not fill shapes). +*/ + +TQBrush::TQBrush() +{ + static TQBrushData* defBrushData = 0; + if ( !defBrushData ) { + static TQSharedCleanupHandler defBrushCleanup; + defBrushData = new TQBrushData; + defBrushData->style = NoBrush; + defBrushData->color = TQt::black; + defBrushData->pixmap = 0; + defBrushCleanup.set( &defBrushData ); + } + data = defBrushData; + data->ref(); +} + +/*! + Constructs a black brush with the style \a style. + + \sa setStyle() +*/ + +TQBrush::TQBrush( BrushStyle style ) +{ + init( TQt::black, style ); +} + +/*! + Constructs a brush with the color \a color and the style \a style. + + \sa setColor(), setStyle() +*/ + +TQBrush::TQBrush( const TQColor &color, BrushStyle style ) +{ + init( color, style ); +} + +/*! + Constructs a brush with the color \a color and a custom pattern + stored in \a pixmap. + + The color will only have an effect for monochrome pixmaps, i.e. + for TQPixmap::depth() == 1. + + Pixmap brushes are currently not supported when printing on X11. + + \sa setColor(), setPixmap() +*/ + +TQBrush::TQBrush( const TQColor &color, const TQPixmap &pixmap ) +{ + init( color, CustomPattern ); + setPixmap( pixmap ); +} + +/*! + Constructs a brush that is a \link shclass.html shallow + copy\endlink of \a b. +*/ + +TQBrush::TQBrush( const TQBrush &b ) +{ + data = b.data; + data->ref(); +} + +/*! + Destroys the brush. +*/ + +TQBrush::~TQBrush() +{ + if ( data->deref() ) { + delete data->pixmap; + delete data; + } +} + + +/*! + Detaches from shared brush data to make sure that this brush is + the only one referring the data. + + If multiple brushes share common data, this brush dereferences the + data and gets a copy of the data. Nothing is done if there is just + a single reference. +*/ + +void TQBrush::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + + +/*! + Assigns \a b to this brush and returns a reference to this brush. +*/ + +TQBrush &TQBrush::operator=( const TQBrush &b ) +{ + b.data->ref(); // beware of b = b + if ( data->deref() ) { + delete data->pixmap; + delete data; + } + data = b.data; + return *this; +} + + +/*! + Returns a \link shclass.html deep copy\endlink of the brush. +*/ + +TQBrush TQBrush::copy() const +{ + if ( data->style == CustomPattern ) { // brush has pixmap + TQBrush b( data->color, *data->pixmap ); + return b; + } else { // brush has std pattern + TQBrush b( data->color, data->style ); + return b; + } +} + + +/*! + \fn BrushStyle TQBrush::style() const + + Returns the brush style. + + \sa setStyle() +*/ + +/*! + Sets the brush style to \a s. + + The brush styles are: + \table + \header \i Pattern \i Meaning + \row \i NoBrush \i will not fill shapes (default). + \row \i SolidPattern \i solid (100%) fill pattern. + \row \i Dense1Pattern \i11 94% fill pattern. + \row \i Dense2Pattern \i11 88% fill pattern. + \row \i Dense3Pattern \i11 63% fill pattern. + \row \i Dense4Pattern \i11 50% fill pattern. + \row \i Dense5Pattern \i11 37% fill pattern. + \row \i Dense6Pattern \i11 12% fill pattern. + \row \i Dense7Pattern \i11 6% fill pattern. + \row \i HorPattern \i horizontal lines pattern. + \row \i VerPattern \i vertical lines pattern. + \row \i CrossPattern \i crossing lines pattern. + \row \i BDiagPattern \i diagonal lines (directed /) pattern. + \row \i FDiagPattern \i diagonal lines (directed \) pattern. + \row \i DiagCrossPattern \i diagonal crossing lines pattern. + \row \i CustomPattern \i set when a pixmap pattern is being used. + \endtable + + On Windows, dense and custom patterns cannot be transparent. + + See the \link #details Detailed Description\endlink for a picture + of all the styles. + + \sa style() +*/ + +void TQBrush::setStyle( BrushStyle s ) // set brush style +{ + if ( data->style == s ) + return; +#if defined(QT_CHECK_RANGE) + if ( s == CustomPattern ) + qWarning( "TQBrush::setStyle: CustomPattern is for internal use" ); +#endif + detach(); + data->style = s; +} + + +/*! + \fn const TQColor &TQBrush::color() const + + Returns the brush color. + + \sa setColor() +*/ + +/*! + Sets the brush color to \a c. + + \sa color(), setStyle() +*/ + +void TQBrush::setColor( const TQColor &c ) +{ + detach(); + data->color = c; +} + + +/*! + \fn TQPixmap *TQBrush::pixmap() const + + Returns a pointer to the custom brush pattern, or 0 if no custom + brush pattern has been set. + + \sa setPixmap() +*/ + +/*! + Sets the brush pixmap to \a pixmap. The style is set to \c + CustomPattern. + + The current brush color will only have an effect for monochrome + pixmaps, i.e. for TQPixmap::depth() == 1. + + Pixmap brushes are currently not supported when printing on X11. + + \sa pixmap(), color() +*/ + +void TQBrush::setPixmap( const TQPixmap &pixmap ) +{ + detach(); + if ( data->pixmap ) + delete data->pixmap; + if ( pixmap.isNull() ) { + data->style = NoBrush; + data->pixmap = 0; + } else { + data->style = CustomPattern; + data->pixmap = new TQPixmap( pixmap ); + if ( data->pixmap->optimization() == TQPixmap::MemoryOptim ) + data->pixmap->setOptimization( TQPixmap::NormalOptim ); + } +} + + +/*! + \fn bool TQBrush::operator!=( const TQBrush &b ) const + + Returns TRUE if the brush is different from \a b; otherwise + returns FALSE. + + Two brushes are different if they have different styles, colors or + pixmaps. + + \sa operator==() +*/ + +/*! + Returns TRUE if the brush is equal to \a b; otherwise returns + FALSE. + + Two brushes are equal if they have equal styles, colors and + pixmaps. + + \sa operator!=() +*/ + +bool TQBrush::operator==( const TQBrush &b ) const +{ + return (b.data == data) || (b.data->style == data->style && + b.data->color == data->color && + b.data->pixmap == data->pixmap); +} + + +/*! + \fn inline double TQPainter::translationX() const + \internal +*/ + +/*! + \fn inline double TQPainter::translationY() const + \internal +*/ + + +/***************************************************************************** + TQBrush stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates TQBrush + + Writes the brush \a b to the stream \a s and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQBrush &b ) +{ + s << (Q_UINT8)b.style() << b.color(); + if ( b.style() == TQt::CustomPattern ) +#ifndef QT_NO_IMAGEIO + s << *b.pixmap(); +#else + qWarning("No Image Brush I/O"); +#endif + return s; +} + +/*! + \relates TQBrush + + Reads the brush \a b from the stream \a s and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQBrush &b ) +{ + Q_UINT8 style; + TQColor color; + s >> style; + s >> color; + if ( style == TQt::CustomPattern ) { +#ifndef QT_NO_IMAGEIO + TQPixmap pm; + s >> pm; + b = TQBrush( color, pm ); +#else + qWarning("No Image Brush I/O"); +#endif + } + else + b = TQBrush( color, (TQt::BrushStyle)style ); + return s; +} +#endif // QT_NO_DATASTREAM diff --git a/src/kernel/qpainter.h b/src/kernel/qpainter.h new file mode 100644 index 000000000..58d3f930c --- /dev/null +++ b/src/kernel/qpainter.h @@ -0,0 +1,721 @@ +/**************************************************************************** +** +** Definition of TQPainter class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPAINTER_H +#define TQPAINTER_H + + +#ifndef QT_H +#include "qcolor.h" +#include "qfontmetrics.h" +#include "qfontinfo.h" +#include "qregion.h" +#include "qpen.h" +#include "qbrush.h" +#include "qpointarray.h" +#include "qwmatrix.h" +#endif // QT_H + +class TQGfx; +class TQTextCodec; +class TQTextParag; +class TQPaintDevice; +class TQTextItem; +#if defined( Q_WS_MAC ) +class TQMacSavedPortInfo; +#endif +class TQPainterPrivate; + +#if defined(Q_WS_QWS) +class TQScreen; +#endif + +class Q_EXPORT TQPainter : public TQt +{ +public: + enum CoordinateMode { CoordDevice, CoordPainter }; + + TQPainter(); + TQPainter( const TQPaintDevice *, bool unclipped = FALSE ); + TQPainter( const TQPaintDevice *, const TQWidget *, bool unclipped = FALSE ); + ~TQPainter(); + + bool begin( const TQPaintDevice *, bool unclipped = FALSE ); + bool begin( const TQPaintDevice *, const TQWidget *, bool unclipped = FALSE ); + bool end(); + TQPaintDevice *device() const; + +#ifdef Q_WS_QWS + TQGfx * internalGfx(); +#ifdef QT_QWS_EXPERIMENTAL_SCREENPAINTER + bool begin(TQScreen *screen); +#endif +#endif + + static void redirect( TQPaintDevice *pdev, TQPaintDevice *replacement ); + static TQPaintDevice *redirect( TQPaintDevice *pdev ); + + bool isActive() const; + + void flush( const TQRegion ®ion, CoordinateMode cm = CoordDevice ); + void flush(); + void save(); + void restore(); + + // Drawing tools + + TQFontMetrics fontMetrics() const; + TQFontInfo fontInfo() const; + + const TQFont &font() const; + void setFont( const TQFont & ); + const TQPen &pen() const; + void setPen( const TQPen & ); + void setPen( PenStyle ); + void setPen( const TQColor & ); + const TQBrush &brush() const; + void setBrush( const TQBrush & ); + void setBrush( BrushStyle ); + void setBrush( const TQColor & ); + TQPoint pos() const; + + // Drawing attributes/modes + + const TQColor &backgroundColor() const; + void setBackgroundColor( const TQColor & ); + BGMode backgroundMode() const; + void setBackgroundMode( BGMode ); + RasterOp rasterOp() const; + void setRasterOp( RasterOp ); + const TQPoint &brushOrigin() const; + void setBrushOrigin( int x, int y ); + void setBrushOrigin( const TQPoint & ); + + // Scaling and transformations + +// PaintUnit unit() const; // get set painter unit +// void setUnit( PaintUnit ); // NOT IMPLEMENTED!!! + + bool hasViewXForm() const; + bool hasWorldXForm() const; + +#ifndef QT_NO_TRANSFORMATIONS + void setViewXForm( bool ); // set xform on/off + TQRect window() const; // get window + void setWindow( const TQRect & ); // set window + void setWindow( int x, int y, int w, int h ); + TQRect viewport() const; // get viewport + void setViewport( const TQRect & ); // set viewport + void setViewport( int x, int y, int w, int h ); + + void setWorldXForm( bool ); // set world xform on/off + const TQWMatrix &worldMatrix() const; // get/set world xform matrix + void setWorldMatrix( const TQWMatrix &, bool combine=FALSE ); + + void saveWorldMatrix(); + void restoreWorldMatrix(); + + void scale( double sx, double sy ); + void shear( double sh, double sv ); + void rotate( double a ); +#endif + void translate( double dx, double dy ); + void resetXForm(); + double translationX() const; + double translationY() const; + + TQPoint xForm( const TQPoint & ) const; // map virtual -> device + TQRect xForm( const TQRect & ) const; + TQPointArray xForm( const TQPointArray & ) const; + TQPointArray xForm( const TQPointArray &, int index, int npoints ) const; + TQPoint xFormDev( const TQPoint & ) const; // map device -> virtual + TQRect xFormDev( const TQRect & ) const; + TQPointArray xFormDev( const TQPointArray & ) const; + TQPointArray xFormDev( const TQPointArray &, int index, int npoints ) const; + + // Clipping + + void setClipping( bool ); // set clipping on/off + bool hasClipping() const; + TQRegion clipRegion( CoordinateMode = CoordDevice ) const; + void setClipRect( const TQRect &, CoordinateMode = CoordDevice ); // set clip rectangle + void setClipRect( int x, int y, int w, int h, CoordinateMode = CoordDevice ); + void setClipRegion( const TQRegion &, CoordinateMode = CoordDevice );// set clip region + + // Graphics drawing functions + + void drawPoint( int x, int y ); + void drawPoint( const TQPoint & ); + void drawPoints( const TQPointArray& a, + int index=0, int npoints=-1 ); + void moveTo( int x, int y ); + void moveTo( const TQPoint & ); + void lineTo( int x, int y ); + void lineTo( const TQPoint & ); + void drawLine( int x1, int y1, int x2, int y2 ); + void drawLine( const TQPoint &, const TQPoint & ); + void drawRect( int x, int y, int w, int h ); + void drawRect( const TQRect & ); + void drawWinFocusRect( int x, int y, int w, int h ); + void drawWinFocusRect( int x, int y, int w, int h, + const TQColor &bgColor ); + void drawWinFocusRect( const TQRect & ); + void drawWinFocusRect( const TQRect &, + const TQColor &bgColor ); + void drawRoundRect( int x, int y, int w, int h, int = 25, int = 25 ); + void drawRoundRect( const TQRect &, int = 25, int = 25 ); + void drawEllipse( int x, int y, int w, int h ); + void drawEllipse( const TQRect & ); + void drawArc( int x, int y, int w, int h, int a, int alen ); + void drawArc( const TQRect &, int a, int alen ); + void drawPie( int x, int y, int w, int h, int a, int alen ); + void drawPie( const TQRect &, int a, int alen ); + void drawChord( int x, int y, int w, int h, int a, int alen ); + void drawChord( const TQRect &, int a, int alen ); + void drawLineSegments( const TQPointArray &, + int index=0, int nlines=-1 ); + void drawPolyline( const TQPointArray &, + int index=0, int npoints=-1 ); + void drawPolygon( const TQPointArray &, bool winding=FALSE, + int index=0, int npoints=-1 ); + void drawConvexPolygon( const TQPointArray &, + int index=0, int npoints=-1 ); +#ifndef QT_NO_BEZIER + void drawCubicBezier( const TQPointArray &, int index=0 ); +#endif + void drawPixmap( int x, int y, const TQPixmap &, + int sx=0, int sy=0, int sw=-1, int sh=-1 ); + void drawPixmap( const TQPoint &, const TQPixmap &, + const TQRect &sr ); + void drawPixmap( const TQPoint &, const TQPixmap & ); + void drawPixmap( const TQRect &, const TQPixmap & ); + void drawImage( int x, int y, const TQImage &, + int sx = 0, int sy = 0, int sw = -1, int sh = -1, + int conversionFlags = 0 ); + void drawImage( const TQPoint &, const TQImage &, + const TQRect &sr, int conversionFlags = 0 ); + void drawImage( const TQPoint &, const TQImage &, + int conversion_flags = 0 ); + void drawImage( const TQRect &, const TQImage & ); + void drawTiledPixmap( int x, int y, int w, int h, const TQPixmap &, + int sx=0, int sy=0 ); + void drawTiledPixmap( const TQRect &, const TQPixmap &, + const TQPoint & ); + void drawTiledPixmap( const TQRect &, const TQPixmap & ); +#ifndef QT_NO_PICTURE + void drawPicture( const TQPicture & ); + void drawPicture( int x, int y, const TQPicture & ); + void drawPicture( const TQPoint &, const TQPicture & ); +#endif + + void fillRect( int x, int y, int w, int h, const TQBrush & ); + void fillRect( const TQRect &, const TQBrush & ); + void eraseRect( int x, int y, int w, int h ); + void eraseRect( const TQRect & ); + + // Text drawing functions + + enum TextDirection { + Auto, + RTL, + LTR + }; + + void drawText( int x, int y, const TQString &, int len = -1, TextDirection dir = Auto ); + void drawText( const TQPoint &, const TQString &, int len = -1, TextDirection dir = Auto ); + + void drawText( int x, int y, const TQString &, int pos, int len, TextDirection dir = Auto ); + void drawText( const TQPoint &p, const TQString &, int pos, int len, TextDirection dir = Auto ); + + void drawText( int x, int y, int w, int h, int flags, + const TQString&, int len = -1, TQRect *br=0, + TQTextParag **intern=0 ); + void drawText( const TQRect &, int flags, + const TQString&, int len = -1, TQRect *br=0, + TQTextParag **intern=0 ); + + void drawTextItem( int x, int y, const TQTextItem &ti, int textflags = 0 ); + void drawTextItem( const TQPoint& p, const TQTextItem &ti, int textflags = 0 ); + + TQRect boundingRect( int x, int y, int w, int h, int flags, + const TQString&, int len = -1, TQTextParag **intern=0 ); + TQRect boundingRect( const TQRect &, int flags, + const TQString&, int len = -1, TQTextParag **intern=0 ); + + int tabStops() const; + void setTabStops( int ); + int *tabArray() const; + void setTabArray( int * ); + + // Other functions + +#if defined(Q_WS_WIN) + HDC handle() const; +#elif defined(Q_WS_X11) || defined(Q_WS_MAC) + HANDLE handle() const; +#endif + + + static void initialize(); + static void cleanup(); + +private: + void init(); + void destroy(); + void updateFont(); + void updatePen(); + void updateBrush(); +#ifndef QT_NO_TRANSFORMATIONS + void updateXForm(); + void updateInvXForm(); +#endif + void map( int, int, int *rx, int *ry ) const; + void map( int, int, int, int, int *, int *, int *, int * ) const; + void mapInv( int, int, int *, int * ) const; + void mapInv( int, int, int, int, int *, int *, int *, int * ) const; + void drawPolyInternal( const TQPointArray &, bool close=TRUE ); + void drawWinFocusRect( int x, int y, int w, int h, bool xorPaint, + const TQColor &penColor ); + + enum { IsActive=0x01, ExtDev=0x02, IsStartingUp=0x04, NoCache=0x08, + VxF=0x10, WxF=0x20, ClipOn=0x40, SafePolygon=0x80, MonoDev=0x100, + DirtyFont=0x200, DirtyPen=0x400, DirtyBrush=0x800, + RGBColor=0x1000, FontMet=0x2000, FontInf=0x4000, CtorBegin=0x8000, + UsePrivateCx = 0x10000, VolatileDC = 0x20000, TQt2Compat = 0x40000 }; + uint flags; + bool testf( uint b ) const { return (flags&b)!=0; } + void setf( uint b ) { flags |= b; } + void setf( uint b, bool v ); + void clearf( uint b ) { flags &= (uint)(~b); } + void fix_neg_rect( int *x, int *y, int *w, int *h ); + + TQPainterPrivate *d; + TQPaintDevice *pdev; + TQColor bg_col; + uchar bg_mode; + uchar rop; + uchar pu; + TQPoint bro; + TQFont cfont; + TQFont *pfont; // font used for metrics (might be different for printers) + TQPen cpen; + TQBrush cbrush; + TQRegion crgn; + int tabstops; + int *tabarray; + int tabarraylen; + bool block_ext; // for temporary blocking of external devices + + // Transformations +#ifndef QT_NO_TRANSFORMATIONS + TQCOORD wx, wy, ww, wh; + TQCOORD vx, vy, vw, vh; + TQWMatrix wxmat; + + // Cached composition (and inverse) of transformations + TQWMatrix xmat; + TQWMatrix ixmat; + + + + double m11() const { return xmat.m11(); } + double m12() const { return xmat.m12(); } + double m21() const { return xmat.m21(); } + double m22() const { return xmat.m22(); } + double dx() const { return xmat.dx(); } + double dy() const { return xmat.dy(); } + double im11() const { return ixmat.m11(); } + double im12() const { return ixmat.m12(); } + double im21() const { return ixmat.m21(); } + double im22() const { return ixmat.m22(); } + double idx() const { return ixmat.dx(); } + double idy() const { return ixmat.dy(); } + + int txop; + bool txinv; + +#else + // even without transformations we still have translations + int xlatex; + int xlatey; +#endif + + void *penRef; // pen cache ref + void *brushRef; // brush cache ref + void *ps_stack; + void *wm_stack; + void killPStack(); + +protected: +#ifdef Q_OS_TEMP + TQPoint internalCurrentPos; + uint old_pix; // ### All win platforms in 4.0 +#endif +#if defined(Q_WS_WIN) + friend class TQFontEngineWin; + friend class TQFontEngineBox; + QT_WIN_PAINTER_MEMBERS +#elif defined(Q_WS_X11) + friend class TQFontEngineXLFD; + friend class TQFontEngineXft; + friend class TQFontEngineBox; + Display *dpy; // current display + int scrn; // current screen + TQt::HANDLE hd; // handle to drawable + TQt::HANDLE rendhd; // handle to Xft draw + GC gc; // graphics context (standard) + GC gc_brush; // graphics contect for brush + TQPoint curPt; // current point + uint clip_serial; // clipping serial number +#elif defined(Q_WS_MAC) + TQt::HANDLE hd; // handle to drawable + void initPaintDevice(bool force=FALSE, TQPoint *off=NULL, TQRegion *rgn=NULL); + friend const TQRegion &qt_mac_update_painter(TQPainter *, bool); + friend class TQFontEngineMac; + friend class TQMacPainter; +#elif defined(Q_WS_QWS) + friend class TQFontEngine; + TQGfx * gfx; + friend void qwsUpdateActivePainters(); +#endif + friend class TQFontMetrics; + friend class TQFontInfo; + friend class TQTextLayout; + friend void qt_format_text( const TQFont &, const TQRect &r, + int tf, const TQString& str, int len, TQRect *brect, + int tabstops, int* tabarray, int tabarraylen, + TQTextParag **internal, TQPainter* painter ); + friend void qt_draw_background( TQPainter *p, int x, int y, int w, int h ); + friend void qt_draw_transformed_rect( TQPainter *p, int x, int y, int w, int h, bool fill ); + friend class TQPrinter; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQPainter( const TQPainter & ); + TQPainter &operator=( const TQPainter & ); +#endif + + enum TransformationCodes { + TxNone = 0, // transformation codes + TxTranslate = 1, // copy in qpainter_*.cpp + TxScale = 2, + TxRotShear = 3 + }; +}; + + +/***************************************************************************** + TQPainter member functions + *****************************************************************************/ + +inline TQPaintDevice *TQPainter::device() const +{ + return pdev; +} + +inline bool TQPainter::isActive() const +{ + return testf(IsActive); +} + +inline const TQFont &TQPainter::font() const +{ + return cfont; +} + +inline const TQPen &TQPainter::pen() const +{ + return cpen; +} + +inline const TQBrush &TQPainter::brush() const +{ + return cbrush; +} + +/* +inline PaintUnit TQPainter::unit() const +{ + return (PaintUnit)pu; +} +*/ + +inline const TQColor &TQPainter::backgroundColor() const +{ + return bg_col; +} + +inline TQt::BGMode TQPainter::backgroundMode() const +{ + return (BGMode)bg_mode; +} + +inline TQt::RasterOp TQPainter::rasterOp() const +{ + return (RasterOp)rop; +} + +inline const TQPoint &TQPainter::brushOrigin() const +{ + return bro; +} + +inline bool TQPainter::hasViewXForm() const +{ +#ifndef QT_NO_TRANSFORMATIONS + return testf(VxF); +#else + return xlatex || xlatey; +#endif +} + +inline bool TQPainter::hasWorldXForm() const +{ +#ifndef QT_NO_TRANSFORMATIONS + return testf(WxF); +#else + return xlatex || xlatey; +#endif +} + +inline double TQPainter::translationX() const +{ +#ifndef QT_NO_TRANSFORMATIONS + return worldMatrix().dx(); +#else + return xlatex; +#endif +} + +inline double TQPainter::translationY() const +{ +#ifndef QT_NO_TRANSFORMATIONS + return worldMatrix().dy(); +#else + return xlatey; +#endif +} + + +inline bool TQPainter::hasClipping() const +{ + return testf(ClipOn); +} + +inline int TQPainter::tabStops() const +{ + return tabstops; +} + +inline int *TQPainter::tabArray() const +{ + return tabarray; +} + +#if defined(Q_WS_WIN) +inline HDC TQPainter::handle() const +{ + return hdc; +} +#elif defined(Q_WS_X11) || defined(Q_WS_MAC) +inline TQt::HANDLE TQPainter::handle() const +{ + return hd; +} +#endif + +inline void TQPainter::setBrushOrigin( const TQPoint &p ) +{ + setBrushOrigin( p.x(), p.y() ); +} + +#ifndef QT_NO_TRANSFORMATIONS +inline void TQPainter::setWindow( const TQRect &r ) +{ + setWindow( r.x(), r.y(), r.width(), r.height() ); +} + +inline void TQPainter::setViewport( const TQRect &r ) +{ + setViewport( r.x(), r.y(), r.width(), r.height() ); +} +#endif + +inline void TQPainter::setClipRect( int x, int y, int w, int h, CoordinateMode m ) +{ + setClipRect( TQRect(x,y,w,h), m ); +} + +inline void TQPainter::drawPoint( const TQPoint &p ) +{ + drawPoint( p.x(), p.y() ); +} + +inline void TQPainter::moveTo( const TQPoint &p ) +{ + moveTo( p.x(), p.y() ); +} + +inline void TQPainter::lineTo( const TQPoint &p ) +{ + lineTo( p.x(), p.y() ); +} + +inline void TQPainter::drawLine( const TQPoint &p1, const TQPoint &p2 ) +{ + drawLine( p1.x(), p1.y(), p2.x(), p2.y() ); +} + +inline void TQPainter::drawRect( const TQRect &r ) +{ + drawRect( r.x(), r.y(), r.width(), r.height() ); +} + +inline void TQPainter::drawWinFocusRect( const TQRect &r ) +{ + drawWinFocusRect( r.x(), r.y(), r.width(), r.height() ); +} + +inline void TQPainter::drawWinFocusRect( const TQRect &r,const TQColor &penColor ) +{ + drawWinFocusRect( r.x(), r.y(), r.width(), r.height(), penColor ); +} + +inline void TQPainter::drawRoundRect( const TQRect &r, int xRnd, int yRnd ) +{ + drawRoundRect( r.x(), r.y(), r.width(), r.height(), xRnd, yRnd ); +} + +inline void TQPainter::drawEllipse( const TQRect &r ) +{ + drawEllipse( r.x(), r.y(), r.width(), r.height() ); +} + +inline void TQPainter::drawArc( const TQRect &r, int a, int alen ) +{ + drawArc( r.x(), r.y(), r.width(), r.height(), a, alen ); +} + +inline void TQPainter::drawPie( const TQRect &r, int a, int alen ) +{ + drawPie( r.x(), r.y(), r.width(), r.height(), a, alen ); +} + +inline void TQPainter::drawChord( const TQRect &r, int a, int alen ) +{ + drawChord( r.x(), r.y(), r.width(), r.height(), a, alen ); +} + +inline void TQPainter::drawPixmap( const TQPoint &p, const TQPixmap &pm, + const TQRect &sr ) +{ + drawPixmap( p.x(), p.y(), pm, sr.x(), sr.y(), sr.width(), sr.height() ); +} + +inline void TQPainter::drawImage( const TQPoint &p, const TQImage &pm, + const TQRect &sr, int conversionFlags ) +{ + drawImage( p.x(), p.y(), pm, + sr.x(), sr.y(), sr.width(), sr.height(), conversionFlags ); +} + +inline void TQPainter::drawTiledPixmap( const TQRect &r, const TQPixmap &pm, + const TQPoint &sp ) +{ + drawTiledPixmap( r.x(), r.y(), r.width(), r.height(), pm, sp.x(), sp.y() ); +} + +inline void TQPainter::drawTiledPixmap( const TQRect &r, const TQPixmap &pm ) +{ + drawTiledPixmap( r.x(), r.y(), r.width(), r.height(), pm, 0, 0 ); +} + +inline void TQPainter::fillRect( const TQRect &r, const TQBrush &brush ) +{ + fillRect( r.x(), r.y(), r.width(), r.height(), brush ); +} + +inline void TQPainter::eraseRect( int x, int y, int w, int h ) +{ + fillRect( x, y, w, h, backgroundColor() ); +} + +inline void TQPainter::eraseRect( const TQRect &r ) +{ + fillRect( r.x(), r.y(), r.width(), r.height(), backgroundColor() ); +} + +inline void TQPainter::drawText( const TQPoint &p, const TQString &s, int len, TextDirection dir ) +{ + drawText( p.x(), p.y(), s, 0, len, dir ); +} + +inline void TQPainter::drawText( const TQPoint &p, const TQString &s, int pos, int len, TextDirection dir ) +{ + drawText( p.x(), p.y(), s, pos, len, dir ); +} + +inline void TQPainter::drawText( int x, int y, int w, int h, int tf, + const TQString& str, int len, TQRect *br, TQTextParag **i ) +{ + TQRect r(x, y, w, h); + drawText( r, tf, str, len, br, i ); +} + +inline void TQPainter::drawTextItem( const TQPoint& p, const TQTextItem &ti, int textflags ) +{ + drawTextItem( p.x(), p.y(), ti, textflags ); +} + +inline TQRect TQPainter::boundingRect( int x, int y, int w, int h, int tf, + const TQString& str, int len, TQTextParag **i ) +{ + TQRect r(x, y, w, h); + return boundingRect( r, tf, str, len, i ); +} + +#if defined(Q_WS_QWS) +inline TQGfx * TQPainter::internalGfx() +{ + return gfx; +} +#endif + +#endif // TQPAINTER_H diff --git a/src/kernel/qpainter_p.h b/src/kernel/qpainter_p.h new file mode 100644 index 000000000..d61c46eb3 --- /dev/null +++ b/src/kernel/qpainter_p.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Definition of some TQt private functions. +** +** Created : 000909 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPAINTER_P_H +#define TQPAINTER_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of qpainter.cpp and qfont.cpp. This header file may change +// from version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#endif // QT_H + +extern void qt_format_text( const TQFont& f, const TQRect &r, + int tf, const TQString& str, int len, TQRect *brect, + int tabstops, int* tabarray, int tabarraylen, + TQTextParag **internal, TQPainter* painter ); + + +#endif diff --git a/src/kernel/qpainter_x11.cpp b/src/kernel/qpainter_x11.cpp new file mode 100644 index 000000000..ef80cff3a --- /dev/null +++ b/src/kernel/qpainter_x11.cpp @@ -0,0 +1,3153 @@ +/**************************************************************************** +** +** Implementation of TQPainter class for X11 +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qplatformdefs.h" + +#include "qfont.h" +#include "qpainter.h" +#include "qwidget.h" +#include "qbitmap.h" +#include "qpixmapcache.h" +#include "qtextcodec.h" +#include "qpaintdevicemetrics.h" + +#include "qt_x11_p.h" + +#include "qtextlayout_p.h" +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include "qtextengine_p.h" + +#include + +// paintevent magic to provide Windows semantics on X11 +static TQRegion* paintEventClipRegion = 0; +static TQPaintDevice* paintEventDevice = 0; + +void qt_set_paintevent_clipping( TQPaintDevice* dev, const TQRegion& region) +{ + if ( !paintEventClipRegion ) + paintEventClipRegion = new TQRegion( region ); + else + *paintEventClipRegion = region; + paintEventDevice = dev; +} + +void qt_clear_paintevent_clipping() +{ + delete paintEventClipRegion; + paintEventClipRegion = 0; + paintEventDevice = 0; +} + +class TQWFlagWidget : public TQWidget +{ +public: + void setWState( WFlags f ) { TQWidget::setWState(f); } + void clearWState( WFlags f ) { TQWidget::clearWState(f); } + void setWFlags( WFlags f ) { TQWidget::setWFlags(f); } + void clearWFlags( WFlags f ) { TQWidget::clearWFlags(f); } +}; + +void qt_erase_region( TQWidget* w, const TQRegion& region) +{ + TQRegion reg = region; + + if ( TQPainter::redirect(w) || (!w->isTopLevel() && w->backgroundPixmap() + && w->backgroundOrigin() != TQWidget::WidgetOrigin) ) { + TQPoint offset = w->backgroundOffset(); + int ox = offset.x(); + int oy = offset.y(); + + bool unclipped = w->testWFlags( TQt::WPaintUnclipped ); + if ( unclipped ) + ((TQWFlagWidget*)w)->clearWFlags( TQt::WPaintUnclipped ); + TQPainter p( w ); + p.setClipRegion( region ); // automatically includes paintEventDevice if retquired + if ( w->backgroundPixmap() ) + p.drawTiledPixmap( 0, 0, w->width(), w->height(), + *w->backgroundPixmap(), ox, oy ); + else + p.fillRect( w->rect(), w->eraseColor() ); + if ( unclipped ) + ((TQWFlagWidget*)w)->setWFlags( TQt::WPaintUnclipped ); + return; + } + + if ( w == paintEventDevice && paintEventClipRegion ) + reg = paintEventClipRegion->intersect( reg ); + + TQMemArray r = reg.rects(); + for (uint i=0; ix11Display(), w->winId(), + rr.x(), rr.y(), rr.width(), rr.height(), False ); + } +} + +void qt_erase_rect( TQWidget* w, const TQRect& r) +{ + if ( TQPainter::redirect(w) || w == paintEventDevice + || w->backgroundOrigin() != TQWidget::WidgetOrigin ) + qt_erase_region( w, r ); + else + XClearArea( w->x11Display(), w->winId(), r.x(), r.y(), r.width(), r.height(), False ); + +} + +#ifdef QT_NO_XFTFREETYPE +static const TQt::HANDLE rendhd = 0; +#endif + +// hack, so we don't have to make TQRegion::clipRectangles() public or include +// X11 headers in qregion.h +inline void *qt_getClipRects( const TQRegion &r, int &num ) +{ + return r.clipRectangles( num ); +} + +static inline void x11SetClipRegion(Display *dpy, GC gc, GC gc2, TQt::HANDLE draw, const TQRegion &r) +{ + int num; + XRectangle *rects = (XRectangle *)qt_getClipRects( r, num ); + + if (gc) + XSetClipRectangles( dpy, gc, 0, 0, rects, num, YXBanded ); + if (gc2) + XSetClipRectangles( dpy, gc2, 0, 0, rects, num, YXBanded ); + +#ifndef QT_NO_XFTFREETYPE + if (draw) + XftDrawSetClipRectangles((XftDraw *) draw, 0, 0, rects, num); +#else + Q_UNUSED(draw); +#endif // QT_NO_XFTFREETYPE +} + +static inline void x11ClearClipRegion(Display *dpy, GC gc, GC gc2, TQt::HANDLE draw) +{ + if (gc) + XSetClipMask(dpy, gc, None); + if (gc2) + XSetClipMask(dpy, gc2, None); + +#ifndef QT_NO_XFTFREETYPE + if (draw) { +# ifdef QT_XFT2 + XftDrawSetClip((XftDraw *) draw, None); +# else + // stupid Xft1 + Picture pict = XftDrawPicture((XftDraw *) draw); + XRenderPictureAttributes pattr; + pattr.clip_mask = None; + XRenderChangePicture(dpy, pict, CPClipMask, &pattr); +# endif // QT_XFT2 + } +#else + Q_UNUSED(draw); +#endif // QT_NO_XFTFREETYPE +} + + +/***************************************************************************** + Trigonometric function for TQPainter + + We have implemented simple sine and cosine function that are called from + TQPainter::drawPie() and TQPainter::drawChord() when drawing the outline of + pies and chords. + These functions are slower and less accurate than math.h sin() and cos(), + but with still around 1/70000th sec. execution time (on a 486DX2-66) and + 8 digits accuracy, it should not be the bottleneck in drawing these shapes. + The advantage is that you don't have to link in the math library. + *****************************************************************************/ + +const double Q_PI = 3.14159265358979323846; // pi +const double Q_2PI = 6.28318530717958647693; // 2*pi +const double Q_PI2 = 1.57079632679489661923; // pi/2 + + +#if defined(Q_CC_GNU) && defined(Q_OS_AIX) +// AIX 4.2 gcc 2.7.2.3 gets internal error. +static int qRoundAIX( double d ) +{ + return qRound(d); +} +#define qRound qRoundAIX +#endif + + +#if defined(Q_CC_GNU) && defined(__i386__) + +inline double qcos( double a ) +{ + double r; + __asm__ ( + "fcos" + : "=t" (r) : "0" (a) ); + return(r); +} + +inline double qsin( double a ) +{ + double r; + __asm__ ( + "fsin" + : "=t" (r) : "0" (a) ); + return(r); +} + +double qsincos( double a, bool calcCos=FALSE ) +{ + return calcCos ? qcos(a) : qsin(a); +} + +#else + +double qsincos( double a, bool calcCos=FALSE ) +{ + if ( calcCos ) // calculate cosine + a -= Q_PI2; + if ( a >= Q_2PI || a <= -Q_2PI ) { // fix range: -2*pi < a < 2*pi + int m = (int)(a/Q_2PI); + a -= Q_2PI*m; + } + if ( a < 0.0 ) // 0 <= a < 2*pi + a += Q_2PI; + int sign = a > Q_PI ? -1 : 1; + if ( a >= Q_PI ) + a = Q_2PI - a; + if ( a >= Q_PI2 ) + a = Q_PI - a; + if ( calcCos ) + sign = -sign; + double a2 = a*a; // here: 0 <= a < pi/4 + double a3 = a2*a; // make taylor sin sum + double a5 = a3*a2; + double a7 = a5*a2; + double a9 = a7*a2; + double a11 = a9*a2; + return (a-a3/6+a5/120-a7/5040+a9/362880-a11/39916800)*sign; +} + +inline double qsin( double a ) { return qsincos(a, FALSE); } +inline double qcos( double a ) { return qsincos(a, TRUE); } + +#endif + + +/***************************************************************************** + TQPainter internal GC (Graphics Context) allocator. + + The GC allocator offers two functions; alloc_gc() and free_gc() that + reuse GC objects instead of calling XCreateGC() and XFreeGC(), which + are a whole lot slower. + *****************************************************************************/ + +struct TQGC +{ + GC gc; + char in_use; + bool mono; + int scrn; +}; + +const int gc_array_size = 256; +static TQGC gc_array[gc_array_size]; // array of GCs +static bool gc_array_init = FALSE; + + +static void init_gc_array() +{ + if ( !gc_array_init ) { + memset( gc_array, 0, gc_array_size*sizeof(TQGC) ); + gc_array_init = TRUE; + } +} + +static void cleanup_gc_array( Display *dpy ) +{ + register TQGC *p = gc_array; + int i = gc_array_size; + if ( gc_array_init ) { + while ( i-- ) { + if ( p->gc ) // destroy GC + XFreeGC( dpy, p->gc ); + p++; + } + gc_array_init = FALSE; + } +} + +// #define DONT_USE_GC_ARRAY + +static GC alloc_gc( Display *dpy, int scrn, Drawable hd, bool monochrome=FALSE, + bool privateGC = FALSE ) +{ +#if defined(DONT_USE_GC_ARRAY) + privateGC = TRUE; // will be slower +#endif + if ( privateGC ) { + GC gc = XCreateGC( dpy, hd, 0, 0 ); + XSetGraphicsExposures( dpy, gc, False ); + return gc; + } + register TQGC *p = gc_array; + int i = gc_array_size; + if ( !gc_array_init ) // not initialized + init_gc_array(); + while ( i-- ) { + if ( !p->gc ) { // create GC (once) + p->gc = XCreateGC( dpy, hd, 0, 0 ); + p->scrn = scrn; + XSetGraphicsExposures( dpy, p->gc, False ); + p->in_use = FALSE; + p->mono = monochrome; + } + if ( !p->in_use && p->mono == monochrome && p->scrn == scrn ) { + p->in_use = TRUE; // available/compatible GC + return p->gc; + } + p++; + } +#if defined(QT_CHECK_NULL) + qWarning( "TQPainter: Internal error; no available GC" ); +#endif + GC gc = XCreateGC( dpy, hd, 0, 0 ); + XSetGraphicsExposures( dpy, gc, False ); + return gc; +} + +static void free_gc( Display *dpy, GC gc, bool privateGC = FALSE ) +{ +#if defined(DONT_USE_GC_ARRAY) + privateGC = TRUE; // will be slower +#endif + if ( privateGC ) { + Q_ASSERT( dpy != 0 ); + XFreeGC( dpy, gc ); + return; + } + register TQGC *p = gc_array; + int i = gc_array_size; + if ( gc_array_init ) { + while ( i-- ) { + if ( p->gc == gc ) { + p->in_use = FALSE; // set available + XSetClipMask( dpy, gc, None ); // make it reusable + XSetFunction( dpy, gc, GXcopy ); + XSetFillStyle( dpy, gc, FillSolid ); + XSetTSOrigin( dpy, gc, 0, 0 ); + return; + } + p++; + } + } + + // not found in gc_array + XFreeGC(dpy, gc); +} + + +/***************************************************************************** + TQPainter internal GC (Graphics Context) cache for solid pens and + brushes. + + The GC cache makes a significant contribution to speeding up + drawing. Setting new pen and brush colors will make the painter + look for another GC with the same color instead of changing the + color value of the GC currently in use. The cache structure is + optimized for fast lookup. Only solid line pens with line width 0 + and solid brushes are cached. + + In addition, stored GCs may have an implicit clipping region + set. This prevents any drawing outside paint events. Both + updatePen() and updateBrush() keep track of the validity of this + clipping region by storing the clip_serial number in the cache. + +*****************************************************************************/ + +struct TQGCC // cached GC +{ + GC gc; + uint pix; + int count; + int hits; + uint clip_serial; + int scrn; +}; + +const int gc_cache_size = 29; // multiply by 4 +static TQGCC *gc_cache_buf; +static TQGCC *gc_cache[4*gc_cache_size]; +static bool gc_cache_init = FALSE; +static uint gc_cache_clip_serial = 0; + + +static void init_gc_cache() +{ + if ( !gc_cache_init ) { + gc_cache_init = TRUE; + gc_cache_clip_serial = 0; + TQGCC *g = gc_cache_buf = new TQGCC[4*gc_cache_size]; + memset( g, 0, 4*gc_cache_size*sizeof(TQGCC) ); + for ( int i=0; i<4*gc_cache_size; i++ ) + gc_cache[i] = g++; + } +} + + +// #define GC_CACHE_STAT +#if defined(GC_CACHE_STAT) +#include "qtextstream.h" +#include "qbuffer.h" + +static int g_numhits = 0; +static int g_numcreates = 0; +static int g_numfaults = 0; +#endif + + +static void cleanup_gc_cache() +{ + if ( !gc_cache_init ) + return; +#if defined(GC_CACHE_STAT) + qDebug( "Number of cache hits = %d", g_numhits ); + qDebug( "Number of cache creates = %d", g_numcreates ); + qDebug( "Number of cache faults = %d", g_numfaults ); + for ( int i=0; igc ? 'X' : '-') << ',' << g->hits << ',' + << g->count << '\t'; + } + s << '\0'; + qDebug( str ); + buf.close(); + } +#endif + delete [] gc_cache_buf; + gc_cache_init = FALSE; +} + + +static bool obtain_gc( void **ref, GC *gc, uint pix, Display *dpy, int scrn, + TQt::HANDLE hd, uint painter_clip_serial ) +{ + if ( !gc_cache_init ) + init_gc_cache(); + + int k = (pix % gc_cache_size) * 4; + TQGCC *g = gc_cache[k]; + TQGCC *prev = 0; + +#define NOMATCH (g->gc && (g->pix != pix || g->scrn != scrn || \ + (g->clip_serial > 0 && g->clip_serial != painter_clip_serial))) + + if ( NOMATCH ) { + prev = g; + g = gc_cache[++k]; + if ( NOMATCH ) { + prev = g; + g = gc_cache[++k]; + if ( NOMATCH ) { + prev = g; + g = gc_cache[++k]; + if ( NOMATCH ) { + if ( g->count == 0 && g->scrn == scrn) { // steal this GC + g->pix = pix; + g->count = 1; + g->hits = 1; + g->clip_serial = 0; + XSetForeground( dpy, g->gc, pix ); + XSetClipMask(dpy, g->gc, None); + gc_cache[k] = prev; + gc_cache[k-1] = g; + *ref = (void *)g; + *gc = g->gc; + return TRUE; + } else { // all GCs in use +#if defined(GC_CACHE_STAT) + g_numfaults++; +#endif + *ref = 0; + return FALSE; + } + } + } + } + } + +#undef NOMATCH + + *ref = (void *)g; + + if ( g->gc ) { // reuse existing GC +#if defined(GC_CACHE_STAT) + g_numhits++; +#endif + *gc = g->gc; + g->count++; + g->hits++; + if ( prev && g->hits > prev->hits ) { // maintain LRU order + gc_cache[k] = prev; + gc_cache[k-1] = g; + } + return TRUE; + } else { // create new GC +#if defined(GC_CACHE_STAT) + g_numcreates++; +#endif + g->gc = alloc_gc( dpy, scrn, hd, FALSE ); + g->scrn = scrn; + g->pix = pix; + g->count = 1; + g->hits = 1; + g->clip_serial = 0; + *gc = g->gc; + return FALSE; + } +} + +static inline void release_gc( void *ref ) +{ + ((TQGCC*)ref)->count--; +} + +/***************************************************************************** + TQPainter member functions + *****************************************************************************/ + +/*! + \internal + + Internal function that initializes the painter. +*/ + +void TQPainter::initialize() +{ + init_gc_array(); + init_gc_cache(); +} + +/*! + \internal + + Internal function that cleans up the painter. +*/ + +void TQPainter::cleanup() +{ + cleanup_gc_cache(); + cleanup_gc_array( TQPaintDevice::x11AppDisplay() ); + TQPointArray::cleanBuffers(); +} + +/*! + \internal + + Internal function that destroys up the painter. +*/ + +void TQPainter::destroy() +{ + +} + +void TQPainter::init() +{ + d = 0; + flags = IsStartingUp; + bg_col = white; // default background color + bg_mode = TransparentMode; // default background mode + rop = CopyROP; // default ROP + tabstops = 0; // default tabbing + tabarray = 0; + tabarraylen = 0; + ps_stack = 0; + wm_stack = 0; + gc = gc_brush = 0; + pdev = 0; + dpy = 0; + txop = txinv = 0; + penRef = brushRef = 0; + clip_serial = 0; + pfont = 0; + block_ext = FALSE; +} + + +/*! + \fn const TQFont &TQPainter::font() const + + Returns the currently set painter font. + + \sa setFont(), TQFont +*/ + +/*! + Sets the painter's font to \a font. + + This font is used by subsequent drawText() functions. The text + color is the same as the pen color. + + \sa font(), drawText() +*/ + +void TQPainter::setFont( const TQFont &font ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setFont: Will be reset by begin()" ); +#endif + if ( cfont.d != font.d ) { + cfont = font; + cfont.x11SetScreen( scrn ); + setf(DirtyFont); + } +} + + +void TQPainter::updateFont() +{ + if (!isActive()) + return; + + clearf(DirtyFont); + if ( testf(ExtDev) ) { + if (pdev->devType() == TQInternal::Printer) { + if ( pfont ) delete pfont; + pfont = new TQFont( cfont.d, pdev ); + } + TQPDevCmdParam param[1]; + param[0].font = &cfont; + if ( !pdev->cmd( TQPaintDevice::PdcSetFont, this, param ) || !hd ) + return; + } + setf(NoCache); + if ( penRef ) + updatePen(); // force a non-cached GC +} + + +void TQPainter::updatePen() +{ + if (!isActive()) + return; + + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + param[0].pen = &cpen; + if ( !pdev->cmd( TQPaintDevice::PdcSetPen, this, param ) || !hd ) + return; + } + + int ps = cpen.style(); + bool cacheIt = !testf(ClipOn|MonoDev|NoCache) && + (ps == NoPen || ps == SolidLine) && + cpen.width() == 0 && rop == CopyROP; + + bool obtained = FALSE; + bool internclipok = hasClipping(); + if ( cacheIt ) { + if ( gc ) { + if ( penRef ) + release_gc( penRef ); + else + free_gc( dpy, gc ); + } + obtained = obtain_gc(&penRef, &gc, cpen.color().pixel(scrn), dpy, scrn, + hd, clip_serial); + if ( !obtained && !penRef ) + gc = alloc_gc( dpy, scrn, hd, FALSE ); + } else { + if ( gc ) { + if ( penRef ) { + release_gc( penRef ); + penRef = 0; + gc = alloc_gc( dpy, scrn, hd, testf(MonoDev) ); + } else { + internclipok = TRUE; + } + } else { + gc = alloc_gc( dpy, scrn, hd, testf(MonoDev), testf(UsePrivateCx) ); + } + } + + if ( !internclipok ) { + if ( pdev == paintEventDevice && paintEventClipRegion ) { + if ( penRef &&((TQGCC*)penRef)->clip_serial < gc_cache_clip_serial ) { + x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion ); + ((TQGCC*)penRef)->clip_serial = gc_cache_clip_serial; + } else if ( !penRef ) { + x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion ); + } + } else if (penRef && ((TQGCC*)penRef)->clip_serial ) { + x11ClearClipRegion(dpy, gc, 0, rendhd); + ((TQGCC*)penRef)->clip_serial = 0; + } + } + + if ( obtained ) + return; + + char dashes[10]; // custom pen dashes + int dash_len = 0; // length of dash list + int s = LineSolid; + int cp = CapButt; + int jn = JoinMiter; + + /* + We are emulating Windows here. Windows treats cpen.width() == 1 + (or 0) as a very special case. The fudge variable unifies this + case with the general case. + */ + int dot = cpen.width(); // width of a dot + int fudge = 1; + bool allow_zero_lw = TRUE; + if ( dot <= 1 ) { + dot = 3; + fudge = 2; + } + + switch( ps ) { + case NoPen: + case SolidLine: + s = LineSolid; + break; + case DashLine: + dashes[0] = fudge * 3 * dot; + dashes[1] = fudge * dot; + dash_len = 2; + allow_zero_lw = FALSE; + break; + case DotLine: + dashes[0] = dot; + dashes[1] = dot; + dash_len = 2; + allow_zero_lw = FALSE; + break; + case DashDotLine: + dashes[0] = 3 * dot; + dashes[1] = fudge * dot; + dashes[2] = dot; + dashes[3] = fudge * dot; + dash_len = 4; + allow_zero_lw = FALSE; + break; + case DashDotDotLine: + dashes[0] = 3 * dot; + dashes[1] = dot; + dashes[2] = dot; + dashes[3] = dot; + dashes[4] = dot; + dashes[5] = dot; + dash_len = 6; + allow_zero_lw = FALSE; + } + Q_ASSERT( dash_len <= (int) sizeof(dashes) ); + + switch ( cpen.capStyle() ) { + case SquareCap: + cp = CapProjecting; + break; + case RoundCap: + cp = CapRound; + break; + case FlatCap: + default: + cp = CapButt; + break; + } + switch ( cpen.joinStyle() ) { + case BevelJoin: + jn = JoinBevel; + break; + case RoundJoin: + jn = JoinRound; + break; + case MiterJoin: + default: + jn = JoinMiter; + break; + } + + XSetForeground( dpy, gc, cpen.color().pixel(scrn) ); + XSetBackground( dpy, gc, bg_col.pixel(scrn) ); + + if ( dash_len ) { // make dash list + XSetDashes( dpy, gc, 0, dashes, dash_len ); + s = bg_mode == TransparentMode ? LineOnOffDash : LineDoubleDash; + } + XSetLineAttributes( dpy, gc, + (! allow_zero_lw && cpen.width() == 0) ? 1 : cpen.width(), + s, cp, jn ); +} + + +void TQPainter::updateBrush() +{ + if (!isActive()) + return; + + static const uchar dense1_pat[] = { 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff }; + static const uchar dense2_pat[] = { 0x77, 0xff, 0xdd, 0xff, 0x77, 0xff, 0xdd, 0xff }; + static const uchar dense3_pat[] = { 0x55, 0xbb, 0x55, 0xee, 0x55, 0xbb, 0x55, 0xee }; + static const uchar dense4_pat[] = { 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa }; + static const uchar dense5_pat[] = { 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44, 0xaa, 0x11 }; + static const uchar dense6_pat[] = { 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00 }; + static const uchar dense7_pat[] = { 0x00, 0x44, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00 }; + static const uchar hor_pat[] = { // horizontal pattern + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + static const uchar ver_pat[] = { // vertical pattern + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20 }; + static const uchar cross_pat[] = { // cross pattern + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0xff, 0xff, 0xff, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0xff, 0xff, 0xff, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0xff, 0xff, 0xff, + 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20, + 0x08, 0x82, 0x20, 0xff, 0xff, 0xff, 0x08, 0x82, 0x20, 0x08, 0x82, 0x20 }; + static const uchar bdiag_pat[] = { // backward diagonal pattern + 0x20, 0x20, 0x10, 0x10, 0x08, 0x08, 0x04, 0x04, 0x02, 0x02, 0x01, 0x01, + 0x80, 0x80, 0x40, 0x40, 0x20, 0x20, 0x10, 0x10, 0x08, 0x08, 0x04, 0x04, + 0x02, 0x02, 0x01, 0x01, 0x80, 0x80, 0x40, 0x40 }; + static const uchar fdiag_pat[] = { // forward diagonal pattern + 0x02, 0x02, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10, 0x20, 0x20, 0x40, 0x40, + 0x80, 0x80, 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x08, 0x08, 0x10, 0x10, + 0x20, 0x20, 0x40, 0x40, 0x80, 0x80, 0x01, 0x01 }; + static const uchar dcross_pat[] = { // diagonal cross pattern + 0x22, 0x22, 0x14, 0x14, 0x08, 0x08, 0x14, 0x14, 0x22, 0x22, 0x41, 0x41, + 0x80, 0x80, 0x41, 0x41, 0x22, 0x22, 0x14, 0x14, 0x08, 0x08, 0x14, 0x14, + 0x22, 0x22, 0x41, 0x41, 0x80, 0x80, 0x41, 0x41 }; + static const uchar * const pat_tbl[] = { + dense1_pat, dense2_pat, dense3_pat, dense4_pat, dense5_pat, + dense6_pat, dense7_pat, + hor_pat, ver_pat, cross_pat, bdiag_pat, fdiag_pat, dcross_pat }; + + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + param[0].brush = &cbrush; + if ( !pdev->cmd( TQPaintDevice::PdcSetBrush, this, param ) || !hd ) + return; + } + + int bs = cbrush.style(); + bool cacheIt = !testf(ClipOn|MonoDev|NoCache) && + (bs == NoBrush || bs == SolidPattern) && + bro.x() == 0 && bro.y() == 0 && rop == CopyROP; + + bool obtained = FALSE; + bool internclipok = hasClipping(); + if ( cacheIt ) { + if ( gc_brush ) { + if ( brushRef ) + release_gc( brushRef ); + else + free_gc( dpy, gc_brush ); + } + obtained = obtain_gc(&brushRef, &gc_brush, cbrush.color().pixel(scrn), dpy, + scrn, hd, clip_serial); + if ( !obtained && !brushRef ) + gc_brush = alloc_gc( dpy, scrn, hd, FALSE ); + } else { + if ( gc_brush ) { + if ( brushRef ) { + release_gc( brushRef ); + brushRef = 0; + gc_brush = alloc_gc( dpy, scrn, hd, testf(MonoDev) ); + } else { + internclipok = TRUE; + } + } else { + gc_brush = alloc_gc( dpy, scrn, hd, testf(MonoDev), testf(UsePrivateCx)); + } + } + + if ( !internclipok ) { + if ( pdev == paintEventDevice && paintEventClipRegion ) { + if ( brushRef &&((TQGCC*)brushRef)->clip_serial < gc_cache_clip_serial ) { + x11SetClipRegion( dpy, gc_brush, 0, rendhd, *paintEventClipRegion ); + ((TQGCC*)brushRef)->clip_serial = gc_cache_clip_serial; + } else if ( !brushRef ){ + x11SetClipRegion( dpy, gc_brush, 0, rendhd, *paintEventClipRegion ); + } + } else if (brushRef && ((TQGCC*)brushRef)->clip_serial ) { + x11ClearClipRegion(dpy, gc_brush, 0, rendhd); + ((TQGCC*)brushRef)->clip_serial = 0; + } + } + + if ( obtained ) + return; + + const uchar *pat = 0; // pattern + int d = 0; // defalt pattern size: d*d + int s = FillSolid; + if ( bs >= Dense1Pattern && bs <= DiagCrossPattern ) { + pat = pat_tbl[ bs-Dense1Pattern ]; + if ( bs <= Dense7Pattern ) + d = 8; + else if ( bs <= CrossPattern ) + d = 24; + else + d = 16; + } + + XSetLineAttributes( dpy, gc_brush, 0, LineSolid, CapButt, JoinMiter ); + XSetForeground( dpy, gc_brush, cbrush.color().pixel(scrn) ); + XSetBackground( dpy, gc_brush, bg_col.pixel(scrn) ); + + if ( bs == CustomPattern || pat ) { + TQPixmap *pm; + if ( pat ) { + TQString key; + key.sprintf( "$qt-brush$%d", bs ); + pm = TQPixmapCache::find( key ); + bool del = FALSE; + if ( !pm ) { // not already in pm dict + pm = new TQBitmap( d, d, pat, TRUE ); + Q_CHECK_PTR( pm ); + del = !TQPixmapCache::insert( key, pm ); + } + if ( cbrush.data->pixmap ) + delete cbrush.data->pixmap; + cbrush.data->pixmap = new TQPixmap( *pm ); + if (del) delete pm; + } + pm = cbrush.data->pixmap; + pm->x11SetScreen( scrn ); + if ( pm->depth() == 1 ) { + XSetStipple( dpy, gc_brush, pm->handle() ); + s = bg_mode == TransparentMode ? FillStippled : FillOpaqueStippled; + } else { + XSetTile( dpy, gc_brush, pm->handle() ); + s = FillTiled; + } + } + XSetFillStyle( dpy, gc_brush, s ); +} + + +/*! + Begins painting the paint device \a pd and returns TRUE if + successful; otherwise returns FALSE. If \a unclipped is TRUE, the + painting will not be clipped at the paint device's boundaries, + (although this is not supported by all platforms). + + The errors that can occur are serious problems, such as these: + + \code + p->begin( 0 ); // impossible - paint device cannot be 0 + + TQPixmap pm( 0, 0 ); + p->begin( pm ); // impossible - pm.isNull(); + + p->begin( myWidget ); + p2->begin( myWidget ); // impossible - only one painter at a time + \endcode + + Note that most of the time, you can use one of the constructors + instead of begin(), and that end() is automatically done at + destruction. + + \warning A paint device can only be painted by one painter at a + time. + + \sa end(), flush() +*/ + +bool TQPainter::begin( const TQPaintDevice *pd, bool unclipped ) +{ + if ( isActive() ) { // already active painting +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::begin: Painter is already active." + "\n\tYou must end() the painter before a second begin()" ); +#endif + return FALSE; + } + if ( pd == 0 ) { +#if defined(QT_CHECK_NULL) + qWarning( "TQPainter::begin: Paint device cannot be null" ); +#endif + return FALSE; + } + + TQPixmap::x11SetDefaultScreen( pd->x11Screen() ); + + const TQWidget *copyFrom = 0; + pdev = redirect( (TQPaintDevice*)pd ); + if ( pdev ) { // redirected paint device? + if ( pd->devType() == TQInternal::Widget ) + copyFrom = (const TQWidget *)pd; // copy widget settings + } else { + pdev = (TQPaintDevice*)pd; + } + + if ( pdev->isExtDev() && pdev->paintingActive() ) { + // somebody else is already painting +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::begin: Another TQPainter is already painting " + "this device;\n\tAn extended paint device can only be " + "painted by one TQPainter at a time." ); +#endif + return FALSE; + } + + bool reinit = flags != IsStartingUp; // 2nd or 3rd etc. time called + flags = IsActive | DirtyFont; // init flags + int dt = pdev->devType(); // get the device type + + if ( (pdev->devFlags & TQInternal::ExternalDevice) != 0 ) + setf(ExtDev); + else if ( dt == TQInternal::Pixmap ) // device is a pixmap + ((TQPixmap*)pdev)->detach(); // will modify it + + dpy = pdev->x11Display(); // get display variable + scrn = pdev->x11Screen(); // get screen variable + hd = pdev->handle(); // get handle to drawable + rendhd = pdev->rendhd; + + if ( testf(ExtDev) ) { // external device + if ( !pdev->cmd( TQPaintDevice::PdcBegin, this, 0 ) ) { + // could not begin painting + if ( reinit ) + clearf( IsActive | DirtyFont ); + else + flags = IsStartingUp; + pdev = 0; + return FALSE; + } + if ( tabstops ) // update tabstops for device + setTabStops( tabstops ); + if ( tabarray ) // update tabarray for device + setTabArray( tabarray ); + } + + if ( pdev->x11Depth() != pdev->x11AppDepth( scrn ) ) { // non-standard depth + setf(NoCache); + setf(UsePrivateCx); + } + + pdev->painters++; // also tell paint device + bro = curPt = TQPoint( 0, 0 ); + if ( reinit ) { + bg_mode = TransparentMode; // default background mode + rop = CopyROP; // default ROP + wxmat.reset(); // reset world xform matrix + xmat.reset(); + ixmat.reset(); + txop = txinv = 0; + if ( dt != TQInternal::Widget ) { + TQFont defaultFont; // default drawing tools + TQPen defaultPen; + TQBrush defaultBrush; + cfont = defaultFont; // set these drawing tools + cpen = defaultPen; + cbrush = defaultBrush; + bg_col = white; // default background color + } + } + wx = wy = vx = vy = 0; // default view origins + + if ( dt == TQInternal::Widget ) { // device is a widget + TQWidget *w = (TQWidget*)pdev; + cfont = w->font(); // use widget font + cpen = TQPen( w->foregroundColor() ); // use widget fg color + if ( reinit ) { + TQBrush defaultBrush; + cbrush = defaultBrush; + } + bg_col = w->backgroundColor(); // use widget bg color + ww = vw = w->width(); // default view size + wh = vh = w->height(); + if ( unclipped || w->testWFlags( WPaintUnclipped ) ) { // paint direct on device + setf( NoCache ); + setf(UsePrivateCx); + updatePen(); + updateBrush(); + XSetSubwindowMode( dpy, gc, IncludeInferiors ); + XSetSubwindowMode( dpy, gc_brush, IncludeInferiors ); +#ifndef QT_NO_XFTFREETYPE + if (rendhd) + XftDrawSetSubwindowMode((XftDraw *) rendhd, IncludeInferiors); +#endif + } + } else if ( dt == TQInternal::Pixmap ) { // device is a pixmap + TQPixmap *pm = (TQPixmap*)pdev; + if ( pm->isNull() ) { +#if defined(QT_CHECK_NULL) + qWarning( "TQPainter::begin: Cannot paint null pixmap" ); +#endif + end(); + return FALSE; + } + bool mono = pm->depth() == 1; // monochrome bitmap + if ( mono ) { + setf( MonoDev ); + bg_col = color0; + cpen.setColor( color1 ); + } + ww = vw = pm->width(); // default view size + wh = vh = pm->height(); + } else if ( testf(ExtDev) ) { // external device + ww = vw = pdev->metric( TQPaintDeviceMetrics::PdmWidth ); + wh = vh = pdev->metric( TQPaintDeviceMetrics::PdmHeight ); + } + if ( ww == 0 ) + ww = wh = vw = vh = 1024; + if ( copyFrom ) { // copy redirected widget + cfont = copyFrom->font(); + cpen = TQPen( copyFrom->foregroundColor() ); + bg_col = copyFrom->backgroundColor(); + } + if ( testf(ExtDev) ) { // external device + setBackgroundColor( bg_col ); // default background color + setBackgroundMode( TransparentMode ); // default background mode + setRasterOp( CopyROP ); // default raster operation + } + clip_serial = gc_cache_clip_serial++; + updateBrush(); + updatePen(); + return TRUE; +} + +/*! + Ends painting. Any resources used while painting are released. + + Note that while you mostly don't need to call end(), the + destructor will do it, there is at least one common case when it + is needed, namely double buffering. + + \code + TQPainter p( myPixmap, this ) + // ... + p.end(); // stops drawing on myPixmap + p.begin( this ); + p.drawPixmap( 0, 0, myPixmap ); + \endcode + + Since you can't draw a TQPixmap while it is being painted, it is + necessary to close the active painter. + + \sa begin(), isActive() +*/ + +bool TQPainter::end() // end painting +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::end: Missing begin() or begin() failed" ); +#endif + return FALSE; + } + killPStack(); + + //#### This should not be necessary: + if ( pdev->devType() == TQInternal::Widget && // ##### + ((TQWidget*)pdev)->testWFlags(WPaintUnclipped) ) { + if ( gc ) + XSetSubwindowMode( dpy, gc, ClipByChildren ); + if ( gc_brush ) + XSetSubwindowMode( dpy, gc_brush, ClipByChildren ); + } + + if ( gc_brush ) { // restore brush gc + if ( brushRef ) { + release_gc( brushRef ); + brushRef = 0; + } else { + free_gc( dpy, gc_brush, testf(UsePrivateCx) ); + } + gc_brush = 0; + + } + if ( gc ) { // restore pen gc + if ( penRef ) { + release_gc( penRef ); + penRef = 0; + } else { + free_gc( dpy, gc, testf(UsePrivateCx) ); + } + gc = 0; + } + + if ( testf(ExtDev) ) + pdev->cmd( TQPaintDevice::PdcEnd, this, 0 ); + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + // reset clipping/subwindow mode on our render picture + XftDrawSetClip((XftDraw *) rendhd, None); + XftDrawSetSubwindowMode((XftDraw *) rendhd, ClipByChildren); + } +#endif // QT_NO_XFTFREETYPE + + if ( pfont ) { + delete pfont; + pfont = 0; + } + + flags = 0; + pdev->painters--; + pdev = 0; + dpy = 0; + return TRUE; +} + +/*! + Flushes any buffered drawing operations inside the region \a + region using clipping mode \a cm. + + The flush may update the whole device if the platform does not + support flushing to a specified region. + + \sa flush() CoordinateMode +*/ + +void TQPainter::flush(const TQRegion &, CoordinateMode) +{ + flush(); +} + + +/*! + \overload + + Flushes any buffered drawing operations. +*/ + +void TQPainter::flush() +{ + if ( isActive() && dpy ) + XFlush( dpy ); +} + + +/*! + Sets the background color of the painter to \a c. + + The background color is the color that is filled in when drawing + opaque text, stippled lines and bitmaps. The background color has + no effect in transparent background mode (which is the default). + + \sa backgroundColor() setBackgroundMode() BackgroundMode +*/ + +void TQPainter::setBackgroundColor( const TQColor &c ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::setBackgroundColor: Call begin() first" ); +#endif + return; + } + bg_col = c; + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + param[0].color = &bg_col; + if ( !pdev->cmd( TQPaintDevice::PdcSetBkColor, this, param ) || !hd ) + return; + } + if ( !penRef ) + updatePen(); // update pen setting + if ( !brushRef ) + updateBrush(); // update brush setting +} + +/*! + Sets the background mode of the painter to \a m, which must be + either \c TransparentMode (the default) or \c OpaqueMode. + + Transparent mode draws stippled lines and text without setting the + background pixels. Opaque mode fills these space with the current + background color. + + Note that in order to draw a bitmap or pixmap transparently, you + must use TQPixmap::setMask(). + + \sa backgroundMode(), setBackgroundColor() +*/ + +void TQPainter::setBackgroundMode( BGMode m ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::setBackgroundMode: Call begin() first" ); +#endif + return; + } + if ( m != TransparentMode && m != OpaqueMode ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPainter::setBackgroundMode: Invalid mode" ); +#endif + return; + } + bg_mode = m; + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + param[0].ival = m; + if ( !pdev->cmd( TQPaintDevice::PdcSetBkMode, this, param ) || !hd ) + return; + } + if ( !penRef ) + updatePen(); // update pen setting + if ( !brushRef ) + updateBrush(); // update brush setting +} + +static const short ropCodes[] = { // ROP translation table + GXcopy, // CopyROP + GXor, // OrROP + GXxor, // XorROP + GXandInverted, // NotAndROP EraseROP + GXcopyInverted, // NotCopyROP + GXorInverted, // NotOrROP + GXequiv, // NotXorROP + GXand, // AndROP + GXinvert, // NotROP + GXclear, // ClearROP + GXset, // SetROP + GXnoop, // NopROP + GXandReverse, // AndNotROP + GXorReverse, // OrNotROP + GXnand, // NandROP + GXnor // NorROP +}; + + +/*! + Sets the \link TQt::RasterOp raster operation \endlink to \a r. + The default is \c CopyROP. + + \sa rasterOp() TQt::RasterOp +*/ + +void TQPainter::setRasterOp( RasterOp r ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::setRasterOp: Call begin() first" ); +#endif + return; + } + if ( (uint)r > LastROP ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPainter::setRasterOp: Invalid ROP code" ); +#endif + return; + } + rop = r; + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + param[0].ival = r; + if ( !pdev->cmd( TQPaintDevice::PdcSetROP, this, param ) || !hd ) + return; + } + if ( penRef ) + updatePen(); // get non-cached pen GC + if ( brushRef ) + updateBrush(); // get non-cached brush GC + XSetFunction( dpy, gc, ropCodes[rop] ); + XSetFunction( dpy, gc_brush, ropCodes[rop] ); +} + +// ### matthias - true? + +/*! + Sets the brush origin to \a (x, y). + + The brush origin specifies the (0, 0) coordinate of the painter's + brush. This setting only applies to pattern brushes and pixmap + brushes. + + \sa brushOrigin() +*/ + +void TQPainter::setBrushOrigin( int x, int y ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::setBrushOrigin: Call begin() first" ); +#endif + return; + } + bro = TQPoint(x, y); + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + param[0].point = &bro; + if ( !pdev->cmd( TQPaintDevice::PdcSetBrushOrigin, this, param ) || + !hd ) + return; + } + if ( brushRef ) + updateBrush(); // get non-cached brush GC + XSetTSOrigin( dpy, gc_brush, x, y ); +} + + +/*! + Enables clipping if \a enable is TRUE, or disables clipping if \a + enable is FALSE. + + \sa hasClipping(), setClipRect(), setClipRegion() +*/ + +void TQPainter::setClipping( bool enable ) +{ + if ( !isActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPainter::setClipping: Will be reset by begin()" ); +#endif + return; + } + + if ( enable == testf(ClipOn) ) + return; + + setf( ClipOn, enable ); + if ( testf(ExtDev) ) { + if ( block_ext ) + return; + TQPDevCmdParam param[1]; + param[0].ival = enable; + if ( !pdev->cmd( TQPaintDevice::PdcSetClip, this, param ) || !hd ) + return; + } + if ( enable ) { + TQRegion rgn = crgn; + if ( pdev == paintEventDevice && paintEventClipRegion ) + rgn = rgn.intersect( *paintEventClipRegion ); + if ( penRef ) + updatePen(); + if ( brushRef ) + updateBrush(); + x11SetClipRegion( dpy, gc, gc_brush, rendhd, rgn ); + } else { + if ( pdev == paintEventDevice && paintEventClipRegion ) { + x11SetClipRegion( dpy, gc, gc_brush , rendhd, *paintEventClipRegion ); + } else { + x11ClearClipRegion(dpy, gc, gc_brush, rendhd); + } + } +} + + +/*! + \overload + + Sets the clip region to the rectangle \a r and enables clipping. + The clip mode is set to \a m. + + \sa CoordinateMode +*/ + +void TQPainter::setClipRect( const TQRect &r, CoordinateMode m ) +{ + setClipRegion( TQRegion( r ), m ); +} + +/*! + Sets the clip region to \a rgn and enables clipping. The clip mode + is set to \a m. + + Note that the clip region is given in physical device coordinates + and \e not subject to any \link coordsys.html coordinate + transformation.\endlink + + \sa setClipRect(), clipRegion(), setClipping() CoordinateMode +*/ + +void TQPainter::setClipRegion( const TQRegion &rgn, CoordinateMode m ) +{ +#if defined(QT_CHECK_STATE) + if ( !isActive() ) + qWarning( "TQPainter::setClipRegion: Will be reset by begin()" ); +#endif + if ( m == CoordDevice ) + crgn = rgn; + else + crgn = xmat * rgn; + + if ( testf(ExtDev) ) { + if ( block_ext ) + return; + TQPDevCmdParam param[2]; + param[0].rgn = &rgn; + param[1].ival = m; + if ( !pdev->cmd( TQPaintDevice::PdcSetClipRegion, this, param ) ) + return; // device cannot clip + } + clearf( ClipOn ); // be sure to update clip rgn + setClipping( TRUE ); +} + + +/*! + \internal + + Internal function for drawing a polygon. +*/ + +void TQPainter::drawPolyInternal( const TQPointArray &a, bool close ) +{ + if ( a.size() < 2 ) + return; + + int x1, y1, x2, y2; // connect last to first point + a.point( a.size()-1, &x1, &y1 ); + a.point( 0, &x2, &y2 ); + bool do_close = close && !(x1 == x2 && y1 == y2); + + if ( close && cbrush.style() != NoBrush ) { // draw filled polygon + XFillPolygon( dpy, hd, gc_brush, (XPoint*)a.shortPoints(), a.size(), + Nonconvex, CoordModeOrigin ); + if ( cpen.style() == NoPen ) { // draw fake outline + XDrawLines( dpy, hd, gc_brush, (XPoint*)a.shortPoints(), a.size(), + CoordModeOrigin ); + if ( do_close ) + XDrawLine( dpy, hd, gc_brush, x1, y1, x2, y2 ); + } + } + if ( cpen.style() != NoPen ) { // draw outline + XDrawLines( dpy, hd, gc, (XPoint*)a.shortPoints(), a.size(), + CoordModeOrigin); + if ( do_close ) + XDrawLine( dpy, hd, gc, x1, y1, x2, y2 ); + } +} + + +/*! + Draws/plots a single point at \a (x, y) using the current pen. + + \sa TQPen +*/ + +void TQPainter::drawPoint( int x, int y ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + TQPoint p( x, y ); + param[0].point = &p; + if ( !pdev->cmd( TQPaintDevice::PdcDrawPoint, this, param ) || + !hd ) + return; + } + map( x, y, &x, &y ); + } + if ( cpen.style() != NoPen ) + XDrawPoint( dpy, hd, gc, x, y ); +} + + +/*! + Draws/plots an array of points, \a a, using the current pen. + + If \a index is non-zero (the default is zero) only points from \a + index are drawn. If \a npoints is negative (the default) the rest + of the points from \a index are drawn. If \a npoints is zero or + greater, \a npoints points are drawn. + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + TQt 4. +*/ + +void TQPainter::drawPoints( const TQPointArray& a, int index, int npoints ) +{ + if ( npoints < 0 ) + npoints = a.size() - index; + if ( index + npoints > (int)a.size() ) + npoints = a.size() - index; + if ( !isActive() || npoints < 1 || index < 0 ) + return; + TQPointArray pa = a; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + for (int i=0; icmd( TQPaintDevice::PdcDrawPoint, this, param )) + return; + } + if ( !hd ) return; + } + if ( txop != TxNone ) { + pa = xForm( a, index, npoints ); + if ( pa.size() != a.size() ) { + index = 0; + npoints = pa.size(); + } + } + } + if ( cpen.style() != NoPen ) + XDrawPoints( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )), + npoints, CoordModeOrigin ); +} + + +/*! \obsolete + Sets the current pen position to \a (x, y) + + \sa lineTo(), pos() +*/ + +void TQPainter::moveTo( int x, int y ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + TQPoint p( x, y ); + param[0].point = &p; + if ( !pdev->cmd( TQPaintDevice::PdcMoveTo, this, param ) || !hd ) + return; + } + } + curPt = TQPoint( x, y ); +} + +/*! \obsolete + Use drawLine() instead. + + Draws a line from the current pen position to \a (x, y) and sets + \a (x, y) to be the new current pen position. + + \sa TQPen moveTo(), drawLine(), pos() +*/ + +void TQPainter::lineTo( int x, int y ) +{ + if ( !isActive() ) + return; + int cx = curPt.x(), cy = curPt.y(); + curPt = TQPoint( x, y ); + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + TQPoint p( x, y ); + param[0].point = &p; + if ( !pdev->cmd( TQPaintDevice::PdcLineTo, this, param ) || !hd ) + return; + } + map( x, y, &x, &y ); + map( cx, cy, &cx, &cy ); + } + if ( cpen.style() != NoPen ) + XDrawLine( dpy, hd, gc, cx, cy, x, y ); +} + +/*! + Draws a line from (\a x1, \a y1) to (\a x2, \a y2) and sets the + current pen position to (\a x2, \a y2). + + \sa pen() +*/ + +void TQPainter::drawLine( int x1, int y1, int x2, int y2 ) +{ + if ( !isActive() ) + return; + curPt = TQPoint( x2, y2 ); + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[2]; + TQPoint p1(x1, y1), p2(x2, y2); + param[0].point = &p1; + param[1].point = &p2; + if ( !pdev->cmd( TQPaintDevice::PdcDrawLine, this, param ) || !hd ) + return; + } + map( x1, y1, &x1, &y1 ); + map( x2, y2, &x2, &y2 ); + } + if ( cpen.style() != NoPen ) + XDrawLine( dpy, hd, gc, x1, y1, x2, y2 ); +} + + + +/*! + Draws a rectangle with upper left corner at \a (x, y) and with + width \a w and height \a h. + + \sa TQPen, drawRoundRect() +*/ + +void TQPainter::drawRect( int x, int y, int w, int h ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + TQRect r( x, y, w, h ); + param[0].rect = &r; + if ( !pdev->cmd( TQPaintDevice::PdcDrawRect, this, param ) || !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear polygon + TQPointArray pa = xmat.mapToPolygon( TQRect(x, y, w, h) ); + pa.resize( 5 ); + pa.setPoint( 4, pa.point( 0 ) ); + drawPolyInternal( pa ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + if ( cbrush.style() != NoBrush ) { + if ( cpen.style() == NoPen ) { + XFillRectangle( dpy, hd, gc_brush, x, y, w, h ); + return; + } + int lw = cpen.width(); + int lw2 = (lw+1)/2; + if ( w > lw && h > lw ) + XFillRectangle( dpy, hd, gc_brush, x+lw2, y+lw2, w-lw-1, h-lw-1 ); + } + if ( cpen.style() != NoPen ) + XDrawRectangle( dpy, hd, gc, x, y, w-1, h-1 ); +} + +/*! + \overload + + Draws a Windows focus rectangle with upper left corner at (\a x, + \a y) and with width \a w and height \a h. + + This function draws a stippled XOR rectangle that is used to + indicate keyboard focus (when TQApplication::style() is \c + WindowStyle). + + \warning This function draws nothing if the coordinate system has + been \link rotate() rotated\endlink or \link shear() + sheared\endlink. + + \sa drawRect(), TQApplication::style() +*/ + +void TQPainter::drawWinFocusRect( int x, int y, int w, int h ) +{ + drawWinFocusRect( x, y, w, h, TRUE, color0 ); +} + +/*! + Draws a Windows focus rectangle with upper left corner at (\a x, + \a y) and with width \a w and height \a h using a pen color that + contrasts with \a bgColor. + + This function draws a stippled rectangle (XOR is not used) that is + used to indicate keyboard focus (when the TQApplication::style() is + \c WindowStyle). + + The pen color used to draw the rectangle is either white or black + depending on the color of \a bgColor (see TQColor::gray()). + + \warning This function draws nothing if the coordinate system has + been \link rotate() rotated\endlink or \link shear() + sheared\endlink. + + \sa drawRect(), TQApplication::style() +*/ + +void TQPainter::drawWinFocusRect( int x, int y, int w, int h, + const TQColor &bgColor ) +{ + drawWinFocusRect( x, y, w, h, FALSE, bgColor ); +} + + +/*! + \internal +*/ + +void TQPainter::drawWinFocusRect( int x, int y, int w, int h, + bool xorPaint, const TQColor &bgColor ) +{ + if ( !isActive() || txop == TxRotShear ) + return; + static char winfocus_line[] = { 1, 1 }; + + TQPen old_pen = cpen; + RasterOp old_rop = (RasterOp)rop; + + if ( xorPaint ) { + if ( TQColor::numBitPlanes() <= 8 ) + setPen( color1 ); + else + setPen( white ); + setRasterOp( XorROP ); + } else { + if ( qGray( bgColor.rgb() ) < 128 ) + setPen( white ); + else + setPen( black ); + } + + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + TQRect r( x, y, w, h ); + param[0].rect = &r; + if ( !pdev->cmd( TQPaintDevice::PdcDrawRect, this, param ) || !hd) { + setRasterOp( old_rop ); + setPen( old_pen ); + return; + } + } + map( x, y, w, h, &x, &y, &w, &h ); + } + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + XSetDashes( dpy, gc, 0, winfocus_line, 2 ); + XSetLineAttributes( dpy, gc, 1, LineOnOffDash, CapButt, JoinMiter ); + + XDrawRectangle( dpy, hd, gc, x, y, w-1, h-1 ); + XSetLineAttributes( dpy, gc, 0, LineSolid, CapButt, JoinMiter ); + setRasterOp( old_rop ); + setPen( old_pen ); +} + + +/*! + Draws a rectangle with rounded corners at \a (x, y), with width \a + w and height \a h. + + The \a xRnd and \a yRnd arguments specify how rounded the corners + should be. 0 is angled corners, 99 is maximum roundedness. + + The width and height include all of the drawn lines. + + \sa drawRect(), TQPen +*/ + +void TQPainter::drawRoundRect( int x, int y, int w, int h, int xRnd, int yRnd ) +{ + if ( !isActive() ) + return; + if ( xRnd <= 0 || yRnd <= 0 ) { + drawRect( x, y, w, h ); // draw normal rectangle + return; + } + if ( xRnd >= 100 ) // fix ranges + xRnd = 99; + if ( yRnd >= 100 ) + yRnd = 99; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[3]; + TQRect r( x, y, w, h ); + param[0].rect = &r; + param[1].ival = xRnd; + param[2].ival = yRnd; + if ( !pdev->cmd( TQPaintDevice::PdcDrawRoundRect, this, param ) || + !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear polygon + if ( w <= 0 || h <= 0 ) + fix_neg_rect( &x, &y, &w, &h ); + w--; + h--; + int rxx = w*xRnd/200; + int ryy = h*yRnd/200; + // were there overflows? + if ( rxx < 0 ) + rxx = w/200*xRnd; + if ( ryy < 0 ) + ryy = h/200*yRnd; + int rxx2 = 2*rxx; + int ryy2 = 2*ryy; + TQPointArray a[4]; + a[0].makeArc( x, y, rxx2, ryy2, 1*16*90, 16*90, xmat ); + a[1].makeArc( x, y+h-ryy2, rxx2, ryy2, 2*16*90, 16*90, xmat ); + a[2].makeArc( x+w-rxx2, y+h-ryy2, rxx2, ryy2, 3*16*90, 16*90, xmat ); + a[3].makeArc( x+w-rxx2, y, rxx2, ryy2, 0*16*90, 16*90, xmat ); + // ### is there a better way to join TQPointArrays? + TQPointArray aa; + aa.resize( a[0].size() + a[1].size() + a[2].size() + a[3].size() ); + uint j = 0; + for ( int k=0; k<4; k++ ) { + for ( uint i=0; ix=px; a->y=py; a->width=w; a->height=h; a->angle1=a1; a->angle2=a2; a++ + XArc arcs[4]; + XArc *a = arcs; + SET_ARC( x+w-rx2, y, rx2, ry2, 0, 90*64 ); + SET_ARC( x, y, rx2, ry2, 90*64, 90*64 ); + SET_ARC( x, y+h-ry2, rx2, ry2, 180*64, 90*64 ); + SET_ARC( x+w-rx2, y+h-ry2, rx2, ry2, 270*64, 90*64 ); + XFillArcs( dpy, hd, gc_brush, arcs, 4 ); +#undef SET_ARC +#define SET_RCT(px, py, w, h) \ + r->x=px; r->y=py; r->width=w; r->height=h; r++ + XRectangle rects[3]; + XRectangle *r = rects; + SET_RCT( x+rx, y+dp, w-rx2, ry ); + SET_RCT( x+dp, y+ry, w+ds, h-ry2 ); + SET_RCT( x+rx, y+h-ry, w-rx2, ry+ds ); + XFillRectangles( dpy, hd, gc_brush, rects, 3 ); +#undef SET_RCT + } + if ( cpen.style() != NoPen ) { // draw outline +#define SET_ARC(px, py, w, h, a1, a2) \ + a->x=px; a->y=py; a->width=w; a->height=h; a->angle1=a1; a->angle2=a2; a++ + XArc arcs[4]; + XArc *a = arcs; + SET_ARC( x+w-rx2, y, rx2, ry2, 0, 90*64 ); + SET_ARC( x, y, rx2, ry2, 90*64, 90*64 ); + SET_ARC( x, y+h-ry2, rx2, ry2, 180*64, 90*64 ); + SET_ARC( x+w-rx2, y+h-ry2, rx2, ry2, 270*64, 90*64 ); + XDrawArcs( dpy, hd, gc, arcs, 4 ); +#undef SET_ARC +#define SET_SEG(xp1, yp1, xp2, yp2) \ + s->x1=xp1; s->y1=yp1; s->x2=xp2; s->y2=yp2; s++ + XSegment segs[4]; + XSegment *s = segs; + SET_SEG( x+rx, y, x+w-rx, y ); + SET_SEG( x+rx, y+h, x+w-rx, y+h ); + SET_SEG( x, y+ry, x, y+h-ry ); + SET_SEG( x+w, y+ry, x+w, y+h-ry ); + XDrawSegments( dpy, hd, gc, segs, 4 ); +#undef SET_SET + } +} + +/*! + Draws an ellipse with center at \a (x + w/2, y + h/2) and size \a + (w, h). +*/ + +void TQPainter::drawEllipse( int x, int y, int w, int h ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + TQRect r( x, y, w, h ); + param[0].rect = &r; + if ( !pdev->cmd( TQPaintDevice::PdcDrawEllipse, this, param ) || + !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear polygon + TQPointArray a; + a.makeArc( x, y, w, h, 0, 360*16, xmat ); + drawPolyInternal( a ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + if ( w == 1 && h == 1 ) { + XDrawPoint( dpy, hd, (cpen.style() == NoPen)?gc_brush:gc, x, y ); + return; + } + w--; + h--; + if ( cbrush.style() != NoBrush ) { // draw filled ellipse + XFillArc( dpy, hd, gc_brush, x, y, w, h, 0, 360*64 ); + if ( cpen.style() == NoPen ) { + XDrawArc( dpy, hd, gc_brush, x, y, w, h, 0, 360*64 ); + return; + } + } + if ( cpen.style() != NoPen ) // draw outline + XDrawArc( dpy, hd, gc, x, y, w, h, 0, 360*64 ); +} + + +/*! + Draws an arc defined by the rectangle \a (x, y, w, h), the start + angle \a a and the arc length \a alen. + + The angles \a a and \a alen are 1/16th of a degree, i.e. a full + circle equals 5760 (16*360). Positive values of \a a and \a alen + mean counter-clockwise while negative values mean the clockwise + direction. Zero degrees is at the 3 o'clock position. + + Example: + \code + TQPainter p( myWidget ); + p.drawArc( 10,10, 70,100, 100*16, 160*16 ); // draws a "(" arc + \endcode + + \sa drawPie(), drawChord() +*/ + +void TQPainter::drawArc( int x, int y, int w, int h, int a, int alen ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[3]; + TQRect r( x, y, w, h ); + param[0].rect = &r; + param[1].ival = a; + param[2].ival = alen; + if ( !pdev->cmd( TQPaintDevice::PdcDrawArc, this, param ) || + !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear + TQPointArray pa; + pa.makeArc( x, y, w, h, a, alen, xmat ); // arc polyline + drawPolyInternal( pa, FALSE ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + w--; + h--; + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + if ( cpen.style() != NoPen ) + XDrawArc( dpy, hd, gc, x, y, w, h, a*4, alen*4 ); +} + + +/*! + Draws a pie defined by the rectangle \a (x, y, w, h), the start + angle \a a and the arc length \a alen. + + The pie is filled with the current brush(). + + The angles \a a and \a alen are 1/16th of a degree, i.e. a full + circle equals 5760 (16*360). Positive values of \a a and \a alen + mean counter-clockwise while negative values mean the clockwise + direction. Zero degrees is at the 3 o'clock position. + + \sa drawArc(), drawChord() +*/ + +void TQPainter::drawPie( int x, int y, int w, int h, int a, int alen ) +{ + // Make sure "a" is 0..360*16, as otherwise a*4 may overflow 16 bits. + if ( a > (360*16) ) { + a = a % (360*16); + } else if ( a < 0 ) { + a = a % (360*16); + if ( a < 0 ) a += (360*16); + } + + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[3]; + TQRect r( x, y, w, h ); + param[0].rect = &r; + param[1].ival = a; + param[2].ival = alen; + if ( !pdev->cmd( TQPaintDevice::PdcDrawPie, this, param ) || !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear + TQPointArray pa; + pa.makeArc( x, y, w, h, a, alen, xmat ); // arc polyline + int n = pa.size(); + int cx, cy; + xmat.map(x+w/2, y+h/2, &cx, &cy); + pa.resize( n+2 ); + pa.setPoint( n, cx, cy ); // add legs + pa.setPoint( n+1, pa.at(0) ); + drawPolyInternal( pa ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + XSetArcMode( dpy, gc_brush, ArcPieSlice ); + w--; + h--; + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + + GC g = gc; + bool nopen = cpen.style() == NoPen; + + if ( cbrush.style() != NoBrush ) { // draw filled pie + XFillArc( dpy, hd, gc_brush, x, y, w, h, a*4, alen*4 ); + if ( nopen ) { + g = gc_brush; + nopen = FALSE; + } + } + if ( !nopen ) { // draw pie outline + double w2 = 0.5*w; // with, height in ellipsis + double h2 = 0.5*h; + double xc = (double)x+w2; + double yc = (double)y+h2; + double ra1 = Q_PI/2880.0*a; // convert a, alen to radians + double ra2 = ra1 + Q_PI/2880.0*alen; + int xic = qRound(xc); + int yic = qRound(yc); + XDrawLine( dpy, hd, g, xic, yic, + qRound(xc + qcos(ra1)*w2), qRound(yc - qsin(ra1)*h2)); + XDrawLine( dpy, hd, g, xic, yic, + qRound(xc + qcos(ra2)*w2), qRound(yc - qsin(ra2)*h2)); + XDrawArc( dpy, hd, g, x, y, w, h, a*4, alen*4 ); + } +} + + +/*! + Draws a chord defined by the rectangle \a (x, y, w, h), the start + angle \a a and the arc length \a alen. + + The chord is filled with the current brush(). + + The angles \a a and \a alen are 1/16th of a degree, i.e. a full + circle equals 5760 (16*360). Positive values of \a a and \a alen + mean counter-clockwise while negative values mean the clockwise + direction. Zero degrees is at the 3 o'clock position. + + \sa drawArc(), drawPie() +*/ + +void TQPainter::drawChord( int x, int y, int w, int h, int a, int alen ) +{ + if ( !isActive() ) + return; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[3]; + TQRect r( x, y, w, h ); + param[0].rect = &r; + param[1].ival = a; + param[2].ival = alen; + if ( !pdev->cmd(TQPaintDevice::PdcDrawChord, this, param) || !hd ) + return; + } + if ( txop == TxRotShear ) { // rotate/shear + TQPointArray pa; + pa.makeArc( x, y, w-1, h-1, a, alen, xmat ); // arc polygon + int n = pa.size(); + pa.resize( n+1 ); + pa.setPoint( n, pa.at(0) ); // connect endpoints + drawPolyInternal( pa ); + return; + } + map( x, y, w, h, &x, &y, &w, &h ); + } + XSetArcMode( dpy, gc_brush, ArcChord ); + w--; + h--; + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) + return; + fix_neg_rect( &x, &y, &w, &h ); + } + + GC g = gc; + bool nopen = cpen.style() == NoPen; + + if ( cbrush.style() != NoBrush ) { // draw filled chord + XFillArc( dpy, hd, gc_brush, x, y, w, h, a*4, alen*4 ); + if ( nopen ) { + g = gc_brush; + nopen = FALSE; + } + } + if ( !nopen ) { // draw chord outline + double w2 = 0.5*w; // with, height in ellipsis + double h2 = 0.5*h; + double xc = (double)x+w2; + double yc = (double)y+h2; + double ra1 = Q_PI/2880.0*a; // convert a, alen to radians + double ra2 = ra1 + Q_PI/2880.0*alen; + XDrawLine( dpy, hd, g, + qRound(xc + qcos(ra1)*w2), qRound(yc - qsin(ra1)*h2), + qRound(xc + qcos(ra2)*w2), qRound(yc - qsin(ra2)*h2)); + XDrawArc( dpy, hd, g, x, y, w, h, a*4, alen*4 ); + } + XSetArcMode( dpy, gc_brush, ArcPieSlice ); +} + + +/*! + Draws \a nlines separate lines from points defined in \a a, + starting at \a a[index] (\a index defaults to 0). If \a nlines is + -1 (the default) all points until the end of the array are used + (i.e. (a.size()-index)/2 lines are drawn). + + Draws the 1st line from \a a[index] to \a a[index+1]. Draws the + 2nd line from \a a[index+2] to \a a[index+3] etc. + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + TQt 4. + + \sa drawPolyline(), drawPolygon(), TQPen +*/ + +void TQPainter::drawLineSegments( const TQPointArray &a, int index, int nlines ) +{ + if ( nlines < 0 ) + nlines = a.size()/2 - index/2; + if ( index + nlines*2 > (int)a.size() ) + nlines = (a.size() - index)/2; + if ( !isActive() || nlines < 1 || index < 0 ) + return; + TQPointArray pa = a; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + if ( 2*nlines != (int)pa.size() ) { + pa = TQPointArray( nlines*2 ); + for ( int i=0; icmd(TQPaintDevice::PdcDrawLineSegments, this, param) || + !hd ) + return; + } + if ( txop != TxNone ) { + pa = xForm( a, index, nlines*2 ); + if ( pa.size() != a.size() ) { + index = 0; + nlines = pa.size()/2; + } + } + } + if ( cpen.style() != NoPen ) + XDrawSegments( dpy, hd, gc, + (XSegment*)(pa.shortPoints( index, nlines*2 )), nlines ); +} + + +/*! + Draws the polyline defined by the \a npoints points in \a a + starting at \a a[index]. (\a index defaults to 0.) + + If \a npoints is -1 (the default) all points until the end of the + array are used (i.e. a.size()-index-1 line segments are drawn). + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + TQt 4. + + \sa drawLineSegments(), drawPolygon(), TQPen +*/ + +void TQPainter::drawPolyline( const TQPointArray &a, int index, int npoints ) +{ + if ( npoints < 0 ) + npoints = a.size() - index; + if ( index + npoints > (int)a.size() ) + npoints = a.size() - index; + if ( !isActive() || npoints < 2 || index < 0 ) + return; + TQPointArray pa = a; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + if ( npoints != (int)pa.size() ) { + pa = TQPointArray( npoints ); + for ( int i=0; icmd(TQPaintDevice::PdcDrawPolyline, this, param) || !hd ) + return; + } + if ( txop != TxNone ) { + pa = xForm( pa, index, npoints ); + if ( pa.size() != a.size() ) { + index = 0; + npoints = pa.size(); + } + } + } + if ( cpen.style() != NoPen ) { + while(npoints>65535) { + XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, 65535 )), + 65535, CoordModeOrigin ); + npoints-=65535; + index+=65535; + } + XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )), + npoints, CoordModeOrigin ); + } +} + +static int global_polygon_shape = Complex; + +/*! + Draws the polygon defined by the \a npoints points in \a a + starting at \a a[index]. (\a index defaults to 0.) + + If \a npoints is -1 (the default) all points until the end of the + array are used (i.e. a.size()-index line segments define the + polygon). + + The first point is always connected to the last point. + + The polygon is filled with the current brush(). If \a winding is + TRUE, the polygon is filled using the winding fill algorithm. If + \a winding is FALSE, the polygon is filled using the even-odd + (alternative) fill algorithm. + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + TQt 4. + + \sa drawLineSegments(), drawPolyline(), TQPen +*/ + +void TQPainter::drawPolygon( const TQPointArray &a, bool winding, + int index, int npoints ) +{ + if ( npoints < 0 ) + npoints = a.size() - index; + if ( index + npoints > (int)a.size() ) + npoints = a.size() - index; + if ( !isActive() || npoints < 2 || index < 0 ) + return; + TQPointArray pa = a; + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + if ( npoints != (int)a.size() ) { + pa = TQPointArray( npoints ); + for ( int i=0; icmd(TQPaintDevice::PdcDrawPolygon, this, param) || !hd ) + return; + } + if ( txop != TxNone ) { + pa = xForm( a, index, npoints ); + if ( pa.size() != a.size() ) { + index = 0; + npoints = pa.size(); + } + } + } + if ( winding ) // set to winding fill rule + XSetFillRule( dpy, gc_brush, WindingRule ); + + if ( pa[index] != pa[index+npoints-1] ){ // close open pointarray + pa.detach(); + pa.resize( index+npoints+1 ); + pa.setPoint( index+npoints, pa[index] ); + npoints++; + } + + if ( cbrush.style() != NoBrush ) { // draw filled polygon + XFillPolygon( dpy, hd, gc_brush, + (XPoint*)(pa.shortPoints( index, npoints )), + npoints, global_polygon_shape, CoordModeOrigin ); + } + if ( cpen.style() != NoPen ) { // draw outline + XDrawLines( dpy, hd, gc, (XPoint*)(pa.shortPoints( index, npoints )), + npoints, CoordModeOrigin ); + } + if ( winding ) // set to normal fill rule + XSetFillRule( dpy, gc_brush, EvenOddRule ); +} + +/*! + Draws the convex polygon defined by the \a npoints points in \a pa + starting at \a pa[index] (\a index defaults to 0). + + If the supplied polygon is not convex, the results are undefined. + + On some platforms (e.g. X Window), this is faster than + drawPolygon(). + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + TQt 4. +*/ +void TQPainter::drawConvexPolygon( const TQPointArray &pa, + int index, int npoints ) +{ + global_polygon_shape = Convex; + drawPolygon(pa, FALSE, index, npoints); + global_polygon_shape = Complex; +} + + + +/*! + Draws a cubic Bezier curve defined by the control points in \a a, + starting at \a a[index] (\a index defaults to 0). + + Control points after \a a[index + 3] are ignored. Nothing happens + if there aren't enough control points. + + \warning On X11, coordinates that do not fit into 16-bit signed + values are truncated. This limitation is expected to go away in + TQt 4. +*/ + +void TQPainter::drawCubicBezier( const TQPointArray &a, int index ) +{ + if ( !isActive() ) + return; + if ( a.size() - index < 4 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPainter::drawCubicBezier: Cubic Bezier needs 4 control " + "points" ); +#endif + return; + } + TQPointArray pa( a ); + if ( index != 0 || a.size() > 4 ) { + pa = TQPointArray( 4 ); + for ( int i=0; i<4; i++ ) + pa.setPoint( i, a.point(index+i) ); + } + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) ) { + TQPDevCmdParam param[1]; + param[0].ptarr = (TQPointArray*)&pa; + if ( !pdev->cmd(TQPaintDevice::PdcDrawCubicBezier, this, param) || + !hd ) + return; + } + if ( txop != TxNone ) + pa = xForm( pa ); + } + if ( cpen.style() != NoPen ) { + pa = pa.cubicBezier(); + XDrawLines( dpy, hd, gc, (XPoint*)pa.shortPoints(), pa.size(), + CoordModeOrigin ); + } +} + + +/*! + Draws a pixmap at \a (x, y) by copying a part of \a pixmap into + the paint device. + + \a (x, y) specifies the top-left point in the paint device that is + to be drawn onto. \a (sx, sy) specifies the top-left point in \a + pixmap that is to be drawn. The default is (0, 0). + + \a (sw, sh) specifies the size of the pixmap that is to be drawn. + The default, (-1, -1), means all the way to the bottom right of + the pixmap. + + Currently the mask of the pixmap or it's alpha channel are ignored + when painting on a TQPrinter. + + \sa bitBlt(), TQPixmap::setMask() +*/ + +void TQPainter::drawPixmap( int x, int y, const TQPixmap &pixmap, + int sx, int sy, int sw, int sh ) +{ + if ( !isActive() || pixmap.isNull() ) + return; + + // right/bottom + if ( sw < 0 ) + sw = pixmap.width() - sx; + if ( sh < 0 ) + sh = pixmap.height() - sy; + + // Sanity-check clipping + if ( sx < 0 ) { + x -= sx; + sw += sx; + sx = 0; + } + if ( sw + sx > pixmap.width() ) + sw = pixmap.width() - sx; + if ( sy < 0 ) { + y -= sy; + sh += sy; + sy = 0; + } + if ( sh + sy > pixmap.height() ) + sh = pixmap.height() - sy; + + if ( sw <= 0 || sh <= 0 ) + return; + + if ( pdev->x11Screen() != pixmap.x11Screen() ) { + TQPixmap* p = (TQPixmap*) &pixmap; + p->x11SetScreen( pdev->x11Screen() ); + } + + TQPixmap::x11SetDefaultScreen( pixmap.x11Screen() ); + + if ( testf(ExtDev|VxF|WxF) ) { + if ( testf(ExtDev) || txop == TxScale || txop == TxRotShear ) { + if ( sx != 0 || sy != 0 || + sw != pixmap.width() || sh != pixmap.height() ) { + TQPixmap tmp( sw, sh, pixmap.depth() ); + bitBlt( &tmp, 0, 0, &pixmap, sx, sy, sw, sh, CopyROP, TRUE ); + if ( pixmap.mask() ) { + TQBitmap mask( sw, sh ); + bitBlt( &mask, 0, 0, pixmap.mask(), sx, sy, sw, sh, + CopyROP, TRUE ); + tmp.setMask( mask ); + } + drawPixmap( x, y, tmp ); + return; + } + if ( testf(ExtDev) ) { + TQPDevCmdParam param[2]; + TQRect r(x, y, pixmap.width(), pixmap.height()); + param[0].rect = &r; + param[1].pixmap = &pixmap; + if ( !pdev->cmd(TQPaintDevice::PdcDrawPixmap, this, param) || !hd ) + return; + } + if ( txop == TxScale || txop == TxRotShear ) { + TQWMatrix mat( m11(), m12(), + m21(), m22(), + dx(), dy() ); + mat = TQPixmap::trueMatrix( mat, sw, sh ); + TQPixmap pm = pixmap.xForm( mat ); + if ( !pm.mask() && txop == TxRotShear ) { + TQBitmap bm_clip( sw, sh, 1 ); + bm_clip.fill( color1 ); + pm.setMask( bm_clip.xForm(mat) ); + } + map( x, y, &x, &y ); // compute position of pixmap + int dx, dy; + mat.map( 0, 0, &dx, &dy ); + uint save_flags = flags; + flags = IsActive | (save_flags & ClipOn); + drawPixmap( x-dx, y-dy, pm ); + flags = save_flags; + return; + } + } + map( x, y, &x, &y ); + } + + TQBitmap *mask = (TQBitmap *)pixmap.mask(); + bool mono = pixmap.depth() == 1; + + if ( mask && !hasClipping() && pdev != paintEventDevice ) { + if ( mono ) { // needs GCs pen color + bool selfmask = pixmap.data->selfmask; + if ( selfmask ) { + XSetFillStyle( dpy, gc, FillStippled ); + XSetStipple( dpy, gc, pixmap.handle() ); + } else { + XSetFillStyle( dpy, gc, FillOpaqueStippled ); + XSetStipple( dpy, gc, pixmap.handle() ); + XSetClipMask( dpy, gc, mask->handle() ); + XSetClipOrigin( dpy, gc, x-sx, y-sy ); + } + XSetTSOrigin( dpy, gc, x-sx, y-sy ); + XFillRectangle( dpy, hd, gc, x, y, sw, sh ); + XSetTSOrigin( dpy, gc, 0, 0 ); + XSetFillStyle( dpy, gc, FillSolid ); + if ( !selfmask ) { + if ( pdev == paintEventDevice && paintEventClipRegion ) { + x11SetClipRegion( dpy, gc, 0, rendhd, *paintEventClipRegion ); + } else { + x11ClearClipRegion(dpy, gc, 0, rendhd); + } + } + } else { + bitBlt( pdev, x, y, &pixmap, sx, sy, sw, sh, (RasterOp)rop ); + } + return; + } + + TQRegion rgn = crgn; + + if ( mask ) { // pixmap has clip mask + // Implies that clipping is on, either explicit or implicit + // Create a new mask that combines the mask with the clip region + + if ( pdev == paintEventDevice && paintEventClipRegion ) { + if ( hasClipping() ) + rgn = rgn.intersect( *paintEventClipRegion ); + else + rgn = *paintEventClipRegion; + } + + TQBitmap *comb = new TQBitmap( sw, sh ); + comb->detach(); + GC cgc = qt_xget_temp_gc( pixmap.x11Screen(), TRUE ); // get temporary mono GC + XSetForeground( dpy, cgc, 0 ); + XFillRectangle( dpy, comb->handle(), cgc, 0, 0, sw, sh ); + XSetBackground( dpy, cgc, 0 ); + XSetForeground( dpy, cgc, 1 ); + int num; + XRectangle *rects = (XRectangle *)qt_getClipRects( rgn, num ); + XSetClipRectangles( dpy, cgc, -x, -y, rects, num, YXBanded ); + XSetFillStyle( dpy, cgc, FillOpaqueStippled ); + XSetStipple( dpy, cgc, mask->handle() ); + XSetTSOrigin( dpy, cgc, -sx, -sy ); + XFillRectangle( dpy, comb->handle(), cgc, 0, 0, sw, sh ); + XSetTSOrigin( dpy, cgc, 0, 0 ); // restore cgc + XSetFillStyle( dpy, cgc, FillSolid ); + XSetClipMask( dpy, cgc, None ); + mask = comb; // it's deleted below + + XSetClipMask( dpy, gc, mask->handle() ); + XSetClipOrigin( dpy, gc, x, y ); + } + + if ( mono ) { + XSetBackground( dpy, gc, bg_col.pixel(scrn) ); + XSetFillStyle( dpy, gc, FillOpaqueStippled ); + XSetStipple( dpy, gc, pixmap.handle() ); + XSetTSOrigin( dpy, gc, x-sx, y-sy ); + XFillRectangle( dpy, hd, gc, x, y, sw, sh ); + XSetTSOrigin( dpy, gc, 0, 0 ); + XSetFillStyle( dpy, gc, FillSolid ); + } else { +#if !defined(QT_NO_XFTFREETYPE) && !defined(QT_NO_XRENDER) + Picture pict = rendhd ? XftDrawPicture((XftDraw *) rendhd) : None; + TQPixmap *alpha = pixmap.data->alphapm; + + if ( pict && pixmap.x11RenderHandle() && + alpha && alpha->x11RenderHandle()) { + XRenderComposite(dpy, PictOpOver, pixmap.x11RenderHandle(), + alpha->x11RenderHandle(), pict, + sx, sy, sx, sy, x, y, sw, sh); + } else +#endif // !QT_NO_XFTFREETYPE && !QT_NO_XRENDER + { + XCopyArea( dpy, pixmap.handle(), hd, gc, sx, sy, sw, sh, x, y ); + } + } + + if ( mask ) { // restore clipping + XSetClipOrigin( dpy, gc, 0, 0 ); + XSetRegion( dpy, gc, rgn.handle() ); + delete mask; // delete comb, created above + } +} + + +/* Internal, used by drawTiledPixmap */ + +static void drawTile( TQPainter *p, int x, int y, int w, int h, + const TQPixmap &pixmap, int xOffset, int yOffset ) +{ + int yPos, xPos, drawH, drawW, yOff, xOff; + yPos = y; + yOff = yOffset; + while( yPos < y + h ) { + drawH = pixmap.height() - yOff; // Cropping first row + if ( yPos + drawH > y + h ) // Cropping last row + drawH = y + h - yPos; + xPos = x; + xOff = xOffset; + while( xPos < x + w ) { + drawW = pixmap.width() - xOff; // Cropping first column + if ( xPos + drawW > x + w ) // Cropping last column + drawW = x + w - xPos; + p->drawPixmap( xPos, yPos, pixmap, xOff, yOff, drawW, drawH ); + xPos += drawW; + xOff = 0; + } + yPos += drawH; + yOff = 0; + } +} + +#if 0 // see comment in drawTiledPixmap +/* Internal, used by drawTiledPixmap */ + +static void fillTile( TQPixmap *tile, const TQPixmap &pixmap ) +{ + bitBlt( tile, 0, 0, &pixmap, 0, 0, -1, -1, TQt::CopyROP, TRUE ); + int x = pixmap.width(); + while ( x < tile->width() ) { + bitBlt( tile, x,0, tile, 0,0, x,pixmap.height(), TQt::CopyROP, TRUE ); + x *= 2; + } + int y = pixmap.height(); + while ( y < tile->height() ) { + bitBlt( tile, 0,y, tile, 0,0, tile->width(),y, TQt::CopyROP, TRUE ); + y *= 2; + } +} +#endif + +/*! + Draws a tiled \a pixmap in the specified rectangle. + + \a (x, y) specifies the top-left point in the paint device that is + to be drawn onto; with the width and height given by \a w and \a + h. \a (sx, sy) specifies the top-left point in \a pixmap that is + to be drawn. The default is (0, 0). + + Calling drawTiledPixmap() is similar to calling drawPixmap() + several times to fill (tile) an area with a pixmap, but is + potentially much more efficient depending on the underlying window + system. + + \sa drawPixmap() +*/ + +void TQPainter::drawTiledPixmap( int x, int y, int w, int h, + const TQPixmap &pixmap, int sx, int sy ) +{ + int sw = pixmap.width(); + int sh = pixmap.height(); + if (!sw || !sh ) + return; + if ( sx < 0 ) + sx = sw - -sx % sw; + else + sx = sx % sw; + if ( sy < 0 ) + sy = sh - -sy % sh; + else + sy = sy % sh; + /* + Retquirements for optimizing tiled pixmaps: + - not an external device + - not scale or rotshear + - not mono pixmap + - no mask + */ + TQBitmap *mask = (TQBitmap *)pixmap.mask(); + if ( !testf(ExtDev) && txop <= TxTranslate && pixmap.depth() > 1 && + mask == 0 ) { + if ( txop == TxTranslate ) + map( x, y, &x, &y ); + +#if !defined(QT_NO_XFTFREETYPE) && !defined(QT_NO_XRENDER) + Picture pict = rendhd ? XftDrawPicture((XftDraw *) rendhd) : None; + TQPixmap *alpha = pixmap.data->alphapm; + + if (pict && pixmap.x11RenderHandle() && alpha && alpha->x11RenderHandle()) { + // this is essentially drawTile() from above, inlined for + // the XRenderComposite call + int yPos, xPos, drawH, drawW, yOff, xOff; + yPos = y; + yOff = sy; + while( yPos < y + h ) { + drawH = pixmap.height() - yOff; // Cropping first row + if ( yPos + drawH > y + h ) // Cropping last row + drawH = y + h - yPos; + xPos = x; + xOff = sx; + while( xPos < x + w ) { + drawW = pixmap.width() - xOff; // Cropping first column + if ( xPos + drawW > x + w ) // Cropping last column + drawW = x + w - xPos; + XRenderComposite(dpy, PictOpOver, pixmap.x11RenderHandle(), + alpha->x11RenderHandle(), pict, + xOff, yOff, xOff, yOff, xPos, yPos, drawW, drawH); + xPos += drawW; + xOff = 0; + } + yPos += drawH; + yOff = 0; + } + return; + } +#endif // !QT_NO_XFTFREETYPE && !QT_NO_XRENDER + + XSetTile( dpy, gc, pixmap.handle() ); + XSetFillStyle( dpy, gc, FillTiled ); + XSetTSOrigin( dpy, gc, x-sx, y-sy ); + XFillRectangle( dpy, hd, gc, x, y, w, h ); + XSetTSOrigin( dpy, gc, 0, 0 ); + XSetFillStyle( dpy, gc, FillSolid ); + return; + } + +#if 0 + // maybe there'll be point in this again, but for the time all it + // does is make trouble for the postscript code. + if ( sw*sh < 8192 && sw*sh < 16*w*h ) { + int tw = sw; + int th = sh; + while( th * tw < 4096 && ( th < h || tw < w ) ) { + if ( h/th > w/tw ) + th *= 2; + else + tw *= 2; + } + TQPixmap tile( tw, th, pixmap.depth(), TQPixmap::NormalOptim ); + fillTile( &tile, pixmap ); + if ( mask ) { + TQBitmap tilemask( tw, th, TQPixmap::NormalOptim ); + fillTile( &tilemask, *mask ); + tile.setMask( tilemask ); + } + drawTile( this, x, y, w, h, tile, sx, sy ); + } else { + drawTile( this, x, y, w, h, pixmap, sx, sy ); + } +#else + // for now we'll just output the original and let the postscript + // code make what it can of it. qpicture will be unhappy. + drawTile( this, x, y, w, h, pixmap, sx, sy ); +#endif +} + +#if 0 +// +// Generate a string that describes a transformed bitmap. This string is used +// to insert and find bitmaps in the global pixmap cache. +// + +static TQString gen_text_bitmap_key( const TQWMatrix &m, const TQFont &font, + const TQString &str, int pos, int len ) +{ + TQString fk = font.key(); + int sz = 4*2 + len*2 + fk.length()*2 + sizeof(double)*6; + TQByteArray buf(sz); + uchar *p = (uchar *)buf.data(); + *((double*)p)=m.m11(); p+=sizeof(double); + *((double*)p)=m.m12(); p+=sizeof(double); + *((double*)p)=m.m21(); p+=sizeof(double); + *((double*)p)=m.m22(); p+=sizeof(double); + *((double*)p)=m.dx(); p+=sizeof(double); + *((double*)p)=m.dy(); p+=sizeof(double); + TQChar h1( '$' ); + TQChar h2( 'q' ); + TQChar h3( 't' ); + TQChar h4( '$' ); + *((TQChar*)p)=h1; p+=2; + *((TQChar*)p)=h2; p+=2; + *((TQChar*)p)=h3; p+=2; + *((TQChar*)p)=h4; p+=2; + memcpy( (char*)p, (char*)(str.unicode()+pos), len*2 ); p += len*2; + memcpy( (char*)p, (char*)fk.unicode(), fk.length()*2 ); p += fk.length()*2; + return TQString( (TQChar*)buf.data(), buf.size()/2 ); +} + +static TQBitmap *get_text_bitmap( const TQString &key ) +{ + return (TQBitmap*)TQPixmapCache::find( key ); +} + +static void ins_text_bitmap( const TQString &key, TQBitmap *bm ) +{ + if ( !TQPixmapCache::insert(key, bm) ) // cannot insert pixmap + delete bm; +} +#endif + +void qt_draw_transformed_rect( TQPainter *p, int x, int y, int w, int h, bool fill ) +{ + XPoint points[5]; + int xp = x, yp = y; + p->map( xp, yp, &xp, &yp ); + points[0].x = xp; + points[0].y = yp; + xp = x + w; yp = y; + p->map( xp, yp, &xp, &yp ); + points[1].x = xp; + points[1].y = yp; + xp = x + w; yp = y + h; + p->map( xp, yp, &xp, &yp ); + points[2].x = xp; + points[2].y = yp; + xp = x; yp = y + h; + p->map( xp, yp, &xp, &yp ); + points[3].x = xp; + points[3].y = yp; + points[4] = points[0]; + + if ( fill ) + XFillPolygon( p->dpy, p->hd, p->gc, points, 4, Convex, CoordModeOrigin ); + else + XDrawLines( p->dpy, p->hd, p->gc, points, 5, CoordModeOrigin ); +} + +void qt_draw_background( TQPainter *p, int x, int y, int w, int h ) +{ + if (p->testf(TQPainter::ExtDev)) { + if (p->pdev->devType() == TQInternal::Printer) + p->fillRect(x, y, w, h, p->bg_col); + return; + } + XSetForeground( p->dpy, p->gc, p->bg_col.pixel(p->scrn) ); + qt_draw_transformed_rect( p, x, y, w, h, TRUE); + XSetForeground( p->dpy, p->gc, p->cpen.color().pixel(p->scrn) ); +} + +/*! + Draws at most \a len characters of the string \a str at position + \a (x, y). + + \a (x, y) is the base line position. Note that the meaning of \a y + is not the same for the two drawText() varieties. +*/ +void TQPainter::drawText( int x, int y, const TQString &str, int len, TQPainter::TextDirection dir ) +{ + drawText( x, y, str, 0, len, dir ); +} + +/*! + Draws at most \a len characters starting at position \a pos from the + string \a str to position \a (x, y). + + \a (x, y) is the base line position. Note that the meaning of \a y + is not the same for the two drawText() varieties. +*/ +void TQPainter::drawText( int x, int y, const TQString &str, int pos, int len, TQPainter::TextDirection dir ) +{ + if ( !isActive() ) + return; + if (len < 0) + len = str.length() - pos; + if ( len <= 0 || pos >= (int)str.length() ) // empty string + return; + if ( pos + len > (int)str.length() ) + len = str.length() - pos; + + if ( testf(DirtyFont) ) { + updateFont(); + } + + if ( testf(ExtDev) && pdev->devType() != TQInternal::Printer ) { + TQPDevCmdParam param[3]; + TQPoint p(x, y); + TQString string = str.mid( pos, len ); + param[0].point = &p; + param[1].str = &string; + param[2].ival = TQFont::Latin; + if ( !pdev->cmd(TQPaintDevice::PdcDrawText2, this, param) || !hd ) + return; + } + + bool simple = (dir == TQPainter::Auto) && str.simpleText(); + // we can't take the complete string here as we would otherwise + // get quadratic behaviour when drawing long strings in parts. + // we do however need some chars around the part we paint to get arabic shaping correct. + // ### maybe possible to remove after cursor restrictions work in TQRT + int start; + int end; + if ( simple ) { + start = pos; + end = pos+len; + } else { + start = TQMAX( 0, pos - 8 ); + end = TQMIN( (int)str.length(), pos + len + 8 ); + } + TQConstString cstr( str.unicode() + start, end - start ); + pos -= start; + + TQTextEngine engine( cstr.string(), pfont ? pfont->d : cfont.d ); + TQTextLayout layout( &engine ); + + // this is actually what beginLayout does. Inlined here, so we can + // avoid the bidi algorithm if we don't need it. + engine.itemize( simple ? TQTextEngine::NoBidi|TQTextEngine::SingleLine : TQTextEngine::Full|TQTextEngine::SingleLine ); + engine.currentItem = 0; + engine.firstItemInLine = -1; + + if ( dir != Auto ) { + int level = dir == RTL ? 1 : 0; + for ( int i = engine.items.size(); i >= 0; i-- ) + engine.items[i].analysis.bidiLevel = level; + } + + if ( !simple ) { + layout.setBoundary( pos ); + layout.setBoundary( pos + len ); + } + + // small hack to force skipping of unneeded items + start = 0; + while ( engine.items[start].position < pos ) + ++start; + engine.currentItem = start; + layout.beginLine( 0xfffffff ); + end = start; + while ( !layout.atEnd() && layout.currentItem().from() < pos + len ) { + layout.addCurrentItem(); + end++; + } + TQFontMetrics fm(fontMetrics()); + int ascent = fm.ascent(), descent = fm.descent(); + int left, right; + layout.endLine( 0, 0, TQt::SingleLine|TQt::AlignLeft, &ascent, &descent, &left, &right ); + + // do _not_ call endLayout() here, as it would clean up the shaped items and we would do shaping another time + // for painting. + + int textFlags = 0; + if ( cfont.d->underline ) textFlags |= TQt::Underline; + if ( cfont.d->overline ) textFlags |= TQt::Overline; + if ( cfont.d->strikeOut ) textFlags |= TQt::StrikeOut; + + if ( bg_mode == OpaqueMode ) + qt_draw_background( this, x, y-ascent, right-left, ascent+descent+1); + + for ( int i = start; i < end; i++ ) { + TQTextItem ti; + ti.item = i; + ti.engine = &engine; + + drawTextItem( x, y - ascent, ti, textFlags ); + } + layout.d = 0; +} + + +/*! \internal + Draws the text item \a ti at position \a (x, y ). + + This method ignores the painters background mode and + color. drawText and qt_format_text have to do it themselves, as + only they know the extents of the complete string. + + It ignores the font set on the painter as the text item has one of its own. + + The underline and strikeout parameters of the text items font are + ignored aswell. You'll need to pass in the correct flags to get + underlining and strikeout. +*/ +void TQPainter::drawTextItem( int x, int y, const TQTextItem &ti, int textFlags ) +{ + if ( testf(ExtDev) ) { + TQPDevCmdParam param[2]; + TQPoint p(x, y); + param[0].point = &p; + param[1].textItem = &ti; + bool retval = pdev->cmd(TQPaintDevice::PdcDrawTextItem, this, param); + if ( !retval || !hd ) + return; + } + + TQTextEngine *engine = ti.engine; + TQScriptItem *si = &engine->items[ti.item]; + + engine->shape( ti.item ); + TQFontEngine *fe = si->fontEngine; + assert( fe != 0 ); + + x += si->x; + y += si->y; + + fe->draw( this, x, y, engine, si, textFlags ); +} + +#if QT_VERSION >= 0x040000 +#error "remove current position and associated methods" +#endif +/*! + \obsolete + Returns the current position of the pen. + + \sa moveTo() + */ +TQPoint TQPainter::pos() const +{ + return curPt; +} diff --git a/src/kernel/qpalette.cpp b/src/kernel/qpalette.cpp new file mode 100644 index 000000000..31e43672b --- /dev/null +++ b/src/kernel/qpalette.cpp @@ -0,0 +1,1224 @@ +/**************************************************************************** +** +** Implementation of TQColorGroup and TQPalette classes +** +** Created : 950323 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpalette.h" + +#ifndef QT_NO_PALETTE +#include "qdatastream.h" +#include "qcleanuphandler.h" + +/***************************************************************************** + TQColorGroup member functions + *****************************************************************************/ + +/*! + \class TQColorGroup qpalette.h + \brief The TQColorGroup class contains a group of widget colors. + + \ingroup appearance + \ingroup graphics + \ingroup images + + A color group contains a group of colors used by widgets for + drawing themselves. We recommend that widgets use color group + roles such as "foreground" and "base" rather than literal colors + like "red" or "turquoise". The color roles are enumerated and + defined in the \l ColorRole documentation. + + The most common use of TQColorGroup is like this: + + \code + TQPainter p; + ... + p.setPen( colorGroup().foreground() ); + p.drawLine( ... ) + \endcode + + It is also possible to modify color groups or create new color + groups from scratch. + + The color group class can be created using three different + constructors or by modifying one supplied by TQt. The default + constructor creates an all-black color group, which can then be + modified using set functions; there's also a constructor for + specifying all the color group colors. And there is also a copy + constructor. + + We strongly recommend using a system-supplied color group and + modifying that as necessary. + + You modify a color group by calling the access functions + setColor() and setBrush(), depending on whether you want a pure + color or a pixmap pattern. + + There are also corresponding color() and brush() getters, and a + commonly used convenience function to get each ColorRole: + background(), foreground(), base(), etc. + + \sa TQColor TQPalette TQWidget::colorGroup() +*/ + + +/*! + \enum TQColorGroup::ColorRole + + The ColorRole enum defines the different symbolic color roles used + in current GUIs. + + The central roles are: + + \value Background general background color. + + \value Foreground general foreground color. + + \value Base used as background color for text entry widgets, for example; + usually white or another light color. + + \value Text the foreground color used with \c Base. Usually this + is the same as the \c Foreground, in which case it must provide good + contrast with \c Background and \c Base. + + \value Button general button background color in which buttons need a + background different from \c Background, as in the Macintosh style. + + \value ButtonText a foreground color used with the \c Button color. + + There are some color roles used mostly for 3D bevel and shadow + effects: + + \value Light lighter than \c Button color. + + \value Midlight between \c Button and \c Light. + + \value Dark darker than \c Button. + + \value Mid between \c Button and \c Dark. + + \value Shadow a very dark color. + By default, the shadow color is \c TQt::black. + + All of these are normally derived from \c Background and used in + ways that depend on that relationship. For example, buttons depend + on it to make the bevels look attractive, and Motif scroll bars + depend on \c Mid to be slightly different from \c Background. + + Selected (marked) items have two roles: + + \value Highlight a color to indicate a selected item or the + current item. By default, the highlight color is \c TQt::darkBlue. + + \value HighlightedText a text color that contrasts with \c Highlight. + By default, the highlighted text color is \c TQt::white. + + Finally, there is a special role for text that needs to be + drawn where \c Text or \c Foreground would give poor contrast, + such as on pressed push buttons: + + \value BrightText a text color that is very different from \c + Foreground and contrasts well with e.g. \c Dark. + + \value Link a text color used for unvisited hyperlinks. + By default, the link color is \c TQt::blue. + + \value LinkVisited a text color used for already visited hyperlinks. + By default, the linkvisited color is \c TQt::magenta. + + \value NColorRoles Internal. + + Note that text colors can be used for things other than just + words; text colors are \e usually used for text, but it's tquite + common to use the text color roles for lines, icons, etc. + + This image shows most of the color roles in use: + \img palette.png Color Roles +*/ + + +class TQColorGroupPrivate : public TQShared +{ +public: + TQBrush br[TQColorGroup::NColorRoles]; + TQColorGroupPrivate* detach() { + if ( count > 1 ) { + deref(); + TQColorGroupPrivate* d = new TQColorGroupPrivate; + for (int i=0; ibr[i] = br[i]; + return d; + } + return this; + } +}; + +/*! + Constructs a color group with all colors set to black. +*/ + +TQColorGroup::TQColorGroup() +{ + static TQColorGroupPrivate* defColorGroupData = 0; + if ( !defColorGroupData ) { + static TQSharedCleanupHandler defColorGroupCleanup; + defColorGroupData = new TQColorGroupPrivate; + defColorGroupCleanup.set( &defColorGroupData ); + } + d = defColorGroupData; + br = d->br; + d->ref(); +} + +/*! + Constructs a color group that is an independent copy of \a other. +*/ +TQColorGroup::TQColorGroup( const TQColorGroup& other ) +{ + d = other.d; + d->ref(); + br = d->br; +} + +/*! + Copies the colors of \a other to this color group. +*/ +TQColorGroup& TQColorGroup::operator =(const TQColorGroup& other) +{ + if ( d != other.d ) { + if ( d->deref() ) + delete d; + d = other.d; + br = d->br; + d->ref(); + } + return *this; +} + +static TQColor qt_mix_colors( TQColor a, TQColor b) +{ + return TQColor( (a.red() + b.red()) / 2, (a.green() + b.green()) / 2, (a.blue() + b.blue()) / 2 ); +} + + +/*! + Constructs a color group. You can pass either brushes, pixmaps or + plain colors for \a foreground, \a button, \a light, \a dark, \a + mid, \a text, \a bright_text, \a base and \a background. + + \sa TQBrush +*/ + TQColorGroup::TQColorGroup( const TQBrush &foreground, const TQBrush &button, + const TQBrush &light, const TQBrush &dark, + const TQBrush &mid, const TQBrush &text, + const TQBrush &bright_text, const TQBrush &base, + const TQBrush &background) +{ + d = new TQColorGroupPrivate; + br = d->br; + br[Foreground] = foreground; + br[Button] = button; + br[Light] = light; + br[Dark] = dark; + br[Mid] = mid; + br[Text] = text; + br[BrightText] = bright_text; + br[ButtonText] = text; + br[Base] = base; + br[Background] = background; + br[Midlight] = qt_mix_colors( br[Button].color(), br[Light].color() ); + br[Shadow] = TQt::black; + br[Highlight] = TQt::darkBlue; + br[HighlightedText] = TQt::white; + br[Link] = TQt::blue; + br[LinkVisited] = TQt::magenta; +} + + +/*!\obsolete + + Constructs a color group with the specified colors. The button + color will be set to the background color. +*/ + +TQColorGroup::TQColorGroup( const TQColor &foreground, const TQColor &background, + const TQColor &light, const TQColor &dark, + const TQColor &mid, + const TQColor &text, const TQColor &base ) +{ + d = new TQColorGroupPrivate; + br = d->br; + br[Foreground] = TQBrush(foreground); + br[Button] = TQBrush(background); + br[Light] = TQBrush(light); + br[Dark] = TQBrush(dark); + br[Mid] = TQBrush(mid); + br[Text] = TQBrush(text); + br[BrightText] = br[Light]; + br[ButtonText] = br[Text]; + br[Base] = TQBrush(base); + br[Background] = TQBrush(background); + br[Midlight] = qt_mix_colors( br[Button].color(), br[Light].color() ); + br[Shadow] = TQt::black; + br[Highlight] = TQt::darkBlue; + br[HighlightedText] = TQt::white; + br[Link] = TQt::blue; + br[LinkVisited] = TQt::magenta; +} + +/*! + Destroys the color group. +*/ + +TQColorGroup::~TQColorGroup() +{ + if ( d->deref() ) + delete d; +} + +/*! + Returns the color that has been set for color role \a r. + + \sa brush() ColorRole + */ +const TQColor &TQColorGroup::color( ColorRole r ) const +{ + return br[r].color(); +} + +/*! + Returns the brush that has been set for color role \a r. + + \sa color() setBrush() ColorRole +*/ +const TQBrush &TQColorGroup::brush( ColorRole r ) const +{ + return br[r]; +} + +/*! + Sets the brush used for color role \a r to a solid color \a c. + + \sa brush() setColor() ColorRole +*/ +void TQColorGroup::setColor( ColorRole r, const TQColor &c ) +{ + setBrush( r, TQBrush(c) ); +} + +/*! + Sets the brush used for color role \a r to \a b. + + \sa brush() setColor() ColorRole +*/ +void TQColorGroup::setBrush( ColorRole r, const TQBrush &b ) +{ + d = d->detach(); + br = d->br; + br[r] = b; +} + + +/*! + \fn const TQColor & TQColorGroup::foreground() const + + Returns the foreground color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::button() const + + Returns the button color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::light() const + + Returns the light color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor& TQColorGroup::midlight() const + + Returns the midlight color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::dark() const + + Returns the dark color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::mid() const + + Returns the mid color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::text() const + + Returns the text foreground color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::brightText() const + + Returns the bright text foreground color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::buttonText() const + + Returns the button text foreground color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::base() const + + Returns the base color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::background() const + + Returns the background color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::shadow() const + + Returns the shadow color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::highlight() const + + Returns the highlight color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::highlightedText() const + + Returns the highlighted text color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::link() const + + Returns the unvisited link text color of the color group. + + \sa ColorRole +*/ + +/*! + \fn const TQColor & TQColorGroup::linkVisited() const + + Returns the visited link text color of the color group. + + \sa ColorRole +*/ + +/*! + \fn bool TQColorGroup::operator!=( const TQColorGroup &g ) const + + Returns TRUE if this color group is different from \a g; otherwise + returns FALSE. + + \sa operator!=() +*/ + +/*! + Returns TRUE if this color group is equal to \a g; otherwise + returns FALSE. + + \sa operator==() +*/ + +bool TQColorGroup::operator==( const TQColorGroup &g ) const +{ + if ( d == g.d ) + return TRUE; + for( int r = 0 ; r < NColorRoles ; r++ ) + if ( br[r] != g.br[r] ) + return FALSE; + return TRUE; +} + + +/***************************************************************************** + TQPalette member functions + *****************************************************************************/ + +/*! + \class TQPalette qpalette.h + + \brief The TQPalette class contains color groups for each widget state. + + \ingroup appearance + \ingroup shared + \ingroup graphics + \ingroup images + \mainclass + + A palette consists of three color groups: \e active, \e disabled, + and \e inactive. All widgets contain a palette, and all widgets in + TQt use their palette to draw themselves. This makes the user + interface easily configurable and easier to keep consistent. + + If you create a new widget we strongly recommend that you use the + colors in the palette rather than hard-coding specific colors. + + The color groups: + \list + \i The active() group is used for the window that has keyboard focus. + \i The inactive() group is used for other windows. + \i The disabled() group is used for widgets (not windows) that are + disabled for some reason. + \endlist + + Both active and inactive windows can contain disabled widgets. + (Disabled widgets are often called \e inaccessible or \e{grayed + out}.) + + In Motif style, active() and inactive() look the same. In Windows + 2000 style and Macintosh Platinum style, the two styles look + slightly different. + + There are setActive(), setInactive(), and setDisabled() functions + to modify the palette. (TQt also supports a normal() group; this is + an obsolete alias for active(), supported for backwards + compatibility.) + + Colors and brushes can be set for particular roles in any of a + palette's color groups with setColor() and setBrush(). + + You can copy a palette using the copy constructor and test to see + if two palettes are \e identical using isCopyOf(). + + \sa TQApplication::setPalette(), TQWidget::setPalette(), TQColorGroup, TQColor +*/ + +/*! + \enum TQPalette::ColorGroup + + \value Disabled + \value Active + \value Inactive + \value NColorGroups + \value Normal synonym for Active +*/ + +/*! + \obsolete + + \fn const TQColorGroup &TQPalette::normal() const + + Returns the active color group. Use active() instead. + + \sa setActive() active() +*/ + +/*! + \obsolete + + \fn void TQPalette::setNormal( const TQColorGroup & cg ) + + Sets the active color group to \a cg. Use setActive() instead. + + \sa setActive() active() +*/ + + +static int palette_count = 1; + +/*! + Constructs a palette that consists of color groups with only black + colors. +*/ + +TQPalette::TQPalette() +{ + static TQPalData *defPalData = 0; + if ( !defPalData ) { // create common palette data + defPalData = new TQPalData; // for the default palette + static TQSharedCleanupHandler defPalCleanup; + defPalCleanup.set( &defPalData ); + defPalData->ser_no = palette_count++; + } + data = defPalData; + data->ref(); +} + +/*!\obsolete + Constructs a palette from the \a button color. The other colors are + automatically calculated, based on this color. Background will be + the button color as well. +*/ + +TQPalette::TQPalette( const TQColor &button ) +{ + data = new TQPalData; + Q_CHECK_PTR( data ); + data->ser_no = palette_count++; + TQColor bg = button, btn = button, fg, base, disfg; + int h, s, v; + bg.hsv( &h, &s, &v ); + if ( v > 128 ) { // light background + fg = TQt::black; + base = TQt::white; + disfg = TQt::darkGray; + } else { // dark background + fg = TQt::white; + base = TQt::black; + disfg = TQt::darkGray; + } + data->active = TQColorGroup( fg, btn, btn.light(150), btn.dark(), + btn.dark(150), fg, TQt::white, base, bg ); + data->disabled = TQColorGroup( disfg, btn, btn.light(150), btn.dark(), + btn.dark(150), disfg, TQt::white, base, bg ); + data->inactive = data->active; +} + +/*! + Constructs a palette from a \a button color and a \a background. + The other colors are automatically calculated, based on these + colors. +*/ + +TQPalette::TQPalette( const TQColor &button, const TQColor &background ) +{ + data = new TQPalData; + Q_CHECK_PTR( data ); + data->ser_no = palette_count++; + TQColor bg = background, btn = button, fg, base, disfg; + int h, s, v; + bg.hsv( &h, &s, &v ); + if ( v > 128 ) { // light background + fg = TQt::black; + base = TQt::white; + disfg = TQt::darkGray; + } else { // dark background + fg = TQt::white; + base = TQt::black; + disfg = TQt::darkGray; + } + data->active = TQColorGroup( fg, btn, btn.light(150), btn.dark(), + btn.dark(150), fg, TQt::white, base, bg ); + data->disabled = TQColorGroup( disfg, btn, btn.light(150), btn.dark(), + btn.dark(150), disfg, TQt::white, base, bg ); + data->inactive = data->active; +} + +/*! + Constructs a palette that consists of the three color groups \a + active, \a disabled and \a inactive. See the \link #details + Detailed Description\endlink for definitions of the color groups + and \l TQColorGroup::ColorRole for definitions of each color role + in the three groups. + + \sa TQColorGroup TQColorGroup::ColorRole TQPalette +*/ + +TQPalette::TQPalette( const TQColorGroup &active, const TQColorGroup &disabled, + const TQColorGroup &inactive ) +{ + data = new TQPalData; + Q_CHECK_PTR( data ); + data->ser_no = palette_count++; + data->active = active; + data->disabled = disabled; + data->inactive = inactive; +} + +/*! + Constructs a copy of \a p. + + This constructor is fast (it uses copy-on-write). +*/ + +TQPalette::TQPalette( const TQPalette &p ) +{ + data = p.data; + data->ref(); +} + +/*! + Destroys the palette. +*/ + +TQPalette::~TQPalette() +{ + if ( data->deref() ) + delete data; +} + +/*! + Assigns \a p to this palette and returns a reference to this + palette. + + This is fast (it uses copy-on-write). + + \sa copy() +*/ + +TQPalette &TQPalette::operator=( const TQPalette &p ) +{ + p.data->ref(); + if ( data->deref() ) + delete data; + data = p.data; + return *this; +} + + +/*! + Returns the color in color group \a gr, used for color role \a r. + + \sa brush() setColor() TQColorGroup::ColorRole +*/ +const TQColor &TQPalette::color( ColorGroup gr, TQColorGroup::ColorRole r ) const +{ + return directBrush( gr, r ).color(); +} + +/*! + Returns the brush in color group \a gr, used for color role \a r. + + \sa color() setBrush() TQColorGroup::ColorRole +*/ +const TQBrush &TQPalette::brush( ColorGroup gr, TQColorGroup::ColorRole r ) const +{ + return directBrush( gr, r ); +} + +/*! + Sets the brush in color group \a gr, used for color role \a r, to + the solid color \a c. + + \sa setBrush() color() TQColorGroup::ColorRole +*/ +void TQPalette::setColor( ColorGroup gr, TQColorGroup::ColorRole r, + const TQColor &c) +{ + setBrush( gr, r, TQBrush(c) ); +} + +/*! + Sets the brush in color group \a gr, used for color role \a r, to + \a b. + + \sa brush() setColor() TQColorGroup::ColorRole +*/ +void TQPalette::setBrush( ColorGroup gr, TQColorGroup::ColorRole r, + const TQBrush &b) +{ + detach(); + data->ser_no = palette_count++; + directSetBrush( gr, r, b); +} + +/*! + \overload + + Sets the brush color used for color role \a r to color \a c in all + three color groups. + + \sa color() setBrush() TQColorGroup::ColorRole +*/ +void TQPalette::setColor( TQColorGroup::ColorRole r, const TQColor &c ) +{ + setBrush( r, TQBrush(c) ); +} + +/*! + \overload + + Sets the brush in for color role \a r in all three color groups to + \a b. + + \sa brush() setColor() TQColorGroup::ColorRole active() inactive() disabled() +*/ +void TQPalette::setBrush( TQColorGroup::ColorRole r, const TQBrush &b ) +{ + detach(); + data->ser_no = palette_count++; + directSetBrush( Active, r, b ); + directSetBrush( Disabled, r, b ); + directSetBrush( Inactive, r, b ); +} + + +/*! + Returns a deep copy of this palette. + + \warning This is slower than the copy constructor and assignment + operator and offers no benefits. +*/ + +TQPalette TQPalette::copy() const +{ + TQPalette p( data->active, data->disabled, data->inactive ); + return p; +} + + +/*! + Detaches this palette from any other TQPalette objects with which + it might implicitly share TQColorGroup objects. In essence, does + the copying part of copy-on-write. + + Calling this should generally not be necessary; TQPalette calls it + itself when necessary. +*/ + +void TQPalette::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + +/*! + \fn const TQColorGroup & TQPalette::disabled() const + + Returns the disabled color group of this palette. + + \sa TQColorGroup, setDisabled(), active(), inactive() +*/ + +/*! + Sets the \c Disabled color group to \a g. + + \sa disabled() setActive() setInactive() +*/ + +void TQPalette::setDisabled( const TQColorGroup &g ) +{ + detach(); + data->ser_no = palette_count++; + data->disabled = g; +} + +/*! + \fn const TQColorGroup & TQPalette::active() const + + Returns the active color group of this palette. + + \sa TQColorGroup, setActive(), inactive(), disabled() +*/ + +/*! + Sets the \c Active color group to \a g. + + \sa active() setDisabled() setInactive() TQColorGroup +*/ + +void TQPalette::setActive( const TQColorGroup &g ) +{ + detach(); + data->ser_no = palette_count++; + data->active = g; +} + +/*! + \fn const TQColorGroup & TQPalette::inactive() const + + Returns the inactive color group of this palette. + + \sa TQColorGroup, setInactive(), active(), disabled() +*/ + +/*! + Sets the \c Inactive color group to \a g. + + \sa active() setDisabled() setActive() TQColorGroup +*/ + +void TQPalette::setInactive( const TQColorGroup &g ) +{ + detach(); + data->ser_no = palette_count++; + data->inactive = g; +} + + +/*! + \fn bool TQPalette::operator!=( const TQPalette &p ) const + + Returns TRUE (slowly) if this palette is different from \a p; + otherwise returns FALSE (usually tquickly). +*/ + +/*! + Returns TRUE (usually tquickly) if this palette is equal to \a p; + otherwise returns FALSE (slowly). +*/ + +bool TQPalette::operator==( const TQPalette &p ) const +{ + return data->active == p.data->active && + data->disabled == p.data->disabled && + data->inactive == p.data->inactive; +} + + +/*! + \fn int TQPalette::serialNumber() const + + Returns a number that uniquely identifies this TQPalette object. + The serial number is intended for caching. Its value may not be + used for anything other than equality testing. + + Note that TQPalette uses copy-on-write, and the serial number + changes during the lazy copy operation (detach()), not during a + shallow copy (copy constructor or assignment). + + \sa TQPixmap TQPixmapCache TQCache +*/ + + +/***************************************************************************** + TQColorGroup/TQPalette stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +/*! + \relates TQColorGroup + + Writes color group, \a g to the stream \a s. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQColorGroup &g ) +{ + if ( s.version() == 1 ) { + // TQt 1.x + s << g.foreground() + << g.background() + << g.light() + << g.dark() + << g.mid() + << g.text() + << g.base(); + } else { + int max = TQColorGroup::NColorRoles; + if ( s.version() <= 3) // TQt 2.x + max = 14; + + for( int r = 0 ; r < max ; r++ ) + s << g.brush( (TQColorGroup::ColorRole)r); + } + return s; +} + +/*! + \related TQColorGroup + + Reads a color group from the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQColorGroup &g ) +{ + if ( s.version() == 1 ) { + // TQt 1.x + TQColor fg, bg, light, dark, mid, text, base; + s >> fg >> bg >> light >> dark >> mid >> text >> base; + TQPalette p( bg ); + TQColorGroup n( p.active() ); + n.setColor( TQColorGroup::Foreground, fg ); + n.setColor( TQColorGroup::Light, light ); + n.setColor( TQColorGroup::Dark, dark ); + n.setColor( TQColorGroup::Mid, mid ); + n.setColor( TQColorGroup::Text, text ); + n.setColor( TQColorGroup::Base, base ); + g = n; + } else { + int max = TQColorGroup::NColorRoles; + if (s.version() <= 3) // TQt 2.x + max = 14; + + TQBrush tmp; + for( int r = 0 ; r < max; r++ ) { + s >> tmp; + g.setBrush( (TQColorGroup::ColorRole)r, tmp); + } + } + return s; +} + + +/*! + \relates TQPalette + + Writes the palette, \a p to the stream \a s and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQPalette &p ) +{ + return s << p.active() + << p.disabled() + << p.inactive(); +} + + +static void readV1ColorGroup( TQDataStream &s, TQColorGroup &g, + TQPalette::ColorGroup r ) +{ + TQColor fg, bg, light, dark, mid, text, base; + s >> fg >> bg >> light >> dark >> mid >> text >> base; + TQPalette p( bg ); + TQColorGroup n; + switch ( r ) { + case TQPalette::Disabled: + n = p.disabled(); + break; + case TQPalette::Inactive: + n = p.inactive(); + break; + default: + n = p.active(); + break; + } + n.setColor( TQColorGroup::Foreground, fg ); + n.setColor( TQColorGroup::Light, light ); + n.setColor( TQColorGroup::Dark, dark ); + n.setColor( TQColorGroup::Mid, mid ); + n.setColor( TQColorGroup::Text, text ); + n.setColor( TQColorGroup::Base, base ); + g = n; +} + + +/*! + \relates TQPalette + + Reads a palette from the stream, \a s into the palette \a p, and + returns a reference to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQPalette &p ) +{ + TQColorGroup active, disabled, inactive; + if ( s.version() == 1 ) { + readV1ColorGroup( s, active, TQPalette::Active ); + readV1ColorGroup( s, disabled, TQPalette::Disabled ); + readV1ColorGroup( s, inactive, TQPalette::Inactive ); + } else { + s >> active >> disabled >> inactive; + } + TQPalette newpal( active, disabled, inactive ); + p = newpal; + return s; +} +#endif //QT_NO_DATASTREAM + +/*! + Returns TRUE if this palette and \a p are copies of each other, + i.e. one of them was created as a copy of the other and neither + was subsequently modified; otherwise returns FALSE. This is much + stricter than equality. + + \sa operator=() operator==() +*/ + +bool TQPalette::isCopyOf( const TQPalette & p ) +{ + return data && data == p.data; +} + +const TQBrush &TQPalette::directBrush( ColorGroup gr, TQColorGroup::ColorRole r ) const +{ + if ( (uint)gr > (uint)TQPalette::NColorGroups ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPalette::directBrush: colorGroup(%i) out of range", gr ); +#endif + return data->active.br[TQColorGroup::Foreground]; + } + if ( (uint)r >= (uint)TQColorGroup::NColorRoles ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPalette::directBrush: colorRole(%i) out of range", r ); +#endif + return data->active.br[TQColorGroup::Foreground]; + } + switch( gr ) { + case Active: + return data->active.br[r]; + //break; + case Disabled: + return data->disabled.br[r]; + //break; + case Inactive: + return data->inactive.br[r]; + //break; + default: + break; + } +#if defined(QT_CHECK_RANGE) + qWarning( "TQPalette::directBrush: colorGroup(%i) internal error", gr ); +#endif + return data->active.br[TQColorGroup::Foreground]; // Satisfy compiler +} + +void TQPalette::directSetBrush( ColorGroup gr, TQColorGroup::ColorRole r, const TQBrush& b) +{ + if ( (uint)gr > (uint)TQPalette::NColorGroups ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPalette::directBrush: colorGroup(%i) out of range", gr ); +#endif + return; + } + if ( (uint)r >= (uint)TQColorGroup::NColorRoles ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPalette::directBrush: colorRole(%i) out of range", r ); +#endif + return; + } + switch( gr ) { + case Active: + data->active.setBrush(r,b); + break; + case Disabled: + data->disabled.setBrush(r,b); + break; + case Inactive: + data->inactive.setBrush(r,b); + break; + default: +#if defined(QT_CHECK_RANGE) + qWarning( "TQPalette::directBrush: colorGroup(%i) internal error", gr ); +#endif + break; + } +} + + +/*!\internal*/ +TQColorGroup::ColorRole TQPalette::foregroundRoleFromMode( TQt::BackgroundMode mode ) +{ + switch (mode) { + case TQt::PaletteButton: + return TQColorGroup::ButtonText; + case TQt::PaletteBase: + return TQColorGroup::Text; + case TQt::PaletteDark: + case TQt::PaletteShadow: + return TQColorGroup::Light; + case TQt::PaletteHighlight: + return TQColorGroup::HighlightedText; + case TQt::PaletteBackground: + default: + return TQColorGroup::Foreground; + } +} + +/*!\internal*/ +TQColorGroup::ColorRole TQPalette::backgroundRoleFromMode( TQt::BackgroundMode mode) +{ + switch (mode) { + case TQt::PaletteForeground: + return TQColorGroup::Foreground; + case TQt::PaletteButton: + return TQColorGroup::Button; + case TQt::PaletteLight: + return TQColorGroup::Light; + case TQt::PaletteMidlight: + return TQColorGroup::Midlight; + case TQt::PaletteDark: + return TQColorGroup::Dark; + case TQt::PaletteMid: + return TQColorGroup::Mid; + case TQt::PaletteText: + return TQColorGroup::Text; + case TQt::PaletteBrightText: + return TQColorGroup::BrightText; + case TQt::PaletteButtonText: + return TQColorGroup::ButtonText; + case TQt::PaletteBase: + return TQColorGroup::Base; + case TQt::PaletteShadow: + return TQColorGroup::Shadow; + case TQt::PaletteHighlight: + return TQColorGroup::Highlight; + case TQt::PaletteHighlightedText: + return TQColorGroup::HighlightedText; + case TQt::PaletteLink: + return TQColorGroup::Link; + case TQt::PaletteLinkVisited: + return TQColorGroup::LinkVisited; + case TQt::PaletteBackground: + default: + return TQColorGroup::Background; + } +} + +#endif // QT_NO_PALETTE diff --git a/src/kernel/qpalette.h b/src/kernel/qpalette.h new file mode 100644 index 000000000..6a5a3b47c --- /dev/null +++ b/src/kernel/qpalette.h @@ -0,0 +1,189 @@ +/**************************************************************************** +** +** Definition of TQColorGroup and TQPalette classes +** +** Created : 950323 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPALETTE_H +#define TQPALETTE_H + +#ifndef QT_H +#include "qwindowdefs.h" +#include "qcolor.h" +#include "qshared.h" +#include "qbrush.h" // TQColor->TQBrush conversion +#endif // QT_H + +#ifndef QT_NO_PALETTE + +class TQColorGroupPrivate; + +class Q_EXPORT TQColorGroup +{ +public: + TQColorGroup(); + TQColorGroup( const TQColor &foreground, const TQColor &button, + const TQColor &light, const TQColor &dark, const TQColor &mid, + const TQColor &text, const TQColor &base ); + TQColorGroup( const TQBrush &foreground, const TQBrush &button, + const TQBrush &light, const TQBrush &dark, const TQBrush &mid, + const TQBrush &text, const TQBrush &bright_text, + const TQBrush &base, const TQBrush &background); + TQColorGroup( const TQColorGroup & ); + + ~TQColorGroup(); + + TQColorGroup& operator =(const TQColorGroup&); + + // Do not change the order, the serialization format depends on it + enum ColorRole { Foreground, Button, Light, Midlight, Dark, Mid, + Text, BrightText, ButtonText, Base, Background, Shadow, + Highlight, HighlightedText, Link, LinkVisited, + NColorRoles }; + + const TQColor &color( ColorRole ) const; + const TQBrush &brush( ColorRole ) const; + void setColor( ColorRole, const TQColor & ); + void setBrush( ColorRole, const TQBrush & ); + + const TQColor &foreground() const { return br[Foreground].color(); } + const TQColor &button() const { return br[Button].color(); } + const TQColor &light() const { return br[Light].color(); } + const TQColor &dark() const { return br[Dark].color(); } + const TQColor &mid() const { return br[Mid].color(); } + const TQColor &text() const { return br[Text].color(); } + const TQColor &base() const { return br[Base].color(); } + const TQColor &background() const { return br[Background].color(); } + + const TQColor &midlight() const { return br[Midlight].color(); } + const TQColor &brightText() const { return br[BrightText].color(); } + const TQColor &buttonText() const { return br[ButtonText].color(); } + const TQColor &shadow() const { return br[Shadow].color(); } + const TQColor &highlight() const { return br[Highlight].color(); } + const TQColor &highlightedText() const{return br[HighlightedText].color(); } + const TQColor &link() const { return br[Link].color(); } + const TQColor &linkVisited() const { return br[LinkVisited].color(); } + + bool operator==( const TQColorGroup &g ) const; + bool operator!=( const TQColorGroup &g ) const + { return !(operator==(g)); } + +private: + TQBrush *br; + TQColorGroupPrivate * d; + + friend class TQPalette; +}; + + +class Q_EXPORT TQPalette +{ +public: + TQPalette(); + TQPalette( const TQColor &button ); + TQPalette( const TQColor &button, const TQColor &background ); + TQPalette( const TQColorGroup &active, const TQColorGroup &disabled, + const TQColorGroup &inactive ); + TQPalette( const TQPalette & ); + ~TQPalette(); + TQPalette &operator=( const TQPalette & ); + + enum ColorGroup { Disabled, Active, Inactive, NColorGroups, Normal=Active }; + + const TQColor &color( ColorGroup, TQColorGroup::ColorRole ) const; + const TQBrush &brush( ColorGroup, TQColorGroup::ColorRole ) const; + void setColor( ColorGroup, TQColorGroup::ColorRole, const TQColor & ); + void setBrush( ColorGroup, TQColorGroup::ColorRole, const TQBrush & ); + + void setColor( TQColorGroup::ColorRole, const TQColor & ); + void setBrush( TQColorGroup::ColorRole, const TQBrush & ); + + TQPalette copy() const; + + const TQColorGroup &active() const { return data->active; } + const TQColorGroup &disabled() const { return data->disabled; } + const TQColorGroup &inactive() const { return data->inactive; } +#ifndef QT_NO_COMPAT + const TQColorGroup &normal() const { return active(); } +#endif + + void setActive( const TQColorGroup & ); + void setDisabled( const TQColorGroup & ); + void setInactive( const TQColorGroup & ); +#ifndef QT_NO_COMPAT + void setNormal( const TQColorGroup & cg ) { setActive(cg); } +#endif + + bool operator==( const TQPalette &p ) const; + bool operator!=( const TQPalette &p ) const + { return !(operator==(p)); } + bool isCopyOf( const TQPalette & ); + + int serialNumber() const { return data->ser_no; } + + + static TQColorGroup::ColorRole foregroundRoleFromMode( TQt::BackgroundMode mode ); + static TQColorGroup::ColorRole backgroundRoleFromMode( TQt::BackgroundMode mode); + +private: + void detach(); + const TQBrush &directBrush( ColorGroup, TQColorGroup::ColorRole ) const; + void directSetBrush( ColorGroup, TQColorGroup::ColorRole, const TQBrush& ); + + struct TQPalData : public TQShared { + TQColorGroup disabled; + TQColorGroup active; + int ser_no; + TQColorGroup inactive; + } *data; +}; + + +/***************************************************************************** + TQColorGroup/TQPalette stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQColorGroup & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQColorGroup & ); + +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQPalette & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQPalette & ); +#endif // QT_NO_DATASTREAM + +#endif // QT_NO_PALETTE +#endif // TQPALETTE_H diff --git a/src/kernel/qpen.h b/src/kernel/qpen.h new file mode 100644 index 000000000..677eec76b --- /dev/null +++ b/src/kernel/qpen.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Definition of TQPen class +** +** Created : 940112 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPEN_H +#define TQPEN_H + +#ifndef QT_H +#include "qcolor.h" +#include "qshared.h" +#endif // QT_H + + +class Q_EXPORT TQPen: public TQt +{ +public: + TQPen(); + TQPen( PenStyle ); + TQPen( const TQColor &color, uint width=0, PenStyle style=SolidLine ); + TQPen( const TQColor &cl, uint w, PenStyle s, PenCapStyle c, PenJoinStyle j); + TQPen( const TQPen & ); + ~TQPen(); + TQPen &operator=( const TQPen & ); + + PenStyle style() const { return data->style; } + void setStyle( PenStyle ); + uint width() const { return data->width; } + void setWidth( uint ); + const TQColor &color() const { return data->color; } + void setColor( const TQColor & ); + PenCapStyle capStyle() const; + void setCapStyle( PenCapStyle ); + PenJoinStyle joinStyle() const; + void setJoinStyle( PenJoinStyle ); + + bool operator==( const TQPen &p ) const; + bool operator!=( const TQPen &p ) const + { return !(operator==(p)); } + +private: + friend class TQPainter; +#ifdef Q_WS_WIN + friend class TQFontEngineWin; +#endif + + TQPen copy() const; + void detach(); + void init( const TQColor &, uint, uint ); + struct TQPenData : public TQShared { // pen data + PenStyle style; + uint width; + TQColor color; + Q_UINT16 linest; + } *data; +}; + + +/***************************************************************************** + TQPen stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQPen & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQPen & ); +#endif + +#endif // TQPEN_H diff --git a/src/kernel/qpicture.cpp b/src/kernel/qpicture.cpp new file mode 100644 index 000000000..af36656b2 --- /dev/null +++ b/src/kernel/qpicture.cpp @@ -0,0 +1,1229 @@ +/**************************************************************************** +** +** Implementation of TQPicture class +** +** Created : 940802 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpicture.h" + +#ifndef QT_NO_PICTURE + +#include "qpainter.h" +#include "qpixmap.h" +#include "qimage.h" +#include "qfile.h" +#include "qdatastream.h" +#include "qpaintdevicemetrics.h" + +#ifndef QT_NO_SVG +#include "private/qsvgdevice_p.h" +#endif + +/*! + \class TQPicture qpicture.h + \brief The TQPicture class is a paint device that records and + replays TQPainter commands. + + \ingroup graphics + \ingroup images + \ingroup shared + + A picture serializes painter commands to an IO device in a + platform-independent format. For example, a picture created under + Windows can be read on a Sun SPARC. + + Pictures are called meta-files on some platforms. + + TQt pictures use a proprietary binary format. Unlike native picture + (meta-file) formats on many window systems, TQt pictures have no + limitations regarding their contents. Everything that can be + painted can also be stored in a picture, e.g. fonts, pixmaps, + regions, transformed graphics, etc. + + TQPicture is an \link shclass.html implicitly shared\endlink class. + + Example of how to record a picture: + \code + TQPicture pic; + TQPainter p; + p.begin( &pic ); // paint in picture + p.drawEllipse( 10,20, 80,70 ); // draw an ellipse + p.end(); // painting done + pic.save( "drawing.pic" ); // save picture + \endcode + + Example of how to replay a picture: + \code + TQPicture pic; + pic.load( "drawing.pic" ); // load picture + TQPainter p; + p.begin( &myWidget ); // paint in myWidget + p.drawPicture( pic ); // draw the picture + p.end(); // painting done + \endcode + + Pictures can also be drawn using play(). Some basic data about a + picture is available, for example, size(), isNull() and + boundingRect(). + +*/ + + +static const char *mfhdr_tag = "TQPIC"; // header tag +static const Q_UINT16 mfhdr_maj = 5; // major version # +static const Q_UINT16 mfhdr_min = 0; // minor version # + + +/*! + Constructs an empty picture. + + The \a formatVersion parameter may be used to \e create a TQPicture + that can be read by applications that are compiled with earlier + versions of TQt. + \list + \i \a formatVersion == 1 is binary compatible with TQt 1.x and later. + \i \a formatVersion == 2 is binary compatible with TQt 2.0.x and later. + \i \a formatVersion == 3 is binary compatible with TQt 2.1.x and later. + \i \a formatVersion == 4 is binary compatible with TQt 3.0.x and later. + \i \a formatVersion == 5 is binary compatible with TQt 3.1. + \endlist + + Note that the default formatVersion is -1 which signifies the + current release, i.e. for TQt 3.1 a formatVersion of 5 is the same + as the default formatVersion of -1. + + Reading pictures generated by earlier versions of TQt is supported + and needs no special coding; the format is automatically detected. +*/ + +TQPicture::TQPicture( int formatVersion ) + : TQPaintDevice( TQInternal::Picture | TQInternal::ExternalDevice ) + // set device type +{ + d = new TQPicturePrivate; + +#if defined(QT_CHECK_RANGE) + if ( formatVersion == 0 ) + qWarning( "TQPicture: invalid format version 0" ); +#endif + + // still accept the 0 default from before TQt 3.0. + if ( formatVersion > 0 && formatVersion != (int)mfhdr_maj ) { + d->formatMajor = formatVersion; + d->formatMinor = 0; + d->formatOk = FALSE; + } + else { + d->resetFormat(); + } +} + +/*! + Constructs a \link shclass.html shallow copy\endlink of \a pic. +*/ + +TQPicture::TQPicture( const TQPicture &pic ) + : TQPaintDevice( TQInternal::Picture | TQInternal::ExternalDevice ) +{ + d = pic.d; + d->ref(); +} + +/*! + Destroys the picture. +*/ +TQPicture::~TQPicture() +{ + if ( d->deref() ) + delete d; +} + + +/*! + \fn bool TQPicture::isNull() const + + Returns TRUE if the picture contains no data; otherwise returns + FALSE. +*/ + +/*! + \fn uint TQPicture::size() const + + Returns the size of the picture data. + + \sa data() +*/ + +/*! + \fn const char* TQPicture::data() const + + Returns a pointer to the picture data. The pointer is only valid + until the next non-const function is called on this picture. The + returned pointer is 0 if the picture contains no data. + + \sa size(), isNull() +*/ + +/*! + Sets the picture data directly from \a data and \a size. This + function copies the input data. + + \sa data(), size() +*/ + +void TQPicture::setData( const char* data, uint size ) +{ + detach(); + TQByteArray a( size ); + memcpy( a.data(), data, size ); + d->pictb.setBuffer( a ); // set byte array in buffer + d->resetFormat(); // we'll have to check +} + + +/*! + Loads a picture from the file specified by \a fileName and returns + TRUE if successful; otherwise returns FALSE. + + By default, the file will be interpreted as being in the native + TQPicture format. Specifying the \a format string is optional and + is only needed for importing picture data stored in a different + format. + + Currently, the only external format supported is the \link + http://www.w3.org/Graphics/SVG/ W3C SVG \endlink format which + retquires the \link xml.html TQt XML module \endlink. The + corresponding \a format string is "svg". + + \sa save() +*/ + +bool TQPicture::load( const TQString &fileName, const char *format ) +{ + TQFile f( fileName ); + if ( !f.open(IO_ReadOnly) ) + return FALSE; + return load( &f, format ); +} + +/*! + \overload + + \a dev is the device to use for loading. +*/ + +bool TQPicture::load( TQIODevice *dev, const char *format ) +{ +#ifndef QT_NO_SVG + if ( qstrcmp( format, "svg" ) == 0 ) { + TQSvgDevice svg; + if ( !svg.load( dev ) ) + return FALSE; + TQPainter p( this ); + bool b = svg.play( &p ); + d->brect = svg.boundingRect(); + return b; + } +#endif + if ( format ) { + qWarning( "TQPicture::load: No such picture format: %s", format ); + return FALSE; + } + + detach(); + TQByteArray a = dev->readAll(); + d->pictb.setBuffer( a ); // set byte array in buffer + return d->checkFormat(); +} + +/*! + Saves a picture to the file specified by \a fileName and returns + TRUE if successful; otherwise returns FALSE. + + Specifying the file \a format string is optional. It's not + recommended unless you intend to export the picture data for + use by a third party reader. By default the data will be saved in + the native TQPicture file format. + + Currently, the only external format supported is the \link + http://www.w3.org/Graphics/SVG/ W3C SVG \endlink format which + retquires the \link xml.html TQt XML module \endlink. The + corresponding \a format string is "svg". + + \sa load() +*/ + +bool TQPicture::save( const TQString &fileName, const char *format ) +{ + if ( paintingActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPicture::save: still being painted on. " + "Call TQPainter::end() first" ); +#endif + return FALSE; + } + +#ifndef QT_NO_SVG + // identical to TQIODevice* code below but the file name + // makes a difference when it comes to saving pixmaps + if ( qstricmp( format, "svg" ) == 0 ) { + TQSvgDevice svg; + TQPainter p( &svg ); + if ( !play( &p ) ) + return FALSE; + svg.setBoundingRect( boundingRect() ); + return svg.save( fileName ); + } +#endif + + TQFile f( fileName ); + if ( !f.open(IO_WriteOnly) ) + return FALSE; + return save( &f, format ); +} + +/*! + \overload + + \a dev is the device to use for saving. +*/ + +bool TQPicture::save( TQIODevice *dev, const char *format ) +{ + if ( paintingActive() ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPicture::save: still being painted on. " + "Call TQPainter::end() first" ); +#endif + return FALSE; + } + +#ifndef QT_NO_SVG + if ( qstricmp( format, "svg" ) == 0 ) { + TQSvgDevice svg; + TQPainter p( &svg ); + if ( !play( &p ) ) + return FALSE; + svg.setBoundingRect( boundingRect() ); + return svg.save( dev ); + } +#endif + if ( format ) { + qWarning( "TQPicture::save: No such picture format: %s", format ); + return FALSE; + } + + dev->writeBlock( d->pictb.buffer().data(), d->pictb.buffer().size() ); + return TRUE; +} + +/*! + Returns the picture's bounding rectangle or an invalid rectangle + if the picture contains no data. +*/ + +TQRect TQPicture::boundingRect() const +{ + if ( !d->formatOk ) + d->checkFormat(); + return d->brect; +} + +/*! + Sets the picture's bounding rectangle to \a r. The automatically + calculated value is overriden. +*/ + +void TQPicture::setBoundingRect( const TQRect &r ) +{ + if ( !d->formatOk ) + d->checkFormat(); + d->brect = r; +} + +/*! + Replays the picture using \a painter, and returns TRUE if + successful; otherwise returns FALSE. + + This function does exactly the same as TQPainter::drawPicture() + with (x, y) = (0, 0). +*/ + +bool TQPicture::play( TQPainter *painter ) +{ + if ( d->pictb.size() == 0 ) // nothing recorded + return TRUE; + + if ( !d->formatOk && !d->checkFormat() ) + return FALSE; + + d->pictb.open( IO_ReadOnly ); // open buffer device + TQDataStream s; + s.setDevice( &d->pictb ); // attach data stream to buffer + s.device()->at( 10 ); // go directly to the data + s.setVersion( d->formatMajor == 4 ? 3 : d->formatMajor ); + + Q_UINT8 c, clen; + Q_UINT32 nrecords; + s >> c >> clen; + Q_ASSERT( c == PdcBegin ); + // bounding rect was introduced in ver 4. Read in checkFormat(). + if ( d->formatMajor >= 4 ) { + Q_INT32 dummy; + s >> dummy >> dummy >> dummy >> dummy; + } + s >> nrecords; + if ( !exec( painter, s, nrecords ) ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPicture::play: Format error" ); +#endif + d->pictb.close(); + return FALSE; + } + d->pictb.close(); + return TRUE; // no end-command +} + + +/*! + \internal + Iterates over the internal picture data and draws the picture using + \a painter. +*/ + +bool TQPicture::exec( TQPainter *painter, TQDataStream &s, int nrecords ) +{ +#if defined(QT_DEBUG) + int strm_pos; +#endif + Q_UINT8 c; // command id + Q_UINT8 tiny_len; // 8-bit length descriptor + Q_INT32 len; // 32-bit length descriptor + Q_INT16 i_16, i1_16, i2_16; // parameters... + Q_INT8 i_8; + Q_UINT32 ul; + TQCString str1; + TQString str; + TQPoint p, p1, p2; + TQRect r; + TQPointArray a; + TQColor color; + TQFont font; + TQPen pen; + TQBrush brush; + TQRegion rgn; +#ifndef QT_NO_TRANSFORMATIONS + TQWMatrix matrix; +#endif + + while ( nrecords-- && !s.eof() ) { + s >> c; // read cmd + s >> tiny_len; // read param length + if ( tiny_len == 255 ) // longer than 254 bytes + s >> len; + else + len = tiny_len; +#if defined(QT_DEBUG) + strm_pos = s.device()->at(); +#endif + switch ( c ) { // exec cmd + case PdcNOP: + break; + case PdcDrawPoint: + s >> p; + painter->drawPoint( p ); + break; + case PdcMoveTo: + s >> p; + painter->moveTo( p ); + break; + case PdcLineTo: + s >> p; + painter->lineTo( p ); + break; + case PdcDrawLine: + s >> p1 >> p2; + painter->drawLine( p1, p2 ); + break; + case PdcDrawRect: + s >> r; + painter->drawRect( r ); + break; + case PdcDrawRoundRect: + s >> r >> i1_16 >> i2_16; + painter->drawRoundRect( r, i1_16, i2_16 ); + break; + case PdcDrawEllipse: + s >> r; + painter->drawEllipse( r ); + break; + case PdcDrawArc: + s >> r >> i1_16 >> i2_16; + painter->drawArc( r, i1_16, i2_16 ); + break; + case PdcDrawPie: + s >> r >> i1_16 >> i2_16; + painter->drawPie( r, i1_16, i2_16 ); + break; + case PdcDrawChord: + s >> r >> i1_16 >> i2_16; + painter->drawChord( r, i1_16, i2_16 ); + break; + case PdcDrawLineSegments: + s >> a; + painter->drawLineSegments( a ); + break; + case PdcDrawPolyline: + s >> a; + painter->drawPolyline( a ); + break; + case PdcDrawPolygon: + s >> a >> i_8; + painter->drawPolygon( a, i_8 ); + break; + case PdcDrawCubicBezier: + s >> a; +#ifndef QT_NO_BEZIER + painter->drawCubicBezier( a ); +#endif + break; + case PdcDrawText: + s >> p >> str1; + painter->drawText( p, str1 ); + break; + case PdcDrawTextFormatted: + s >> r >> i_16 >> str1; + painter->drawText( r, i_16, str1 ); + break; + case PdcDrawText2: + s >> p >> str; + painter->drawText( p, str ); + break; + case PdcDrawText2Formatted: + s >> r >> i_16 >> str; + painter->drawText( r, i_16, str ); + break; + case PdcDrawPixmap: { + TQPixmap pixmap; + if ( d->formatMajor < 4 ) { + s >> p >> pixmap; + painter->drawPixmap( p, pixmap ); + } else { + s >> r >> pixmap; + painter->drawPixmap( r, pixmap ); + } + } + break; + case PdcDrawImage: { + TQImage image; + if ( d->formatMajor < 4 ) { + s >> p >> image; + painter->drawImage( p, image ); + } else { + s >> r >> image; + painter->drawImage( r, image ); + } + } + break; + case PdcBegin: + s >> ul; // number of records + if ( !exec( painter, s, ul ) ) + return FALSE; + break; + case PdcEnd: + if ( nrecords == 0 ) + return TRUE; + break; + case PdcSave: + painter->save(); + break; + case PdcRestore: + painter->restore(); + break; + case PdcSetBkColor: + s >> color; + painter->setBackgroundColor( color ); + break; + case PdcSetBkMode: + s >> i_8; + painter->setBackgroundMode( (TQt::BGMode)i_8 ); + break; + case PdcSetROP: + s >> i_8; + painter->setRasterOp( (TQt::RasterOp)i_8 ); + break; + case PdcSetBrushOrigin: + s >> p; + painter->setBrushOrigin( p ); + break; + case PdcSetFont: + s >> font; + painter->setFont( font ); + break; + case PdcSetPen: + s >> pen; + painter->setPen( pen ); + break; + case PdcSetBrush: + s >> brush; + painter->setBrush( brush ); + break; + case PdcSetTabStops: + s >> i_16; + painter->setTabStops( i_16 ); + break; + case PdcSetTabArray: + s >> i_16; + if ( i_16 == 0 ) { + painter->setTabArray( 0 ); + } else { + int *ta = new int[i_16]; + Q_CHECK_PTR( ta ); + for ( int i=0; i> i1_16; + ta[i] = i1_16; + } + painter->setTabArray( ta ); + delete [] ta; + } + break; + case PdcSetVXform: + s >> i_8; +#ifndef QT_NO_TRANSFORMATIONS + painter->setViewXForm( i_8 ); +#endif + break; + case PdcSetWindow: + s >> r; +#ifndef QT_NO_TRANSFORMATIONS + painter->setWindow( r ); +#endif + break; + case PdcSetViewport: + s >> r; +#ifndef QT_NO_TRANSFORMATIONS + painter->setViewport( r ); +#endif + break; + case PdcSetWXform: + s >> i_8; +#ifndef QT_NO_TRANSFORMATIONS + painter->setWorldXForm( i_8 ); +#endif + break; + case PdcSetWMatrix: +#ifndef QT_NO_TRANSFORMATIONS // #### fix me! + s >> matrix >> i_8; + painter->setWorldMatrix( matrix, i_8 ); +#endif + break; +#ifndef QT_NO_TRANSFORMATIONS + case PdcSaveWMatrix: + painter->saveWorldMatrix(); + break; + case PdcRestoreWMatrix: + painter->restoreWorldMatrix(); + break; +#endif + case PdcSetClip: + s >> i_8; + painter->setClipping( i_8 ); + break; + case PdcSetClipRegion: + s >> rgn >> i_8; + painter->setClipRegion( rgn, (TQPainter::CoordinateMode)i_8 ); + break; + default: +#if defined(QT_CHECK_RANGE) + qWarning( "TQPicture::play: Invalid command %d", c ); +#endif + if ( len ) // skip unknown command + s.device()->at( s.device()->at()+len ); + } +#if defined(QT_DEBUG) + //qDebug( "device->at(): %i, strm_pos: %i len: %i", s.device()->at(), strm_pos, len ); + Q_ASSERT( Q_INT32(s.device()->at() - strm_pos) == len ); +#endif + } + return FALSE; +} + + +/*! + \internal + Records painter commands and stores them in the pictb buffer. +*/ + +bool TQPicture::cmd( int c, TQPainter *pt, TQPDevCmdParam *p ) +{ + detach(); + return d->cmd( c, pt, p ); +} + +/*! + \internal + Implementation of the function forwarded above to the internal data struct. +*/ + +bool TQPicture::TQPicturePrivate::cmd( int c, TQPainter *pt, TQPDevCmdParam *p ) +{ + TQDataStream s; + s.setDevice( &pictb ); + // when moving up to 4 the TQDataStream version remained at 3 + s.setVersion( formatMajor != 4 ? formatMajor : 3 ); + if ( c == PdcBegin ) { // begin; write header + TQByteArray empty( 0 ); + pictb.setBuffer( empty ); // reset byte array in buffer + pictb.open( IO_WriteOnly ); + s.writeRawBytes( mfhdr_tag, 4 ); + s << (Q_UINT16)0 << (Q_UINT16)formatMajor << (Q_UINT16)formatMinor; + s << (Q_UINT8)c << (Q_UINT8)sizeof(Q_INT32); + brect = TQRect(); + if ( formatMajor >= 4 ) { + s << (Q_INT32)brect.left() << (Q_INT32)brect.top() + << (Q_INT32)brect.width() << (Q_INT32)brect.height(); + } + trecs = 0; + s << (Q_UINT32)trecs; // total number of records + formatOk = FALSE; + return TRUE; + } else if ( c == PdcEnd ) { // end; calc checksum and close + trecs++; + s << (Q_UINT8)c << (Q_UINT8)0; + TQByteArray buf = pictb.buffer(); + int cs_start = sizeof(Q_UINT32); // pos of checksum word + int data_start = cs_start + sizeof(Q_UINT16); + int brect_start = data_start + 2*sizeof(Q_INT16) + 2*sizeof(Q_UINT8); + int pos = pictb.at(); + pictb.at( brect_start ); + if ( formatMajor >= 4 ) { // bounding rectangle + s << (Q_INT32)brect.left() << (Q_INT32)brect.top() + << (Q_INT32)brect.width() << (Q_INT32)brect.height(); + } + s << (Q_UINT32)trecs; // write number of records + pictb.at( cs_start ); + Q_UINT16 cs = (Q_UINT16)qChecksum( buf.data()+data_start, pos-data_start ); + s << cs; // write checksum + pictb.close(); + return TRUE; + } + trecs++; + s << (Q_UINT8)c; // write cmd to stream + s << (Q_UINT8)0; // write dummy length info + int pos = (int)pictb.at(); // save position + TQRect br; // bounding rect addition + bool corr = FALSE; // correction for pen width + + switch ( c ) { + case PdcDrawPoint: + case PdcMoveTo: + case PdcLineTo: + case PdcSetBrushOrigin: + s << *p[0].point; + br = TQRect( *p[0].point, TQSize( 1, 1 ) ); + corr = TRUE; + break; + case PdcDrawLine: + s << *p[0].point << *p[1].point; + br = TQRect( *p[0].point, *p[1].point ).normalize(); + corr = TRUE; + break; + case PdcDrawRect: + case PdcDrawEllipse: + s << *p[0].rect; + br = *p[0].rect; + corr = TRUE; + break; + case PdcDrawRoundRect: + case PdcDrawArc: + case PdcDrawPie: + case PdcDrawChord: + s << *p[0].rect << (Q_INT16)p[1].ival << (Q_INT16)p[2].ival; + br = *p[0].rect; + corr = TRUE; + break; + case PdcDrawLineSegments: + case PdcDrawPolyline: + s << *p[0].ptarr; + br = p[0].ptarr->boundingRect(); + corr = TRUE; + break; +#ifndef QT_NO_BEZIER + case PdcDrawCubicBezier: + s << *p[0].ptarr; + br = p[0].ptarr->cubicBezier().boundingRect(); + corr = TRUE; + break; +#endif + case PdcDrawPolygon: + s << *p[0].ptarr << (Q_INT8)p[1].ival; + br = p[0].ptarr->boundingRect(); + corr = TRUE; + break; + case PdcDrawText2: + if ( formatMajor == 1 ) { + pictb.at( pos - 2 ); + s << (Q_UINT8)PdcDrawText << (Q_UINT8)0; + TQCString str1( (*p[1].str).latin1() ); + s << *p[0].point << str1; + } + else { + s << *p[0].point << *p[1].str; + } + br = pt->fontMetrics().boundingRect( *p[1].str ); + br.moveBy( p[0].point->x(), p[0].point->y() ); + break; + case PdcDrawText2Formatted: + if ( formatMajor == 1 ) { + pictb.at( pos - 2 ); + s << (Q_UINT8)PdcDrawTextFormatted << (Q_UINT8)0; + TQCString str1( (*p[2].str).latin1() ); + s << *p[0].rect << (Q_INT16)p[1].ival << str1; + } + else { + s << *p[0].rect << (Q_INT16)p[1].ival << *p[2].str; + } + br = *p[0].rect; + break; + case PdcDrawPixmap: + if ( formatMajor < 4 ) { + s << *p[0].point; + s << *p[1].pixmap; + br = TQRect( *p[0].point, p[1].pixmap->size() ); + } else { + s << *p[0].rect; + s << *p[1].pixmap; + br = *p[0].rect; + } + break; + case PdcDrawImage: + if ( formatMajor < 4 ) { + TQPoint pt( p[0].point->x(), p[0].point->y() ); + s << pt; + s << *p[1].image; + br = TQRect( *p[0].point, p[1].image->size() ); + } else { + s << *p[0].rect; + s << *p[1].image; + br = *p[0].rect; + } + break; + case PdcSave: + case PdcRestore: + break; + case PdcSetBkColor: + s << *p[0].color; + break; + case PdcSetBkMode: + case PdcSetROP: + s << (Q_INT8)p[0].ival; + break; + case PdcSetFont: { + TQFont fnt = *p[0].font; + if (fnt.pointSize() > 0) + // we have to store pixels to get correct replay. + // the resolution is 72 dpi, so points == pixels + fnt.setPixelSize(TQFontInfo(fnt).pixelSize()); + s << fnt; + } + break; + case PdcSetPen: + s << *p[0].pen; + break; + case PdcSetBrush: + s << *p[0].brush; + break; + case PdcSetTabStops: + s << (Q_INT16)p[0].ival; + break; + case PdcSetTabArray: + s << (Q_INT16)p[0].ival; + if ( p[0].ival ) { + int *ta = p[1].ivec; + for ( int i=0; ipen().width() / 2; + br.setCoords( br.left() - w2, br.top() - w2, + br.right() + w2, br.bottom() + w2 ); + } +#ifndef QT_NO_TRANSFORMATIONS + br = pt->worldMatrix().map( br ); +#endif + if ( pt->hasClipping() ) { + TQRect cr = pt->clipRegion().boundingRect(); + br &= cr; + } + if ( br.isValid() ) + brect |= br; // merge with existing rect + } + + return TRUE; +} + + +/*! + Internal implementation of the virtual TQPaintDevice::metric() + function. + + Use the TQPaintDeviceMetrics class instead. + + A picture has the following hard-coded values: dpi=72, + numcolors=16777216 and depth=24. + + \a m is the metric to get. +*/ + +int TQPicture::metric( int m ) const +{ + int val; + switch ( m ) { + // ### hard coded dpi and color depth values ! + case TQPaintDeviceMetrics::PdmWidth: + val = d->brect.width(); + break; + case TQPaintDeviceMetrics::PdmHeight: + val = d->brect.height(); + break; + case TQPaintDeviceMetrics::PdmWidthMM: + val = int(25.4/72.0*d->brect.width()); + break; + case TQPaintDeviceMetrics::PdmHeightMM: + val = int(25.4/72.0*d->brect.height()); + break; + case TQPaintDeviceMetrics::PdmDpiX: + case TQPaintDeviceMetrics::PdmPhysicalDpiX: + val = 72; + break; + case TQPaintDeviceMetrics::PdmDpiY: + case TQPaintDeviceMetrics::PdmPhysicalDpiY: + val = 72; + break; + case TQPaintDeviceMetrics::PdmNumColors: + val = 16777216; + break; + case TQPaintDeviceMetrics::PdmDepth: + val = 24; + break; + default: + val = 0; +#if defined(QT_CHECK_RANGE) + qWarning( "TQPicture::metric: Invalid metric command" ); +#endif + } + return val; +} + +/*! + Detaches from shared picture data and makes sure that this picture + is the only one referring to the data. + + If multiple pictures share common data, this picture makes a copy + of the data and detaches itself from the sharing mechanism. + Nothing is done if there is just a single reference. +*/ + +void TQPicture::detach() +{ + if ( d->count != 1 ) + *this = copy(); +} + +/*! + Returns a \link shclass.html deep copy\endlink of the picture. +*/ + +TQPicture TQPicture::copy() const +{ + TQPicture p; + TQByteArray a( size() ); + memcpy( a.data(), data(), size() ); + p.d->pictb.setBuffer( a ); // set byte array in buffer + if ( d->pictb.isOpen() ) { // copy buffer state + p.d->pictb.open( d->pictb.mode() ); + p.d->pictb.at( d->pictb.at() ); + } + p.d->trecs = d->trecs; + p.d->formatOk = d->formatOk; + p.d->formatMinor = d->formatMajor; + p.d->brect = boundingRect(); + return p; +} + +/***************************************************************************** + TQPainter member functions + *****************************************************************************/ + +/*! + Replays the picture \a pic translated by (\a x, \a y). + + This function does exactly the same as TQPicture::play() when + called with (\a x, \a y) = (0, 0). +*/ + +void TQPainter::drawPicture( int x, int y, const TQPicture &pic ) +{ + save(); + translate( x, y ); + ((TQPicture*)&pic)->play( (TQPainter*)this ); + restore(); +} + +/*! + \overload void TQPainter::drawPicture( const TQPoint &p, const TQPicture &pic ) + + Draws picture \a pic at point \a p. +*/ + +void TQPainter::drawPicture( const TQPoint &p, const TQPicture &pic ) +{ + drawPicture( p.x(), p.y(), pic ); +} + +/*! + \obsolete + + Use one of the other TQPainter::drawPicture() functions with a (0, 0) + offset instead. +*/ + +void TQPainter::drawPicture( const TQPicture &pic ) +{ + drawPicture( 0, 0, pic ); +} + +/*! + Assigns a \link shclass.html shallow copy\endlink of \a p to this + picture and returns a reference to this picture. +*/ + +TQPicture& TQPicture::operator= (const TQPicture& p) +{ + p.d->ref(); // avoid 'x = x' + if ( d->deref() ) + delete d; + d = p.d; + return *this; +} + + +/*! + \internal + + Sets formatOk to FALSE and resets the format version numbers to default +*/ + +void TQPicture::TQPicturePrivate::resetFormat() +{ + formatOk = FALSE; + formatMajor = mfhdr_maj; + formatMinor = mfhdr_min; +} + +/*! + \internal + + Checks data integrity and format version number. Set formatOk to TRUE + on success, to FALSE otherwise. Returns the resulting formatOk value. +*/ + +bool TQPicture::TQPicturePrivate::checkFormat() +{ + resetFormat(); + + // can't check anything in an empty buffer + if ( pictb.size() == 0 ) + return FALSE; + + pictb.open( IO_ReadOnly ); // open buffer device + TQDataStream s; + s.setDevice( &pictb ); // attach data stream to buffer + + char mf_id[4]; // picture header tag + s.readRawBytes( mf_id, 4 ); // read actual tag + if ( memcmp(mf_id, mfhdr_tag, 4) != 0 ) { // wrong header id +#if defined(QT_CHECK_RANGE) + qWarning( "TQPicture::checkFormat: Incorrect header" ); +#endif + pictb.close(); + return FALSE; + } + + int cs_start = sizeof(Q_UINT32); // pos of checksum word + int data_start = cs_start + sizeof(Q_UINT16); + Q_UINT16 cs,ccs; + TQByteArray buf = pictb.buffer(); // pointer to data + s >> cs; // read checksum + ccs = qChecksum( buf.data() + data_start, buf.size() - data_start ); + if ( ccs != cs ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPicture::checkFormat: Invalid checksum %x, %x expected", + ccs, cs ); +#endif + pictb.close(); + return FALSE; + } + + Q_UINT16 major, minor; + s >> major >> minor; // read version number + if ( major > mfhdr_maj ) { // new, incompatible version +#if defined(QT_CHECK_RANGE) + qWarning( "TQPicture::checkFormat: Incompatible version %d.%d", + major, minor); +#endif + pictb.close(); + return FALSE; + } + s.setVersion( major != 4 ? major : 3 ); + + Q_UINT8 c, clen; + s >> c >> clen; + if ( c == PdcBegin ) { + if ( !( major >= 1 && major <= 3 )) { + Q_INT32 l, t, w, h; + s >> l >> t >> w >> h; + brect = TQRect( l, t, w, h ); + } + } else { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPicture::checkFormat: Format error" ); +#endif + pictb.close(); + return FALSE; + } + pictb.close(); + + formatOk = TRUE; // picture seems to be ok + formatMajor = major; + formatMinor = minor; + return TRUE; +} + +/***************************************************************************** + TQPicture stream functions + *****************************************************************************/ + +/*! + \relates TQPicture + + Writes picture \a r to the stream \a s and returns a reference to + the stream. +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQPicture &r ) +{ + Q_UINT32 size = r.d->pictb.buffer().size(); + s << size; + // null picture ? + if ( size == 0 ) + return s; + // just write the whole buffer to the stream + return s.writeRawBytes ( r.d->pictb.buffer().data(), + r.d->pictb.buffer().size() ); +} + +/*! + \relates TQPicture + + Reads a picture from the stream \a s into picture \a r and returns + a reference to the stream. +*/ + +TQDataStream &operator>>( TQDataStream &s, TQPicture &r ) +{ + TQDataStream sr; + + // "init"; this code is similar to the beginning of TQPicture::cmd() + sr.setDevice( &r.d->pictb ); + sr.setVersion( r.d->formatMajor ); + Q_UINT32 len; + s >> len; + TQByteArray data( len ); + if ( len > 0 ) + s.readRawBytes( data.data(), len ); + + r.d->pictb.setBuffer( data ); + r.d->resetFormat(); + + return s; +} + +#endif // QT_NO_PICTURE + diff --git a/src/kernel/qpicture.h b/src/kernel/qpicture.h new file mode 100644 index 000000000..27b87c24c --- /dev/null +++ b/src/kernel/qpicture.h @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Definition of TQPicture class +** +** Created : 940729 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPICTURE_H +#define TQPICTURE_H + +#ifndef QT_H +#include "qpaintdevice.h" +#include "qbuffer.h" +#endif // QT_H + +#ifndef QT_NO_PICTURE + +class Q_EXPORT TQPicture : public TQPaintDevice // picture class +{ +public: + TQPicture( int formatVersion = -1 ); + TQPicture( const TQPicture & ); + ~TQPicture(); + + bool isNull() const; + + uint size() const; + const char* data() const; + virtual void setData( const char* data, uint size ); + + bool play( TQPainter * ); + + bool load( TQIODevice *dev, const char *format = 0 ); + bool load( const TQString &fileName, const char *format = 0 ); + bool save( TQIODevice *dev, const char *format = 0 ); + bool save( const TQString &fileName, const char *format = 0 ); + + TQRect boundingRect() const; + void setBoundingRect( const TQRect &r ); + + TQPicture& operator= (const TQPicture&); + + friend Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQPicture & ); + friend Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQPicture & ); + +protected: + bool cmd( int, TQPainter *, TQPDevCmdParam * ); + int metric( int ) const; + void detach(); + TQPicture copy() const; + +private: + bool exec( TQPainter *, TQDataStream &, int ); + + struct TQPicturePrivate : public TQShared { + bool cmd( int, TQPainter *, TQPDevCmdParam * ); + bool checkFormat(); + void resetFormat(); + + TQBuffer pictb; + int trecs; + bool formatOk; + int formatMajor; + int formatMinor; + TQRect brect; + } *d; +}; + + +inline bool TQPicture::isNull() const +{ + return d->pictb.buffer().isNull(); +} + +inline uint TQPicture::size() const +{ + return d->pictb.buffer().size(); +} + +inline const char* TQPicture::data() const +{ + return d->pictb.buffer().data(); +} + +/***************************************************************************** + TQPicture stream functions + *****************************************************************************/ + +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQPicture & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQPicture & ); + +#endif // QT_NO_PICTURE + +#endif // TQPICTURE_H diff --git a/src/kernel/qpixmap.cpp b/src/kernel/qpixmap.cpp new file mode 100644 index 000000000..f719a8303 --- /dev/null +++ b/src/kernel/qpixmap.cpp @@ -0,0 +1,1510 @@ +/**************************************************************************** +** +** Implementation of TQPixmap class +** +** Created : 950301 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpixmap.h" + +#include "qbitmap.h" +#include "qimage.h" +#include "qwidget.h" +#include "qpainter.h" +#include "qdatastream.h" +#include "qbuffer.h" +#include "qobjectlist.h" +#include "qapplication.h" +#include +#include "qmime.h" +#include "qdragobject.h" +#include "qfile.h" + +/*! + \class TQPixmap qpixmap.h + \brief The TQPixmap class is an off-screen, pixel-based paint device. + + \ingroup graphics + \ingroup images + \ingroup shared + \mainclass + + TQPixmap is one of the two classes TQt provides for dealing with + images; the other is TQImage. TQPixmap is designed and optimized + for drawing; TQImage is designed and optimized for I/O and for + direct pixel access/manipulation. There are (slow) functions to + convert between TQImage and TQPixmap: convertToImage() and + convertFromImage(). + + One common use of the TQPixmap class is to enable smooth updating + of widgets. Whenever something complex needs to be drawn, you can + use a pixmap to obtain flicker-free drawing, like this: + + \list 1 + \i Create a pixmap with the same size as the widget. + \i Fill the pixmap with the widget background color. + \i Paint the pixmap. + \i bitBlt() the pixmap contents onto the widget. + \endlist + + Pixel data in a pixmap is internal and is managed by the + underlying window system. Pixels can be accessed only through + TQPainter functions, through bitBlt(), and by converting the + TQPixmap to a TQImage. + + You can easily display a TQPixmap on the screen using + TQLabel::setPixmap(). For example, all the TQButton subclasses + support pixmap use. + + The TQPixmap class uses \link shclass.html copy-on-write\endlink, + so it is practical to pass TQPixmap objects by value. + + You can retrieve the width(), height(), depth() and size() of a + pixmap. The enclosing rectangle is given by rect(). Pixmaps can be + filled with fill() and resized with resize(). You can create and + set a mask with createHeuristicMask() and setMask(). Use + selfMask() to see if the pixmap is identical to its mask. + + In addition to loading a pixmap from file using load() you can + also loadFromData(). You can control optimization with + setOptimization() and obtain a transformed version of the pixmap + using xForm() + + Note regarding Windows 95 and 98: on Windows 9x the system crashes + if you create more than about 1000 pixmaps, independent of the + size of the pixmaps or installed RAM. Windows NT-systems (including + 2000, XP and following versions) do not have the same limitation, + but depending on the graphics etquipment the system will fail to + allocate pixmap objects at some point (due to system running out of + GDI resources). + + TQt tries to work around the resource limitation. If you set the + pixmap optimization to \c TQPixmap::MemoryOptim and the width of + your pixmap is less than or equal to 128 pixels, TQt stores the + pixmap in a way that is very memory-efficient when there are many + pixmaps. + + If your application uses dozens or hundreds of pixmaps (for + example on tool bar buttons and in popup menus), and you plan to + run it on Windows 95 or Windows 98, we recommend using code like + this: + + \code + TQPixmap::setDefaultOptimization( TQPixmap::MemoryOptim ); + while ( ... ) { + // load tool bar pixmaps etc. + TQPixmap *pixmap = new TQPixmap(fileName); + } + TQPixmap::setDefaultOptimization( TQPixmap::NormalOptim ); + \endcode + + In general it is recommended to make as much use of TQPixmap's + implicit sharing and the TQPixmapCache as possible. + + \sa TQBitmap, TQImage, TQImageIO, \link shclass.html Shared Classes\endlink +*/ + +/*! + \enum TQPixmap::ColorMode + + This enum type defines the color modes that exist for converting + TQImage objects to TQPixmap. + + \value Auto Select \c Color or \c Mono on a case-by-case basis. + \value Color Always create colored pixmaps. + \value Mono Always create bitmaps. +*/ + +/*! + \enum TQPixmap::Optimization + + TQPixmap has the choice of optimizing for speed or memory in a few + places; the best choice varies from pixmap to pixmap but can + generally be derived heuristically. This enum type defines a + number of optimization modes that you can set for any pixmap to + tweak the speed/memory tradeoffs: + + \value DefaultOptim Whatever TQPixmap::defaultOptimization() + returns. A pixmap with this optimization will have whatever + the current default optimization is. If the default + optimization is changed using setDefaultOptimization(), then + this will not effect any pixmaps that have already been + created. + + \value NoOptim No optimization (currently the same as \c + MemoryOptim). + + \value MemoryOptim Optimize for minimal memory use on Windows + 9x and X11 systems. + + \value NormalOptim Optimize for typical usage. Often uses more + memory than \c MemoryOptim, and is often faster. + + \value BestOptim Optimize for pixmaps that are drawn very often + and where performance is critical. Generally uses more memory + than \c NormalOptim and may provide a little more speed. + + We recommend using \c DefaultOptim. + +*/ + + +TQPixmap::Optimization TQPixmap::defOptim = TQPixmap::NormalOptim; + + +/*! + \internal + Private constructor which takes the bitmap flag, the optimization.and a screen. +*/ + +TQPixmap::TQPixmap( int w, int h, int depth, bool bitmap, + Optimization optimization ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( w, h, depth, bitmap, optimization ); +} + + +/*! + Constructs a null pixmap. + + \sa isNull() +*/ + +TQPixmap::TQPixmap() + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); +} + +/*! + Constructs a pixmap from the TQImage \a image. + + \sa convertFromImage() +*/ + +TQPixmap::TQPixmap( const TQImage& image ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + convertFromImage( image ); +} + +/*! + Constructs a pixmap with \a w width, \a h height and \a depth bits + per pixel. The pixmap is optimized in accordance with the \a + optimization value. + + The contents of the pixmap is uninitialized. + + The \a depth can be either 1 (monochrome) or the depth of the + current video mode. If \a depth is negative, then the hardware + depth of the current video mode will be used. + + If either \a w or \a h is zero, a null pixmap is constructed. + + \sa isNull() TQPixmap::Optimization +*/ + +TQPixmap::TQPixmap( int w, int h, int depth, Optimization optimization ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( w, h, depth, FALSE, optimization ); +} + +/*! + \overload TQPixmap::TQPixmap( const TQSize &size, int depth, Optimization optimization ) + + Constructs a pixmap of size \a size, \a depth bits per pixel, + optimized in accordance with the \a optimization value. +*/ + +TQPixmap::TQPixmap( const TQSize &size, int depth, Optimization optimization ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( size.width(), size.height(), depth, FALSE, optimization ); +} + +#ifndef QT_NO_IMAGEIO +/*! + Constructs a pixmap from the file \a fileName. If the file does + not exist or is of an unknown format, the pixmap becomes a null + pixmap. + + The \a fileName, \a format and \a conversion_flags parameters are + passed on to load(). This means that the data in \a fileName is + not compiled into the binary. If \a fileName contains a relative + path (e.g. the filename only) the relevant file must be found + relative to the runtime working directory. + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversion_flags to specify how you'd prefer this to happen. + + \sa TQt::ImageConversionFlags isNull(), load(), loadFromData(), save(), imageFormat() +*/ + +TQPixmap::TQPixmap( const TQString& fileName, const char *format, + int conversion_flags ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + load( fileName, format, conversion_flags ); +} + +/*! + Constructs a pixmap from the file \a fileName. If the file does + not exist or is of an unknown format, the pixmap becomes a null + pixmap. + + The \a fileName, \a format and \a mode parameters are passed on to + load(). This means that the data in \a fileName is not compiled + into the binary. If \a fileName contains a relative path (e.g. the + filename only) the relevant file must be found relative to the + runtime working directory. + + \sa TQPixmap::ColorMode isNull(), load(), loadFromData(), save(), imageFormat() +*/ + +TQPixmap::TQPixmap( const TQString& fileName, const char *format, ColorMode mode ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + load( fileName, format, mode ); +} + +/*! + Constructs a pixmap from \a xpm, which must be a valid XPM image. + + Errors are silently ignored. + + Note that it's possible to squeeze the XPM variable a little bit + by using an unusual declaration: + + \code + static const char * const start_xpm[]={ + "16 15 8 1", + "a c #cec6bd", + .... + \endcode + + The extra \c const makes the entire definition read-only, which is + slightly more efficient (for example, when the code is in a shared + library) and ROMable when the application is to be stored in ROM. + + In order to use that sort of declaration you must cast the + variable back to \c{const char **} when you create the TQPixmap. +*/ + +TQPixmap::TQPixmap( const char *xpm[] ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + TQImage image( xpm ); + if ( !image.isNull() ) + convertFromImage( image ); +} + +/*! + Constructs a pixmaps by loading from \a img_data. The data can be + in any image format supported by TQt. + + \sa loadFromData() +*/ + +TQPixmap::TQPixmap( const TQByteArray & img_data ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + init( 0, 0, 0, FALSE, defOptim ); + loadFromData( img_data ); +} +#endif //QT_NO_IMAGEIO + +/*! + Constructs a pixmap that is a copy of \a pixmap. +*/ + +TQPixmap::TQPixmap( const TQPixmap &pixmap ) + : TQPaintDevice( TQInternal::Pixmap ) +{ + if ( pixmap.paintingActive() ) { // make a deep copy + data = 0; + operator=( pixmap.copy() ); + } else { + data = pixmap.data; + data->ref(); + devFlags = pixmap.devFlags; // copy TQPaintDevice flags +#if defined(Q_WS_WIN) + hdc = pixmap.hdc; // copy Windows device context +#elif defined(Q_WS_X11) + hd = pixmap.hd; // copy X11 drawable + rendhd = pixmap.rendhd; + copyX11Data( &pixmap ); // copy x11Data +#elif defined(Q_WS_MAC) + hd = pixmap.hd; +#endif + } +} + + +/*! + Destroys the pixmap. +*/ + +TQPixmap::~TQPixmap() +{ + deref(); +} + +/*! Convenience function. Gets the data associated with the absolute + name \a abs_name from the default mime source factory and decodes it + to a pixmap. + + \sa TQMimeSourceFactory, TQImage::fromMimeSource(), TQImageDrag::decode() +*/ + +#ifndef QT_NO_MIME +TQPixmap TQPixmap::fromMimeSource( const TQString &abs_name ) +{ + const TQMimeSource *m = TQMimeSourceFactory::defaultFactory()->data( abs_name ); + if ( !m ) { + if ( TQFile::exists( abs_name ) ) + return TQPixmap( abs_name ); +#if defined(QT_CHECK_STATE) + if ( !abs_name.isEmpty() ) + qWarning( "TQPixmap::fromMimeSource: Cannot find pixmap \"%s\" in the mime source factory", + abs_name.latin1() ); +#endif + return TQPixmap(); + } + TQPixmap pix; + TQImageDrag::decode( m, pix ); + return pix; +} +#endif + +/*! + Returns a \link shclass.html deep copy\endlink of the pixmap using + the bitBlt() function to copy the pixels. + + \sa operator=() +*/ + +TQPixmap TQPixmap::copy( bool ignoreMask ) const +{ +#if defined(Q_WS_X11) + int old = x11SetDefaultScreen( x11Screen() ); +#endif // Q_WS_X11 + + TQPixmap pm( data->w, data->h, data->d, data->bitmap, data->optim ); + + if ( !pm.isNull() ) { // copy the bitmap +#if defined(Q_WS_X11) + pm.cloneX11Data( this ); +#endif // Q_WS_X11 + + if ( ignoreMask ) + bitBlt( &pm, 0, 0, this, 0, 0, data->w, data->h, TQt::CopyROP, TRUE ); + else + copyBlt( &pm, 0, 0, this, 0, 0, data->w, data->h ); + } + +#if defined(Q_WS_X11) + x11SetDefaultScreen( old ); +#endif // Q_WS_X11 + + return pm; +} + + +/*! + Assigns the pixmap \a pixmap to this pixmap and returns a + reference to this pixmap. +*/ + +TQPixmap &TQPixmap::operator=( const TQPixmap &pixmap ) +{ + if ( paintingActive() ) { +#if defined(QT_CHECK_STATE) + qWarning("TQPixmap::operator=: Cannot assign to pixmap during painting"); +#endif + return *this; + } + pixmap.data->ref(); // avoid 'x = x' + deref(); + if ( pixmap.paintingActive() ) { // make a deep copy + init( pixmap.width(), pixmap.height(), pixmap.depth(), + pixmap.data->bitmap, pixmap.data->optim ); + data->uninit = FALSE; + if ( !isNull() ) + copyBlt( this, 0, 0, &pixmap, 0, 0, pixmap.width(), pixmap.height() ); + pixmap.data->deref(); + } else { + data = pixmap.data; + devFlags = pixmap.devFlags; // copy TQPaintDevice flags +#if defined(Q_WS_WIN) + hdc = pixmap.hdc; +#elif defined(Q_WS_X11) + hd = pixmap.hd; // copy TQPaintDevice drawable + rendhd = pixmap.rendhd; + copyX11Data( &pixmap ); // copy x11Data +#elif defined(Q_WS_MACX) || defined(Q_OS_MAC9) + hd = pixmap.hd; +#endif + } + return *this; +} + + +/*! + \overload + + Converts the image \a image to a pixmap that is assigned to this + pixmap. Returns a reference to the pixmap. + + \sa convertFromImage(). +*/ + +TQPixmap &TQPixmap::operator=( const TQImage &image ) +{ + convertFromImage( image ); + return *this; +} + + +/*! + \fn bool TQPixmap::isTQBitmap() const + + Returns TRUE if this is a TQBitmap; otherwise returns FALSE. +*/ + +/*! + \fn bool TQPixmap::isNull() const + + Returns TRUE if this is a null pixmap; otherwise returns FALSE. + + A null pixmap has zero width, zero height and no contents. You + cannot draw in a null pixmap or bitBlt() anything to it. + + Resizing an existing pixmap to (0, 0) makes a pixmap into a null + pixmap. + + \sa resize() +*/ + +/*! + \fn int TQPixmap::width() const + + Returns the width of the pixmap. + + \sa height(), size(), rect() +*/ + +/*! + \fn int TQPixmap::height() const + + Returns the height of the pixmap. + + \sa width(), size(), rect() +*/ + +/*! + \fn TQSize TQPixmap::size() const + + Returns the size of the pixmap. + + \sa width(), height(), rect() +*/ + +/*! + \fn TQRect TQPixmap::rect() const + + Returns the enclosing rectangle (0,0,width(),height()) of the pixmap. + + \sa width(), height(), size() +*/ + +/*! + \fn int TQPixmap::depth() const + + Returns the depth of the pixmap. + + The pixmap depth is also called bits per pixel (bpp) or bit planes + of a pixmap. A null pixmap has depth 0. + + \sa defaultDepth(), isNull(), TQImage::convertDepth() +*/ + + +/*! + \overload void TQPixmap::fill( const TQWidget *widget, const TQPoint &ofs ) + + Fills the pixmap with the \a widget's background color or pixmap. + If the background is empty, nothing is done. + + The \a ofs point is an offset in the widget. + + The point \a ofs is a point in the widget's coordinate system. The + pixmap's top-left pixel will be mapped to the point \a ofs in the + widget. This is significant if the widget has a background pixmap; + otherwise the pixmap will simply be filled with the background + color of the widget. + + Example: + \code + void CuteWidget::paintEvent( TQPaintEvent *e ) + { + TQRect ur = e->rect(); // rectangle to update + TQPixmap pix( ur.size() ); // Pixmap for double-buffering + pix.fill( this, ur.topLeft() ); // fill with widget background + + TQPainter p( &pix ); + p.translate( -ur.x(), -ur.y() ); // use widget coordinate system + // when drawing on pixmap + // ... draw on pixmap ... + + p.end(); + + bitBlt( this, ur.topLeft(), &pix ); + } + \endcode +*/ + +/*! + \overload void TQPixmap::fill( const TQWidget *widget, int xofs, int yofs ) + + Fills the pixmap with the \a widget's background color or pixmap. + If the background is empty, nothing is done. \a xofs, \a yofs is + an offset in the widget. +*/ + +void TQPixmap::fill( const TQWidget *widget, int xofs, int yofs ) +{ + const TQPixmap* bgpm = widget->backgroundPixmap(); + fill( widget->backgroundColor() ); + if ( bgpm ) { + if ( !bgpm->isNull() ) { + TQPoint ofs = widget->backgroundOffset(); + xofs += ofs.x(); + yofs += ofs.y(); + + TQPainter p; + p.begin( this ); + p.setPen( NoPen ); + p.drawTiledPixmap( 0, 0, width(), height(), *widget->backgroundPixmap(), xofs, yofs ); + p.end(); + } + } +} + + +/*! + \overload void TQPixmap::resize( const TQSize &size ) + + Resizes the pixmap to size \a size. +*/ + +/*! + Resizes the pixmap to \a w width and \a h height. If either \a w + or \a h is 0, the pixmap becomes a null pixmap. + + If both \a w and \a h are greater than 0, a valid pixmap is + created. New pixels will be uninitialized (random) if the pixmap + is expanded. +*/ + +void TQPixmap::resize( int w, int h ) +{ + if ( w < 1 || h < 1 ) { // becomes null + TQPixmap pm( 0, 0, 0, data->bitmap, data->optim ); + *this = pm; + return; + } + int d; + if ( depth() > 0 ) + d = depth(); + else + d = isTQBitmap() ? 1 : -1; + // Create new pixmap + TQPixmap pm( w, h, d, data->bitmap, data->optim ); +#ifdef Q_WS_X11 + pm.x11SetScreen( x11Screen() ); +#endif // Q_WS_X11 + if ( !data->uninit && !isNull() ) // has existing pixmap + bitBlt( &pm, 0, 0, this, 0, 0, // copy old pixmap + TQMIN(width(), w), + TQMIN(height(),h), CopyROP, TRUE ); +#if defined(Q_WS_MAC) + if(data->alphapm) { + data->alphapm->resize(w, h); + } else +#elif defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + if (data->alphapm) + qWarning("TQPixmap::resize: TODO: resize alpha data"); + else +#endif // Q_WS_X11 + if ( data->mask ) { // resize mask as well + if ( data->selfmask ) { // preserve self-mask + pm.setMask( *((TQBitmap*)&pm) ); + } else { // independent mask + TQBitmap m = *data->mask; + m.resize( w, h ); + pm.setMask( m ); + } + } + *this = pm; +} + + +/*! + \fn const TQBitmap *TQPixmap::mask() const + + Returns the mask bitmap, or 0 if no mask has been set. + + \sa setMask(), TQBitmap, hasAlpha() +*/ + +/*! + Sets a mask bitmap. + + The \a newmask bitmap defines the clip mask for this pixmap. Every + pixel in \a newmask corresponds to a pixel in this pixmap. Pixel + value 1 means opaque and pixel value 0 means transparent. The mask + must have the same size as this pixmap. + + \warning Setting the mask on a pixmap will cause any alpha channel + data to be cleared. For example: + \code + TQPixmap alpha( "image-with-alpha.png" ); + TQPixmap alphacopy = alpha; + alphacopy.setMask( *alphacopy.mask() ); + \endcode + Now, alpha and alphacopy are visually different. + + Setting a \link isNull() null\endlink mask resets the mask. + + \sa mask(), createHeuristicMask(), TQBitmap +*/ + +void TQPixmap::setMask( const TQBitmap &newmask ) +{ + const TQPixmap *tmp = &newmask; // dec cxx bug + if ( (data == tmp->data) || + ( newmask.handle() && newmask.handle() == handle() ) ) { + TQPixmap m = tmp->copy( TRUE ); + setMask( *((TQBitmap*)&m) ); + data->selfmask = TRUE; // mask == pixmap + return; + } + + if ( newmask.isNull() ) { // reset the mask + if (data->mask) { + detach(); + data->selfmask = FALSE; + + delete data->mask; + data->mask = 0; + } + return; + } + + detach(); + data->selfmask = FALSE; + + if ( newmask.width() != width() || newmask.height() != height() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPixmap::setMask: The pixmap and the mask must have " + "the same size" ); +#endif + return; + } +#if defined(Q_WS_MAC) || (defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE)) + // when setting the mask, we get rid of the alpha channel completely + delete data->alphapm; + data->alphapm = 0; +#endif // Q_WS_X11 && !QT_NO_XFTFREETYPE + + delete data->mask; + TQBitmap* newmaskcopy; + if ( newmask.mask() ) + newmaskcopy = (TQBitmap*)new TQPixmap( tmp->copy( TRUE ) ); + else + newmaskcopy = new TQBitmap( newmask ); +#ifdef Q_WS_X11 + newmaskcopy->x11SetScreen( x11Screen() ); +#endif + data->mask = newmaskcopy; +} + + +/*! + \fn bool TQPixmap::selfMask() const + + Returns TRUE if the pixmap's mask is identical to the pixmap + itself; otherwise returns FALSE. + + \sa mask() +*/ + +#ifndef QT_NO_IMAGE_HEURISTIC_MASK +/*! + Creates and returns a heuristic mask for this pixmap. It works by + selecting a color from one of the corners and then chipping away + pixels of that color, starting at all the edges. + + The mask may not be perfect but it should be reasonable, so you + can do things such as the following: + \code + pm->setMask( pm->createHeuristicMask() ); + \endcode + + This function is slow because it involves transformation to a + TQImage, non-trivial computations and a transformation back to a + TQBitmap. + + If \a clipTight is TRUE the mask is just large enough to cover the + pixels; otherwise, the mask is larger than the data pixels. + + \sa TQImage::createHeuristicMask() +*/ + +TQBitmap TQPixmap::createHeuristicMask( bool clipTight ) const +{ + TQBitmap m; + m.convertFromImage( convertToImage().createHeuristicMask(clipTight) ); + return m; +} +#endif +#ifndef QT_NO_IMAGEIO +/*! + Returns a string that specifies the image format of the file \a + fileName, or 0 if the file cannot be read or if the format cannot + be recognized. + + The TQImageIO documentation lists the supported image formats. + + \sa load(), save() +*/ + +const char* TQPixmap::imageFormat( const TQString &fileName ) +{ + return TQImageIO::imageFormat(fileName); +} + +/*! + Loads a pixmap from the file \a fileName at runtime. Returns TRUE + if successful; otherwise returns FALSE. + + If \a format is specified, the loader attempts to read the pixmap + using the specified format. If \a format is not specified + (default), the loader reads a few bytes from the header to guess + the file's format. + + See the convertFromImage() documentation for a description of the + \a conversion_flags argument. + + The TQImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa loadFromData(), save(), imageFormat(), TQImage::load(), + TQImageIO +*/ + +bool TQPixmap::load( const TQString &fileName, const char *format, + int conversion_flags ) +{ + TQImageIO io( fileName, format ); + bool result = io.read(); + if ( result ) { + detach(); // ###hanord: Why detach here, convertFromImage does it + result = convertFromImage( io.image(), conversion_flags ); + } + return result; +} + +/*! + \overload + + Loads a pixmap from the file \a fileName at runtime. + + If \a format is specified, the loader attempts to read the pixmap + using the specified format. If \a format is not specified + (default), the loader reads a few bytes from the header to guess + the file's format. + + The \a mode is used to specify the color mode of the pixmap. + + \sa TQPixmap::ColorMode +*/ + +bool TQPixmap::load( const TQString &fileName, const char *format, + ColorMode mode ) +{ + int conversion_flags = 0; + switch (mode) { + case Color: + conversion_flags |= ColorOnly; + break; + case Mono: + conversion_flags |= MonoOnly; + break; + default: + break;// Nothing. + } + return load( fileName, format, conversion_flags ); +} +#endif //QT_NO_IMAGEIO + +/*! + \overload + + Converts \a image and sets this pixmap using color mode \a mode. + Returns TRUE if successful; otherwise returns FALSE. + + \sa TQPixmap::ColorMode +*/ + +bool TQPixmap::convertFromImage( const TQImage &image, ColorMode mode ) +{ + if ( image.isNull() ) { + // convert null image to null pixmap + *this = TQPixmap(); + return TRUE; + } + + int conversion_flags = 0; + switch (mode) { + case Color: + conversion_flags |= ColorOnly; + break; + case Mono: + conversion_flags |= MonoOnly; + break; + default: + break;// Nothing. + } + return convertFromImage( image, conversion_flags ); +} + +#ifndef QT_NO_IMAGEIO +/*! + Loads a pixmap from the binary data in \a buf (\a len bytes). + Returns TRUE if successful; otherwise returns FALSE. + + If \a format is specified, the loader attempts to read the pixmap + using the specified format. If \a format is not specified + (default), the loader reads a few bytes from the header to guess + the file's format. + + See the convertFromImage() documentation for a description of the + \a conversion_flags argument. + + The TQImageIO documentation lists the supported image formats and + explains how to add extra formats. + + \sa load(), save(), imageFormat(), TQImage::loadFromData(), + TQImageIO +*/ + +bool TQPixmap::loadFromData( const uchar *buf, uint len, const char *format, + int conversion_flags ) +{ + TQByteArray a; + a.setRawData( (char *)buf, len ); + TQBuffer b( a ); + b.open( IO_ReadOnly ); + TQImageIO io( &b, format ); + bool result = io.read(); + b.close(); + a.resetRawData( (char *)buf, len ); + if ( result ) { + detach(); + result = convertFromImage( io.image(), conversion_flags ); + } + return result; +} + +/*! + \overload + + Loads a pixmap from the binary data in \a buf (\a len bytes) using + color mode \a mode. Returns TRUE if successful; otherwise returns + FALSE. + + If \a format is specified, the loader attempts to read the pixmap + using the specified format. If \a format is not specified + (default), the loader reads a few bytes from the header to guess + the file's format. + + \sa TQPixmap::ColorMode +*/ + +bool TQPixmap::loadFromData( const uchar *buf, uint len, const char *format, + ColorMode mode ) +{ + int conversion_flags = 0; + switch (mode) { + case Color: + conversion_flags |= ColorOnly; + break; + case Mono: + conversion_flags |= MonoOnly; + break; + default: + break;// Nothing. + } + return loadFromData( buf, len, format, conversion_flags ); +} + +/*! + \overload +*/ + +bool TQPixmap::loadFromData( const TQByteArray &buf, const char *format, + int conversion_flags ) +{ + return loadFromData( (const uchar *)(buf.data()), buf.size(), + format, conversion_flags ); +} + + +/*! + Saves the pixmap to the file \a fileName using the image file + format \a format and a quality factor \a quality. \a quality must + be in the range [0,100] or -1. Specify 0 to obtain small + compressed files, 100 for large uncompressed files, and -1 to use + the default settings. Returns TRUE if successful; otherwise + returns FALSE. + + \sa load(), loadFromData(), imageFormat(), TQImage::save(), + TQImageIO +*/ + +bool TQPixmap::save( const TQString &fileName, const char *format, int quality ) const +{ + if ( isNull() ) + return FALSE; // nothing to save + TQImageIO io( fileName, format ); + return doImageIO( &io, quality ); +} + +/*! + \overload + + This function writes a TQPixmap to the TQIODevice, \a device. This + can be used, for example, to save a pixmap directly into a + TQByteArray: + \code + TQPixmap pixmap; + TQByteArray ba; + TQBuffer buffer( ba ); + buffer.open( IO_WriteOnly ); + pixmap.save( &buffer, "PNG" ); // writes pixmap into ba in PNG format + \endcode +*/ + +bool TQPixmap::save( TQIODevice* device, const char* format, int quality ) const +{ + if ( isNull() ) + return FALSE; // nothing to save + TQImageIO io( device, format ); + return doImageIO( &io, quality ); +} + +/*! \internal +*/ + +bool TQPixmap::doImageIO( TQImageIO* io, int quality ) const +{ + if ( !io ) + return FALSE; + io->setImage( convertToImage() ); +#if defined(QT_CHECK_RANGE) + if ( quality > 100 || quality < -1 ) + qWarning( "TQPixmap::save: quality out of range [-1,100]" ); +#endif + if ( quality >= 0 ) + io->setQuality( TQMIN(quality,100) ); + return io->write(); +} + +#endif //QT_NO_IMAGEIO + +/*! + \fn int TQPixmap::serialNumber() const + + Returns a number that uniquely identifies the contents of this + TQPixmap object. This means that multiple TQPixmap objects can have + the same serial number as long as they refer to the same contents. + + An example of where this is useful is for caching TQPixmaps. + + \sa TQPixmapCache +*/ + + +/*! + Returns the default pixmap optimization setting. + + \sa setDefaultOptimization(), setOptimization(), optimization() +*/ + +TQPixmap::Optimization TQPixmap::defaultOptimization() +{ + return defOptim; +} + +/*! + Sets the default pixmap optimization. + + All \e new pixmaps that are created will use this default + optimization. You may also set optimization for individual pixmaps + using the setOptimization() function. + + The initial default \a optimization setting is \c TQPixmap::Normal. + + \sa defaultOptimization(), setOptimization(), optimization() +*/ + +void TQPixmap::setDefaultOptimization( Optimization optimization ) +{ + if ( optimization != DefaultOptim ) + defOptim = optimization; +} + + +// helper for next function. +static TQPixmap grabChildWidgets( TQWidget * w ) +{ + TQPixmap res( w->width(), w->height() ); + if ( res.isNull() && w->width() ) + return res; + res.fill( w, TQPoint( 0, 0 ) ); + TQPaintDevice *oldRedirect = TQPainter::redirect( w ); + TQPainter::redirect( w, &res ); + bool dblbfr = TQSharedDoubleBuffer::isDisabled(); + TQSharedDoubleBuffer::setDisabled( TRUE ); + TQPaintEvent e( w->rect(), FALSE ); + TQApplication::sendEvent( w, &e ); + TQSharedDoubleBuffer::setDisabled( dblbfr ); + TQPainter::redirect( w, oldRedirect ); + + const TQObjectList * children = w->children(); + if ( children ) { + TQPainter p( &res ); + TQObjectListIt it( *children ); + TQObject * child; + while( (child=it.current()) != 0 ) { + ++it; + if ( child->isWidgetType() && + !((TQWidget *)child)->isHidden() && + !((TQWidget *)child)->isTopLevel() && + ((TQWidget *)child)->geometry().intersects( w->rect() ) ) { + // those conditions aren't tquite right, it's possible + // to have a grandchild completely outside its + // grandparent, but partially inside its parent. no + // point in optimizing for that. + + // make sure to evaluate pos() first - who knows what + // the paint event(s) inside grabChildWidgets() will do. + TQPoint childpos = ((TQWidget *)child)->pos(); + TQPixmap cpm = grabChildWidgets( (TQWidget *)child ); + if ( cpm.isNull() ) { + // Some child pixmap failed - abort and reset + res.resize( 0, 0 ); + break; + } + p.drawPixmap( childpos, cpm); + } + } + } + return res; +} + + +/*! + Creates a pixmap and paints \a widget in it. + + If the \a widget has any children, then they are also painted in + the appropriate positions. + + If you specify \a x, \a y, \a w or \a h, only the rectangle you + specify is painted. The defaults are 0, 0 (top-left corner) and + -1,-1 (which means the entire widget). + + (If \a w is negative, the function copies everything to the right + border of the window. If \a h is negative, the function copies + everything to the bottom of the window.) + + If \a widget is 0, or if the rectangle defined by \a x, \a y, the + modified \a w and the modified \a h does not overlap the \a + {widget}->rect(), this function will return a null TQPixmap. + + This function actually asks \a widget to paint itself (and its + children to paint themselves). TQPixmap::grabWindow() grabs pixels + off the screen, which is a bit faster and picks up \e exactly + what's on-screen. This function works by calling paintEvent() with + painter redirection turned on. If there are overlaying windows, + grabWindow() will see them, but not this function. + + If there is overlap, it returns a pixmap of the size you want, + containing a rendering of \a widget. If the rectangle you ask for + is a superset of \a widget, the areas outside \a widget are + covered with the widget's background. + + If an error occurs when trying to grab the widget, such as the + size of the widget being too large to fit in memory, an isNull() + pixmap is returned. + + \sa grabWindow() TQPainter::redirect() TQWidget::paintEvent() +*/ + +TQPixmap TQPixmap::grabWidget( TQWidget * widget, int x, int y, int w, int h ) +{ + TQPixmap res; + if ( !widget ) + return res; + + if ( w < 0 ) + w = widget->width() - x; + if ( h < 0 ) + h = widget->height() - y; + + TQRect wr( x, y, w, h ); + if ( wr == widget->rect() ) + return grabChildWidgets( widget ); + if ( !wr.intersects( widget->rect() ) ) + return res; + + res.resize( w, h ); + if( res.isNull() ) + return res; + res.fill( widget, TQPoint( w,h ) ); + TQPixmap tmp( grabChildWidgets( widget ) ); + if( tmp.isNull() ) + return tmp; + ::bitBlt( &res, 0, 0, &tmp, x, y, w, h ); + return res; +} + +/*! + Returns the actual matrix used for transforming a pixmap with \a w + width and \a h height and matrix \a matrix. + + When transforming a pixmap with xForm(), the transformation matrix + is internally adjusted to compensate for unwanted translation, + i.e. xForm() returns the smallest pixmap containing all + transformed points of the original pixmap. + + This function returns the modified matrix, which maps points + correctly from the original pixmap into the new pixmap. + + \sa xForm(), TQWMatrix +*/ +#ifndef QT_NO_PIXMAP_TRANSFORMATION +TQWMatrix TQPixmap::trueMatrix( const TQWMatrix &matrix, int w, int h ) +{ + const double dt = (double)0.; + double x1,y1, x2,y2, x3,y3, x4,y4; // get corners + double xx = (double)w; + double yy = (double)h; + + TQWMatrix mat( matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), 0., 0. ); + + mat.map( dt, dt, &x1, &y1 ); + mat.map( xx, dt, &x2, &y2 ); + mat.map( xx, yy, &x3, &y3 ); + mat.map( dt, yy, &x4, &y4 ); + + double ymin = y1; // lowest y value + if ( y2 < ymin ) ymin = y2; + if ( y3 < ymin ) ymin = y3; + if ( y4 < ymin ) ymin = y4; + double xmin = x1; // lowest x value + if ( x2 < xmin ) xmin = x2; + if ( x3 < xmin ) xmin = x3; + if ( x4 < xmin ) xmin = x4; + + double ymax = y1; // lowest y value + if ( y2 > ymax ) ymax = y2; + if ( y3 > ymax ) ymax = y3; + if ( y4 > ymax ) ymax = y4; + double xmax = x1; // lowest x value + if ( x2 > xmax ) xmax = x2; + if ( x3 > xmax ) xmax = x3; + if ( x4 > xmax ) xmax = x4; + + if ( xmax-xmin > 1.0 ) + xmin -= xmin/(xmax-xmin); + if ( ymax-ymin > 1.0 ) + ymin -= ymin/(ymax-ymin); + + mat.setMatrix( matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), -xmin, -ymin ); + return mat; +} +#endif // QT_NO_WMATRIX + + + + + +/***************************************************************************** + TQPixmap stream functions + *****************************************************************************/ +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +/*! + \relates TQPixmap + + Writes the pixmap \a pixmap to the stream \a s as a PNG image. + + Note that writing the stream to a file will not produce a valid image file. + + \sa TQPixmap::save() + \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQPixmap &pixmap ) +{ + s << pixmap.convertToImage(); + return s; +} + +/*! + \relates TQPixmap + + Reads a pixmap from the stream \a s into the pixmap \a pixmap. + + \sa TQPixmap::load() + \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQPixmap &pixmap ) +{ + TQImage img; + s >> img; + pixmap.convertFromImage( img ); + return s; +} + +#endif //QT_NO_DATASTREAM + + + + +/***************************************************************************** + TQPixmap (and TQImage) helper functions + *****************************************************************************/ +/* + This internal function contains the common (i.e. platform independent) code + to do a transformation of pixel data. It is used by TQPixmap::xForm() and by + TQImage::xForm(). + + \a trueMat is the true transformation matrix (see TQPixmap::trueMatrix()) and + \a xoffset is an offset to the matrix. + + \a msbfirst specifies for 1bpp images, if the MSB or LSB comes first and \a + depth specifies the colordepth of the data. + + \a dptr is a pointer to the destination data, \a dbpl specifies the bits per + line for the destination data, \a p_inc is the offset that we advance for + every scanline and \a dHeight is the height of the destination image. + + \a sprt is the pointer to the source data, \a sbpl specifies the bits per + line of the source data, \a sWidth and \a sHeight are the width and height of + the source data. +*/ +#ifndef QT_NO_PIXMAP_TRANSFORMATION +#undef IWX_MSB +#define IWX_MSB(b) if ( trigx < maxws && trigy < maxhs ) { \ + if ( *(sptr+sbpl*(trigy>>16)+(trigx>>19)) & \ + (1 << (7-((trigx>>16)&7))) ) \ + *dptr |= b; \ + } \ + trigx += m11; \ + trigy += m12; + // END OF MACRO +#undef IWX_LSB +#define IWX_LSB(b) if ( trigx < maxws && trigy < maxhs ) { \ + if ( *(sptr+sbpl*(trigy>>16)+(trigx>>19)) & \ + (1 << ((trigx>>16)&7)) ) \ + *dptr |= b; \ + } \ + trigx += m11; \ + trigy += m12; + // END OF MACRO +#undef IWX_PIX +#define IWX_PIX(b) if ( trigx < maxws && trigy < maxhs ) { \ + if ( (*(sptr+sbpl*(trigy>>16)+(trigx>>19)) & \ + (1 << (7-((trigx>>16)&7)))) == 0 ) \ + *dptr &= ~b; \ + } \ + trigx += m11; \ + trigy += m12; + // END OF MACRO +bool qt_xForm_helper( const TQWMatrix &trueMat, int xoffset, + int type, int depth, + uchar *dptr, int dbpl, int p_inc, int dHeight, + uchar *sptr, int sbpl, int sWidth, int sHeight + ) +{ + int m11 = int(trueMat.m11()*65536.0 + 1.); + int m12 = int(trueMat.m12()*65536.0 + 1.); + int m21 = int(trueMat.m21()*65536.0 + 1.); + int m22 = int(trueMat.m22()*65536.0 + 1.); + int dx = qRound(trueMat.dx() *65536.0); + int dy = qRound(trueMat.dy() *65536.0); + + int m21ydx = dx + (xoffset<<16); + int m22ydy = dy; + uint trigx; + uint trigy; + uint maxws = sWidth<<16; + uint maxhs = sHeight<<16; + + for ( int y=0; y>16)+(trigx>>16)); + trigx += m11; + trigy += m12; + dptr++; + } + break; + + case 16: // 16 bpp transform + while ( dptr < maxp ) { + if ( trigx < maxws && trigy < maxhs ) + *((ushort*)dptr) = *((ushort *)(sptr+sbpl*(trigy>>16) + + ((trigx>>16)<<1))); + trigx += m11; + trigy += m12; + dptr++; + dptr++; + } + break; + + case 24: { // 24 bpp transform + uchar *p2; + while ( dptr < maxp ) { + if ( trigx < maxws && trigy < maxhs ) { + p2 = sptr+sbpl*(trigy>>16) + ((trigx>>16)*3); + dptr[0] = p2[0]; + dptr[1] = p2[1]; + dptr[2] = p2[2]; + } + trigx += m11; + trigy += m12; + dptr += 3; + } + } + break; + + case 32: // 32 bpp transform + while ( dptr < maxp ) { + if ( trigx < maxws && trigy < maxhs ) + *((uint*)dptr) = *((uint *)(sptr+sbpl*(trigy>>16) + + ((trigx>>16)<<2))); + trigx += m11; + trigy += m12; + dptr += 4; + } + break; + + default: { + return FALSE; + } + } + } else { + switch ( type ) { + case QT_XFORM_TYPE_MSBFIRST: + while ( dptr < maxp ) { + IWX_MSB(128); + IWX_MSB(64); + IWX_MSB(32); + IWX_MSB(16); + IWX_MSB(8); + IWX_MSB(4); + IWX_MSB(2); + IWX_MSB(1); + dptr++; + } + break; + case QT_XFORM_TYPE_LSBFIRST: + while ( dptr < maxp ) { + IWX_LSB(1); + IWX_LSB(2); + IWX_LSB(4); + IWX_LSB(8); + IWX_LSB(16); + IWX_LSB(32); + IWX_LSB(64); + IWX_LSB(128); + dptr++; + } + break; +# if defined(Q_WS_WIN) + case QT_XFORM_TYPE_WINDOWSPIXMAP: + while ( dptr < maxp ) { + IWX_PIX(128); + IWX_PIX(64); + IWX_PIX(32); + IWX_PIX(16); + IWX_PIX(8); + IWX_PIX(4); + IWX_PIX(2); + IWX_PIX(1); + dptr++; + } + break; +# endif + } + } + m21ydx += m21; + m22ydy += m22; + dptr += p_inc; + } + return TRUE; +} +#undef IWX_MSB +#undef IWX_LSB +#undef IWX_PIX +#endif // QT_NO_PIXMAP_TRANSFORMATION diff --git a/src/kernel/qpixmap.h b/src/kernel/qpixmap.h new file mode 100644 index 000000000..86a061dcb --- /dev/null +++ b/src/kernel/qpixmap.h @@ -0,0 +1,354 @@ +/**************************************************************************** +** +** Definition of TQPixmap class +** +** Created : 940501 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPIXMAP_H +#define TQPIXMAP_H + +#ifndef QT_H +#include "qpaintdevice.h" +#include "qcolor.h" // char*->TQColor conversion +#include "qstring.h" // char*->TQString conversion +#include "qnamespace.h" +#endif // QT_H + +class TQGfx; +class TQPixmapPrivate; + +#if defined(Q_WS_WIN) +// Internal pixmap memory optimization class for Windows 9x +class TQMultiCellPixmap; +#endif + + +class Q_EXPORT TQPixmap : public TQPaintDevice, public TQt +{ +public: + enum ColorMode { Auto, Color, Mono }; + enum Optimization { DefaultOptim, NoOptim, MemoryOptim=NoOptim, + NormalOptim, BestOptim }; + + TQPixmap(); + TQPixmap( const TQImage& image ); + TQPixmap( int w, int h, int depth = -1, Optimization = DefaultOptim ); + TQPixmap( const TQSize &, int depth = -1, Optimization = DefaultOptim ); +#ifndef QT_NO_IMAGEIO + TQPixmap( const TQString& fileName, const char *format=0, + ColorMode mode=Auto ); + TQPixmap( const TQString& fileName, const char *format, + int conversion_flags ); + TQPixmap( const char *xpm[] ); // ### in 4.0, 'const char * const xpm[]'? + TQPixmap( const TQByteArray &data ); +#endif + TQPixmap( const TQPixmap & ); + ~TQPixmap(); + + TQPixmap &operator=( const TQPixmap & ); + TQPixmap &operator=( const TQImage & ); + + bool isNull() const; + + int width() const { return data->w; } + int height() const { return data->h; } + TQSize size() const { return TQSize(data->w,data->h); } + TQRect rect() const { return TQRect(0,0,data->w,data->h); } + int depth() const { return data->d; } + static int defaultDepth(); + + void fill( const TQColor &fillColor = TQt::white ); + void fill( const TQWidget *, int xofs, int yofs ); + void fill( const TQWidget *, const TQPoint &ofs ); + void resize( int width, int height ); + void resize( const TQSize & ); + + const TQBitmap *mask() const; + void setMask( const TQBitmap & ); + bool selfMask() const; + bool hasAlpha() const; + bool hasAlphaChannel() const; +#ifndef QT_NO_IMAGE_HEURISTIC_MASK + TQBitmap createHeuristicMask( bool clipTight = TRUE ) const; +#endif +#ifndef QT_NO_MIME + static TQPixmap fromMimeSource( const TQString& abs_name ); +#endif + static TQPixmap grabWindow( WId, int x=0, int y=0, int w=-1, int h=-1 ); + static TQPixmap grabWidget( TQWidget * widget, + int x=0, int y=0, int w=-1, int h=-1 ); + +#ifndef QT_NO_PIXMAP_TRANSFORMATION + TQPixmap xForm( const TQWMatrix & ) const; + static TQWMatrix trueMatrix( const TQWMatrix &, int w, int h ); +#endif + + TQImage convertToImage() const; + bool convertFromImage( const TQImage &, ColorMode mode=Auto ); + bool convertFromImage( const TQImage &, int conversion_flags ); +#ifndef QT_NO_IMAGEIO + static const char* imageFormat( const TQString &fileName ); + bool load( const TQString& fileName, const char *format=0, + ColorMode mode=Auto ); + bool load( const TQString& fileName, const char *format, + int conversion_flags ); + bool loadFromData( const uchar *buf, uint len, + const char* format=0, + ColorMode mode=Auto ); + bool loadFromData( const uchar *buf, uint len, + const char* format, + int conversion_flags ); + bool loadFromData( const TQByteArray &data, + const char* format=0, + int conversion_flags=0 ); + bool save( const TQString& fileName, const char* format, int quality = -1 ) const; + bool save( TQIODevice* device, const char* format, int quality = -1 ) const; +#endif + +#if defined(Q_WS_WIN) + HBITMAP hbm() const; +#endif + + int serialNumber() const; + + Optimization optimization() const; + void setOptimization( Optimization ); + static Optimization defaultOptimization(); + static void setDefaultOptimization( Optimization ); + + virtual void detach(); + + bool isTQBitmap() const; + +#if defined(Q_WS_WIN) + // These functions are internal and used by Windows 9x only + bool isMultiCellPixmap() const; + HDC multiCellHandle() const; + HBITMAP multiCellBitmap() const; + int multiCellOffset() const; + int allocCell(); + void freeCell( bool = FALSE ); +#endif + +#if defined(Q_WS_QWS) + virtual TQGfx * graphicsContext(bool clip_children=TRUE) const; + virtual unsigned char * scanLine(int) const; + virtual int bytesPerLine() const; + TQRgb * clut() const; + int numCols() const; +#elif defined(Q_WS_X11) + static int x11SetDefaultScreen( int screen ); + void x11SetScreen( int screen ); +#endif + +#ifndef Q_QDOC + Q_DUMMY_COMPARISON_OPERATOR(TQPixmap) +#endif + +protected: + TQPixmap( int w, int h, const uchar *data, bool isXbitmap ); + int metric( int ) const; + +#if defined(Q_WS_WIN) + struct TQMCPI { // mem optim for win9x + TQMultiCellPixmap *mcp; + int offset; + }; +#endif + + struct TQPixmapData : public TQShared { // internal pixmap data + TQCOORD w, h; + short d; + uint uninit : 1; + uint bitmap : 1; + uint selfmask : 1; +#if defined(Q_WS_WIN) + uint mcp : 1; +#endif + int ser_no; + TQBitmap *mask; +#if defined(Q_WS_WIN) + TQPixmap *maskpm; + union { + HBITMAP hbm; // if mcp == FALSE + TQMCPI *mcpi; // if mcp == TRUE + } hbm_or_mcpi; + uchar *realAlphaBits; +#ifdef Q_OS_TEMP + uchar* ppvBits; // Pointer to DIBSection bits +#endif +#elif defined(Q_WS_X11) + void *ximage; + void *maskgc; + TQPixmap *alphapm; +#elif defined(Q_WS_MAC) + ColorTable *clut; + TQPixmap *alphapm; +#elif defined(Q_WS_QWS) + int id; // ### should use TQPaintDevice::hd, since it is there + TQRgb * clut; + int numcols; + int rw; + int rh; + bool hasAlpha; +#endif + Optimization optim; +#if defined(Q_WS_WIN) + HBITMAP old_hbm; +#endif + } *data; +private: +#ifndef QT_NO_IMAGEIO + bool doImageIO( TQImageIO* io, int quality ) const; +#endif + TQPixmap( int w, int h, int depth, bool, Optimization ); + void init( int, int, int, bool, Optimization ); + void deref(); + TQPixmap copy( bool ignoreMask = FALSE ) const; +#if defined(Q_WS_WIN) + void initAlphaPixmap( uchar *bytes, int length, struct tagBITMAPINFO *bmi ); + void convertToAlphaPixmap( bool initAlpha=TRUE ); + static void bitBltAlphaPixmap( TQPixmap *dst, int dx, int dy, + const TQPixmap *src, int sx, int sy, + int sw, int sh, bool useDstAlpha ); +#endif + static Optimization defOptim; + friend Q_EXPORT void bitBlt( TQPaintDevice *, int, int, + const TQPaintDevice *, + int, int, int, int, RasterOp, bool ); + friend Q_EXPORT void bitBlt( TQPaintDevice *, int, int, + const TQImage* src, + int, int, int, int, int conversion_flags ); + friend Q_EXPORT void copyBlt( TQPixmap *dst, int dx, int dy, + const TQPixmap *src, int sx, int sy, + int sw, int sh ); + +#if defined(Q_WS_MAC) + friend void unclippedScaledBitBlt(TQPaintDevice *, int, int, int, int, + const TQPaintDevice *, int, int, int, int, + TQt::RasterOp, bool, bool); +#endif + + friend class TQBitmap; + friend class TQPaintDevice; + friend class TQPainter; + friend class TQGLWidget; +}; + + +inline bool TQPixmap::isNull() const +{ + return data->w == 0; +} + +inline void TQPixmap::fill( const TQWidget *w, const TQPoint &ofs ) +{ + fill( w, ofs.x(), ofs.y() ); +} + +inline void TQPixmap::resize( const TQSize &s ) +{ + resize( s.width(), s.height() ); +} + +inline const TQBitmap *TQPixmap::mask() const +{ + return data->mask; +} + +inline bool TQPixmap::selfMask() const +{ + return data->selfmask; +} + +#if defined(Q_WS_WIN) +inline HBITMAP TQPixmap::hbm() const +{ + return data->mcp ? 0 : data->hbm_or_mcpi.hbm; +} +#endif + +inline int TQPixmap::serialNumber() const +{ + return data->ser_no; +} + +inline TQPixmap::Optimization TQPixmap::optimization() const +{ + return data->optim; +} + +inline bool TQPixmap::isTQBitmap() const +{ + return data->bitmap; +} + +#if defined(Q_WS_WIN) +inline bool TQPixmap::isMultiCellPixmap() const +{ + return data->mcp; +} +#endif + + +/***************************************************************************** + TQPixmap stream functions + *****************************************************************************/ + +#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO) +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQPixmap & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQPixmap & ); +#endif + +/***************************************************************************** + TQPixmap (and TQImage) helper functions + *****************************************************************************/ + +#ifndef QT_NO_PIXMAP_TRANSFORMATION +# define QT_XFORM_TYPE_MSBFIRST 0 +# define QT_XFORM_TYPE_LSBFIRST 1 +# if defined(Q_WS_WIN) +# define QT_XFORM_TYPE_WINDOWSPIXMAP 2 +# endif +bool qt_xForm_helper( const TQWMatrix&, int, int, int, uchar*, int, int, int, uchar*, int, int, int ); +#endif + +Q_EXPORT void copyBlt( TQPixmap *dst, int dx, int dy, + const TQPixmap *src, int sx = 0, int sy = 0, + int sw = -1, int sh = -1 ); + +#endif // TQPIXMAP_H diff --git a/src/kernel/qpixmap_x11.cpp b/src/kernel/qpixmap_x11.cpp new file mode 100644 index 000000000..b5b087c3b --- /dev/null +++ b/src/kernel/qpixmap_x11.cpp @@ -0,0 +1,2476 @@ +/**************************************************************************** +** +** Implementation of TQPixmap class for X11 +** +** Created : 940501 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +// NOT REVISED + +#include "qplatformdefs.h" + +#if defined(Q_OS_WIN32) && defined(QT_MITSHM) +#undef QT_MITSHM +#endif + +#ifdef QT_MITSHM + +// Use the MIT Shared Memory extension for pixmap<->image conversions +#define QT_MITSHM_CONVERSIONS + +// Uncomment the next line to enable the MIT Shared Memory extension +// for TQPixmap::xForm() +// +// WARNING: This has some problems: +// +// 1. Consumes a 800x600 pixmap +// 2. TQt does not handle the ShmCompletion message, so you will +// get strange effects if you xForm() repeatedly. +// +// #define QT_MITSHM_XFORM + +#else +#undef QT_MITSHM_CONVERSIONS +#undef QT_MITSHM_XFORM +#endif + +#include "qbitmap.h" +#include "qpaintdevicemetrics.h" +#include "qimage.h" +#include "qwmatrix.h" +#include "qapplication.h" +#include "qt_x11_p.h" + +#include + +#if defined(Q_CC_MIPS) +# define for if(0){}else for +#endif + + +/*! + \class TQPixmap::TQPixmapData + \brief The TQPixmap::TQPixmapData class is an internal class. + \internal +*/ + + +// For thread-safety: +// image->data does not belong to X11, so we must free it ourselves. + +inline static void qSafeXDestroyImage( XImage *x ) +{ + if ( x->data ) { + free( x->data ); + x->data = 0; + } + XDestroyImage( x ); +} + + +/***************************************************************************** + MIT Shared Memory Extension support: makes xForm noticeably (~20%) faster. + *****************************************************************************/ + +#if defined(QT_MITSHM_XFORM) + +static bool xshminit = FALSE; +static XShmSegmentInfo xshminfo; +static XImage *xshmimg = 0; +static Pixmap xshmpm = 0; + +static void qt_cleanup_mitshm() +{ + if ( xshmimg == 0 ) + return; + Display *dpy = TQPaintDevice::x11AppDisplay(); + if ( xshmpm ) { + XFreePixmap( dpy, xshmpm ); + xshmpm = 0; + } + XShmDetach( dpy, &xshminfo ); xshmimg->data = 0; + qSafeXDestroyImage( xshmimg ); xshmimg = 0; + shmdt( xshminfo.shmaddr ); + shmctl( xshminfo.shmid, IPC_RMID, 0 ); +} + + +static bool qt_create_mitshm_buffer( const TQPaintDevice* dev, int w, int h ) +{ + static int major, minor; + static Bool pixmaps_ok; + Display *dpy = dev->x11Display(); + int dd = dev->x11Depth(); + Visual *vis = (Visual*)dev->x11Visual(); + + if ( xshminit ) { + qt_cleanup_mitshm(); + } else { + if ( !XShmQueryVersion(dpy, &major, &minor, &pixmaps_ok) ) + return FALSE; // MIT Shm not supported + qAddPostRoutine( qt_cleanup_mitshm ); + xshminit = TRUE; + } + + xshmimg = XShmCreateImage( dpy, vis, dd, ZPixmap, 0, &xshminfo, w, h ); + if ( !xshmimg ) + return FALSE; + + bool ok; + xshminfo.shmid = shmget( IPC_PRIVATE, + xshmimg->bytes_per_line * xshmimg->height, + IPC_CREAT | 0777 ); + ok = xshminfo.shmid != -1; + if ( ok ) { + xshmimg->data = (char*)shmat( xshminfo.shmid, 0, 0 ); + xshminfo.shmaddr = xshmimg->data; + ok = ( xshminfo.shmaddr != (char*)-1 ); + } + xshminfo.readOnly = FALSE; + if ( ok ) + ok = XShmAttach( dpy, &xshminfo ); + if ( !ok ) { + qSafeXDestroyImage( xshmimg ); + xshmimg = 0; + if ( xshminfo.shmaddr ) + shmdt( xshminfo.shmaddr ); + if ( xshminfo.shmid != -1 ) + shmctl( xshminfo.shmid, IPC_RMID, 0 ); + return FALSE; + } + if ( pixmaps_ok ) + xshmpm = XShmCreatePixmap( dpy, DefaultRootWindow(dpy), xshmimg->data, + &xshminfo, w, h, dd ); + + return TRUE; +} + +#else + +// If extern, need a dummy. +// +// static bool qt_create_mitshm_buffer( TQPaintDevice*, int, int ) +// { +// return FALSE; +// } + +#endif // QT_MITSHM_XFORM + +#ifdef QT_MITSHM_CONVERSIONS + +static bool qt_mitshm_error = false; +static int qt_mitshm_errorhandler( Display*, XErrorEvent* ) +{ + qt_mitshm_error = true; + return 0; +} + +static XImage* qt_XShmCreateImage( Display* dpy, Visual* visual, unsigned int depth, + int format, int /*offset*/, char* /*data*/, unsigned int width, unsigned int height, + int /*bitmap_pad*/, int /*bytes_per_line*/, XShmSegmentInfo* shminfo ) +{ + if( width * height * depth < 100*100*32 ) + return NULL; + static int shm_inited = -1; + if( shm_inited == -1 ) { + if( XShmQueryExtension( dpy )) + shm_inited = 1; + else + shm_inited = 0; + } + if( shm_inited == 0 ) + return NULL; + XImage* xi = XShmCreateImage( dpy, visual, depth, format, NULL, shminfo, width, + height ); + if( xi == NULL ) + return NULL; + shminfo->shmid = shmget( IPC_PRIVATE, xi->bytes_per_line * xi->height, + IPC_CREAT|0600); + if( shminfo->shmid < 0 ) { + XDestroyImage( xi ); + return NULL; + } + shminfo->readOnly = False; + shminfo->shmaddr = (char*)shmat( shminfo->shmid, 0, 0 ); + if( shminfo->shmaddr == (char*)-1 ) { + XDestroyImage( xi ); + shmctl( shminfo->shmid, IPC_RMID, 0 ); + return NULL; + } + xi->data = shminfo->shmaddr; +#ifndef QT_MITSHM_RMID_IGNORES_REFCOUNT + // mark as deleted to automatically free the memory in case + // of a crash (but this doesn't work e.g. on Solaris) + shmctl( shminfo->shmid, IPC_RMID, 0 ); +#endif + if( shm_inited == 1 ) { // first time + XErrorHandler old_h = XSetErrorHandler( qt_mitshm_errorhandler ); + XShmAttach( dpy, shminfo ); + shm_inited = 2; + XSync( dpy, False ); + XSetErrorHandler( old_h ); + if( qt_mitshm_error ) { // oops ... perhaps we are remote? + shm_inited = 0; + XDestroyImage( xi ); + shmdt( shminfo->shmaddr ); +#ifdef QT_MITSHM_RMID_IGNORES_REFCOUNT + shmctl( shminfo->shmid, IPC_RMID, 0 ); +#endif + return NULL; + } + } else + XShmAttach( dpy, shminfo ); + return xi; +} + +static void qt_XShmDestroyImage( XImage* xi, XShmSegmentInfo* shminfo ) +{ + XShmDetach( TQPaintDevice::x11AppDisplay(), shminfo ); + XDestroyImage( xi ); + shmdt( shminfo->shmaddr ); +#ifdef QT_MITSHM_RMID_IGNORES_REFCOUNT + shmctl( shminfo->shmid, IPC_RMID, 0 ); +#endif +} + +static XImage* qt_XShmGetImage( const TQPixmap* pix, int format, + XShmSegmentInfo* shminfo ) +{ + XImage* xi = qt_XShmCreateImage( pix->x11Display(), (Visual*)pix->x11Visual(), + pix->depth(), format, 0, 0, pix->width(), pix->height(), 32, 0, shminfo ); + if( xi == NULL ) + return NULL; + if( XShmGetImage( pix->x11Display(), pix->handle(), xi, 0, 0, AllPlanes ) == False ) { + qt_XShmDestroyImage( xi, shminfo ); + return NULL; + } + return xi; +} + +#endif // QT_MITSHM_CONVERSIONS + +/***************************************************************************** + Internal functions + *****************************************************************************/ + +extern const uchar *qt_get_bitflip_array(); // defined in qimage.cpp + +static uchar *flip_bits( const uchar *bits, int len ) +{ + register const uchar *p = bits; + const uchar *end = p + len; + uchar *newdata = new uchar[len]; + uchar *b = newdata; + const uchar *f = qt_get_bitflip_array(); + while ( p < end ) + *b++ = f[*p++]; + return newdata; +} + +// Returns position of highest bit set or -1 if none +static int highest_bit( uint v ) +{ + int i; + uint b = (uint)1 << 31; + for ( i=31; ((b & v) == 0) && i>=0; i-- ) + b >>= 1; + return i; +} + +// Returns position of lowest set bit in 'v' as an integer (0-31), or -1 +static int lowest_bit( uint v ) +{ + int i; + ulong lb; + lb = 1; + for (i=0; ((v & lb) == 0) && i<32; i++, lb<<=1); + return i==32 ? -1 : i; +} + +// Counts the number of bits set in 'v' +static uint n_bits( uint v ) +{ + int i = 0; + while ( v ) { + v = v & (v - 1); + i++; + } + return i; +} + +static uint *red_scale_table = 0; +static uint *green_scale_table = 0; +static uint *blue_scale_table = 0; + +static void cleanup_scale_tables() +{ + delete[] red_scale_table; + delete[] green_scale_table; + delete[] blue_scale_table; +} + +/* + Could do smart bitshifting, but the "obvious" algorithm only works for + nBits >= 4. This is more robust. +*/ +static void build_scale_table( uint **table, uint nBits ) +{ + if ( nBits > 7 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "build_scale_table: internal error, nBits = %i", nBits ); +#endif + return; + } + if (!*table) { + static bool firstTable = TRUE; + if ( firstTable ) { + qAddPostRoutine( cleanup_scale_tables ); + firstTable = FALSE; + } + *table = new uint[256]; + } + int maxVal = (1 << nBits) - 1; + int valShift = 8 - nBits; + int i; + for( i = 0 ; i < maxVal + 1 ; i++ ) + (*table)[i << valShift] = i*255/maxVal; +} + +static int defaultScreen = -1; + +extern bool qt_use_xrender; // defined in qapplication_x11.cpp +extern bool qt_has_xft; // defined in qfont_x11.cpp + +#ifndef QT_NO_XFTFREETYPE +#ifndef QT_XFT2 +// Xft1 doesn't have XftDrawCreateAlpha, so we fake it in qtaddons_x11.cpp +extern "C" XftDraw *XftDrawCreateAlpha( Display *, TQt::HANDLE, int ); +#endif // QT_XFT2 +#endif // QT_NO_XFTFREETYPE + +/***************************************************************************** + TQPixmap member functions + *****************************************************************************/ + +/*! + \internal + Initializes the pixmap data. +*/ + +void TQPixmap::init( int w, int h, int d, bool bitmap, Optimization optim ) +{ +#if defined(QT_CHECK_STATE) + if ( qApp->type() == TQApplication::Tty ) { + qWarning( "TQPixmap: Cannot create a TQPixmap when no GUI " + "is being used" ); + } +#endif + + static int serial = 0; + + if ( defaultScreen >= 0 && defaultScreen != x11Screen() ) { + TQPaintDeviceX11Data* xd = getX11Data( TRUE ); + xd->x_screen = defaultScreen; + xd->x_depth = TQPaintDevice::x11AppDepth( xd->x_screen ); + xd->x_cells = TQPaintDevice::x11AppCells( xd->x_screen ); + xd->x_colormap = TQPaintDevice::x11AppColormap( xd->x_screen ); + xd->x_defcolormap = TQPaintDevice::x11AppDefaultColormap( xd->x_screen ); + xd->x_visual = TQPaintDevice::x11AppVisual( xd->x_screen ); + xd->x_defvisual = TQPaintDevice::x11AppDefaultVisual( xd->x_screen ); + setX11Data( xd ); + } + + int dd = x11Depth(); + + if ( d != -1 ) + dd = d; + + if ( optim == DefaultOptim ) // use default optimization + optim = defOptim; + + data = new TQPixmapData; + Q_CHECK_PTR( data ); + + memset( data, 0, sizeof(TQPixmapData) ); + data->count = 1; + data->uninit = TRUE; + data->bitmap = bitmap; + data->ser_no = ++serial; + data->optim = optim; + + bool make_null = w == 0 || h == 0; // create null pixmap + if ( d == 1 ) // monocrome pixmap + data->d = 1; + else if ( d < 0 || d == dd ) // def depth pixmap + data->d = dd; + if ( make_null || w < 0 || h < 0 || data->d == 0 ) { + hd = 0; + rendhd = 0; +#if defined(QT_CHECK_RANGE) + if ( !make_null ) + qWarning( "TQPixmap: Invalid pixmap parameters" ); +#endif + return; + } + data->w = w; + data->h = h; + hd = (HANDLE)XCreatePixmap( x11Display(), RootWindow(x11Display(), x11Screen() ), + w, h, data->d ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft ) { + if ( data->d == 1 ) { + rendhd = (HANDLE) XftDrawCreateBitmap( x11Display(), hd ); + } else { + rendhd = (HANDLE) XftDrawCreate( x11Display(), hd, + (Visual *) x11Visual(), + x11Colormap() ); + } + } +#endif // QT_NO_XFTFREETYPE + +} + + +void TQPixmap::deref() +{ + if ( data && data->deref() ) { // last reference lost + delete data->mask; + delete data->alphapm; + if ( data->ximage ) + qSafeXDestroyImage( (XImage*)data->ximage ); + if ( data->maskgc ) + XFreeGC( x11Display(), (GC)data->maskgc ); + if ( qApp && hd) { + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + XftDrawDestroy( (XftDraw *) rendhd ); + rendhd = 0; + } +#endif // QT_NO_XFTFREETYPE + + XFreePixmap( x11Display(), hd ); + hd = 0; + } + delete data; + } +} + + +/*! + Constructs a monochrome pixmap, with width \a w and height \a h, + that is initialized with the data in \a bits. The \a isXbitmap + indicates whether the data is an X bitmap and defaults to FALSE. + This constructor is protected and used by the TQBitmap class. +*/ + +TQPixmap::TQPixmap( int w, int h, const uchar *bits, bool isXbitmap) + : TQPaintDevice( TQInternal::Pixmap ) +{ // for bitmaps only + init( 0, 0, 0, FALSE, defOptim ); + if ( w <= 0 || h <= 0 ) // create null pixmap + return; + + data->uninit = FALSE; + data->w = w; + data->h = h; + data->d = 1; + uchar *flipped_bits; + if ( isXbitmap ) { + flipped_bits = 0; + } else { // not X bitmap -> flip bits + flipped_bits = flip_bits( bits, ((w+7)/8)*h ); + bits = flipped_bits; + } + hd = (HANDLE)XCreateBitmapFromData( x11Display(), + RootWindow(x11Display(), x11Screen() ), + (char *)bits, w, h ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft ) + rendhd = (HANDLE) XftDrawCreateBitmap (x11Display (), hd); +#endif // QT_NO_XFTFREETYPE + + if ( flipped_bits ) // Avoid purify complaint + delete [] flipped_bits; +} + + +/*! + This is a special-purpose function that detaches the pixmap from + shared pixmap data. + + A pixmap is automatically detached by TQt whenever its contents is + about to change. This is done in all TQPixmap member functions + that modify the pixmap (fill(), resize(), convertFromImage(), + load(), etc.), in bitBlt() for the destination pixmap and in + TQPainter::begin() on a pixmap. + + It is possible to modify a pixmap without letting TQt know. You can + first obtain the system-dependent handle() and then call + system-specific functions (for instance, BitBlt under Windows) + that modify the pixmap contents. In such cases, you can call + detach() to cut the pixmap loose from other pixmaps that share + data with this one. + + detach() returns immediately if there is just a single reference + or if the pixmap has not been initialized yet. +*/ + +void TQPixmap::detach() +{ + if ( data->count != 1 ) + *this = copy(); + data->uninit = FALSE; + + // reset cached data + if ( data->ximage ) { + qSafeXDestroyImage( (XImage*)data->ximage ); + data->ximage = 0; + } + if ( data->maskgc ) { + XFreeGC( x11Display(), (GC)data->maskgc ); + data->maskgc = 0; + } +} + + +/*! + Returns the default pixmap depth, i.e. the depth a pixmap gets if + -1 is specified. + + \sa depth() +*/ + +int TQPixmap::defaultDepth() +{ + return x11AppDepth(); +} + + +/*! + \fn TQPixmap::Optimization TQPixmap::optimization() const + + Returns the optimization setting for this pixmap. + + The default optimization setting is \c TQPixmap::NormalOptim. You + can change this setting in two ways: + \list + \i Call setDefaultOptimization() to set the default optimization + for all new pixmaps. + \i Call setOptimization() to set the optimization for individual + pixmaps. + \endlist + + \sa setOptimization(), setDefaultOptimization(), defaultOptimization() +*/ + +/*! + Sets pixmap drawing optimization for this pixmap. + + The \a optimization setting affects pixmap operations, in + particular drawing of transparent pixmaps (bitBlt() a pixmap with + a mask set) and pixmap transformations (the xForm() function). + + Pixmap optimization involves keeping intermediate results in a + cache buffer and using the cache to speed up bitBlt() and xForm(). + The cost is more memory consumption, up to twice as much as an + unoptimized pixmap. + + Use the setDefaultOptimization() to change the default + optimization for all new pixmaps. + + \sa optimization(), setDefaultOptimization(), defaultOptimization() +*/ + +void TQPixmap::setOptimization( Optimization optimization ) +{ + if ( optimization == data->optim ) + return; + detach(); + data->optim = optimization == DefaultOptim ? + defOptim : optimization; + if ( data->optim == MemoryOptim && data->ximage ) { + qSafeXDestroyImage( (XImage*)data->ximage ); + data->ximage = 0; + } +} + + +/*! + Fills the pixmap with the color \a fillColor. +*/ + +void TQPixmap::fill( const TQColor &fillColor ) +{ + if ( isNull() ) + return; + detach(); // detach other references + GC gc = qt_xget_temp_gc( x11Screen(), depth()==1 ); + XSetForeground( x11Display(), gc, fillColor.pixel(x11Screen()) ); + XFillRectangle( x11Display(), hd, gc, 0, 0, width(), height() ); +} + + +/*! + Internal implementation of the virtual TQPaintDevice::metric() function. + + Use the TQPaintDeviceMetrics class instead. + + \a m is the metric to get. +*/ + +int TQPixmap::metric( int m ) const +{ + int val; + if ( m == TQPaintDeviceMetrics::PdmWidth ) + val = width(); + else if ( m == TQPaintDeviceMetrics::PdmHeight ) { + val = height(); + } else { + Display *dpy = x11Display(); + int scr = x11Screen(); + switch ( m ) { + case TQPaintDeviceMetrics::PdmDpiX: + case TQPaintDeviceMetrics::PdmPhysicalDpiX: + val = TQPaintDevice::x11AppDpiX( scr ); + break; + case TQPaintDeviceMetrics::PdmDpiY: + case TQPaintDeviceMetrics::PdmPhysicalDpiY: + val = TQPaintDevice::x11AppDpiY( scr ); + break; + case TQPaintDeviceMetrics::PdmWidthMM: + val = (DisplayWidthMM(dpy,scr)*width())/ + DisplayWidth(dpy,scr); + break; + case TQPaintDeviceMetrics::PdmHeightMM: + val = (DisplayHeightMM(dpy,scr)*height())/ + DisplayHeight(dpy,scr); + break; + case TQPaintDeviceMetrics::PdmNumColors: + val = 1 << depth(); + break; + case TQPaintDeviceMetrics::PdmDepth: + val = depth(); + break; + default: + val = 0; +#if defined(QT_CHECK_RANGE) + qWarning( "TQPixmap::metric: Invalid metric command" ); +#endif + } + } + return val; +} + +/*! + Converts the pixmap to a TQImage. Returns a null image if it fails. + + If the pixmap has 1-bit depth, the returned image will also be 1 + bit deep. If the pixmap has 2- to 8-bit depth, the returned image + has 8-bit depth. If the pixmap has greater than 8-bit depth, the + returned image has 32-bit depth. + + Note that for the moment, alpha masks on monochrome images are + ignored. + + \sa convertFromImage() +*/ + +TQImage TQPixmap::convertToImage() const +{ + TQImage image; + if ( isNull() ) + return image; // null image + + int w = width(); + int h = height(); + int d = depth(); + bool mono = d == 1; + Visual *visual = (Visual *)x11Visual(); + bool trucol = (visual->c_class == TrueColor || visual->c_class == DirectColor) && !mono && d > 8; + + if ( d > 1 && d <= 8 ) // set to nearest valid depth + d = 8; // 2..8 ==> 8 + // we could run into the situation where d == 8 AND trucol is true, which can + // cause problems when converting to and from images. in this case, always treat + // the depth as 32... from Klaus Schmidinger and qt-bugs/arc-15/31333. + if ( d > 8 || trucol ) + d = 32; // > 8 ==> 32 + + XImage *xi = (XImage *)data->ximage; // any cached ximage? +#ifdef QT_MITSHM_CONVERSIONS + bool mitshm_ximage = false; + XShmSegmentInfo shminfo; +#endif + if ( !xi ) { // fetch data from X server +#ifdef QT_MITSHM_CONVERSIONS + xi = qt_XShmGetImage( this, mono ? XYPixmap : ZPixmap, &shminfo ); + if( xi ) { + mitshm_ximage = true; + } else +#endif + xi = XGetImage( x11Display(), hd, 0, 0, w, h, AllPlanes, + mono ? XYPixmap : ZPixmap ); + } + Q_CHECK_PTR( xi ); + if (!xi) + return image; // null image + + TQImage::Endian bitOrder = TQImage::IgnoreEndian; + if ( mono ) { + bitOrder = xi->bitmap_bit_order == LSBFirst ? + TQImage::LittleEndian : TQImage::BigEndian; + } + image.create( w, h, d, 0, bitOrder ); + if ( image.isNull() ) { // could not create image +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + qt_XShmDestroyImage( xi, &shminfo ); + else +#endif + qSafeXDestroyImage( xi ); + return image; + } + + const TQPixmap* msk = mask(); + const TQPixmap *alf = data->alphapm; + + TQImage alpha; + if (alf) { + XImage* axi; +#ifdef QT_MITSHM_CONVERSIONS + bool mitshm_aximage = false; + XShmSegmentInfo ashminfo; + axi = qt_XShmGetImage( alf, ZPixmap, &ashminfo ); + if( axi ) { + mitshm_aximage = true; + } else +#endif + axi = XGetImage(x11Display(), alf->hd, 0, 0, w, h, AllPlanes, ZPixmap); + + if (axi) { + image.setAlphaBuffer( TRUE ); + alpha.create(w, h, 8); + + // copy each scanline + char *src = axi->data; + int bpl = TQMIN(alpha.bytesPerLine(), axi->bytes_per_line); + for (int y = 0; y < h; y++ ) { + memcpy( alpha.scanLine(y), src, bpl ); + src += axi->bytes_per_line; + } + +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_aximage ) + qt_XShmDestroyImage( axi, &ashminfo ); + else +#endif + qSafeXDestroyImage( axi ); + } + } else if (msk) { + image.setAlphaBuffer( TRUE ); + alpha = msk->convertToImage(); + } + bool ale = alpha.bitOrder() == TQImage::LittleEndian; + + if ( trucol ) { // truecolor + const uint red_mask = (uint)visual->red_mask; + const uint green_mask = (uint)visual->green_mask; + const uint blue_mask = (uint)visual->blue_mask; + const int red_shift = highest_bit( red_mask ) - 7; + const int green_shift = highest_bit( green_mask ) - 7; + const int blue_shift = highest_bit( blue_mask ) - 7; + + const uint red_bits = n_bits( red_mask ); + const uint green_bits = n_bits( green_mask ); + const uint blue_bits = n_bits( blue_mask ); + + static uint red_table_bits = 0; + static uint green_table_bits = 0; + static uint blue_table_bits = 0; + + if ( red_bits < 8 && red_table_bits != red_bits) { + build_scale_table( &red_scale_table, red_bits ); + red_table_bits = red_bits; + } + if ( blue_bits < 8 && blue_table_bits != blue_bits) { + build_scale_table( &blue_scale_table, blue_bits ); + blue_table_bits = blue_bits; + } + if ( green_bits < 8 && green_table_bits != green_bits) { + build_scale_table( &green_scale_table, green_bits ); + green_table_bits = green_bits; + } + + int r, g, b; + + TQRgb *dst; + uchar *src; + uint pixel; + int bppc = xi->bits_per_pixel; + + if ( bppc > 8 && xi->byte_order == LSBFirst ) + bppc++; + + for ( int y=0; ydata + xi->bytes_per_line*y; + for ( int x=0; x 0 ) + r = (pixel & red_mask) >> red_shift; + else + r = (pixel & red_mask) << -red_shift; + if ( green_shift > 0 ) + g = (pixel & green_mask) >> green_shift; + else + g = (pixel & green_mask) << -green_shift; + if ( blue_shift > 0 ) + b = (pixel & blue_mask) >> blue_shift; + else + b = (pixel & blue_mask) << -blue_shift; + + if ( red_bits < 8 ) + r = red_scale_table[r]; + if ( green_bits < 8 ) + g = green_scale_table[g]; + if ( blue_bits < 8 ) + b = blue_scale_table[b]; + + if (alf) { + *dst++ = qRgba(r, g, b, asrc[x]); + } else if (msk) { + if ( ale ) { + *dst++ = (asrc[x >> 3] & (1 << (x & 7))) + ? qRgba(r, g, b, 0xff) : qRgba(r, g, b, 0x00); + } else { + *dst++ = (asrc[x >> 3] & (1 << (7 -(x & 7)))) + ? qRgba(r, g, b, 0xff) : qRgba(r, g, b, 0x00); + } + } else { + *dst++ = qRgb(r, g, b); + } + } + } + } else if ( xi->bits_per_pixel == d ) { // compatible depth + char *xidata = xi->data; // copy each scanline + int bpl = TQMIN(image.bytesPerLine(),xi->bytes_per_line); + for ( int y=0; ybytes_per_line; + } + } else { + /* Typically 2 or 4 bits display depth */ +#if defined(QT_CHECK_RANGE) + qWarning( "TQPixmap::convertToImage: Display not supported (bpp=%d)", + xi->bits_per_pixel ); +#endif + image.reset(); +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + qt_XShmDestroyImage( xi, &shminfo ); + else +#endif + qSafeXDestroyImage( xi ); + return image; + } + + if ( mono ) { // bitmap + image.setNumColors( 2 ); + image.setColor( 0, qRgb(255,255,255) ); + image.setColor( 1, qRgb(0,0,0) ); + } else if ( !trucol ) { // pixmap with colormap + register uchar *p; + uchar *end; + uchar use[256]; // pixel-in-use table + uchar pix[256]; // pixel translation table + int ncols, i, bpl; + memset( use, 0, 256 ); + memset( pix, 0, 256 ); + bpl = image.bytesPerLine(); + + if (msk) { // which pixels are used? + for ( i=0; i> 3] & (1 << (x & 7))) + use[*p] = 1; + } else { + if (asrc[x >> 3] & (1 << (7 -(x & 7)))) + use[*p] = 1; + } + ++p; + } + } + } else { + for ( i=0; i> 3] & (1 << (x & 7)))) + *p = trans; + } else { + if (!(asrc[x >> 3] & (1 << (7 -(x & 7))))) + *p = trans; + } + ++p; + } + } + } else { + image.setNumColors( ncols ); // create color table + } + int j = 0; + for ( i=0; i<256; i++ ) { // translate pixels + if ( use[i] ) { + image.setColor( j++, + ( msk ? 0xff000000 : 0 ) + | qRgb( (carr[i].red >> 8) & 255, + (carr[i].green >> 8) & 255, + (carr[i].blue >> 8) & 255 ) ); + } + } + + delete [] carr; + } + if ( data->optim != BestOptim ) { // throw away image data +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + qt_XShmDestroyImage( xi, &shminfo ); + else +#endif + qSafeXDestroyImage( xi ); + ((TQPixmap*)this)->data->ximage = 0; + } else { // keep ximage data +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) { // copy the XImage? + qt_XShmDestroyImage( xi, &shminfo ); + xi = 0; + } +#endif + ((TQPixmap*)this)->data->ximage = xi; + } + + return image; +} + + +/*! + Converts image \a img and sets this pixmap. Returns TRUE if + successful; otherwise returns FALSE. + + The \a conversion_flags argument is a bitwise-OR of the + \l{TQt::ImageConversionFlags}. Passing 0 for \a conversion_flags + sets all the default options. + + Note that even though a TQPixmap with depth 1 behaves much like a + TQBitmap, isTQBitmap() returns FALSE. + + If a pixmap with depth 1 is painted with color0 and color1 and + converted to an image, the pixels painted with color0 will produce + pixel index 0 in the image and those painted with color1 will + produce pixel index 1. + + \sa convertToImage(), isTQBitmap(), TQImage::convertDepth(), + defaultDepth(), TQImage::hasAlphaBuffer() +*/ + +bool TQPixmap::convertFromImage( const TQImage &img, int conversion_flags ) +{ + if ( img.isNull() ) { +#if defined(QT_CHECK_NULL) + qWarning( "TQPixmap::convertFromImage: Cannot convert a null image" ); +#endif + return FALSE; + } + detach(); // detach other references + TQImage image = img; + const uint w = image.width(); + const uint h = image.height(); + int d = image.depth(); + const int dd = x11Depth(); + bool force_mono = (dd == 1 || isTQBitmap() || + (conversion_flags & ColorMode_Mask)==MonoOnly ); + + if ( w >= 32768 || h >= 32768 ) + return FALSE; + + // get rid of the mask + delete data->mask; + data->mask = 0; + + // get rid of alpha pixmap + delete data->alphapm; + data->alphapm = 0; + + // must be monochrome + if ( force_mono ) { + if ( d != 1 ) { + // dither + image = image.convertDepth( 1, conversion_flags ); + d = 1; + } + } else { // can be both + bool conv8 = FALSE; + if ( d > 8 && dd <= 8 ) { // convert to 8 bit + if ( (conversion_flags & DitherMode_Mask) == AutoDither ) + conversion_flags = (conversion_flags & ~DitherMode_Mask) + | PreferDither; + conv8 = TRUE; + } else if ( (conversion_flags & ColorMode_Mask) == ColorOnly ) { + conv8 = d == 1; // native depth wanted + } else if ( d == 1 ) { + if ( image.numColors() == 2 ) { + TQRgb c0 = image.color(0); // Auto: convert to best + TQRgb c1 = image.color(1); + conv8 = TQMIN(c0,c1) != qRgb(0,0,0) || TQMAX(c0,c1) != qRgb(255,255,255); + } else { + // eg. 1-color monochrome images (they do exist). + conv8 = TRUE; + } + } + if ( conv8 ) { + image = image.convertDepth( 8, conversion_flags ); + d = 8; + } + } + + if ( d == 1 ) { // 1 bit pixmap (bitmap) + if ( hd ) { // delete old X pixmap + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + XftDrawDestroy( (XftDraw *) rendhd ); + rendhd = 0; + } +#endif // QT_NO_XFTFREETYPE + + XFreePixmap( x11Display(), hd ); + } + + // make sure image.color(0) == color0 (white) and image.color(1) == color1 (black) + if (image.color(0) == TQt::black.rgb() && image.color(1) == TQt::white.rgb()) { + image.invertPixels(); + image.setColor(0, TQt::white.rgb()); + image.setColor(1, TQt::black.rgb()); + } + + char *bits; + uchar *tmp_bits; + int bpl = (w+7)/8; + int ibpl = image.bytesPerLine(); + if ( image.bitOrder() == TQImage::BigEndian || bpl != ibpl ) { + tmp_bits = new uchar[bpl*h]; + Q_CHECK_PTR( tmp_bits ); + bits = (char *)tmp_bits; + uchar *p, *b, *end; + uint y, count; + if ( image.bitOrder() == TQImage::BigEndian ) { + const uchar *f = qt_get_bitflip_array(); + b = tmp_bits; + for ( y=0; y 4 ) { + *b++ = f[*p++]; + *b++ = f[*p++]; + *b++ = f[*p++]; + *b++ = f[*p++]; + count -= 4; + } + while ( p < end ) + *b++ = f[*p++]; + } + } else { // just copy + b = tmp_bits; + p = image.scanLine( 0 ); + for ( y=0; yw = w; data->h = h; data->d = 1; + + if ( image.hasAlphaBuffer() ) { + TQBitmap m; + m = image.createAlphaMask( conversion_flags ); + setMask( m ); + } + return TRUE; + } + + Display *dpy = x11Display(); + Visual *visual = (Visual *)x11Visual(); + XImage *xi = 0; + bool trucol = (visual->c_class == TrueColor || visual->c_class == DirectColor); + int nbytes = image.numBytes(); + uchar *newbits= 0; + int newbits_size = 0; +#ifdef QT_MITSHM_CONVERSIONS + bool mitshm_ximage = false; + XShmSegmentInfo shminfo; +#endif + + if ( trucol ) { // truecolor display + TQRgb pix[256]; // pixel translation table + const bool d8 = d == 8; + const uint red_mask = (uint)visual->red_mask; + const uint green_mask = (uint)visual->green_mask; + const uint blue_mask = (uint)visual->blue_mask; + const int red_shift = highest_bit( red_mask ) - 7; + const int green_shift = highest_bit( green_mask ) - 7; + const int blue_shift = highest_bit( blue_mask ) - 7; + const uint rbits = highest_bit(red_mask) - lowest_bit(red_mask) + 1; + const uint gbits = highest_bit(green_mask) - lowest_bit(green_mask) + 1; + const uint bbits = highest_bit(blue_mask) - lowest_bit(blue_mask) + 1; + + if ( d8 ) { // setup pixel translation + TQRgb *ctable = image.colorTable(); + for ( int i=0; i 0 ? r << red_shift : r >> -red_shift; + g = green_shift > 0 ? g << green_shift : g >> -green_shift; + b = blue_shift > 0 ? b << blue_shift : b >> -blue_shift; + pix[i] = (b & blue_mask) | (g & green_mask) | (r & red_mask) + | ~(blue_mask | green_mask | red_mask); + } + } + +#ifdef QT_MITSHM_CONVERSIONS + xi = qt_XShmCreateImage( dpy, visual, dd, ZPixmap, 0, 0, w, h, 32, 0, &shminfo ); + if( xi != NULL ) { + mitshm_ximage = true; + newbits = (uchar*)xi->data; + } + else +#endif + xi = XCreateImage( dpy, visual, dd, ZPixmap, 0, 0, w, h, 32, 0 ); + if (!xi) + return false; + if( newbits == NULL ) + newbits = (uchar *)malloc( xi->bytes_per_line*h ); + Q_CHECK_PTR( newbits ); + if ( !newbits ) // no memory + return FALSE; + int bppc = xi->bits_per_pixel; + + bool contig_bits = n_bits(red_mask) == rbits && + n_bits(green_mask) == gbits && + n_bits(blue_mask) == bbits; + bool dither_tc = + // Want it? + (conversion_flags & Dither_Mask) != ThresholdDither && + (conversion_flags & DitherMode_Mask) != AvoidDither && + // Need it? + bppc < 24 && !d8 && + // Can do it? (Contiguous bits?) + contig_bits; + + static bool init=FALSE; + static int D[16][16]; + if ( dither_tc && !init ) { + // I also contributed this code to XV - WWA. + /* + The dither matrix, D, is obtained with this formula: + + D2 = [ 0 2 ] + [ 3 1 ] + + + D2*n = [ 4*Dn 4*Dn+2*Un ] + [ 4*Dn+3*Un 4*Dn+1*Un ] + */ + int n,i,j; + init=1; + + /* Set D2 */ + D[0][0]=0; + D[1][0]=2; + D[0][1]=3; + D[1][1]=1; + + /* Expand using recursive definition given above */ + for (n=2; n<16; n*=2) { + for (i=0; i 8 && xi->byte_order == LSBFirst ) + bppc++; + + int wordsize; + bool bigendian; + qSysInfo( &wordsize, &bigendian ); + bool same_msb_lsb = ( xi->byte_order == MSBFirst ) == ( bigendian ); + + if( bppc == 8 ) // 8 bit + mode = BPP8; + else if( bppc == 16 || bppc == 17 ) { // 16 bit MSB/LSB + if( red_shift == 8 && green_shift == 3 && blue_shift == -3 + && !d8 && same_msb_lsb ) + mode = BPP16_8_3_M3; + else if( red_shift == 7 && green_shift == 2 && blue_shift == -3 + && !d8 && same_msb_lsb ) + mode = BPP16_7_2_M3; + else + mode = bppc == 17 ? BPP16_LSB : BPP16_MSB; + } else if( bppc == 24 || bppc == 25 ) { // 24 bit MSB/LSB + mode = bppc == 25 ? BPP24_LSB : BPP24_MSB; + } else if( bppc == 32 || bppc == 33 ) { // 32 bit MSB/LSB + if( red_shift == 16 && green_shift == 8 && blue_shift == 0 + && !d8 && same_msb_lsb ) + mode = BPP32_16_8_0; + else + mode = bppc == 33 ? BPP32_LSB : BPP32_MSB; + } else + qFatal("Logic error 3"); + +#define GET_PIXEL \ + int pixel; \ + if ( d8 ) pixel = pix[*src++]; \ + else { \ + int r = qRed ( *p ); \ + int g = qGreen( *p ); \ + int b = qBlue ( *p++ ); \ + r = red_shift > 0 \ + ? r << red_shift : r >> -red_shift; \ + g = green_shift > 0 \ + ? g << green_shift : g >> -green_shift; \ + b = blue_shift > 0 \ + ? b << blue_shift : b >> -blue_shift; \ + pixel = (r & red_mask)|(g & green_mask) | (b & blue_mask) \ + | ~(blue_mask | green_mask | red_mask); \ + } + +// optimized case - no d8 case, shift only once instead of twice, mask only once instead of twice, +// use direct values instead of variables, and use only one statement +// (*p >> 16), (*p >> 8 ) and (*p) are qRed(),qGreen() and qBlue() without masking +// shifts have to be passed including the shift operator (e.g. '>>3'), because of the direction +#define GET_PIXEL_OPT(red_shift,green_shift,blue_shift,red_mask,green_mask,blue_mask) \ + int pixel = ((( *p >> 16 ) red_shift ) & red_mask ) \ + | ((( *p >> 8 ) green_shift ) & green_mask ) \ + | ((( *p ) blue_shift ) & blue_mask ); \ + ++p; + +#define GET_PIXEL_DITHER_TC \ + int r = qRed ( *p ); \ + int g = qGreen( *p ); \ + int b = qBlue ( *p++ ); \ + const int thres = D[x%16][y%16]; \ + if ( r <= (255-(1<<(8-rbits))) && ((r< thres) \ + r += (1<<(8-rbits)); \ + if ( g <= (255-(1<<(8-gbits))) && ((g< thres) \ + g += (1<<(8-gbits)); \ + if ( b <= (255-(1<<(8-bbits))) && ((b< thres) \ + b += (1<<(8-bbits)); \ + r = red_shift > 0 \ + ? r << red_shift : r >> -red_shift; \ + g = green_shift > 0 \ + ? g << green_shift : g >> -green_shift; \ + b = blue_shift > 0 \ + ? b << blue_shift : b >> -blue_shift; \ + int pixel = (r & red_mask)|(g & green_mask) | (b & blue_mask); + +// again, optimized case +// can't be optimized that much :( +#define GET_PIXEL_DITHER_TC_OPT(red_shift,green_shift,blue_shift,red_mask,green_mask,blue_mask, \ + rbits,gbits,bbits) \ + const int thres = D[x%16][y%16]; \ + int r = qRed ( *p ); \ + if ( r <= (255-(1<<(8-rbits))) && ((r< thres) \ + r += (1<<(8-rbits)); \ + int g = qGreen( *p ); \ + if ( g <= (255-(1<<(8-gbits))) && ((g< thres) \ + g += (1<<(8-gbits)); \ + int b = qBlue ( *p++ ); \ + if ( b <= (255-(1<<(8-bbits))) && ((b< thres) \ + b += (1<<(8-bbits)); \ + int pixel = (( r red_shift ) & red_mask ) \ + | (( g green_shift ) & green_mask ) \ + | (( b blue_shift ) & blue_mask ); + +#define CYCLE(body) \ + for ( uint y=0; ybytes_per_line*y; \ + TQRgb* p = (TQRgb *)src; \ + body \ + } + + if ( dither_tc ) { + switch ( mode ) { + case BPP16_8_3_M3: + CYCLE( + Q_INT16* dst16 = (Q_INT16*)dst; + for ( uint x=0; x>3,0xf800,0x7e0,0x1f,5,6,5) + *dst16++ = pixel; + } + ) + break; + case BPP16_7_2_M3: + CYCLE( + Q_INT16* dst16 = (Q_INT16*)dst; + for ( uint x=0; x>3,0x7c00,0x3e0,0x1f,5,5,5) + *dst16++ = pixel; + } + ) + break; + case BPP16_MSB: // 16 bit MSB + CYCLE( + for ( uint x=0; x> 8); + *dst++ = pixel; + } + ) + break; + case BPP16_LSB: // 16 bit LSB + CYCLE( + for ( uint x=0; x> 8; + } + ) + break; + default: + qFatal("Logic error"); + } + } else { + switch ( mode ) { + case BPP8: // 8 bit + CYCLE( + Q_UNUSED(p); + for ( uint x=0; x>3,0xf800,0x7e0,0x1f) + *dst16++ = pixel; + } + ) + break; + case BPP16_7_2_M3: + CYCLE( + Q_INT16* dst16 = (Q_INT16*)dst; + for ( uint x=0; x>3,0x7c00,0x3e0,0x1f) + *dst16++ = pixel; + } + ) + break; + case BPP16_MSB: // 16 bit MSB + CYCLE( + for ( uint x=0; x> 8); + *dst++ = pixel; + } + ) + break; + case BPP16_LSB: // 16 bit LSB + CYCLE( + for ( uint x=0; x> 8; + } + ) + break; + case BPP24_MSB: // 24 bit MSB + CYCLE( + for ( uint x=0; x> 16; + *dst++ = pixel >> 8; + *dst++ = pixel; + } + ) + break; + case BPP24_LSB: // 24 bit LSB + CYCLE( + for ( uint x=0; x> 8; + *dst++ = pixel >> 16; + } + ) + break; + case BPP32_16_8_0: + CYCLE( + memcpy( dst, p, w * 4 ); + ) + break; + case BPP32_MSB: // 32 bit MSB + CYCLE( + for ( uint x=0; x> 24; + *dst++ = pixel >> 16; + *dst++ = pixel >> 8; + *dst++ = pixel; + } + ) + break; + case BPP32_LSB: // 32 bit LSB + CYCLE( + for ( uint x=0; x> 8; + *dst++ = pixel >> 16; + *dst++ = pixel >> 24; + } + ) + break; + default: + qFatal("Logic error 2"); + } + } + xi->data = (char *)newbits; + } + + if ( d == 8 && !trucol ) { // 8 bit pixmap + int pop[256]; // pixel popularity + + if ( image.numColors() == 0 ) + image.setNumColors( 1 ); + + memset( pop, 0, sizeof(int)*256 ); // reset popularity array + uint i; + for ( i=0; i 0 ) + ncols++; + } + for ( i=image.numColors(); i<256; i++ ) // ignore out-of-range pixels + pop[i] = 0; + + // works since we make sure above to have at least + // one color in the image + if ( ncols == 0 ) + ncols = 1; + + PIX pixarr[256]; // pixel array + PIX pixarr_sorted[256]; // pixel array (sorted) + memset( pixarr, 0, ncols*sizeof(PIX) ); + PIX *px = &pixarr[0]; + int maxpop = 0; + int maxpix = 0; + Q_CHECK_PTR( pixarr ); + uint j = 0; + TQRgb* ctable = image.colorTable(); + for ( i=0; i<256; i++ ) { // init pixel array + if ( pop[i] > 0 ) { + px->r = qRed ( ctable[i] ); + px->g = qGreen( ctable[i] ); + px->b = qBlue ( ctable[i] ); + px->n = 0; + px->use = pop[i]; + if ( pop[i] > maxpop ) { // select most popular entry + maxpop = pop[i]; + maxpix = j; + } + px->index = i; + px->mindist = 1000000; + px++; + j++; + } + } + pixarr_sorted[0] = pixarr[maxpix]; + pixarr[maxpix].use = 0; + + for ( i=1; i< (uint) ncols; i++ ) { // sort pixels + int minpix = -1, mindist = -1; + px = &pixarr_sorted[i-1]; + int r = px->r; + int g = px->g; + int b = px->b; + int dist; + if ( (i & 1) || i<10 ) { // sort on max distance + for ( int j=0; juse ) { + dist = (px->r - r)*(px->r - r) + + (px->g - g)*(px->g - g) + + (px->b - b)*(px->b - b); + if ( px->mindist > dist ) + px->mindist = dist; + if ( px->mindist > mindist ) { + mindist = px->mindist; + minpix = j; + } + } + } + } else { // sort on max popularity + for ( int j=0; juse ) { + dist = (px->r - r)*(px->r - r) + + (px->g - g)*(px->g - g) + + (px->b - b)*(px->b - b); + if ( px->mindist > dist ) + px->mindist = dist; + if ( px->use > mindist ) { + mindist = px->use; + minpix = j; + } + } + } + } + pixarr_sorted[i] = pixarr[minpix]; + pixarr[minpix].use = 0; + } + + uint pix[256]; // pixel translation table + px = &pixarr_sorted[0]; + for ( i=0; i< (uint) ncols; i++ ) { // allocate colors + TQColor c( px->r, px->g, px->b ); + pix[px->index] = c.pixel(x11Screen()); + px++; + } + + p = newbits; + for ( i=0; i< (uint) nbytes; i++ ) { // translate pixels + *p = pix[*p]; + p++; + } + } + + if ( !xi ) { // X image not created +#ifdef QT_MITSHM_CONVERSIONS + xi = qt_XShmCreateImage( dpy, visual, dd, ZPixmap, 0, 0, w, h, 32, 0, &shminfo ); + if( xi != NULL ) + mitshm_ximage = true; + else +#endif + xi = XCreateImage( dpy, visual, dd, ZPixmap, 0, 0, w, h, 32, 0 ); + if ( xi->bits_per_pixel == 16 ) { // convert 8 bpp ==> 16 bpp + ushort *p2; + int p2inc = xi->bytes_per_line/sizeof(ushort); + ushort *newerbits = (ushort *)malloc( xi->bytes_per_line * h ); + newbits_size = xi->bytes_per_line * h; + Q_CHECK_PTR( newerbits ); + if ( !newerbits ) // no memory + return FALSE; + uchar* p = newbits; + for ( uint y=0; ybits_per_pixel != 8 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPixmap::convertFromImage: Display not supported " + "(bpp=%d)", xi->bits_per_pixel ); +#endif + } +#ifdef QT_MITSHM_CONVERSIONS + if( newbits_size > 0 && mitshm_ximage ) { // need to copy to shared memory + memcpy( xi->data, newbits, newbits_size ); + free( newbits ); + newbits = (uchar*)xi->data; + } + else +#endif + xi->data = (char *)newbits; + } + + if ( hd && (width() != (int)w || height() != (int)h || this->depth() != dd) ) { + +#ifndef QT_NO_XFTFREETYPE + if (rendhd) { + XftDrawDestroy( (XftDraw *) rendhd ); + rendhd = 0; + } +#endif // QT_NO_XFTFREETYPE + + XFreePixmap( dpy, hd ); // don't reuse old pixmap + hd = 0; + } + if ( !hd ) { // create new pixmap + hd = (HANDLE)XCreatePixmap( x11Display(), + RootWindow(x11Display(), x11Screen() ), + w, h, dd ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft ) { + if ( data->d == 1 ) { + rendhd = (HANDLE) XftDrawCreateBitmap( x11Display (), hd ); + } else { + rendhd = (HANDLE) XftDrawCreate( x11Display (), hd, + (Visual *) x11Visual(), x11Colormap() ); + } + } +#endif // QT_NO_XFTFREETYPE + + } + +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + XShmPutImage( dpy, hd, qt_xget_readonly_gc( x11Screen(), FALSE ), + xi, 0, 0, 0, 0, w, h, False ); + else +#endif + XPutImage( dpy, hd, qt_xget_readonly_gc( x11Screen(), FALSE ), + xi, 0, 0, 0, 0, w, h ); + + data->w = w; + data->h = h; + data->d = dd; + + XImage* axi = NULL; +#ifdef QT_MITSHM_CONVERSIONS + bool mitshm_aximage = false; + XShmSegmentInfo ashminfo; +#endif + if ( image.hasAlphaBuffer() ) { + TQBitmap m; + m = image.createAlphaMask( conversion_flags ); + setMask( m ); + +#ifndef QT_NO_XFTFREETYPE + // does this image have an alphamap (and not just a 1bpp mask)? + bool alphamap = image.depth() == 32; + if (image.depth() == 8) { + const TQRgb * const rgb = image.colorTable(); + for (int i = 0, count = image.numColors(); i < count; ++i) { + const int alpha = qAlpha(rgb[i]); + if (alpha != 0 && alpha != 0xff) { + alphamap = TRUE; + break; + } + } + } + + if (qt_use_xrender && qt_has_xft && alphamap) { + data->alphapm = new TQPixmap; // create a null pixmap + + // setup pixmap data + data->alphapm->data->w = w; + data->alphapm->data->h = h; + data->alphapm->data->d = 8; + + // create 8bpp pixmap and render picture + data->alphapm->hd = + XCreatePixmap(x11Display(), RootWindow(x11Display(), x11Screen()), + w, h, 8); + + data->alphapm->rendhd = + (HANDLE) XftDrawCreateAlpha( x11Display(), data->alphapm->hd, 8 ); + +#ifdef QT_MITSHM_CONVERSIONS + axi = qt_XShmCreateImage( x11Display(), (Visual*)x11Visual(), + 8, ZPixmap, 0, 0, w, h, 8, 0, &ashminfo ); + if( axi != NULL ) + mitshm_aximage = true; + else +#endif + axi = XCreateImage(x11Display(), (Visual *) x11Visual(), + 8, ZPixmap, 0, 0, w, h, 8, 0); + + if (axi) { + if( axi->data==NULL ) { + // the data is deleted by qSafeXDestroyImage + axi->data = (char *) malloc(h * axi->bytes_per_line); + Q_CHECK_PTR( axi->data ); + } + char *aptr = axi->data; + + if (image.depth() == 32) { + const int *iptr = (const int *) image.bits(); + if( axi->bytes_per_line == (int)w ) { + int max = w * h; + while (max--) + *aptr++ = *iptr++ >> 24; // stquirt + } else { + for (uint i = 0; i < h; ++i ) { + for (uint j = 0; j < w; ++j ) + *aptr++ = *iptr++ >> 24; // stquirt + aptr += ( axi->bytes_per_line - w ); + } + } + } else if (image.depth() == 8) { + const TQRgb * const rgb = image.colorTable(); + for (uint y = 0; y < h; ++y) { + const uchar *iptr = image.scanLine(y); + for (uint x = 0; x < w; ++x) + *aptr++ = qAlpha(rgb[*iptr++]); + aptr += ( axi->bytes_per_line - w ); + } + } + + GC gc = XCreateGC(x11Display(), data->alphapm->hd, 0, 0); + #ifdef QT_MITSHM_CONVERSIONS + if( mitshm_aximage ) + XShmPutImage( dpy, data->alphapm->hd, gc, axi, 0, 0, 0, 0, w, h, False ); + else +#endif + XPutImage(dpy, data->alphapm->hd, gc, axi, 0, 0, 0, 0, w, h); + XFreeGC(x11Display(), gc); + } + } +#endif // QT_NO_XFTFREETYPE + } + +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage || mitshm_aximage ) + XSync( x11Display(), False ); // wait until processed +#endif + + if ( data->optim != BestOptim ) { // throw away image +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) + qt_XShmDestroyImage( xi, &shminfo ); + else +#endif + qSafeXDestroyImage( xi ); + data->ximage = 0; + } else { // keep ximage that we created +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_ximage ) { // copy the XImage? + qt_XShmDestroyImage( xi, &shminfo ); + xi = 0; + } +#endif + data->ximage = xi; + } + if( axi ) { +#ifdef QT_MITSHM_CONVERSIONS + if( mitshm_aximage ) + qt_XShmDestroyImage( axi, &ashminfo ); + else +#endif + qSafeXDestroyImage(axi); + } + return TRUE; +} + + +/*! + Grabs the contents of the window \a window and makes a pixmap out + of it. Returns the pixmap. + + The arguments \a (x, y) specify the offset in the window, whereas + \a (w, h) specify the width and height of the area to be copied. + + If \a w is negative, the function copies everything to the right + border of the window. If \a h is negative, the function copies + everything to the bottom of the window. + + Note that grabWindow() grabs pixels from the screen, not from the + window. If there is another window partially or entirely over the + one you grab, you get pixels from the overlying window, too. + + Note also that the mouse cursor is generally not grabbed. + + The reason we use a window identifier and not a TQWidget is to + enable grabbing of windows that are not part of the application, + window system frames, and so on. + + \warning Grabbing an area outside the screen is not safe in + general. This depends on the underlying window system. + + \warning X11 only: If \a window is not the same depth as the root + window and another window partially or entirely obscures the one + you grab, you will \e not get pixels from the overlying window. + The contests of the obscured areas in the pixmap are undefined and + uninitialized. + + \sa grabWidget() +*/ + +TQPixmap TQPixmap::grabWindow( WId window, int x, int y, int w, int h ) +{ + if ( w == 0 || h == 0 ) + return TQPixmap(); + + Display *dpy = x11AppDisplay(); + XWindowAttributes window_attr; + if ( ! XGetWindowAttributes( dpy, window, &window_attr ) ) + return TQPixmap(); + + if ( w < 0 ) + w = window_attr.width - x; + if ( h < 0 ) + h = window_attr.height - y; + + // determine the screen + int scr; + for ( scr = 0; scr < ScreenCount( dpy ); ++scr ) { + if ( window_attr.root == RootWindow( dpy, scr ) ) // found it + break; + } + if ( scr >= ScreenCount( dpy ) ) // sanity check + return TQPixmap(); + + + // get the depth of the root window + XWindowAttributes root_attr; + if ( ! XGetWindowAttributes( dpy, window_attr.root, &root_attr ) ) + return TQPixmap(); + + if ( window_attr.depth == root_attr.depth ) { + // if the depth of the specified window and the root window are the + // same, grab pixels from the root window (so that we get the any + // overlapping windows and window manager frames) + + // map x and y to the root window + WId unused; + if ( ! XTranslateCoordinates( dpy, window, window_attr.root, x, y, + &x, &y, &unused ) ) + return TQPixmap(); + + window = window_attr.root; + } + + TQPixmap pm( w, h ); + pm.data->uninit = FALSE; + pm.x11SetScreen( scr ); + + GC gc = qt_xget_temp_gc( scr, FALSE ); + XSetSubwindowMode( dpy, gc, IncludeInferiors ); + XCopyArea( dpy, window, pm.handle(), gc, x, y, w, h, 0, 0 ); + XSetSubwindowMode( dpy, gc, ClipByChildren ); + + return pm; +} + +/*! + Returns a copy of the pixmap that is transformed using \a matrix. + The original pixmap is not changed. + + The transformation \a matrix is internally adjusted to compensate + for unwanted translation, i.e. xForm() returns the smallest image + that contains all the transformed points of the original image. + + This function is slow because it involves transformation to a + TQImage, non-trivial computations and a transformation back to a + TQPixmap. + + \sa trueMatrix(), TQWMatrix, TQPainter::setWorldMatrix() TQImage::xForm() +*/ + +TQPixmap TQPixmap::xForm( const TQWMatrix &matrix ) const +{ + uint w = 0; + uint h = 0; // size of target pixmap + uint ws, hs; // size of source pixmap + uchar *dptr; // data in target pixmap + uint dbpl, dbytes; // bytes per line/bytes total + uchar *sptr; // data in original pixmap + int sbpl; // bytes per line in original + int bpp; // bits per pixel + bool depth1 = depth() == 1; + Display *dpy = x11Display(); + + if ( isNull() ) // this is a null pixmap + return copy(); + + ws = width(); + hs = height(); + + TQWMatrix mat( matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), 0., 0. ); + + double scaledWidth; + double scaledHeight; + + if ( matrix.m12() == 0.0F && matrix.m21() == 0.0F ) { + if ( matrix.m11() == 1.0F && matrix.m22() == 1.0F ) + return *this; // identity matrix + scaledHeight = matrix.m22()*hs; + scaledWidth = matrix.m11()*ws; + h = TQABS( qRound( scaledHeight ) ); + w = TQABS( qRound( scaledWidth ) ); + } else { // rotation or shearing + TQPointArray a( TQRect(0,0,ws+1,hs+1) ); + a = mat.map( a ); + TQRect r = a.boundingRect().normalize(); + w = r.width()-1; + h = r.height()-1; + scaledWidth = w; + scaledHeight = h; + } + + mat = trueMatrix( mat, ws, hs ); // true matrix + + + bool invertible; + mat = mat.invert( &invertible ); // invert matrix + + if ( h == 0 || w == 0 || !invertible + || TQABS(scaledWidth) >= 32768 || TQABS(scaledHeight) >= 32768 ) { // error, return null pixmap + TQPixmap pm; + pm.data->bitmap = data->bitmap; + return pm; + } + +#if defined(QT_MITSHM_XFORM) + static bool try_once = TRUE; + if (try_once) { + try_once = FALSE; + if ( !xshminit ) + qt_create_mitshm_buffer( this, 800, 600 ); + } + + bool use_mitshm = xshmimg && !depth1 && + xshmimg->width >= w && xshmimg->height >= h; +#endif + XImage *xi = (XImage*)data->ximage; // any cached ximage? + if ( !xi ) + xi = XGetImage( x11Display(), handle(), 0, 0, ws, hs, AllPlanes, + depth1 ? XYPixmap : ZPixmap ); + + if ( !xi ) { // error, return null pixmap + TQPixmap pm; + pm.data->bitmap = data->bitmap; + pm.data->alphapm = data->alphapm; + return pm; + } + + sbpl = xi->bytes_per_line; + sptr = (uchar *)xi->data; + bpp = xi->bits_per_pixel; + + if ( depth1 ) + dbpl = (w+7)/8; + else + dbpl = ((w*bpp+31)/32)*4; + dbytes = dbpl*h; + +#if defined(QT_MITSHM_XFORM) + if ( use_mitshm ) { + dptr = (uchar *)xshmimg->data; + uchar fillbyte = bpp == 8 ? white.pixel() : 0xff; + for ( int y=0; ybytes_per_line, fillbyte, dbpl ); + } else { +#endif + dptr = (uchar *)malloc( dbytes ); // create buffer for bits + Q_CHECK_PTR( dptr ); + if ( depth1 ) // fill with zeros + memset( dptr, 0, dbytes ); + else if ( bpp == 8 ) // fill with background color + memset( dptr, TQt::white.pixel( x11Screen() ), dbytes ); + else + memset( dptr, 0xff, dbytes ); +#if defined(QT_MITSHM_XFORM) + } +#endif + + // #define QT_DEBUG_XIMAGE +#if defined(QT_DEBUG_XIMAGE) + qDebug( "----IMAGE--INFO--------------" ); + qDebug( "width............. %d", xi->width ); + qDebug( "height............ %d", xi->height ); + qDebug( "xoffset........... %d", xi->xoffset ); + qDebug( "format............ %d", xi->format ); + qDebug( "byte order........ %d", xi->byte_order ); + qDebug( "bitmap unit....... %d", xi->bitmap_unit ); + qDebug( "bitmap bit order.. %d", xi->bitmap_bit_order ); + qDebug( "depth............. %d", xi->depth ); + qDebug( "bytes per line.... %d", xi->bytes_per_line ); + qDebug( "bits per pixel.... %d", xi->bits_per_pixel ); +#endif + + int type; + if ( xi->bitmap_bit_order == MSBFirst ) + type = QT_XFORM_TYPE_MSBFIRST; + else + type = QT_XFORM_TYPE_LSBFIRST; + int xbpl, p_inc; + if ( depth1 ) { + xbpl = (w+7)/8; + p_inc = dbpl - xbpl; + } else { + xbpl = (w*bpp)/8; + p_inc = dbpl - xbpl; +#if defined(QT_MITSHM_XFORM) + if ( use_mitshm ) + p_inc = xshmimg->bytes_per_line - xbpl; +#endif + } + + if ( !qt_xForm_helper( mat, xi->xoffset, type, bpp, dptr, xbpl, p_inc, h, sptr, sbpl, ws, hs ) ){ +#if defined(QT_CHECK_RANGE) + qWarning( "TQPixmap::xForm: display not supported (bpp=%d)",bpp); +#endif + TQPixmap pm; + return pm; + } + + if ( data->optim == NoOptim ) { // throw away ximage + qSafeXDestroyImage( xi ); + data->ximage = 0; + } else { // keep ximage that we fetched + data->ximage = xi; + } + + if ( depth1 ) { // mono bitmap + TQPixmap pm( w, h, dptr, TQImage::systemBitOrder() != TQImage::BigEndian ); + pm.data->bitmap = data->bitmap; + free( dptr ); + if ( data->mask ) { + if ( data->selfmask ) // pixmap == mask + pm.setMask( *((TQBitmap*)(&pm)) ); + else + pm.setMask( data->mask->xForm(matrix) ); + } + return pm; + } else { // color pixmap + GC gc = qt_xget_readonly_gc( x11Screen(), FALSE ); + TQPixmap pm( w, h ); + pm.data->uninit = FALSE; + pm.x11SetScreen( x11Screen() ); +#if defined(QT_MITSHM_XFORM) + if ( use_mitshm ) { + XCopyArea( dpy, xshmpm, pm.handle(), gc, 0, 0, w, h, 0, 0 ); + } else { +#endif + xi = XCreateImage( dpy, (Visual *)x11Visual(), x11Depth(), + ZPixmap, 0, (char *)dptr, w, h, 32, 0 ); + XPutImage( dpy, pm.handle(), gc, xi, 0, 0, 0, 0, w, h); + qSafeXDestroyImage( xi ); +#if defined(QT_MITSHM_XFORM) + } +#endif + + if ( data->mask ) // xform mask, too + pm.setMask( data->mask->xForm(matrix) ); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_use_xrender && qt_has_xft && data->alphapm ) { // xform the alpha channel + XImage *axi = 0; + if ((axi = XGetImage(x11Display(), data->alphapm->handle(), + 0, 0, ws, hs, AllPlanes, ZPixmap))) { + sbpl = axi->bytes_per_line; + sptr = (uchar *) axi->data; + bpp = axi->bits_per_pixel; + dbytes = dbpl * h; + dptr = (uchar *) malloc(dbytes); + Q_CHECK_PTR( dptr ); + memset(dptr, 0, dbytes); + if ( axi->bitmap_bit_order == MSBFirst ) + type = QT_XFORM_TYPE_MSBFIRST; + else + type = QT_XFORM_TYPE_LSBFIRST; + + if (qt_xForm_helper( mat, axi->xoffset, type, bpp, dptr, w, + 0, h, sptr, sbpl, ws, hs )) { + delete pm.data->alphapm; + pm.data->alphapm = new TQPixmap; // create a null pixmap + + // setup pixmap data + pm.data->alphapm->data->w = w; + pm.data->alphapm->data->h = h; + pm.data->alphapm->data->d = 8; + + // create 8bpp pixmap and render picture + pm.data->alphapm->hd = + XCreatePixmap(x11Display(), + RootWindow(x11Display(), x11Screen()), + w, h, 8); + + pm.data->alphapm->rendhd = + (HANDLE) XftDrawCreateAlpha( x11Display(), + pm.data->alphapm->hd, 8 ); + + XImage *axi2 = XCreateImage(x11Display(), (Visual *) x11Visual(), + 8, ZPixmap, 0, (char *)dptr, w, h, 8, 0); + + if (axi2) { + // the data is deleted by qSafeXDestroyImage + GC gc = XCreateGC(x11Display(), pm.data->alphapm->hd, 0, 0); + XPutImage(dpy, pm.data->alphapm->hd, gc, axi2, 0, 0, 0, 0, w, h); + XFreeGC(x11Display(), gc); + qSafeXDestroyImage(axi2); + } + } + qSafeXDestroyImage(axi); + } + } +#endif // QT_NO_XFTFREETYPE + + return pm; + } +} + + +/*! + \internal +*/ +int TQPixmap::x11SetDefaultScreen( int screen ) +{ + int old = defaultScreen; + defaultScreen = screen; + return old; +} + +/*! + \internal +*/ +void TQPixmap::x11SetScreen( int screen ) +{ + if ( screen < 0 ) + screen = x11AppScreen(); + + if ( screen == x11Screen() ) + return; // nothing to do + + if ( isNull() ) { + TQPaintDeviceX11Data* xd = getX11Data( TRUE ); + xd->x_screen = screen; + xd->x_depth = TQPaintDevice::x11AppDepth( screen ); + xd->x_cells = TQPaintDevice::x11AppCells( screen ); + xd->x_colormap = TQPaintDevice::x11AppColormap( screen ); + xd->x_defcolormap = TQPaintDevice::x11AppDefaultColormap( screen ); + xd->x_visual = TQPaintDevice::x11AppVisual( screen ); + xd->x_defvisual = TQPaintDevice::x11AppDefaultVisual( screen ); + setX11Data( xd ); + return; + } +#if 0 + qDebug("TQPixmap::x11SetScreen for %p from %d to %d. Size is %d/%d", data, x11Screen(), screen, width(), height() ); +#endif + + TQImage img = convertToImage(); + resize(0,0); + TQPaintDeviceX11Data* xd = getX11Data( TRUE ); + xd->x_screen = screen; + xd->x_depth = TQPaintDevice::x11AppDepth( screen ); + xd->x_cells = TQPaintDevice::x11AppCells( screen ); + xd->x_colormap = TQPaintDevice::x11AppColormap( screen ); + xd->x_defcolormap = TQPaintDevice::x11AppDefaultColormap( screen ); + xd->x_visual = TQPaintDevice::x11AppVisual( screen ); + xd->x_defvisual = TQPaintDevice::x11AppDefaultVisual( screen ); + setX11Data( xd ); + convertFromImage( img ); +} + +/*! + Returns TRUE this pixmap has an alpha channel or a mask. + + \sa hasAlphaChannel() mask() +*/ +bool TQPixmap::hasAlpha() const +{ + return data->alphapm || data->mask; +} + +/*! + Returns TRUE if the pixmap has an alpha channel; otherwise it + returns FALSE. + + NOTE: If the pixmap has a mask but not alpha channel, this + function returns FALSE. + + \sa hasAlpha() mask() +*/ +bool TQPixmap::hasAlphaChannel() const +{ + return data->alphapm != 0; +} + +/*! + \relates TQPixmap + + Copies a block of pixels from \a src to \a dst. The alpha channel + and mask data (if any) is also copied from \a src. NOTE: \a src + is \e not alpha blended or masked when copied to \a dst. Use + bitBlt() or TQPainter::drawPixmap() to perform alpha blending or + masked drawing. + + \a sx, \a sy is the top-left pixel in \a src (0, 0 by default), \a + dx, \a dy is the top-left position in \a dst and \a sw, \sh is the + size of the copied block (all of \a src by default). + + If \a src, \a dst, \a sw or \a sh is 0 (zero), copyBlt() does + nothing. If \a sw or \a sh is negative, copyBlt() copies starting + at \a sx (and respectively, \a sy) and ending at the right edge + (and respectively, the bottom edge) of \a src. + + copyBlt() does nothing if \a src and \a dst have different depths. +*/ +Q_EXPORT void copyBlt( TQPixmap *dst, int dx, int dy, + const TQPixmap *src, int sx, int sy, int sw, int sh ) +{ + if ( ! dst || ! src || sw == 0 || sh == 0 || dst->depth() != src->depth() ) { +#ifdef QT_CHECK_NULL + Q_ASSERT( dst != 0 ); + Q_ASSERT( src != 0 ); +#endif + return; + } + + // copy pixel data + bitBlt( dst, dx, dy, src, sx, sy, sw, sh, TQt::CopyROP, TRUE ); + + // copy mask data + if ( src->data->mask ) { + if ( ! dst->data->mask ) { + dst->data->mask = new TQBitmap( dst->width(), dst->height() ); + + // new masks are fully opaque by default + dst->data->mask->fill( TQt::color1 ); + } + + bitBlt( dst->data->mask, dx, dy, + src->data->mask, sx, sy, sw, sh, TQt::CopyROP, TRUE ); + } + +#ifndef QT_NO_XFTFREETYPE + // copy alpha data + extern bool qt_use_xrender; // from qapplication_x11.cpp + if ( ! qt_use_xrender || ! src->data->alphapm ) + return; + + if ( sw < 0 ) + sw = src->width() - sx; + else + sw = TQMIN( src->width()-sx, sw ); + sw = TQMIN( dst->width()-dx, sw ); + + if ( sh < 0 ) + sh = src->height() - sy ; + else + sh = TQMIN( src->height()-sy, sh ); + sh = TQMIN( dst->height()-dy, sh ); + + if ( sw <= 0 || sh <= 0 ) + return; + + // create an alpha pixmap for dst if it doesn't exist + bool do_init = FALSE; + if ( ! dst->data->alphapm ) { + dst->data->alphapm = new TQPixmap; + + // setup pixmap d + dst->data->alphapm->data->w = dst->width(); + dst->data->alphapm->data->h = dst->height(); + dst->data->alphapm->data->d = 8; + + // create 8bpp pixmap and render picture + dst->data->alphapm->hd = + XCreatePixmap(dst->x11Display(), + RootWindow(dst->x11Display(), dst->x11Screen()), + dst->width(), dst->height(), 8); + + // new alpha pixmaps should be fully opaque by default + do_init = TRUE; + + dst->data->alphapm->rendhd = + (TQt::HANDLE) XftDrawCreateAlpha( dst->x11Display(), + dst->data->alphapm->hd, 8 ); + } + + GC gc = XCreateGC(dst->x11Display(), dst->data->alphapm->hd, 0, 0); + + if ( do_init ) { + // the alphapm was just created, make it fully opaque + XSetForeground( dst->x11Display(), gc, 255 ); + XSetBackground( dst->x11Display(), gc, 255 ); + XFillRectangle( dst->x11Display(), dst->data->alphapm->hd, gc, + 0, 0, dst->data->alphapm->data->w, + dst->data->alphapm->data->h ); + } + + XCopyArea(dst->x11Display(), src->data->alphapm->hd, dst->data->alphapm->hd, gc, + sx, sy, sw, sh, dx, dy); + XFreeGC(dst->x11Display(), gc); +#endif // QT_NO_XFTFREETYPE +} diff --git a/src/kernel/qpixmapcache.cpp b/src/kernel/qpixmapcache.cpp new file mode 100644 index 000000000..32f696c16 --- /dev/null +++ b/src/kernel/qpixmapcache.cpp @@ -0,0 +1,336 @@ +/**************************************************************************** +** +** Implementation of TQPixmapCache class +** +** Created : 950504 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpixmapcache.h" +#include "qcache.h" +#include "qobject.h" +#include "qcleanuphandler.h" + + +// REVISED: paul +/*! + \class TQPixmapCache qpixmapcache.h + + \brief The TQPixmapCache class provides an application-global cache for + pixmaps. + + \ingroup environment + \ingroup graphics + \ingroup images + + This class is a tool for optimized drawing with TQPixmap. You can + use it to store temporary pixmaps that are expensive to generate + without using more storage space than cacheLimit(). Use insert() + to insert pixmaps, find() to find them and clear() to empty the + cache. + + For example, TQRadioButton has a non-trivial visual representation + so we don't want to regenerate a pixmap whenever a radio button is + displayed or changes state. In the function + TQRadioButton::drawButton(), we do not draw the radio button + directly. Instead, we first check the global pixmap cache for a + pixmap with the key "$qt_radio_nnn_", where \c nnn is a numerical + value that specifies the the radio button state. If a pixmap is + found, we bitBlt() it onto the widget and return. Otherwise, we + create a new pixmap, draw the radio button in the pixmap, and + finally insert the pixmap in the global pixmap cache, using the + key above. The bitBlt() is ten times faster than drawing the + radio button. All radio buttons in the program share the cached + pixmap since TQPixmapCache is application-global. + + TQPixmapCache contains no member data, only static functions to + access the global pixmap cache. It creates an internal TQCache for + caching the pixmaps. + + The cache associates a pixmap with a string (key). If two pixmaps + are inserted into the cache using equal keys, then the last pixmap + will hide the first pixmap. The TQDict and TQCache classes do + exactly the same. + + The cache becomes full when the total size of all pixmaps in the + cache exceeds cacheLimit(). The initial cache limit is 1024 KByte + (1 MByte); it is changed with setCacheLimit(). A pixmap takes + roughly width*height*depth/8 bytes of memory. + + See the \l TQCache documentation for more details about the cache + mechanism. +*/ + + +static const int cache_size = 149; // size of internal hash array +#ifdef Q_WS_MAC9 +static int cache_limit = 256; // 256 KB cache limit +#else +static int cache_limit = 1024; // 1024 KB cache limit +#endif + +class TQPMCache: public TQObject, public TQCache +{ +public: + TQPMCache(): + TQObject( 0, "global pixmap cache" ), + TQCache( cache_limit * 1024, cache_size ), + id( 0 ), ps( 0 ), t( FALSE ) + { + setAutoDelete( TRUE ); + } + ~TQPMCache() {} + void timerEvent( TQTimerEvent * ); + bool insert( const TQString& k, const TQPixmap *d, int c, int p = 0 ); +private: + int id; + int ps; + bool t; +}; + + +/* + This is supposed to cut the cache size down by about 80-90% in a + minute once the application becomes idle, to let any inserted pixmap + remain in the cache for some time before it becomes a candidate for + cleaning-up, and to not cut down the size of the cache while the + cache is in active use. + + When the last pixmap has been deleted from the cache, kill the + timer so TQt won't keep the CPU from going into sleep mode. +*/ + +void TQPMCache::timerEvent( TQTimerEvent * ) +{ + int mc = maxCost(); + bool nt = totalCost() == ps; + setMaxCost( nt ? totalCost() * 3 / 4 : totalCost() -1 ); + setMaxCost( mc ); + ps = totalCost(); + + if ( !count() ) { + killTimer( id ); + id = 0; + } else if ( nt != t ) { + killTimer( id ); + id = startTimer( nt ? 10000 : 30000 ); + t = nt; + } +} + +bool TQPMCache::insert( const TQString& k, const TQPixmap *d, int c, int p ) +{ + bool r = TQCache::insert( k, d, c, p ); + if ( r && !id ) { + id = startTimer( 30000 ); + t = FALSE; + } + return r; +} + +static TQPMCache *pm_cache = 0; // global pixmap cache + +static TQSingleCleanupHandler qpm_cleanup_cache; + +/*! + Returns the pixmap associated with the \a key in the cache, or + null if there is no such pixmap. + + \warning If valid, you should copy the pixmap immediately (this is + fast). Subsequent insertions into the cache could cause the + pointer to become invalid. For this reason, we recommend you use + find(const TQString&, TQPixmap&) instead. + + Example: + \code + TQPixmap* pp; + TQPixmap p; + if ( (pp=TQPixmapCache::find("my_big_image", pm)) ) { + p = *pp; + } else { + p.load("bigimage.png"); + TQPixmapCache::insert("my_big_image", new TQPixmap(p)); + } + painter->drawPixmap(0, 0, p); + \endcode +*/ + +TQPixmap *TQPixmapCache::find( const TQString &key ) +{ + return pm_cache ? pm_cache->find(key) : 0; +} + + +/*! + \overload + + Looks for a cached pixmap associated with the \a key in the cache. + If a pixmap is found, the function sets \a pm to that pixmap and + returns TRUE; otherwise leaves \a pm alone and returns FALSE. + + Example: + \code + TQPixmap p; + if ( !TQPixmapCache::find("my_big_image", pm) ) { + pm.load("bigimage.png"); + TQPixmapCache::insert("my_big_image", pm); + } + painter->drawPixmap(0, 0, p); + \endcode +*/ + +bool TQPixmapCache::find( const TQString &key, TQPixmap& pm ) +{ + TQPixmap* p = pm_cache ? pm_cache->find(key) : 0; + if ( p ) pm = *p; + return !!p; +} + + +/*! + \obsolete + Inserts the pixmap \a pm associated with \a key into the cache. + Returns TRUE if successful, or FALSE if the pixmap is too big for the cache. + + + Note: \a pm must be allocated on the heap (using \c new). + + If this function returns FALSE, you must delete \a pm yourself. + + If this function returns TRUE, do not use \a pm afterwards or + keep references to it because any other insertions into the cache, + whether from anywhere in the application or within TQt itself, could cause + the pixmap to be discarded from the cache and the pointer to + become invalid. + + Due to these dangers, we strongly recommend that you use + insert(const TQString&, const TQPixmap&) instead. + +*/ + +bool TQPixmapCache::insert( const TQString &key, TQPixmap *pm ) +{ + if ( !pm_cache ) { // create pixmap cache + pm_cache = new TQPMCache; + Q_CHECK_PTR( pm_cache ); + qpm_cleanup_cache.set( &pm_cache ); + } + return pm_cache->insert( key, pm, pm->width()*pm->height()*pm->depth()/8 ); +} + +/*! + Inserts a copy of the pixmap \a pm associated with the \a key into + the cache. + + All pixmaps inserted by the TQt library have a key starting with + "$qt", so your own pixmap keys should never begin "$qt". + + When a pixmap is inserted and the cache is about to exceed its + limit, it removes pixmaps until there is enough room for the + pixmap to be inserted. + + The oldest pixmaps (least recently accessed in the cache) are + deleted when more space is needed. + + \sa setCacheLimit(). +*/ + +bool TQPixmapCache::insert( const TQString &key, const TQPixmap& pm ) +{ + if ( !pm_cache ) { // create pixmap cache + pm_cache = new TQPMCache; + Q_CHECK_PTR( pm_cache ); + qpm_cleanup_cache.set( &pm_cache ); + } + TQPixmap *p = new TQPixmap(pm); + bool rt = pm_cache->insert( key, p, p->width()*p->height()*p->depth()/8 ); + if ( !rt ) + delete p; + + return rt; +} + +/*! + Returns the cache limit (in kilobytes). + + The default setting is 1024 kilobytes. + + \sa setCacheLimit(). +*/ + +int TQPixmapCache::cacheLimit() +{ + return cache_limit; +} + +/*! + Sets the cache limit to \a n kilobytes. + + The default setting is 1024 kilobytes. + + \sa cacheLimit() +*/ + +void TQPixmapCache::setCacheLimit( int n ) +{ +#ifdef Q_WS_MAC9 + if(n > 256) + qWarning("TQPixmapCache::setCacheLimit: Setting cache limits high is harmfull to mac9's health"); +#endif + cache_limit = n; + if ( pm_cache ) + pm_cache->setMaxCost( 1024*cache_limit ); +} + + +/*! + Removes the pixmap associated with \a key from the cache. +*/ +void TQPixmapCache::remove( const TQString &key ) +{ + if ( pm_cache ) + pm_cache->remove( key ); +} + + +/*! + Removes all pixmaps from the cache. +*/ + +void TQPixmapCache::clear() +{ + if ( pm_cache ) + pm_cache->clear(); +} diff --git a/src/kernel/qpixmapcache.h b/src/kernel/qpixmapcache.h new file mode 100644 index 000000000..a5a633681 --- /dev/null +++ b/src/kernel/qpixmapcache.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Definition of TQPixmapCache class +** +** Created : 950501 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPIXMAPCACHE_H +#define TQPIXMAPCACHE_H + +#ifndef QT_H +#include "qpixmap.h" +#endif // QT_H + + +class Q_EXPORT TQPixmapCache // global pixmap cache +{ +public: + static int cacheLimit(); + static void setCacheLimit( int ); + static TQPixmap *find( const TQString &key ); + static bool find( const TQString &key, TQPixmap& ); + static bool insert( const TQString &key, TQPixmap * ); + static bool insert( const TQString &key, const TQPixmap& ); + static void remove( const TQString &key ); + static void clear(); +}; + + +#endif // TQPIXMAPCACHE_H diff --git a/src/kernel/qpngio.cpp b/src/kernel/qpngio.cpp new file mode 100644 index 000000000..a552461ef --- /dev/null +++ b/src/kernel/qpngio.cpp @@ -0,0 +1,1256 @@ +/**************************************************************************** +** +** Implementation of PNG TQImage IOHandler +** +** Created : 970521 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpngio.h" + +#ifndef QT_NO_IMAGEIO_PNG + +#include "qasyncimageio.h" +#include "qiodevice.h" + +#include + + +#ifdef Q_OS_TEMP +#define CALLBACK_CALL_TYPE __cdecl +#else +#define CALLBACK_CALL_TYPE +#endif + + +/* + All PNG files load to the minimal TQImage equivalent. + + All TQImage formats output to reasonably efficient PNG equivalents. + Never to grayscale. +*/ + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif + +static +void CALLBACK_CALL_TYPE iod_read_fn(png_structp png_ptr, png_bytep data, png_size_t length) +{ + TQImageIO* iio = (TQImageIO*)png_get_io_ptr(png_ptr); + TQIODevice* in = iio->ioDevice(); + + while (length) { + int nr = in->readBlock((char*)data, length); + if (nr <= 0) { + png_error(png_ptr, "Read Error"); + return; + } + length -= nr; + } +} + + +static +void CALLBACK_CALL_TYPE qpiw_write_fn( png_structp png_ptr, png_bytep data, png_size_t length ) +{ + TQPNGImageWriter* qpiw = (TQPNGImageWriter*)png_get_io_ptr( png_ptr ); + TQIODevice* out = qpiw->device(); + + uint nr = out->writeBlock( (char*)data, length ); + if ( nr != length ) { + png_error( png_ptr, "Write Error" ); + return; + } +} + + +static +void CALLBACK_CALL_TYPE qpiw_flush_fn( png_structp png_ptr ) +{ + TQPNGImageWriter* qpiw = (TQPNGImageWriter*)png_get_io_ptr( png_ptr ); + TQIODevice* out = qpiw->device(); + + out->flush(); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + +static +void setup_qt( TQImage& image, png_structp png_ptr, png_infop info_ptr, float screen_gamma=0.0 ) +{ + if ( screen_gamma != 0.0 && png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) ) { + double file_gamma; + png_get_gAMA(png_ptr, info_ptr, &file_gamma); + png_set_gamma( png_ptr, screen_gamma, file_gamma ); + } + + png_uint_32 width; + png_uint_32 height; + int bit_depth; + int color_type; + png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, + 0, 0, 0); + + if ( color_type == PNG_COLOR_TYPE_GRAY ) { + // Black & White or 8-bit grayscale + if ( bit_depth == 1 && info_ptr->channels == 1 ) { + png_set_invert_mono( png_ptr ); + png_read_update_info( png_ptr, info_ptr ); + if (!image.create( width, height, 1, 2, TQImage::BigEndian )) + return; + image.setColor( 1, qRgb(0,0,0) ); + image.setColor( 0, qRgb(255,255,255) ); + } else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { + png_set_expand(png_ptr); + png_set_strip_16(png_ptr); + png_set_gray_to_rgb(png_ptr); + + if (!image.create(width, height, 32)) + return; + image.setAlphaBuffer(TRUE); + + if (TQImage::systemByteOrder() == TQImage::BigEndian) + png_set_swap_alpha(png_ptr); + + png_read_update_info(png_ptr, info_ptr); + } else { + if ( bit_depth == 16 ) + png_set_strip_16(png_ptr); + else if ( bit_depth < 8 ) + png_set_packing(png_ptr); + int ncols = bit_depth < 8 ? 1 << bit_depth : 256; + png_read_update_info(png_ptr, info_ptr); + if (!image.create(width, height, 8, ncols)) + return; + for (int i=0; i1 || ( PNG_LIBPNG_VER_MAJOR==1 && PNG_LIBPNG_VER_MINOR>=4 ) + const int g = info_ptr->trans_color.gray; +#else + const int g = info_ptr->trans_values.gray; +#endif + if (g < ncols) { + image.setAlphaBuffer(TRUE); + image.setColor(g, image.color(g) & RGB_MASK); + } + } + } + } else if ( color_type == PNG_COLOR_TYPE_PALETTE + && png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE) + && info_ptr->num_palette <= 256 ) + { + // 1-bit and 8-bit color + if ( bit_depth != 1 ) + png_set_packing( png_ptr ); + png_read_update_info( png_ptr, info_ptr ); + png_get_IHDR(png_ptr, info_ptr, + &width, &height, &bit_depth, &color_type, 0, 0, 0); + if (!image.create(width, height, bit_depth, info_ptr->num_palette, + TQImage::BigEndian)) + return; + int i = 0; + if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ) { + image.setAlphaBuffer( TRUE ); + while ( i < info_ptr->num_trans ) { + image.setColor(i, qRgba( + info_ptr->palette[i].red, + info_ptr->palette[i].green, + info_ptr->palette[i].blue, +#if PNG_LIBPNG_VER_MAJOR>1 || ( PNG_LIBPNG_VER_MAJOR==1 && PNG_LIBPNG_VER_MINOR>=4 ) + info_ptr->trans_alpha[i] +#else + info_ptr->trans[i] +#endif + ) + ); + i++; + } + } + while ( i < info_ptr->num_palette ) { + image.setColor(i, qRgba( + info_ptr->palette[i].red, + info_ptr->palette[i].green, + info_ptr->palette[i].blue, + 0xff + ) + ); + i++; + } + } else { + // 32-bit + if ( bit_depth == 16 ) + png_set_strip_16(png_ptr); + + png_set_expand(png_ptr); + + if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA ) + png_set_gray_to_rgb(png_ptr); + + if (!image.create(width, height, 32)) + return; + + // Only add filler if no alpha, or we can get 5 channel data. + if (!(color_type & PNG_COLOR_MASK_ALPHA) + && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { + png_set_filler(png_ptr, 0xff, + TQImage::systemByteOrder() == TQImage::BigEndian ? + PNG_FILLER_BEFORE : PNG_FILLER_AFTER); + // We want 4 bytes, but it isn't an alpha channel + } else { + image.setAlphaBuffer(TRUE); + } + + if ( TQImage::systemByteOrder() == TQImage::BigEndian ) { + png_set_swap_alpha(png_ptr); + } + + png_read_update_info(png_ptr, info_ptr); + } + + // TQt==ARGB==Big(ARGB)==Little(BGRA) + if ( TQImage::systemByteOrder() == TQImage::LittleEndian ) { + png_set_bgr(png_ptr); + } +} + + +#if defined(Q_C_CALLBACKS) +extern "C" { +#endif +static void CALLBACK_CALL_TYPE qt_png_warning(png_structp /*png_ptr*/, png_const_charp message) +{ + qWarning("libpng warning: %s", message); +} + +#if defined(Q_C_CALLBACKS) +} +#endif + + +static +void read_png_image(TQImageIO* iio) +{ + png_structp png_ptr; + png_infop info_ptr; + png_infop end_info; + png_bytep* row_pointers; + + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0); + if (!png_ptr) { + iio->setStatus(-1); + return; + } + + png_set_error_fn(png_ptr, 0, 0, qt_png_warning); + + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + png_destroy_read_struct(&png_ptr, 0, 0); + iio->setStatus(-2); + return; + } + + end_info = png_create_info_struct(png_ptr); + if (!end_info) { + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + iio->setStatus(-3); + return; + } + + if (setjmp(png_ptr->jmpbuf)) { + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + iio->setStatus(-4); + return; + } + + png_set_read_fn(png_ptr, (void*)iio, iod_read_fn); + png_read_info(png_ptr, info_ptr); + + TQImage image; + setup_qt(image, png_ptr, info_ptr, iio->gamma()); + if (image.isNull()) { + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + iio->setStatus(-5); + return; + } + + png_uint_32 width; + png_uint_32 height; + int bit_depth; + int color_type; + png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, + 0, 0, 0); + + uchar** jt = image.jumpTable(); + row_pointers=new png_bytep[height]; + + for (uint y=0; y1 || ( PNG_LIBPNG_VER_MAJOR==1 && PNG_LIBPNG_VER_MINOR>=4 ) + (info_ptr->trans_color.red << 8 >> bit_depth)&0xff, + (info_ptr->trans_color.green << 8 >> bit_depth)&0xff, + (info_ptr->trans_color.blue << 8 >> bit_depth)&0xff); +#else + (info_ptr->trans_values.red << 8 >> bit_depth)&0xff, + (info_ptr->trans_values.green << 8 >> bit_depth)&0xff, + (info_ptr->trans_values.blue << 8 >> bit_depth)&0xff); +#endif + for (uint y=0; ywidth; x++) { + if (((uint**)jt)[y][x] == trans) { + ((uint**)jt)[y][x] &= 0x00FFFFFF; + } else { + } + } + } + } +#endif + + image.setDotsPerMeterX(png_get_x_pixels_per_meter(png_ptr,info_ptr)); + image.setDotsPerMeterY(png_get_y_pixels_per_meter(png_ptr,info_ptr)); + +#ifndef QT_NO_IMAGE_TEXT + png_textp text_ptr; + int num_text=0; + png_get_text(png_ptr,info_ptr,&text_ptr,&num_text); + while (num_text--) { + image.setText(text_ptr->key,0,text_ptr->text); + text_ptr++; + } +#endif + + delete [] row_pointers; + + if ( image.hasAlphaBuffer() ) { + // Many PNG files lie (eg. from PhotoShop). Fortunately this loop will + // usually be tquick to find those that tell the truth. + TQRgb* c; + int n; + if (image.depth()==32) { + c = (TQRgb*)image.bits(); + n = image.bytesPerLine() * image.height() / 4; + } else { + c = image.colorTable(); + n = image.numColors(); + } + while ( n-- && qAlpha(*c++)==0xff ) + ; + if ( n<0 ) // LIAR! + image.setAlphaBuffer(FALSE); + } + + iio->setImage(image); + + png_read_end(png_ptr, end_info); + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + + iio->setStatus(0); +} + +TQPNGImageWriter::TQPNGImageWriter(TQIODevice* iod) : + dev(iod), + frames_written(0), + disposal(Unspecified), + looping(-1), + ms_delay(-1), + gamma(0.0) +{ +} + +TQPNGImageWriter::~TQPNGImageWriter() +{ +} + +void TQPNGImageWriter::setDisposalMethod(DisposalMethod dm) +{ + disposal = dm; +} + +void TQPNGImageWriter::setLooping(int loops) +{ + looping = loops; +} + +void TQPNGImageWriter::setFrameDelay(int msecs) +{ + ms_delay = msecs; +} + +void TQPNGImageWriter::setGamma(float g) +{ + gamma = g; +} + + +#ifndef QT_NO_IMAGE_TEXT +static void set_text(const TQImage& image, png_structp png_ptr, png_infop info_ptr, bool short_not_long) +{ + TQValueList keys = image.textList(); + if ( keys.count() ) { + png_textp text_ptr = new png_text[keys.count()]; + int i=0; + for (TQValueList::Iterator it=keys.begin(); + it != keys.end(); ++it) + { + TQString t = image.text(*it); + if ( (t.length() <= 200) == short_not_long ) { + if ( t.length() < 40 ) + text_ptr[i].compression = PNG_TEXT_COMPRESSION_NONE; + else + text_ptr[i].compression = PNG_TEXT_COMPRESSION_zTXt; + text_ptr[i].key = (png_charp)(*it).key.data(); + text_ptr[i].text = (png_charp)t.latin1(); + //text_ptr[i].text = qstrdup(t.latin1()); + i++; + } + } + png_set_text(png_ptr, info_ptr, text_ptr, i); + //for (int j=0; jjmpbuf)) { + png_destroy_write_struct(&png_ptr, &info_ptr); + return FALSE; + } + + int quality = quality_in; + if (quality >= 0) { + if (quality > 9) { +#if defined(QT_CHECK_RANGE) + qWarning( "PNG: Quality %d out of range", quality ); +#endif + quality = 9; + } + png_set_compression_level(png_ptr, quality); + } + + if (gamma != 0.0) { + png_set_gAMA(png_ptr, info_ptr, 1.0/gamma); + } + + png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn); + + info_ptr->channels = + (image.depth() == 32) + ? (image.hasAlphaBuffer() ? 4 : 3) + : 1; + + png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(), + image.depth() == 1 ? 1 : 8 /* per channel */, + image.depth() == 32 + ? image.hasAlphaBuffer() + ? PNG_COLOR_TYPE_RGB_ALPHA + : PNG_COLOR_TYPE_RGB + : PNG_COLOR_TYPE_PALETTE, 0, 0, 0); + + + //png_set_sBIT(png_ptr, info_ptr, 8); + info_ptr->sig_bit.red = 8; + info_ptr->sig_bit.green = 8; + info_ptr->sig_bit.blue = 8; + + if (image.depth() == 1 && image.bitOrder() == TQImage::LittleEndian) + png_set_packswap(png_ptr); + + png_colorp palette = 0; + png_bytep copy_trans = 0; + if (image.numColors()) { + // Paletted + int num_palette = image.numColors(); + palette = new png_color[num_palette]; + png_set_PLTE(png_ptr, info_ptr, palette, num_palette); + int* trans = new int[num_palette]; + int num_trans = 0; + for (int i=0; ipalette[i].red = qRed(rgb); + info_ptr->palette[i].green = qGreen(rgb); + info_ptr->palette[i].blue = qBlue(rgb); + if (image.hasAlphaBuffer()) { + trans[i] = rgb >> 24; + if (trans[i] < 255) { + num_trans = i+1; + } + } + } + if (num_trans) { + copy_trans = new png_byte[num_trans]; + for (int i=0; isig_bit.alpha = 8; + } + + // Swap ARGB to RGBA (normal PNG format) before saving on + // BigEndian machines + if ( TQImage::systemByteOrder() == TQImage::BigEndian ) { + png_set_swap_alpha(png_ptr); + } + + // TQt==ARGB==Big(ARGB)==Little(BGRA) + if ( TQImage::systemByteOrder() == TQImage::LittleEndian ) { + png_set_bgr(png_ptr); + } + + if (off_x || off_y) { + png_set_oFFs(png_ptr, info_ptr, off_x, off_y, PNG_OFFSET_PIXEL); + } + + if ( frames_written > 0 ) + png_set_sig_bytes(png_ptr, 8); + + if ( image.dotsPerMeterX() > 0 || image.dotsPerMeterY() > 0 ) { + png_set_pHYs(png_ptr, info_ptr, + image.dotsPerMeterX(), image.dotsPerMeterY(), + PNG_RESOLUTION_METER); + } + +#ifndef QT_NO_IMAGE_TEXT + // Write short texts early. + set_text(image,png_ptr,info_ptr,TRUE); +#endif + + png_write_info(png_ptr, info_ptr); + +#ifndef QT_NO_IMAGE_TEXT + // Write long texts later. + set_text(image,png_ptr,info_ptr,FALSE); +#endif + + if ( image.depth() != 1 ) + png_set_packing(png_ptr); + + if ( image.depth() == 32 && !image.hasAlphaBuffer() ) + png_set_filler(png_ptr, 0, + TQImage::systemByteOrder() == TQImage::BigEndian ? + PNG_FILLER_BEFORE : PNG_FILLER_AFTER); + + if ( looping >= 0 && frames_written == 0 ) { + uchar data[13] = "NETSCAPE2.0"; + // 0123456789aBC + data[0xB] = looping%0x100; + data[0xC] = looping/0x100; + png_write_chunk(png_ptr, (png_byte*)"gIFx", data, 13); + } + if ( ms_delay >= 0 || disposal!=Unspecified ) { + uchar data[4]; + data[0] = disposal; + data[1] = 0; + data[2] = (ms_delay/10)/0x100; // hundredths + data[3] = (ms_delay/10)%0x100; + png_write_chunk(png_ptr, (png_byte*)"gIFg", data, 4); + } + + png_uint_32 width; + png_uint_32 height; + int bit_depth; + int color_type; + png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, + 0, 0, 0); + + uchar** jt = image.jumpTable(); + row_pointers=new png_bytep[height]; + uint y; + for (y=0; yioDevice()); + int quality = iio->quality(); + if ( quality >= 0 ) { + quality = TQMIN( quality, 100 ); + quality = (100-quality) * 9 / 91; // map [0,100] -> [9,0] + } + writer.setGamma(iio->gamma()); + bool ok = writer.writeImage( iio->image(), quality ); + iio->setStatus( ok ? 0 : -1 ); +} + +/*! + \class TQPNGImagePacker qpngio.h + \brief The TQPNGImagePacker class creates well-compressed PNG animations. + + \ingroup images + \ingroup graphics + + By using transparency, TQPNGImagePacker allows you to build a PNG + image from a sequence of TQImages. + + Images are added using packImage(). +*/ + + +/*! + Creates an image packer that writes PNG data to IO device \a iod + using a \a storage_depth bit encoding (use 8 or 32, depending on + the desired quality and compression retquirements). + + If the image needs to be modified to fit in a lower-resolution + result (e.g. converting from 32-bit to 8-bit), use the \a + conversionflags to specify how you'd prefer this to happen. + + \sa TQt::ImageConversionFlags +*/ +TQPNGImagePacker::TQPNGImagePacker(TQIODevice* iod, int storage_depth, + int conversionflags) : + TQPNGImageWriter(iod), + depth(storage_depth), + convflags(conversionflags), + alignx(1) +{ +} + +/*! + Aligns pixel differences to \a x pixels. For example, using 8 can + improve playback on certain hardware. Normally the default of + 1-pixel alignment (i.e. no alignment) gives better compression and + performance. +*/ +void TQPNGImagePacker::setPixelAlignment(int x) +{ + alignx = x; +} + +/*! + Adds the image \a img to the PNG animation, analyzing the + differences between this and the previous image to improve + compression. +*/ +bool TQPNGImagePacker::packImage(const TQImage& img) +{ + TQImage image = img.convertDepth(32); + if ( previous.isNull() ) { + // First image + writeImage(image.convertDepth(depth,convflags)); + } else { + bool done; + int minx, maxx, miny, maxy; + int w = image.width(); + int h = image.height(); + + TQRgb** jt = (TQRgb**)image.jumpTable(); + TQRgb** pjt = (TQRgb**)previous.jumpTable(); + + // Find left edge of change + done = FALSE; + for (minx = 0; minx < w && !done; minx++) { + for (int ty = 0; ty < h; ty++) { + if ( jt[ty][minx] != pjt[ty][minx] ) { + done = TRUE; + break; + } + } + } + minx--; + + // Find right edge of change + done = FALSE; + for (maxx = w-1; maxx >= 0 && !done; maxx--) { + for (int ty = 0; ty < h; ty++) { + if ( jt[ty][maxx] != pjt[ty][maxx] ) { + done = TRUE; + break; + } + } + } + maxx++; + + // Find top edge of change + done = FALSE; + for (miny = 0; miny < h && !done; miny++) { + for (int tx = 0; tx < w; tx++) { + if ( jt[miny][tx] != pjt[miny][tx] ) { + done = TRUE; + break; + } + } + } + miny--; + + // Find right edge of change + done = FALSE; + for (maxy = h-1; maxy >= 0 && !done; maxy--) { + for (int tx = 0; tx < w; tx++) { + if ( jt[maxy][tx] != pjt[maxy][tx] ) { + done = TRUE; + break; + } + } + } + maxy++; + + if ( minx > maxx ) minx=maxx=0; + if ( miny > maxy ) miny=maxy=0; + + if ( alignx > 1 ) { + minx -= minx % alignx; + maxx = maxx - maxx % alignx + alignx - 1; + } + + int dw = maxx-minx+1; + int dh = maxy-miny+1; + + TQImage diff(dw, dh, 32); + + diff.setAlphaBuffer(TRUE); + int x, y; + if ( alignx < 1 ) + alignx = 1; + for (y = 0; y < dh; y++) { + TQRgb* li = (TQRgb*)image.scanLine(y+miny)+minx; + TQRgb* lp = (TQRgb*)previous.scanLine(y+miny)+minx; + TQRgb* ld = (TQRgb*)diff.scanLine(y); + if ( alignx ) { + for (x = 0; x < dw; x+=alignx) { + int i; + for (i=0; iinfo(png_ptr,info); +} + +static void +CALLBACK_CALL_TYPE row_callback(png_structp png_ptr, png_bytep new_row, + png_uint_32 row_num, int pass) +{ + TQPNGFormat* that = (TQPNGFormat*)png_get_progressive_ptr(png_ptr); + that->row(png_ptr,new_row,row_num,pass); +} + +static void +CALLBACK_CALL_TYPE end_callback(png_structp png_ptr, png_infop info) +{ + TQPNGFormat* that = (TQPNGFormat*)png_get_progressive_ptr(png_ptr); + that->end(png_ptr,info); +} + +#if 0 +#ifdef PNG_USER_CHUNKS_SUPPORTED +static int +CALLBACK_CALL_TYPE user_chunk_callback(png_structp png_ptr, + png_unknown_chunkp chunk) +{ + TQPNGFormat* that = (TQPNGFormat*)png_get_progressive_ptr(png_ptr); + return that->user_chunk(png_ptr,chunk->data,chunk->size); +} +#endif +#endif + +#if defined(Q_C_CALLBACKS) +} +#endif + + +/*! + Constructs a TQPNGFormat object. +*/ +TQPNGFormat::TQPNGFormat() +{ + state = MovieStart; + first_frame = 1; + base_offx = 0; + base_offy = 0; + png_ptr = 0; + info_ptr = 0; +} + + +/*! + Destroys a TQPNGFormat object. +*/ +TQPNGFormat::~TQPNGFormat() +{ + if ( png_ptr ) + png_destroy_read_struct(&png_ptr, &info_ptr, 0); +} + + +/*! + This function decodes some data into image changes. + + Returns the number of bytes consumed. +*/ +int TQPNGFormat::decode(TQImage& img, TQImageConsumer* cons, + const uchar* buffer, int length) +{ + consumer = cons; + image = &img; + + if ( state != Inside ) { + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); + if (!png_ptr) { + info_ptr = 0; + image = 0; + return -1; + } + + png_set_error_fn(png_ptr, 0, 0, qt_png_warning); + png_set_compression_level(png_ptr, 9); + + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + image = 0; + return -1; + } + + if (setjmp((png_ptr)->jmpbuf)) { + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + image = 0; + return -1; + } + + png_set_progressive_read_fn(png_ptr, (void *)this, + info_callback, row_callback, end_callback); + +#ifdef PNG_USER_CHUNKS_SUPPORTED + // Can't do this yet. libpng has a crash bug with unknown (user) chunks. + // Warwick has sent them a patch. + // png_set_read_user_chunk_fn(png_ptr, 0, user_chunk_callback); + // png_set_keep_unknown_chunks(png_ptr, 2/*HANDLE_CHUNK_IF_SAFE*/, 0, 0); +#endif + + if ( state != MovieStart && *buffer != 0211 ) { + // Good, no signature - the preferred way to concat PNG images. + // Skip them. + png_set_sig_bytes(png_ptr, 8); + } + + state = Inside; + } + + if ( !png_ptr ) return 0; + + if (setjmp(png_ptr->jmpbuf)) { + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + image = 0; + state = MovieStart; + return -1; + } + unused_data = 0; + png_process_data(png_ptr, info_ptr, (png_bytep)buffer, length); + int l = length - unused_data; + + // TODO: send incremental stuff to consumer (optional) + + if ( state != Inside ) { + if ( png_ptr ) + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + } + + image = 0; + return l; +} + +void TQPNGFormat::info(png_structp png, png_infop) +{ + png_set_interlace_handling(png); + setup_qt(*image, png, info_ptr); +} + +void TQPNGFormat::row(png_structp png, png_bytep new_row, + png_uint_32 row_num, int) +{ + uchar* old_row = image->scanLine(row_num); + png_progressive_combine_row(png, old_row, new_row); +} + + +void TQPNGFormat::end(png_structp png, png_infop info) +{ + int offx = png_get_x_offset_pixels(png,info) - base_offx; + int offy = png_get_y_offset_pixels(png,info) - base_offy; + if ( first_frame ) { + base_offx = offx; + base_offy = offy; + first_frame = 0; + } + image->setOffset(TQPoint(offx,offy)); + image->setDotsPerMeterX(png_get_x_pixels_per_meter(png,info)); + image->setDotsPerMeterY(png_get_y_pixels_per_meter(png,info)); +#ifndef QT_NO_IMAGE_TEXT + png_textp text_ptr; + int num_text=0; + png_get_text(png,info,&text_ptr,&num_text); + while (num_text--) { + image->setText(text_ptr->key,0,text_ptr->text); + text_ptr++; + } +#endif + TQRect r(0,0,image->width(),image->height()); + consumer->frameDone(TQPoint(offx,offy),r); + consumer->end(); + state = FrameStart; + unused_data = (int)png->buffer_size; // Since libpng doesn't tell us +} + +#ifdef PNG_USER_CHUNKS_SUPPORTED + +/* +#ifndef QT_NO_IMAGE_TEXT +static bool skip(png_uint_32& max, png_bytep& data) +{ + while (*data) { + if ( !max ) return FALSE; + max--; + data++; + } + if ( !max ) return FALSE; + max--; + data++; // skip to after NUL + return TRUE; +} +#endif +*/ + +int TQPNGFormat::user_chunk(png_structp png, + png_bytep data, png_uint_32 length) +{ +#if 0 // NOT SUPPORTED: experimental PNG animation. + // qDebug("Got %ld-byte %s chunk", length, png->chunk_name); + if ( 0==qstrcmp((char*)png->chunk_name, "gIFg") + && length == 4 ) { + + //TQPNGImageWriter::DisposalMethod disposal = + // (TQPNGImageWriter::DisposalMethod)data[0]; + // ### TODO: use the disposal method + int ms_delay = ((data[2] << 8) | data[3])*10; + consumer->setFramePeriod(ms_delay); + return 1; + } else if ( 0==qstrcmp((char*)png->chunk_name, "gIFx") + && length == 13 ) { + if ( qstrncmp((char*)data,"NETSCAPE2.0",11)==0 ) { + int looping = (data[0xC]<<8)|data[0xB]; + consumer->setLooping(looping); + return 1; + } + } +#else + Q_UNUSED( png ) + Q_UNUSED( data ) + Q_UNUSED( length ) +#endif + +#ifndef QT_NO_IMAGE_TEXT + /* + + libpng now supports this chunk. + + + if ( 0==qstrcmp((char*)png->chunk_name, "iTXt") && length>=6 ) { + const char* keyword = (const char*)data; + if ( !skip(length,data) ) return 0; + if ( length >= 4 ) { + char compression_flag = *data++; + char compression_method = *data++; + if ( compression_flag == compression_method ) { + // fool the compiler into thinking they're used + } + const char* lang = (const char*)data; + if ( !skip(length,data) ) return 0; + // const char* keyword_utf8 = (const char*)data; + if ( !skip(length,data) ) return 0; + const char* text_utf8 = (const char*)data; + if ( !skip(length,data) ) return 0; + TQString text = TQString::fromUtf8(text_utf8); + image->setText(keyword,lang[0] ? lang : 0,text); + return 1; + } + } + */ +#endif + + return 0; +} +#endif + + +static TQPNGFormatType* globalPngFormatTypeObject = 0; + +#endif // QT_NO_ASYNC_IMAGE_IO + +static bool done = FALSE; +void qCleanupPngIO() +{ +#ifndef QT_NO_ASYNC_IMAGE_IO + if ( globalPngFormatTypeObject ) { + delete globalPngFormatTypeObject; + globalPngFormatTypeObject = 0; + } +#endif + done = FALSE; +} + +void qInitPngIO() +{ + if ( !done ) { + done = TRUE; + TQImageIO::defineIOHandler( "PNG", "^.PNG\r", 0, read_png_image, + write_png_image); +#ifndef QT_NO_ASYNC_IMAGE_IO + globalPngFormatTypeObject = new TQPNGFormatType; +#endif + qAddPostRoutine( qCleanupPngIO ); + } +} + +void qt_zlib_compression_hack() +{ + compress(0,0,0,0); + uncompress(0,0,0,0); +} + +#endif // QT_NO_IMAGEIO_PNG diff --git a/src/kernel/qpngio.h b/src/kernel/qpngio.h new file mode 100644 index 000000000..819e01e45 --- /dev/null +++ b/src/kernel/qpngio.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Definition of PNG TQImage IOHandler +** +** Created : 970521 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPNGIO_H +#define TQPNGIO_H + +#ifndef QT_H +#include "qimage.h" +#endif // QT_H + +#ifndef QT_NO_IMAGEIO_PNG + +void qInitPngIO(); + +class TQIODevice; + +#ifndef Q_PNGEXPORT +#if !defined(QT_PLUGIN) +#define Q_PNGEXPORT Q_EXPORT +#else +#define Q_PNGEXPORT +#endif +#endif + +class Q_PNGEXPORT TQPNGImageWriter { +public: + TQPNGImageWriter(TQIODevice*); + ~TQPNGImageWriter(); + + enum DisposalMethod { Unspecified, NoDisposal, RestoreBackground, RestoreImage }; + void setDisposalMethod(DisposalMethod); + void setLooping(int loops=0); // 0 == infinity + void setFrameDelay(int msecs); + void setGamma(float); + + bool writeImage(const TQImage& img, int x, int y); + bool writeImage(const TQImage& img, int quality, int x, int y); + bool writeImage(const TQImage& img) + { return writeImage(img, 0, 0); } + bool writeImage(const TQImage& img, int quality) + { return writeImage(img, quality, 0, 0); } + + TQIODevice* device() { return dev; } + +private: + TQIODevice* dev; + int frames_written; + DisposalMethod disposal; + int looping; + int ms_delay; + float gamma; +}; + +class Q_PNGEXPORT TQPNGImagePacker : public TQPNGImageWriter { +public: + TQPNGImagePacker(TQIODevice*, int depth, int convflags); + + void setPixelAlignment(int x); + bool packImage(const TQImage& img); + +private: + TQImage previous; + int depth; + int convflags; + int alignx; +}; + +#endif // QT_NO_IMAGEIO_PNG + +#endif // TQPNGIO_H diff --git a/src/kernel/qpoint.cpp b/src/kernel/qpoint.cpp new file mode 100644 index 000000000..3853e4e3d --- /dev/null +++ b/src/kernel/qpoint.cpp @@ -0,0 +1,443 @@ +/**************************************************************************** +** +** Implementation of TQPoint class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpoint.h" +#include "qdatastream.h" + + +/*! + \class TQPoint qpoint.h + \brief The TQPoint class defines a point in the plane. + + \ingroup images + \ingroup graphics + \mainclass + + A point is specified by an x coordinate and a y coordinate. + + The coordinate type is \c TQCOORD (a 32-bit integer). The minimum + value of \c TQCOORD is \c TQCOORD_MIN (-2147483648) and the maximum + value is \c TQCOORD_MAX (2147483647). + + The coordinates are accessed by the functions x() and y(); they + can be set by setX() and setY() or by the reference functions rx() + and ry(). + + Given a point \e p, the following statements are all equivalent: + \code + p.setX( p.x() + 1 ); + p += TQPoint( 1, 0 ); + p.rx()++; + \endcode + + A TQPoint can also be used as a vector. Addition and subtraction + of TQPoints are defined as for vectors (each component is added + separately). You can divide or multiply a TQPoint by an \c int or a + \c double. The function manhattanLength() gives an inexpensive + approximation of the length of the TQPoint interpreted as a vector. + + Example: + \code + //TQPoint oldPos is defined somewhere else + MyWidget::mouseMoveEvent( TQMouseEvent *e ) + { + TQPoint vector = e->pos() - oldPos; + if ( vector.manhattanLength() > 3 ) + ... //mouse has moved more than 3 pixels since oldPos + } + \endcode + + TQPoints can be compared for equality or inequality, and they can + be written to and read from a TQStream. + + \sa TQPointArray TQSize, TQRect +*/ + + +/***************************************************************************** + TQPoint member functions + *****************************************************************************/ + +/*! + \fn TQPoint::TQPoint() + + Constructs a point with coordinates (0, 0) (isNull() returns TRUE). +*/ + +/*! + \fn TQPoint::TQPoint( int xpos, int ypos ) + + Constructs a point with x value \a xpos and y value \a ypos. +*/ + +/*! + \fn bool TQPoint::isNull() const + + Returns TRUE if both the x value and the y value are 0; otherwise + returns FALSE. +*/ + +/*! + \fn int TQPoint::x() const + + Returns the x coordinate of the point. + + \sa setX() y() +*/ + +/*! + \fn int TQPoint::y() const + + Returns the y coordinate of the point. + + \sa setY() x() +*/ + +/*! + \fn void TQPoint::setX( int x ) + + Sets the x coordinate of the point to \a x. + + \sa x() setY() +*/ + +/*! + \fn void TQPoint::setY( int y ) + + Sets the y coordinate of the point to \a y. + + \sa y() setX() +*/ + + +/*! + \fn TQCOORD &TQPoint::rx() + + Returns a reference to the x coordinate of the point. + + Using a reference makes it possible to directly manipulate x. + + Example: + \code + TQPoint p( 1, 2 ); + p.rx()--; // p becomes (0, 2) + \endcode + + \sa ry() +*/ + +/*! + \fn TQCOORD &TQPoint::ry() + + Returns a reference to the y coordinate of the point. + + Using a reference makes it possible to directly manipulate y. + + Example: + \code + TQPoint p( 1, 2 ); + p.ry()++; // p becomes (1, 3) + \endcode + + \sa rx() +*/ + + +/*! + \fn TQPoint &TQPoint::operator+=( const TQPoint &p ) + + Adds point \a p to this point and returns a reference to this + point. + + Example: + \code + TQPoint p( 3, 7 ); + TQPoint q( -1, 4 ); + p += q; // p becomes (2,11) + \endcode +*/ + +/*! + \fn TQPoint &TQPoint::operator-=( const TQPoint &p ) + + Subtracts point \a p from this point and returns a reference to + this point. + + Example: + \code + TQPoint p( 3, 7 ); + TQPoint q( -1, 4 ); + p -= q; // p becomes (4,3) + \endcode +*/ + +/*! + \fn TQPoint &TQPoint::operator*=( int c ) + + Multiplies this point's x and y by \a c, and returns a reference + to this point. + + Example: + \code + TQPoint p( -1, 4 ); + p *= 2; // p becomes (-2,8) + \endcode +*/ + +/*! + \overload TQPoint &TQPoint::operator*=( double c ) + + Multiplies this point's x and y by \a c, and returns a reference + to this point. + + Example: + \code + TQPoint p( -1, 4 ); + p *= 2.5; // p becomes (-3,10) + \endcode + + Note that the result is truncated because points are held as + integers. +*/ + + +/*! + \fn bool operator==( const TQPoint &p1, const TQPoint &p2 ) + + \relates TQPoint + + Returns TRUE if \a p1 and \a p2 are equal; otherwise returns FALSE. +*/ + +/*! + \fn bool operator!=( const TQPoint &p1, const TQPoint &p2 ) + + \relates TQPoint + + Returns TRUE if \a p1 and \a p2 are not equal; otherwise returns FALSE. +*/ + +/*! + \fn const TQPoint operator+( const TQPoint &p1, const TQPoint &p2 ) + + \relates TQPoint + + Returns the sum of \a p1 and \a p2; each component is added separately. +*/ + +/*! + \fn const TQPoint operator-( const TQPoint &p1, const TQPoint &p2 ) + + \relates TQPoint + + Returns \a p2 subtracted from \a p1; each component is subtracted + separately. +*/ + +/*! + \fn const TQPoint operator*( const TQPoint &p, int c ) + + \relates TQPoint + + Returns the TQPoint formed by multiplying both components of \a p + by \a c. +*/ + +/*! + \overload const TQPoint operator*( int c, const TQPoint &p ) + + \relates TQPoint + + Returns the TQPoint formed by multiplying both components of \a p + by \a c. +*/ + +/*! + \overload const TQPoint operator*( const TQPoint &p, double c ) + + \relates TQPoint + + Returns the TQPoint formed by multiplying both components of \a p + by \a c. + + Note that the result is truncated because points are held as + integers. +*/ + +/*! + \overload const TQPoint operator*( double c, const TQPoint &p ) + + \relates TQPoint + + Returns the TQPoint formed by multiplying both components of \a p + by \a c. + + Note that the result is truncated because points are held as + integers. +*/ + +/*! + \overload const TQPoint operator-( const TQPoint &p ) + + \relates TQPoint + + Returns the TQPoint formed by changing the sign of both components + of \a p, equivalent to \c{TQPoint(0,0) - p}. +*/ + +/*! + \fn TQPoint &TQPoint::operator/=( int c ) + + Divides both x and y by \a c, and returns a reference to this + point. + + Example: + \code + TQPoint p( -2, 8 ); + p /= 2; // p becomes (-1,4) + \endcode +*/ + +/*! + \overload TQPoint &TQPoint::operator/=( double c ) + + Divides both x and y by \a c, and returns a reference to this + point. + + Example: + \code + TQPoint p( -3, 10 ); + p /= 2.5; // p becomes (-1,4) + \endcode + + Note that the result is truncated because points are held as + integers. +*/ + +/*! + \fn const TQPoint operator/( const TQPoint &p, int c ) + + \relates TQPoint + + Returns the TQPoint formed by dividing both components of \a p by + \a c. +*/ + +/*! + \overload const TQPoint operator/( const TQPoint &p, double c ) + + \relates TQPoint + + Returns the TQPoint formed by dividing both components of \a p + by \a c. + + Note that the result is truncated because points are held as + integers. +*/ + + +void TQPoint::warningDivByZero() +{ +#if defined(QT_CHECK_MATH) + qWarning( "TQPoint: Division by zero error" ); +#endif +} + + +/***************************************************************************** + TQPoint stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates TQPoint + + Writes point \a p to the stream \a s and returns a reference to + the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQPoint &p ) +{ + if ( s.version() == 1 ) + s << (Q_INT16)p.x() << (Q_INT16)p.y(); + else + s << (Q_INT32)p.x() << (Q_INT32)p.y(); + return s; +} + +/*! + \relates TQPoint + + Reads a TQPoint from the stream \a s into point \a p and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQPoint &p ) +{ + if ( s.version() == 1 ) { + Q_INT16 x, y; + s >> x; p.rx() = x; + s >> y; p.ry() = y; + } + else { + Q_INT32 x, y; + s >> x; p.rx() = x; + s >> y; p.ry() = y; + } + return s; +} +#endif // QT_NO_DATASTREAM +/*! + Returns the sum of the absolute values of x() and y(), + traditionally known as the "Manhattan length" of the vector from + the origin to the point. The tradition arises because such + distances apply to travelers who can only travel on a rectangular + grid, like the streets of Manhattan. + + This is a useful, and tquick to calculate, approximation to the + true length: sqrt(pow(x(),2)+pow(y(),2)). +*/ +int TQPoint::manhattanLength() const +{ + return TQABS(x())+TQABS(y()); +} diff --git a/src/kernel/qpoint.h b/src/kernel/qpoint.h new file mode 100644 index 000000000..ebcdc3a07 --- /dev/null +++ b/src/kernel/qpoint.h @@ -0,0 +1,219 @@ +/**************************************************************************** +** +** Definition of TQPoint class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPOINT_H +#define TQPOINT_H + +#ifndef QT_H +#include "qwindowdefs.h" +#endif // QT_H + + +class Q_EXPORT TQPoint +{ +public: + TQPoint(); + TQPoint( int xpos, int ypos ); + + bool isNull() const; + + int x() const; + int y() const; + void setX( int x ); + void setY( int y ); + + int manhattanLength() const; + + TQCOORD &rx(); + TQCOORD &ry(); + + TQPoint &operator+=( const TQPoint &p ); + TQPoint &operator-=( const TQPoint &p ); + TQPoint &operator*=( int c ); + TQPoint &operator*=( double c ); + TQPoint &operator/=( int c ); + TQPoint &operator/=( double c ); + + friend inline bool operator==( const TQPoint &, const TQPoint & ); + friend inline bool operator!=( const TQPoint &, const TQPoint & ); + friend inline const TQPoint operator+( const TQPoint &, const TQPoint & ); + friend inline const TQPoint operator-( const TQPoint &, const TQPoint & ); + friend inline const TQPoint operator*( const TQPoint &, int ); + friend inline const TQPoint operator*( int, const TQPoint & ); + friend inline const TQPoint operator*( const TQPoint &, double ); + friend inline const TQPoint operator*( double, const TQPoint & ); + friend inline const TQPoint operator-( const TQPoint & ); + friend inline const TQPoint operator/( const TQPoint &, int ); + friend inline const TQPoint operator/( const TQPoint &, double ); + +private: + static void warningDivByZero(); + +#if defined(Q_OS_MAC) + TQCOORD yp; + TQCOORD xp; +#else + TQCOORD xp; + TQCOORD yp; +#endif +}; + + +/***************************************************************************** + TQPoint stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQPoint & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQPoint & ); +#endif + +/***************************************************************************** + TQPoint inline functions + *****************************************************************************/ + +inline TQPoint::TQPoint() +{ xp=0; yp=0; } + +inline TQPoint::TQPoint( int xpos, int ypos ) +{ xp=(TQCOORD)xpos; yp=(TQCOORD)ypos; } + +inline bool TQPoint::isNull() const +{ return xp == 0 && yp == 0; } + +inline int TQPoint::x() const +{ return xp; } + +inline int TQPoint::y() const +{ return yp; } + +inline void TQPoint::setX( int x ) +{ xp = (TQCOORD)x; } + +inline void TQPoint::setY( int y ) +{ yp = (TQCOORD)y; } + +inline TQCOORD &TQPoint::rx() +{ return xp; } + +inline TQCOORD &TQPoint::ry() +{ return yp; } + +inline TQPoint &TQPoint::operator+=( const TQPoint &p ) +{ xp+=p.xp; yp+=p.yp; return *this; } + +inline TQPoint &TQPoint::operator-=( const TQPoint &p ) +{ xp-=p.xp; yp-=p.yp; return *this; } + +inline TQPoint &TQPoint::operator*=( int c ) +{ xp*=(TQCOORD)c; yp*=(TQCOORD)c; return *this; } + +inline TQPoint &TQPoint::operator*=( double c ) +{ xp=(TQCOORD)(xp*c); yp=(TQCOORD)(yp*c); return *this; } + +inline bool operator==( const TQPoint &p1, const TQPoint &p2 ) +{ return p1.xp == p2.xp && p1.yp == p2.yp; } + +inline bool operator!=( const TQPoint &p1, const TQPoint &p2 ) +{ return p1.xp != p2.xp || p1.yp != p2.yp; } + +inline const TQPoint operator+( const TQPoint &p1, const TQPoint &p2 ) +{ return TQPoint(p1.xp+p2.xp, p1.yp+p2.yp); } + +inline const TQPoint operator-( const TQPoint &p1, const TQPoint &p2 ) +{ return TQPoint(p1.xp-p2.xp, p1.yp-p2.yp); } + +inline const TQPoint operator*( const TQPoint &p, int c ) +{ return TQPoint(p.xp*c, p.yp*c); } + +inline const TQPoint operator*( int c, const TQPoint &p ) +{ return TQPoint(p.xp*c, p.yp*c); } + +inline const TQPoint operator*( const TQPoint &p, double c ) +{ return TQPoint((TQCOORD)(p.xp*c), (TQCOORD)(p.yp*c)); } + +inline const TQPoint operator*( double c, const TQPoint &p ) +{ return TQPoint((TQCOORD)(p.xp*c), (TQCOORD)(p.yp*c)); } + +inline const TQPoint operator-( const TQPoint &p ) +{ return TQPoint(-p.xp, -p.yp); } + +inline TQPoint &TQPoint::operator/=( int c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0 ) + warningDivByZero(); +#endif + xp/=(TQCOORD)c; + yp/=(TQCOORD)c; + return *this; +} + +inline TQPoint &TQPoint::operator/=( double c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0.0 ) + warningDivByZero(); +#endif + xp=(TQCOORD)(xp/c); + yp=(TQCOORD)(yp/c); + return *this; +} + +inline const TQPoint operator/( const TQPoint &p, int c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0 ) + TQPoint::warningDivByZero(); +#endif + return TQPoint(p.xp/c, p.yp/c); +} + +inline const TQPoint operator/( const TQPoint &p, double c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0.0 ) + TQPoint::warningDivByZero(); +#endif + return TQPoint((TQCOORD)(p.xp/c), (TQCOORD)(p.yp/c)); +} + +#define Q_DEFINED_QPOINT +#include "qwinexport.h" +#endif // TQPOINT_H diff --git a/src/kernel/qpointarray.cpp b/src/kernel/qpointarray.cpp new file mode 100644 index 000000000..8659c5181 --- /dev/null +++ b/src/kernel/qpointarray.cpp @@ -0,0 +1,1109 @@ +/**************************************************************************** +** +** Implementation of TQPointArray class +** +** Created : 940213 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpointarray.h" +#include "qrect.h" +#include "qdatastream.h" +#include "qwmatrix.h" +#include + +const double Q_PI = 3.14159265358979323846; // pi // one more useful comment + + +/*! + \class TQPointArray qpointarray.h + \brief The TQPointArray class provides an array of points. + + \ingroup images + \ingroup graphics + \ingroup shared + + A TQPointArray is an array of TQPoint objects. In addition to the + functions provided by TQMemArray, TQPointArray provides some + point-specific functions. + + For convenient reading and writing of the point data use + setPoints(), putPoints(), point(), and setPoint(). + + For geometry operations use boundingRect() and translate(). There + is also the TQWMatrix::map() function for more general + transformations of TQPointArrays. You can also create arcs and + ellipses with makeArc() and makeEllipse(). + + Among others, TQPointArray is used by TQPainter::drawLineSegments(), + TQPainter::drawPolyline(), TQPainter::drawPolygon() and + TQPainter::drawCubicBezier(). + + Note that because this class is a TQMemArray, copying an array and + modifying the copy modifies the original as well, i.e. a shallow + copy. If you need a deep copy use copy() or detach(), for example: + + \code + void drawGiraffe( const TQPointArray & r, TQPainter * p ) + { + TQPointArray tmp = r; + tmp.detach(); + // some code that modifies tmp + p->drawPoints( tmp ); + } + \endcode + + If you forget the tmp.detach(), the const array will be modified. + + \sa TQPainter TQWMatrix TQMemArray +*/ + + +/***************************************************************************** + TQPointArray member functions + *****************************************************************************/ + +/*! + \fn TQPointArray::TQPointArray() + + Constructs a null point array. + + \sa isNull() +*/ + +/*! + \fn TQPointArray::TQPointArray( int size ) + + Constructs a point array with room for \a size points. Makes a + null array if \a size == 0. + + \sa resize(), isNull() +*/ + +/*! + \fn TQPointArray::TQPointArray( const TQPointArray &a ) + + Constructs a shallow copy of the point array \a a. + + \sa copy() detach() +*/ + +/*! + Constructs a point array from the rectangle \a r. + + If \a closed is FALSE, then the point array just contains the + following four points in the listed order: r.topLeft(), + r.topRight(), r.bottomRight() and r.bottomLeft(). + + If \a closed is TRUE, then a fifth point is set to r.topLeft(). +*/ + +TQPointArray::TQPointArray( const TQRect &r, bool closed ) +{ + setPoints( 4, r.left(), r.top(), + r.right(), r.top(), + r.right(), r.bottom(), + r.left(), r.bottom() ); + if ( closed ) { + resize( 5 ); + setPoint( 4, r.left(), r.top() ); + } +} + +/*! + \internal + Constructs a point array with \a nPoints points, taken from the + \a points array. + + Equivalent to setPoints(nPoints, points). +*/ + +TQPointArray::TQPointArray( int nPoints, const TQCOORD *points ) +{ + setPoints( nPoints, points ); +} + + +/*! + \fn TQPointArray::~TQPointArray() + + Destroys the point array. +*/ + + +/*! + \fn TQPointArray &TQPointArray::operator=( const TQPointArray &a ) + + Assigns a shallow copy of \a a to this point array and returns a + reference to this point array. + + Equivalent to assign(a). + + \sa copy() detach() +*/ + +/*! + \fn TQPointArray TQPointArray::copy() const + + Creates a deep copy of the array. + + \sa detach() +*/ + + + +/*! + Translates all points in the array by \a (dx, dy). +*/ + +void TQPointArray::translate( int dx, int dy ) +{ + register TQPoint *p = data(); + register int i = size(); + TQPoint pt( dx, dy ); + while ( i-- ) { + *p += pt; + p++; + } +} + + +/*! + Reads the coordinates of the point at position \a index within the + array and writes them into \a *x and \a *y. +*/ + +void TQPointArray::point( uint index, int *x, int *y ) const +{ + TQPoint p = TQMemArray::at( index ); + if ( x ) + *x = (int)p.x(); + if ( y ) + *y = (int)p.y(); +} + +/*! + \overload + + Returns the point at position \a index within the array. +*/ + +TQPoint TQPointArray::point( uint index ) const +{ // #### index out of bounds + return TQMemArray::at( index ); +} + +/*! + \fn void TQPointArray::setPoint( uint i, const TQPoint &p ) + + \overload + + Sets the point at array index \a i to \a p. +*/ + +/*! + Sets the point at position \a index in the array to \a (x, y). +*/ + +void TQPointArray::setPoint( uint index, int x, int y ) +{ // #### index out of bounds + TQMemArray::at( index ) = TQPoint( x, y ); +} + +/*! + \internal + Resizes the array to \a nPoints and sets the points in the array to + the values taken from \a points. + + Returns TRUE if successful, or FALSE if the array could not be + resized (normally due to lack of memory). + + The example code creates an array with two points (1,2) and (3,4): + \code + static TQCOORD points[] = { 1,2, 3,4 }; + TQPointArray a; + a.setPoints( 2, points ); + \endcode + + \sa resize(), putPoints() +*/ + +bool TQPointArray::setPoints( int nPoints, const TQCOORD *points ) +{ + if ( !resize(nPoints) ) + return FALSE; + int i = 0; + while ( nPoints-- ) { // make array of points + setPoint( i++, *points, *(points+1) ); + points++; + points++; + } + return TRUE; +} + +/*! + \overload + + Resizes the array to \a nPoints and sets the points in the array + to the values taken from the variable argument list. + + Returns TRUE if successful, or FALSE if the array could not be + resized (typically due to lack of memory). + + The example code creates an array with two points (1,2) and (3,4): + + \code + TQPointArray a; + a.setPoints( 2, 1,2, 3,4 ); + \endcode + + The points are given as a sequence of integers, starting with \a + firstx then \a firsty, and so on. + + \sa resize(), putPoints() +*/ + +bool TQPointArray::setPoints( int nPoints, int firstx, int firsty, ... ) +{ + va_list ap; + if ( !resize(nPoints) ) + return FALSE; + setPoint( 0, firstx, firsty ); // set first point + int i = 1, x, y; + nPoints--; + va_start( ap, firsty ); + while ( nPoints-- ) { + x = va_arg( ap, int ); + y = va_arg( ap, int ); + setPoint( i++, x, y ); + } + va_end( ap ); + return TRUE; +} + +/*! \overload + \internal + Copies \a nPoints points from the \a points coord array into + this point array, and resizes the point array if + \c{index+nPoints} exceeds the size of the array. + + Returns TRUE if successful, or FALSE if the array could not be + resized (typically due to lack of memory). + +*/ + +bool TQPointArray::putPoints( int index, int nPoints, const TQCOORD *points ) +{ + if ( index + nPoints > (int)size() ) { // extend array + if ( !resize( index + nPoints ) ) + return FALSE; + } + int i = index; + while ( nPoints-- ) { // make array of points + setPoint( i++, *points, *(points+1) ); + points++; + points++; + } + return TRUE; +} + +/*! + Copies \a nPoints points from the variable argument list into this + point array from position \a index, and resizes the point array if + \c{index+nPoints} exceeds the size of the array. + + Returns TRUE if successful, or FALSE if the array could not be + resized (typically due to lack of memory). + + The example code creates an array with three points (4,5), (6,7) + and (8,9), by expanding the array from 1 to 3 points: + + \code + TQPointArray a( 1 ); + a[0] = TQPoint( 4, 5 ); + a.putPoints( 1, 2, 6,7, 8,9 ); // index == 1, points == 2 + \endcode + + This has the same result, but here putPoints overwrites rather + than extends: + \code + TQPointArray a( 3 ); + a.putPoints( 0, 3, 4,5, 0,0, 8,9 ); + a.putPoints( 1, 1, 6,7 ); + \endcode + + The points are given as a sequence of integers, starting with \a + firstx then \a firsty, and so on. + + \sa resize() +*/ + +bool TQPointArray::putPoints( int index, int nPoints, int firstx, int firsty, + ... ) +{ + va_list ap; + if ( index + nPoints > (int)size() ) { // extend array + if ( !resize(index + nPoints) ) + return FALSE; + } + if ( nPoints <= 0 ) + return TRUE; + setPoint( index, firstx, firsty ); // set first point + int i = index + 1, x, y; + nPoints--; + va_start( ap, firsty ); + while ( nPoints-- ) { + x = va_arg( ap, int ); + y = va_arg( ap, int ); + setPoint( i++, x, y ); + } + va_end( ap ); + return TRUE; +} + + +/*! + \overload + + This version of the function copies \a nPoints from \a from into + this array, starting at \a index in this array and \a fromIndex in + \a from. \a fromIndex is 0 by default. + + \code + TQPointArray a; + a.putPoints( 0, 3, 1,2, 0,0, 5,6 ); + // a is now the three-point array ( 1,2, 0,0, 5,6 ); + TQPointArray b; + b.putPoints( 0, 3, 4,4, 5,5, 6,6 ); + // b is now ( 4,4, 5,5, 6,6 ); + a.putPoints( 2, 3, b ); + // a is now ( 1,2, 0,0, 4,4, 5,5, 6,6 ); + \endcode +*/ + +bool TQPointArray::putPoints( int index, int nPoints, + const TQPointArray & from, int fromIndex ) +{ + if ( index + nPoints > (int)size() ) { // extend array + if ( !resize(index + nPoints) ) + return FALSE; + } + if ( nPoints <= 0 ) + return TRUE; + int n = 0; + while( n < nPoints ) { + setPoint( index+n, from[fromIndex+n] ); + n++; + } + return TRUE; +} + + +/*! + Returns the bounding rectangle of the points in the array, or + TQRect(0,0,0,0) if the array is empty. +*/ + +TQRect TQPointArray::boundingRect() const +{ + if ( isEmpty() ) + return TQRect( 0, 0, 0, 0 ); // null rectangle + register TQPoint *pd = data(); + int minx, maxx, miny, maxy; + minx = maxx = pd->x(); + miny = maxy = pd->y(); + pd++; + for ( int i=1; i<(int)size(); i++ ) { // find min+max x and y + if ( pd->x() < minx ) + minx = pd->x(); + else if ( pd->x() > maxx ) + maxx = pd->x(); + if ( pd->y() < miny ) + miny = pd->y(); + else if ( pd->y() > maxy ) + maxy = pd->y(); + pd++; + } + return TQRect( TQPoint(minx,miny), TQPoint(maxx,maxy) ); +} + + +static inline int fix_angle( int a ) +{ + if ( a > 16*360 ) + a %= 16*360; + else if ( a < -16*360 ) { + a = -( (-a) % (16*360) ); + } + return a; +} + +/*! + Sets the points of the array to those describing an arc of an + ellipse with size, width \a w by height \a h, and position (\a x, + \a y), starting from angle \a a1 and spanning by angle \a a2. The + resulting array has sufficient resolution for pixel accuracy (see + the overloaded function which takes an additional TQWMatrix + parameter). + + Angles are specified in 16ths of a degree, i.e. a full circle + equals 5760 (16*360). Positive values mean counter-clockwise, + whereas negative values mean the clockwise direction. Zero degrees + is at the 3 o'clock position. + + See the \link qcanvasellipse.html#anglediagram angle diagram\endlink. +*/ + +void TQPointArray::makeArc( int x, int y, int w, int h, int a1, int a2 ) +{ +#if !defined(QT_OLD_MAKEELLIPSE) && !defined(QT_NO_TRANSFORMATIONS) + TQWMatrix unit; + makeArc(x,y,w,h,a1,a2,unit); +#else + a1 = fix_angle( a1 ); + if ( a1 < 0 ) + a1 += 16*360; + a2 = fix_angle( a2 ); + int a3 = a2 > 0 ? a2 : -a2; // abs angle + makeEllipse( x, y, w, h ); + int npts = a3*size()/(16*360); // # points in arc array + TQPointArray a(npts); + int i = a1*size()/(16*360); + int j = 0; + if ( a2 > 0 ) { + while ( npts-- ) { + if ( i >= (int)size() ) // wrap index + i = 0; + a.TQMemArray::at( j++ ) = TQMemArray::at( i++ ); + } + } else { + while ( npts-- ) { + if ( i < 0 ) // wrap index + i = (int)size()-1; + a.TQMemArray::at( j++ ) = TQMemArray::at( i-- ); + } + } + *this = a; + return; +#endif +} + +#ifndef QT_NO_TRANSFORMATIONS +// Based upon: +// parelarc.c from Graphics Gems III +// VanAken / Simar, "A Parametric Elliptical Arc Algorithm" +// +static void +qtr_elips(TQPointArray& a, int off, double dxP, double dyP, double dxQ, double dyQ, double dxK, double dyK, int m) +{ +#define PIV2 102944 /* fixed point PI/2 */ +#define TWOPI 411775 /* fixed point 2*PI */ +#define HALF 32768 /* fixed point 1/2 */ + + int xP, yP, xQ, yQ, xK, yK; + xP = int(dxP * 65536.0); yP = int(dyP * 65536.0); + xQ = int(dxQ * 65536.0); yQ = int(dyQ * 65536.0); + xK = int(dxK * 65536.0); yK = int(dyK * 65536.0); + + int i; + int vx, ux, vy, uy, xJ, yJ; + + vx = xK - xQ; /* displacements from center */ + ux = xK - xP; + vy = yK - yQ; + uy = yK - yP; + xJ = xP - vx + HALF; /* center of ellipse J */ + yJ = yP - vy + HALF; + + int r; + ux -= (r = ux >> (2*m + 3)); /* cancel 2nd-order error */ + ux -= (r >>= (2*m + 4)); /* cancel 4th-order error */ + ux -= r >> (2*m + 3); /* cancel 6th-order error */ + ux += vx >> (m + 1); /* cancel 1st-order error */ + uy -= (r = uy >> (2*m + 3)); /* cancel 2nd-order error */ + uy -= (r >>= (2*m + 4)); /* cancel 4th-order error */ + uy -= r >> (2*m + 3); /* cancel 6th-order error */ + uy += vy >> (m + 1); /* cancel 1st-order error */ + + const int qn = a.size()/4; + for (i = 0; i < qn; i++) { + a[off+i] = TQPoint((xJ + vx) >> 16, (yJ + vy) >> 16); + ux -= vx >> m; + vx += ux >> m; + uy -= vy >> m; + vy += uy >> m; + } + +#undef PIV2 +#undef TWOPI +#undef HALF +} + + +/*! + \overload + + Sets the points of the array to those describing an arc of an + ellipse with width \a w and height \a h and position (\a x, \a y), + starting from angle \a a1, and spanning angle by \a a2, and + transformed by the matrix \a xf. The resulting array has + sufficient resolution for pixel accuracy. + + Angles are specified in 16ths of a degree, i.e. a full circle + equals 5760 (16*360). Positive values mean counter-clockwise, + whereas negative values mean the clockwise direction. Zero degrees + is at the 3 o'clock position. + + See the \link qcanvasellipse.html#anglediagram angle diagram\endlink. +*/ +void TQPointArray::makeArc( int x, int y, int w, int h, + int a1, int a2, + const TQWMatrix& xf ) +{ +#define PIV2 102944 /* fixed point PI/2 */ + if ( --w < 0 || --h < 0 || !a2 ) { + resize( 0 ); + return; + } + + bool rev = a2 < 0; + if ( rev ) { + a1 += a2; + a2 = -a2; + } + a1 = fix_angle( a1 ); + if ( a1 < 0 ) + a1 += 16*360; + a2 = fix_angle( a2 ); + + bool arc = a1 != 0 || a2 != 360*16 || rev; + + double xP, yP, xQ, yQ, xK, yK; + + xf.map(x+w, y+h/2.0, &xP, &yP); + xf.map(x+w/2.0, y, &xQ, &yQ); + xf.map(x+w, y, &xK, &yK); + + int m = 3; + int max; + int q = int(TQMAX(TQABS(xP-xQ),TQABS(yP-yQ))); + if ( arc ) + q *= 2; + do { + m++; + max = 4*(1 + (PIV2 >> (16 - m)) ); + } while (max < q && m < 16); // 16 limits memory usage on HUGE arcs + + double inc = 1.0/(1<> (16 - m)); + resize(qn*4); + + qtr_elips(*this, 0, xP, yP, xQ, yQ, xK, yK, m); + xP = xQ; yP = yQ; + xf.map(x, y+h/2.0, &xQ, &yQ); + xf.map(x, y, &xK, &yK); + qtr_elips(*this, qn, xP, yP, xQ, yQ, xK, yK, m); + xP = xQ; yP = yQ; + xf.map(x+w/2.0, y+h, &xQ, &yQ); + xf.map(x, y+h, &xK, &yK); + qtr_elips(*this, qn*2, xP, yP, xQ, yQ, xK, yK, m); + xP = xQ; yP = yQ; + xf.map(x+w, y+h/2.0, &xQ, &yQ); + xf.map(x+w, y+h, &xK, &yK); + qtr_elips(*this, qn*3, xP, yP, xQ, yQ, xK, yK, m); + + int n = size(); + + if ( arc ) { + double da1 = double(a1)*Q_PI / (360*8); + double da3 = double(a2+a1)*Q_PI / (360*8); + int i = int(da1/inc+0.5); + int l = int(da3/inc+0.5); + int k = (l-i)+1; + TQPointArray r(k); + int j = 0; + + if ( rev ) { + while ( k-- ) + r[j++] = at((i+k)%n); + } else { + while ( j < k ) { + r[j] = at((i+j)%n); + j++; + } + } + *this = r; + } +#undef PIV2 +} + +#endif // QT_NO_TRANSFORMATIONS + +/*! + Sets the points of the array to those describing an ellipse with + size, width \a w by height \a h, and position (\a x, \a y). + + The returned array has sufficient resolution for use as pixels. +*/ +void TQPointArray::makeEllipse( int x, int y, int w, int h ) +{ // midpoint, 1/4 ellipse +#if !defined(QT_OLD_MAKEELLIPSE) && !defined(QT_NO_TRANSFORMATIONS) + TQWMatrix unit; + makeArc(x,y,w,h,0,360*16,unit); + return; +#else + if ( w <= 0 || h <= 0 ) { + if ( w == 0 || h == 0 ) { + resize( 0 ); + return; + } + if ( w < 0 ) { // negative width + w = -w; + x -= w; + } + if ( h < 0 ) { // negative height + h = -h; + y -= h; + } + } + int s = (w+h+2)/2; // max size of xx,yy array + int *px = new int[s]; // 1/4th of ellipse + int *py = new int[s]; + int xx, yy, i=0; + double d1, d2; + double a2=(w/2)*(w/2), b2=(h/2)*(h/2); + xx = 0; + yy = int(h/2); + d1 = b2 - a2*(h/2) + 0.25*a2; + px[i] = xx; + py[i] = yy; + i++; + while ( a2*(yy-0.5) > b2*(xx+0.5) ) { // region 1 + if ( d1 < 0 ) { + d1 = d1 + b2*(3.0+2*xx); + xx++; + } else { + d1 = d1 + b2*(3.0+2*xx) + 2.0*a2*(1-yy); + xx++; + yy--; + } + px[i] = xx; + py[i] = yy; + i++; + } + d2 = b2*(xx+0.5)*(xx+0.5) + a2*(yy-1)*(yy-1) - a2*b2; + while ( yy > 0 ) { // region 2 + if ( d2 < 0 ) { + d2 = d2 + 2.0*b2*(xx+1) + a2*(3-2*yy); + xx++; + yy--; + } else { + d2 = d2 + a2*(3-2*yy); + yy--; + } + px[i] = xx; + py[i] = yy; + i++; + } + s = i; + resize( 4*s ); // make full point array + x += w/2; + y += h/2; + for ( i=0; i + * 1 if T is on the open ray ending at P: <--P + * 2 if T is on the closed interior along: P--Q + * 3 if T is on the open ray beginning at Q: Q--> + * + * Example: consider the line P = (3,2), Q = (17,7). A plot + * of the test points T(x,y) (with 0 mapped onto '.') yields: + * + * 8| . . . . . . . . . . . . . . . . . 3 3 + * Y 7| . . . . . . . . . . . . . . 2 2 Q 3 3 Q = 2 + * 6| . . . . . . . . . . . 2 2 2 2 2 . . . + * a 5| . . . . . . . . 2 2 2 2 2 2 . . . . . + * x 4| . . . . . 2 2 2 2 2 2 . . . . . . . . + * i 3| . . . 2 2 2 2 2 . . . . . . . . . . . + * s 2| 1 1 P 2 2 . . . . . . . . . . . . . . P = 2 + * 1| 1 1 . . . . . . . . . . . . . . . . . + * +-------------------------------------- + * 1 2 3 4 5 X-axis 10 15 19 + * + * Point-Line distance is normalized with the Infinity Norm + * avoiding square-root code and tightening the test vs the + * Manhattan Norm. All math is done on the field of integers. + * The latter replaces the initial ">= MAX(...)" test with + * "> (ABS(qx-px) + ABS(qy-py))" loosening both inequality + * and norm, yielding a broader target line for selection. + * The tightest test is employed here for best discrimination + * in merging collinear (to grid coordinates) vertex chains + * into a larger, spanning vectors within the Lemming editor. + */ + + // if all points are coincident, return condition 2 (on line) + if(q[0]==p[0] && q[1]==p[1] && q[0]==t[0] && q[1]==t[1]) { + return 2; + } + + if ( TQABS((q[1]-p[1])*(t[0]-p[0])-(t[1]-p[1])*(q[0]-p[0])) >= + (TQMAX(TQABS(q[0]-p[0]), TQABS(q[1]-p[1])))) return 0; + + if (((q[0] maxsize / 2 ) + { + // This never happens in practice. + + if ( accsize >= maxsize-4 ) + return; + // Running out of space - approximate by a line. + acc[accsize++] = ctrl[0]; + acc[accsize++] = ctrl[1]; + acc[accsize++] = ctrl[6]; + acc[accsize++] = ctrl[7]; + return; + } + + //intersects: + double l[8]; + double r[8]; + split( ctrl, l, r); + + // convert to integers for line condition check + int c0[2]; c0[0] = int(ctrl[0]); c0[1] = int(ctrl[1]); + int c1[2]; c1[0] = int(ctrl[2]); c1[1] = int(ctrl[3]); + int c2[2]; c2[0] = int(ctrl[4]); c2[1] = int(ctrl[5]); + int c3[2]; c3[0] = int(ctrl[6]); c3[1] = int(ctrl[7]); + + // #### Duplication needed? + if ( TQABS(c1[0]-c0[0]) <= 1 && TQABS(c1[1]-c0[1]) <= 1 + && TQABS(c2[0]-c0[0]) <= 1 && TQABS(c2[1]-c0[1]) <= 1 + && TQABS(c3[0]-c1[0]) <= 1 && TQABS(c3[1]-c0[1]) <= 1 ) + { + // Approximate by one line. + // Dont need to write last pt as it is the same as first pt + // on the next segment + acc[accsize++] = l[0]; + acc[accsize++] = l[1]; + return; + } + + if ( ( pnt_on_line( c0, c3, c1 ) == 2 && pnt_on_line( c0, c3, c2 ) == 2 ) + || ( TQABS(c1[0]-c0[0]) <= 1 && TQABS(c1[1]-c0[1]) <= 1 + && TQABS(c2[0]-c0[0]) <= 1 && TQABS(c2[1]-c0[1]) <= 1 + && TQABS(c3[0]-c1[0]) <= 1 && TQABS(c3[1]-c0[1]) <= 1 ) ) + { + // Approximate by one line. + // Dont need to write last pt as it is the same as first pt + // on the next segment + acc[accsize++] = l[0]; + acc[accsize++] = l[1]; + return; + } + + // Too big and too curved - recusively subdivide. + polygonizeTQBezier( acc, accsize, l, maxsize ); + polygonizeTQBezier( acc, accsize, r, maxsize ); +} + +/*! + Returns the Bezier points for the four control points in this + array. +*/ + +TQPointArray TQPointArray::cubicBezier() const +{ +#ifdef USE_SIMPLE_QBEZIER_CODE + if ( size() != 4 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPointArray::bezier: The array must have 4 control points" ); +#endif + TQPointArray p; + return p; + } + + int v; + float xvec[4]; + float yvec[4]; + for ( v=0; v<4; v++ ) { // store all x,y in xvec,yvec + int x, y; + point( v, &x, &y ); + xvec[v] = (float)x; + yvec[v] = (float)y; + } + + TQRect r = boundingRect(); + int m = TQMAX(r.width(),r.height())/2; + m = TQMIN(m,30); // m = number of result points + if ( m < 2 ) // at least two points + m = 2; + TQPointArray p( m ); // p = Bezier point array + register TQPointData *pd = p.data(); + + float x0 = xvec[0], y0 = yvec[0]; + float dt = 1.0F/m; + float cx = 3.0F * (xvec[1] - x0); + float bx = 3.0F * (xvec[2] - xvec[1]) - cx; + float ax = xvec[3] - (x0 + cx + bx); + float cy = 3.0F * (yvec[1] - y0); + float by = 3.0F * (yvec[2] - yvec[1]) - cy; + float ay = yvec[3] - (y0 + cy + by); + float t = dt; + + pd->rx() = (TQCOORD)xvec[0]; + pd->ry() = (TQCOORD)yvec[0]; + pd++; + m -= 2; + + while ( m-- ) { + pd->rx() = (TQCOORD)qRound( ((ax * t + bx) * t + cx) * t + x0 ); + pd->ry() = (TQCOORD)qRound( ((ay * t + by) * t + cy) * t + y0 ); + pd++; + t += dt; + } + + pd->rx() = (TQCOORD)xvec[3]; + pd->ry() = (TQCOORD)yvec[3]; + + return p; +#else + + if ( size() != 4 ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQPointArray::bezier: The array must have 4 control points" ); +#endif + TQPointArray pa; + return pa; + } else { + TQRect r = boundingRect(); + int m = 4+2*TQMAX(r.width(),r.height()); + double *p = new double[m]; + double ctrl[8]; + int i; + for (i=0; i<4; i++) { + ctrl[i*2] = at(i).x(); + ctrl[i*2+1] = at(i).y(); + } + int len=0; + polygonizeTQBezier( p, len, ctrl, m ); + TQPointArray pa((len/2)+1); // one extra point for last point on line + int j=0; + for (i=0; j>( TQDataStream &s, TQPointArray &a ) +{ + register uint i; + uint len; + s >> len; // read size of array + if ( !a.resize( len ) ) // no memory + return s; + TQPoint p; + for ( i=0; i> p; + a.setPoint( i, p ); + } + return s; +} +#endif //QT_NO_DATASTREAM + + + +struct TQShortPoint { // Binary compatible with XPoint + short x, y; +}; + +uint TQPointArray::splen = 0; +void* TQPointArray::sp = 0; // Really a TQShortPoint* + +/*! + \internal + + Converts the point coords to short (16bit) size, compatible with + X11's XPoint structure. The pointer returned points to a static + array, so its contents will be overwritten the next time this + function is called. +*/ + +void* TQPointArray::shortPoints( int index, int nPoints ) const +{ + + if ( isNull() || !nPoints ) + return 0; + TQPoint* p = data(); + p += index; + uint i = nPoints < 0 ? size() : nPoints; + if ( splen < i ) { + if ( sp ) + delete[] ((TQShortPoint*)sp); + sp = new TQShortPoint[i]; + splen = i; + } + TQShortPoint* ps = (TQShortPoint*)sp; + while ( i-- ) { + ps->x = (short)p->x(); + ps->y = (short)p->y(); + p++; + ps++; + } + return sp; +} + + +/*! + \internal + + Deallocates the internal buffer used by shortPoints(). +*/ + +void TQPointArray::cleanBuffers() +{ + if ( sp ) + delete[] ((TQShortPoint*)sp); + sp = 0; + splen = 0; +} diff --git a/src/kernel/qpointarray.h b/src/kernel/qpointarray.h new file mode 100644 index 000000000..384d8b981 --- /dev/null +++ b/src/kernel/qpointarray.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Definition of TQPointArray class +** +** Created : 940213 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPOINTARRAY_H +#define TQPOINTARRAY_H + +#ifndef QT_H +#include "qmemarray.h" +#include "qpoint.h" +#endif // QT_H + + +#if defined(Q_TEMPLATEDLL) +//Q_TEMPLATE_EXTERN template class Q_EXPORT TQMemArray; +#endif + +class Q_EXPORT TQPointArray : public TQMemArray +{ +public: + TQPointArray() {} + ~TQPointArray() {} + TQPointArray( int size ) : TQMemArray( size ) {} + TQPointArray( const TQPointArray &a ) : TQMemArray( a ) {} + TQPointArray( const TQRect &r, bool closed=FALSE ); + TQPointArray( int nPoints, const TQCOORD *points ); + + TQPointArray &operator=( const TQPointArray &a ) + { return (TQPointArray&)assign( a ); } + + TQPointArray copy() const + { TQPointArray tmp; return *((TQPointArray*)&tmp.duplicate(*this)); } + + void translate( int dx, int dy ); + TQRect boundingRect() const; + + void point( uint i, int *x, int *y ) const; + TQPoint point( uint i ) const; + void setPoint( uint i, int x, int y ); + void setPoint( uint i, const TQPoint &p ); + bool setPoints( int nPoints, const TQCOORD *points ); + bool setPoints( int nPoints, int firstx, int firsty, ... ); + bool putPoints( int index, int nPoints, const TQCOORD *points ); + bool putPoints( int index, int nPoints, int firstx, int firsty, ... ); + bool putPoints( int index, int nPoints, + const TQPointArray & from, int fromIndex=0 ); + + void makeArc( int x, int y, int w, int h, int a1, int a2 ); + void makeEllipse( int x, int y, int w, int h ); + void makeArc( int x, int y, int w, int h, int a1, int a2, + const TQWMatrix& ); +#ifndef QT_NO_BEZIER + TQPointArray cubicBezier() const; +#endif + void* shortPoints( int index = 0, int nPoints = -1 ) const; + static void cleanBuffers(); + +protected: + static uint splen; + static void* sp; +}; + + +/***************************************************************************** + TQPointArray stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQPointArray & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQPointArray & ); +#endif + +/***************************************************************************** + Misc. TQPointArray functions + *****************************************************************************/ + +inline void TQPointArray::setPoint( uint i, const TQPoint &p ) +{ + setPoint( i, p.x(), p.y() ); +} + + +#endif // TQPOINTARRAY_H diff --git a/src/kernel/qpolygonscanner.cpp b/src/kernel/qpolygonscanner.cpp new file mode 100644 index 000000000..7db531b76 --- /dev/null +++ b/src/kernel/qpolygonscanner.cpp @@ -0,0 +1,937 @@ +/**************************************************************************** +** +** Implementation of TQPolygonScanner class +** +** Created : 000120 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qpolygonscanner.h" +#include "qpointarray.h" +#include + + +// Based on Xserver code miFillGeneralPoly... +/* + * + * Written by Brian Kelleher; Oct. 1985 + * + * Routine to fill a polygon. Two fill rules are + * supported: frWINDING and frEVENODD. + * + * See fillpoly.h for a complete description of the algorithm. + */ + +/* + * These are the data structures needed to scan + * convert regions. Two different scan conversion + * methods are available -- the even-odd method, and + * the winding number method. + * The even-odd rule states that a point is inside + * the polygon if a ray drawn from that point in any + * direction will pass through an odd number of + * path segments. + * By the winding number rule, a point is decided + * to be inside the polygon if a ray drawn from that + * point in any direction passes through a different + * number of clockwise and counterclockwise path + * segments. + * + * These data structures are adapted somewhat from + * the algorithm in (Foley/Van Dam) for scan converting + * polygons. + * The basic algorithm is to start at the top (smallest y) + * of the polygon, stepping down to the bottom of + * the polygon by incrementing the y coordinate. We + * keep a list of edges which the current scanline crosses, + * sorted by x. This list is called the Active Edge Table (AET) + * As we change the y-coordinate, we update each entry in + * in the active edge table to reflect the edges new xcoord. + * This list must be sorted at each scanline in case + * two edges intersect. + * We also keep a data structure known as the Edge Table (ET), + * which keeps track of all the edges which the current + * scanline has not yet reached. The ET is basically a + * list of ScanLineList structures containing a list of + * edges which are entered at a given scanline. There is one + * ScanLineList per scanline at which an edge is entered. + * When we enter a new edge, we move it from the ET to the AET. + * + * From the AET, we can implement the even-odd rule as in + * (Foley/Van Dam). + * The winding number rule is a little trickier. We also + * keep the EdgeTableEntries in the AET linked by the + * nextWETE (winding EdgeTableEntry) link. This allows + * the edges to be linked just as before for updating + * purposes, but only uses the edges linked by the nextWETE + * link as edges representing spans of the polygon to + * drawn (as with the even-odd rule). + */ + +/* $XConsortium: miscanfill.h,v 1.5 94/04/17 20:27:50 dpw Exp $ */ +/* + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall +not be used in advertising or otherwise to promote the sale, use or +other dealings in this Software without prior written authorization +from the X Consortium. + +*/ + + +/* + * scanfill.h + * + * Written by Brian Kelleher; Jan 1985 + * + * This file contains a few macros to help track + * the edge of a filled object. The object is assumed + * to be filled in scanline order, and thus the + * algorithm used is an extension of Bresenham's line + * drawing algorithm which assumes that y is always the + * major axis. + * Since these pieces of code are the same for any filled shape, + * it is more convenient to gather the library in one + * place, but since these pieces of code are also in + * the inner loops of output primitives, procedure call + * overhead is out of the question. + * See the author for a derivation if needed. + */ + +/* + * In scan converting polygons, we want to choose those pixels + * which are inside the polygon. Thus, we add .5 to the starting + * x coordinate for both left and right edges. Now we choose the + * first pixel which is inside the pgon for the left edge and the + * first pixel which is outside the pgon for the right edge. + * Draw the left pixel, but not the right. + * + * How to add .5 to the starting x coordinate: + * If the edge is moving to the right, then subtract dy from the + * error term from the general form of the algorithm. + * If the edge is moving to the left, then add dy to the error term. + * + * The reason for the difference between edges moving to the left + * and edges moving to the right is simple: If an edge is moving + * to the right, then we want the algorithm to flip immediately. + * If it is moving to the left, then we don't want it to flip until + * we traverse an entire pixel. + */ +#define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \ + int dx; /* local storage */ \ +\ + /* \ + * if the edge is horizontal, then it is ignored \ + * and assumed not to be processed. Otherwise, do this stuff. \ + */ \ + if ((dy) != 0) { \ + xStart = (x1); \ + dx = (x2) - xStart; \ + if (dx < 0) { \ + m = dx / (dy); \ + m1 = m - 1; \ + incr1 = -2 * dx + 2 * (dy) * m1; \ + incr2 = -2 * dx + 2 * (dy) * m; \ + d = 2 * m * (dy) - 2 * dx - 2 * (dy); \ + } else { \ + m = dx / (dy); \ + m1 = m + 1; \ + incr1 = 2 * dx - 2 * (dy) * m1; \ + incr2 = 2 * dx - 2 * (dy) * m; \ + d = -2 * m * (dy) + 2 * dx; \ + } \ + } \ +} + +#define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \ + if (m1 > 0) { \ + if (d > 0) { \ + minval += m1; \ + d += incr1; \ + } \ + else { \ + minval += m; \ + d += incr2; \ + } \ + } else {\ + if (d >= 0) { \ + minval += m1; \ + d += incr1; \ + } \ + else { \ + minval += m; \ + d += incr2; \ + } \ + } \ +} + + +/* + * This structure contains all of the information needed + * to run the bresenham algorithm. + * The variables may be hardcoded into the declarations + * instead of using this structure to make use of + * register declarations. + */ +typedef struct { + int minor; /* minor axis */ + int d; /* decision variable */ + int m, m1; /* slope and slope+1 */ + int incr1, incr2; /* error increments */ +} BRESINFO; + + +#define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \ + BRESINITPGON(dmaj, min1, min2, bres.minor, bres.d, \ + bres.m, bres.m1, bres.incr1, bres.incr2) + +#define BRESINCRPGONSTRUCT(bres) \ + BRESINCRPGON(bres.d, bres.minor, bres.m, bres.m1, bres.incr1, bres.incr2) + + +typedef struct _EdgeTableEntry { + int ymax; /* ycoord at which we exit this edge. */ + BRESINFO bres; /* Bresenham info to run the edge */ + struct _EdgeTableEntry *next; /* next in the list */ + struct _EdgeTableEntry *back; /* for insertion sort */ + struct _EdgeTableEntry *nextWETE; /* for winding num rule */ + int ClockWise; /* flag for winding number rule */ +} EdgeTableEntry; + + +typedef struct _ScanLineList{ + int scanline; /* the scanline represented */ + EdgeTableEntry *edgelist; /* header node */ + struct _ScanLineList *next; /* next in the list */ +} ScanLineList; + + +typedef struct { + int ymax; /* ymax for the polygon */ + int ymin; /* ymin for the polygon */ + ScanLineList scanlines; /* header node */ +} EdgeTable; + + +/* + * Here is a struct to help with storage allocation + * so we can allocate a big chunk at a time, and then take + * pieces from this heap when we need to. + */ +#define SLLSPERBLOCK 25 + +typedef struct _ScanLineListBlock { + ScanLineList SLLs[SLLSPERBLOCK]; + struct _ScanLineListBlock *next; +} ScanLineListBlock; + +/* + * number of points to buffer before sending them off + * to scanlines() : Must be an even number + */ +#define NUMPTSTOBUFFER 200 + +/* + * + * a few macros for the inner loops of the fill code where + * performance considerations don't allow a procedure call. + * + * Evaluate the given edge at the given scanline. + * If the edge has expired, then we leave it and fix up + * the active edge table; otherwise, we increment the + * x value to be ready for the next scanline. + * The winding number rule is in effect, so we must notify + * the caller when the edge has been removed so he + * can reorder the Winding Active Edge Table. + */ +#define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \ + if (pAET->ymax == y) { /* leaving this edge */ \ + pPrevAET->next = pAET->next; \ + pAET = pPrevAET->next; \ + fixWAET = 1; \ + if (pAET) \ + pAET->back = pPrevAET; \ + } \ + else { \ + BRESINCRPGONSTRUCT(pAET->bres); \ + pPrevAET = pAET; \ + pAET = pAET->next; \ + } \ +} + + +/* + * Evaluate the given edge at the given scanline. + * If the edge has expired, then we leave it and fix up + * the active edge table; otherwise, we increment the + * x value to be ready for the next scanline. + * The even-odd rule is in effect. + */ +#define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \ + if (pAET->ymax == y) { /* leaving this edge */ \ + pPrevAET->next = pAET->next; \ + pAET = pPrevAET->next; \ + if (pAET) \ + pAET->back = pPrevAET; \ + } \ + else { \ + BRESINCRPGONSTRUCT(pAET->bres) \ + pPrevAET = pAET; \ + pAET = pAET->next; \ + } \ +} + +/*********************************************************** + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987 by Digital Etquipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSETQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +******************************************************************/ + +#define MAXINT 0x7fffffff +#define MININT -MAXINT + +/* + * fillUtils.c + * + * Written by Brian Kelleher; Oct. 1985 + * + * This module contains all of the utility functions + * needed to scan convert a polygon. + * + */ +/* + * InsertEdgeInET + * + * Insert the given edge into the edge table. + * First we must find the correct bucket in the + * Edge table, then find the right slot in the + * bucket. Finally, we can insert it. + * + */ +static bool +miInsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE, + int scanline, ScanLineListBlock **SLLBlock, int *iSLLBlock) +{ + register EdgeTableEntry *start, *prev; + register ScanLineList *pSLL, *pPrevSLL; + ScanLineListBlock *tmpSLLBlock; + + /* + * find the right bucket to put the edge into + */ + pPrevSLL = &ET->scanlines; + pSLL = pPrevSLL->next; + while (pSLL && (pSLL->scanline < scanline)) + { + pPrevSLL = pSLL; + pSLL = pSLL->next; + } + + /* + * reassign pSLL (pointer to ScanLineList) if necessary + */ + if ((!pSLL) || (pSLL->scanline > scanline)) + { + if (*iSLLBlock > SLLSPERBLOCK-1) + { + tmpSLLBlock = + (ScanLineListBlock *)malloc(sizeof(ScanLineListBlock)); + if (!tmpSLLBlock) + return FALSE; + (*SLLBlock)->next = tmpSLLBlock; + tmpSLLBlock->next = 0; + *SLLBlock = tmpSLLBlock; + *iSLLBlock = 0; + } + pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]); + + pSLL->next = pPrevSLL->next; + pSLL->edgelist = 0; + pPrevSLL->next = pSLL; + } + pSLL->scanline = scanline; + + /* + * now insert the edge in the right bucket + */ + prev = 0; + start = pSLL->edgelist; + while (start && (start->bres.minor < ETE->bres.minor)) + { + prev = start; + start = start->next; + } + ETE->next = start; + + if (prev) + prev->next = ETE; + else + pSLL->edgelist = ETE; + return TRUE; +} + +/* + * CreateEdgeTable + * + * This routine creates the edge table for + * scan converting polygons. + * The Edge Table (ET) looks like: + * + * EdgeTable + * -------- + * | ymax | ScanLineLists + * |scanline|-->------------>-------------->... + * -------- |scanline| |scanline| + * |edgelist| |edgelist| + * --------- --------- + * | | + * | | + * V V + * list of ETEs list of ETEs + * + * where ETE is an EdgeTableEntry data structure, + * and there is one ScanLineList per scanline at + * which an edge is initially entered. + * + */ + +typedef struct { +#if defined(Q_OS_MAC) + int y, x; +#else + int x, y; +#endif + +} DDXPointRec, *DDXPointPtr; + +/* + * Clean up our act. + */ +static void +miFreeStorage(ScanLineListBlock *pSLLBlock) +{ + register ScanLineListBlock *tmpSLLBlock; + + while (pSLLBlock) + { + tmpSLLBlock = pSLLBlock->next; + free(pSLLBlock); + pSLLBlock = tmpSLLBlock; + } +} + +static bool +miCreateETandAET(int count, DDXPointPtr pts, EdgeTable *ET, + EdgeTableEntry *AET, EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock) +{ + register DDXPointPtr top, bottom; + register DDXPointPtr PrevPt, CurrPt; + int iSLLBlock = 0; + + int dy; + + if (count < 2) return TRUE; + + /* + * initialize the Active Edge Table + */ + AET->next = 0; + AET->back = 0; + AET->nextWETE = 0; + AET->bres.minor = MININT; + + /* + * initialize the Edge Table. + */ + ET->scanlines.next = 0; + ET->ymax = MININT; + ET->ymin = MAXINT; + pSLLBlock->next = 0; + + PrevPt = &pts[count-1]; + + /* + * for each vertex in the array of points. + * In this loop we are dealing with two vertices at + * a time -- these make up one edge of the polygon. + */ + while (count--) + { + CurrPt = pts++; + + /* + * find out which point is above and which is below. + */ + if (PrevPt->y > CurrPt->y) + { + bottom = PrevPt, top = CurrPt; + pETEs->ClockWise = 0; + } + else + { + bottom = CurrPt, top = PrevPt; + pETEs->ClockWise = 1; + } + + /* + * don't add horizontal edges to the Edge table. + */ + if (bottom->y != top->y) + { + pETEs->ymax = bottom->y-1; /* -1 so we don't get last scanline */ + + /* + * initialize integer edge algorithm + */ + dy = bottom->y - top->y; + BRESINITPGONSTRUCT(dy, top->x, bottom->x, pETEs->bres) + + if (!miInsertEdgeInET(ET, pETEs, top->y, &pSLLBlock, &iSLLBlock)) + { + miFreeStorage(pSLLBlock->next); + return FALSE; + } + + ET->ymax = TQMAX(ET->ymax, PrevPt->y); + ET->ymin = TQMIN(ET->ymin, PrevPt->y); + pETEs++; + } + + PrevPt = CurrPt; + } + return TRUE; +} + +/* + * loadAET + * + * This routine moves EdgeTableEntries from the + * EdgeTable into the Active Edge Table, + * leaving them sorted by smaller x coordinate. + * + */ + +static void +miloadAET(EdgeTableEntry *AET, EdgeTableEntry *ETEs) +{ + register EdgeTableEntry *pPrevAET; + register EdgeTableEntry *tmp; + + pPrevAET = AET; + AET = AET->next; + while (ETEs) + { + while (AET && (AET->bres.minor < ETEs->bres.minor)) + { + pPrevAET = AET; + AET = AET->next; + } + tmp = ETEs->next; + ETEs->next = AET; + if (AET) + AET->back = ETEs; + ETEs->back = pPrevAET; + pPrevAET->next = ETEs; + pPrevAET = ETEs; + + ETEs = tmp; + } +} + +/* + * computeWAET + * + * This routine links the AET by the + * nextWETE (winding EdgeTableEntry) link for + * use by the winding number rule. The final + * Active Edge Table (AET) might look something + * like: + * + * AET + * ---------- --------- --------- + * |ymax | |ymax | |ymax | + * | ... | |... | |... | + * |next |->|next |->|next |->... + * |nextWETE| |nextWETE| |nextWETE| + * --------- --------- ^-------- + * | | | + * V-------------------> V---> ... + * + */ +static void +micomputeWAET(EdgeTableEntry *AET) +{ + register EdgeTableEntry *pWETE; + register int inside = 1; + register int isInside = 0; + + AET->nextWETE = 0; + pWETE = AET; + AET = AET->next; + while (AET) + { + if (AET->ClockWise) + isInside++; + else + isInside--; + + if ((!inside && !isInside) || + ( inside && isInside)) + { + pWETE->nextWETE = AET; + pWETE = AET; + inside = !inside; + } + AET = AET->next; + } + pWETE->nextWETE = 0; +} + +/* + * InsertionSort + * + * Just a simple insertion sort using + * pointers and back pointers to sort the Active + * Edge Table. + * + */ + +static int +miInsertionSort(EdgeTableEntry *AET) +{ + register EdgeTableEntry *pETEchase; + register EdgeTableEntry *pETEinsert; + register EdgeTableEntry *pETEchaseBackTMP; + register int changed = 0; + + AET = AET->next; + while (AET) + { + pETEinsert = AET; + pETEchase = AET; + while (pETEchase->back->bres.minor > AET->bres.minor) + pETEchase = pETEchase->back; + + AET = AET->next; + if (pETEchase != pETEinsert) + { + pETEchaseBackTMP = pETEchase->back; + pETEinsert->back->next = AET; + if (AET) + AET->back = pETEinsert->back; + pETEinsert->next = pETEchase; + pETEchase->back->next = pETEinsert; + pETEchase->back = pETEinsert; + pETEinsert->back = pETEchaseBackTMP; + changed = 1; + } + } + return(changed); +} + +/*! + \overload +*/ +void TQPolygonScanner::scan(const TQPointArray& pa, bool winding, int index, int npoints) +{ + scan( pa, winding, index, npoints, TRUE ); +} + +/*! + \overload + + If \a stitchable is FALSE, the right and bottom edges of the + polygon are included. This causes adjacent polygons to overlap. +*/ +void TQPolygonScanner::scan(const TQPointArray& pa, bool winding, int index, int npoints, bool stitchable) +{ + scan( pa, winding, index, npoints, + stitchable ? Edge(Left+Top) : Edge(Left+Right+Top+Bottom) ); +} + +/*! + Calls processSpans() for all scanlines of the polygon defined by + \a npoints starting at \a index in \a pa. + + If \a winding is TRUE, the Winding algorithm rather than the + Odd-Even rule is used. + + The \a edges is any bitwise combination of: + \list + \i \c TQPolygonScanner::Left + \i \c TQPolygonScanner::Right + \i \c TQPolygonScanner::Top + \i \c TQPolygonScanner::Bottom + \endlist + \a edges determines which edges are included. + + \warning The edges feature does not work properly. + +*/ +void TQPolygonScanner::scan( const TQPointArray& pa, bool winding, int index, int npoints, Edge edges ) +{ + + + DDXPointPtr ptsIn = (DDXPointPtr)pa.data(); + ptsIn += index; + register EdgeTableEntry *pAET; /* the Active Edge Table */ + register int y; /* the current scanline */ + register int nPts = 0; /* number of pts in buffer */ + register EdgeTableEntry *pWETE; /* Winding Edge Table */ + register ScanLineList *pSLL; /* Current ScanLineList */ + register DDXPointPtr ptsOut; /* ptr to output buffers */ + int *width; + DDXPointRec FirstPoint[NUMPTSTOBUFFER]; /* the output buffers */ + int FirstWidth[NUMPTSTOBUFFER]; + EdgeTableEntry *pPrevAET; /* previous AET entry */ + EdgeTable ET; /* Edge Table header node */ + EdgeTableEntry AET; /* Active ET header node */ + EdgeTableEntry *pETEs; /* Edge Table Entries buff */ + ScanLineListBlock SLLBlock; /* header for ScanLineList */ + int fixWAET = 0; + int edge_l = (edges & Left) ? 1 : 0; + int edge_r = (edges & Right) ? 1 : 0; + int edge_t = 1; //#### (edges & Top) ? 1 : 0; + int edge_b = (edges & Bottom) ? 1 : 0; + + if (npoints == -1) + npoints = pa.size(); + + if (npoints < 3) + return; + + if(!(pETEs = (EdgeTableEntry *) + malloc(sizeof(EdgeTableEntry) * npoints))) + return; + ptsOut = FirstPoint; + width = FirstWidth; + if (!miCreateETandAET(npoints, ptsIn, &ET, &AET, pETEs, &SLLBlock)) + { + free(pETEs); + return; + } + pSLL = ET.scanlines.next; + + if (!winding) + { + /* + * for each scanline + */ + for (y = ET.ymin+1-edge_t; y < ET.ymax+edge_b; y++) + { + /* + * Add a new edge to the active edge table when we + * get to the next edge. + */ + if (pSLL && y == pSLL->scanline) + { + miloadAET(&AET, pSLL->edgelist); + pSLL = pSLL->next; + } + pPrevAET = &AET; + pAET = AET.next; + + /* + * for each active edge + */ + while (pAET) + { + ptsOut->x = pAET->bres.minor + 1 - edge_l; + ptsOut++->y = y; + *width++ = pAET->next->bres.minor - pAET->bres.minor + - 1 + edge_l + edge_r; + nPts++; + + /* + * send out the buffer when its full + */ + if (nPts == NUMPTSTOBUFFER) + { + processSpans( nPts, (TQPoint*)FirstPoint, FirstWidth ); + ptsOut = FirstPoint; + width = FirstWidth; + nPts = 0; + } + EVALUATEEDGEEVENODD(pAET, pPrevAET, y) + EVALUATEEDGEEVENODD(pAET, pPrevAET, y) + } + miInsertionSort(&AET); + } + } + else /* default to WindingNumber */ + { + /* + * for each scanline + */ + for (y = ET.ymin+1-edge_t; y < ET.ymax+edge_b; y++) + { + /* + * Add a new edge to the active edge table when we + * get to the next edge. + */ + if (pSLL && y == pSLL->scanline) + { + miloadAET(&AET, pSLL->edgelist); + micomputeWAET(&AET); + pSLL = pSLL->next; + } + pPrevAET = &AET; + pAET = AET.next; + pWETE = pAET; + + /* + * for each active edge + */ + while (pAET) + { + /* + * if the next edge in the active edge table is + * also the next edge in the winding active edge + * table. + */ + if (pWETE == pAET) + { + ptsOut->x = pAET->bres.minor + 1 - edge_l; + ptsOut++->y = y; + *width++ = pAET->nextWETE->bres.minor - pAET->bres.minor - 1 + edge_l + edge_r; + nPts++; + + /* + * send out the buffer + */ + if (nPts == NUMPTSTOBUFFER) + { + processSpans( nPts, (TQPoint*)FirstPoint, FirstWidth ); + ptsOut = FirstPoint; + width = FirstWidth; + nPts = 0; + } + + pWETE = pWETE->nextWETE; + while (pWETE != pAET) { + EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) + } + pWETE = pWETE->nextWETE; + } + EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) + } + + /* + * reevaluate the Winding active edge table if we + * just had to resort it or if we just exited an edge. + */ + if (miInsertionSort(&AET) || fixWAET) + { + micomputeWAET(&AET); + fixWAET = 0; + } + } + } + + /* + * Get any spans that we missed by buffering + */ + + + processSpans( nPts, (TQPoint*)FirstPoint, FirstWidth ); + free(pETEs); + miFreeStorage(SLLBlock.next); +} +/***** END OF X11-based CODE *****/ + + diff --git a/src/kernel/qpolygonscanner.h b/src/kernel/qpolygonscanner.h new file mode 100644 index 000000000..ba3acf600 --- /dev/null +++ b/src/kernel/qpolygonscanner.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Definition of TQPolygonScanner class +** +** Created : 000120 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPOLYGONSCANNER_H +#define TQPOLYGONSCANNER_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + +class TQPointArray; +class TQPoint; + +class Q_EXPORT TQPolygonScanner { +public: + // BIC: fix for 3.0 + void scan( const TQPointArray& pa, bool winding, int index=0, int npoints=-1 ); + void scan( const TQPointArray& pa, bool winding, int index, int npoints, bool stitchable ); + enum Edge { Left=1, Right=2, Top=4, Bottom=8 }; + void scan( const TQPointArray& pa, bool winding, int index, int npoints, Edge edges ); + virtual void processSpans( int n, TQPoint* point, int* width )=0; +}; + +#endif // TQPOLYGONSCANNER_H diff --git a/src/kernel/qprinter.cpp b/src/kernel/qprinter.cpp new file mode 100644 index 000000000..1496351d4 --- /dev/null +++ b/src/kernel/qprinter.cpp @@ -0,0 +1,1025 @@ +/********************************************************************** +** +** Implementation of TQPrinter class +** +** Created : 941003 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qprinter.h" +#include "qprinter_p.h" + +#ifndef QT_NO_PRINTER + +/*! + \class TQPrinter qprinter.h + \brief The TQPrinter class is a paint device that paints on a printer. + + \ingroup images + \ingroup graphics + \mainclass + + On Windows it uses the built-in printer drivers. On X11 it + generates postscript and sends that to lpr, lp, or another print + command. + + TQPrinter is used in much the same way as TQWidget and TQPixmap are + used. The big difference is that you must keep track of the pages. + + TQPrinter supports a number of settable parameters, most of which + can be changed by the end user when the application calls + TQPrinter::setup(). + + The most important parameters are: + \list + \i setOrientation() tells TQPrinter which page orientation to use (virtual). + \i setPageSize() tells TQPrinter what page size to expect from the + printer. + \i setResolution() tells TQPrinter what resolution you wish the + printer to provide (in dpi). + \i setFullPage() tells TQPrinter whether you want to deal with the + full page or just with the part the printer can draw on. The + default is FALSE, so that by default you should be able to paint + on (0,0). If TRUE the origin of the coordinate system will be in + the top left corner of the paper and most probably the printer + will not be able to paint something there due to it's physical + margins. + \i setNumCopies() tells TQPrinter how many copies of the document + it should print. + \i setMinMax() tells TQPrinter and TQPrintDialog what the allowed + range for fromPage() and toPage() are. + \endlist + + Except where noted, you can only call the set functions before + setup(), or between TQPainter::end() and setup(). (Some may take + effect between setup() and begin(), or between begin() and end(), + but that's strictly undocumented and such behaviour may differ + depending on platform.) + + There are also some settings that the user sets (through the + printer dialog) and that applications are expected to obey: + + \list + + \i pageOrder() tells the application program whether to print + first-page-first or last-page-first. + + \i colorMode() tells the application program whether to print in + color or grayscale. (If you print in color and the printer does + not support color, TQt will try to approximate. The document may + take longer to print, but the quality should not be made visibly + poorer.) + + \i fromPage() and toPage() indicate what pages the application + program should print. + + \i paperSource() tells the application progam which paper source + to print from. + + \endlist + + You can of course call these functions to establish defaults + before you ask the user through TQPrinter::setup(). + + Once you start printing, calling newPage() is essential. You will + probably also need to look at the TQPaintDeviceMetrics for the + printer (see the \link simple-application.html#printersimple print + function\endlink in the Application walk-through). In previous versions, + paint device metrics were valid only after the TQPrinter has been set + up, i.e. after setup() has returned successfully. This is no longer + the case and paint device metrics can be requested safely before set up. + + If you want to abort the print job, abort() will try its best to + stop printing. It may cancel the entire job or just some of it. + + \omit Need a function to setup() without a dialog (i.e. use defaults). + \endomit + + The TrueType font embedding for TQt's postscript driver uses code + by David Chappell of Trinity College Computing Center. + + \legalese + + Copyright 1995, Trinity College Computing Center. + Written by David Chappell. + + Permission to use, copy, modify, and distribute this software and + its documentation for any purpose and without fee is hereby + granted, provided that the above copyright notice appear in all + copies and that both that copyright notice and this permission + notice appear in supporting documentation. This software is + provided "as is" without express or implied warranty. + + TrueType font support. These functions allow PPR to generate + PostScript fonts from Microsoft compatible TrueType font files. + + The functions in this file do most of the work to convert a + TrueType font to a type 3 PostScript font. + + Most of the material in this file is derived from a program called + "ttf2ps" which L. S. Ng posted to the usenet news group + "comp.sources.postscript". The author did not provide a copyright + notice or indicate any restrictions on use. + + Last revised 11 July 1995. + +*/ + +/*! + \enum TQPrinter::PrinterMode + + This enum describes the mode the printer should work in. It + basically presets a certain resolution and working mode. + + \value ScreenResolution Sets the resolution of the print device to + the screen resolution. This has the big advantage that the results + obtained when painting on the printer will match more or less + exactly the visible output on the screen. It is the easiest to + use, as font metrics on the screen and on the printer are the + same. This is the default value. ScreenResolution will produce a + lower quality output than HighResolution and should only be used + for drafts. + + \value PrinterResolution Use the physical resolution of the + printer on Windows. On Unix, set the postscript resolution to 72 + dpi. + + \value HighResolution Use printer resolution on windows, set the + resolution of the postscript driver to 600dpi. + + \value Compatible Almost the same as PrinterResolution, but keeps + some peculiarities of the TQt 2.x printer driver. This is useful + for applications ported from TQt 2.x to TQt 3.x. +*/ + +/*! + \enum TQPrinter::Orientation + + This enum type (not to be confused with TQt::Orientation) is used + to specify each page's orientation. + + \value Portrait the page's height is greater than its width (the + default). + + \value Landscape the page's width is greater than its height. + + This type interacts with \l TQPrinter::PageSize and + TQPrinter::setFullPage() to determine the final size of the page + available to the application. +*/ + + +/*! + \enum TQPrinter::PageSize + + This enum type specifies what paper size TQPrinter should use. + TQPrinter does not check that the paper size is available; it just + uses this information, together with TQPrinter::Orientation and + TQPrinter::setFullPage(), to determine the printable area (see + TQPaintDeviceMetrics). + + The defined sizes (with setFullPage(TRUE)) are: + + \value A0 841 x 1189 mm This value is not supported on windows. + \value A1 594 x 841 mm This value is not supported on windows. + \value A2 420 x 594 mm + \value A3 297 x 420 mm + \value A4 210 x 297 mm, 8.26 x 11.7 inches + \value A5 148 x 210 mm + \value A6 105 x 148 mm + \value A7 74 x 105 mm + \value A8 52 x 74 mm + \value A9 37 x 52 mm + \value B0 1030 x 1456 mm + \value B1 728 x 1030 mm + \value B10 32 x 45 mm + \value B2 515 x 728 mm + \value B3 364 x 515 mm + \value B4 257 x 364 mm + \value B5 182 x 257 mm, 7.17 x 10.13 inches + \value B6 128 x 182 mm + \value B7 91 x 128 mm + \value B8 64 x 91 mm + \value B9 45 x 64 mm + \value C5E 163 x 229 mm + \value Comm10E 105 x 241 mm, US Common #10 Envelope + \value DLE 110 x 220 mm + \value Executive 7.5 x 10 inches, 191 x 254 mm + \value Folio 210 x 330 mm + \value Ledger 432 x 279 mm + \value Legal 8.5 x 14 inches, 216 x 356 mm + \value Letter 8.5 x 11 inches, 216 x 279 mm + \value Tabloid 279 x 432 mm + \value Custom + \value NPageSize (internal) + + With setFullPage(FALSE) (the default), the metrics will be a bit + smaller; how much depends on the printer in use. +*/ + + +/*! + \enum TQPrinter::PageOrder + + This enum type is used by TQPrinter to tell the application program + how to print. + + \value FirstPageFirst the lowest-numbered page should be printed + first. + + \value LastPageFirst the highest-numbered page should be printed + first. +*/ + +/*! + \enum TQPrinter::ColorMode + + This enum type is used to indicate whether TQPrinter should print + in color or not. + + \value Color print in color if available, otherwise in grayscale. + + \value GrayScale print in grayscale, even on color printers. + Might be a little faster than \c Color. This is the default. +*/ + +/*! + \enum TQPrinter::PaperSource + + This enum type specifies what paper source TQPrinter is to use. + TQPrinter does not check that the paper source is available; it + just uses this information to try and set the paper source. + Whether it will set the paper source depends on whether the + printer has that particular source. + + Note: this is currently only implemented for Windows. + + \value OnlyOne + \value Lower + \value Middle + \value Manual + \value Envelope + \value EnvelopeManual + \value Auto + \value Tractor + \value SmallFormat + \value LargeFormat + \value LargeCapacity + \value Cassette + \value FormSource +*/ + +/*! + \enum TQPrinter::PrintRange + + This enum is used to specify which print range the application + should use to print. + + \value AllPages All pages should be printed + \value Selection Only the selection should be printed. + \value PageRange From page, to page option. + + \sa setPrintRange(), printRange() +*/ + +/*! + \enum TQPrinter::PrinterOption + + This enum describes various printer options that appear in the + printer setup dialog. It is used to enable and disable these + options in the setup dialog. + + \value PrintToFile Describes if print to file should be enabled. + \value PrintSelection Describes if printing selections should be enabled. + \value PrintPageRange Describes if printing page ranges (from, to) should + be enabled + + \sa setOptionEnabled(), isOptionEnabled() +*/ + + +/*! + \fn TQString TQPrinter::printerName() const + + Returns the printer name. This value is initially set to the name + of the default printer. + + \sa setPrinterName() +*/ + +/*! + \fn bool TQPrinter::outputToFile() const + + Returns TRUE if the output should be written to a file, or FALSE + if the output should be sent directly to the printer. The default + setting is FALSE. + + This function is currently only supported under X11 and Mac OS X. + + \sa setOutputToFile(), setOutputFileName() +*/ + +/*! + Specifies whether the output should be written to a file or sent + directly to the printer. + + Will output to a file if \a enable is TRUE, or will output + directly to the printer if \a enable is FALSE. + + This function is currently only supported under X11 and Mac OS X. + + \sa outputToFile(), setOutputFileName() +*/ + +void TQPrinter::setOutputToFile( bool enable ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPrinter::setOutputToFile: Cannot do this during printing" ); +#endif + return; + } + output_file = enable; +} + + +/*! + \fn TQString TQPrinter::outputFileName() const + + Returns the name of the output file. There is no default file + name. + + \sa setOutputFileName(), setOutputToFile() +*/ + +/*! + Sets the name of the output file to \a fileName. + + Setting a null or empty name (0 or "") disables output to a file, + i.e. calls setOutputToFile(FALSE). Setting a non-empty name + enables output to a file, i.e. calls setOutputToFile(TRUE). + + This function is currently only supported under X11. + + \sa outputFileName(), setOutputToFile() +*/ + +void TQPrinter::setOutputFileName( const TQString &fileName ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning("TQPrinter::setOutputFileName: Cannot do this during printing"); +#endif + return; + } + output_filename = fileName; + output_file = !output_filename.isEmpty(); +} + + +/*! + \fn TQString TQPrinter::printProgram() const + + Returns the name of the program that sends the print output to the + printer. + + The default is to return a null string; meaning that TQPrinter will + try to be smart in a system-dependent way. On X11 only, you can + set it to something different to use a specific print program. + + On Windows, this function returns the name of the printer device + driver. + + \sa setPrintProgram() setPrinterSelectionOption() +*/ + +/*! + Sets the name of the program that should do the print job to \a + printProg. + + On X11, this function sets the program to call with the PostScript + output. On other platforms, it has no effect. + + \sa printProgram() +*/ + +void TQPrinter::setPrintProgram( const TQString &printProg ) +{ + print_prog = printProg; +} + + +/*! + \fn TQString TQPrinter::docName() const + + Returns the document name. + + \sa setDocName() +*/ + +/*! + Sets the document name to \a name. +*/ + +void TQPrinter::setDocName( const TQString &name ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPrinter::setDocName: Cannot do this during printing" ); +#endif + return; + } + doc_name = name; +} + + +/*! + \fn TQString TQPrinter::creator() const + + Returns the name of the application that created the document. + + \sa setCreator() +*/ + +/*! + Sets the name of the application that created the document to \a + creator. + + This function is only applicable to the X11 version of TQt. If no + creator name is specified, the creator will be set to "TQt" + followed by some version number. + + \sa creator() +*/ + +void TQPrinter::setCreator( const TQString &creator ) +{ + creator_name = creator; +} + + +/*! + \fn Orientation TQPrinter::orientation() const + + Returns the orientation setting. The default value is \c + TQPrinter::Portrait. + + \sa setOrientation() +*/ + +/*! + Sets the print orientation to \a orientation. + + The orientation can be either \c TQPrinter::Portrait or \c + TQPrinter::Landscape. + + The printer driver reads this setting and prints using the + specified orientation. On Windows this setting won't take effect + until the printer dialog is shown (using TQPrinter::setup()). + + Windows only! This option can be changed while printing and will + take effect from the next call to newPage() + + \sa orientation() +*/ + +void TQPrinter::setOrientation( Orientation orientation ) +{ + orient = orientation; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + + +/*! + \fn PageSize TQPrinter::pageSize() const + + Returns the printer page size. The default value is system-dependent. + + \sa setPageSize() +*/ + + +/*! + Sets the printer page size to \a newPageSize if that size is + supported. The result if undefined if \a newPageSize is not + supported. + + The default page size is system-dependent. + + This function is useful mostly for setting a default value that + the user can override in the print dialog when you call setup(). + + \sa pageSize() PageSize setFullPage() setResolution() +*/ + +void TQPrinter::setPageSize( PageSize newPageSize ) +{ + if ( newPageSize > NPageSize ) { +#if defined(QT_CHECK_STATE) + qWarning("TQPrinter::SetPageSize: illegal page size %d", newPageSize ); +#endif + return; + } + page_size = newPageSize; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + +/*! + Sets the page order to \a newPageOrder. + + The page order can be \c TQPrinter::FirstPageFirst or \c + TQPrinter::LastPageFirst. The application programmer is responsible + for reading the page order and printing accordingly. + + This function is useful mostly for setting a default value that + the user can override in the print dialog when you call setup(). + + \bug This value is not kept in sync with the Windows or Mac OS X printer + dialogs. +*/ + +void TQPrinter::setPageOrder( PageOrder newPageOrder ) +{ + page_order = newPageOrder; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + + +/*! + Returns the current page order. + + The default page order is \c FirstPageFirst. + + \bug This value is not kept in sync with the Windows or Mac OS X printer + dialogs. +*/ + +TQPrinter::PageOrder TQPrinter::pageOrder() const +{ + return page_order; +} + + +/*! + Sets the printer's color mode to \a newColorMode, which can be + either \c Color or \c GrayScale (the default). + + \sa colorMode() +*/ + +void TQPrinter::setColorMode( ColorMode newColorMode ) +{ + color_mode = newColorMode; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + + +/*! + Returns the current color mode. The default color mode is \c + Color. + + \sa setColorMode() +*/ + +TQPrinter::ColorMode TQPrinter::colorMode() const +{ + return color_mode; +} + + +/*! + \fn int TQPrinter::fromPage() const + + Returns the from-page setting. The default value is 0. + + If fromPage() and toPage() both return 0 this signifies 'print the + whole document'. + + The programmer is responsible for reading this setting and + printing accordingly. + + \sa setFromTo(), toPage() +*/ + +/*! + \fn int TQPrinter::toPage() const + + Returns the to-page setting. The default value is 0. + + If fromPage() and toPage() both return 0 this signifies 'print the + whole document'. + + The programmer is responsible for reading this setting and + printing accordingly. + + \sa setFromTo(), fromPage() +*/ + +/*! + Sets the from-page and to-page settings to \a fromPage and \a + toPage respectively. + + The from-page and to-page settings specify what pages to print. + + If fromPage() and toPage() both return 0 this signifies 'print the + whole document'. + + This function is useful mostly to set a default value that the + user can override in the print dialog when you call setup(). + + \sa fromPage(), toPage(), setMinMax(), setup() +*/ + +void TQPrinter::setFromTo( int fromPage, int toPage ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPrinter::setFromTo: Cannot do this during printing" ); +#endif + return; + } + from_pg = fromPage; + to_pg = toPage; +} + + +/*! + \fn int TQPrinter::minPage() const + + Returns the min-page setting, i.e. the lowest page number a user + is allowed to choose. The default value is 0. + + \sa maxPage(), setMinMax() setFromTo() +*/ + +/*! + \fn int TQPrinter::maxPage() const + + Returns the max-page setting. A user can't choose a higher page + number than maxPage() when they select a print range. The default + value is 0. + + \sa minPage(), setMinMax() setFromTo() +*/ + +/*! + Sets the min-page and max-page settings to \a minPage and \a + maxPage respectively. + + The min-page and max-page restrict the from-page and to-page + settings. When the printer setup dialog appears, the user cannot + select a from page or a to page that are outside the range + specified by min and max pages. + + \sa minPage(), maxPage(), setFromTo(), setup() +*/ + +void TQPrinter::setMinMax( int minPage, int maxPage ) +{ + min_pg = minPage; + max_pg = maxPage; + if ( from_pg == 0 || from_pg < minPage ) + from_pg = minPage; + if ( to_pg == 0 || to_pg > maxPage ) + to_pg = maxPage; +} + + +/*! + \fn int TQPrinter::numCopies() const + + Returns the number of copies to be printed. The default value is 1. + + This value will return the number of times the application is + retquired to print in order to match the number specified in the + printer setup dialog. This has been done since some printer + drivers are not capable of buffering up the copies and the + application in those cases have to make an explicit call to the + print code for each copy. + + \sa setNumCopies() +*/ + +/*! + \fn bool TQPrinter::collateCopiesEnabled() const + + \internal + + Returns TRUE if the application should provide the user with the + option of choosing a collated printout; otherwise returns FALSE. + + Collation means that each page is printed in order, i.e. print the + first page, then the second page, then the third page and so on, and + then repeat this sequence for as many copies as have been requested. + If you don't collate you get several copies of the first page, then + several copies of the second page, then several copies of the third + page, and so on. + + \sa setCollateCopiesEnabled() setCollateCopies() collateCopies() +*/ + +/*! + \fn void TQPrinter::setCollateCopiesEnabled(bool enable) + + \internal + + If \a enable is TRUE (the default) the user is given the choice of + whether to print out multiple copies collated in the print dialog. + If \a enable is FALSE, then collateCopies() will be ignored. + + Collation means that each page is printed in order, i.e. print the + first page, then the second page, then the third page and so on, and + then repeat this sequence for as many copies as have been requested. + If you don't collate you get several copies of the first page, then + several copies of the second page, then several copies of the third + page, and so on. + + \sa collateCopiesEnabled() setCollateCopies() collateCopies() +*/ + +/*! + \fn bool TQPrinter::collateCopies() const + + \internal + + Returns TRUE if collation is turned on when multiple copies is selected. + Returns FALSE if it is turned off when multiple copies is selected. + + \sa collateCopiesEnabled() setCollateCopiesEnabled() setCollateCopies() +*/ + +/*! + \internal + + Sets the default value for collation checkbox when the print dialog appears. + If \a on is TRUE, it will enable setCollateCopiesEnabled(). + The default value is FALSE. This value will be changed by what the + user presses in the print dialog. + + \sa collateCopiesEnabled() setCollateCopiesEnabled() collateCopies() +*/ + +void TQPrinter::setCollateCopies(bool on) +{ + if (!collateCopiesEnabled() && on) + setCollateCopiesEnabled(on); + usercolcopies = on; +} + +/*! + Sets the number of copies to be printed to \a numCopies. + + The printer driver reads this setting and prints the specified + number of copies. + + \sa numCopies(), setup() +*/ + +void TQPrinter::setNumCopies( int numCopies ) +{ + ncopies = numCopies; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + + +/*! + Returns the printer options selection string. This is useful only + if the print command has been explicitly set. + + The default value (a null string) implies that the printer should + be selected in a system-dependent manner. + + Any other value implies that the given value should be used. + + \sa setPrinterSelectionOption() +*/ + +TQString TQPrinter::printerSelectionOption() const +{ + return option_string; +} + + +/*! + Sets the printer to use \a option to select the printer. \a option + is null by default (which implies that TQt should be smart enough + to guess correctly), but it can be set to other values to use a + specific printer selection option. + + If the printer selection option is changed while the printer is + active, the current print job may or may not be affected. + + \sa printerSelectionOption() +*/ + +void TQPrinter::setPrinterSelectionOption( const TQString & option ) +{ + option_string = option; +} + + +/*! + Sets TQPrinter to have the origin of the coordinate system at the + top-left corner of the paper if \a fp is TRUE, or where it thinks + the top-left corner of the printable area is if \a fp is FALSE. + + The default is FALSE. You can (probably) print on (0,0), and + TQPaintDeviceMetrics will report something smaller than the size + indicated by PageSize. (Note that TQPrinter may be wrong on Unix + systems - it does not have perfect knowledge of the physical + printer.) + + If you set \a fp to TRUE, TQPaintDeviceMetrics will report the + exact same size as indicated by \c PageSize, but you cannot print + on all of that - you must take care of the output margins + yourself. + + \sa PageSize setPageSize() TQPaintDeviceMetrics fullPage() +*/ + +void TQPrinter::setFullPage( bool fp ) +{ + to_edge = fp; +} + + +/*! + Returns TRUE if the origin of the printer's coordinate system is + at the corner of the sheet and FALSE if it is at the edge of the + printable area. + + See setFullPage() for details and caveats. + + \sa setFullPage() PageSize TQPaintDeviceMetrics +*/ + +bool TQPrinter::fullPage() const +{ + return to_edge; +} + + +/*! + Requests that the printer prints at \a dpi or as near to \a dpi as + possible. + + This setting affects the coordinate system as returned by, for + example, TQPaintDeviceMetrics and TQPainter::viewport(). + + The value depends on the \c PrintingMode used in the TQPrinter + constructor. By default, the dpi value of the screen is used. + + This function must be called before setup() to have an effect on + all platforms. + + \sa resolution() setPageSize() +*/ + +void TQPrinter::setResolution( int dpi ) +{ + res = dpi; + res_set = TRUE; +} + + +/*! + Returns the current assumed resolution of the printer, as set by + setResolution() or by the printer subsystem. + + \sa setResolution() +*/ + +int TQPrinter::resolution() const +{ + return res; +} + +/*! + Sets the paper source setting to \a source. + + Windows only! This option can be changed while printing and will + take effect from the next call to newPage() + + \sa paperSource() +*/ + +void TQPrinter::setPaperSource( PaperSource source ) +{ + paper_source = source; +#if defined(Q_WS_WIN) + reinit(); +#endif +} + +/*! + Returns the currently set paper source of the printer. + + \sa setPaperSource() +*/ + +TQPrinter::PaperSource TQPrinter::paperSource() const +{ + return paper_source; +} + +/*! + Sets the default selected page range to be used when the print setup + dialog is opened to \a range. If the PageRange specified by \a range is + currently disabled the function does nothing. + + \sa printRange() +*/ +void TQPrinter::setPrintRange( PrintRange range ) +{ + if( range != AllPages ) + if( range == Selection + && !isOptionEnabled( PrintSelection ) ) + setOptionEnabled( PrintSelection, TRUE ); + else if( range == PageRange + && !isOptionEnabled( PrintPageRange ) ) + setOptionEnabled( PrintPageRange, TRUE ); + d->printRange = range; +} + +/*! + Returns the PageRange of the TQPrinter. After the print setup dialog + has been opened, this function returns the value selected by the user. + + \sa setPrintRange() +*/ +TQPrinter::PrintRange TQPrinter::printRange() const +{ + return d->printRange; +} + +/*! + Enables the printer option with the identifier \a option if \a + enable is TRUE, and disables option \a option if \a enable is FALSE. + + \sa isOptionEnabled() +*/ +void TQPrinter::setOptionEnabled( PrinterOption option, bool enable ) +{ + if( enable ) { + d->printerOptions |= ( 1 << option ); + if( ( option == PrintPageRange ) && min_pg==0 && max_pg==0 ) + max_pg = 9999; + } else { + d->printerOptions &= ( ~( 1 << option ) ); + } +} + +/*! + Returns TRUE if the printer option with identifier \a option is enabled; + otherwise returns FALSE. + + \sa setOptionEnabled() + */ +bool TQPrinter::isOptionEnabled( PrinterOption option ) +{ + return d->printerOptions & ( 1 << option ); +} +#endif // QT_NO_PRINTER + diff --git a/src/kernel/qprinter.h b/src/kernel/qprinter.h new file mode 100644 index 000000000..96bb964de --- /dev/null +++ b/src/kernel/qprinter.h @@ -0,0 +1,284 @@ +/********************************************************************** +** +** Definition of TQPrinter class +** +** Created : 940927 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPRINTER_H +#define TQPRINTER_H + +#ifndef QT_H +#include "qpaintdevice.h" +#include "qstring.h" +#include "qstringlist.h" +#endif // QT_H + +#ifndef QT_NO_PRINTER + +#if defined(B0) +#undef B0 // Terminal hang-up. We assume that you do not want that. +#endif + +class TQPrinterPrivate; + +class Q_EXPORT TQPrinter : public TQPaintDevice +{ +public: + enum PrinterMode { ScreenResolution, PrinterResolution, HighResolution, Compatible }; + + TQPrinter( PrinterMode mode = ScreenResolution ); + ~TQPrinter(); + + enum Orientation { Portrait, Landscape }; + + enum PageSize { A4, B5, Letter, Legal, Executive, + A0, A1, A2, A3, A5, A6, A7, A8, A9, B0, B1, + B10, B2, B3, B4, B6, B7, B8, B9, C5E, Comm10E, + DLE, Folio, Ledger, Tabloid, Custom, NPageSize = Custom }; + + enum PageOrder { FirstPageFirst, LastPageFirst }; + + enum ColorMode { GrayScale, Color }; + + enum PaperSource { OnlyOne, Lower, Middle, Manual, Envelope, + EnvelopeManual, Auto, Tractor, SmallFormat, + LargeFormat, LargeCapacity, Cassette, FormSource }; + + enum PrintRange { AllPages, + Selection, + PageRange }; + + enum PrinterOption { PrintToFile, + PrintSelection, + PrintPageRange }; + + TQString printerName() const; + virtual void setPrinterName( const TQString &); + bool outputToFile() const; + virtual void setOutputToFile( bool ); + TQString outputFileName()const; + virtual void setOutputFileName( const TQString &); + + TQString printProgram() const; + virtual void setPrintProgram( const TQString &); + + TQString printerSelectionOption() const; + virtual void setPrinterSelectionOption( const TQString & ); + + TQString docName() const; + virtual void setDocName( const TQString &); + TQString creator() const; + virtual void setCreator( const TQString &); + + Orientation orientation() const; + virtual void setOrientation( Orientation ); + PageSize pageSize() const; + virtual void setPageSize( PageSize ); +#ifdef Q_WS_WIN + void setWinPageSize( short winPageSize ); + short winPageSize() const; +#endif +#ifdef Q_WS_MAC + bool printSetup(); + bool pageSetup(); +#endif + virtual void setPageOrder( PageOrder ); + PageOrder pageOrder() const; + + void setResolution( int ); + int resolution() const; + + virtual void setColorMode( ColorMode ); + ColorMode colorMode() const; + + virtual void setFullPage( bool ); + bool fullPage() const; + TQSize margins() const; + void setMargins( uint top, uint left, uint bottom, uint right ); + void margins( uint *top, uint *left, uint *bottom, uint *right ) const; + + int fromPage() const; + int toPage() const; + virtual void setFromTo( int fromPage, int toPage ); + int minPage() const; + int maxPage() const; + virtual void setMinMax( int minPage, int maxPage ); + int numCopies() const; + virtual void setNumCopies( int ); + + bool collateCopiesEnabled() const; + void setCollateCopiesEnabled(bool ); + + bool collateCopies() const; + void setCollateCopies( bool ); + + PrintRange printRange() const; + void setPrintRange( PrintRange range ); + + bool newPage(); + bool abort(); + bool aborted() const; + + bool setup( TQWidget *parent = 0 ); + + PaperSource paperSource() const; + virtual void setPaperSource( PaperSource ); + + void setOptionEnabled( PrinterOption, bool enable ); + bool isOptionEnabled( PrinterOption ); + +protected: + bool cmd( int, TQPainter *, TQPDevCmdParam * ); + int metric( int ) const; + +#if defined(Q_WS_WIN) + virtual void setActive(); + virtual void setIdle(); +#endif + +private: +#if defined(Q_WS_X11) || defined(Q_WS_QWS) + TQPaintDevice *pdrv; + int pid; +#endif +#if defined(Q_WS_MAC) + friend class TQPrinterPrivate; + PMPageFormat pformat; + PMPrintSettings psettings; + PMPrintSession psession; + bool prepare(PMPrintSettings *); + bool prepare(PMPageFormat *); + void interpret(PMPrintSettings *); + void interpret(PMPageFormat *); +#endif +#if defined(Q_WS_WIN) + void readPdlg( void* ); + void readPdlgA( void* ); + void writeDevmode( TQt::HANDLE ); + void writeDevmodeA( TQt::HANDLE ); + void reinit(); + + bool viewOffsetDone; + TQPainter* painter; + TQt::HANDLE hdevmode; + TQt::HANDLE hdevnames; +#endif + + int state; + TQString printer_name; + TQString option_string; + TQString output_filename; + bool output_file; + TQString print_prog; + TQString doc_name; + TQString creator_name; + + PageSize page_size; + PaperSource paper_source; + PageOrder page_order; + ColorMode color_mode; + Orientation orient; + uint to_edge : 1; + uint appcolcopies : 1; + uint usercolcopies : 1; + uint res_set : 1; + short from_pg, to_pg; + short min_pg, max_pg; + short ncopies; + int res; + TQPrinterPrivate *d; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQPrinter( const TQPrinter & ); + TQPrinter &operator=( const TQPrinter & ); +#endif +}; + + +inline TQString TQPrinter::printerName() const +{ return printer_name; } + +inline bool TQPrinter::outputToFile() const +{ return output_file; } + +inline TQString TQPrinter::outputFileName() const +{ return output_filename; } + +inline TQString TQPrinter::printProgram() const +{ return print_prog; } + +inline TQString TQPrinter::docName() const +{ return doc_name; } + +inline TQString TQPrinter::creator() const +{ return creator_name; } + +inline TQPrinter::PageSize TQPrinter::pageSize() const +{ return page_size; } + +inline TQPrinter::Orientation TQPrinter::orientation() const +{ return orient; } + +inline int TQPrinter::fromPage() const +{ return from_pg; } + +inline int TQPrinter::toPage() const +{ return to_pg; } + +inline int TQPrinter::minPage() const +{ return min_pg; } + +inline int TQPrinter::maxPage() const +{ return max_pg; } + +inline int TQPrinter::numCopies() const +{ return ncopies; } + +inline bool TQPrinter::collateCopiesEnabled() const +{ return appcolcopies; } + +inline void TQPrinter::setCollateCopiesEnabled(bool v) +{ appcolcopies = v; } + +inline bool TQPrinter::collateCopies() const +{ return usercolcopies; } + + +#endif // QT_NO_PRINTER + +#endif // TQPRINTER_H diff --git a/src/kernel/qprinter_p.h b/src/kernel/qprinter_p.h new file mode 100644 index 000000000..f5d7617ba --- /dev/null +++ b/src/kernel/qprinter_p.h @@ -0,0 +1,59 @@ +/********************************************************************** +** +** Definition of TQPrinter class +** +** Created : 940927 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPRINTER_P_H +#define TQPRINTER_P_H +#ifndef QT_NO_PRINTER + +#ifndef QT_H +#include +#include +#include +#endif // QT_H + +class TQPrinterPrivate +{ +public: + Q_UINT32 printerOptions; + TQPrinter::PrintRange printRange; +}; + +#endif +#endif diff --git a/src/kernel/qprinter_unix.cpp b/src/kernel/qprinter_unix.cpp new file mode 100644 index 000000000..89eb13ede --- /dev/null +++ b/src/kernel/qprinter_unix.cpp @@ -0,0 +1,672 @@ +/**************************************************************************** +** +** Implementation of TQPrinter class for Unix +** +** Created : 950810 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +static inline int qt_open(const char *pathname, int flags, mode_t mode) +{ return ::open(pathname, flags, mode); } +#if defined(open) +# undef open +#endif + +#include "qprinter.h" + +#ifndef QT_NO_PRINTER + +#include "qpaintdevicemetrics.h" +#include "qpsprinter_p.h" +#include "qprintdialog.h" +#include "qapplication.h" +#include "qprinter_p.h" + +#include // For ::sleep() +#include + + +// NOT REVISED + + +class TQPrinterUnixPrivate : public TQPrinterPrivate +{ +public: + bool marginsSpecified; + uint topMargin; + uint leftMargin; + uint bottomMargin; + uint rightMargin; +}; + +#define D ( (TQPrinterUnixPrivate*) d ) + +/***************************************************************************** + TQPrinter member functions + *****************************************************************************/ + +// TQPrinter states + +#define PST_IDLE 0 +#define PST_ACTIVE 1 +#define PST_ERROR 2 +#define PST_ABORTED 3 + +// Default values for TQPrinter members + +struct PrinterDefaults { + TQString printerName; + bool outputToFile; + TQString outputFileName; + TQPrinter::Orientation orientation; + TQPrinter::PageSize pageSize; + TQPrinter::PageOrder pageOrder; + TQPrinter::ColorMode colorMode; + int numCopies; +}; + +static PrinterDefaults * globalPrinterDefaults = 0; + +/*! + Constructs a printer paint device with mode \a m. + + \sa TQPrinter::PrinterMode +*/ + +TQPrinter::TQPrinter( PrinterMode m ) + : TQPaintDevice( TQInternal::Printer | TQInternal::ExternalDevice ) +{ + pdrv = 0; + pid = 0; + orient = Portrait; + page_size = A4; + page_order = FirstPageFirst; + color_mode = GrayScale; + ncopies = 1; + printer_name = getenv("PRINTER"); + from_pg = to_pg = min_pg = max_pg = 0; + state = PST_IDLE; + output_file = FALSE; + to_edge = FALSE; + paper_source = OnlyOne; + switch ( m ) { + case ScreenResolution: +#ifdef Q_WS_QWS + res = 72; +#else + res = TQPaintDevice::x11AppDpiY(); +#endif + break; + case Compatible: + case PrinterResolution: + res = 72; + break; + case HighResolution: + res = 600; + } + + d = new TQPrinterUnixPrivate; + D->marginsSpecified = FALSE; + d->printerOptions = 0; + setOptionEnabled( PrintToFile, TRUE ); + setOptionEnabled( PrintPageRange, TRUE ); + setPrintRange( AllPages ); +} + +/*! + Destroys the printer paint device and cleans up. +*/ + +TQPrinter::~TQPrinter() +{ + delete pdrv; + if ( pid ) { + (void)::kill( pid, 6 ); + (void)::wait( 0 ); + pid = 0; + } + delete d; +} + + +/*! + Advances to a new page on the printer. Returns TRUE if successful; + otherwise returns FALSE. +*/ + +bool TQPrinter::newPage() +{ + if ( state == PST_ACTIVE && pdrv ) + return ((TQPSPrinter*)pdrv)->cmd( TQPSPrinter::NewPage, 0, 0 ); + return FALSE; +} + + +/*! + Aborts the print job. Returns TRUE if successful; otherwise + returns FALSE. + + \sa aborted() +*/ + +bool TQPrinter::abort() +{ + if ( state == PST_ACTIVE && pdrv ) { + ((TQPSPrinter*)pdrv)->cmd( TQPSPrinter::AbortPrinting, 0, 0 ); + state = PST_ABORTED; + if ( pid ) { + (void)::kill( pid, 6 ); + (void)::wait( 0 ); + pid = 0; + } + } + return state == PST_ABORTED; +} + +/*! + Returns TRUE if the print job was aborted; otherwise returns + FALSE. + + \sa abort() +*/ + +bool TQPrinter::aborted() const +{ + return state == PST_ABORTED; +} + +/*! + Sets the printer name to \a name. + + The default printer will be used if no printer name is set. + + Under X11, the \c PRINTER environment variable defines the default + printer. Under any other window system, the window system defines + the default printer. + + \sa printerName() +*/ + +void TQPrinter::setPrinterName( const TQString &name ) +{ + if ( state != 0 ) { +#if defined(QT_CHECK_STATE) + qWarning( "TQPrinter::setPrinterName: Cannot do this during printing" ); +#endif + return; + } + printer_name = name; +} + +static void deleteGlobalPrinterDefaults() +{ + delete globalPrinterDefaults; + globalPrinterDefaults = 0; +} + +/*! + Opens a printer setup dialog, with parent \a parent, and asks the + user to specify which printer they wish to use and what settings + it should have. + + Returns TRUE if the user pressed "OK" to print, or FALSE if the + user canceled the operation. +*/ + +bool TQPrinter::setup( TQWidget * parent ) +{ +#ifndef QT_NO_PRINTDIALOG + bool result = TQPrintDialog::getPrinterSetup( this, parent ); +#else + bool result = FALSE; +#endif + if ( result ) { + if ( !globalPrinterDefaults ) { + globalPrinterDefaults = new PrinterDefaults; + qAddPostRoutine( deleteGlobalPrinterDefaults ); + } + globalPrinterDefaults->printerName = printerName(); + globalPrinterDefaults->outputToFile = outputToFile(); + globalPrinterDefaults->outputFileName = outputFileName(); + globalPrinterDefaults->orientation = orientation(); + globalPrinterDefaults->pageSize = pageSize(); + globalPrinterDefaults->pageOrder = pageOrder(); + globalPrinterDefaults->colorMode = colorMode(); + } + return result; +} + +static void closeAllOpenFds() +{ + // hack time... getting the maximum number of open + // files, if possible. if not we assume it's the + // larger of 256 and the fd we got + int i; +#if defined(Q_OS_OS2EMX) + LONG req_count = 0; + ULONG rc, handle_count; + rc = DosSetRelMaxFH (&req_count, &handle_count); + /* if (rc != NO_ERROR) ... */ + i = (int)handle_count; +#elif defined(_SC_OPEN_MAX) + i = (int)sysconf( _SC_OPEN_MAX ); +#elif defined(_POSIX_OPEN_MAX) + i = (int)_POSIX_OPEN_MAX; +#elif defined(OPEN_MAX) + i = (int)OPEN_MAX; +#else + i = TQMAX( 256, fds[0] ); +#endif // Q_OS_OS2EMX // ways-to-set i + while( --i > 0 ) + ::close( i ); +} + + + +static const char * const psToStr[TQPrinter::NPageSize+1] = +{ "A4", "B5", "Letter", "Legal", "Executive", + "A0", "A1", "A2", "A3", "A5", "A6", "A7", "A8", "A9", "B0", "B1", + "B10", "B2", "B3", "B4", "B6", "B7", "B8", "B9", "C5E", "Comm10E", + "DLE", "Folio", "Ledger", "Tabloid", 0 +}; + + +/*! + \internal + Handles painter commands to the printer. +*/ + +bool TQPrinter::cmd( int c, TQPainter *paint, TQPDevCmdParam *p ) +{ + if ( c == PdcBegin ) { + if ( state == PST_IDLE ) { + if ( output_file ) { + int fd = 0; + fd = qt_open( output_filename.local8Bit(), + O_CREAT | O_NOCTTY | O_TRUNC | O_WRONLY, + 0666 ); + if ( fd >= 0 ) { + pdrv = new TQPSPrinter( this, fd ); + state = PST_ACTIVE; + } + } else { + TQString pr; + if ( printer_name ) + pr = printer_name; + TQApplication::flushX(); + int fds[2]; + if ( pipe( fds ) != 0 ) { + qWarning( "TQPSPrinter: could not open pipe to print" ); + state = PST_ERROR; + return FALSE; + } + +// ### shouldn't we use TQProcess here???? +#if 0 && defined(Q_OS_OS2EMX) + // this code is still not used, and maybe it's not + // usable either, any more. if you want to use it, + // you may need to fix it first. + + // old comment: + + // this code is usable but not in use. spawn() is + // preferable to fork()/exec() for very large + // programs. if fork()/exec() is a problem and you + // use OS/2, remove '0 && ' from the #if. + int tmp; + tmp = dup(0); + dup2( fds[0], 0 ); + ::close( fds[0] ); + fcntl(tmp, F_SETFD, FD_CLOEXEC); + fcntl(fds[1], F_SETFD, FD_CLOEXEC); + if ( option_string ) + pr.prepend( option_string ); + else + pr.prepend( "-P" ); // ### + if ( spawnlp(P_NOWAIT,print_prog.data(), print_prog.data(), + pr.data(), output->name(), 0) == -1 ) { + ; // couldn't exec, ignored + } + dup2( tmp, 0 ); + ::close( tmp ); + pdrv = new TQPSPrinter( this, fds[1] ); + state = PST_ACTIVE; +#else + pid = fork(); + if ( pid == 0 ) { // child process + // if possible, exit tquickly, so the actual lp/lpr + // becomes a child of init, and ::waitpid() is + // guaranteed not to wait. + if ( fork() > 0 ) { + closeAllOpenFds(); + + // try to replace this process with "true" - this prevents + // global destructors from being called (that could possibly + // do wrong things to the parent process) + (void)execlp("true", "true", (char *)0); + (void)execl("/bin/true", "true", (char *)0); + (void)execl("/usr/bin/true", "true", (char *)0); + ::exit( 0 ); + } + dup2( fds[0], 0 ); + + closeAllOpenFds(); + + if ( print_prog ) { + if ( option_string ) + pr.prepend( option_string ); + else + pr.prepend( TQString::fromLatin1( "-P" ) ); + (void)execlp( print_prog.ascii(), print_prog.ascii(), + pr.ascii(), (char *)0 ); + } else { + // if no print program has been specified, be smart + // about the option string too. + TQStringList lprhack; + TQStringList lphack; + TQString media; + if ( pr || option_string ) { + if ( option_string ) { + lprhack = TQStringList::split(TQChar(' '), option_string); + lphack = lprhack; + } else { + lprhack.append( TQString::fromLatin1( "-P" ) ); + lphack.append( TQString::fromLatin1( "-d" ) ); + } + lprhack.append(pr); + lphack.append(pr); + } + char ** lpargs = new char *[lphack.size()+6]; + lpargs[0] = "lp"; + uint i; + for (i = 0; i < lphack.size(); ++i) + lpargs[i+1] = (char *)lphack[i].ascii(); +#ifndef Q_OS_OSF + if (psToStr[page_size]) { + lpargs[++i] = "-o"; + lpargs[++i] = (char *)psToStr[page_size]; + lpargs[++i] = "-o"; + media = "media="; + media += psToStr[page_size]; + lpargs[++i] = (char *)media.ascii(); + } +#endif + lpargs[++i] = 0; + char **lprargs = new char *[lprhack.size()+1]; + lprargs[0] = "lpr"; + for (uint x = 0; x < lprhack.size(); ++x) + lprargs[x+1] = (char *)lprhack[x].ascii(); + lprargs[lprhack.size() + 1] = 0; + (void)execvp( "lp", lpargs ); + (void)execvp( "lpr", lprargs ); + (void)execv( "/bin/lp", lpargs); + (void)execv( "/bin/lpr", lprargs); + (void)execv( "/usr/bin/lp", lpargs); + (void)execv( "/usr/bin/lpr", lprargs); + } + // if we couldn't exec anything, close the fd, + // wait for a second so the parent process (the + // child of the GUI process) has exited. then + // exit. + ::close( 0 ); + (void)::sleep( 1 ); + ::exit( 0 ); + } else { // parent process + ::close( fds[0] ); + pdrv = new TQPSPrinter( this, fds[1] ); + state = PST_ACTIVE; + } +#endif // else part of Q_OS_OS2EMX + } + if ( state == PST_ACTIVE && pdrv ) + return ((TQPSPrinter*)pdrv)->cmd( c, paint, p ); + } else { + // ignore it? I don't know + } + } else { + bool r = FALSE; + if ( state == PST_ACTIVE && pdrv ) { + r = ((TQPSPrinter*)pdrv)->cmd( c, paint, p ); + if ( c == PdcEnd ) { + state = PST_IDLE; + delete pdrv; + pdrv = 0; + if ( pid ) { + (void)::waitpid( pid, 0, 0 ); + pid = 0; + } + } + } else if ( state == PST_ABORTED && c == PdcEnd ) + state = PST_IDLE; + return r; + } + return TRUE; +} + + +#define MM(n) int((n * 720 + 127) / 254) +#define IN(n) int(n * 72) + +struct PaperSize { + int width, height; +}; + +static const PaperSize paperSizes[TQPrinter::NPageSize] = +{ + { MM(210), MM(297) }, // A4 + { MM(176), MM(250) }, // B5 + { IN(8.5), IN(11) }, // Letter + { IN(8.5), IN(14) }, // Legal + { IN(7.5), IN(10) }, // Executive + { MM(841), MM(1189) }, // A0 + { MM(594), MM(841) }, // A1 + { MM(420), MM(594) }, // A2 + { MM(297), MM(420) }, // A3 + { MM(148), MM(210) }, // A5 + { MM(105), MM(148) }, // A6 + { MM(74), MM(105)}, // A7 + { MM(52), MM(74) }, // A8 + { MM(37), MM(52) }, // A9 + { MM(1000), MM(1414) }, // B0 + { MM(707), MM(1000) }, // B1 + { MM(31), MM(44) }, // B10 + { MM(500), MM(707) }, // B2 + { MM(353), MM(500) }, // B3 + { MM(250), MM(353) }, // B4 + { MM(125), MM(176) }, // B6 + { MM(88), MM(125) }, // B7 + { MM(62), MM(88) }, // B8 + { MM(44), MM(62) }, // B9 + { MM(162), MM(229) }, // C5E + { IN(4.125), IN(9.5) }, // Comm10E + { MM(110), MM(220) }, // DLE + { IN(8.5), IN(13) }, // Folio + { IN(17), IN(11) }, // Ledger + { IN(11), IN(17) } // Tabloid +}; + +/*! + Internal implementation of the virtual TQPaintDevice::metric() function. + + Use the TQPaintDeviceMetrics class instead. + + \internal + Hard coded return values for PostScript under X. +*/ + +int TQPrinter::metric( int m ) const +{ + int val; + PageSize s = pageSize(); +#if defined(QT_CHECK_RANGE) + Q_ASSERT( (uint)s < (uint)NPageSize ); +#endif + switch ( m ) { + case TQPaintDeviceMetrics::PdmWidth: + val = orient == Portrait ? paperSizes[s].width : paperSizes[s].height; + if ( res != 72 ) + val = (val * res + 36) / 72; + if ( !fullPage() ) { + if ( D->marginsSpecified ) + val -= D->leftMargin + D->rightMargin; + else + val -= 2*margins().width(); + } + break; + case TQPaintDeviceMetrics::PdmHeight: + val = orient == Portrait ? paperSizes[s].height : paperSizes[s].width; + if ( res != 72 ) + val = (val * res + 36) / 72; + if ( !fullPage() ) { + if ( D->marginsSpecified ) + val -= D->topMargin + D->bottomMargin; + else + val -= 2*margins().height(); + } + break; + case TQPaintDeviceMetrics::PdmDpiX: + val = res; + break; + case TQPaintDeviceMetrics::PdmDpiY: + val = res; + break; + case TQPaintDeviceMetrics::PdmPhysicalDpiX: + case TQPaintDeviceMetrics::PdmPhysicalDpiY: + val = 72; + break; + case TQPaintDeviceMetrics::PdmWidthMM: + // double rounding error here. hooray. + val = metric( TQPaintDeviceMetrics::PdmWidth ); + val = (val * 254 + 5*res) / (10*res); + break; + case TQPaintDeviceMetrics::PdmHeightMM: + val = metric( TQPaintDeviceMetrics::PdmHeight ); + val = (val * 254 + 5*res) / (10*res); + break; + case TQPaintDeviceMetrics::PdmNumColors: + val = 16777216; + break; + case TQPaintDeviceMetrics::PdmDepth: + val = 24; + break; + default: + val = 0; +#if defined(QT_CHECK_RANGE) + qWarning( "TQPixmap::metric: Invalid metric command" ); +#endif + } + return val; +} + + +/*! + Returns the width of the left margin and the height of the top + margin of the printer. On Unix, this is a best-effort guess, not + based on perfect knowledge. + + If you have called setFullPage( TRUE ), margins().width() may be + treated as the smallest sane left margin you can use, and + margins().height() as the smallest sane top margin you can + use. + + If you have called setFullPage( FALSE ) (this is the default), + margins() is automatically subtracted from the pageSize() by + TQPrinter. + + \sa setFullPage() TQPaintDeviceMetrics PageSize +*/ +TQSize TQPrinter::margins() const +{ + if ( D->marginsSpecified ) + return TQSize( D->leftMargin, D->topMargin ); + + if (orient == Portrait) + return TQSize( res/2, res/3 ); + + return TQSize( res/3, res/2 ); +} + +/*! + \overload + + Sets \a top, \a left, \a bottom and \a right to the margins of the + printer. On Unix, this is a best-effort guess, not based on + perfect knowledge. + + If you have called setFullPage( TRUE ), the four values specify + the smallest sane margins you can use. + + If you have called setFullPage( FALSE ) (this is the default), + the margins are automatically subtracted from the pageSize() by + TQPrinter. + + \sa setFullPage() TQPaintDeviceMetrics PageSize +*/ +void TQPrinter::margins( uint *top, uint *left, uint *bottom, uint *right ) const +{ + if ( !D->marginsSpecified ) { + int x = orient == Portrait ? res/2 : res/3; + int y = orient == Portrait ? res/3 : res/2; + *top = *bottom = y; + *left = *right = x; + } else { + *top = D->topMargin; + *left = D->leftMargin; + *bottom = D->bottomMargin; + *right = D->rightMargin; + } +} + +/*! + Sets the printer margins to the sizes specified in \a top, \a left, + \a bottom and \a right. + + This function currently only has an effect on Unix systems. + + \sa margins() +*/ +void TQPrinter::setMargins( uint top, uint left, uint bottom, uint right ) +{ + D->topMargin = top; + D->leftMargin = left; + D->bottomMargin = bottom; + D->rightMargin = right; + D->marginsSpecified = TRUE; +} + +#endif diff --git a/src/kernel/qprocess.cpp b/src/kernel/qprocess.cpp new file mode 100644 index 000000000..c27757949 --- /dev/null +++ b/src/kernel/qprocess.cpp @@ -0,0 +1,806 @@ +/**************************************************************************** +** +** Implementation of TQProcess class +** +** Created : 20000905 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include +#include + +#include "qprocess.h" + +#ifndef QT_NO_PROCESS + +#include "qapplication.h" +#include "private/qinternal_p.h" + + +//#define QT_QPROCESS_DEBUG + + +/*! + \class TQProcess qprocess.h + + \brief The TQProcess class is used to start external programs and + to communicate with them. + + \ingroup io + \ingroup misc + \mainclass + + You can write to the started program's standard input, and can + read the program's standard output and standard error. You can + pass command line arguments to the program either in the + constructor or with setArguments() or addArgument(). The program's + working directory can be set with setWorkingDirectory(). If you + need to set up environment variables pass them to the start() or + launch() functions (see below). The processExited() signal is + emitted if the program exits. The program's exit status is + available from exitStatus(), although you could simply call + normalExit() to see if the program terminated normally. + + There are two different ways to start a process. If you just want + to run a program, optionally passing data to its standard input at + the beginning, use one of the launch() functions. If you want full + control of the program's standard input (especially if you don't + know all the data you want to send to standard input at the + beginning), use the start() function. + + If you use start() you can write to the program's standard input + using writeToStdin() and you can close the standard input with + closeStdin(). The wroteToStdin() signal is emitted if the data + sent to standard input has been written. You can read from the + program's standard output using readStdout() or readLineStdout(). + These functions return an empty TQByteArray if there is no data to + read. The readyReadStdout() signal is emitted when there is data + available to be read from standard output. Standard error has a + set of functions that correspond to the standard output functions, + i.e. readStderr(), readLineStderr() and readyReadStderr(). + + If you use one of the launch() functions the data you pass will be + sent to the program's standard input which will be closed once all + the data has been written. You should \e not use writeToStdin() or + closeStdin() if you use launch(). If you need to send data to the + program's standard input after it has started running use start() + instead of launch(). + + Both start() and launch() can accept a string list of strings each + of which has the format, key=value, where the keys are the names + of environment variables. + + You can test to see if a program is running with isRunning(). The + program's process identifier is available from + processIdentifier(). If you want to terminate a running program + use tryTerminate(), but note that the program may ignore this. If + you \e really want to terminate the program, without it having any + chance to clean up, you can use kill(). + + As an example, suppose we want to start the \c uic command (a TQt + command line tool used with \e{TQt Designer}) and perform some + operations on the output (the \c uic outputs the code it generates + to standard output by default). Suppose further that we want to + run the program on the file "small_dialog.ui" with the command + line options "-tr i18n". On the command line we would write: + \code + uic -tr i18n small_dialog.ui + \endcode + + \quotefile process/process.cpp + + A code snippet for this with the TQProcess class might look like + this: + + \skipto UicManager::UicManager() + \printline UicManager::UicManager() + \printline { + \skipto proc = new TQProcess( this ); + \printline proc = new TQProcess( this ); + \skipto proc->addArgument( "uic" ); + \printuntil this, SLOT(readFromStdout()) ); + \skipto if ( !proc->start() ) { + \printuntil // error handling + \skipto } + \printline } + \printline } + + \skipto void UicManager::readFromStdout() + \printuntil // Bear in mind that the data might be output in chunks. + \skipto } + \printline } + + Although you may need quotes for a file named on the command line + (e.g. if it contains spaces) you shouldn't use extra quotes for + arguments passed to addArgument() or setArguments(). + + The readyReadStdout() signal is emitted when there is new data on + standard output. This happens asynchronously: you don't know if + more data will arrive later. + + In the above example you could connect the processExited() signal + to the slot UicManager::readFromStdout() instead. If you do so, + you will be certain that all the data is available when the slot + is called. On the other hand, you must wait until the process has + finished before doing any processing. + + Note that if you are expecting a lot of output from the process, + you may hit platform-dependent limits to the pipe buffer size. The + solution is to make sure you connect to the output, e.g. the + readyReadStdout() and readyReadStderr() signals and read the data + as soon as it becomes available. + + Please note that TQProcess does not emulate a shell. This means that + TQProcess does not do any expansion of arguments: a '*' is passed as a '*' + to the program and is \e not replaced by all the files, a '$HOME' is also + passed literally and is \e not replaced by the environment variable HOME + and the special characters for IO redirection ('>', '|', etc.) are also + passed literally and do \e not have the special meaning as they have in a + shell. + + Also note that TQProcess does not emulate a terminal. This means that + certain programs which need direct terminal control, do not work as + expected with TQProcess. Such programs include console email programs (like + pine and mutt) but also programs which retquire the user to enter a password + (like su and ssh). + + \section1 Notes for Windows users + + Some Windows commands, for example, \c dir, are not provided by + separate applications, but by the command interpreter. + If you attempt to use TQProcess to execute these commands directly + it won't work. One possible solution is to execute the command + interpreter itself (\c cmd.exe on some Windows systems), and ask + the interpreter to execute the desired command. + + Under Windows there are certain problems starting 16-bit applications + and capturing their output. Microsoft recommends using an intermediate + application to start 16-bit applications. + + \sa TQSocket +*/ + +/*! + \enum TQProcess::Communication + + This enum type defines the communication channels connected to the + process. + + \value Stdin Data can be written to the process's standard input. + + \value Stdout Data can be read from the process's standard + output. + + \value Stderr Data can be read from the process's standard error. + + \value DupStderr Both the process's standard error output \e and + its standard output are written to its standard output. (Like + Unix's dup2().) This means that nothing is sent to the standard + error output. This is especially useful if your application + retquires that the output on standard output and on standard error + must be read in the same order that they are produced. This is a + flag, so to activate it you must pass \c{Stdout|Stderr|DupStderr}, + or \c{Stdin|Stdout|Stderr|DupStderr} if you want to provide input, + to the setCommunication() call. + + \sa setCommunication() communication() +*/ + +/*! + Constructs a TQProcess object. The \a parent and \a name parameters + are passed to the TQObject constructor. + + \sa setArguments() addArgument() start() +*/ +TQProcess::TQProcess( TQObject *parent, const char *name ) + : TQObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ), + wroteToStdinConnected( FALSE ), + readStdoutCalled( FALSE ), readStderrCalled( FALSE ), + comms( Stdin|Stdout|Stderr ) +{ + init(); +} + +/*! + Constructs a TQProcess with \a arg0 as the command to be executed. + The \a parent and \a name parameters are passed to the TQObject + constructor. + + The process is not started. You must call start() or launch() to + start the process. + + \sa setArguments() addArgument() start() +*/ +TQProcess::TQProcess( const TQString& arg0, TQObject *parent, const char *name ) + : TQObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ), + wroteToStdinConnected( FALSE ), + readStdoutCalled( FALSE ), readStderrCalled( FALSE ), + comms( Stdin|Stdout|Stderr ) +{ + init(); + addArgument( arg0 ); +} + +/*! + Constructs a TQProcess with \a args as the arguments of the + process. The first element in the list is the command to be + executed. The other elements in the list are the arguments to this + command. The \a parent and \a name parameters are passed to the + TQObject constructor. + + The process is not started. You must call start() or launch() to + start the process. + + \sa setArguments() addArgument() start() +*/ +TQProcess::TQProcess( const TQStringList& args, TQObject *parent, const char *name ) + : TQObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ), + wroteToStdinConnected( FALSE ), + readStdoutCalled( FALSE ), readStderrCalled( FALSE ), + comms( Stdin|Stdout|Stderr ) +{ + init(); + setArguments( args ); +} + + +/*! + Returns the list of arguments that are set for the process. + Arguments can be specified with the constructor or with the + functions setArguments() and addArgument(). + + Note that if you want to iterate over the list, you should iterate + over a copy, e.g. + \code + TQStringList list = myProcess.arguments(); + TQStringList::Iterator it = list.begin(); + while( it != list.end() ) { + myProcessing( *it ); + ++it; + } + \endcode + + \sa setArguments() addArgument() +*/ +TQStringList TQProcess::arguments() const +{ + return _arguments; +} + +/*! + Clears the list of arguments that are set for the process. + + \sa setArguments() addArgument() +*/ +void TQProcess::clearArguments() +{ + _arguments.clear(); +} + +/*! + Sets \a args as the arguments for the process. The first element + in the list is the command to be executed. The other elements in + the list are the arguments to the command. Any previous arguments + are deleted. + + TQProcess does not perform argument substitutions; for example, if you + specify "*" or "$DISPLAY", these values are passed to the process + literally. If you want to have the same behavior as the shell + provides, you must do the substitutions yourself; i.e. instead of + specifying a "*" you must specify the list of all the filenames in + the current directory, and instead of "$DISPLAY" you must specify + the value of the environment variable \c DISPLAY. + + Note for Windows users. The standard Windows shells, e.g. \c + command.com and \c cmd.exe, do not perform file globbing, i.e. + they do not convert a "*" on the command line into a list of files + in the current directory. For this reason most Windows + applications implement their own file globbing, and as a result of + this, specifying an argument of "*" for a Windows application is + likely to result in the application performing a file glob and + ending up with a list of filenames. + + \sa arguments() addArgument() +*/ +void TQProcess::setArguments( const TQStringList& args ) +{ + _arguments = args; +} + +/*! + Adds \a arg to the end of the list of arguments. + + The first element in the list of arguments is the command to be + executed; the following elements are the command's arguments. + + \sa arguments() setArguments() +*/ +void TQProcess::addArgument( const TQString& arg ) +{ + _arguments.append( arg ); +} + +#ifndef QT_NO_DIR +/*! + Returns the working directory that was set with + setWorkingDirectory(), or the current directory if none has been + explicitly set. + + \sa setWorkingDirectory() TQDir::current() +*/ +TQDir TQProcess::workingDirectory() const +{ + return workingDir; +} + +/*! + Sets \a dir as the working directory for processes. This does not + affect running processes; only processes that are started + afterwards are affected. + + Setting the working directory is especially useful for processes + that try to access files with relative paths. + + \sa workingDirectory() start() +*/ +void TQProcess::setWorkingDirectory( const TQDir& dir ) +{ + workingDir = dir; +} +#endif //QT_NO_DIR + +/*! + Returns the communication retquired with the process, i.e. some + combination of the \c Communication flags. + + \sa setCommunication() +*/ +int TQProcess::communication() const +{ + return comms; +} + +/*! + Sets \a commFlags as the communication retquired with the process. + + \a commFlags is a bitwise OR of the flags defined by the \c + Communication enum. + + The default is \c{Stdin|Stdout|Stderr}. + + \sa communication() +*/ +void TQProcess::setCommunication( int commFlags ) +{ + comms = commFlags; +} + +/*! + Returns TRUE if the process has exited normally; otherwise returns + FALSE. This implies that this function returns FALSE if the + process is still running. + + \sa isRunning() exitStatus() processExited() +*/ +bool TQProcess::normalExit() const +{ + // isRunning() has the side effect that it determines the exit status! + if ( isRunning() ) + return FALSE; + else + return exitNormal; +} + +/*! + Returns the exit status of the process or 0 if the process is + still running. This function returns immediately and does not wait + until the process is finished. + + If normalExit() is FALSE (e.g. if the program was killed or + crashed), this function returns 0, so you should check the return + value of normalExit() before relying on this value. + + \sa normalExit() processExited() +*/ +int TQProcess::exitStatus() const +{ + // isRunning() has the side effect that it determines the exit status! + if ( isRunning() ) + return 0; + else + return exitStat; +} + + +/*! + Reads the data that the process has written to standard output. + When new data is written to standard output, the class emits the + signal readyReadStdout(). + + If there is no data to read, this function returns a TQByteArray of + size 0: it does not wait until there is something to read. + + \sa readyReadStdout() readLineStdout() readStderr() writeToStdin() +*/ +TQByteArray TQProcess::readStdout() +{ + if ( readStdoutCalled ) { + return TQByteArray(); + } + readStdoutCalled = TRUE; + TQMembuf *buf = membufStdout(); + readStdoutCalled = FALSE; + + return buf->readAll(); +} + +/*! + Reads the data that the process has written to standard error. + When new data is written to standard error, the class emits the + signal readyReadStderr(). + + If there is no data to read, this function returns a TQByteArray of + size 0: it does not wait until there is something to read. + + \sa readyReadStderr() readLineStderr() readStdout() writeToStdin() +*/ +TQByteArray TQProcess::readStderr() +{ + if ( readStderrCalled ) { + return TQByteArray(); + } + readStderrCalled = TRUE; + TQMembuf *buf = membufStderr(); + readStderrCalled = FALSE; + + return buf->readAll(); +} + +/*! + Reads a line of text from standard output, excluding any trailing + newline or carriage return characters, and returns it. Returns + TQString::null if canReadLineStdout() returns FALSE. + + By default, the text is interpreted to be in Latin-1 encoding. If you need + other codecs, you can set a different codec with + TQTextCodec::setCodecForCStrings(). + + \sa canReadLineStdout() readyReadStdout() readStdout() readLineStderr() +*/ +TQString TQProcess::readLineStdout() +{ + TQByteArray a( 256 ); + TQMembuf *buf = membufStdout(); + if ( !buf->scanNewline( &a ) ) { + if ( !canReadLineStdout() ) + return TQString::null; + + if ( !buf->scanNewline( &a ) ) + return TQString( buf->readAll() ); + } + + uint size = a.size(); + buf->consumeBytes( size, 0 ); + + // get rid of terminating \n or \r\n + if ( size>0 && a.at( size - 1 ) == '\n' ) { + if ( size>1 && a.at( size - 2 ) == '\r' ) + a.at( size - 2 ) = '\0'; + else + a.at( size - 1 ) = '\0'; + } + return TQString( a ); +} + +/*! + Reads a line of text from standard error, excluding any trailing + newline or carriage return characters and returns it. Returns + TQString::null if canReadLineStderr() returns FALSE. + + By default, the text is interpreted to be in Latin-1 encoding. If you need + other codecs, you can set a different codec with + TQTextCodec::setCodecForCStrings(). + + \sa canReadLineStderr() readyReadStderr() readStderr() readLineStdout() +*/ +TQString TQProcess::readLineStderr() +{ + TQByteArray a( 256 ); + TQMembuf *buf = membufStderr(); + if ( !buf->scanNewline( &a ) ) { + if ( !canReadLineStderr() ) + return TQString::null; + + if ( !buf->scanNewline( &a ) ) + return TQString( buf->readAll() ); + } + + uint size = a.size(); + buf->consumeBytes( size, 0 ); + + // get rid of terminating \n or \r\n + if ( size>0 && a.at( size - 1 ) == '\n' ) { + if ( size>1 && a.at( size - 2 ) == '\r' ) + a.at( size - 2 ) = '\0'; + else + a.at( size - 1 ) = '\0'; + } + return TQString( a ); +} + +/*! + \fn void TQProcess::launchFinished() + + This signal is emitted when the process was started with launch(). + If the start was successful, this signal is emitted after all the + data has been written to standard input. If the start failed, then + this signal is emitted immediately. + + This signal is especially useful if you want to know when you can + safely delete the TQProcess object when you are not interested in + reading from standard output or standard error. + + \sa launch() TQObject::deleteLater() +*/ + +/*! + Runs the process and writes the data \a buf to the process's + standard input. If all the data is written to standard input, + standard input is closed. The command is searched for in the path + for executable programs; you can also use an absolute path in the + command itself. + + If \a env is null, then the process is started with the same + environment as the starting process. If \a env is non-null, then + the values in the string list are interpreted as environment + setttings of the form \c {key=value} and the process is started + with these environment settings. For convenience, there is a small + exception to this rule under Unix: if \a env does not contain any + settings for the environment variable \c LD_LIBRARY_PATH, then + this variable is inherited from the starting process. + + Returns TRUE if the process could be started; otherwise returns + FALSE. + + Note that you should not use the slots writeToStdin() and + closeStdin() on processes started with launch(), since the result + is not well-defined. If you need these slots, use start() instead. + + The process may or may not read the \a buf data sent to its + standard input. + + You can call this function even when a process that was started + with this instance is still running. Be aware that if you do this + the standard input of the process that was launched first will be + closed, with any pending data being deleted, and the process will + be left to run out of your control. Similarly, if the process + could not be started the standard input will be closed and the + pending data deleted. (On operating systems that have zombie + processes, TQt will also wait() on the old process.) + + The object emits the signal launchFinished() when this function + call is finished. If the start was successful, this signal is + emitted after all the data has been written to standard input. If + the start failed, then this signal is emitted immediately. + + \sa start() launchFinished(); +*/ +bool TQProcess::launch( const TQByteArray& buf, TQStringList *env ) +{ + if ( start( env ) ) { + if ( !buf.isEmpty() ) { + connect( this, SIGNAL(wroteToStdin()), + this, SLOT(closeStdinLaunch()) ); + writeToStdin( buf ); + } else { + closeStdin(); + emit launchFinished(); + } + return TRUE; + } else { + emit launchFinished(); + return FALSE; + } +} + +/*! + \overload + + The data \a buf is written to standard input with writeToStdin() + using the TQString::local8Bit() representation of the strings. +*/ +bool TQProcess::launch( const TQString& buf, TQStringList *env ) +{ + if ( start( env ) ) { + if ( !buf.isEmpty() ) { + connect( this, SIGNAL(wroteToStdin()), + this, SLOT(closeStdinLaunch()) ); + writeToStdin( buf ); + } else { + closeStdin(); + emit launchFinished(); + } + return TRUE; + } else { + emit launchFinished(); + return FALSE; + } +} + +/* + This private slot is used by the launch() functions to close standard input. +*/ +void TQProcess::closeStdinLaunch() +{ + disconnect( this, SIGNAL(wroteToStdin()), + this, SLOT(closeStdinLaunch()) ); + closeStdin(); + emit launchFinished(); +} + + +/*! + \fn void TQProcess::readyReadStdout() + + This signal is emitted when the process has written data to + standard output. You can read the data with readStdout(). + + Note that this signal is only emitted when there is new data and + not when there is old, but unread data. In the slot connected to + this signal, you should always read everything that is available + at that moment to make sure that you don't lose any data. + + \sa readStdout() readLineStdout() readyReadStderr() +*/ + +/*! + \fn void TQProcess::readyReadStderr() + + This signal is emitted when the process has written data to + standard error. You can read the data with readStderr(). + + Note that this signal is only emitted when there is new data and + not when there is old, but unread data. In the slot connected to + this signal, you should always read everything that is available + at that moment to make sure that you don't lose any data. + + \sa readStderr() readLineStderr() readyReadStdout() +*/ + +/*! + \fn void TQProcess::processExited() + + This signal is emitted when the process has exited. + + \sa isRunning() normalExit() exitStatus() start() launch() +*/ + +/*! + \fn void TQProcess::wroteToStdin() + + This signal is emitted if the data sent to standard input (via + writeToStdin()) was actually written to the process. This does not + imply that the process really read the data, since this class only + detects when it was able to write the data to the operating + system. But it is now safe to close standard input without losing + pending data. + + \sa writeToStdin() closeStdin() +*/ + + +/*! + \overload + + The string \a buf is handled as text using the + TQString::local8Bit() representation. +*/ +void TQProcess::writeToStdin( const TQString& buf ) +{ + TQByteArray tmp = buf.local8Bit(); + tmp.resize( qstrlen( tmp.data() ) ); + writeToStdin( tmp ); +} + + +/* + * Under Windows the implementation is not so nice: it is not that easy to + * detect when one of the signals should be emitted; therefore there are some + * timers that query the information. + * To keep it a little efficient, use the timers only when they are needed. + * They are needed, if you are interested in the signals. So use + * connectNotify() and disconnectNotify() to keep track of your interest. + */ +/*! \reimp +*/ +void TQProcess::connectNotify( const char * signal ) +{ +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::connectNotify(): signal %s has been connected", signal ); +#endif + if ( !ioRedirection ) + if ( qstrcmp( signal, SIGNAL(readyReadStdout()) )==0 || + qstrcmp( signal, SIGNAL(readyReadStderr()) )==0 + ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::connectNotify(): set ioRedirection to TRUE" ); +#endif + setIoRedirection( TRUE ); + return; + } + if ( !notifyOnExit && qstrcmp( signal, SIGNAL(processExited()) )==0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::connectNotify(): set notifyOnExit to TRUE" ); +#endif + setNotifyOnExit( TRUE ); + return; + } + if ( !wroteToStdinConnected && qstrcmp( signal, SIGNAL(wroteToStdin()) )==0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::connectNotify(): set wroteToStdinConnected to TRUE" ); +#endif + setWroteStdinConnected( TRUE ); + return; + } +} + +/*! \reimp +*/ +void TQProcess::disconnectNotify( const char * ) +{ + if ( ioRedirection && + receivers( SIGNAL(readyReadStdout()) ) ==0 && + receivers( SIGNAL(readyReadStderr()) ) ==0 + ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::disconnectNotify(): set ioRedirection to FALSE" ); +#endif + setIoRedirection( FALSE ); + } + if ( notifyOnExit && receivers( SIGNAL(processExited()) ) == 0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::disconnectNotify(): set notifyOnExit to FALSE" ); +#endif + setNotifyOnExit( FALSE ); + } + if ( wroteToStdinConnected && receivers( SIGNAL(wroteToStdin()) ) == 0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::disconnectNotify(): set wroteToStdinConnected to FALSE" ); +#endif + setWroteStdinConnected( FALSE ); + } +} + +#endif // QT_NO_PROCESS diff --git a/src/kernel/qprocess.h b/src/kernel/qprocess.h new file mode 100644 index 000000000..a447a4a6b --- /dev/null +++ b/src/kernel/qprocess.h @@ -0,0 +1,178 @@ +/**************************************************************************** +** +** Implementation of TQProcess class +** +** Created : 20000905 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPROCESS_H +#define TQPROCESS_H + +#ifndef QT_H +#include "qobject.h" +#include "qstringlist.h" +#include "qdir.h" +#endif // QT_H + +#ifndef QT_NO_PROCESS + +class TQProcessPrivate; +class TQMembuf; + + +class Q_EXPORT TQProcess : public TQObject +{ + Q_OBJECT +public: + TQProcess( TQObject *parent=0, const char *name=0 ); + TQProcess( const TQString& arg0, TQObject *parent=0, const char *name=0 ); + TQProcess( const TQStringList& args, TQObject *parent=0, const char *name=0 ); + ~TQProcess(); + + // set and get the arguments and working directory + TQStringList arguments() const; + void clearArguments(); + virtual void setArguments( const TQStringList& args ); + virtual void addArgument( const TQString& arg ); +#ifndef QT_NO_DIR + TQDir workingDirectory() const; + virtual void setWorkingDirectory( const TQDir& dir ); +#endif + + // set and get the comms wanted + enum Communication { Stdin=0x01, Stdout=0x02, Stderr=0x04, DupStderr=0x08 }; + void setCommunication( int c ); + int communication() const; + + // start the execution + virtual bool start( TQStringList *env=0 ); + virtual bool launch( const TQString& buf, TQStringList *env=0 ); + virtual bool launch( const TQByteArray& buf, TQStringList *env=0 ); + + // intquire the status + bool isRunning() const; + bool normalExit() const; + int exitStatus() const; + + // reading + virtual TQByteArray readStdout(); + virtual TQByteArray readStderr(); + bool canReadLineStdout() const; + bool canReadLineStderr() const; + virtual TQString readLineStdout(); + virtual TQString readLineStderr(); + + // get platform dependent process information +#if defined(Q_OS_WIN32) + typedef void* PID; +#else + typedef Q_LONG PID; +#endif + PID processIdentifier(); + + void flushStdin(); + +signals: + void readyReadStdout(); + void readyReadStderr(); + void processExited(); + void wroteToStdin(); + void launchFinished(); + +public slots: + // end the execution + void tryTerminate() const; + void kill() const; + + // input + virtual void writeToStdin( const TQByteArray& buf ); + virtual void writeToStdin( const TQString& buf ); + virtual void closeStdin(); + +protected: // ### or private? + void connectNotify( const char * signal ); + void disconnectNotify( const char * signal ); +private: + void setIoRedirection( bool value ); + void setNotifyOnExit( bool value ); + void setWroteStdinConnected( bool value ); + + void init(); + void reset(); +#if defined(Q_OS_WIN32) + uint readStddev( HANDLE dev, char *buf, uint bytes ); +#endif + TQMembuf* membufStdout(); + TQMembuf* membufStderr(); + +private slots: + void socketRead( int fd ); + void socketWrite( int fd ); + void timeout(); + void closeStdinLaunch(); + +private: + TQProcessPrivate *d; +#ifndef QT_NO_DIR + TQDir workingDir; +#endif + TQStringList _arguments; + + int exitStat; // exit status + bool exitNormal; // normal exit? + bool ioRedirection; // automatically set be (dis)connectNotify + bool notifyOnExit; // automatically set be (dis)connectNotify + bool wroteToStdinConnected; // automatically set be (dis)connectNotify + + bool readStdoutCalled; + bool readStderrCalled; + int comms; + + friend class TQProcessPrivate; +#if defined(Q_OS_UNIX) + friend class TQProcessManager; + friend class TQProc; +#endif + +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQProcess( const TQProcess & ); + TQProcess &operator=( const TQProcess & ); +#endif +}; + +#endif // QT_NO_PROCESS + +#endif // TQPROCESS_H diff --git a/src/kernel/qprocess_unix.cpp b/src/kernel/qprocess_unix.cpp new file mode 100644 index 000000000..b1842e80b --- /dev/null +++ b/src/kernel/qprocess_unix.cpp @@ -0,0 +1,1409 @@ +/**************************************************************************** +** +** Implementation of TQProcess class for Unix +** +** Created : 20000905 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qplatformdefs.h" + +// Solaris redefines connect -> __xnet_connect with _XOPEN_SOURCE_EXTENDED. +#if defined(connect) +#undef connect +#endif + +#include "qprocess.h" + +#ifndef QT_NO_PROCESS + +#include "qapplication.h" +#include "qptrqueue.h" +#include "qptrlist.h" +#include "qsocketnotifier.h" +#include "qtimer.h" +#include "qcleanuphandler.h" +#include "qregexp.h" +#include "private/qinternal_p.h" + +#include +#include +#include + +#ifdef __MIPSEL__ +# ifndef SOCK_DGRAM +# define SOCK_DGRAM 1 +# endif +# ifndef SOCK_STREAM +# define SOCK_STREAM 2 +# endif +#endif + +//#define QT_QPROCESS_DEBUG + + +#ifdef Q_C_CALLBACKS +extern "C" { +#endif // Q_C_CALLBACKS + + QT_SIGNAL_RETTYPE qt_C_sigchldHnd(QT_SIGNAL_ARGS); + +#ifdef Q_C_CALLBACKS +} +#endif // Q_C_CALLBACKS + + +class TQProc; +class TQProcessManager; +class TQProcessPrivate +{ +public: + TQProcessPrivate(); + ~TQProcessPrivate(); + + void closeOpenSocketsForChild(); + void newProc( pid_t pid, TQProcess *process ); + + TQMembuf bufStdout; + TQMembuf bufStderr; + + TQPtrQueue stdinBuf; + + TQSocketNotifier *notifierStdin; + TQSocketNotifier *notifierStdout; + TQSocketNotifier *notifierStderr; + + ssize_t stdinBufRead; + TQProc *proc; + + bool exitValuesCalculated; + bool socketReadCalled; + + static TQProcessManager *procManager; +}; + + +/*********************************************************************** + * + * TQProc + * + **********************************************************************/ +/* + The class TQProcess does not necessarily map exactly to the running + child processes: if the process is finished, the TQProcess class may still be + there; furthermore a user can use TQProcess to start more than one process. + + The helper-class TQProc has the semantics that one instance of this class maps + directly to a running child process. +*/ +class TQProc +{ +public: + TQProc( pid_t p, TQProcess *proc=0 ) : pid(p), process(proc) + { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProc: Constructor for pid %d and TQProcess %p", pid, process ); +#endif + socketStdin = 0; + socketStdout = 0; + socketStderr = 0; + } + ~TQProc() + { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProc: Destructor for pid %d and TQProcess %p", pid, process ); +#endif + if ( process ) { + if ( process->d->notifierStdin ) + process->d->notifierStdin->setEnabled( FALSE ); + if ( process->d->notifierStdout ) + process->d->notifierStdout->setEnabled( FALSE ); + if ( process->d->notifierStderr ) + process->d->notifierStderr->setEnabled( FALSE ); + process->d->proc = 0; + } + if( socketStdin ) + ::close( socketStdin ); + if( socketStdout ) + ::close( socketStdout ); + if( socketStderr ) + ::close( socketStderr ); + } + + pid_t pid; + int socketStdin; + int socketStdout; + int socketStderr; + TQProcess *process; +}; + +/*********************************************************************** + * + * TQProcessManager + * + **********************************************************************/ +class TQProcessManager : public TQObject +{ + Q_OBJECT + +public: + TQProcessManager(); + ~TQProcessManager(); + + void append( TQProc *p ); + void remove( TQProc *p ); + + void cleanup(); + +public slots: + void removeMe(); + void sigchldHnd( int ); + +public: + struct sigaction oldactChld; + struct sigaction oldactPipe; + TQPtrList *procList; + int sigchldFd[2]; + +private: + TQSocketNotifier *sn; +}; + +static void qprocess_cleanup() +{ + delete TQProcessPrivate::procManager; + TQProcessPrivate::procManager = 0; +} + +#ifdef Q_OS_QNX6 +#define BAILOUT close(tmpSocket);close(socketFD[1]);return -1; +int qnx6SocketPairReplacement (int socketFD[2]) { + int tmpSocket; + tmpSocket = socket (AF_INET, SOCK_STREAM, 0); + if (tmpSocket == -1) + return -1; + socketFD[1] = socket(AF_INET, SOCK_STREAM, 0); + if (socketFD[1] == -1) { BAILOUT }; + + sockaddr_in ipAddr; + memset(&ipAddr, 0, sizeof(ipAddr)); + ipAddr.sin_family = AF_INET; + ipAddr.sin_addr.s_addr = INADDR_ANY; + + int socketOptions = 1; + setsockopt(tmpSocket, SOL_SOCKET, SO_REUSEADDR, &socketOptions, sizeof(int)); + + bool found = FALSE; + for (int socketIP = 2000; (socketIP < 2500) && !(found); socketIP++) { + ipAddr.sin_port = htons(socketIP); + if (bind(tmpSocket, (struct sockaddr *)&ipAddr, sizeof(ipAddr))) + found = TRUE; + } + + if (listen(tmpSocket, 5)) { BAILOUT }; + + // Select non-blocking mode + int originalFlags = fcntl(socketFD[1], F_GETFL, 0); + fcntl(socketFD[1], F_SETFL, originalFlags | O_NONBLOCK); + + // Request connection + if (connect(socketFD[1], (struct sockaddr*)&ipAddr, sizeof(ipAddr))) + if (errno != EINPROGRESS) { BAILOUT }; + + // Accept connection + socketFD[0] = accept(tmpSocket, (struct sockaddr *)NULL, (size_t *)NULL); + if(socketFD[0] == -1) { BAILOUT }; + + // We're done + close(tmpSocket); + + // Restore original flags , ie return to blocking + fcntl(socketFD[1], F_SETFL, originalFlags); + return 0; +} +#undef BAILOUT +#endif + +TQProcessManager::TQProcessManager() : sn(0) +{ + procList = new TQPtrList; + procList->setAutoDelete( TRUE ); + + // The SIGCHLD handler writes to a socket to tell the manager that + // something happened. This is done to get the processing in sync with the + // event reporting. +#ifndef Q_OS_QNX6 + if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, sigchldFd ) ) { +#else + if ( qnx6SocketPairReplacement (sigchldFd) ) { +#endif + sigchldFd[0] = 0; + sigchldFd[1] = 0; + } else { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager: install socket notifier (%d)", sigchldFd[1] ); +#endif + sn = new TQSocketNotifier( sigchldFd[1], + TQSocketNotifier::Read, this ); + connect( sn, SIGNAL(activated(int)), + this, SLOT(sigchldHnd(int)) ); + sn->setEnabled( TRUE ); + } + + // install a SIGCHLD handler and ignore SIGPIPE + struct sigaction act; + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager: install a SIGCHLD handler" ); +#endif + act.sa_handler = qt_C_sigchldHnd; + sigemptyset( &(act.sa_mask) ); + sigaddset( &(act.sa_mask), SIGCHLD ); + act.sa_flags = SA_NOCLDSTOP; +#if defined(SA_RESTART) + act.sa_flags |= SA_RESTART; +#endif + if ( sigaction( SIGCHLD, &act, &oldactChld ) != 0 ) + qWarning( "Error installing SIGCHLD handler" ); + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager: install a SIGPIPE handler (SIG_IGN)" ); +#endif + act.sa_handler = QT_SIGNAL_IGNORE; + sigemptyset( &(act.sa_mask) ); + sigaddset( &(act.sa_mask), SIGPIPE ); + act.sa_flags = 0; + if ( sigaction( SIGPIPE, &act, &oldactPipe ) != 0 ) + qWarning( "Error installing SIGPIPE handler" ); +} + +TQProcessManager::~TQProcessManager() +{ + delete procList; + + if ( sigchldFd[0] != 0 ) + ::close( sigchldFd[0] ); + if ( sigchldFd[1] != 0 ) + ::close( sigchldFd[1] ); + + // restore SIGCHLD handler +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager: restore old sigchild handler" ); +#endif + if ( sigaction( SIGCHLD, &oldactChld, 0 ) != 0 ) + qWarning( "Error restoring SIGCHLD handler" ); + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager: restore old sigpipe handler" ); +#endif + if ( sigaction( SIGPIPE, &oldactPipe, 0 ) != 0 ) + qWarning( "Error restoring SIGPIPE handler" ); +} + +void TQProcessManager::append( TQProc *p ) +{ + procList->append( p ); +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager: append process (procList.count(): %d)", procList->count() ); +#endif +} + +void TQProcessManager::remove( TQProc *p ) +{ + procList->remove( p ); +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager: remove process (procList.count(): %d)", procList->count() ); +#endif + cleanup(); +} + +void TQProcessManager::cleanup() +{ + if ( procList->count() == 0 ) { + TQTimer::singleShot( 0, this, SLOT(removeMe()) ); + } +} + +void TQProcessManager::removeMe() +{ + if ( procList->count() == 0 ) { + qRemovePostRoutine(qprocess_cleanup); + TQProcessPrivate::procManager = 0; + delete this; + } +} + +void TQProcessManager::sigchldHnd( int fd ) +{ + // Disable the socket notifier to make sure that this function is not + // called recursively -- this can happen, if you enter the event loop in + // the slot connected to the processExited() signal (e.g. by showing a + // modal dialog) and there are more than one process which exited in the + // meantime. + if ( sn ) { + if ( !sn->isEnabled() ) + return; + sn->setEnabled( FALSE ); + } + + char tmp; + ::read( fd, &tmp, sizeof(tmp) ); +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager::sigchldHnd()" ); +#endif + TQProc *proc; + TQProcess *process; + bool removeProc; + proc = procList->first(); + while ( proc != 0 ) { + removeProc = FALSE; + process = proc->process; + if ( process != 0 ) { + if ( !process->isRunning() ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager::sigchldHnd() (PID: %d): process exited (TQProcess available)", proc->pid ); +#endif + /* + Apparently, there is not consistency among different + operating systems on how to use FIONREAD. + + FreeBSD, Linux and Solaris all expect the 3rd + argument to ioctl() to be an int, which is normally + 32-bit even on 64-bit machines. + + IRIX, on the other hand, expects a size_t, which is + 64-bit on 64-bit machines. + + So, the solution is to use size_t initialized to + zero to make sure all bits are set to zero, + preventing underflow with the FreeBSD/Linux/Solaris + ioctls. + */ + size_t nbytes = 0; + // read pending data + if ( proc->socketStdout && ::ioctl(proc->socketStdout, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager::sigchldHnd() (PID: %d): reading %d bytes of pending data on stdout", proc->pid, nbytes ); +#endif + process->socketRead( proc->socketStdout ); + } + nbytes = 0; + if ( proc->socketStderr && ::ioctl(proc->socketStderr, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager::sigchldHnd() (PID: %d): reading %d bytes of pending data on stderr", proc->pid, nbytes ); +#endif + process->socketRead( proc->socketStderr ); + } + // close filedescriptors if open, and disable the + // socket notifiers + if ( proc->socketStdout ) { + ::close( proc->socketStdout ); + proc->socketStdout = 0; + if (process->d->notifierStdout) + process->d->notifierStdout->setEnabled(FALSE); + } + if ( proc->socketStderr ) { + ::close( proc->socketStderr ); + proc->socketStderr = 0; + if (process->d->notifierStderr) + process->d->notifierStderr->setEnabled(FALSE); + } + + if ( process->notifyOnExit ) + emit process->processExited(); + + removeProc = TRUE; + } + } else { + int status; + if ( ::waitpid( proc->pid, &status, WNOHANG ) == proc->pid ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessManager::sigchldHnd() (PID: %d): process exited (TQProcess not available)", proc->pid ); +#endif + removeProc = TRUE; + } + } + if ( removeProc ) { + TQProc *oldproc = proc; + proc = procList->next(); + remove( oldproc ); + } else { + proc = procList->next(); + } + } + if ( sn ) + sn->setEnabled( TRUE ); +} + +#include "qprocess_unix.moc" + + +/*********************************************************************** + * + * TQProcessPrivate + * + **********************************************************************/ +TQProcessManager *TQProcessPrivate::procManager = 0; + +TQProcessPrivate::TQProcessPrivate() +{ +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessPrivate: Constructor" ); +#endif + stdinBufRead = 0; + + notifierStdin = 0; + notifierStdout = 0; + notifierStderr = 0; + + exitValuesCalculated = FALSE; + socketReadCalled = FALSE; + + proc = 0; +} + +TQProcessPrivate::~TQProcessPrivate() +{ +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcessPrivate: Destructor" ); +#endif + + if ( proc != 0 ) { + if ( proc->socketStdin != 0 ) { + ::close( proc->socketStdin ); + proc->socketStdin = 0; + } + proc->process = 0; + } + + while ( !stdinBuf.isEmpty() ) { + delete stdinBuf.dequeue(); + } + delete notifierStdin; + delete notifierStdout; + delete notifierStderr; +} + +/* + Closes all open sockets in the child process that are not needed by the child + process. Otherwise one child may have an open socket on standard input, etc. + of another child. +*/ +void TQProcessPrivate::closeOpenSocketsForChild() +{ + if ( procManager != 0 ) { + if ( procManager->sigchldFd[0] != 0 ) + ::close( procManager->sigchldFd[0] ); + if ( procManager->sigchldFd[1] != 0 ) + ::close( procManager->sigchldFd[1] ); + + // close also the sockets from other TQProcess instances + for ( TQProc *p=procManager->procList->first(); p!=0; p=procManager->procList->next() ) { + ::close( p->socketStdin ); + ::close( p->socketStdout ); + ::close( p->socketStderr ); + } + } +} + +void TQProcessPrivate::newProc( pid_t pid, TQProcess *process ) +{ + proc = new TQProc( pid, process ); + if ( procManager == 0 ) { + procManager = new TQProcessManager; + qAddPostRoutine(qprocess_cleanup); + } + // the TQProcessManager takes care of deleting the TQProc instances + procManager->append( proc ); +} + +/*********************************************************************** + * + * sigchld handler callback + * + **********************************************************************/ +QT_SIGNAL_RETTYPE qt_C_sigchldHnd( QT_SIGNAL_ARGS ) +{ + if ( TQProcessPrivate::procManager == 0 ) + return; + if ( TQProcessPrivate::procManager->sigchldFd[0] == 0 ) + return; + + char a = 1; + ::write( TQProcessPrivate::procManager->sigchldFd[0], &a, sizeof(a) ); +} + + +/*********************************************************************** + * + * TQProcess + * + **********************************************************************/ +/* + This private class does basic initialization. +*/ +void TQProcess::init() +{ + d = new TQProcessPrivate(); + exitStat = 0; + exitNormal = FALSE; +} + +/* + This private class resets the process variables, etc. so that it can be used + for another process to start. +*/ +void TQProcess::reset() +{ + delete d; + d = new TQProcessPrivate(); + exitStat = 0; + exitNormal = FALSE; + d->bufStdout.clear(); + d->bufStderr.clear(); +} + +TQMembuf* TQProcess::membufStdout() +{ + if ( d->proc && d->proc->socketStdout ) { + /* + Apparently, there is not consistency among different + operating systems on how to use FIONREAD. + + FreeBSD, Linux and Solaris all expect the 3rd argument to + ioctl() to be an int, which is normally 32-bit even on + 64-bit machines. + + IRIX, on the other hand, expects a size_t, which is 64-bit + on 64-bit machines. + + So, the solution is to use size_t initialized to zero to + make sure all bits are set to zero, preventing underflow + with the FreeBSD/Linux/Solaris ioctls. + */ + size_t nbytes = 0; + if ( ::ioctl(d->proc->socketStdout, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) + socketRead( d->proc->socketStdout ); + } + return &d->bufStdout; +} + +TQMembuf* TQProcess::membufStderr() +{ + if ( d->proc && d->proc->socketStderr ) { + /* + Apparently, there is not consistency among different + operating systems on how to use FIONREAD. + + FreeBSD, Linux and Solaris all expect the 3rd argument to + ioctl() to be an int, which is normally 32-bit even on + 64-bit machines. + + IRIX, on the other hand, expects a size_t, which is 64-bit + on 64-bit machines. + + So, the solution is to use size_t initialized to zero to + make sure all bits are set to zero, preventing underflow + with the FreeBSD/Linux/Solaris ioctls. + */ + size_t nbytes = 0; + if ( ::ioctl(d->proc->socketStderr, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) + socketRead( d->proc->socketStderr ); + } + return &d->bufStderr; +} + +/*! + Destroys the instance. + + If the process is running, it is not terminated! The + standard input, standard output and standard error of the process + are closed. + + You can connect the destroyed() signal to the kill() slot, if you + want the process to be terminated automatically when the instance + is destroyed. + + \sa tryTerminate() kill() +*/ +TQProcess::~TQProcess() +{ + delete d; +} + +/*! + Tries to run a process for the command and arguments that were + specified with setArguments(), addArgument() or that were + specified in the constructor. The command is searched for in the + path for executable programs; you can also use an absolute path in + the command itself. + + If \a env is null, then the process is started with the same + environment as the starting process. If \a env is non-null, then + the values in the stringlist are interpreted as environment + setttings of the form \c {key=value} and the process is started in + these environment settings. For convenience, there is a small + exception to this rule: under Unix, if \a env does not contain any + settings for the environment variable \c LD_LIBRARY_PATH, then + this variable is inherited from the starting process; under + Windows the same applies for the environment variable \c PATH. + + Returns TRUE if the process could be started; otherwise returns + FALSE. + + You can write data to the process's standard input with + writeToStdin(). You can close standard input with closeStdin() and + you can terminate the process with tryTerminate(), or with kill(). + + You can call this function even if you've used this instance to + create a another process which is still running. In such cases, + TQProcess closes the old process's standard input and deletes + pending data, i.e., you lose all control over the old process, but + the old process is not terminated. This applies also if the + process could not be started. (On operating systems that have + zombie processes, TQt will also wait() on the old process.) + + \sa launch() closeStdin() +*/ +bool TQProcess::start( TQStringList *env ) +{ +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::start()" ); +#endif + reset(); + + int sStdin[2]; + int sStdout[2]; + int sStderr[2]; + + // open sockets for piping +#ifndef Q_OS_QNX6 + if ( (comms & Stdin) && ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStdin ) == -1 ) { +#else + if ( (comms & Stdin) && qnx6SocketPairReplacement(sStdin) == -1 ) { +#endif + return FALSE; + } +#ifndef Q_OS_QNX6 + if ( (comms & Stderr) && ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStderr ) == -1 ) { +#else + if ( (comms & Stderr) && qnx6SocketPairReplacement(sStderr) == -1 ) { +#endif + if ( comms & Stdin ) { + ::close( sStdin[0] ); + ::close( sStdin[1] ); + } + return FALSE; + } +#ifndef Q_OS_QNX6 + if ( (comms & Stdout) && ::socketpair( AF_UNIX, SOCK_STREAM, 0, sStdout ) == -1 ) { +#else + if ( (comms & Stdout) && qnx6SocketPairReplacement(sStdout) == -1 ) { +#endif + if ( comms & Stdin ) { + ::close( sStdin[0] ); + ::close( sStdin[1] ); + } + if ( comms & Stderr ) { + ::close( sStderr[0] ); + ::close( sStderr[1] ); + } + return FALSE; + } + + // the following pipe is only used to determine if the process could be + // started + int fd[2]; + if ( pipe( fd ) < 0 ) { + // non critical error, go on + fd[0] = 0; + fd[1] = 0; + } + + // construct the arguments for exec + TQCString *arglistQ = new TQCString[ _arguments.count() + 1 ]; + const char** arglist = new const char*[ _arguments.count() + 1 ]; + int i = 0; + for ( TQStringList::Iterator it = _arguments.begin(); it != _arguments.end(); ++it ) { + arglistQ[i] = (*it).local8Bit(); + arglist[i] = arglistQ[i]; +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::start(): arg %d = %s", i, arglist[i] ); +#endif + i++; + } +#ifdef Q_OS_MACX + if(i) { + TQCString arg_bundle = arglistQ[0]; + TQFileInfo fi(arg_bundle); + if(fi.exists() && fi.isDir() && arg_bundle.right(4) == ".app") { + TQCString exe = arg_bundle; + int lslash = exe.findRev('/'); + if(lslash != -1) + exe = exe.mid(lslash+1); + exe = TQCString(arg_bundle + "/Contents/MacOS/" + exe); + exe = exe.left(exe.length() - 4); //chop off the .app + if(TQFile::exists(exe)) { + arglistQ[0] = exe; + arglist[0] = arglistQ[0]; + } + } + } +#endif + arglist[i] = 0; + + // Must make sure signal handlers are installed before exec'ing + // in case the process exits tquickly. + if ( d->procManager == 0 ) { + d->procManager = new TQProcessManager; + qAddPostRoutine(qprocess_cleanup); + } + + // fork and exec + TQApplication::flushX(); + pid_t pid = fork(); + if ( pid == 0 ) { + // child + d->closeOpenSocketsForChild(); + if ( comms & Stdin ) { + ::close( sStdin[1] ); + ::dup2( sStdin[0], STDIN_FILENO ); + } + if ( comms & Stdout ) { + ::close( sStdout[0] ); + ::dup2( sStdout[1], STDOUT_FILENO ); + } + if ( comms & Stderr ) { + ::close( sStderr[0] ); + ::dup2( sStderr[1], STDERR_FILENO ); + } + if ( comms & DupStderr ) { + ::dup2( STDOUT_FILENO, STDERR_FILENO ); + } +#ifndef QT_NO_DIR + ::chdir( workingDir.absPath().latin1() ); +#endif + if ( fd[0] ) + ::close( fd[0] ); + if ( fd[1] ) + ::fcntl( fd[1], F_SETFD, FD_CLOEXEC ); // close on exec shows sucess + + if ( env == 0 ) { // inherit environment and start process +#ifndef Q_OS_QNX4 + ::execvp( arglist[0], (char*const*)arglist ); // ### cast not nice +#else + ::execvp( arglist[0], (char const*const*)arglist ); // ### cast not nice +#endif + } else { // start process with environment settins as specified in env + // construct the environment for exec + int numEntries = env->count(); +#if defined(Q_OS_MACX) + TQString ld_library_path("DYLD_LIBRARY_PATH"); +#else + TQString ld_library_path("LD_LIBRARY_PATH"); +#endif + bool setLibraryPath = + env->grep( TQRegExp( "^" + ld_library_path + "=" ) ).empty() && + getenv( ld_library_path ) != 0; + if ( setLibraryPath ) + numEntries++; + TQCString *envlistQ = new TQCString[ numEntries + 1 ]; + const char** envlist = new const char*[ numEntries + 1 ]; + int i = 0; + if ( setLibraryPath ) { + envlistQ[i] = TQString( ld_library_path + "=%1" ).arg( getenv( ld_library_path ) ).local8Bit(); + envlist[i] = envlistQ[i]; + i++; + } + for ( TQStringList::Iterator it = env->begin(); it != env->end(); ++it ) { + envlistQ[i] = (*it).local8Bit(); + envlist[i] = envlistQ[i]; + i++; + } + envlist[i] = 0; + + // look for the executable in the search path + if ( _arguments.count()>0 && getenv("PATH")!=0 ) { + TQString command = _arguments[0]; + if ( !command.contains( '/' ) ) { + TQStringList pathList = TQStringList::split( ':', getenv( "PATH" ) ); + for (TQStringList::Iterator it = pathList.begin(); it != pathList.end(); ++it ) { + TQString dir = *it; +#if defined(Q_OS_MACX) //look in a bundle + if(!TQFile::exists(dir + "/" + command) && TQFile::exists(dir + "/" + command + ".app")) + dir += "/" + command + ".app/Contents/MacOS"; +#endif +#ifndef QT_NO_DIR + TQFileInfo fileInfo( dir, command ); +#else + TQFileInfo fileInfo( dir + "/" + command ); +#endif + if ( fileInfo.isExecutable() ) { +#if defined(Q_OS_MACX) + arglistQ[0] = fileInfo.absFilePath().local8Bit(); +#else + arglistQ[0] = fileInfo.filePath().local8Bit(); +#endif + arglist[0] = arglistQ[0]; + break; + } + } + } + } +#ifndef Q_OS_QNX4 + ::execve( arglist[0], (char*const*)arglist, (char*const*)envlist ); // ### casts not nice +#else + ::execve( arglist[0], (char const*const*)arglist,(char const*const*)envlist ); // ### casts not nice +#endif + } + if ( fd[1] ) { + char buf = 0; + ::write( fd[1], &buf, 1 ); + ::close( fd[1] ); + } + ::_exit( -1 ); + } else if ( pid == -1 ) { + // error forking + goto error; + } + + // test if exec was successful + if ( fd[1] ) + ::close( fd[1] ); + if ( fd[0] ) { + char buf; + for ( ;; ) { + int n = ::read( fd[0], &buf, 1 ); + if ( n==1 ) { + // socket was not closed => error + if ( ::waitpid( pid, 0, WNOHANG ) != pid ) { + // The wait did not succeed yet, so try again when we get + // the sigchild (to avoid zombies). + d->newProc( pid, 0 ); + } + d->proc = 0; + goto error; + } else if ( n==-1 ) { + if ( errno==EAGAIN || errno==EINTR ) + // try it again + continue; + } + break; + } + ::close( fd[0] ); + } + + d->newProc( pid, this ); + + if ( comms & Stdin ) { + ::close( sStdin[0] ); + d->proc->socketStdin = sStdin[1]; + + // Select non-blocking mode + int originalFlags = fcntl(d->proc->socketStdin, F_GETFL, 0); + fcntl(d->proc->socketStdin, F_SETFL, originalFlags | O_NONBLOCK); + + d->notifierStdin = new TQSocketNotifier( sStdin[1], TQSocketNotifier::Write ); + connect( d->notifierStdin, SIGNAL(activated(int)), + this, SLOT(socketWrite(int)) ); + // setup notifiers for the sockets + if ( !d->stdinBuf.isEmpty() ) { + d->notifierStdin->setEnabled( TRUE ); + } + } + if ( comms & Stdout ) { + ::close( sStdout[1] ); + d->proc->socketStdout = sStdout[0]; + d->notifierStdout = new TQSocketNotifier( sStdout[0], TQSocketNotifier::Read ); + connect( d->notifierStdout, SIGNAL(activated(int)), + this, SLOT(socketRead(int)) ); + if ( ioRedirection ) + d->notifierStdout->setEnabled( TRUE ); + } + if ( comms & Stderr ) { + ::close( sStderr[1] ); + d->proc->socketStderr = sStderr[0]; + d->notifierStderr = new TQSocketNotifier( sStderr[0], TQSocketNotifier::Read ); + connect( d->notifierStderr, SIGNAL(activated(int)), + this, SLOT(socketRead(int)) ); + if ( ioRedirection ) + d->notifierStderr->setEnabled( TRUE ); + } + + // cleanup and return + delete[] arglistQ; + delete[] arglist; + return TRUE; + +error: +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::start(): error starting process" ); +#endif + if ( d->procManager ) + d->procManager->cleanup(); + if ( comms & Stdin ) { + ::close( sStdin[1] ); + ::close( sStdin[0] ); + } + if ( comms & Stdout ) { + ::close( sStdout[0] ); + ::close( sStdout[1] ); + } + if ( comms & Stderr ) { + ::close( sStderr[0] ); + ::close( sStderr[1] ); + } + ::close( fd[0] ); + ::close( fd[1] ); + delete[] arglistQ; + delete[] arglist; + return FALSE; +} + + +/*! + Asks the process to terminate. Processes can ignore this if they + wish. If you want to be certain that the process really + terminates, you can use kill() instead. + + The slot returns immediately: it does not wait until the process + has finished. When the process terminates, the processExited() + signal is emitted. + + \sa kill() processExited() +*/ +void TQProcess::tryTerminate() const +{ + if ( d->proc != 0 ) + ::kill( d->proc->pid, SIGTERM ); +} + +/*! + Terminates the process. This is not a safe way to end a process + since the process will not be able to do any cleanup. + tryTerminate() is safer, but processes can ignore a + tryTerminate(). + + The nice way to end a process and to be sure that it is finished, + is to do something like this: + \code + process->tryTerminate(); + TQTimer::singleShot( 5000, process, SLOT( kill() ) ); + \endcode + + This tries to terminate the process the nice way. If the process + is still running after 5 seconds, it terminates the process the + hard way. The timeout should be chosen depending on the time the + process needs to do all its cleanup: use a higher value if the + process is likely to do a lot of computation or I/O on cleanup. + + The slot returns immediately: it does not wait until the process + has finished. When the process terminates, the processExited() + signal is emitted. + + \sa tryTerminate() processExited() +*/ +void TQProcess::kill() const +{ + if ( d->proc != 0 ) + ::kill( d->proc->pid, SIGKILL ); +} + +/*! + Returns TRUE if the process is running; otherwise returns FALSE. + + \sa normalExit() exitStatus() processExited() +*/ +bool TQProcess::isRunning() const +{ + if ( d->exitValuesCalculated ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::isRunning(): FALSE (already computed)" ); +#endif + return FALSE; + } + if ( d->proc == 0 ) + return FALSE; + int status; + if ( ::waitpid( d->proc->pid, &status, WNOHANG ) == d->proc->pid ) { + // compute the exit values + TQProcess *that = (TQProcess*)this; // mutable + that->exitNormal = WIFEXITED( status ) != 0; + if ( exitNormal ) { + that->exitStat = (char)WEXITSTATUS( status ); + } + d->exitValuesCalculated = TRUE; + + // On heavy processing, the socket notifier for the sigchild might not + // have found time to fire yet. + if ( d->procManager && d->procManager->sigchldFd[1] < FD_SETSIZE ) { + fd_set fds; + struct timeval tv; + FD_ZERO( &fds ); + FD_SET( d->procManager->sigchldFd[1], &fds ); + tv.tv_sec = 0; + tv.tv_usec = 0; + if ( ::select( d->procManager->sigchldFd[1]+1, &fds, 0, 0, &tv ) > 0 ) + d->procManager->sigchldHnd( d->procManager->sigchldFd[1] ); + } + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::isRunning() (PID: %d): FALSE", d->proc->pid ); +#endif + return FALSE; + } +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::isRunning() (PID: %d): TRUE", d->proc->pid ); +#endif + return TRUE; +} + +/*! + Returns TRUE if it's possible to read an entire line of text from + standard output at this time; otherwise returns FALSE. + + \sa readLineStdout() canReadLineStderr() +*/ +bool TQProcess::canReadLineStdout() const +{ + if ( !d->proc || !d->proc->socketStdout ) + return d->bufStdout.size() != 0; + + TQProcess *that = (TQProcess*)this; + return that->membufStdout()->scanNewline( 0 ); +} + +/*! + Returns TRUE if it's possible to read an entire line of text from + standard error at this time; otherwise returns FALSE. + + \sa readLineStderr() canReadLineStdout() +*/ +bool TQProcess::canReadLineStderr() const +{ + if ( !d->proc || !d->proc->socketStderr ) + return d->bufStderr.size() != 0; + + TQProcess *that = (TQProcess*)this; + return that->membufStderr()->scanNewline( 0 ); +} + +/*! + Writes the data \a buf to the process's standard input. The + process may or may not read this data. + + This function always returns immediately. The data you + pass to writeToStdin() is copied into an internal memory buffer in + TQProcess, and when control goes back to the event loop, TQProcess will + starting transferring data from this buffer to the running process.   + Sometimes the data will be transferred in several payloads, depending on + how much data is read at a time by the process itself. When TQProcess has + transferred all the data from its memory buffer to the running process, it + emits wroteToStdin(). + + Note that some operating systems use a buffer to transfer + the data. As a result, wroteToStdin() may be emitted before the + running process has actually read all the data. + + \sa wroteToStdin() closeStdin() readStdout() readStderr() +*/ +void TQProcess::writeToStdin( const TQByteArray& buf ) +{ +#if defined(QT_QPROCESS_DEBUG) +// qDebug( "TQProcess::writeToStdin(): write to stdin (%d)", d->socketStdin ); +#endif + d->stdinBuf.enqueue( new TQByteArray(buf) ); + if ( d->notifierStdin != 0 ) + d->notifierStdin->setEnabled( TRUE ); +} + + +/*! + Closes the process's standard input. + + This function also deletes any pending data that has not been + written to standard input. + + \sa wroteToStdin() +*/ +void TQProcess::closeStdin() +{ + if ( d->proc == 0 ) + return; + if ( d->proc->socketStdin !=0 ) { + while ( !d->stdinBuf.isEmpty() ) { + delete d->stdinBuf.dequeue(); + } + delete d->notifierStdin; + d->notifierStdin = 0; + if ( ::close( d->proc->socketStdin ) != 0 ) { + qWarning( "Could not close stdin of child process" ); + } +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::closeStdin(): stdin (%d) closed", d->proc->socketStdin ); +#endif + d->proc->socketStdin = 0; + } +} + + +/* + This private slot is called when the process has outputted data to either + standard output or standard error. +*/ +void TQProcess::socketRead( int fd ) +{ + if ( d->socketReadCalled ) { + // the slots that are connected to the readyRead...() signals might + // trigger a recursive call of socketRead(). Avoid this since you get a + // blocking read otherwise. + return; + } + +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::socketRead(): %d", fd ); +#endif + if ( fd == 0 ) + return; + if ( !d->proc ) + return; + TQMembuf *buffer = 0; + int n; + if ( fd == d->proc->socketStdout ) { + buffer = &d->bufStdout; + } else if ( fd == d->proc->socketStderr ) { + buffer = &d->bufStderr; + } else { + // this case should never happen, but just to be safe + return; + } +#if defined(QT_QPROCESS_DEBUG) + uint oldSize = buffer->size(); +#endif + + // try to read data first (if it fails, the filedescriptor was closed) + const int basize = 4096; + TQByteArray *ba = new TQByteArray( basize ); + n = ::read( fd, ba->data(), basize ); + if ( n > 0 ) { + ba->resize( n ); + buffer->append( ba ); + ba = 0; + } else { + delete ba; + ba = 0; + } + // eof or error? + if ( n == 0 || n == -1 ) { + if ( fd == d->proc->socketStdout ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::socketRead(): stdout (%d) closed", fd ); +#endif + d->notifierStdout->setEnabled( FALSE ); + delete d->notifierStdout; + d->notifierStdout = 0; + ::close( d->proc->socketStdout ); + d->proc->socketStdout = 0; + return; + } else if ( fd == d->proc->socketStderr ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::socketRead(): stderr (%d) closed", fd ); +#endif + d->notifierStderr->setEnabled( FALSE ); + delete d->notifierStderr; + d->notifierStderr = 0; + ::close( d->proc->socketStderr ); + d->proc->socketStderr = 0; + return; + } + } + + if ( fd < FD_SETSIZE ) { + fd_set fds; + struct timeval tv; + FD_ZERO( &fds ); + FD_SET( fd, &fds ); + tv.tv_sec = 0; + tv.tv_usec = 0; + while ( ::select( fd+1, &fds, 0, 0, &tv ) > 0 ) { + // prepare for the next round + FD_ZERO( &fds ); + FD_SET( fd, &fds ); + // read data + ba = new TQByteArray( basize ); + n = ::read( fd, ba->data(), basize ); + if ( n > 0 ) { + ba->resize( n ); + buffer->append( ba ); + ba = 0; + } else { + delete ba; + ba = 0; + break; + } + } + } + + d->socketReadCalled = TRUE; + if ( fd == d->proc->socketStdout ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::socketRead(): %d bytes read from stdout (%d)", + buffer->size()-oldSize, fd ); +#endif + emit readyReadStdout(); + } else if ( fd == d->proc->socketStderr ) { +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::socketRead(): %d bytes read from stderr (%d)", + buffer->size()-oldSize, fd ); +#endif + emit readyReadStderr(); + } + d->socketReadCalled = FALSE; +} + + +/* + This private slot is called when the process tries to read data from standard + input. +*/ +void TQProcess::socketWrite( int fd ) +{ + while ( fd == d->proc->socketStdin && d->proc->socketStdin != 0 ) { + if ( d->stdinBuf.isEmpty() ) { + d->notifierStdin->setEnabled( FALSE ); + return; + } + ssize_t ret = ::write( fd, + d->stdinBuf.head()->data() + d->stdinBufRead, + d->stdinBuf.head()->size() - d->stdinBufRead ); +#if defined(QT_QPROCESS_DEBUG) + qDebug( "TQProcess::socketWrite(): wrote %d bytes to stdin (%d)", ret, fd ); +#endif + if ( ret == -1 ) + return; + d->stdinBufRead += ret; + if ( d->stdinBufRead == (ssize_t)d->stdinBuf.head()->size() ) { + d->stdinBufRead = 0; + delete d->stdinBuf.dequeue(); + if ( wroteToStdinConnected && d->stdinBuf.isEmpty() ) + emit wroteToStdin(); + } + } +} + +/*! + \internal + Flushes standard input. This is useful if you want to use TQProcess in a + synchronous manner. + + This function should probably go into the public API. +*/ +void TQProcess::flushStdin() +{ + if (d->proc) + socketWrite(d->proc->socketStdin); +} + +/* + This private slot is only used under Windows (but moc does not know about #if + defined()). +*/ +void TQProcess::timeout() +{ +} + + +/* + This private function is used by connectNotify() and disconnectNotify() to + change the value of ioRedirection (and related behaviour) +*/ +void TQProcess::setIoRedirection( bool value ) +{ + ioRedirection = value; + if ( ioRedirection ) { + if ( d->notifierStdout ) + d->notifierStdout->setEnabled( TRUE ); + if ( d->notifierStderr ) + d->notifierStderr->setEnabled( TRUE ); + } else { + if ( d->notifierStdout ) + d->notifierStdout->setEnabled( FALSE ); + if ( d->notifierStderr ) + d->notifierStderr->setEnabled( FALSE ); + } +} + +/* + This private function is used by connectNotify() and + disconnectNotify() to change the value of notifyOnExit (and related + behaviour) +*/ +void TQProcess::setNotifyOnExit( bool value ) +{ + notifyOnExit = value; +} + +/* + This private function is used by connectNotify() and disconnectNotify() to + change the value of wroteToStdinConnected (and related behaviour) +*/ +void TQProcess::setWroteStdinConnected( bool value ) +{ + wroteToStdinConnected = value; +} + +/*! \enum TQProcess::PID + \internal +*/ +/*! + Returns platform dependent information about the process. This can + be used together with platform specific system calls. + + Under Unix the return value is the PID of the process, or -1 if no + process belongs to this object. + + Under Windows it is a pointer to the \c PROCESS_INFORMATION + struct, or 0 if no process is belongs to this object. + + Use of this function's return value is likely to be non-portable. +*/ +TQProcess::PID TQProcess::processIdentifier() +{ + if ( d->proc == 0 ) + return -1; + return d->proc->pid; +} + +#endif // QT_NO_PROCESS diff --git a/src/kernel/qpsprinter.cpp b/src/kernel/qpsprinter.cpp new file mode 100644 index 000000000..66621591e --- /dev/null +++ b/src/kernel/qpsprinter.cpp @@ -0,0 +1,6582 @@ +/********************************************************************** +** +** Implementation of TQPSPrinter class +** +** Created : 941003 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qplatformdefs.h" + +// POSIX Large File Support redefines open -> open64 +#if defined(open) +# undef open +#endif + +// POSIX Large File Support redefines truncate -> truncate64 +#if defined(truncate) +# undef truncate +#endif + +#include "qpsprinter_p.h" + +#ifndef QT_NO_PRINTER + +#undef Q_PRINTER_USE_TYPE42 + +#include "qpainter.h" +#include "qapplication.h" +#include "qpaintdevicemetrics.h" +#include "qimage.h" +#include "qdatetime.h" +#include "qstring.h" +#include "qdict.h" +#include "qmemarray.h" +#include "qfile.h" +#include "qbuffer.h" +#include "qintdict.h" +#include "qtextcodec.h" +#include "qsettings.h" +#include "qmap.h" +#include "qfontdatabase.h" +#include "qregexp.h" +#include "qbitmap.h" +#include + +#if defined(Q_OS_WIN32) +#include +#ifdef Q_PRINTER_USE_TYPE42 +#include +#endif +#else +#include +#include +#endif + +#ifdef Q_WS_X11 +#include "qt_x11_p.h" +#ifdef None +#undef None +#endif +#ifdef GrayScale +#undef GrayScale +#endif +#endif + +#if defined( Q_WS_X11 ) || defined (Q_WS_QWS) +#include "qfontdata_p.h" +#include "qfontengine_p.h" +#include "qtextlayout_p.h" +#include "qtextengine_p.h" +extern bool qt_has_xft; +#endif + +static bool qt_gen_epsf = FALSE; +static bool embedFonts = TRUE; + +Q_EXPORT void qt_generate_epsf( bool b ) +{ + qt_gen_epsf = b; +} + +static const char *const ps_header = +"/d/def load def/D{bind d}bind d/d2{dup dup}D/B{0 d2}D/W{255 d2}D/ED{exch d}D\n" +"/D0{0 ED}D/LT{lineto}D/MT{moveto}D/S{stroke}D/F{setfont}D/SW{setlinewidth}D\n" +"/CP{closepath}D/RL{rlineto}D/NP{newpath}D/CM{currentmatrix}D/SM{setmatrix}D\n" +"/TR{translate}D/SD{setdash}D/SC{aload pop setrgbcolor}D/CR{currentfile read\n" +"pop}D/i{index}D/bs{bitshift}D/scs{setcolorspace}D/DB{dict dup begin}D/DE{end\n" +"d}D/ie{ifelse}D/sp{astore pop}D/BSt 0 d/LWi 1 d/PSt 1 d/Cx 0 d/Cy 0 d/WFi\n" +"false d/OMo false d/BCol[1 1 1]d/PCol[0 0 0]d/BkCol[1 1 1]d/BDArr[0.94 0.88\n" +"0.63 0.50 0.37 0.12 0.06]d/defM matrix d/nS 0 d/GPS{PSt 1 ge PSt 5 le and{{\n" +"LArr PSt 1 sub 2 mul get}{LArr PSt 2 mul 1 sub get}ie}{[]}ie}D/QS{PSt 0 ne{\n" +"gsave LWi SW true GPS 0 SD S OMo PSt 1 ne and{BkCol SC false GPS dup 0 get\n" +"SD S}if grestore}if}D/r28{{CR dup 32 gt{exit}if pop}loop 3{CR}repeat 0 4{7\n" +"bs exch dup 128 gt{84 sub}if 42 sub 127 and add}repeat}D/rA 0 d/rL 0 d/rB{rL\n" +"0 eq{/rA r28 d/rL 28 d}if dup rL gt{rA exch rL sub rL exch/rA 0 d/rL 0 d rB\n" +"exch bs add}{dup rA 16#fffffff 3 -1 roll bs not and exch dup rL exch sub/rL\n" +"ED neg rA exch bs/rA ED}ie}D/uc{/rL 0 d 0{dup 2 i length ge{exit}if 1 rB 1\n" +"eq{3 rB dup 3 ge{1 add dup rB 1 i 5 ge{1 i 6 ge{1 i 7 ge{1 i 8 ge{128 add}if\n" +"64 add}if 32 add}if 16 add}if 3 add exch pop}if 3 add exch 10 rB 1 add{dup 3\n" +"i lt{dup}{2 i}ie 4 i 3 i 3 i sub 2 i getinterval 5 i 4 i 3 -1 roll\n" +"putinterval dup 4 -1 roll add 3 1 roll 4 -1 roll exch sub dup 0 eq{exit}if 3\n" +"1 roll}loop pop pop}{3 rB 1 add{2 copy 8 rB put 1 add}repeat}ie}loop pop}D\n" +"/sl D0/TQCIgray D0/TQCIcolor D0/TQCIindex D0/TQCI{/colorimage where{pop false 3\n" +"colorimage}{exec/TQCIcolor ED/TQCIgray TQCIcolor length 3 idiv string d 0 1\n" +"TQCIcolor length 3 idiv 1 sub{/TQCIindex ED/x TQCIindex 3 mul d TQCIgray\n" +"TQCIindex TQCIcolor x get 0.30 mul TQCIcolor x 1 add get 0.59 mul TQCIcolor x 2\n" +"add get 0.11 mul add add cvi put}for TQCIgray image}ie}D/di{gsave TR 1 i 1 eq\n" +"{false eq{pop true 3 1 roll 4 i 4 i false 4 i 4 i imagemask BkCol SC\n" +"imagemask}{pop false 3 1 roll imagemask}ie}{dup false ne{/languagelevel\n" +"where{pop languagelevel 3 ge}{false}ie}{false}ie{/ma ED 8 eq{/dc[0 1]d\n" +"/DeviceGray}{/dc[0 1 0 1 0 1]d/DeviceRGB}ie scs/im ED/mt ED/h ED/w ED/id 7\n" +"DB/ImageType 1 d/Width w d/Height h d/ImageMatrix mt d/DataSource im d\n" +"/BitsPerComponent 8 d/Decode dc d DE/md 7 DB/ImageType 1 d/Width w d/Height\n" +"h d/ImageMatrix mt d/DataSource ma d/BitsPerComponent 1 d/Decode[0 1]d DE 4\n" +"DB/ImageType 3 d/DataDict id d/MaskDict md d/InterleaveType 3 d end image}{\n" +"pop 8 4 1 roll 8 eq{image}{TQCI}ie}ie}ie grestore}d/BF{gsave BSt 1 eq{BCol SC\n" +"WFi{fill}{eofill}ie}if BSt 2 ge BSt 8 le and{BDArr BSt 2 sub get/sc ED BCol{\n" +"1. exch sub sc mul 1. exch sub}forall 3 array astore SC WFi{fill}{eofill}ie}\n" +"if BSt 9 ge BSt 14 le and{WFi{clip}{eoclip}ie defM SM pathbbox 3 i 3 i TR 4\n" +"2 roll 3 2 roll exch sub/h ED sub/w ED OMo{NP 0 0 MT 0 h RL w 0 RL 0 h neg\n" +"RL CP BkCol SC fill}if BCol SC 0.3 SW NP BSt 9 eq BSt 11 eq or{0 4 h{dup 0\n" +"exch MT w exch LT}for}if BSt 10 eq BSt 11 eq or{0 4 w{dup 0 MT h LT}for}if\n" +"BSt 12 eq BSt 14 eq or{w h gt{0 6 w h add{dup 0 MT h sub h LT}for}{0 6 w h\n" +"add{dup 0 exch MT w sub w exch LT}for}ie}if BSt 13 eq BSt 14 eq or{w h gt{0\n" +"6 w h add{dup h MT h sub 0 LT}for}{0 6 w h add{dup w exch MT w sub 0 exch LT\n" +"}for}ie}if S}if BSt 24 eq{}if grestore}D/mat matrix d/ang1 D0/ang2 D0/w D0/h\n" +"D0/x D0/y D0/ARC{/ang2 ED/ang1 ED/h ED/w ED/y ED/x ED mat CM pop x w 2 div\n" +"add y h 2 div add TR 1 h w div neg scale ang2 0 ge{0 0 w 2 div ang1 ang1\n" +"ang2 add arc}{0 0 w 2 div ang1 ang1 ang2 add arcn}ie mat SM}D/C D0/P{NP MT\n" +"0.5 0.5 rmoveto 0 -1 RL -1 0 RL 0 1 RL CP fill}D/M{/Cy ED/Cx ED}D/L{NP Cx Cy\n" +"MT/Cy ED/Cx ED Cx Cy LT QS}D/DL{NP MT LT QS}D/HL{1 i DL}D/VL{2 i exch DL}D/R\n" +"{/h ED/w ED/y ED/x ED NP x y MT 0 h RL w 0 RL 0 h neg RL CP BF QS}D/ACR{/h\n" +"ED/w ED/y ED/x ED x y MT 0 h RL w 0 RL 0 h neg RL CP}D/xr D0/yr D0/rx D0/ry\n" +"D0/rx2 D0/ry2 D0/RR{/yr ED/xr ED/h ED/w ED/y ED/x ED xr 0 le yr 0 le or{x y\n" +"w h R}{xr 100 ge yr 100 ge or{x y w h E}{/rx xr w mul 200 div d/ry yr h mul\n" +"200 div d/rx2 rx 2 mul d/ry2 ry 2 mul d NP x rx add y MT x y rx2 ry2 180 -90\n" +"x y h add ry2 sub rx2 ry2 270 -90 x w add rx2 sub y h add ry2 sub rx2 ry2 0\n" +"-90 x w add rx2 sub y rx2 ry2 90 -90 ARC ARC ARC ARC CP BF QS}ie}ie}D/E{/h\n" +"ED/w ED/y ED/x ED mat CM pop x w 2 div add y h 2 div add TR 1 h w div scale\n" +"NP 0 0 w 2 div 0 360 arc mat SM BF QS}D/A{16 div exch 16 div exch NP ARC QS}\n" +"D/PIE{/ang2 ED/ang1 ED/h ED/w ED/y ED/x ED NP x w 2 div add y h 2 div add MT\n" +"x y w h ang1 16 div ang2 16 div ARC CP BF QS}D/CH{16 div exch 16 div exch NP\n" +"ARC CP BF QS}D/BZ{curveto QS}D/CRGB{255 div 3 1 roll 255 div 3 1 roll 255\n" +"div 3 1 roll}D/BC{CRGB BkCol sp}D/BR{CRGB BCol sp/BSt ED}D/WB{1 W BR}D/NB{0\n" +"B BR}D/PE{setlinejoin setlinecap CRGB PCol sp/LWi ED/PSt ED LWi 0 eq{0.25\n" +"/LWi ED}if PCol SC}D/P1{1 0 5 2 roll 0 0 PE}D/ST{defM SM concat}D/MF{true\n" +"exch true exch{exch pop exch pop dup 0 get dup findfont dup/FontName get 3\n" +"-1 roll eq{exit}if}forall exch dup 1 get/fxscale ED 2 get/fslant ED exch\n" +"/fencoding ED[fxscale 0 fslant 1 0 0]makefont fencoding false eq{}{dup\n" +"maxlength dict begin{1 i/FID ne{def}{pop pop}ifelse}forall/Encoding\n" +"fencoding d currentdict end}ie definefont pop}D/MFEmb{findfont dup length\n" +"dict begin{1 i/FID ne{d}{pop pop}ifelse}forall/Encoding ED currentdict end\n" +"definefont pop}D/DF{findfont/fs 3 -1 roll d[fs 0 0 fs -1 mul 0 0]makefont d}\n" +"D/ty 0 d/Y{/ty ED}D/Tl{gsave SW NP 1 i exch MT 1 i 0 RL S grestore}D/XYT{ty\n" +"MT/xyshow where{pop pop xyshow}{exch pop 1 i dup length 2 div exch\n" +"stringwidth pop 3 -1 roll exch sub exch div exch 0 exch ashow}ie}D/AT{ty MT\n" +"1 i dup length 2 div exch stringwidth pop 3 -1 roll exch sub exch div exch 0\n" +"exch ashow}D/QI{/C save d pageinit/Cx 0 d/Cy 0 d/OMo false d}D/QP{C restore\n" +"showpage}D/SPD{/setpagedevice where{1 DB 3 1 roll d end setpagedevice}{pop\n" +"pop}ie}D/SV{BSt LWi PSt Cx Cy WFi OMo BCol PCol BkCol/nS nS 1 add d gsave}D\n" +"/RS{nS 0 gt{grestore/BkCol ED/PCol ED/BCol ED/OMo ED/WFi ED/Cy ED/Cx ED/PSt\n" +"ED/LWi ED/BSt ED/nS nS 1 sub d}if}D/CLSTART{/clipTmp matrix CM d defM SM NP}\n" +"D/CLEND{clip NP clipTmp SM}D/CLO{grestore gsave defM SM}D\n"; + +// the next table is derived from a list provided by Adobe on its web +// server: http://partners.adobe.com/asn/developer/typeforum/glyphlist.txt + +// the start of the header comment: +// +// Name: Adobe Glyph List +// Table version: 1.2 +// Date: 22 Oct 1998 +// +// Description: +// +// The Adobe Glyph List (AGL) list relates Unicode values (UVs) to glyph +// names, and should be used only as described in the document "Unicode and +// Glyph Names," at +// http://partners.adobe.com:80/asn/developer/type/unicodegn.html +// +// IMPORTANT NOTE: +// the list contains glyphs in the private use area of unicode. These should get removed when regenerating the glyphlist. +// also 0 shout be mapped to .notdef +static const struct { + Q_UINT16 u; + const char * g; +} unicodetoglyph[] = { + // grep '^[0-9A-F][0-9A-F][0-9A-F][0-9A-F];' < /tmp/glyphlist.txt | sed -e 's/;/, "/' -e 's-;-" }, // -' -e 's/^/ { 0x/' | sort + { 0x0000, ".notdef" }, + { 0x0020, "space" }, // SPACE + { 0x0021, "exclam" }, // EXCLAMATION MARK + { 0x0022, "quotedbl" }, // TQUOTATION MARK + { 0x0023, "numbersign" }, // NUMBER SIGN + { 0x0024, "dollar" }, // DOLLAR SIGN + { 0x0025, "percent" }, // PERCENT SIGN + { 0x0026, "ampersand" }, // AMPERSAND + { 0x0027, "quotesingle" }, // APOSTROPHE + { 0x0028, "parenleft" }, // LEFT PARENTHESIS + { 0x0029, "parenright" }, // RIGHT PARENTHESIS + { 0x002A, "asterisk" }, // ASTERISK + { 0x002B, "plus" }, // PLUS SIGN + { 0x002C, "comma" }, // COMMA + { 0x002D, "hyphen" }, // HYPHEN-MINUS + { 0x002E, "period" }, // FULL STOP + { 0x002F, "slash" }, // SOLIDUS + { 0x0030, "zero" }, // DIGIT ZERO + { 0x0031, "one" }, // DIGIT ONE + { 0x0032, "two" }, // DIGIT TWO + { 0x0033, "three" }, // DIGIT THREE + { 0x0034, "four" }, // DIGIT FOUR + { 0x0035, "five" }, // DIGIT FIVE + { 0x0036, "six" }, // DIGIT SIX + { 0x0037, "seven" }, // DIGIT SEVEN + { 0x0038, "eight" }, // DIGIT EIGHT + { 0x0039, "nine" }, // DIGIT NINE + { 0x003A, "colon" }, // COLON + { 0x003B, "semicolon" }, // SEMICOLON + { 0x003C, "less" }, // LESS-THAN SIGN + { 0x003D, "equal" }, // ETQUALS SIGN + { 0x003E, "greater" }, // GREATER-THAN SIGN + { 0x003F, "question" }, // TQUESTION MARK + { 0x0040, "at" }, // COMMERCIAL AT + { 0x0041, "A" }, // LATIN CAPITAL LETTER A + { 0x0042, "B" }, // LATIN CAPITAL LETTER B + { 0x0043, "C" }, // LATIN CAPITAL LETTER C + { 0x0044, "D" }, // LATIN CAPITAL LETTER D + { 0x0045, "E" }, // LATIN CAPITAL LETTER E + { 0x0046, "F" }, // LATIN CAPITAL LETTER F + { 0x0047, "G" }, // LATIN CAPITAL LETTER G + { 0x0048, "H" }, // LATIN CAPITAL LETTER H + { 0x0049, "I" }, // LATIN CAPITAL LETTER I + { 0x004A, "J" }, // LATIN CAPITAL LETTER J + { 0x004B, "K" }, // LATIN CAPITAL LETTER K + { 0x004C, "L" }, // LATIN CAPITAL LETTER L + { 0x004D, "M" }, // LATIN CAPITAL LETTER M + { 0x004E, "N" }, // LATIN CAPITAL LETTER N + { 0x004F, "O" }, // LATIN CAPITAL LETTER O + { 0x0050, "P" }, // LATIN CAPITAL LETTER P + { 0x0051, "Q" }, // LATIN CAPITAL LETTER Q + { 0x0052, "R" }, // LATIN CAPITAL LETTER R + { 0x0053, "S" }, // LATIN CAPITAL LETTER S + { 0x0054, "T" }, // LATIN CAPITAL LETTER T + { 0x0055, "U" }, // LATIN CAPITAL LETTER U + { 0x0056, "V" }, // LATIN CAPITAL LETTER V + { 0x0057, "W" }, // LATIN CAPITAL LETTER W + { 0x0058, "X" }, // LATIN CAPITAL LETTER X + { 0x0059, "Y" }, // LATIN CAPITAL LETTER Y + { 0x005A, "Z" }, // LATIN CAPITAL LETTER Z + { 0x005B, "bracketleft" }, // LEFT STQUARE BRACKET + { 0x005C, "backslash" }, // REVERSE SOLIDUS + { 0x005D, "bracketright" }, // RIGHT STQUARE BRACKET + { 0x005E, "asciicircum" }, // CIRCUMFLEX ACCENT + { 0x005F, "underscore" }, // LOW LINE + { 0x0060, "grave" }, // GRAVE ACCENT + { 0x0061, "a" }, // LATIN SMALL LETTER A + { 0x0062, "b" }, // LATIN SMALL LETTER B + { 0x0063, "c" }, // LATIN SMALL LETTER C + { 0x0064, "d" }, // LATIN SMALL LETTER D + { 0x0065, "e" }, // LATIN SMALL LETTER E + { 0x0066, "f" }, // LATIN SMALL LETTER F + { 0x0067, "g" }, // LATIN SMALL LETTER G + { 0x0068, "h" }, // LATIN SMALL LETTER H + { 0x0069, "i" }, // LATIN SMALL LETTER I + { 0x006A, "j" }, // LATIN SMALL LETTER J + { 0x006B, "k" }, // LATIN SMALL LETTER K + { 0x006C, "l" }, // LATIN SMALL LETTER L + { 0x006D, "m" }, // LATIN SMALL LETTER M + { 0x006E, "n" }, // LATIN SMALL LETTER N + { 0x006F, "o" }, // LATIN SMALL LETTER O + { 0x0070, "p" }, // LATIN SMALL LETTER P + { 0x0071, "q" }, // LATIN SMALL LETTER Q + { 0x0072, "r" }, // LATIN SMALL LETTER R + { 0x0073, "s" }, // LATIN SMALL LETTER S + { 0x0074, "t" }, // LATIN SMALL LETTER T + { 0x0075, "u" }, // LATIN SMALL LETTER U + { 0x0076, "v" }, // LATIN SMALL LETTER V + { 0x0077, "w" }, // LATIN SMALL LETTER W + { 0x0078, "x" }, // LATIN SMALL LETTER X + { 0x0079, "y" }, // LATIN SMALL LETTER Y + { 0x007A, "z" }, // LATIN SMALL LETTER Z + { 0x007B, "braceleft" }, // LEFT CURLY BRACKET + { 0x007C, "bar" }, // VERTICAL LINE + { 0x007D, "braceright" }, // RIGHT CURLY BRACKET + { 0x007E, "asciitilde" }, // TILDE + { 0x00A0, "space" }, // NO-BREAK SPACE;Duplicate + { 0x00A1, "exclamdown" }, // INVERTED EXCLAMATION MARK + { 0x00A2, "cent" }, // CENT SIGN + { 0x00A3, "sterling" }, // POUND SIGN + { 0x00A4, "currency" }, // CURRENCY SIGN + { 0x00A5, "yen" }, // YEN SIGN + { 0x00A6, "brokenbar" }, // BROKEN BAR + { 0x00A7, "section" }, // SECTION SIGN + { 0x00A8, "dieresis" }, // DIAERESIS + { 0x00A9, "copyright" }, // COPYRIGHT SIGN + { 0x00AA, "ordfeminine" }, // FEMININE ORDINAL INDICATOR + { 0x00AB, "guillemotleft" }, // LEFT-POINTING DOUBLE ANGLE TQUOTATION MARK + { 0x00AC, "logicalnot" }, // NOT SIGN + { 0x00AD, "hyphen" }, // SOFT HYPHEN;Duplicate + { 0x00AE, "registered" }, // REGISTERED SIGN + { 0x00AF, "macron" }, // MACRON + { 0x00B0, "degree" }, // DEGREE SIGN + { 0x00B1, "plusminus" }, // PLUS-MINUS SIGN + { 0x00B2, "twosuperior" }, // SUPERSCRIPT TWO + { 0x00B3, "threesuperior" }, // SUPERSCRIPT THREE + { 0x00B4, "acute" }, // ACUTE ACCENT + { 0x00B5, "mu" }, // MICRO SIGN + { 0x00B6, "paragraph" }, // PILCROW SIGN + { 0x00B7, "periodcentered" }, // MIDDLE DOT + { 0x00B8, "cedilla" }, // CEDILLA + { 0x00B9, "onesuperior" }, // SUPERSCRIPT ONE + { 0x00BA, "ordmasculine" }, // MASCULINE ORDINAL INDICATOR + { 0x00BB, "guillemotright" }, // RIGHT-POINTING DOUBLE ANGLE TQUOTATION MARK + { 0x00BC, "onequarter" }, // VULGAR FRACTION ONE TQUARTER + { 0x00BD, "onehalf" }, // VULGAR FRACTION ONE HALF + { 0x00BE, "threequarters" }, // VULGAR FRACTION THREE TQUARTERS + { 0x00BF, "questiondown" }, // INVERTED TQUESTION MARK + { 0x00C0, "Agrave" }, // LATIN CAPITAL LETTER A WITH GRAVE + { 0x00C1, "Aacute" }, // LATIN CAPITAL LETTER A WITH ACUTE + { 0x00C2, "Acircumflex" }, // LATIN CAPITAL LETTER A WITH CIRCUMFLEX + { 0x00C3, "Atilde" }, // LATIN CAPITAL LETTER A WITH TILDE + { 0x00C4, "Adieresis" }, // LATIN CAPITAL LETTER A WITH DIAERESIS + { 0x00C5, "Aring" }, // LATIN CAPITAL LETTER A WITH RING ABOVE + { 0x00C6, "AE" }, // LATIN CAPITAL LETTER AE + { 0x00C7, "Ccedilla" }, // LATIN CAPITAL LETTER C WITH CEDILLA + { 0x00C8, "Egrave" }, // LATIN CAPITAL LETTER E WITH GRAVE + { 0x00C9, "Eacute" }, // LATIN CAPITAL LETTER E WITH ACUTE + { 0x00CA, "Ecircumflex" }, // LATIN CAPITAL LETTER E WITH CIRCUMFLEX + { 0x00CB, "Edieresis" }, // LATIN CAPITAL LETTER E WITH DIAERESIS + { 0x00CC, "Igrave" }, // LATIN CAPITAL LETTER I WITH GRAVE + { 0x00CD, "Iacute" }, // LATIN CAPITAL LETTER I WITH ACUTE + { 0x00CE, "Icircumflex" }, // LATIN CAPITAL LETTER I WITH CIRCUMFLEX + { 0x00CF, "Idieresis" }, // LATIN CAPITAL LETTER I WITH DIAERESIS + { 0x00D0, "Eth" }, // LATIN CAPITAL LETTER ETH + { 0x00D1, "Ntilde" }, // LATIN CAPITAL LETTER N WITH TILDE + { 0x00D2, "Ograve" }, // LATIN CAPITAL LETTER O WITH GRAVE + { 0x00D3, "Oacute" }, // LATIN CAPITAL LETTER O WITH ACUTE + { 0x00D4, "Ocircumflex" }, // LATIN CAPITAL LETTER O WITH CIRCUMFLEX + { 0x00D5, "Otilde" }, // LATIN CAPITAL LETTER O WITH TILDE + { 0x00D6, "Odieresis" }, // LATIN CAPITAL LETTER O WITH DIAERESIS + { 0x00D7, "multiply" }, // MULTIPLICATION SIGN + { 0x00D8, "Oslash" }, // LATIN CAPITAL LETTER O WITH STROKE + { 0x00D9, "Ugrave" }, // LATIN CAPITAL LETTER U WITH GRAVE + { 0x00DA, "Uacute" }, // LATIN CAPITAL LETTER U WITH ACUTE + { 0x00DB, "Ucircumflex" }, // LATIN CAPITAL LETTER U WITH CIRCUMFLEX + { 0x00DC, "Udieresis" }, // LATIN CAPITAL LETTER U WITH DIAERESIS + { 0x00DD, "Yacute" }, // LATIN CAPITAL LETTER Y WITH ACUTE + { 0x00DE, "Thorn" }, // LATIN CAPITAL LETTER THORN + { 0x00DF, "germandbls" }, // LATIN SMALL LETTER SHARP S + { 0x00E0, "agrave" }, // LATIN SMALL LETTER A WITH GRAVE + { 0x00E1, "aacute" }, // LATIN SMALL LETTER A WITH ACUTE + { 0x00E2, "acircumflex" }, // LATIN SMALL LETTER A WITH CIRCUMFLEX + { 0x00E3, "atilde" }, // LATIN SMALL LETTER A WITH TILDE + { 0x00E4, "adieresis" }, // LATIN SMALL LETTER A WITH DIAERESIS + { 0x00E5, "aring" }, // LATIN SMALL LETTER A WITH RING ABOVE + { 0x00E6, "ae" }, // LATIN SMALL LETTER AE + { 0x00E7, "ccedilla" }, // LATIN SMALL LETTER C WITH CEDILLA + { 0x00E8, "egrave" }, // LATIN SMALL LETTER E WITH GRAVE + { 0x00E9, "eacute" }, // LATIN SMALL LETTER E WITH ACUTE + { 0x00EA, "ecircumflex" }, // LATIN SMALL LETTER E WITH CIRCUMFLEX + { 0x00EB, "edieresis" }, // LATIN SMALL LETTER E WITH DIAERESIS + { 0x00EC, "igrave" }, // LATIN SMALL LETTER I WITH GRAVE + { 0x00ED, "iacute" }, // LATIN SMALL LETTER I WITH ACUTE + { 0x00EE, "icircumflex" }, // LATIN SMALL LETTER I WITH CIRCUMFLEX + { 0x00EF, "idieresis" }, // LATIN SMALL LETTER I WITH DIAERESIS + { 0x00F0, "eth" }, // LATIN SMALL LETTER ETH + { 0x00F1, "ntilde" }, // LATIN SMALL LETTER N WITH TILDE + { 0x00F2, "ograve" }, // LATIN SMALL LETTER O WITH GRAVE + { 0x00F3, "oacute" }, // LATIN SMALL LETTER O WITH ACUTE + { 0x00F4, "ocircumflex" }, // LATIN SMALL LETTER O WITH CIRCUMFLEX + { 0x00F5, "otilde" }, // LATIN SMALL LETTER O WITH TILDE + { 0x00F6, "odieresis" }, // LATIN SMALL LETTER O WITH DIAERESIS + { 0x00F7, "divide" }, // DIVISION SIGN + { 0x00F8, "oslash" }, // LATIN SMALL LETTER O WITH STROKE + { 0x00F9, "ugrave" }, // LATIN SMALL LETTER U WITH GRAVE + { 0x00FA, "uacute" }, // LATIN SMALL LETTER U WITH ACUTE + { 0x00FB, "ucircumflex" }, // LATIN SMALL LETTER U WITH CIRCUMFLEX + { 0x00FC, "udieresis" }, // LATIN SMALL LETTER U WITH DIAERESIS + { 0x00FD, "yacute" }, // LATIN SMALL LETTER Y WITH ACUTE + { 0x00FE, "thorn" }, // LATIN SMALL LETTER THORN + { 0x00FF, "ydieresis" }, // LATIN SMALL LETTER Y WITH DIAERESIS + { 0x0100, "Amacron" }, // LATIN CAPITAL LETTER A WITH MACRON + { 0x0101, "amacron" }, // LATIN SMALL LETTER A WITH MACRON + { 0x0102, "Abreve" }, // LATIN CAPITAL LETTER A WITH BREVE + { 0x0103, "abreve" }, // LATIN SMALL LETTER A WITH BREVE + { 0x0104, "Aogonek" }, // LATIN CAPITAL LETTER A WITH OGONEK + { 0x0105, "aogonek" }, // LATIN SMALL LETTER A WITH OGONEK + { 0x0106, "Cacute" }, // LATIN CAPITAL LETTER C WITH ACUTE + { 0x0107, "cacute" }, // LATIN SMALL LETTER C WITH ACUTE + { 0x0108, "Ccircumflex" }, // LATIN CAPITAL LETTER C WITH CIRCUMFLEX + { 0x0109, "ccircumflex" }, // LATIN SMALL LETTER C WITH CIRCUMFLEX + { 0x010A, "Cdotaccent" }, // LATIN CAPITAL LETTER C WITH DOT ABOVE + { 0x010B, "cdotaccent" }, // LATIN SMALL LETTER C WITH DOT ABOVE + { 0x010C, "Ccaron" }, // LATIN CAPITAL LETTER C WITH CARON + { 0x010D, "ccaron" }, // LATIN SMALL LETTER C WITH CARON + { 0x010E, "Dcaron" }, // LATIN CAPITAL LETTER D WITH CARON + { 0x010F, "dcaron" }, // LATIN SMALL LETTER D WITH CARON + { 0x0110, "Dcroat" }, // LATIN CAPITAL LETTER D WITH STROKE + { 0x0111, "dcroat" }, // LATIN SMALL LETTER D WITH STROKE + { 0x0112, "Emacron" }, // LATIN CAPITAL LETTER E WITH MACRON + { 0x0113, "emacron" }, // LATIN SMALL LETTER E WITH MACRON + { 0x0114, "Ebreve" }, // LATIN CAPITAL LETTER E WITH BREVE + { 0x0115, "ebreve" }, // LATIN SMALL LETTER E WITH BREVE + { 0x0116, "Edotaccent" }, // LATIN CAPITAL LETTER E WITH DOT ABOVE + { 0x0117, "edotaccent" }, // LATIN SMALL LETTER E WITH DOT ABOVE + { 0x0118, "Eogonek" }, // LATIN CAPITAL LETTER E WITH OGONEK + { 0x0119, "eogonek" }, // LATIN SMALL LETTER E WITH OGONEK + { 0x011A, "Ecaron" }, // LATIN CAPITAL LETTER E WITH CARON + { 0x011B, "ecaron" }, // LATIN SMALL LETTER E WITH CARON + { 0x011C, "Gcircumflex" }, // LATIN CAPITAL LETTER G WITH CIRCUMFLEX + { 0x011D, "gcircumflex" }, // LATIN SMALL LETTER G WITH CIRCUMFLEX + { 0x011E, "Gbreve" }, // LATIN CAPITAL LETTER G WITH BREVE + { 0x011F, "gbreve" }, // LATIN SMALL LETTER G WITH BREVE + { 0x0120, "Gdotaccent" }, // LATIN CAPITAL LETTER G WITH DOT ABOVE + { 0x0121, "gdotaccent" }, // LATIN SMALL LETTER G WITH DOT ABOVE + { 0x0122, "Gcommaaccent" }, // LATIN CAPITAL LETTER G WITH CEDILLA + { 0x0123, "gcommaaccent" }, // LATIN SMALL LETTER G WITH CEDILLA + { 0x0124, "Hcircumflex" }, // LATIN CAPITAL LETTER H WITH CIRCUMFLEX + { 0x0125, "hcircumflex" }, // LATIN SMALL LETTER H WITH CIRCUMFLEX + { 0x0126, "Hbar" }, // LATIN CAPITAL LETTER H WITH STROKE + { 0x0127, "hbar" }, // LATIN SMALL LETTER H WITH STROKE + { 0x0128, "Itilde" }, // LATIN CAPITAL LETTER I WITH TILDE + { 0x0129, "itilde" }, // LATIN SMALL LETTER I WITH TILDE + { 0x012A, "Imacron" }, // LATIN CAPITAL LETTER I WITH MACRON + { 0x012B, "imacron" }, // LATIN SMALL LETTER I WITH MACRON + { 0x012C, "Ibreve" }, // LATIN CAPITAL LETTER I WITH BREVE + { 0x012D, "ibreve" }, // LATIN SMALL LETTER I WITH BREVE + { 0x012E, "Iogonek" }, // LATIN CAPITAL LETTER I WITH OGONEK + { 0x012F, "iogonek" }, // LATIN SMALL LETTER I WITH OGONEK + { 0x0130, "Idotaccent" }, // LATIN CAPITAL LETTER I WITH DOT ABOVE + { 0x0131, "dotlessi" }, // LATIN SMALL LETTER DOTLESS I + { 0x0132, "IJ" }, // LATIN CAPITAL LIGATURE IJ + { 0x0133, "ij" }, // LATIN SMALL LIGATURE IJ + { 0x0134, "Jcircumflex" }, // LATIN CAPITAL LETTER J WITH CIRCUMFLEX + { 0x0135, "jcircumflex" }, // LATIN SMALL LETTER J WITH CIRCUMFLEX + { 0x0136, "Kcommaaccent" }, // LATIN CAPITAL LETTER K WITH CEDILLA + { 0x0137, "kcommaaccent" }, // LATIN SMALL LETTER K WITH CEDILLA + { 0x0138, "kgreenlandic" }, // LATIN SMALL LETTER KRA + { 0x0139, "Lacute" }, // LATIN CAPITAL LETTER L WITH ACUTE + { 0x013A, "lacute" }, // LATIN SMALL LETTER L WITH ACUTE + { 0x013B, "Lcommaaccent" }, // LATIN CAPITAL LETTER L WITH CEDILLA + { 0x013C, "lcommaaccent" }, // LATIN SMALL LETTER L WITH CEDILLA + { 0x013D, "Lcaron" }, // LATIN CAPITAL LETTER L WITH CARON + { 0x013E, "lcaron" }, // LATIN SMALL LETTER L WITH CARON + { 0x013F, "Ldot" }, // LATIN CAPITAL LETTER L WITH MIDDLE DOT + { 0x0140, "ldot" }, // LATIN SMALL LETTER L WITH MIDDLE DOT + { 0x0141, "Lslash" }, // LATIN CAPITAL LETTER L WITH STROKE + { 0x0142, "lslash" }, // LATIN SMALL LETTER L WITH STROKE + { 0x0143, "Nacute" }, // LATIN CAPITAL LETTER N WITH ACUTE + { 0x0144, "nacute" }, // LATIN SMALL LETTER N WITH ACUTE + { 0x0145, "Ncommaaccent" }, // LATIN CAPITAL LETTER N WITH CEDILLA + { 0x0146, "ncommaaccent" }, // LATIN SMALL LETTER N WITH CEDILLA + { 0x0147, "Ncaron" }, // LATIN CAPITAL LETTER N WITH CARON + { 0x0148, "ncaron" }, // LATIN SMALL LETTER N WITH CARON + { 0x0149, "napostrophe" }, // LATIN SMALL LETTER N PRECEDED BY APOSTROPHE + { 0x014A, "Eng" }, // LATIN CAPITAL LETTER ENG + { 0x014B, "eng" }, // LATIN SMALL LETTER ENG + { 0x014C, "Omacron" }, // LATIN CAPITAL LETTER O WITH MACRON + { 0x014D, "omacron" }, // LATIN SMALL LETTER O WITH MACRON + { 0x014E, "Obreve" }, // LATIN CAPITAL LETTER O WITH BREVE + { 0x014F, "obreve" }, // LATIN SMALL LETTER O WITH BREVE + { 0x0150, "Ohungarumlaut" }, // LATIN CAPITAL LETTER O WITH DOUBLE ACUTE + { 0x0151, "ohungarumlaut" }, // LATIN SMALL LETTER O WITH DOUBLE ACUTE + { 0x0152, "OE" }, // LATIN CAPITAL LIGATURE OE + { 0x0153, "oe" }, // LATIN SMALL LIGATURE OE + { 0x0154, "Racute" }, // LATIN CAPITAL LETTER R WITH ACUTE + { 0x0155, "racute" }, // LATIN SMALL LETTER R WITH ACUTE + { 0x0156, "Rcommaaccent" }, // LATIN CAPITAL LETTER R WITH CEDILLA + { 0x0157, "rcommaaccent" }, // LATIN SMALL LETTER R WITH CEDILLA + { 0x0158, "Rcaron" }, // LATIN CAPITAL LETTER R WITH CARON + { 0x0159, "rcaron" }, // LATIN SMALL LETTER R WITH CARON + { 0x015A, "Sacute" }, // LATIN CAPITAL LETTER S WITH ACUTE + { 0x015B, "sacute" }, // LATIN SMALL LETTER S WITH ACUTE + { 0x015C, "Scircumflex" }, // LATIN CAPITAL LETTER S WITH CIRCUMFLEX + { 0x015D, "scircumflex" }, // LATIN SMALL LETTER S WITH CIRCUMFLEX + { 0x015E, "Scedilla" }, // LATIN CAPITAL LETTER S WITH CEDILLA + { 0x015F, "scedilla" }, // LATIN SMALL LETTER S WITH CEDILLA + { 0x0160, "Scaron" }, // LATIN CAPITAL LETTER S WITH CARON + { 0x0161, "scaron" }, // LATIN SMALL LETTER S WITH CARON + { 0x0162, "Tcommaaccent" }, // LATIN CAPITAL LETTER T WITH CEDILLA + { 0x0163, "tcommaaccent" }, // LATIN SMALL LETTER T WITH CEDILLA + { 0x0164, "Tcaron" }, // LATIN CAPITAL LETTER T WITH CARON + { 0x0165, "tcaron" }, // LATIN SMALL LETTER T WITH CARON + { 0x0166, "Tbar" }, // LATIN CAPITAL LETTER T WITH STROKE + { 0x0167, "tbar" }, // LATIN SMALL LETTER T WITH STROKE + { 0x0168, "Utilde" }, // LATIN CAPITAL LETTER U WITH TILDE + { 0x0169, "utilde" }, // LATIN SMALL LETTER U WITH TILDE + { 0x016A, "Umacron" }, // LATIN CAPITAL LETTER U WITH MACRON + { 0x016B, "umacron" }, // LATIN SMALL LETTER U WITH MACRON + { 0x016C, "Ubreve" }, // LATIN CAPITAL LETTER U WITH BREVE + { 0x016D, "ubreve" }, // LATIN SMALL LETTER U WITH BREVE + { 0x016E, "Uring" }, // LATIN CAPITAL LETTER U WITH RING ABOVE + { 0x016F, "uring" }, // LATIN SMALL LETTER U WITH RING ABOVE + { 0x0170, "Uhungarumlaut" }, // LATIN CAPITAL LETTER U WITH DOUBLE ACUTE + { 0x0171, "uhungarumlaut" }, // LATIN SMALL LETTER U WITH DOUBLE ACUTE + { 0x0172, "Uogonek" }, // LATIN CAPITAL LETTER U WITH OGONEK + { 0x0173, "uogonek" }, // LATIN SMALL LETTER U WITH OGONEK + { 0x0174, "Wcircumflex" }, // LATIN CAPITAL LETTER W WITH CIRCUMFLEX + { 0x0175, "wcircumflex" }, // LATIN SMALL LETTER W WITH CIRCUMFLEX + { 0x0176, "Ycircumflex" }, // LATIN CAPITAL LETTER Y WITH CIRCUMFLEX + { 0x0177, "ycircumflex" }, // LATIN SMALL LETTER Y WITH CIRCUMFLEX + { 0x0178, "Ydieresis" }, // LATIN CAPITAL LETTER Y WITH DIAERESIS + { 0x0179, "Zacute" }, // LATIN CAPITAL LETTER Z WITH ACUTE + { 0x017A, "zacute" }, // LATIN SMALL LETTER Z WITH ACUTE + { 0x017B, "Zdotaccent" }, // LATIN CAPITAL LETTER Z WITH DOT ABOVE + { 0x017C, "zdotaccent" }, // LATIN SMALL LETTER Z WITH DOT ABOVE + { 0x017D, "Zcaron" }, // LATIN CAPITAL LETTER Z WITH CARON + { 0x017E, "zcaron" }, // LATIN SMALL LETTER Z WITH CARON + { 0x017F, "longs" }, // LATIN SMALL LETTER LONG S + { 0x0192, "florin" }, // LATIN SMALL LETTER F WITH HOOK + { 0x01A0, "Ohorn" }, // LATIN CAPITAL LETTER O WITH HORN + { 0x01A1, "ohorn" }, // LATIN SMALL LETTER O WITH HORN + { 0x01AF, "Uhorn" }, // LATIN CAPITAL LETTER U WITH HORN + { 0x01B0, "uhorn" }, // LATIN SMALL LETTER U WITH HORN + { 0x01E6, "Gcaron" }, // LATIN CAPITAL LETTER G WITH CARON + { 0x01E7, "gcaron" }, // LATIN SMALL LETTER G WITH CARON + { 0x01FA, "Aringacute" }, // LATIN CAPITAL LETTER A WITH RING ABOVE AND ACUTE + { 0x01FB, "aringacute" }, // LATIN SMALL LETTER A WITH RING ABOVE AND ACUTE + { 0x01FC, "AEacute" }, // LATIN CAPITAL LETTER AE WITH ACUTE + { 0x01FD, "aeacute" }, // LATIN SMALL LETTER AE WITH ACUTE + { 0x01FE, "Oslashacute" }, // LATIN CAPITAL LETTER O WITH STROKE AND ACUTE + { 0x01FF, "oslashacute" }, // LATIN SMALL LETTER O WITH STROKE AND ACUTE + { 0x0218, "Scommaaccent" }, // LATIN CAPITAL LETTER S WITH COMMA BELOW + { 0x0219, "scommaaccent" }, // LATIN SMALL LETTER S WITH COMMA BELOW + { 0x021A, "Tcommaaccent" }, // LATIN CAPITAL LETTER T WITH COMMA BELOW;Duplicate + { 0x021B, "tcommaaccent" }, // LATIN SMALL LETTER T WITH COMMA BELOW;Duplicate + { 0x02BC, "afii57929" }, // MODIFIER LETTER APOSTROPHE + { 0x02BD, "afii64937" }, // MODIFIER LETTER REVERSED COMMA + { 0x02C6, "circumflex" }, // MODIFIER LETTER CIRCUMFLEX ACCENT + { 0x02C7, "caron" }, // CARON + { 0x02C9, "macron" }, // MODIFIER LETTER MACRON;Duplicate + { 0x02D8, "breve" }, // BREVE + { 0x02D9, "dotaccent" }, // DOT ABOVE + { 0x02DA, "ring" }, // RING ABOVE + { 0x02DB, "ogonek" }, // OGONEK + { 0x02DC, "tilde" }, // SMALL TILDE + { 0x02DD, "hungarumlaut" }, // DOUBLE ACUTE ACCENT + { 0x0300, "gravecomb" }, // COMBINING GRAVE ACCENT + { 0x0301, "acutecomb" }, // COMBINING ACUTE ACCENT + { 0x0303, "tildecomb" }, // COMBINING TILDE + { 0x0309, "hookabovecomb" }, // COMBINING HOOK ABOVE + { 0x0323, "dotbelowcomb" }, // COMBINING DOT BELOW + { 0x0384, "tonos" }, // GREEK TONOS + { 0x0385, "dieresistonos" }, // GREEK DIALYTIKA TONOS + { 0x0386, "Alphatonos" }, // GREEK CAPITAL LETTER ALPHA WITH TONOS + { 0x0387, "anoteleia" }, // GREEK ANO TELEIA + { 0x0388, "Epsilontonos" }, // GREEK CAPITAL LETTER EPSILON WITH TONOS + { 0x0389, "Etatonos" }, // GREEK CAPITAL LETTER ETA WITH TONOS + { 0x038A, "Iotatonos" }, // GREEK CAPITAL LETTER IOTA WITH TONOS + { 0x038C, "Omicrontonos" }, // GREEK CAPITAL LETTER OMICRON WITH TONOS + { 0x038E, "Upsilontonos" }, // GREEK CAPITAL LETTER UPSILON WITH TONOS + { 0x038F, "Omegatonos" }, // GREEK CAPITAL LETTER OMEGA WITH TONOS + { 0x0390, "iotadieresistonos" }, // GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS + { 0x0391, "Alpha" }, // GREEK CAPITAL LETTER ALPHA + { 0x0392, "Beta" }, // GREEK CAPITAL LETTER BETA + { 0x0393, "Gamma" }, // GREEK CAPITAL LETTER GAMMA + { 0x0394, "Delta" }, // GREEK CAPITAL LETTER DELTA;Duplicate + { 0x0395, "Epsilon" }, // GREEK CAPITAL LETTER EPSILON + { 0x0396, "Zeta" }, // GREEK CAPITAL LETTER ZETA + { 0x0397, "Eta" }, // GREEK CAPITAL LETTER ETA + { 0x0398, "Theta" }, // GREEK CAPITAL LETTER THETA + { 0x0399, "Iota" }, // GREEK CAPITAL LETTER IOTA + { 0x039A, "Kappa" }, // GREEK CAPITAL LETTER KAPPA + { 0x039B, "Lambda" }, // GREEK CAPITAL LETTER LAMDA + { 0x039C, "Mu" }, // GREEK CAPITAL LETTER MU + { 0x039D, "Nu" }, // GREEK CAPITAL LETTER NU + { 0x039E, "Xi" }, // GREEK CAPITAL LETTER XI + { 0x039F, "Omicron" }, // GREEK CAPITAL LETTER OMICRON + { 0x03A0, "Pi" }, // GREEK CAPITAL LETTER PI + { 0x03A1, "Rho" }, // GREEK CAPITAL LETTER RHO + { 0x03A3, "Sigma" }, // GREEK CAPITAL LETTER SIGMA + { 0x03A4, "Tau" }, // GREEK CAPITAL LETTER TAU + { 0x03A5, "Upsilon" }, // GREEK CAPITAL LETTER UPSILON + { 0x03A6, "Phi" }, // GREEK CAPITAL LETTER PHI + { 0x03A7, "Chi" }, // GREEK CAPITAL LETTER CHI + { 0x03A8, "Psi" }, // GREEK CAPITAL LETTER PSI + { 0x03A9, "Omega" }, // GREEK CAPITAL LETTER OMEGA;Duplicate + { 0x03AA, "Iotadieresis" }, // GREEK CAPITAL LETTER IOTA WITH DIALYTIKA + { 0x03AB, "Upsilondieresis" }, // GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA + { 0x03AC, "alphatonos" }, // GREEK SMALL LETTER ALPHA WITH TONOS + { 0x03AD, "epsilontonos" }, // GREEK SMALL LETTER EPSILON WITH TONOS + { 0x03AE, "etatonos" }, // GREEK SMALL LETTER ETA WITH TONOS + { 0x03AF, "iotatonos" }, // GREEK SMALL LETTER IOTA WITH TONOS + { 0x03B0, "upsilondieresistonos" }, // GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS + { 0x03B1, "alpha" }, // GREEK SMALL LETTER ALPHA + { 0x03B2, "beta" }, // GREEK SMALL LETTER BETA + { 0x03B3, "gamma" }, // GREEK SMALL LETTER GAMMA + { 0x03B4, "delta" }, // GREEK SMALL LETTER DELTA + { 0x03B5, "epsilon" }, // GREEK SMALL LETTER EPSILON + { 0x03B6, "zeta" }, // GREEK SMALL LETTER ZETA + { 0x03B7, "eta" }, // GREEK SMALL LETTER ETA + { 0x03B8, "theta" }, // GREEK SMALL LETTER THETA + { 0x03B9, "iota" }, // GREEK SMALL LETTER IOTA + { 0x03BA, "kappa" }, // GREEK SMALL LETTER KAPPA + { 0x03BB, "lambda" }, // GREEK SMALL LETTER LAMDA + { 0x03BC, "mu" }, // GREEK SMALL LETTER MU;Duplicate + { 0x03BD, "nu" }, // GREEK SMALL LETTER NU + { 0x03BE, "xi" }, // GREEK SMALL LETTER XI + { 0x03BF, "omicron" }, // GREEK SMALL LETTER OMICRON + { 0x03C0, "pi" }, // GREEK SMALL LETTER PI + { 0x03C1, "rho" }, // GREEK SMALL LETTER RHO + { 0x03C2, "sigma1" }, // GREEK SMALL LETTER FINAL SIGMA + { 0x03C3, "sigma" }, // GREEK SMALL LETTER SIGMA + { 0x03C4, "tau" }, // GREEK SMALL LETTER TAU + { 0x03C5, "upsilon" }, // GREEK SMALL LETTER UPSILON + { 0x03C6, "phi" }, // GREEK SMALL LETTER PHI + { 0x03C7, "chi" }, // GREEK SMALL LETTER CHI + { 0x03C8, "psi" }, // GREEK SMALL LETTER PSI + { 0x03C9, "omega" }, // GREEK SMALL LETTER OMEGA + { 0x03CA, "iotadieresis" }, // GREEK SMALL LETTER IOTA WITH DIALYTIKA + { 0x03CB, "upsilondieresis" }, // GREEK SMALL LETTER UPSILON WITH DIALYTIKA + { 0x03CC, "omicrontonos" }, // GREEK SMALL LETTER OMICRON WITH TONOS + { 0x03CD, "upsilontonos" }, // GREEK SMALL LETTER UPSILON WITH TONOS + { 0x03CE, "omegatonos" }, // GREEK SMALL LETTER OMEGA WITH TONOS + { 0x03D1, "theta1" }, // GREEK THETA SYMBOL + { 0x03D2, "Upsilon1" }, // GREEK UPSILON WITH HOOK SYMBOL + { 0x03D5, "phi1" }, // GREEK PHI SYMBOL + { 0x03D6, "omega1" }, // GREEK PI SYMBOL + { 0x0401, "afii10023" }, // CYRILLIC CAPITAL LETTER IO + { 0x0402, "afii10051" }, // CYRILLIC CAPITAL LETTER DJE + { 0x0403, "afii10052" }, // CYRILLIC CAPITAL LETTER GJE + { 0x0404, "afii10053" }, // CYRILLIC CAPITAL LETTER UKRAINIAN IE + { 0x0405, "afii10054" }, // CYRILLIC CAPITAL LETTER DZE + { 0x0406, "afii10055" }, // CYRILLIC CAPITAL LETTER BYELORUSSIAN-UKRAINIAN I + { 0x0407, "afii10056" }, // CYRILLIC CAPITAL LETTER YI + { 0x0408, "afii10057" }, // CYRILLIC CAPITAL LETTER JE + { 0x0409, "afii10058" }, // CYRILLIC CAPITAL LETTER LJE + { 0x040A, "afii10059" }, // CYRILLIC CAPITAL LETTER NJE + { 0x040B, "afii10060" }, // CYRILLIC CAPITAL LETTER TSHE + { 0x040C, "afii10061" }, // CYRILLIC CAPITAL LETTER KJE + { 0x040E, "afii10062" }, // CYRILLIC CAPITAL LETTER SHORT U + { 0x040F, "afii10145" }, // CYRILLIC CAPITAL LETTER DZHE + { 0x0410, "afii10017" }, // CYRILLIC CAPITAL LETTER A + { 0x0411, "afii10018" }, // CYRILLIC CAPITAL LETTER BE + { 0x0412, "afii10019" }, // CYRILLIC CAPITAL LETTER VE + { 0x0413, "afii10020" }, // CYRILLIC CAPITAL LETTER GHE + { 0x0414, "afii10021" }, // CYRILLIC CAPITAL LETTER DE + { 0x0415, "afii10022" }, // CYRILLIC CAPITAL LETTER IE + { 0x0416, "afii10024" }, // CYRILLIC CAPITAL LETTER ZHE + { 0x0417, "afii10025" }, // CYRILLIC CAPITAL LETTER ZE + { 0x0418, "afii10026" }, // CYRILLIC CAPITAL LETTER I + { 0x0419, "afii10027" }, // CYRILLIC CAPITAL LETTER SHORT I + { 0x041A, "afii10028" }, // CYRILLIC CAPITAL LETTER KA + { 0x041B, "afii10029" }, // CYRILLIC CAPITAL LETTER EL + { 0x041C, "afii10030" }, // CYRILLIC CAPITAL LETTER EM + { 0x041D, "afii10031" }, // CYRILLIC CAPITAL LETTER EN + { 0x041E, "afii10032" }, // CYRILLIC CAPITAL LETTER O + { 0x041F, "afii10033" }, // CYRILLIC CAPITAL LETTER PE + { 0x0420, "afii10034" }, // CYRILLIC CAPITAL LETTER ER + { 0x0421, "afii10035" }, // CYRILLIC CAPITAL LETTER ES + { 0x0422, "afii10036" }, // CYRILLIC CAPITAL LETTER TE + { 0x0423, "afii10037" }, // CYRILLIC CAPITAL LETTER U + { 0x0424, "afii10038" }, // CYRILLIC CAPITAL LETTER EF + { 0x0425, "afii10039" }, // CYRILLIC CAPITAL LETTER HA + { 0x0426, "afii10040" }, // CYRILLIC CAPITAL LETTER TSE + { 0x0427, "afii10041" }, // CYRILLIC CAPITAL LETTER CHE + { 0x0428, "afii10042" }, // CYRILLIC CAPITAL LETTER SHA + { 0x0429, "afii10043" }, // CYRILLIC CAPITAL LETTER SHCHA + { 0x042A, "afii10044" }, // CYRILLIC CAPITAL LETTER HARD SIGN + { 0x042B, "afii10045" }, // CYRILLIC CAPITAL LETTER YERU + { 0x042C, "afii10046" }, // CYRILLIC CAPITAL LETTER SOFT SIGN + { 0x042D, "afii10047" }, // CYRILLIC CAPITAL LETTER E + { 0x042E, "afii10048" }, // CYRILLIC CAPITAL LETTER YU + { 0x042F, "afii10049" }, // CYRILLIC CAPITAL LETTER YA + { 0x0430, "afii10065" }, // CYRILLIC SMALL LETTER A + { 0x0431, "afii10066" }, // CYRILLIC SMALL LETTER BE + { 0x0432, "afii10067" }, // CYRILLIC SMALL LETTER VE + { 0x0433, "afii10068" }, // CYRILLIC SMALL LETTER GHE + { 0x0434, "afii10069" }, // CYRILLIC SMALL LETTER DE + { 0x0435, "afii10070" }, // CYRILLIC SMALL LETTER IE + { 0x0436, "afii10072" }, // CYRILLIC SMALL LETTER ZHE + { 0x0437, "afii10073" }, // CYRILLIC SMALL LETTER ZE + { 0x0438, "afii10074" }, // CYRILLIC SMALL LETTER I + { 0x0439, "afii10075" }, // CYRILLIC SMALL LETTER SHORT I + { 0x043A, "afii10076" }, // CYRILLIC SMALL LETTER KA + { 0x043B, "afii10077" }, // CYRILLIC SMALL LETTER EL + { 0x043C, "afii10078" }, // CYRILLIC SMALL LETTER EM + { 0x043D, "afii10079" }, // CYRILLIC SMALL LETTER EN + { 0x043E, "afii10080" }, // CYRILLIC SMALL LETTER O + { 0x043F, "afii10081" }, // CYRILLIC SMALL LETTER PE + { 0x0440, "afii10082" }, // CYRILLIC SMALL LETTER ER + { 0x0441, "afii10083" }, // CYRILLIC SMALL LETTER ES + { 0x0442, "afii10084" }, // CYRILLIC SMALL LETTER TE + { 0x0443, "afii10085" }, // CYRILLIC SMALL LETTER U + { 0x0444, "afii10086" }, // CYRILLIC SMALL LETTER EF + { 0x0445, "afii10087" }, // CYRILLIC SMALL LETTER HA + { 0x0446, "afii10088" }, // CYRILLIC SMALL LETTER TSE + { 0x0447, "afii10089" }, // CYRILLIC SMALL LETTER CHE + { 0x0448, "afii10090" }, // CYRILLIC SMALL LETTER SHA + { 0x0449, "afii10091" }, // CYRILLIC SMALL LETTER SHCHA + { 0x044A, "afii10092" }, // CYRILLIC SMALL LETTER HARD SIGN + { 0x044B, "afii10093" }, // CYRILLIC SMALL LETTER YERU + { 0x044C, "afii10094" }, // CYRILLIC SMALL LETTER SOFT SIGN + { 0x044D, "afii10095" }, // CYRILLIC SMALL LETTER E + { 0x044E, "afii10096" }, // CYRILLIC SMALL LETTER YU + { 0x044F, "afii10097" }, // CYRILLIC SMALL LETTER YA + { 0x0451, "afii10071" }, // CYRILLIC SMALL LETTER IO + { 0x0452, "afii10099" }, // CYRILLIC SMALL LETTER DJE + { 0x0453, "afii10100" }, // CYRILLIC SMALL LETTER GJE + { 0x0454, "afii10101" }, // CYRILLIC SMALL LETTER UKRAINIAN IE + { 0x0455, "afii10102" }, // CYRILLIC SMALL LETTER DZE + { 0x0456, "afii10103" }, // CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I + { 0x0457, "afii10104" }, // CYRILLIC SMALL LETTER YI + { 0x0458, "afii10105" }, // CYRILLIC SMALL LETTER JE + { 0x0459, "afii10106" }, // CYRILLIC SMALL LETTER LJE + { 0x045A, "afii10107" }, // CYRILLIC SMALL LETTER NJE + { 0x045B, "afii10108" }, // CYRILLIC SMALL LETTER TSHE + { 0x045C, "afii10109" }, // CYRILLIC SMALL LETTER KJE + { 0x045E, "afii10110" }, // CYRILLIC SMALL LETTER SHORT U + { 0x045F, "afii10193" }, // CYRILLIC SMALL LETTER DZHE + { 0x0462, "afii10146" }, // CYRILLIC CAPITAL LETTER YAT + { 0x0463, "afii10194" }, // CYRILLIC SMALL LETTER YAT + { 0x0472, "afii10147" }, // CYRILLIC CAPITAL LETTER FITA + { 0x0473, "afii10195" }, // CYRILLIC SMALL LETTER FITA + { 0x0474, "afii10148" }, // CYRILLIC CAPITAL LETTER IZHITSA + { 0x0475, "afii10196" }, // CYRILLIC SMALL LETTER IZHITSA + { 0x0490, "afii10050" }, // CYRILLIC CAPITAL LETTER GHE WITH UPTURN + { 0x0491, "afii10098" }, // CYRILLIC SMALL LETTER GHE WITH UPTURN + { 0x04D9, "afii10846" }, // CYRILLIC SMALL LETTER SCHWA + { 0x05B0, "afii57799" }, // HEBREW POINT SHEVA + { 0x05B1, "afii57801" }, // HEBREW POINT HATAF SEGOL + { 0x05B2, "afii57800" }, // HEBREW POINT HATAF PATAH + { 0x05B3, "afii57802" }, // HEBREW POINT HATAF TQAMATS + { 0x05B4, "afii57793" }, // HEBREW POINT HIRIQ + { 0x05B5, "afii57794" }, // HEBREW POINT TSERE + { 0x05B6, "afii57795" }, // HEBREW POINT SEGOL + { 0x05B7, "afii57798" }, // HEBREW POINT PATAH + { 0x05B8, "afii57797" }, // HEBREW POINT TQAMATS + { 0x05B9, "afii57806" }, // HEBREW POINT HOLAM + { 0x05BB, "afii57796" }, // HEBREW POINT TQUBUTS + { 0x05BC, "afii57807" }, // HEBREW POINT DAGESH OR MAPIQ + { 0x05BD, "afii57839" }, // HEBREW POINT METEG + { 0x05BE, "afii57645" }, // HEBREW PUNCTUATION MATQAF + { 0x05BF, "afii57841" }, // HEBREW POINT RAFE + { 0x05C0, "afii57842" }, // HEBREW PUNCTUATION PASEQ + { 0x05C1, "afii57804" }, // HEBREW POINT SHIN DOT + { 0x05C2, "afii57803" }, // HEBREW POINT SIN DOT + { 0x05C3, "afii57658" }, // HEBREW PUNCTUATION SOF PASUQ + { 0x05D0, "afii57664" }, // HEBREW LETTER ALEF + { 0x05D1, "afii57665" }, // HEBREW LETTER BET + { 0x05D2, "afii57666" }, // HEBREW LETTER GIMEL + { 0x05D3, "afii57667" }, // HEBREW LETTER DALET + { 0x05D4, "afii57668" }, // HEBREW LETTER HE + { 0x05D5, "afii57669" }, // HEBREW LETTER VAV + { 0x05D6, "afii57670" }, // HEBREW LETTER ZAYIN + { 0x05D7, "afii57671" }, // HEBREW LETTER HET + { 0x05D8, "afii57672" }, // HEBREW LETTER TET + { 0x05D9, "afii57673" }, // HEBREW LETTER YOD + { 0x05DA, "afii57674" }, // HEBREW LETTER FINAL KAF + { 0x05DB, "afii57675" }, // HEBREW LETTER KAF + { 0x05DC, "afii57676" }, // HEBREW LETTER LAMED + { 0x05DD, "afii57677" }, // HEBREW LETTER FINAL MEM + { 0x05DE, "afii57678" }, // HEBREW LETTER MEM + { 0x05DF, "afii57679" }, // HEBREW LETTER FINAL NUN + { 0x05E0, "afii57680" }, // HEBREW LETTER NUN + { 0x05E1, "afii57681" }, // HEBREW LETTER SAMEKH + { 0x05E2, "afii57682" }, // HEBREW LETTER AYIN + { 0x05E3, "afii57683" }, // HEBREW LETTER FINAL PE + { 0x05E4, "afii57684" }, // HEBREW LETTER PE + { 0x05E5, "afii57685" }, // HEBREW LETTER FINAL TSADI + { 0x05E6, "afii57686" }, // HEBREW LETTER TSADI + { 0x05E7, "afii57687" }, // HEBREW LETTER TQOF + { 0x05E8, "afii57688" }, // HEBREW LETTER RESH + { 0x05E9, "afii57689" }, // HEBREW LETTER SHIN + { 0x05EA, "afii57690" }, // HEBREW LETTER TAV + { 0x05F0, "afii57716" }, // HEBREW LIGATURE YIDDISH DOUBLE VAV + { 0x05F1, "afii57717" }, // HEBREW LIGATURE YIDDISH VAV YOD + { 0x05F2, "afii57718" }, // HEBREW LIGATURE YIDDISH DOUBLE YOD + { 0x060C, "afii57388" }, // ARABIC COMMA + { 0x061B, "afii57403" }, // ARABIC SEMICOLON + { 0x061F, "afii57407" }, // ARABIC TQUESTION MARK + { 0x0621, "afii57409" }, // ARABIC LETTER HAMZA + { 0x0622, "afii57410" }, // ARABIC LETTER ALEF WITH MADDA ABOVE + { 0x0623, "afii57411" }, // ARABIC LETTER ALEF WITH HAMZA ABOVE + { 0x0624, "afii57412" }, // ARABIC LETTER WAW WITH HAMZA ABOVE + { 0x0625, "afii57413" }, // ARABIC LETTER ALEF WITH HAMZA BELOW + { 0x0626, "afii57414" }, // ARABIC LETTER YEH WITH HAMZA ABOVE + { 0x0627, "afii57415" }, // ARABIC LETTER ALEF + { 0x0628, "afii57416" }, // ARABIC LETTER BEH + { 0x0629, "afii57417" }, // ARABIC LETTER TEH MARBUTA + { 0x062A, "afii57418" }, // ARABIC LETTER TEH + { 0x062B, "afii57419" }, // ARABIC LETTER THEH + { 0x062C, "afii57420" }, // ARABIC LETTER JEEM + { 0x062D, "afii57421" }, // ARABIC LETTER HAH + { 0x062E, "afii57422" }, // ARABIC LETTER KHAH + { 0x062F, "afii57423" }, // ARABIC LETTER DAL + { 0x0630, "afii57424" }, // ARABIC LETTER THAL + { 0x0631, "afii57425" }, // ARABIC LETTER REH + { 0x0632, "afii57426" }, // ARABIC LETTER ZAIN + { 0x0633, "afii57427" }, // ARABIC LETTER SEEN + { 0x0634, "afii57428" }, // ARABIC LETTER SHEEN + { 0x0635, "afii57429" }, // ARABIC LETTER SAD + { 0x0636, "afii57430" }, // ARABIC LETTER DAD + { 0x0637, "afii57431" }, // ARABIC LETTER TAH + { 0x0638, "afii57432" }, // ARABIC LETTER ZAH + { 0x0639, "afii57433" }, // ARABIC LETTER AIN + { 0x063A, "afii57434" }, // ARABIC LETTER GHAIN + { 0x0640, "afii57440" }, // ARABIC TATWEEL + { 0x0641, "afii57441" }, // ARABIC LETTER FEH + { 0x0642, "afii57442" }, // ARABIC LETTER TQAF + { 0x0643, "afii57443" }, // ARABIC LETTER KAF + { 0x0644, "afii57444" }, // ARABIC LETTER LAM + { 0x0645, "afii57445" }, // ARABIC LETTER MEEM + { 0x0646, "afii57446" }, // ARABIC LETTER NOON + { 0x0647, "afii57470" }, // ARABIC LETTER HEH + { 0x0648, "afii57448" }, // ARABIC LETTER WAW + { 0x0649, "afii57449" }, // ARABIC LETTER ALEF MAKSURA + { 0x064A, "afii57450" }, // ARABIC LETTER YEH + { 0x064B, "afii57451" }, // ARABIC FATHATAN + { 0x064C, "afii57452" }, // ARABIC DAMMATAN + { 0x064D, "afii57453" }, // ARABIC KASRATAN + { 0x064E, "afii57454" }, // ARABIC FATHA + { 0x064F, "afii57455" }, // ARABIC DAMMA + { 0x0650, "afii57456" }, // ARABIC KASRA + { 0x0651, "afii57457" }, // ARABIC SHADDA + { 0x0652, "afii57458" }, // ARABIC SUKUN + { 0x0660, "afii57392" }, // ARABIC-INDIC DIGIT ZERO + { 0x0661, "afii57393" }, // ARABIC-INDIC DIGIT ONE + { 0x0662, "afii57394" }, // ARABIC-INDIC DIGIT TWO + { 0x0663, "afii57395" }, // ARABIC-INDIC DIGIT THREE + { 0x0664, "afii57396" }, // ARABIC-INDIC DIGIT FOUR + { 0x0665, "afii57397" }, // ARABIC-INDIC DIGIT FIVE + { 0x0666, "afii57398" }, // ARABIC-INDIC DIGIT SIX + { 0x0667, "afii57399" }, // ARABIC-INDIC DIGIT SEVEN + { 0x0668, "afii57400" }, // ARABIC-INDIC DIGIT EIGHT + { 0x0669, "afii57401" }, // ARABIC-INDIC DIGIT NINE + { 0x066A, "afii57381" }, // ARABIC PERCENT SIGN + { 0x066D, "afii63167" }, // ARABIC FIVE POINTED STAR + { 0x0679, "afii57511" }, // ARABIC LETTER TTEH + { 0x067E, "afii57506" }, // ARABIC LETTER PEH + { 0x0686, "afii57507" }, // ARABIC LETTER TCHEH + { 0x0688, "afii57512" }, // ARABIC LETTER DDAL + { 0x0691, "afii57513" }, // ARABIC LETTER RREH + { 0x0698, "afii57508" }, // ARABIC LETTER JEH + { 0x06A4, "afii57505" }, // ARABIC LETTER VEH + { 0x06AF, "afii57509" }, // ARABIC LETTER GAF + { 0x06BA, "afii57514" }, // ARABIC LETTER NOON GHUNNA + { 0x06D2, "afii57519" }, // ARABIC LETTER YEH BARREE + { 0x06D5, "afii57534" }, // ARABIC LETTER AE + { 0x1E80, "Wgrave" }, // LATIN CAPITAL LETTER W WITH GRAVE + { 0x1E81, "wgrave" }, // LATIN SMALL LETTER W WITH GRAVE + { 0x1E82, "Wacute" }, // LATIN CAPITAL LETTER W WITH ACUTE + { 0x1E83, "wacute" }, // LATIN SMALL LETTER W WITH ACUTE + { 0x1E84, "Wdieresis" }, // LATIN CAPITAL LETTER W WITH DIAERESIS + { 0x1E85, "wdieresis" }, // LATIN SMALL LETTER W WITH DIAERESIS + { 0x1EF2, "Ygrave" }, // LATIN CAPITAL LETTER Y WITH GRAVE + { 0x1EF3, "ygrave" }, // LATIN SMALL LETTER Y WITH GRAVE + { 0x200C, "afii61664" }, // ZERO WIDTH NON-JOINER + { 0x200D, "afii301" }, // ZERO WIDTH JOINER + { 0x200E, "afii299" }, // LEFT-TO-RIGHT MARK + { 0x200F, "afii300" }, // RIGHT-TO-LEFT MARK + { 0x2012, "figuredash" }, // FIGURE DASH + { 0x2013, "endash" }, // EN DASH + { 0x2014, "emdash" }, // EM DASH + { 0x2015, "afii00208" }, // HORIZONTAL BAR + { 0x2017, "underscoredbl" }, // DOUBLE LOW LINE + { 0x2018, "quoteleft" }, // LEFT SINGLE TQUOTATION MARK + { 0x2019, "quoteright" }, // RIGHT SINGLE TQUOTATION MARK + { 0x201A, "quotesinglbase" }, // SINGLE LOW-9 TQUOTATION MARK + { 0x201B, "quotereversed" }, // SINGLE HIGH-REVERSED-9 TQUOTATION MARK + { 0x201C, "quotedblleft" }, // LEFT DOUBLE TQUOTATION MARK + { 0x201D, "quotedblright" }, // RIGHT DOUBLE TQUOTATION MARK + { 0x201E, "quotedblbase" }, // DOUBLE LOW-9 TQUOTATION MARK + { 0x2020, "dagger" }, // DAGGER + { 0x2021, "daggerdbl" }, // DOUBLE DAGGER + { 0x2022, "bullet" }, // BULLET + { 0x2024, "onedotenleader" }, // ONE DOT LEADER + { 0x2025, "twodotenleader" }, // TWO DOT LEADER + { 0x2026, "ellipsis" }, // HORIZONTAL ELLIPSIS + { 0x202C, "afii61573" }, // POP DIRECTIONAL FORMATTING + { 0x202D, "afii61574" }, // LEFT-TO-RIGHT OVERRIDE + { 0x202E, "afii61575" }, // RIGHT-TO-LEFT OVERRIDE + { 0x2030, "perthousand" }, // PER MILLE SIGN + { 0x2032, "minute" }, // PRIME + { 0x2033, "second" }, // DOUBLE PRIME + { 0x2039, "guilsinglleft" }, // SINGLE LEFT-POINTING ANGLE TQUOTATION MARK + { 0x203A, "guilsinglright" }, // SINGLE RIGHT-POINTING ANGLE TQUOTATION MARK + { 0x203C, "exclamdbl" }, // DOUBLE EXCLAMATION MARK + { 0x2044, "fraction" }, // FRACTION SLASH + { 0x2070, "zerosuperior" }, // SUPERSCRIPT ZERO + { 0x2074, "foursuperior" }, // SUPERSCRIPT FOUR + { 0x2075, "fivesuperior" }, // SUPERSCRIPT FIVE + { 0x2076, "sixsuperior" }, // SUPERSCRIPT SIX + { 0x2077, "sevensuperior" }, // SUPERSCRIPT SEVEN + { 0x2078, "eightsuperior" }, // SUPERSCRIPT EIGHT + { 0x2079, "ninesuperior" }, // SUPERSCRIPT NINE + { 0x207D, "parenleftsuperior" }, // SUPERSCRIPT LEFT PARENTHESIS + { 0x207E, "parenrightsuperior" }, // SUPERSCRIPT RIGHT PARENTHESIS + { 0x207F, "nsuperior" }, // SUPERSCRIPT LATIN SMALL LETTER N + { 0x2080, "zeroinferior" }, // SUBSCRIPT ZERO + { 0x2081, "oneinferior" }, // SUBSCRIPT ONE + { 0x2082, "twoinferior" }, // SUBSCRIPT TWO + { 0x2083, "threeinferior" }, // SUBSCRIPT THREE + { 0x2084, "fourinferior" }, // SUBSCRIPT FOUR + { 0x2085, "fiveinferior" }, // SUBSCRIPT FIVE + { 0x2086, "sixinferior" }, // SUBSCRIPT SIX + { 0x2087, "seveninferior" }, // SUBSCRIPT SEVEN + { 0x2088, "eightinferior" }, // SUBSCRIPT EIGHT + { 0x2089, "nineinferior" }, // SUBSCRIPT NINE + { 0x208D, "parenleftinferior" }, // SUBSCRIPT LEFT PARENTHESIS + { 0x208E, "parenrightinferior" }, // SUBSCRIPT RIGHT PARENTHESIS + { 0x20A1, "colonmonetary" }, // COLON SIGN + { 0x20A3, "franc" }, // FRENCH FRANC SIGN + { 0x20A4, "lira" }, // LIRA SIGN + { 0x20A7, "peseta" }, // PESETA SIGN + { 0x20AA, "afii57636" }, // NEW SHETQEL SIGN + { 0x20AB, "dong" }, // DONG SIGN + { 0x20AC, "Euro" }, // EURO SIGN + { 0x2105, "afii61248" }, // CARE OF + { 0x2111, "Ifraktur" }, // BLACK-LETTER CAPITAL I + { 0x2113, "afii61289" }, // SCRIPT SMALL L + { 0x2116, "afii61352" }, // NUMERO SIGN + { 0x2118, "weierstrass" }, // SCRIPT CAPITAL P + { 0x211C, "Rfraktur" }, // BLACK-LETTER CAPITAL R + { 0x211E, "prescription" }, // PRESCRIPTION TAKE + { 0x2122, "trademark" }, // TRADE MARK SIGN + { 0x2126, "Omega" }, // OHM SIGN + { 0x212E, "estimated" }, // ESTIMATED SYMBOL + { 0x2135, "aleph" }, // ALEF SYMBOL + { 0x2153, "onethird" }, // VULGAR FRACTION ONE THIRD + { 0x2154, "twothirds" }, // VULGAR FRACTION TWO THIRDS + { 0x215B, "oneeighth" }, // VULGAR FRACTION ONE EIGHTH + { 0x215C, "threeeighths" }, // VULGAR FRACTION THREE EIGHTHS + { 0x215D, "fiveeighths" }, // VULGAR FRACTION FIVE EIGHTHS + { 0x215E, "seveneighths" }, // VULGAR FRACTION SEVEN EIGHTHS + { 0x2190, "arrowleft" }, // LEFTWARDS ARROW + { 0x2191, "arrowup" }, // UPWARDS ARROW + { 0x2192, "arrowright" }, // RIGHTWARDS ARROW + { 0x2193, "arrowdown" }, // DOWNWARDS ARROW + { 0x2194, "arrowboth" }, // LEFT RIGHT ARROW + { 0x2195, "arrowupdn" }, // UP DOWN ARROW + { 0x21A8, "arrowupdnbse" }, // UP DOWN ARROW WITH BASE + { 0x21B5, "carriagereturn" }, // DOWNWARDS ARROW WITH CORNER LEFTWARDS + { 0x21D0, "arrowdblleft" }, // LEFTWARDS DOUBLE ARROW + { 0x21D1, "arrowdblup" }, // UPWARDS DOUBLE ARROW + { 0x21D2, "arrowdblright" }, // RIGHTWARDS DOUBLE ARROW + { 0x21D3, "arrowdbldown" }, // DOWNWARDS DOUBLE ARROW + { 0x21D4, "arrowdblboth" }, // LEFT RIGHT DOUBLE ARROW + { 0x2200, "universal" }, // FOR ALL + { 0x2202, "partialdiff" }, // PARTIAL DIFFERENTIAL + { 0x2203, "existential" }, // THERE EXISTS + { 0x2205, "emptyset" }, // EMPTY SET + { 0x2206, "Delta" }, // INCREMENT + { 0x2207, "gradient" }, // NABLA + { 0x2208, "element" }, // ELEMENT OF + { 0x2209, "notelement" }, // NOT AN ELEMENT OF + { 0x220B, "suchthat" }, // CONTAINS AS MEMBER + { 0x220F, "product" }, // N-ARY PRODUCT + { 0x2211, "summation" }, // N-ARY SUMMATION + { 0x2212, "minus" }, // MINUS SIGN + { 0x2215, "fraction" }, // DIVISION SLASH;Duplicate + { 0x2217, "asteriskmath" }, // ASTERISK OPERATOR + { 0x2219, "periodcentered" }, // BULLET OPERATOR;Duplicate + { 0x221A, "radical" }, // STQUARE ROOT + { 0x221D, "proportional" }, // PROPORTIONAL TO + { 0x221E, "infinity" }, // INFINITY + { 0x221F, "orthogonal" }, // RIGHT ANGLE + { 0x2220, "angle" }, // ANGLE + { 0x2227, "logicaland" }, // LOGICAL AND + { 0x2228, "logicalor" }, // LOGICAL OR + { 0x2229, "intersection" }, // INTERSECTION + { 0x222A, "union" }, // UNION + { 0x222B, "integral" }, // INTEGRAL + { 0x2234, "therefore" }, // THEREFORE + { 0x223C, "similar" }, // TILDE OPERATOR + { 0x2245, "congruent" }, // APPROXIMATELY ETQUAL TO + { 0x2248, "approxequal" }, // ALMOST ETQUAL TO + { 0x2260, "notequal" }, // NOT ETQUAL TO + { 0x2261, "equivalence" }, // IDENTICAL TO + { 0x2264, "lessequal" }, // LESS-THAN OR ETQUAL TO + { 0x2265, "greaterequal" }, // GREATER-THAN OR ETQUAL TO + { 0x2282, "propersubset" }, // SUBSET OF + { 0x2283, "propersuperset" }, // SUPERSET OF + { 0x2284, "notsubset" }, // NOT A SUBSET OF + { 0x2286, "reflexsubset" }, // SUBSET OF OR ETQUAL TO + { 0x2287, "reflexsuperset" }, // SUPERSET OF OR ETQUAL TO + { 0x2295, "circleplus" }, // CIRCLED PLUS + { 0x2297, "circlemultiply" }, // CIRCLED TIMES + { 0x22A5, "perpendicular" }, // UP TACK + { 0x22C5, "dotmath" }, // DOT OPERATOR + { 0x2302, "house" }, // HOUSE + { 0x2310, "revlogicalnot" }, // REVERSED NOT SIGN + { 0x2320, "integraltp" }, // TOP HALF INTEGRAL + { 0x2321, "integralbt" }, // BOTTOM HALF INTEGRAL + { 0x2329, "angleleft" }, // LEFT-POINTING ANGLE BRACKET + { 0x232A, "angleright" }, // RIGHT-POINTING ANGLE BRACKET + { 0x2500, "SF100000" }, // BOX DRAWINGS LIGHT HORIZONTAL + { 0x2502, "SF110000" }, // BOX DRAWINGS LIGHT VERTICAL + { 0x250C, "SF010000" }, // BOX DRAWINGS LIGHT DOWN AND RIGHT + { 0x2510, "SF030000" }, // BOX DRAWINGS LIGHT DOWN AND LEFT + { 0x2514, "SF020000" }, // BOX DRAWINGS LIGHT UP AND RIGHT + { 0x2518, "SF040000" }, // BOX DRAWINGS LIGHT UP AND LEFT + { 0x251C, "SF080000" }, // BOX DRAWINGS LIGHT VERTICAL AND RIGHT + { 0x2524, "SF090000" }, // BOX DRAWINGS LIGHT VERTICAL AND LEFT + { 0x252C, "SF060000" }, // BOX DRAWINGS LIGHT DOWN AND HORIZONTAL + { 0x2534, "SF070000" }, // BOX DRAWINGS LIGHT UP AND HORIZONTAL + { 0x253C, "SF050000" }, // BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL + { 0x2550, "SF430000" }, // BOX DRAWINGS DOUBLE HORIZONTAL + { 0x2551, "SF240000" }, // BOX DRAWINGS DOUBLE VERTICAL + { 0x2552, "SF510000" }, // BOX DRAWINGS DOWN SINGLE AND RIGHT DOUBLE + { 0x2553, "SF520000" }, // BOX DRAWINGS DOWN DOUBLE AND RIGHT SINGLE + { 0x2554, "SF390000" }, // BOX DRAWINGS DOUBLE DOWN AND RIGHT + { 0x2555, "SF220000" }, // BOX DRAWINGS DOWN SINGLE AND LEFT DOUBLE + { 0x2556, "SF210000" }, // BOX DRAWINGS DOWN DOUBLE AND LEFT SINGLE + { 0x2557, "SF250000" }, // BOX DRAWINGS DOUBLE DOWN AND LEFT + { 0x2558, "SF500000" }, // BOX DRAWINGS UP SINGLE AND RIGHT DOUBLE + { 0x2559, "SF490000" }, // BOX DRAWINGS UP DOUBLE AND RIGHT SINGLE + { 0x255A, "SF380000" }, // BOX DRAWINGS DOUBLE UP AND RIGHT + { 0x255B, "SF280000" }, // BOX DRAWINGS UP SINGLE AND LEFT DOUBLE + { 0x255C, "SF270000" }, // BOX DRAWINGS UP DOUBLE AND LEFT SINGLE + { 0x255D, "SF260000" }, // BOX DRAWINGS DOUBLE UP AND LEFT + { 0x255E, "SF360000" }, // BOX DRAWINGS VERTICAL SINGLE AND RIGHT DOUBLE + { 0x255F, "SF370000" }, // BOX DRAWINGS VERTICAL DOUBLE AND RIGHT SINGLE + { 0x2560, "SF420000" }, // BOX DRAWINGS DOUBLE VERTICAL AND RIGHT + { 0x2561, "SF190000" }, // BOX DRAWINGS VERTICAL SINGLE AND LEFT DOUBLE + { 0x2562, "SF200000" }, // BOX DRAWINGS VERTICAL DOUBLE AND LEFT SINGLE + { 0x2563, "SF230000" }, // BOX DRAWINGS DOUBLE VERTICAL AND LEFT + { 0x2564, "SF470000" }, // BOX DRAWINGS DOWN SINGLE AND HORIZONTAL DOUBLE + { 0x2565, "SF480000" }, // BOX DRAWINGS DOWN DOUBLE AND HORIZONTAL SINGLE + { 0x2566, "SF410000" }, // BOX DRAWINGS DOUBLE DOWN AND HORIZONTAL + { 0x2567, "SF450000" }, // BOX DRAWINGS UP SINGLE AND HORIZONTAL DOUBLE + { 0x2568, "SF460000" }, // BOX DRAWINGS UP DOUBLE AND HORIZONTAL SINGLE + { 0x2569, "SF400000" }, // BOX DRAWINGS DOUBLE UP AND HORIZONTAL + { 0x256A, "SF540000" }, // BOX DRAWINGS VERTICAL SINGLE AND HORIZONTAL DOUBLE + { 0x256B, "SF530000" }, // BOX DRAWINGS VERTICAL DOUBLE AND HORIZONTAL SINGLE + { 0x256C, "SF440000" }, // BOX DRAWINGS DOUBLE VERTICAL AND HORIZONTAL + { 0x2580, "upblock" }, // UPPER HALF BLOCK + { 0x2584, "dnblock" }, // LOWER HALF BLOCK + { 0x2588, "block" }, // FULL BLOCK + { 0x258C, "lfblock" }, // LEFT HALF BLOCK + { 0x2590, "rtblock" }, // RIGHT HALF BLOCK + { 0x2591, "ltshade" }, // LIGHT SHADE + { 0x2592, "shade" }, // MEDIUM SHADE + { 0x2593, "dkshade" }, // DARK SHADE + { 0x25A0, "filledbox" }, // BLACK STQUARE + { 0x25A1, "H22073" }, // WHITE STQUARE + { 0x25AA, "H18543" }, // BLACK SMALL STQUARE + { 0x25AB, "H18551" }, // WHITE SMALL STQUARE + { 0x25AC, "filledrect" }, // BLACK RECTANGLE + { 0x25B2, "triagup" }, // BLACK UP-POINTING TRIANGLE + { 0x25BA, "triagrt" }, // BLACK RIGHT-POINTING POINTER + { 0x25BC, "triagdn" }, // BLACK DOWN-POINTING TRIANGLE + { 0x25C4, "triaglf" }, // BLACK LEFT-POINTING POINTER + { 0x25CA, "lozenge" }, // LOZENGE + { 0x25CB, "circle" }, // WHITE CIRCLE + { 0x25CF, "H18533" }, // BLACK CIRCLE + { 0x25D8, "invbullet" }, // INVERSE BULLET + { 0x25D9, "invcircle" }, // INVERSE WHITE CIRCLE + { 0x25E6, "openbullet" }, // WHITE BULLET + { 0x263A, "smileface" }, // WHITE SMILING FACE + { 0x263B, "invsmileface" }, // BLACK SMILING FACE + { 0x263C, "sun" }, // WHITE SUN WITH RAYS + { 0x2640, "female" }, // FEMALE SIGN + { 0x2642, "male" }, // MALE SIGN + { 0x2660, "spade" }, // BLACK SPADE SUIT + { 0x2663, "club" }, // BLACK CLUB SUIT + { 0x2665, "heart" }, // BLACK HEART SUIT + { 0x2666, "diamond" }, // BLACK DIAMOND SUIT + { 0x266A, "musicalnote" }, // EIGHTH NOTE + { 0x266B, "musicalnotedbl" }, // BEAMED EIGHTH NOTES + // The names below are in the PU area of Unicode, but needed to get a correct mapping of the symbol font + { 0xF6D9, "copyrightserif" }, + { 0xF6DA, "registerserif" }, + { 0xF6DB, "trademarkserif" }, + { 0xF8E5, "radicalex" }, + { 0xF8E6, "arrowvertex" }, + { 0xF8E7, "arrowhorizex" }, + { 0xF8E8, "registersans" }, + { 0xF8E9, "copyrightsans" }, + { 0xF8EA, "trademarksans" }, + { 0xF8EB, "parenlefttp" }, + { 0xF8EC, "parenleftex" }, + { 0xF8ED, "parenleftbt" }, + { 0xF8EE, "bracketlefttp" }, + { 0xF8EF, "bracketleftex" }, + { 0xF8F0, "bracketleftbt" }, + { 0xF8F1, "bracelefttp" }, + { 0xF8F2, "braceleftmid" }, + { 0xF8F3, "braceleftbt" }, + { 0xF8F4, "braceex" }, + { 0xF8F5, "integralex" }, + { 0xF8F6, "parenrighttp" }, + { 0xF8F7, "parenrightex" }, + { 0xF8F8, "parenrightbt" }, + { 0xF8F9, "bracketrighttp" }, + { 0xF8FA, "bracketrightex" }, + { 0xF8FB, "bracketrightbt" }, + { 0xF8FC, "bracerighttp" }, + { 0xF8FD, "bracerightmid" }, + { 0xF8FE, "bracerightbt" }, + // End of extensions needed for symbols + { 0xFB00, "ff" }, // LATIN SMALL LIGATURE FF + { 0xFB01, "fi" }, // LATIN SMALL LIGATURE FI + { 0xFB02, "fl" }, // LATIN SMALL LIGATURE FL + { 0xFB03, "ffi" }, // LATIN SMALL LIGATURE FFI + { 0xFB04, "ffl" }, // LATIN SMALL LIGATURE FFL + { 0xFB1F, "afii57705" }, // HEBREW LIGATURE YIDDISH YOD YOD PATAH + { 0xFB2A, "afii57694" }, // HEBREW LETTER SHIN WITH SHIN DOT + { 0xFB2B, "afii57695" }, // HEBREW LETTER SHIN WITH SIN DOT + { 0xFB35, "afii57723" }, // HEBREW LETTER VAV WITH DAGESH + { 0xFB4B, "afii57700" }, // HEBREW LETTER VAV WITH HOLAM + // end of stuff from glyphlist.txt + { 0xFFFF, 0 } +}; + +// --------------------------------------------------------------------- +// postscript font substitution dictionary. We assume every postscript printer has at least +// Helvetica, Times, Courier and Symbol + +struct psfont { + const char *psname; + float slant; + float xscale; +}; + +static const psfont Arial[] = { + {"Arial", 0, 84.04 }, + { "Arial-Italic", 0, 84.04 }, + { "Arial-Bold", 0, 88.65 }, + { "Arial-BoldItalic", 0, 88.65 } +}; + +static const psfont AvantGarde[] = { + { "AvantGarde-Book", 0, 87.43 }, + { "AvantGarde-BookOblique", 0, 88.09 }, + { "AvantGarde-Demi", 0, 88.09 }, + { "AvantGarde-DemiOblique", 0, 87.43 }, +}; + +static const psfont Bookman [] = { + { "Bookman-Light", 0, 93.78 }, + { "Bookman-LightItalic", 0, 91.42 }, + { "Bookman-Demi", 0, 99.86 }, + { "Bookman-DemiItalic", 0, 101.54 } +}; + +static const psfont Charter [] = { + { "CharterBT-Roman", 0, 84.04 }, + { "CharterBT-Italic", 0.0, 81.92 }, + { "CharterBT-Bold", 0, 88.99 }, + { "CharterBT-BoldItalic", 0.0, 88.20 } +}; + +static const psfont Courier [] = { + { "Courier", 0, 100. }, + { "Courier-Oblique", 0, 100. }, + { "Courier-Bold", 0, 100. }, + { "Courier-BoldOblique", 0, 100. } +}; + +static const psfont Garamond [] = { + { "Garamond-Antiqua", 0, 78.13 }, + { "Garamond-Kursiv", 0, 78.13 }, + { "Garamond-Halbfett", 0, 78.13 }, + { "Garamond-KursivHalbfett", 0, 78.13 } +}; + +static const psfont GillSans [] = { // ### some estimated value for xstretch + { "GillSans", 0, 82 }, + { "GillSans-Italic", 0, 82 }, + { "GillSans-Bold", 0, 82 }, + { "GillSans-BoldItalic", 0, 82 } +}; + +static const psfont Helvetica [] = { + { "Helvetica", 0, 84.04 }, + { "Helvetica-Oblique", 0, 84.04 }, + { "Helvetica-Bold", 0, 88.65 }, + { "Helvetica-BoldOblique", 0, 88.65 } +}; + +static const psfont Letter [] = { + { "LetterGothic", 0, 83.32 }, + { "LetterGothic-Italic", 0, 83.32 }, + { "LetterGothic-Bold", 0, 83.32 }, + { "LetterGothic-Bold", 0.2, 83.32 } +}; + +static const psfont LucidaSans [] = { + { "LucidaSans", 0, 94.36 }, + { "LucidaSans-Oblique", 0, 94.36 }, + { "LucidaSans-Demi", 0, 98.10 }, + { "LucidaSans-DemiOblique", 0, 98.08 } +}; + +static const psfont LucidaSansTT [] = { + { "LucidaSans-Typewriter", 0, 100.50 }, + { "LucidaSans-TypewriterOblique", 0, 100.50 }, + { "LucidaSans-TypewriterBold", 0, 100.50 }, + { "LucidaSans-TypewriterBoldOblique", 0, 100.50 } +}; + +static const psfont LucidaBright [] = { + { "LucidaBright", 0, 93.45 }, + { "LucidaBright-Italic", 0, 91.98 }, + { "LucidaBright-Demi", 0, 96.22 }, + { "LucidaBright-DemiItalic", 0, 96.98 } +}; + +static const psfont Palatino [] = { + { "Palatino-Roman", 0, 82.45 }, + { "Palatino-Italic", 0, 76.56 }, + { "Palatino-Bold", 0, 83.49 }, + { "Palatino-BoldItalic", 0, 81.51 } +}; + +static const psfont Symbol [] = { + { "Symbol", 0, 82.56 }, + { "Symbol", 0.2, 82.56 }, + { "Symbol", 0, 82.56 }, + { "Symbol", 0.2, 82.56 } +}; + +static const psfont Tahoma [] = { + { "Tahoma", 0, 83.45 }, + { "Tahoma", 0.2, 83.45 }, + { "Tahoma-Bold", 0, 95.59 }, + { "Tahoma-Bold", 0.2, 95.59 } +}; + +static const psfont Times [] = { + { "Times-Roman", 0, 82.45 }, + { "Times-Italic", 0, 82.45 }, + { "Times-Bold", 0, 82.45 }, + { "Times-BoldItalic", 0, 82.45 } +}; + +static const psfont Verdana [] = { + { "Verdana", 0, 96.06 }, + { "Verdana-Italic", 0, 96.06 }, + { "Verdana-Bold", 0, 107.12 }, + { "Verdana-BoldItalic", 0, 107.10 } +}; + +static const psfont Utopia [] = { // ### + { "Utopia-Regular", 0, 84.70 }, + { "Utopia-Regular", 0.2, 84.70 }, + { "Utopia-Bold", 0, 88.01 }, + { "Utopia-Bold", 0.2, 88.01 } +}; + +static const psfont * const SansSerifReplacements[] = { + Helvetica, 0 + }; +static const psfont * const SerifReplacements[] = { + Times, 0 + }; +static const psfont * const FixedReplacements[] = { + Courier, 0 + }; +static const psfont * const TahomaReplacements[] = { + Verdana, AvantGarde, Helvetica, 0 + }; +static const psfont * const VerdanaReplacements[] = { + Tahoma, AvantGarde, Helvetica, 0 + }; + +static const struct { + const char * input; // spaces are stripped in here, and everything lowercase + const psfont * ps; + const psfont *const * replacements; +} postscriptFonts [] = { + { "arial", Arial, SansSerifReplacements }, + { "arialmt", Arial, SansSerifReplacements }, + { "arialunicodems", Arial, SansSerifReplacements }, + { "avantgarde", AvantGarde, SansSerifReplacements }, + { "bookman", Bookman, SerifReplacements }, + { "charter", Charter, SansSerifReplacements }, + { "bitstreamcharter", Charter, SansSerifReplacements }, + { "bitstreamcyberbit", Times, SerifReplacements }, // ### + { "courier", Courier, 0 }, + { "couriernew", Courier, 0 }, + { "fixed", Courier, 0 }, + { "garamond", Garamond, SerifReplacements }, + { "gillsans", GillSans, SansSerifReplacements }, + { "helvetica", Helvetica, 0 }, + { "letter", Letter, FixedReplacements }, + { "lucida", LucidaSans, SansSerifReplacements }, + { "lucidasans", LucidaSans, SansSerifReplacements }, + { "lucidabright", LucidaBright, SerifReplacements }, + { "lucidasanstypewriter", LucidaSansTT, FixedReplacements }, + { "luciduxsans", LucidaSans, SansSerifReplacements }, + { "luciduxserif", LucidaBright, SerifReplacements }, + { "luciduxmono", LucidaSansTT, FixedReplacements }, + { "palatino", Palatino, SerifReplacements }, + { "symbol", Symbol, 0 }, + { "tahoma", Tahoma, TahomaReplacements }, + { "terminal", Courier, 0 }, + { "times", Times, 0 }, + { "timesnewroman", Times, 0 }, + { "verdana", Verdana, VerdanaReplacements }, + { "utopia", Utopia, SerifReplacements }, + { 0, 0, 0 } +}; + + +// ------------------------------End of static data ---------------------------------- + +// make sure DSC comments are not longer than 255 chars per line. +static TQString wrapDSC( const TQString &str ) +{ + TQString dsc = str.simplifyWhiteSpace(); + const uint wrapAt = 254; + TQString wrapped; + if ( dsc.length() < wrapAt ) + wrapped = dsc; + else { + wrapped = dsc.left( wrapAt ); + TQString tmp = dsc.mid( wrapAt ); + while ( tmp.length() > wrapAt-3 ) { + wrapped += "\n%%+" + tmp.left( wrapAt-3 ); + tmp = tmp.mid( wrapAt-3 ); + } + wrapped += "\n%%+" + tmp; + } + return wrapped + "\n"; +} + +static TQString toString( const float num ) +{ + return TQString::number( num, 'f', 3 ); +} + +// ----------------------------- Internal class declarations ----------------------------- + +class TQPSPrinterFontPrivate; + +class TQPSPrinterPrivate { +public: + TQPSPrinterPrivate( TQPrinter *prt, int filedes ); + ~TQPSPrinterPrivate(); + + void matrixSetup( TQPainter * ); + void clippingSetup( TQPainter * ); + void setClippingOff( TQPainter * ); + void orientationSetup(); + void resetDrawingTools( TQPainter * ); + void emitHeader( bool finished ); + void setFont( const TQFont &, int script ); + void drawImage( TQPainter *, float x, float y, float w, float h, const TQImage &img, const TQImage &mask ); + void initPage( TQPainter *paint ); + void flushPage( bool last = FALSE ); + + TQPrinter *printer; + int pageCount; + bool dirtyMatrix; + bool dirtyNewPage; + bool epsf; + TQString fontsUsed; + + // outstream is the stream the build up pages are copied to. It points to buffer + // at the start, and is reset to use the outDevice after emitHeader has been called. + TQTextStream outStream; + + // stores the descriptions of the first pages. outStream operates on this buffer + // until we call emitHeader + TQBuffer *buffer; + int pagesInBuffer; + + // the device the output is in the end streamed to. + TQIODevice * outDevice; + int fd; + + // buffer for the current page. Needed becaus we might have page fonts. + TQBuffer *pageBuffer; + TQTextStream pageStream; + + TQDict headerFontNames; + TQDict pageFontNames; + TQDict fonts; + TQPSPrinterFontPrivate *currentFontFile; + int headerFontNumber; + int pageFontNumber; + TQBuffer * fontBuffer; + TQTextStream fontStream; + bool dirtyClipping; + bool firstClipOnPage; + TQRect boundingBox; + TQImage * savedImage; + TQPen cpen; + TQBrush cbrush; + bool dirtypen; + bool dirtybrush; + TQColor bkColor; + bool dirtyBkColor; + TQt::BGMode bkMode; + bool dirtyBkMode; +#ifndef QT_NO_TEXTCODEC + TQTextCodec * currentFontCodec; +#endif + TQString currentFont; + TQFontMetrics fm; + int textY; + TQFont currentUsed; + int scriptUsed; + TQFont currentSet; + float scale; + + TQStringList fontpath; +}; + + +class TQPSPrinterFontPrivate { +public: + TQPSPrinterFontPrivate(); + virtual ~TQPSPrinterFontPrivate() {} + virtual TQString postScriptFontName() { return psname; } + virtual TQString defineFont( TQTextStream &stream, const TQString &ps, const TQFont &f, const TQString &key, + TQPSPrinterPrivate *d ); + virtual void download(TQTextStream& s, bool global); + virtual void drawText( TQTextStream &stream, const TQPoint &p, TQTextEngine *engine, int item, + const TQString &text, TQPSPrinterPrivate *d, TQPainter *paint); + virtual unsigned short mapUnicode( unsigned short unicode ); + void downloadMapping( TQTextStream &s, bool global ); + TQString glyphName( unsigned short glyphindex, bool *glyphSet = 0 ); + virtual void restore(); + + virtual unsigned short unicode_for_glyph(int glyphindex) { return glyphindex; } + virtual unsigned short glyph_for_unicode(unsigned short unicode) { return unicode; } + unsigned short insertIntoSubset( unsigned short unicode ); + virtual bool embedded() { return FALSE; } + + bool operator == ( const TQPSPrinterFontPrivate &other ) { + return other.psname == psname; + } + inline void setSymbol() { symbol = TRUE; } + +protected: + TQString psname; + TQStringList replacementList; + + TQMap subset; // unicode subset in the global font + TQMap page_subset; // subset added in this page + int subsetCount; + int pageSubsetCount; + bool global_dict; + bool downloaded; + bool symbol; +}; + +// ------------------- end of class declarations --------------------------- + +// -------------------------------------------------------------- +// beginning of font related methods +// -------------------------------------------------------------- + + +static int getPsFontType( const TQFontEngine *fe ) +{ + int weight = fe->fontDef.weight; + bool italic = fe->fontDef.italic; + + int type = 0; // used to look up in the psname array + // get the right modification, or build something + if ( weight > TQFont::Normal && italic ) + type = 3; + else if ( weight > TQFont::Normal ) + type = 2; + else if ( italic ) + type = 1; + return type; +} + +static int addPsFontNameExtension( const TQFontEngine *fe, TQString &ps, const psfont *psf = 0 ) +{ + int type = getPsFontType( fe ); + + if ( psf ) { + ps = TQString::fromLatin1( psf[type].psname ); + } else { + switch ( type ) { + case 1: + ps.append( TQString::fromLatin1("-Italic") ); + break; + case 2: + ps.append( TQString::fromLatin1("-Bold") ); + break; + case 3: + ps.append( TQString::fromLatin1("-BoldItalic") ); + break; + case 0: + default: + break; + } + } + return type; +} + +static TQString makePSFontName( const TQFontEngine *fe, int *listpos = 0, int *ftype = 0 ) +{ + TQString ps; + int i; + + TQString family = fe->fontDef.family.lower(); + + // try to make a "good" postscript name + ps = family.simplifyWhiteSpace(); + i = 0; + while( (unsigned int)i < ps.length() ) { + if ( i != 0 && ps[i] == '[') { + if ( ps[i-1] == ' ' ) + ps.truncate (i-1); + else + ps.truncate (i); + break; + } + if ( i == 0 || ps[i-1] == ' ' ) { + ps[i] = ps[i].upper(); + if ( i ) + ps.remove( i-1, 1 ); + else + i++; + } else { + i++; + } + } + + if ( ps.isEmpty() ) + ps = "Helvetica"; + + // see if the table has a better name + i = 0; + TQString lowerName = ps.lower(); + while( postscriptFonts[i].input && + postscriptFonts[i].input != lowerName ) + i++; + const psfont *psf = postscriptFonts[i].ps; + + int type = addPsFontNameExtension( fe, ps, psf ); + + if ( listpos ) + *listpos = i; + if ( ftype ) + *ftype = type; + return ps; +} + +static void appendReplacements( TQStringList &list, const psfont * const * replacements, int type, float xscale = 100. ) +{ + // iterate through the replacement fonts + while ( *replacements ) { + const psfont *psf = *replacements; + TQString ps = "[ /" + TQString::fromLatin1( psf[type].psname ) + " " + + toString( xscale / psf[type].xscale ) + " " + + toString( psf[type].slant ) + " ]"; + list.append( ps ); + ++replacements; + } +} + +static TQStringList makePSFontNameList( const TQFontEngine *fe, const TQString &psname = TQString::null, bool useNameForLookup = FALSE ) +{ + int i; + int type; + TQStringList list; + TQString ps = psname; + + if ( !ps.isEmpty() && !useNameForLookup ) { + TQString best = "[ /" + ps + " 1.0 0.0 ]"; + list.append( best ); + } + + ps = makePSFontName( fe, &i, &type ); + + const psfont *psf = postscriptFonts[i].ps; + const psfont * const * replacements = postscriptFonts[i].replacements; + float xscale = 100; + if ( psf ) { + // xscale for the "right" font is always 1. We scale the replacements... + xscale = psf->xscale; + ps = "[ /" + TQString::fromLatin1( psf[type].psname ) + " 1.0 " + + toString( psf[type].slant ) + " ]"; + } else { + ps = "[ /" + ps + " 1.0 0.0 ]"; + // only add default replacement fonts in case this font was unknown. + if ( fe->fontDef.fixedPitch ) { + replacements = FixedReplacements; + } else { + replacements = SansSerifReplacements; + // 100 is courier, but most fonts are not as wide as courier. Using 100 + // here would make letters overlap for some fonts. This value is empirical. + xscale = 83; + } + } + list.append( ps ); + + if ( replacements ) + appendReplacements( list, replacements, type, xscale); + return list; +} + +static void emitPSFontNameList( TQTextStream &s, const TQString &psname, const TQStringList &list ) +{ + s << "/" << psname << "List [\n"; + s << list.join("\n "); + s << "\n] d\n"; +} + +static inline float pointSize( const TQFont &f, float scale ) +{ + float psize; + if ( f.pointSize() != -1 ) + psize = f.pointSize()/scale; + else + psize = f.pixelSize(); + return psize; +} + + +// ========================== FONT CLASSES =============== + + +TQPSPrinterFontPrivate::TQPSPrinterFontPrivate() +{ + global_dict = FALSE; + downloaded = FALSE; + symbol = FALSE; + // map 0 to .notdef + subset.insert( 0, 0 ); + subsetCount = 1; + pageSubsetCount = 0; +} + +unsigned short TQPSPrinterFontPrivate::insertIntoSubset( unsigned short u ) +{ + unsigned short retval = 0; + if ( subset.find(u) == subset.end() ) { + if ( !downloaded ) { // we need to add to the page subset + subset.insert( u, subsetCount ); // mark it as used + //printf("GLOBAL SUBSET ADDED %04x = %04x\n",u, subsetCount); + retval = subsetCount; + subsetCount++; + } else if ( page_subset.find(u) == page_subset.end() ) { + page_subset.insert( u, pageSubsetCount ); // mark it as used + //printf("PAGE SUBSET ADDED %04x = %04x\n",u, pageSubsetCount); + retval = pageSubsetCount + (subsetCount/256 + 1) * 256; + pageSubsetCount++; + } + } else { + qWarning("TQPSPrinterFont::internal error"); + } + return retval; +} + +void TQPSPrinterFontPrivate::restore() +{ + page_subset.clear(); + pageSubsetCount = 0; + //qDebug("restore for font %s\n",psname.latin1()); +} + +static inline const char *toHex( uchar u ) +{ + static char hexVal[3]; + int i = 1; + while ( i >= 0 ) { + ushort hex = (u & 0x000f); + if ( hex < 0x0a ) + hexVal[i] = '0'+hex; + else + hexVal[i] = 'A'+(hex-0x0a); + u = u >> 4; + i--; + } + hexVal[2] = '\0'; + return hexVal; +} + +static inline const char *toHex( ushort u ) +{ + static char hexVal[5]; + int i = 3; + while ( i >= 0 ) { + ushort hex = (u & 0x000f); + if ( hex < 0x0a ) + hexVal[i] = '0'+hex; + else + hexVal[i] = 'A'+(hex-0x0a); + u = u >> 4; + i--; + } + hexVal[4] = '\0'; + return hexVal; +} + +static inline const char * toInt( int i ) +{ + static char intVal[20]; + intVal[19] = 0; + int pos = 19; + if ( i == 0 ) { + intVal[--pos] = '0'; + } else { + bool neg = FALSE; + if ( i < 0 ) { + neg = TRUE; + i = -i; + } + while ( i ) { + int dec = i%10; + intVal[--pos] = '0'+dec; + i /= 10; + } + if ( neg ) + intVal[--pos] = '-'; + } + return intVal+pos; +} + +void TQPSPrinterFontPrivate::drawText( TQTextStream &stream, const TQPoint &p, TQTextEngine *engine, int item, + const TQString &text, TQPSPrinterPrivate *d, TQPainter *paint) +{ + int len = engine->length( item ); + TQScriptItem &si = engine->items[item]; + + int x = p.x() + si.x; + int y = p.y() + si.y; + if ( y != d->textY || d->textY == 0 ) + stream << y << " Y"; + d->textY = y; + + stream << "<"; + if ( si.analysis.bidiLevel % 2 ) { + for ( int i = len-1; i >=0; i-- ) + stream << toHex( mapUnicode(text.unicode()[i].unicode()) ); + } else { + for ( int i = 0; i < len; i++ ) + stream << toHex( mapUnicode(text.unicode()[i].unicode()) ); + } + stream << ">"; + + stream << si.width << " " << x; + + if ( paint->font().underline() ) + stream << ' ' << y + d->fm.underlinePos() + d->fm.lineWidth() + << " " << d->fm.lineWidth() << " Tl"; + if ( paint->font().strikeOut() ) + stream << ' ' << y + d->fm.strikeOutPos() + << " " << d->fm.lineWidth() << " Tl"; + stream << " AT\n"; + +} + + +TQString TQPSPrinterFontPrivate::defineFont( TQTextStream &stream, const TQString &ps, const TQFont &f, const TQString &key, + TQPSPrinterPrivate *d ) +{ + TQString fontName; + fontName.sprintf( "/%s-Uni", ps.latin1()); + + if ( d->buffer ) { + ++d->headerFontNumber; + d->fontStream << "/F" << d->headerFontNumber << " " + << pointSize( f, d->scale ) << fontName << " DF\n"; + fontName.sprintf( "F%d", d->headerFontNumber ); + d->headerFontNames.insert( key, new TQString( fontName ) ); + } else { + ++d->pageFontNumber; + stream << "/F" << d->pageFontNumber << " " + << pointSize( f, d->scale ) << fontName << " DF\n"; + fontName.sprintf( "F%d", d->pageFontNumber ); + d->pageFontNames.insert( key, new TQString( fontName ) ); + } + return fontName; +} + +unsigned short TQPSPrinterFontPrivate::mapUnicode( unsigned short unicode ) +{ + TQMap::iterator res; + res = subset.find( unicode ); + unsigned short offset = 0; + bool found = FALSE; + if ( res != subset.end() ) { + found = TRUE; + } else { + if ( downloaded ) { + res = page_subset.find( unicode ); + offset = (subsetCount/256 + 1) * 256; + if ( res != page_subset.end() ) + found = TRUE; + } + } + if ( !found ) { + return insertIntoSubset( unicode ); + } + //qDebug("mapping unicode %x to %x", unicode, offset+*res); + return offset + *res; +} + +// This map is used for symbol fonts to get the correct glyph names for the latin range +static const unsigned short symbol_map[0x100] = { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, + 0x0020, 0x0021, 0x2200, 0x0023, 0x2203, 0x0025, 0x0026, 0x220b, + 0x0028, 0x0029, 0x2217, 0x002b, 0x002c, 0x2212, 0x002e, 0x002f, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, + + 0x2245, 0x0391, 0x0392, 0x03a7, 0x0394, 0x0395, 0x03a6, 0x0393, + 0x0397, 0x0399, 0x03d1, 0x039a, 0x039b, 0x039c, 0x039d, 0x039f, + 0x03a0, 0x0398, 0x03a1, 0x03a3, 0x03a4, 0x03a5, 0x03c2, 0x03a9, + 0x039e, 0x03a8, 0x0396, 0x005b, 0x2234, 0x005d, 0x22a5, 0x005f, + 0xf8e5, 0x03b1, 0x03b2, 0x03c7, 0x03b4, 0x03b5, 0x03c6, 0x03b3, + 0x03b7, 0x03b9, 0x03d5, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf, + 0x03c0, 0x03b8, 0x03c1, 0x03c3, 0x03c4, 0x03c5, 0x03d6, 0x03c9, + 0x03be, 0x03c8, 0x03b6, 0x007b, 0x007c, 0x007d, 0x223c, 0x007f, + + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, + 0x20ac, 0x03d2, 0x2023, 0x2264, 0x2044, 0x221e, 0x0192, 0x2263, + 0x2666, 0x2665, 0x2660, 0x2194, 0x2190, 0x2191, 0x2192, 0x2193, + 0x00b0, 0x00b1, 0x2033, 0x2265, 0x00d7, 0x221d, 0x2202, 0x2022, + 0x00f7, 0x2260, 0x2261, 0x2248, 0x2026, 0xf8e6, 0xf8e7, 0x21b5, + + 0x2135, 0x2111, 0x211c, 0x2118, 0x2297, 0x2295, 0x2205, 0x2229, + 0x222a, 0x2283, 0x2287, 0x2284, 0x2282, 0x2286, 0x2208, 0x2209, + 0x2220, 0x2207, 0xf6da, 0xf6d9, 0xf6db, 0x220f, 0x221a, 0x22c5, + 0x00ac, 0x2227, 0x2228, 0x21d4, 0x21d0, 0x21d1, 0x21d2, 0x21d3, + 0x25ca, 0x2329, 0xf8e8, 0xf8e9, 0xf8ea, 0x2211, 0xf8eb, 0xf8ec, + 0xf8ed, 0xf8ee, 0xf8ef, 0xf8f0, 0xf8f1, 0xf8f2, 0xf8f3, 0xf8f4, + 0x0000, 0x232a, 0x222b, 0x2320, 0xf8f5, 0x2321, 0xf8f6, 0xf8f7, + 0xf8f8, 0xf8f9, 0xf8fa, 0xf8fb, 0xf8fc, 0xf8fd, 0xf8fe, 0x0000, +}; + +TQString TQPSPrinterFontPrivate::glyphName( unsigned short glyphindex, bool *glyphSet ) +{ + TQString glyphname; + int l = 0; + unsigned short unicode = unicode_for_glyph( glyphindex ); + if (symbol && unicode < 0x100) { + // map from latin1 to symbol + unicode = symbol_map[unicode]; + } + if ( !unicode && glyphindex ) { + glyphname = "gl"; + glyphname += toHex( glyphindex ); + } else { + while( unicodetoglyph[l].u < unicode ) + l++; + if ( unicodetoglyph[l].u == unicode ) { + glyphname = unicodetoglyph[l].g; + if ( glyphSet ) { + int other = 0; + switch ( unicode ) { + // some glyph names are duplicate in postscript. Make sure we give the + // duplicate a different name to avoid infinite recursion + case 0x0394: + other = 0x2206; + break; + case 0x03a9: + other = 0x2126; + break; + case 0x0162: + other = 0x021a; + break; + case 0x2215: + other = 0x2044; + break; + case 0x00ad: + other = 0x002d; + break; + case 0x02c9: + other = 0x00af; + break; + case 0x03bc: + other = 0x00b5; + break; + case 0x2219: + other = 0x00b7; + break; + case 0x00a0: + other = 0x0020; + break; + case 0x0163: + other = 0x021b; + break; + default: + break; + } + if ( other ) { + int oglyph = glyph_for_unicode( other ); + if( oglyph && oglyph != glyphindex && glyphSet[oglyph] ) { + glyphname = "uni"; + glyphname += toHex( unicode ); + } + } + } + } else { + glyphname = "uni"; + glyphname += toHex( unicode ); + } + } + return glyphname; +} + +void TQPSPrinterFontPrivate::download(TQTextStream &s, bool global) +{ + //printf("defining mapping for printer font %s\n",psname.latin1()); + downloadMapping( s, global ); +} + +void TQPSPrinterFontPrivate::downloadMapping( TQTextStream &s, bool global ) +{ + uchar rangeOffset = 0; + uchar numRanges = (uchar)(subsetCount/256 + 1); + uchar range; + TQMap *subsetDict = ⊂ + if ( !global ) { + rangeOffset = numRanges; + numRanges = pageSubsetCount/256 + 1; + subsetDict = &page_subset; + } + // build up inverse table + unsigned short *inverse = new unsigned short[numRanges * 256]; + memset( inverse, 0, numRanges * 256 * sizeof( unsigned short ) ); + + TQMap::iterator it; + for ( it = subsetDict->begin(); it != subsetDict->end(); ++it) { + const unsigned short &mapped = *it; + inverse[mapped] = it.key(); + } + + TQString vector; + TQString glyphname; + + for (range=0; range < numRanges; range++) { + //printf("outputing range %04x\n",range*256); + vector = "%% Font Page "; + vector += toHex((uchar)(range + rangeOffset)); + vector += "\n/"; + vector += psname; + vector += "-ENC-"; + vector += toHex((uchar)(range + rangeOffset)); + vector += " [\n"; + + TQString line; + for(int k=0; k<256; k++ ) { + int c = range*256 + k; + unsigned short glyph = inverse[c]; + glyphname = glyphName( glyph ); + if ( line.length() + glyphname.length() > 76 ) { + vector += line; + vector += "\n"; + line = ""; + } + line += "/" + glyphname; + } + vector += line; + vector += "] def\n"; + s << vector; + } + + delete [] inverse; + + // DEFINE BASE FONTS + + for (range=0; range < numRanges; range++) { + s << "/"; + s << psname; + s << "-Uni-"; + s << toHex((uchar)(range + rangeOffset)); + s << " "; + s << psname; + s << "-ENC-"; + s << toHex((uchar)(range + rangeOffset)); + if ( embedded() && embedFonts ) { + s << " /"; + s << psname; + s << " MFEmb\n"; + } else { + s << " " << psname << "List"; + s << " MF\n"; + } + } + + // === write header === + // int VMMin; + // int VMMax; + + s << wrapDSC( "%%BeginFont: " + psname ) + << "%!PS-AdobeFont-1.0 Composite Font\n" + << wrapDSC( "%%FontName: " + psname + "-Uni" ) + << "%%Creator: Composite font created by TQt\n"; + + /* Start the dictionary which will eventually */ + /* become the font. */ + s << "25 dict begin\n"; // need to verify. Sivan + + s << "/FontName /"; + s << psname; + s << "-Uni"; + s << " def\n"; + s << "/PaintType 0 def\n"; + + // This is concatenated with the base fonts, so it should perform + // no transformation. Sivan + s << "/FontMatrix[1 0 0 1 0 0]def\n"; + + s << "/FontType "; + s << 0; + s << " def\n"; + + // now come composite font structures + // FMapTypes: + // 2: 8/8, 8 bits select the font, 8 the glyph + + s << "/FMapType 2 def\n"; + + // The encoding in a composite font is used for indirection. + // Every char is split into a font-number and a character-selector. + // PostScript prints glyph number character-selector from the font + // FDepVector[ Encoding[ font-number ] ]. + + s << "/Encoding ["; + for (range=0; range < rangeOffset + numRanges; range++) { + if (range % 16 == 0) + s << "\n"; + else + s << " "; + s << range; + } + s << "]def\n"; + + // Descendent fonts + + s << "/FDepVector [\n"; + for (range=0; range < rangeOffset + numRanges; range++) { + s << "/"; + s << psname; + s << "-Uni-"; + s << toHex( range ); + s << " findfont\n"; + } + s << "]def\n"; + + // === trailer === + + s << "FontName currentdict end definefont pop\n"; + s << "%%EndFont\n"; +} + + +// ================== TTF ==================== + +typedef Q_UINT8 BYTE; +typedef Q_UINT16 USHORT; +typedef Q_UINT16 uFWord; +typedef Q_INT16 SHORT; +typedef Q_INT16 FWord; +typedef Q_UINT32 ULONG; +typedef Q_INT32 FIXED; + +typedef struct { + Q_INT16 whole; + Q_UINT16 fraction; +} Fixed; // 16.16 bit fixed-point number + +static float f2dot14( ushort s ) +{ + float f = ((float)( s & 0x3fff ))/ 16384.; + f += (s & 0x8000) ? ( (s & 0x4000) ? -1 : -2 ) : ( (s & 0x4000) ? 1 : 0 ); + return f; +} + +typedef struct { + int* epts_ctr; /* array of contour endpoints */ + int num_pts, num_ctr; /* number of points, number of coutours */ + FWord* xcoor, *ycoor; /* arrays of x and y coordinates */ + BYTE* tt_flags; /* array of TrueType flags */ + double* area_ctr; + char* check_ctr; + int* ctrset; /* in contour index followed by out contour index */ +} charproc_data; + + +class TQPSPrinterFontTTF + : public TQPSPrinterFontPrivate { +public: + TQPSPrinterFontTTF(const TQFontEngine *f, TQByteArray& data); + virtual void download(TQTextStream& s, bool global); + virtual void drawText( TQTextStream &stream, const TQPoint &p, TQTextEngine *engine, int item, + const TQString &text, TQPSPrinterPrivate *d, TQPainter *paint); + // virtual ~TQPSPrinterFontTTF(); + + virtual bool embedded() { return TRUE; } +private: + TQByteArray data; + TQMemArray uni2glyph; // to speed up lookups + TQMemArray glyph2uni; // to speed up lookups + bool defective; // if we can't process this file + + BYTE* getTable(const char *); + void uni2glyphSetup(); + unsigned short unicode_for_glyph(int glyphindex); + unsigned short glyph_for_unicode(unsigned short unicode); + int topost(FWord x) { return (int)( ((int)(x) * 1000 + HUPM) / unitsPerEm ); } + +#ifdef Q_PRINTER_USE_TYPE42 + void sfnts_pputBYTE(BYTE n,TQTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_pputUSHORT(USHORT n,TQTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_pputULONG(ULONG n,TQTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_end_string(TQTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_new_table(ULONG length,TQTextStream& s, + int& string_len, int& line_len, bool& in_string); + void sfnts_glyf_table(ULONG oldoffset, + ULONG correct_total_length, + TQTextStream& s, + int& string_len, int& line_len, bool& in_string); + void download_sfnts(TQTextStream& s); +#endif + + void subsetGlyph(int charindex,bool* glyphset); + + void charproc(int charindex, TQTextStream& s, bool *glyphSet); + BYTE* charprocFindGlyphData(int charindex); + void charprocComposite(BYTE *glyph, TQTextStream& s, bool *glyphSet); + void charprocLoad(BYTE *glyph, charproc_data* cd); + + int target_type; /* 42 or 3 */ + + int numTables; /* number of tables present */ + TQString PostName; /* Font's PostScript name */ + TQString FullName; /* Font's full name */ + TQString FamilyName; /* Font's family name */ + TQString Style; /* Font's style string */ + TQString Copyright; /* Font's copyright string */ + TQString Version; /* Font's version string */ + TQString Trademark; /* Font's trademark string */ + int llx,lly,urx,ury; /* bounding box */ + + Fixed TTVersion; /* Truetype version number from offset table */ + Fixed MfrRevision; /* Revision number of this font */ + + BYTE *offset_table; /* Offset table in memory */ + BYTE *post_table; /* 'post' table in memory */ + + BYTE *loca_table; /* 'loca' table in memory */ + BYTE *glyf_table; /* 'glyf' table in memory */ + BYTE *hmtx_table; /* 'hmtx' table in memory */ + + USHORT numberOfHMetrics; + int unitsPerEm; /* unitsPerEm converted to int */ + int HUPM; /* half of above */ + + int numGlyphs; /* from 'post' table */ + + int indexToLocFormat; /* short or long offsets */ + +}; + + +static ULONG getULONG(BYTE *p) +{ + int x; + ULONG val=0; + + for(x=0; x<4; x++) { + val *= 0x100; + val += p[x]; + } + + return val; +} + +static USHORT getUSHORT(BYTE *p) +{ + int x; + USHORT val=0; + + for(x=0; x<2; x++) { + val *= 0x100; + val += p[x]; + } + + return val; +} + +static Fixed getFixed(BYTE *s) +{ + Fixed val={0,0}; + + val.whole = ((s[0] * 256) + s[1]); + val.fraction = ((s[2] * 256) + s[3]); + + return val; +} + +static FWord getFWord(BYTE* s) { return (FWord) getUSHORT(s); } +static uFWord getuFWord(BYTE* s) { return (uFWord) getUSHORT(s); } +static SHORT getSHORT(BYTE* s) { return (SHORT) getUSHORT(s); } + +#if 0 +static const char * const Apple_CharStrings[]={ + ".notdef",".null","nonmarkingreturn","space","exclam","quotedbl","numbersign", + "dollar","percent","ampersand","quotesingle","parenleft","parenright", + "asterisk","plus", "comma","hyphen","period","slash","zero","one","two", + "three","four","five","six","seven","eight","nine","colon","semicolon", + "less","equal","greater","question","at","A","B","C","D","E","F","G","H","I", + "J","K", "L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", + "bracketleft","backslash","bracketright","asciicircum","underscore","grave", + "a","b","c","d","e","f","g","h","i","j","k", "l","m","n","o","p","q","r","s", + "t","u","v","w","x","y","z","braceleft","bar","braceright","asciitilde", + "Adieresis","Aring","Ccedilla","Eacute","Ntilde","Odieresis","Udieresis", + "aacute","agrave","acircumflex","adieresis","atilde","aring","ccedilla", + "eacute","egrave","ecircumflex","edieresis","iacute","igrave","icircumflex", + "idieresis","ntilde","oacute","ograve","ocircumflex","odieresis","otilde", + "uacute","ugrave","ucircumflex","udieresis","dagger","degree","cent", + "sterling","section","bullet","paragraph","germandbls","registered", + "copyright","trademark","acute","dieresis","notequal","AE","Oslash", + "infinity","plusminus","lessequal","greaterequal","yen","mu","partialdiff", + "summation","product","pi","integral","ordfeminine","ordmasculine","Omega", + "ae","oslash","questiondown","exclamdown","logicalnot","radical","florin", + "approxequal","Delta","guillemotleft","guillemotright","ellipsis", + "nobreakspace","Agrave","Atilde","Otilde","OE","oe","endash","emdash", + "quotedblleft","quotedblright","quoteleft","quoteright","divide","lozenge", + "ydieresis","Ydieresis","fraction","currency","guilsinglleft","guilsinglright", + "fi","fl","daggerdbl","periodcentered","quotesinglbase","quotedblbase", + "perthousand","Acircumflex","Ecircumflex","Aacute","Edieresis","Egrave", + "Iacute","Icircumflex","Idieresis","Igrave","Oacute","Ocircumflex","apple", + "Ograve","Uacute","Ucircumflex","Ugrave","dotlessi","circumflex","tilde", + "macron","breve","dotaccent","ring","cedilla","hungarumlaut","ogonek","caron", + "Lslash","lslash","Scaron","scaron","Zcaron","zcaron","brokenbar","Eth","eth", + "Yacute","yacute","Thorn","thorn","minus","multiply","onesuperior", + "twosuperior","threesuperior","onehalf","onequarter","threequarters","franc", + "Gbreve","gbreve","Idot","Scedilla","scedilla","Cacute","cacute","Ccaron", + "ccaron","dmacron","markingspace","capslock","shift","propeller","enter", + "markingtabrtol","markingtabltor","control","markingdeleteltor", + "markingdeletertol","option","escape","parbreakltor","parbreakrtol", + "newpage","checkmark","linebreakltor","linebreakrtol","markingnobreakspace", + "diamond","appleoutline"}; +#endif + +// #define DEBUG_TRUETYPE + +TQPSPrinterFontTTF::TQPSPrinterFontTTF(const TQFontEngine *f, TQByteArray& d) +{ + data = d; + defective = FALSE; + + BYTE *ptr; + + target_type = 3; // will work on any printer + //target_type = 42; // works with gs, faster, better quality + +#ifdef Q_PRINTER_USE_TYPE42 + char* environment_preference = getenv("QT_TTFTOPS"); + if (environment_preference) { + if (TQString(environment_preference) == "42") + target_type = 42; + else if (TQString(environment_preference) == "3") + target_type = 3; + else + qWarning("The value of QT_TTFTOPS must be 42 or 3"); + } +#endif + offset_table = (unsigned char*) data.data(); /* first 12 bytes */ + + /* Determine how many directory entries there are. */ + numTables = getUSHORT( offset_table + 4 ); + + /* Extract information from the "Offset" table. */ + TTVersion = getFixed( offset_table ); + + /* Load the "head" table and extract information from it. */ + ptr = getTable("head"); + if ( !ptr ) { + defective = TRUE; + return; + } + MfrRevision = getFixed( ptr + 4 ); /* font revision number */ + unitsPerEm = getUSHORT( ptr + 18 ); + HUPM = unitsPerEm / 2; +#ifdef DEBUG_TRUETYPE + printf("unitsPerEm=%d",(int)unitsPerEm); +#endif + llx = topost( getFWord( ptr + 36 ) ); /* bounding box info */ + lly = topost( getFWord( ptr + 38 ) ); + urx = topost( getFWord( ptr + 40 ) ); + ury = topost( getFWord( ptr + 42 ) ); + indexToLocFormat = getSHORT( ptr + 50 ); /* size of 'loca' data */ + if(indexToLocFormat != 0 && indexToLocFormat != 1) { + qWarning("TrueType font is unusable because indexToLocFormat != 0"); + defective = TRUE; + return; + } + if( getSHORT(ptr+52) != 0 ) { + qWarning("TrueType font is unusable because glyphDataFormat != 0"); + defective = TRUE; + return; + } + + // === Load information from the "name" table === + + /* Set default values to avoid future references to */ + /* undefined pointers. */ + + psname = FullName = FamilyName = Version = Style = "unknown"; + Copyright = "No copyright notice"; + Trademark = "No trademark notice"; + + BYTE* table_ptr = getTable("name"); /* pointer to table */ + if ( !table_ptr ) { + defective = TRUE; + qDebug("couldn't find name table" ); + return; + } + int numrecords = getUSHORT( table_ptr + 2 ); /* number of names */ + char* strings = (char *)table_ptr + getUSHORT( table_ptr + 4 ); /* start of string storage */ + + BYTE* ptr2 = table_ptr + 6; + for(int x=0; x < numrecords; x++,ptr2+=12) { + int platform = getUSHORT(ptr2); + //int encoding = getUSHORT(ptr2+2); + //int language = getUSHORT(ptr2+4); + int nameid = getUSHORT(ptr2+6); + int length = getUSHORT(ptr2+8); + int offset = getUSHORT(ptr2+10); + + if( platform == 1 && nameid == 0 ) + Copyright.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 1 ) + FamilyName.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 2 ) + Style.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 4 ) + FullName.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 5 ) + Version.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 6 ) + psname.setLatin1(strings+offset,length); + + if( platform == 1 && nameid == 7 ) + Trademark.setLatin1(strings+offset,length); + + } + psname.replace(' ', '-'); + psname.replace("/", ""); + if (psname.isEmpty()) + psname = makePSFontName(f); + + //read_cmap(font); + + /* We need to have the PostScript table around. */ + + post_table = getTable("post"); +#if 0 + if ( post_table ) { + Fixed post_format = getFixed( post_table ); + + if( post_format.whole != 2 || post_format.fraction != 0 ) { + qWarning("TrueType font does not have a format 2.0 'post' table"); + qWarning("post format is %d.%d",post_format.whole,post_format.fraction); + // Sivan Feb 2001: no longer defective. + // defective = TRUE; + } + } +#endif + BYTE *maxp = getTable("maxp"); + if ( !maxp ) { + defective = TRUE; + qDebug("no maxp table in font"); + return; + } + numGlyphs = getUSHORT( maxp + 4 ); +// qDebug("number of glyphs is %d", numGlyphs); + replacementList = makePSFontNameList( f, psname ); + uni2glyphSetup(); +} + + +void TQPSPrinterFontTTF::drawText( TQTextStream &stream, const TQPoint &p, TQTextEngine *engine, int item, + const TQString &text, TQPSPrinterPrivate *d, TQPainter *paint) +{ + // we draw glyphs here to get correct shaping of arabic and indic + TQScriptItem &si = engine->items[item]; + engine->shape( item ); + int len = si.num_glyphs; + + int x = p.x() + si.x; + int y = p.y() + si.y; + if ( y != d->textY || d->textY == 0 ) + stream << y << " Y"; + d->textY = y; + + TQCString xyarray; + int xo = 0; + int yo = 0; + + glyph_t *glyphs = engine->glyphs( &si ); + advance_t *advances = engine->advances( &si ); + qoffset_t *offsets = engine->offsets( &si ); +#ifdef Q_WS_X11 + int type = si.fontEngine->type(); + bool glyphIndices = (type == TQFontEngine::Xft); + // This helps us get arabic for XLFD fonts working. In that case we have a Unicode + // cmap (== 0), and the glyphs array contains the shaped string. + bool useGlyphAsUnicode = (type == TQFontEngine::XLFD && si.fontEngine->cmap() == 0); +#else // Q_WS_QWS + const bool glyphIndices = FALSE; + const bool useGlyphAsUnicode = TRUE; +#endif + stream << "<"; + if ( si.analysis.bidiLevel % 2 ) { + for ( int i = len-1; i >=0; i-- ) { + // map unicode is not really the correct name, as we map glyphs, but we also download glyphs, so this works + unsigned short glyph; + if (glyphIndices) + glyph = glyphs[i]; + else + glyph = glyph_for_unicode(useGlyphAsUnicode ? glyphs[i] : text.unicode()[i].unicode()); + stream << toHex(mapUnicode(glyph)); + if ( i != len-1 ) { + xyarray += toInt( xo + offsets[i].x + advances[i+1] ); + xyarray += " "; + xyarray += toInt( yo + offsets[i].y ); + xyarray += " "; + xo = -offsets[i].x; + yo = -offsets[i].y; + } + } + } else { + for ( int i = 0; i < len; i++ ) { + // map unicode is not really the correct name, as we map glyphs, but we also download glyphs, so this works + unsigned short glyph; + if (glyphIndices) + glyph = glyphs[i]; + else + glyph = glyph_for_unicode(useGlyphAsUnicode ? glyphs[i] : text.unicode()[i].unicode()); + stream << toHex(mapUnicode(glyph)); + if ( i ) { + xyarray += toInt( xo + offsets[i].x + advances[i-1] ); + xyarray += " "; + xyarray += toInt( yo + offsets[i].y ); + xyarray += " "; + xo = -offsets[i].x; + yo = -offsets[i].y; + } + } + } + stream << ">"; + + stream << "[" << xyarray << "0 0]"; + stream << si.width << " " << x; + + if ( paint->font().underline() ) + stream << ' ' << y + d->fm.underlinePos() + d->fm.lineWidth() + << " " << d->fm.lineWidth() << " Tl"; + if ( paint->font().strikeOut() ) + stream << ' ' << y + d->fm.strikeOutPos() + << " " << d->fm.lineWidth() << " Tl"; + stream << " XYT\n"; + +} + + +void TQPSPrinterFontTTF::download(TQTextStream& s,bool global) +{ + emitPSFontNameList( s, psname, replacementList); + if ( !embedFonts ) { + downloadMapping(s, global); + return; + } + + //qDebug("downloading ttf font %s", psname.latin1() ); + //qDebug("target type=%d", target_type); + global_dict = global; + TQMap *subsetDict = ⊂ + if ( !global ) + subsetDict = &page_subset; + + downloaded = TRUE; + + if (defective) { + s << "% Font "; + s << FullName; + s << " cannot be downloaded\n"; + return; + } + + // === write header === + int VMMin; + int VMMax; + + s << wrapDSC( "%%BeginFont: " + FullName ); + if( target_type == 42 ) { + s << "%!PS-TrueTypeFont-" + << TTVersion.whole + << "." + << TTVersion.fraction + << "-" + << MfrRevision.whole + << "." + << MfrRevision.fraction + << "\n"; + } else { + /* If it is not a Type 42 font, we will use a different format. */ + s << "%!PS-Adobe-3.0 Resource-Font\n"; + } /* See RBIIp 641 */ + + if( Copyright != (char*)NULL ) { + s << wrapDSC( "%%Copyright: " + Copyright ); + } + + if( target_type == 42 ) + s << "%%Creator: Converted from TrueType to type 42 by TQt\n"; + else + s << "%%Creator: Converted from TrueType by TQt\n"; + + /* If VM usage information is available, print it. */ + if( target_type == 42 && post_table) + { + VMMin = (int)getULONG( post_table + 16 ); + VMMax = (int)getULONG( post_table + 20 ); + if( VMMin > 0 && VMMax > 0 ) + s << "%%VMUsage: " << VMMin << " " << VMMax << "\n"; + } + + /* Start the dictionary which will eventually */ + /* become the font. */ + if( target_type != 3 ) { + s << "15 dict begin\n"; + } else { + s << "25 dict begin\n"; + + /* Type 3 fonts will need some subroutines here. */ + s << "/_d{bind def}bind def\n"; + s << "/_m{moveto}_d\n"; + s << "/_l{lineto}_d\n"; + s << "/_cl{closepath eofill}_d\n"; + s << "/_c{curveto}_d\n"; + s << "/_sc{7 -1 roll{setcachedevice}{pop pop pop pop pop pop}ifelse}_d\n"; + s << "/_e{exec}_d\n"; + } + + s << "/FontName /"; + s << psname; + s << " def\n"; + s << "/PaintType 0 def\n"; + + if(target_type == 42) + s << "/FontMatrix[1 0 0 1 0 0]def\n"; + else + s << "/FontMatrix[.001 0 0 .001 0 0]def\n"; + + s << "/FontBBox["; + s<< llx; + s << " "; + s<< lly; + s << " "; + s<< urx; + s << " "; + s<< ury; + s << "]def\n"; + + s << "/FontType "; + s<< target_type; + s << " def\n"; + + // === write encoding === + + s << "/Encoding StandardEncoding def\n"; + + // === write fontinfo dict === + + /* We create a sub dictionary named "FontInfo" where we */ + /* store information which though it is not used by the */ + /* interpreter, is useful to some programs which will */ + /* be printing with the font. */ + s << "/FontInfo 10 dict dup begin\n"; + + /* These names come from the TrueType font's "name" table. */ + s << "/FamilyName ("; + s << FamilyName; + s << ") def\n"; + + s << "/FullName ("; + s << FullName; + s << ") def\n"; + + s << "/Notice ("; + s << Copyright; + s << " "; + s << Trademark; + s << ") def\n"; + + /* This information is not tquite correct. */ + s << "/Weight ("; + s << Style; + s << ") def\n"; + + /* Some fonts have this as "version". */ + s << "/Version ("; + s << Version; + s << ") def\n"; + + /* Some information from the "post" table. */ + if ( post_table ) { + Fixed ItalicAngle = getFixed( post_table + 4 ); + s << "/ItalicAngle "; + s << ItalicAngle.whole; + s << "."; + s << ItalicAngle.fraction; + s << " def\n"; + + s << "/isFixedPitch "; + s << (getULONG( post_table + 12 ) ? "true" : "false" ); + s << " def\n"; + + s << "/UnderlinePosition "; + s << (int)getFWord( post_table + 8 ); + s << " def\n"; + + s << "/UnderlineThickness "; + s << (int)getFWord( post_table + 10 ); + s << " def\n"; + } + s << "end readonly def\n"; + +#ifdef Q_PRINTER_USE_TYPE42 + /* If we are generating a type 42 font, */ + /* emmit the sfnts array. */ + if( target_type == 42 ) + download_sfnts(s); +#endif + /* If we are generating a Type 3 font, we will need to */ + /* have the 'loca' and 'glyf' tables arround while */ + /* we are generating the CharStrings. */ + if(target_type == 3) + { + BYTE *ptr; /* We need only one value */ + ptr = getTable("hhea"); + numberOfHMetrics = getUSHORT(ptr + 34); + + loca_table = getTable("loca"); + glyf_table = getTable("glyf"); + hmtx_table = getTable("hmtx"); + } + + // === CharStrings array === + + // subsetting. We turn a char subset into a glyph subset + // and we mark as used the base glyphs of used composite glyphs. + + bool glyphset[65536]; + for(int c=0; c < 65536; c++) + glyphset[c] = FALSE; + glyphset[0] = TRUE; // always output .notdef + + TQMap::iterator it; + for( it = subsetDict->begin(); it != subsetDict->end(); ++it ) { + subsetGlyph( it.key(), glyphset ); + } + int nGlyphs = numGlyphs; + if ( target_type == 3 ) { + nGlyphs = 0;; + for(int c=0; c < 65536; c++) + if ( glyphset[c] ) nGlyphs++; + } + + s << "/CharStrings "; + s << nGlyphs; + s << " dict dup begin\n"; + + // Emmit one key-value pair for each glyph. + for(int x=0; x < 65536; x++) { + if(target_type == 42) { + s << "/"; + s << glyphName( x ); + s << " "; + s << x; + s << " def\n"; + } else { /* type 3 */ + if (!glyphset[x]) continue; + + //qDebug("emitting charproc for glyph %d, name=%s", x, glyphName(x).latin1() ); + s << "/"; + s << glyphName( x, glyphset ); + s << "{"; + charproc(x,s, glyphset); + s << "}_d\n"; /* "} bind def" */ + } + } + + s << "end readonly def\n"; + + // === trailer === + + /* If we are generating a type 3 font, we need to provide */ + /* a BuildGlyph and BuildChar proceedures. */ + if( target_type == 3 ) { + s << "\n"; + + s << "/BuildGlyph\n"; + s << " {exch begin\n"; /* start font dictionary */ + s << " CharStrings exch\n"; + s << " 2 copy known not{pop /.notdef}if\n"; + s << " true 3 1 roll get exec\n"; + s << " end}_d\n"; + + s << "\n"; + + /* This proceedure is for compatiblity with */ + /* level 1 interpreters. */ + s << "/BuildChar {\n"; + s << " 1 index /Encoding get exch get\n"; + s << " 1 index /BuildGlyph get exec\n"; + s << "}_d\n"; + + s << "\n"; + + } + + /* If we are generating a type 42 font, we need to check to see */ + /* if this PostScript interpreter understands type 42 fonts. If */ + /* it doesn't, we will hope that the Apple TrueType rasterizer */ + /* has been loaded and we will adjust the font accordingly. */ + /* I found out how to do this by examining a TrueType font */ + /* generated by a Macintosh. That is where the TrueType interpreter */ + /* setup instructions and part of BuildGlyph came from. */ + else if( target_type == 42 ) { + s << "\n"; + + /* If we have no "resourcestatus" command, or FontType 42 */ + /* is unknown, leave "true" on the stack. */ + s << "systemdict/resourcestatus known\n"; + s << " {42 /FontType resourcestatus\n"; + s << " {pop pop false}{true}ifelse}\n"; + s << " {true}ifelse\n"; + + /* If true, execute code to produce an error message if */ + /* we can't find Apple's TrueDict in VM. */ + s << "{/TrueDict where{pop}{(%%[ Error: no TrueType rasterizer ]%%)= flush}ifelse\n"; + + /* Since we are expected to use Apple's TrueDict TrueType */ + /* reasterizer, change the font type to 3. */ + s << "/FontType 3 def\n"; + + /* Define a string to hold the state of the Apple */ + /* TrueType interpreter. */ + s << " /TrueState 271 string def\n"; + + /* It looks like we get information about the resolution */ + /* of the printer and store it in the TrueState string. */ + s << " TrueDict begin sfnts save\n"; + s << " 72 0 matrix defaultmatrix dtransform dup\n"; + s << " mul exch dup mul add sqrt cvi 0 72 matrix\n"; + s << " defaultmatrix dtransform dup mul exch dup\n"; + s << " mul add sqrt cvi 3 -1 roll restore\n"; + s << " TrueState initer end\n"; + + /* This BuildGlyph procedure will look the name up in the */ + /* CharStrings array, and then check to see if what it gets */ + /* is a procedure. If it is, it executes it, otherwise, it */ + /* lets the TrueType rasterizer loose on it. */ + + /* When this proceedure is executed the stack contains */ + /* the font dictionary and the character name. We */ + /* exchange arguments and move the dictionary to the */ + /* dictionary stack. */ + s << " /BuildGlyph{exch begin\n"; + /* stack: charname */ + + /* Put two copies of CharStrings on the stack and consume */ + /* one testing to see if the charname is defined in it, */ + /* leave the answer on the stack. */ + s << " CharStrings dup 2 index known\n"; + /* stack: charname CharStrings bool */ + + /* Exchange the CharStrings dictionary and the charname, */ + /* but if the answer was false, replace the character name */ + /* with ".notdef". */ + s << " {exch}{exch pop /.notdef}ifelse\n"; + /* stack: CharStrings charname */ + + /* Get the value from the CharStrings dictionary and see */ + /* if it is executable. */ + s << " get dup xcheck\n"; + /* stack: CharStrings_entry */ + + /* If is a proceedure. Execute according to RBIIp 277-278. */ + s << " {currentdict systemdict begin begin exec end end}\n"; + + /* Is a TrueType character index, let the rasterizer at it. */ + s << " {TrueDict begin /bander load cvlit exch TrueState render end}\n"; + + s << " ifelse\n"; + + /* Pop the font's dictionary off the stack. */ + s << " end}bind def\n"; + + /* This is the level 1 compatibility BuildChar procedure. */ + /* See RBIIp 281. */ + s << " /BuildChar{\n"; + s << " 1 index /Encoding get exch get\n"; + s << " 1 index /BuildGlyph get exec\n"; + s << " }bind def\n"; + + /* Here we close the condition which is true */ + /* if the printer has no built-in TrueType */ + /* rasterizer. */ + s << "}if\n"; + s << "\n"; + } /* end of if Type 42 not understood. */ + + s << "FontName currentdict end definefont pop\n"; + + downloadMapping(s, global); + s << "%%EndFont\n"; +} + +BYTE* TQPSPrinterFontTTF::getTable(const char* name) +{ + BYTE *ptr; + int x; + + /* We must search the table directory. */ + ptr = offset_table + 12; + x=0; + while (x != numTables) { + if( strncmp((const char *)ptr,name,4) == 0 ) { + ULONG offset; + //ULONG length; + BYTE *table; + + offset = getULONG( ptr + 8 ); + //length = getULONG( ptr + 12 ); + + table = offset_table + offset; + return table; + } + + x++; + ptr += 16; + } + + return 0; +} + +void TQPSPrinterFontTTF::uni2glyphSetup() +{ + uni2glyph.resize(65536); + int i; + for (i=0; i<65536; i++) uni2glyph[i] = 0x0000; + glyph2uni.resize(65536); + for (i=0; i<65536; i++) glyph2uni[i] = 0x0000; + + unsigned char* cmap = getTable("cmap"); + int pos = 0; + + //USHORT version = getUSHORT(cmap + pos); + pos += 2; + USHORT nmaps = getUSHORT(cmap + pos); pos += 2; + + //fprintf(stderr,"cmap version %d (should be 0), %d maps\n",version,nmaps); + + ULONG offset = 0; + int map = -1; + bool symbol = TRUE; + for (i=0; i 70) { + s << "\n"; + line_len=0; + } +} + +// Write a USHORT as a hexadecimal value as part of the sfnts array. + +void TQPSPrinterFontTTF::sfnts_pputUSHORT(USHORT n,TQTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + sfnts_pputBYTE(n / 256,s, string_len, line_len, in_string); + sfnts_pputBYTE(n % 256,s, string_len, line_len, in_string); +} + + +// Write a ULONG as part of the sfnts array. + +void TQPSPrinterFontTTF::sfnts_pputULONG(ULONG n,TQTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + int x1 = n % 256; n /= 256; + int x2 = n % 256; n /= 256; + int x3 = n % 256; n /= 256; + + sfnts_pputBYTE(n,s , string_len, line_len, in_string); + sfnts_pputBYTE(x3,s, string_len, line_len, in_string); + sfnts_pputBYTE(x2,s, string_len, line_len, in_string); + sfnts_pputBYTE(x1,s, string_len, line_len, in_string); +} + +/* +** This is called whenever it is +** necessary to end a string in the sfnts array. +** +** (The array must be broken into strings which are +** no longer than 64K characters.) +*/ +void TQPSPrinterFontTTF::sfnts_end_string(TQTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + if(in_string) { + string_len=0; /* fool sfnts_pputBYTE() */ + + // s << "\n% dummy byte:\n"; + + // extra byte for pre-2013 compatibility + sfnts_pputBYTE(0, s, string_len, line_len, in_string); + + s << ">"; + line_len++; + } + + in_string=FALSE; +} + +/* +** This is called at the start of each new table. +** The argement is the length in bytes of the table +** which will follow. If the new table will not fit +** in the current string, a new one is started. +*/ +void TQPSPrinterFontTTF::sfnts_new_table(ULONG length,TQTextStream& s, + int& string_len, int& line_len, bool& in_string) +{ + if( (string_len + length) > 65528 ) + sfnts_end_string(s, string_len, line_len, in_string); +} + +/* +** We may have to break up the 'glyf' table. That is the reason +** why we provide this special routine to copy it into the sfnts +** array. +*/ +void TQPSPrinterFontTTF::sfnts_glyf_table(ULONG oldoffset, + ULONG correct_total_length, + TQTextStream& s, + int& string_len, int& line_len, bool& in_string) + +{ + int x; + ULONG off; + ULONG length; + int c; + ULONG total=0; /* running total of bytes written to table */ + + loca_table = getTable("loca"); + + int font_off = oldoffset; + + /* Copy the glyphs one by one */ + for(x=0; x < numGlyphs; x++) { + /* Read the glyph offset from the index-to-location table. */ + if(indexToLocFormat == 0) { + off = getUSHORT( loca_table + (x * 2) ); + off *= 2; + length = getUSHORT( loca_table + ((x+1) * 2) ); + length *= 2; + length -= off; + } else { + off = getULONG( loca_table + (x * 4) ); + length = getULONG( loca_table + ((x+1) * 4) ); + length -= off; + } + + // fprintf(stderr,"glyph length=%d",(int)length); + + /* Start new string if necessary. */ + sfnts_new_table( (int)length, s, string_len, line_len, in_string ); + + /* + ** Make sure the glyph is padded out to a + ** two byte boundary. + */ + if( length % 2 ) { + qWarning("TrueType font contains a 'glyf' table without 2 byte padding"); + defective = TRUE; + return; + } + + /* Copy the bytes of the glyph. */ + while( length-- ) { + c = offset_table[ font_off ]; + font_off++; + + sfnts_pputBYTE(c, s, string_len, line_len, in_string); + total++; /* add to running total */ + } + } + + /* Pad out to full length from table directory */ + while( total < correct_total_length ) { + sfnts_pputBYTE(0, s, string_len, line_len, in_string); + total++; + } + + /* Look for unexplainable descrepancies between sizes */ + if( total != correct_total_length ) { + qWarning("TQPSPrinterFontTTF::sfnts_glyf_table: total != correct_total_length"); + defective = TRUE; + return; + } +} + +/* +** Here is the routine which ties it all together. +** +** Create the array called "sfnts" which +** holds the actual TrueType data. +*/ + +void TQPSPrinterFontTTF::download_sfnts(TQTextStream& s) +{ + // tables worth including in a type 42 font + char *table_names[]= { + "cvt ", + "fpgm", + "glyf", + "head", + "hhea", + "hmtx", + "loca", + "maxp", + "prep" + }; + + struct { /* The location of each of */ + ULONG oldoffset; /* the above tables. */ + ULONG newoffset; + ULONG length; + ULONG checksum; + } tables[9]; + + int c; /* Input character. */ + int diff; + int count; /* How many `important' tables did we find? */ + + BYTE* ptr = offset_table + 12; // original table directory + ULONG nextoffset=0; + count=0; + + /* + ** Find the tables we want and store there vital + ** statistics in tables[]. + */ + for(int x=0; x < 9; x++ ) { + do { + diff = strncmp( (char*)ptr, table_names[x], 4 ); + + if( diff > 0 ) { /* If we are past it. */ + tables[x].length = 0; + diff = 0; + } + else if( diff < 0 ) { /* If we haven't hit it yet. */ + ptr += 16; + } + else if( diff == 0 ) { /* Here it is! */ + tables[x].newoffset = nextoffset; + tables[x].checksum = getULONG( ptr + 4 ); + tables[x].oldoffset = getULONG( ptr + 8 ); + tables[x].length = getULONG( ptr + 12 ); + nextoffset += ( ((tables[x].length + 3) / 4) * 4 ); + count++; + ptr += 16; + } + } while(diff != 0); + } /* end of for loop which passes over the table directory */ + + /* Begin the sfnts array. */ + + s << "/sfnts[<"; + + bool in_string=TRUE; + int string_len=0; + int line_len=8; + + /* Generate the offset table header */ + /* Start by copying the TrueType version number. */ + ptr = offset_table; + for(int x=0; x < 4; x++) + sfnts_pputBYTE( *(ptr++) , s, string_len, line_len, in_string ); + + /* Now, generate those silly numTables numbers. */ + sfnts_pputUSHORT(count,s, string_len, line_len, in_string); /* number of tables */ + if( count == 9 ) { + sfnts_pputUSHORT(7,s, string_len, line_len, in_string); /* searchRange */ + sfnts_pputUSHORT(3,s, string_len, line_len, in_string); /* entrySelector */ + sfnts_pputUSHORT(81,s, string_len, line_len, in_string); /* rangeShift */ + } + else { + qWarning("Fewer than 9 tables selected"); + } + + /* Now, emmit the table directory. */ + for(int x=0; x < 9; x++) { + if( tables[x].length == 0 ) /* Skip missing tables */ + continue; + + /* Name */ + sfnts_pputBYTE( table_names[x][0], s, string_len, line_len, in_string); + sfnts_pputBYTE( table_names[x][1], s, string_len, line_len, in_string); + sfnts_pputBYTE( table_names[x][2], s, string_len, line_len, in_string); + sfnts_pputBYTE( table_names[x][3], s, string_len, line_len, in_string); + + /* Checksum */ + sfnts_pputULONG( tables[x].checksum, s, string_len, line_len, in_string ); + + /* Offset */ + sfnts_pputULONG( tables[x].newoffset + 12 + (count * 16), s, + string_len, line_len, in_string ); + + /* Length */ + sfnts_pputULONG( tables[x].length, s, + string_len, line_len, in_string ); + } + + /* Now, send the tables */ + for(int x=0; x < 9; x++) { + if( tables[x].length == 0 ) /* skip tables that aren't there */ + continue; + + /* 'glyf' table gets special treatment */ + if( strcmp(table_names[x],"glyf")==0 ) { + sfnts_glyf_table(tables[x].oldoffset,tables[x].length, s, + string_len, line_len, in_string); + } else { // other tables should not exceed 64K (not always true; Sivan) + if( tables[x].length > 65535 ) { + qWarning("TrueType font has a table which is too long"); + defective = TRUE; + return; + } + + /* Start new string if necessary. */ + sfnts_new_table(tables[x].length, s, + string_len, line_len, in_string); + + int font_off = tables[x].oldoffset; + /* Copy the bytes of the table. */ + for( int y=0; y < (int)tables[x].length; y++ ) { + c = offset_table[ font_off ]; + font_off++; + + sfnts_pputBYTE(c, s, string_len, line_len, in_string); + } + } + + /* Padd it out to a four byte boundary. */ + int y=tables[x].length; + while( (y % 4) != 0 ) { + sfnts_pputBYTE(0, s, string_len, line_len, in_string); + y++; + } + + } /* End of loop for all tables */ + + /* Close the array. */ + sfnts_end_string(s, string_len, line_len, in_string); + s << "]def\n"; +} +#endif + +// ****************** Type 3 CharProcs ******* + +/* +** This routine is used to break the character +** procedure up into a number of smaller +** procedures. This is necessary so as not to +** overflow the stack on certain level 1 interpreters. +** +** Prepare to push another item onto the stack, +** starting a new proceedure if necessary. +** +** Not all the stack depth calculations in this routine +** are perfectly accurate, but they do the job. +*/ +static int stack_depth = 0; +static void stack(int num_pts, int newnew, TQTextStream& s) +{ + if( num_pts > 25 ) { /* Only do something of we will */ + /* have a log of points. */ + if(stack_depth == 0) { + s << "{"; + stack_depth=1; + } + + stack_depth += newnew; /* Account for what we propose to add */ + + if(stack_depth > 100) { + s << "}_e{"; + stack_depth = 3 + newnew; /* A rough estimate */ + } + } +} + +static void stack_end(TQTextStream& s) /* called at end */ +{ + if(stack_depth) { + s << "}_e"; + stack_depth=0; + } +} + +// postscript drawing commands + +static void PSMoveto(FWord x, FWord y, TQTextStream& ts) +{ + ts << x; + ts << " "; + ts << y; + ts << " _m\n"; +} + +static void PSLineto(FWord x, FWord y, TQTextStream& ts) +{ + ts << x; + ts << " "; + ts << y; + ts << " _l\n"; +} + +/* Emmit a PostScript "curveto" command. */ +static void PSCurveto(FWord* xcoor, FWord* ycoor, + FWord x, FWord y, int s, int t, TQTextStream& ts) +{ + int N, i; + double sx[3], sy[3], cx[4], cy[4]; + + N = t-s+2; + for(i=0; inum_ctr; j++) + if (cd->check_ctr[j]==0 && cd->area_ctr[j] < 0) { + cd->check_ctr[j]=1; + return j; + } + + return NOMOREOUTCTR; +} /* end of nextoutctr() */ + +static int nextinctr(int co, int /*ci*/, charproc_data* cd) +{ + int j; + + for(j=0; jnum_ctr; j++) + if (cd->ctrset[2*j+1]==co) + if (cd->check_ctr[ cd->ctrset[2*j] ]==0) { + cd->check_ctr[ cd->ctrset[2*j] ]=1; + return cd->ctrset[2*j]; + } + + return NOMOREINCTR; +} + +static double intest( int co, int ci, charproc_data *cd ) +{ + int i, j, start, end; + double r1, r2; + FWord xi[3], yi[3]; + + j = start = ( co == 0 ) ? 0 : ( cd->epts_ctr[co - 1] + 1 ); + end = cd->epts_ctr[co]; + i = ( ci == 0 ) ? 0 : ( cd->epts_ctr[ci - 1] + 1 ); + xi[0] = cd->xcoor[i]; + yi[0] = cd->ycoor[i]; + r1 = sqr( cd->xcoor[start] - xi[0] ) + sqr( cd->ycoor[start] - yi[0] ); + + for ( i = start; i <= end; i++ ) { + r2 = sqr( cd->xcoor[i] - xi[0] ) + sqr( cd->ycoor[i] - yi[0] ); + if ( r2 < r1 ) { + r1 = r2; + j = i; + } + } + if ( j == start ) { + xi[1] = cd->xcoor[end]; + yi[1] = cd->ycoor[end]; + } else { + xi[1] = cd->xcoor[j - 1]; + yi[1] = cd->ycoor[j - 1]; + } + if ( j == end ) { + xi[2] = cd->xcoor[start]; + yi[2] = cd->ycoor[start]; + } else { + xi[2] = cd->xcoor[j + 1]; + yi[2] = cd->ycoor[j + 1]; + } + return area( xi, yi, 3 ); +} + +/* +** find the nearest out contour to a specified in contour. +*/ +static int nearout(int ci, charproc_data* cd) +{ + int k = 0; /* !!! is this right? */ + int co; + double a, a1=0; + + for (co=0; co < cd->num_ctr; co++) { + if(cd->area_ctr[co] < 0) { + a=intest(co,ci, cd); + if (a<0 && a1==0) { + k=co; + a1=a; + } + if(a<0 && a1!=0 && a>a1) { + k=co; + a1=a; + } + } + } + + return k; +} /* end of nearout() */ + + +/* +** We call this routine to emmit the PostScript code +** for the character we have loaded with load_char(). +*/ +static void PSConvert(TQTextStream& s, charproc_data* cd) +{ + int i,j,k,fst,start_offpt; + int end_offpt=0; + + cd->area_ctr = new double[cd->num_ctr]; + memset(cd->area_ctr, 0, (cd->num_ctr*sizeof(double))); + + cd->check_ctr = new char[cd->num_ctr]; + memset(cd->check_ctr, 0, (cd->num_ctr*sizeof(char))); + + cd->ctrset = new int[2*(cd->num_ctr)]; + memset(cd->ctrset, 0, (cd->num_ctr*2*sizeof(int))); + + cd->check_ctr[0]=1; + cd->area_ctr[0]=area(cd->xcoor, cd->ycoor, cd->epts_ctr[0]+1); + + for (i=1; inum_ctr; i++) + cd->area_ctr[i]=area(cd->xcoor+cd->epts_ctr[i-1]+1, + cd->ycoor+cd->epts_ctr[i-1]+1, + cd->epts_ctr[i]-cd->epts_ctr[i-1]); + + for (i=0; inum_ctr; i++) { + if (cd->area_ctr[i]>0) { + cd->ctrset[2*i]=i; + cd->ctrset[2*i+1]=nearout(i,cd); + } else { + cd->ctrset[2*i]=-1; + cd->ctrset[2*i+1]=-1; + } + } + + /* Step thru the coutours. */ + /* I believe that a contour is a detatched */ + /* set of curves and lines. */ + i=j=k=0; + while (i < cd->num_ctr ) { + fst = j = (k==0) ? 0 : (cd->epts_ctr[k-1]+1); + + /* Move to the first point on the contour. */ + stack(cd->num_pts,3,s); + PSMoveto(cd->xcoor[j],cd->ycoor[j],s); + start_offpt = 0; /* No off curve points yet. */ + + /* Step thru the remaining points of this contour. */ + for(j++; j <= cd->epts_ctr[k]; j++) { + if (!(cd->tt_flags[j]&1)) { /* Off curve */ + if (!start_offpt) + { start_offpt = end_offpt = j; } + else + end_offpt++; + } else { /* On Curve */ + if (start_offpt) { + stack(cd->num_pts,7,s); + PSCurveto(cd->xcoor,cd->ycoor, + cd->xcoor[j],cd->ycoor[j], + start_offpt,end_offpt,s); + start_offpt = 0; + } else { + stack(cd->num_pts,3,s); + PSLineto(cd->xcoor[j], cd->ycoor[j],s); + } + } + } + + /* Do the final curve or line */ + /* of this coutour. */ + if (start_offpt) { + stack(cd->num_pts,7,s); + PSCurveto(cd->xcoor,cd->ycoor, + cd->xcoor[fst],cd->ycoor[fst], + start_offpt,end_offpt,s); + } else { + stack(cd->num_pts,3,s); + PSLineto(cd->xcoor[fst],cd->ycoor[fst],s); + } + + k=nextinctr(i,k,cd); + + if (k==NOMOREINCTR) + i=k=nextoutctr(i,cd); + + if (i==NOMOREOUTCTR) + break; + } + + /* Now, we can fill the whole thing. */ + stack(cd->num_pts,1,s); + s << "_cl"; /* "closepath eofill" */ + + /* Free our work arrays. */ + delete [] cd->area_ctr; + delete [] cd->check_ctr; + delete [] cd->ctrset; +} + + +/* +** Load the simple glyph data pointed to by glyph. +** The pointer "glyph" should point 10 bytes into +** the glyph data. +*/ +void TQPSPrinterFontTTF::charprocLoad(BYTE *glyph, charproc_data* cd) +{ + int x; + BYTE c, ct; + + /* Read the contour endpoints list. */ + cd->epts_ctr = new int[cd->num_ctr]; + //cd->epts_ctr = (int *)myalloc(cd->num_ctr,sizeof(int)); + for (x = 0; x < cd->num_ctr; x++) { + cd->epts_ctr[x] = getUSHORT(glyph); + glyph += 2; + } + + /* From the endpoint of the last contour, we can */ + /* determine the number of points. */ + cd->num_pts = cd->epts_ctr[cd->num_ctr-1]+1; +#ifdef DEBUG_TRUETYPE + fprintf(stderr,"num_pts=%d\n",cd->num_pts); +#endif + + /* Skip the instructions. */ + x = getUSHORT(glyph); + glyph += 2; + glyph += x; + + /* Allocate space to hold the data. */ + //cd->tt_flags = (BYTE *)myalloc(num_pts,sizeof(BYTE)); + //cd->xcoor = (FWord *)myalloc(num_pts,sizeof(FWord)); + //cd->ycoor = (FWord *)myalloc(num_pts,sizeof(FWord)); + cd->tt_flags = new BYTE[cd->num_pts]; + cd->xcoor = new FWord[cd->num_pts]; + cd->ycoor = new FWord[cd->num_pts]; + + /* Read the flags array, uncompressing it as we go. */ + /* There is danger of overflow here. */ + for (x = 0; x < cd->num_pts; ) { + cd->tt_flags[x++] = c = *(glyph++); + + if (c&8) { /* If next byte is repeat count, */ + ct = *(glyph++); + + if( (x + ct) > cd->num_pts ) { + qWarning("Fatal Error in TT flags"); + return; + } + + while (ct--) + cd->tt_flags[x++] = c; + } + } + + /* Read the x coordinates */ + for (x = 0; x < cd->num_pts; x++) { + if (cd->tt_flags[x] & 2) { /* one byte value with */ + /* external sign */ + c = *(glyph++); + cd->xcoor[x] = (cd->tt_flags[x] & 0x10) ? c : (-1 * (int)c); + } else if(cd->tt_flags[x] & 0x10) { /* repeat last */ + cd->xcoor[x] = 0; + } else { /* two byte signed value */ + cd->xcoor[x] = getFWord(glyph); + glyph+=2; + } + } + + /* Read the y coordinates */ + for(x = 0; x < cd->num_pts; x++) { + if (cd->tt_flags[x] & 4) { /* one byte value with */ + /* external sign */ + c = *(glyph++); + cd->ycoor[x] = (cd->tt_flags[x] & 0x20) ? c : (-1 * (int)c); + } else if (cd->tt_flags[x] & 0x20) { /* repeat last value */ + cd->ycoor[x] = 0; + } else { /* two byte signed value */ + cd->ycoor[x] = getUSHORT(glyph); + glyph+=2; + } + } + + /* Convert delta values to absolute values. */ + for(x = 1; x < cd->num_pts; x++) { + cd->xcoor[x] += cd->xcoor[x-1]; + cd->ycoor[x] += cd->ycoor[x-1]; + } + + for(x=0; x < cd->num_pts; x++) { + cd->xcoor[x] = topost(cd->xcoor[x]); + cd->ycoor[x] = topost(cd->ycoor[x]); + } +} + +#define ARG_1_AND_2_ARE_WORDS 1 +#define ARGS_ARE_XY_VALUES 2 +#define ROUND_XY_TO_GRID 4 +#define WE_HAVE_A_SCALE 8 +/* RESERVED 16 */ +#define MORE_COMPONENTS 32 +#define WE_HAVE_AN_X_AND_Y_SCALE 64 +#define WE_HAVE_A_TWO_BY_TWO 128 +#define WE_HAVE_INSTRUCTIONS 256 +#define USE_MY_METRICS 512 + +void TQPSPrinterFontTTF::subsetGlyph(int charindex,bool* glyphset) +{ + USHORT flags; + USHORT glyphIndex; + charproc_data cd; + + glyphset[charindex] = TRUE; + //printf("subsetting %s ==> ",glyphName(charindex).latin1()); + + /* Get a pointer to the data. */ + BYTE* glyph = charprocFindGlyphData( charindex ); + + /* If the character is blank, it has no bounding box, */ + /* otherwise read the bounding box. */ + if( glyph == (BYTE*)NULL ) { + cd.num_ctr=0; + } else { + cd.num_ctr = getSHORT(glyph); + /* Advance the pointer past bounding box. */ + glyph += 10; + } + + if( cd.num_ctr < 0 ) { // composite + /* Once around this loop for each component. */ + do { + flags = getUSHORT(glyph); /* read the flags word */ + glyph += 2; + glyphIndex = getUSHORT(glyph); /* read the glyphindex word */ + glyph += 2; + + glyphset[ glyphIndex ] = TRUE; + subsetGlyph( glyphIndex, glyphset ); + //printf("subset contains: %d %s ",glyphIndex, glyphName(glyphIndex).latin1()); + + if(flags & ARG_1_AND_2_ARE_WORDS) { + glyph += 2; + glyph += 2; + } else { + glyph += 1; + glyph += 1; + } + + if(flags & WE_HAVE_A_SCALE) { + glyph += 2; + } else if(flags & WE_HAVE_AN_X_AND_Y_SCALE) { + glyph += 2; + glyph += 2; + } else if(flags & WE_HAVE_A_TWO_BY_TWO) { + glyph += 2; + glyph += 2; + glyph += 2; + glyph += 2; + } else { + } + } while(flags & MORE_COMPONENTS); + } + //printf("\n"); +} + + +/* +** Emmit PostScript code for a composite character. +*/ +void TQPSPrinterFontTTF::charprocComposite(BYTE *glyph, TQTextStream& s, bool *glyphSet) +{ + USHORT flags; + USHORT glyphIndex; + int arg1; + int arg2; + float xscale = 1; + float yscale = 1; +#ifdef DEBUG_TRUETYPE + float scale01 = 0; + float scale10 = 0; +#endif + + /* Once around this loop for each component. */ + do { + flags = getUSHORT(glyph); /* read the flags word */ + glyph += 2; + + glyphIndex = getUSHORT(glyph); /* read the glyphindex word */ + glyph += 2; + + if(flags & ARG_1_AND_2_ARE_WORDS) { + /* The tt spec. seems to say these are signed. */ + arg1 = getSHORT(glyph); + glyph += 2; + arg2 = getSHORT(glyph); + glyph += 2; + } else { /* The tt spec. does not clearly indicate */ + /* whether these values are signed or not. */ + arg1 = (char)*(glyph++); + arg2 = (char)*(glyph++); + } + + if(flags & WE_HAVE_A_SCALE) { + xscale = yscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; + } else if(flags & WE_HAVE_AN_X_AND_Y_SCALE) { + xscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; + yscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; + } else if(flags & WE_HAVE_A_TWO_BY_TWO) { + xscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; +#ifdef DEBUG_TRUETYPE + scale01 = f2dot14( getUSHORT(glyph) ); +#endif + glyph += 2; +#ifdef DEBUG_TRUETYPE + scale10 = f2dot14( getUSHORT(glyph) ); +#endif + glyph += 2; + yscale = f2dot14( getUSHORT(glyph) ); + glyph += 2; + } + + /* Debugging */ +#ifdef DEBUG_TRUETYPE + s << "% flags=" << flags << ", arg1=" << arg1 << ", arg2=" << arg2 << ", xscale=" << xscale << ", yscale=" << yscale << + ", scale01=" << scale01 << ", scale10=" << scale10 << endl; +#endif + + + if ( (flags & ARGS_ARE_XY_VALUES) != ARGS_ARE_XY_VALUES ) { + s << "% unimplemented shift, arg1=" << arg1; + s << ", arg2=" << arg2 << "\n"; + arg1 = arg2 = 0; + } + + /* If we have an (X,Y) shif and it is non-zero, */ + /* translate the coordinate system. */ + if ( flags & (WE_HAVE_A_TWO_BY_TWO|WE_HAVE_AN_X_AND_Y_SCALE) ) { +#if 0 + // code similar to this would be needed for two_by_two + s << "gsave [ " << xscale << " " << scale01 << " " << scale10 << " " + << yscale << " " << topost(arg1) << " " << topost(arg2) << "] SM\n"; +#endif + if ( flags & WE_HAVE_A_TWO_BY_TWO ) + s << "% Two by two transformation, unimplemented\n"; + s << "gsave " << topost(arg1); + s << " " << topost(arg2); + s << " translate\n"; + s << xscale << " " << yscale << " scale\n"; + } else if ( flags & ARGS_ARE_XY_VALUES && ( arg1 != 0 || arg2 != 0 ) ) { + s << "gsave " << topost(arg1); + s << " " << topost(arg2); + s << " translate\n"; + } + + /* Invoke the CharStrings procedure to print the component. */ + s << "false CharStrings /"; + s << glyphName( glyphIndex, glyphSet ); + s << " get exec\n"; + + // printf("false CharStrings /%s get exec\n", + //ttfont_CharStrings_getname(font,glyphIndex)); + + /* If we translated the coordinate system, */ + /* put it back the way it was. */ + if( (flags & ARGS_ARE_XY_VALUES && (arg1 != 0 || arg2 != 0) ) || + ( flags & (WE_HAVE_A_TWO_BY_TWO|WE_HAVE_AN_X_AND_Y_SCALE) ) ) { + s << "grestore "; + } + } while (flags & MORE_COMPONENTS); +} + +/* +** Return a pointer to a specific glyph's data. +*/ +BYTE* TQPSPrinterFontTTF::charprocFindGlyphData(int charindex) +{ + ULONG off; + ULONG length; + + /* Read the glyph offset from the index to location table. */ + if(indexToLocFormat == 0) { + off = getUSHORT( loca_table + (charindex * 2) ); + off *= 2; + length = getUSHORT( loca_table + ((charindex+1) * 2) ); + length *= 2; + length -= off; + } else { + off = getULONG( loca_table + (charindex * 4) ); + length = getULONG( loca_table + ((charindex+1) * 4) ); + length -= off; + } + + if(length > 0) + return glyf_table + off; + else + return (BYTE*)NULL; +} + +void TQPSPrinterFontTTF::charproc(int charindex, TQTextStream& s, bool *glyphSet ) +{ + int llx,lly,urx,ury; + int advance_width; + charproc_data cd; + +#ifdef DEBUG_TRUETYPE + s << "% tt_type3_charproc for "; + s << charindex; + s << "\n"; +#endif + + /* Get a pointer to the data. */ + BYTE* glyph = charprocFindGlyphData( charindex ); + + /* If the character is blank, it has no bounding box, */ + /* otherwise read the bounding box. */ + if( glyph == (BYTE*)NULL ) { + llx=lly=urx=ury=0; /* A blank char has an all zero BoundingBox */ + cd.num_ctr=0; /* Set this for later if()s */ + } else { + /* Read the number of contours. */ + cd.num_ctr = getSHORT(glyph); + + /* Read PostScript bounding box. */ + llx = getFWord(glyph + 2); + lly = getFWord(glyph + 4); + urx = getFWord(glyph + 6); + ury = getFWord(glyph + 8); + + /* Advance the pointer. */ + glyph += 10; + } + + /* If it is a simple character, load its data. */ + if (cd.num_ctr > 0) + charprocLoad(glyph, &cd); + else + cd.num_pts=0; + + /* Consult the horizontal metrics table to determine */ + /* the character width. */ + if( charindex < numberOfHMetrics ) + advance_width = getuFWord( hmtx_table + (charindex * 4) ); + else + advance_width = getuFWord( hmtx_table + ((numberOfHMetrics-1) * 4) ); + + /* Execute setcachedevice in order to inform the font machinery */ + /* of the character bounding box and advance width. */ + stack(cd.num_pts,7,s); + s << topost(advance_width); + s << " 0 "; + s << topost(llx); + s << " "; + s << topost(lly); + s << " "; + s << topost(urx); + s << " "; + s << topost(ury); + s << " _sc\n"; + + /* If it is a simple glyph, convert it, */ + /* otherwise, close the stack business. */ + if( cd.num_ctr > 0 ) { // simple + PSConvert(s,&cd); + delete [] cd.tt_flags; + delete [] cd.xcoor; + delete [] cd.ycoor; + delete [] cd.epts_ctr; + } else if( cd.num_ctr < 0 ) { // composite + charprocComposite(glyph,s, glyphSet); + } + + stack_end(s); +} /* end of tt_type3_charproc() */ + + +// ================== PFA ==================== + +class TQPSPrinterFontPFA + : public TQPSPrinterFontPrivate { +public: + TQPSPrinterFontPFA(const TQFontEngine *f, TQByteArray& data); + virtual void download(TQTextStream& s, bool global); + virtual bool embedded() { return TRUE; } +private: + TQByteArray data; +}; + +TQPSPrinterFontPFA::TQPSPrinterFontPFA(const TQFontEngine *f, TQByteArray& d) +{ + data = d; + + int pos = 0; + char* p = data.data(); + TQString fontname; + + if (p[ pos ] != '%' || p[ pos+1 ] != '!') { // PFA marker + qWarning("invalid pfa file"); + return; + } + + char* fontnameptr = strstr(p+pos,"/FontName"); + if (fontnameptr == NULL) + return; + + fontnameptr += strlen("/FontName") + 1; + while (*fontnameptr == ' ' || *fontnameptr == '/') fontnameptr++; + int l=0; + while (fontnameptr[l] != ' ') l++; + + psname = TQString::fromLatin1(fontnameptr,l); + replacementList = makePSFontNameList( f, psname ); +} + +void TQPSPrinterFontPFA::download(TQTextStream& s, bool global) +{ + emitPSFontNameList( s, psname, replacementList); + + if ( !embedFonts ) { + downloadMapping(s, global); + return; + } + + //qDebug("downloading pfa font %s", psname.latin1() ); + char* p = data.data(); + + s << "% Font resource\n"; + for (int i=0; i < (int)data.size(); i++) s << p[i]; + s << "% End of font resource\n"; + downloadMapping( s, global ); +} + +// ================== PFB ==================== + +class TQPSPrinterFontPFB + : public TQPSPrinterFontPrivate { +public: + TQPSPrinterFontPFB(const TQFontEngine *f, TQByteArray& data); + virtual void download(TQTextStream& s, bool global); + virtual bool embedded() { return TRUE; } +private: + TQByteArray data; +}; + +TQPSPrinterFontPFB::TQPSPrinterFontPFB(const TQFontEngine *f, TQByteArray& d) +{ + data = d; + + int pos = 0; + int len; + // int typ; + unsigned char* p = (unsigned char*) data.data(); + TQString fontname; + + if (p[ pos ] != 0x80) { // PFB marker + qWarning("pfb file does not start with 0x80"); + return; + } + pos++; + // typ = p[ pos ]; // 1=ascii 2=binary 3=done + pos++; + len = p[ pos ]; pos++; + len |= (p[ pos ] << 8) ; pos++; + len |= (p[ pos ] << 16); pos++; + len |= (p[ pos ] << 24); pos++; + + //printf("font block type %d len %d\n",typ,len); + + char* fontnameptr = strstr((char*)p+pos,"/FontName"); + if (fontnameptr == NULL) + return; + + fontnameptr += strlen("/FontName") + 1; + while (*fontnameptr == ' ' || *fontnameptr == '/') fontnameptr++; + int l=0; + while (fontnameptr[l] != ' ') l++; + + psname = TQString::fromLatin1(fontnameptr,l); + replacementList = makePSFontNameList( f, psname ); +} + +void TQPSPrinterFontPFB::download(TQTextStream& s, bool global) +{ + emitPSFontNameList( s, psname, replacementList); + + if ( !embedFonts ) { + downloadMapping(s, global); + return; + } + + //qDebug("downloading pfb font %s", psname.latin1() ); + unsigned char* p = (unsigned char*) data.data(); + int pos; + int len; + int typ; + + int hexcol = 0; + int line_length = 64; + + s << "% Font resource\n"; + + pos = 0; + typ = -1; + while (typ != 3) { // not end of file + if (p[ pos ] != 0x80) // PFB marker + return; // pfb file does not start with 0x80 + pos++; + typ = p[ pos ]; // 1=ascii 2=binary 3=done + pos++; + + if (typ == 3) break; + + len = p[ pos ]; pos++; + len |= (p[ pos ] << 8) ; pos++; + len |= (p[ pos ] << 16); pos++; + len |= (p[ pos ] << 24); pos++; + + //qDebug("font block type %d len %d",typ,len); + + int end = pos + len; + if (typ==1) { + while (pos < end) { + if (hexcol > 0) { + s << "\n"; + hexcol = 0; + } + //qWarning(TQString::fromLatin1((char*)(p+pos),1)); + if (p[pos] == '\r' || p[pos] == '\n') { + s << "\n"; + while (pos < end && (p[pos] == '\r' || p[pos] == '\n')) + pos++; + } else { + s << TQString::fromLatin1((char*)(p+pos),1); + pos++; + } + } + } + if (typ==2) { + static const char *hexchar = "0123456789abcdef"; + while (pos < end) { + /* trim hexadecimal lines to line_length columns */ + if (hexcol >= line_length) { + s << "\n"; + hexcol = 0; + } + s << TQString::fromLatin1(hexchar+((p[pos] >> 4) & 0xf),1) + << TQString::fromLatin1(hexchar+((p[pos] ) & 0xf),1); + pos++; + hexcol += 2; + } + } + } + s << "% End of font resource\n"; + downloadMapping( s, global ); +} + +// ================== AFontFileNotFound ============ + + + +class TQPSPrinterFontNotFound + : public TQPSPrinterFontPrivate { +public: + TQPSPrinterFontNotFound(const TQFontEngine* f); + virtual void download(TQTextStream& s, bool global); +private: + TQByteArray data; +}; + +TQPSPrinterFontNotFound::TQPSPrinterFontNotFound(const TQFontEngine* f) +{ + psname = makePSFontName( f ); + replacementList = makePSFontNameList( f ); +} + +void TQPSPrinterFontNotFound::download(TQTextStream& s, bool) +{ + //qDebug("downloading not found font %s", psname.latin1() ); + emitPSFontNameList( s, psname, replacementList ); + s << "% No embeddable font for "; + s << psname; + s << " found\n"; + TQPSPrinterFontPrivate::download(s, TRUE); +} + +#ifndef QT_NO_TEXTCODEC +// =================== A font file for asian ============ + +class TQPSPrinterFontAsian + : public TQPSPrinterFontPrivate { +public: + TQPSPrinterFontAsian() + : TQPSPrinterFontPrivate(), codec( 0 ) {} + void download(TQTextStream& s, bool global); + TQString defineFont( TQTextStream &stream, const TQString &ps, const TQFont &f, const TQString &key, + TQPSPrinterPrivate *d ); + void drawText( TQTextStream &stream, const TQPoint &p, TQTextEngine *engine, int item, + const TQString &text, TQPSPrinterPrivate *d, TQPainter *paint ); + + TQString makePSFontName( const TQFontEngine *f, int type ) const; + virtual TQString extension() const = 0; + + TQTextCodec *codec; +}; + +TQString TQPSPrinterFontAsian::makePSFontName( const TQFontEngine *f, int type ) const +{ + TQString ps; + int i; + + TQString family = f->fontDef.family.lower(); + + // try to make a "good" postscript name + ps = family.simplifyWhiteSpace(); + i = 0; + while( (unsigned int)i < ps.length() ) { + if ( i != 0 && ps[i] == '[') { + if ( ps[i-1] == ' ' ) + ps.truncate (i-1); + else + ps.truncate (i); + break; + } + if ( i == 0 || ps[i-1] == ' ' ) { + ps[i] = ps[i].upper(); + if ( i ) + ps.remove( i-1, 1 ); + else + i++; + } else { + i++; + } + } + + switch ( type ) { + case 1: + ps.append( TQString::fromLatin1("-Italic") ); + break; + case 2: + ps.append( TQString::fromLatin1("-Bold") ); + break; + case 3: + ps.append( TQString::fromLatin1("-BoldItalic") ); + break; + case 0: + default: + break; + } + + ps += extension(); + + return ps; +} + + +TQString TQPSPrinterFontAsian::defineFont( TQTextStream &stream, const TQString &ps, const TQFont &f, + const TQString &key, TQPSPrinterPrivate *d) +{ + TQString fontName; + TQString fontName2; + + TQString *tmp = d->headerFontNames.find( ps ); + + if ( d->buffer ) { + if ( tmp ) { + fontName = *tmp; + } else { + fontName.sprintf( "F%d", ++d->headerFontNumber ); + d->fontStream << "/" << fontName << " false " << ps << "List MF\n"; + d->headerFontNames.insert( ps, new TQString( fontName ) ); + } + fontName2.sprintf( "F%d", ++d->headerFontNumber ); + d->fontStream << "/" << fontName2 << " " + << pointSize( f, d->scale ) << "/" << fontName << " DF\n"; + d->headerFontNames.insert( key, new TQString( fontName2 ) ); + } else { + if ( tmp ) { + fontName = *tmp; + } else { + fontName.sprintf( "F%d", ++d->pageFontNumber ); + stream << "/" << fontName << " false " << ps << "List MF\n"; + d->pageFontNames.insert( ps, new TQString( fontName ) ); + } + fontName2.sprintf( "F%d", ++d->pageFontNumber ); + stream << "/" << fontName2 << " " + << pointSize( f, d->scale ) << "/" << fontName << " DF\n"; + d->pageFontNames.insert( key, new TQString( fontName2 ) ); + } + return fontName2; +} + + +void TQPSPrinterFontAsian::download(TQTextStream& s, bool) +{ + //qDebug("downloading asian font %s", psname.latin1() ); + s << "% Asian postscript font requested. Using " + << psname << endl; + emitPSFontNameList( s, psname, replacementList ); +} + +void TQPSPrinterFontAsian::drawText( TQTextStream &stream, const TQPoint &p, TQTextEngine *engine, int item, + const TQString &text, TQPSPrinterPrivate *d, TQPainter *paint) +{ + int len = engine->length( item ); + TQScriptItem &si = engine->items[item]; + + int x = p.x() + si.x; + int y = p.y() + si.y; + if ( y != d->textY || d->textY == 0 ) + stream << y << " Y"; + d->textY = y; + + TQString mdf; + if ( paint->font().underline() ) + mdf += " " + TQString().setNum( y + d->fm.underlinePos() + d->fm.lineWidth() ) + + " " + toString( d->fm.lineWidth() ) + " Tl"; + if ( paint->font().strikeOut() ) + mdf += " " + TQString().setNum( y + d->fm.strikeOutPos() ) + + " " + toString( d->fm.lineWidth() ) + " Tl"; + TQCString mb; + TQCString out; + TQString dummy( TQChar(0x20) ); + + if ( si.analysis.bidiLevel % 2 ) { + for ( int i = len-1; i >= 0; i-- ) { + TQChar ch = text.unicode()[i]; + if ( !ch.row() ) { + ; // ignore, we should never get here anyway + } else { + if ( codec ) { + dummy[0] = ch; + mb = codec->fromUnicode( dummy ); + } else + mb = " "; + + for ( unsigned int j = 0; j < mb.length (); j++ ) { + if ( mb.at(j) == '(' || mb.at(j) == ')' || mb.at(j) == '\\' ) + out += "\\"; + out += mb.at(j); + } + } + } + } else { + for ( int i = 0; i < len; i++ ) { + TQChar ch = text.unicode()[i]; + if ( !ch.row() ) { + ; // ignore, we should never get here anyway + } else { + if ( codec ) { + dummy[0] = ch; + mb = codec->fromUnicode( dummy ); + } else + mb = " "; + + for ( unsigned int j = 0; j < mb.length (); j++ ) { + if ( mb.at(j) == '(' || mb.at(j) == ')' || mb.at(j) == '\\' ) + out += "\\"; + out += mb.at(j); + } + } + } + } + stream << "(" << out << ")" << si.width << " " << x << mdf << " AT\n"; +} + +// ----------- Japanese -------------- + +static const psfont Japanese1 [] = { + { "Ryumin-Light-H", 0, 100. }, + { "Ryumin-Light-H", 0.2, 100. }, + { "GothicBBB-Medium-H", 0, 100. }, + { "GothicBBB-Medium-H", 0.2, 100. } +}; + +static const psfont Japanese1a [] = { + { "GothicBBB-Medium-H", 0, 100. }, + { "GothicBBB-Medium-H", 0.2, 100. }, + { "Ryumin-Light-H", 0, 100. }, + { "Ryumin-Light-H", 0.2, 100. } +}; + +static const psfont Japanese2 [] = { + { "GothicBBB-Medium-H", 0, 100. }, + { "GothicBBB-Medium-H", 0.2, 100. }, + { "GothicBBB-Medium-H", 0, 100. }, + { "GothicBBB-Medium-H", 0.2, 100. } +}; + +static const psfont Japanese2a [] = { + { "Ryumin-Light-H", 0, 100. }, + { "Ryumin-Light-H", 0.2, 100. }, + { "Ryumin-Light-H", 0, 100. }, + { "Ryumin-Light-H", 0.2, 100. } +}; + + +// Wadalab fonts + +static const psfont WadaMin [] = { + { "WadaMin-Regular-H", 0, 100. }, + { "WadaMin-Regular-H", 0.2, 100. }, + { "WadaMin-Bold-H", 0, 100. }, + { "WadaMin-Bold-H", 0.2, 100. } +}; + +static const psfont WadaGo [] = { + { "WadaMaruGo-Regular-H", 0, 100. }, + { "WadaMaruGo-Regular-H", 0.2, 100. }, + { "WadaGo-Bold-H", 0, 100. }, + { "WadaGo-Bold-H", 0.2, 100. } +}; + +// Adobe Wadalab + +static const psfont WadaGoAdobe [] = { + { "WadaMaruGo-RegularH-Hojo-H", 0, 100. }, + { "WadaMaruGo-RegularH-Hojo-H", 0.2, 100. }, + { "WadaMaruGo-RegularH-Hojo-H", 0, 100. }, + { "WadaMaruGo-RegularH-Hojo-H", 0.2, 100. }, +}; +static const psfont WadaMinAdobe [] = { + { "WadaMin-RegularH-Hojo-H", 0, 100. }, + { "WadaMin-RegularH-Hojo-H", 0.2, 100. }, + { "WadaMin-RegularH-Hojo-H", 0, 100. }, + { "WadaMin-RegularH-Hojo-H", 0.2, 100. }, +}; + + +static const psfont * const Japanese1Replacements[] = { + Japanese1, Japanese1a, WadaMin, WadaGo, WadaMinAdobe, WadaGoAdobe, 0 +}; +static const psfont * const Japanese2Replacements[] = { + Japanese2, Japanese2a, WadaMin, WadaGo, WadaMinAdobe, WadaGoAdobe, 0 +}; + +class TQPSPrinterFontJapanese + : public TQPSPrinterFontAsian { +public: + TQPSPrinterFontJapanese(const TQFontEngine* f); + virtual TQString extension() const; +}; + +TQPSPrinterFontJapanese::TQPSPrinterFontJapanese(const TQFontEngine* f) +{ + codec = TQTextCodec::codecForMib( 63 ); // jisx0208.1983-0 + + int type = getPsFontType( f ); + psname = makePSFontName( f, type ); + TQString best = "[ /" + psname + " 1.0 0.0 ]"; + replacementList.append( best ); + + const psfont *const *replacements = ( psname.contains( "Helvetica" ) ? Japanese2Replacements : Japanese1Replacements ); + appendReplacements( replacementList, replacements, type ); +} + +TQString TQPSPrinterFontJapanese::extension() const +{ + return "-H"; +} + +// ----------- Korean -------------- + +// sans serif +static const psfont SMGothic [] = { + { "SMGothic-Medium-KSC-EUC-H", 0, 100. }, + { "SMGothic-Medium-KSC-EUC-H", 0.2, 100. }, + { "SMGothic-DemiBold-KSC-EUC-H", 0, 100. }, + { "SMGothic-DemiBold-KSC-EUC-H", 0.2, 100. } +}; + +// serif +#if 0 // ### this is never used? +static const psfont SMMyungjo [] = { + { "SMMyungjo-Light-KSC-EUC-H", 0, 100. }, + { "SMMyungjo-Light-KSC-EUC-H", 0.2, 100. }, + { "SMMyungjo-Bold-KSC-EUC-H", 0, 100. }, + { "SMMyungjo-Bold-KSC-EUC-H", 0.2, 100. } +}; +#endif + +static const psfont MKai [] = { + { "MingMT-Light-KSC-EUC-H", 0, 100. }, + { "MingMT-Light-KSC-EUC-H", 0.2, 100. }, + { "MKai-Medium-KSC-EUC-H", 0, 100. }, + { "MKai-Medium-KSC-EUC-H", 0.2, 100. }, +}; + + +static const psfont Munhwa [] = { + { "Munhwa-Regular-KSC-EUC-H", 0, 100. }, + { "Munhwa-Regular-KSC-EUC-H", 0.2, 100. }, + { "Munhwa-Bold-KSC-EUC-H", 0, 100. }, + { "Munhwa-Bold-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont MunhwaGothic [] = { + { "MunhwaGothic-Regular-KSC-EUC-H", 0, 100. }, + { "MunhwaGothic-Regular-KSC-EUC-H", 0.2, 100. }, + { "MunhwaGothic-Bold-KSC-EUC-H", 0, 100. }, + { "MunhwaGothic-Bold-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont MunhwaGungSeo [] = { + { "MunhwaGungSeo-Light-KSC-EUC-H", 0, 100. }, + { "MunhwaGungSeo-Light-KSC-EUC-H", 0.2, 100. }, + { "MunhwaGungSeo-Bold-KSC-EUC-H", 0, 100. }, + { "MunhwaGungSeo-Bold-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont MunhwaGungSeoHeulim [] = { + { "MunhwaGungSeoHeulim-Light-KSC-EUC-H", 0, 100. }, + { "MunhwaGungSeoHeulim-Light-KSC-EUC-H", 0.2, 100. }, + { "MunhwaGungSeoHeulim-Bold-KSC-EUC-H", 0, 100. }, + { "MunhwaGungSeoHeulim-Bold-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont MunhwaHoonMin [] = { + { "MunhwaHoonMin-Regular-KSC-EUC-H", 0, 100. }, + { "MunhwaHoonMin-Regular-KSC-EUC-H", 0.2, 100. }, + { "MunhwaHoonMin-Regular-KSC-EUC-H", 0, 100. }, + { "MunhwaHoonMin-Regular-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont BaekmukGulim [] = { + { "Baekmuk-Gulim-KSC-EUC-H", 0, 100. }, + { "Baekmuk-Gulim-KSC-EUC-H", 0.2, 100. }, + { "Baekmuk-Gulim-KSC-EUC-H", 0, 100. }, + { "Baekmuk-Gulim-KSC-EUC-H", 0.2, 100. } +}; + +static const psfont * const KoreanReplacements[] = { + BaekmukGulim, SMGothic, Munhwa, MunhwaGothic, MKai, MunhwaGungSeo, + MunhwaGungSeoHeulim, MunhwaHoonMin, Helvetica, 0 +}; + +class TQPSPrinterFontKorean + : public TQPSPrinterFontAsian { +public: + TQPSPrinterFontKorean(const TQFontEngine* f); + TQString extension() const; +}; + +TQPSPrinterFontKorean::TQPSPrinterFontKorean(const TQFontEngine* f) +{ + codec = TQTextCodec::codecForMib( 38 ); // eucKR + int type = getPsFontType( f ); + psname = makePSFontName( f, type ); + TQString best = "[ /" + psname + " 1.0 0.0 ]"; + replacementList.append( best ); + appendReplacements( replacementList, KoreanReplacements, type ); +} + +TQString TQPSPrinterFontKorean::extension() const +{ + return "-KSC-EUC-H"; +} +// ----------- traditional chinese ------------ + +// Arphic Public License Big5 TrueType fonts (on Debian and CLE and others) +static const psfont ShanHeiSun [] = { + { "ShanHeiSun-Light-ETen-B5-H", 0, 100. }, + { "ShanHeiSun-Light-ETen-B5-H", 0.2, 100. }, + { "ShanHeiSun-Light-ETen-B5-H", 0, 100. }, + { "ShanHeiSun-Light-ETen-B5-H", 0.2, 100. }, +}; +static const psfont ZenKai [] = { + { "ZenKai-Medium-ETen-B5-H", 0, 100. }, + { "ZenKai-Medium-Italic-ETen-B5-H", 0.2, 100. }, + { "ZenKai-Medium-Bold-ETen-B5-H", 0, 100. }, + { "ZenKai-Medium-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; + +// Fonts on Turbolinux +static const psfont SongB5 [] = { + { "B5-MSung-Light-ETen-B5-H", 0, 100. }, + { "B5-MSung-Italic-ETen-B5-H", 0, 100. }, + { "B5-MSung-Bold-ETen-B5-H", 0, 100. }, + { "B5-MSung-BoldItalic-ETen-B5-H", 0, 100. }, +}; +static const psfont KaiB5 [] = { + { "B5-MKai-Medium-ETen-B5-H", 0, 100. }, + { "B5-MKai-Italic-ETen-B5-H", 0, 100. }, + { "B5-MKai-Bold-ETen-B5-H", 0, 100. }, + { "B5-MKai-BoldItalic-ETen-B5-H", 0, 100. }, +}; +static const psfont HeiB5 [] = { + { "B5-MHei-Medium-ETen-B5-H", 0, 100. }, + { "B5-MHei-Italic-ETen-B5-H", 0, 100. }, + { "B5-MHei-Bold-ETen-B5-H", 0, 100. }, + { "B5-MHei-BoldItalic-ETen-B5-H", 0, 100. }, +}; +static const psfont FangSongB5 [] = { + { "B5-CFangSong-Light-ETen-B5-H", 0, 100. }, + { "B5-CFangSong-Italic-ETen-B5-H", 0, 100. }, + { "B5-CFangSong-Bold-ETen-B5-H", 0, 100. }, + { "B5-CFangSong-BoldItalic-ETen-B5-H", 0, 100. }, +}; + +// Arphic fonts on Thiz Linux +static const psfont LinGothic [] = { + { "LinGothic-Light-ETen-B5-H", 0, 100. }, + { "LinGothic-Light-Italic-ETen-B5-H", 0.2, 100. }, + { "LinGothic-Light-Bold-ETen-B5-H", 0, 100. }, + { "LinGothic-Light-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; +static const psfont YenRound [] = { + { "YenRound-Light-ETen-B5-H", 0, 100. }, + { "YenRound-Light-Italic-ETen-B5-H", 0.2, 100. }, + { "YenRound-Light-Bold-ETen-B5-H", 0, 100. }, + { "YenRound-Light-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; + +// Dr. Wang Hann-Tzong's GPL'ed Big5 TrueType fonts +#if 0 // ### this is never used? +static const psfont HtWFangSong [] = { + { "HtW-FSong-Light-ETen-B5-H", 0, 100. }, + { "HtW-FSong-Light-Italic-ETen-B5-H", 0.2, 100. }, + { "HtW-FSong-Light-Bold-ETen-B5-H", 0, 100. }, + { "HtW-FSong-Light-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; +#endif + +static const psfont MingB5 [] = { + { "Ming-Light-ETen-B5-H", 0, 100. }, + { "Ming-Light-Italic-ETen-B5-H", 0.2, 100. }, + { "Ming-Light-Bold-ETen-B5-H", 0, 100. }, + { "Ming-Light-BoldItalic-ETen-B5-H", 0.2, 100. }, +}; + +// Microsoft's Ming/Sung font? +static const psfont MSung [] = { + { "MSung-Light-ETenms-B5-H", 0, 100. }, + { "MSung-Light-ETenms-B5-H", 0.2, 100. }, + { "MSung-Light-ETenms-B5-H", 0, 100. }, + { "MSung-Light-ETenms-B5-H", 0.2, 100. }, +}; +// "Standard Sung/Ming" font by Taiwan Ministry of Education +static const psfont MOESung [] = { + { "MOESung-Regular-B5-H", 0, 100. }, + { "MOESung-Regular-B5-H", 0.2, 100. }, + { "MOESung-Regular-B5-H", 0, 100. }, + { "MOESung-Regular-B5-H", 0.2, 100. }, +}; + +static const psfont MOEKai [] = { + { "MOEKai-Regular-B5-H", 0, 100. }, + { "MOEKai-Regular-B5-H", 0.2, 100. }, + { "MOEKai-Regular-B5-H", 0, 100. }, + { "MOEKai-Regular-B5-H", 0.2, 100. }, +}; + +static const psfont * const TraditionalReplacements[] = { + MOESung, SongB5, ShanHeiSun, MingB5, MSung, FangSongB5, KaiB5, ZenKai, HeiB5, + LinGothic, YenRound, MOEKai, Helvetica, 0 + }; + +#if 0 // ### these are never used? +static const psfont * const SongB5Replacements[] = { + SongB5, ShanHeiSun, MingB5, MSung, MOESung, Helvetica, 0 + }; + +static const psfont * const FangSongB5Replacements[] = { + FangSongB5, HtWFangSong, Courier, 0 + }; +static const psfont * const KaiB5Replacements[] = { + KaiB5, ZenKai, Times, 0 + }; +static const psfont * const HeiB5Replacements[] = { + HeiB5, LinGothic, YenRound, LucidaSans, 0 + }; +static const psfont * const YuanB5Replacements[] = { + YenRound, LinGothic, HeiB5, LucidaSans, 0 + }; +#endif + + +class TQPSPrinterFontTraditionalChinese + : public TQPSPrinterFontAsian { +public: + TQPSPrinterFontTraditionalChinese(const TQFontEngine* f); + TQString extension() const; +}; + +TQPSPrinterFontTraditionalChinese::TQPSPrinterFontTraditionalChinese(const TQFontEngine* f) +{ + codec = TQTextCodec::codecForMib( 2026 ); // Big5-0 + int type = getPsFontType( f ); + psname = makePSFontName( f, type ); + TQString best = "[ /" + psname + " 1.0 0.0 ]"; + replacementList.append( best ); + appendReplacements( replacementList, TraditionalReplacements, type ); +} + +TQString TQPSPrinterFontTraditionalChinese::extension() const +{ + return "-ETen-B5-H"; +} + +// ----------- simplified chinese ------------ + +#if 0 +// GB18030 fonts on XteamLinux (?) +static const psfont SimplifiedGBK2K [] = { + { "MSung-Light-GBK2K-H", 0, 100. }, + { "MSung-Light-GBK2K-H", 0.2, 100. }, + { "MKai-Medium-GBK2K-H", 0, 100. }, + { "MKai-Medium-GBK2K-H", 0.2, 100. }, +}; +#endif + +// GB18030 fonts on Turbolinux +static const psfont SongGBK2K [] = { + { "MSung-Light-GBK2K-H", 0, 100. }, + { "MSung-Italic-GBK2K-H", 0, 100. }, + { "MSung-Bold-GBK2K-H", 0, 100. }, + { "MSung-BoldItalic-GBK2K-H", 0, 100. }, +}; +static const psfont KaiGBK2K [] = { + { "MKai-Medium-GBK2K-H", 0, 100. }, + { "MKai-Italic-GBK2K-H", 0, 100. }, + { "MKai-Bold-GBK2K-H", 0, 100. }, + { "MKai-BoldItalic-GBK2K-H", 0, 100. }, +}; +static const psfont HeiGBK2K [] = { + { "MHei-Medium-GBK2K-H", 0, 100. }, + { "MHei-Italic-GBK2K-H", 0, 100. }, + { "MHei-Bold-GBK2K-H", 0, 100. }, + { "MHei-BoldItalic-GBK2K-H", 0, 100. }, +}; +static const psfont FangSongGBK2K [] = { + { "CFangSong-Light-GBK2K-H", 0, 100. }, + { "CFangSong-Italic-GBK2K-H", 0, 100. }, + { "CFangSong-Bold-GBK2K-H", 0, 100. }, + { "CFangSong-BoldItalic-GBK2K-H", 0, 100. }, +}; + +static const psfont Simplified [] = { + { "MSung-Light-GBK-EUC-H", 0, 100. }, + { "MSung-Light-GBK-EUC-H", 0.2, 100. }, + { "MKai-Medium-GBK-EUC-H", 0, 100. }, + { "MKai-Medium-GBK-EUC-H", 0.2, 100. }, +}; + +static const psfont MSungGBK [] = { + { "MSung-Light-GBK-EUC-H", 0, 100. }, + { "MSung-Light-GBK-EUC-H", 0.2, 100. }, + { "MSung-Light-GBK-EUC-H", 0, 100. }, + { "MSung-Light-GBK-EUC-H", 0.2, 100. }, +}; + +static const psfont FangSong [] = { + { "CFangSong-Light-GBK-EUC-H", 0, 100. }, + { "CFangSong-Light-GBK-EUC-H", 0.2, 100. }, + { "CFangSong-Light-GBK-EUC-H", 0, 100. }, + { "CFangSong-Light-GBK-EUC-H", 0.2, 100. }, +}; + +// Arphic Public License GB2312 TrueType fonts (on Debian and CLE and others) +static const psfont BousungEG [] = { + { "BousungEG-Light-GB-GB-EUC-H", 0, 100. }, + { "BousungEG-Light-GB-GB-EUC-H", 0.2, 100. }, + { "BousungEG-Light-GB-Bold-GB-EUC-H", 0, 100. }, + { "BousungEG-Light-GB-Bold-GB-EUC-H", 0.2, 100. }, +}; +static const psfont GBZenKai [] = { + { "GBZenKai-Medium-GB-GB-EUC-H", 0, 100. }, + { "GBZenKai-Medium-GB-GB-EUC-H", 0.2, 100. }, + { "GBZenKai-Medium-GB-Bold-GB-EUC-H", 0, 100. }, + { "GBZenKai-Medium-GB-Bold-GB-EUC-H", 0.2, 100. }, +}; + +static const psfont * const SimplifiedReplacements[] = { + SongGBK2K, FangSongGBK2K, KaiGBK2K, HeiGBK2K, + Simplified, MSungGBK, FangSong, BousungEG, GBZenKai, Helvetica, 0 + }; +#if 0 +static const psfont * const SongGBK2KReplacements[] = { + SongGBK2K, MSungGBK, BousungEG, Helvetica, 0 + }; +#endif +static const psfont * const FangSongGBK2KReplacements[] = { + FangSongGBK2K, FangSong, Courier, 0 + }; +static const psfont * const KaiGBK2KReplacements[] = { + KaiGBK2K, GBZenKai, Times, 0 + }; +static const psfont * const HeiGBK2KReplacements[] = { + HeiGBK2K, LucidaSans, 0 + }; + +class TQPSPrinterFontSimplifiedChinese + : public TQPSPrinterFontAsian { +public: + TQPSPrinterFontSimplifiedChinese(const TQFontEngine* f); + TQString extension() const; +}; + +TQPSPrinterFontSimplifiedChinese::TQPSPrinterFontSimplifiedChinese(const TQFontEngine* f) +{ + codec = TQTextCodec::codecForMib( 114 ); // GB18030 + int type = getPsFontType( f ); + TQString family = f->fontDef.family.lower(); + if( family.contains("kai",FALSE) ) { + psname = KaiGBK2K[type].psname; + appendReplacements( replacementList, KaiGBK2KReplacements, type ); + } else if( family.contains("fangsong",FALSE) ) { + psname = FangSongGBK2K[type].psname; + appendReplacements( replacementList, FangSongGBK2KReplacements, type ); + } else if( family.contains("hei",FALSE) ) { + psname = HeiGBK2K[type].psname; + appendReplacements( replacementList, HeiGBK2KReplacements, type ); + } else { + psname = SongGBK2K[type].psname; + appendReplacements( replacementList, SimplifiedReplacements, type ); + } + //qDebug("simplified chinese: fontname is %s, psname=%s", f.family().latin1(), psname.latin1() ); +} + +TQString TQPSPrinterFontSimplifiedChinese::extension() const +{ + return "-GBK2K-H"; +} + +#endif + + +// ================== TQPSPrinterFont ==================== + +class TQPSPrinterFont { +public: + TQPSPrinterFont(const TQFont& f, int script, TQPSPrinterPrivate *priv); + ~TQPSPrinterFont(); + TQString postScriptFontName() { return p->postScriptFontName(); } + TQString defineFont( TQTextStream &stream, const TQString &ps, const TQFont &f, const TQString &key, + TQPSPrinterPrivate *d ) + { return p->defineFont( stream, ps, f, key, d ); } + void download(TQTextStream& s, bool global) { p->download(s, global); } + TQPSPrinterFontPrivate *handle() { return p; } + TQString xfontname; +private: + TQByteArray data; + TQPSPrinterFontPrivate* p; +}; + +TQPSPrinterFont::~TQPSPrinterFont() +{ + // the dict in TQFontPrivate does deletion for us. + // delete p; +} + + +TQPSPrinterFont::TQPSPrinterFont(const TQFont &f, int script, TQPSPrinterPrivate *priv) + : p(0) +{ + TQString fontfilename; + TQString fontname; + + enum { NONE, PFB, PFA, TTF } type = NONE; + + TQFontEngine *engine = f.d->engineForScript( (TQFont::Script) script ); + // ### implement similar code for TQWS and WIN + xfontname = makePSFontName( engine ); + +#if defined( Q_WS_X11 ) + bool xlfd = FALSE; + //qDebug("engine = %p name=%s, script=%d", engine, engine ? engine->name() : "(null)", script); + +#ifndef QT_NO_XFTFREETYPE + if ( qt_has_xft && engine && engine->type() == TQFontEngine::Xft ) { + XftPattern *pattern = static_cast( engine )->pattern(); + char *filename = 0; + XftPatternGetString (pattern, XFT_FILE, 0, &filename); + //qDebug("filename for font is '%s'", filename); + if ( filename ) { + fontfilename = TQString::fromLocal8Bit( filename ); + xfontname = fontfilename; + } + } else +#endif + { + TQString rawName; + if ( engine && engine != (TQFontEngine *)-1 ) + rawName = engine->name(); + int index = rawName.find('-'); + if (index == 0) { + // this is an XLFD font name + for (int i=0; i < 6; i++) { + index = rawName.find('-',index+1); + } + xfontname = rawName.mid(0,index); + if ( xfontname.endsWith( "*" ) ) + xfontname.truncate( xfontname.length() - 1 ); + xlfd = TRUE; + } + } +#endif // Q_WS_X11 +#ifndef QT_NO_TEXTCODEC + // map some scripts to something more useful + if ( script == TQFont::Han ) { + TQTextCodec *lc = TQTextCodec::codecForLocale(); + switch( lc->mibEnum() ) { + case 36: // KS C 5601 + case 38: // EUC KR + script = TQFont::Hangul; + break; + + case 57: // gb2312.1980-0 + case 113: // GBK + case -113: // gbk-0 + case 114: // GB18030 + case -114: // gb18030-0 + case 2025: // GB2312 + case 2026: // Big5 + case -2026: // Big5-HKSCS + case 2101: // big5-0, big5.eten-0 + case -2101: // big5hkscs-0, hkscs-1 + break; + + case 16: // JIS7 + case 17: // SJIS + case 18: // EUC JP + case 63: // JIS X 0208 + default: + script = TQFont::Hiragana; + break; + } + } else if ( script == TQFont::Katakana ) + script = TQFont::Hiragana; + else if ( script == TQFont::Bopomofo ) + script = TQFont::Han; +#endif + + TQString searchname = xfontname; +#if defined(Q_WS_X11) + // we need an extension here due to the fact that we use different + // fonts for different scripts + if ( xlfd && script >= TQFont::Han && script <= TQFont::Bopomofo ) + xfontname += "/" + toString( script ); +#endif + + //qDebug("looking for font %s in dict", xfontname.latin1() ); + p = priv->fonts.find(xfontname); + if ( p ) + return; + +#if defined(Q_WS_X11) + if ( xlfd ) { + + for (TQStringList::Iterator it=priv->fontpath.begin(); it!=priv->fontpath.end() && fontfilename.isEmpty(); ++it) { + if ((*it).left(1) != "/") continue; // not a path name, a font server + TQString fontmapname; + int num = 0; + // search font.dir and font.scale for the right file + while ( num < 2 ) { + if ( num == 0 ) + fontmapname = (*it) + "/fonts.scale"; + else + fontmapname = (*it) + "/fonts.dir"; + //qWarning(fontmapname); + TQFile fontmap(fontmapname); + if (fontmap.open(IO_ReadOnly)) { + while (!fontmap.atEnd()) { + TQString mapping; + fontmap.readLine(mapping,512); + // fold to lower (since X folds to lowercase) + //qWarning(xfontname); + //qWarning(mapping); + if (mapping.lower().contains(searchname.lower())) { + int index = mapping.find(' ',0); + TQString ffn = mapping.mid(0,index); + // remove the most common bitmap formats + if( !ffn.contains( ".pcf" ) && !ffn.contains( ".bdf" ) && + !ffn.contains( ".spd" ) && !ffn.contains( ".phont" ) ) { + fontfilename = (*it) + TQString("/") + ffn; + if ( TQFile::exists(fontfilename) ) { + //qDebug("found font file %s", fontfilename.latin1()); + break; + } else // unset fontfilename + fontfilename = TQString(); + } + } + } + fontmap.close(); + } + num++; + } + } + } +#endif + + //qDebug("font=%s, fontname=%s, file=%s, p=%p", f.family().latin1(), xfontname.latin1(), fontfilename.latin1(), p); + + // memory mapping would be better here + if (fontfilename.length() > 0) { // maybe there is no file name + TQFile fontfile(fontfilename); + if ( fontfile.exists() ) { + //printf("font name %s size = %d\n",fontfilename.latin1(),fontfile.size()); + data = TQByteArray( fontfile.size() ); + + fontfile.open(IO_Raw | IO_ReadOnly); + fontfile.readBlock(data.data(), fontfile.size()); + fontfile.close(); + } + } + + if (!data.isNull() && data.size() > 0) { + unsigned char* d = (unsigned char *)data.data(); + if (d[0] == 0x80 && d[1] == 0x01 && d[6] == '%' && d[7] == '!') + type = PFB; + else if (d[0] == '%' && d[1] == '!' && d[2] == 'P' && d[3] == 'S') + type = PFA; + else if (d[0]==0x00 && d[1]==0x01 && d[2]==0x00 && d[3]==0x00) + type = TTF; + else + type = NONE; + } else + type = NONE; + + //qDebug("font is of type %d", type ); + switch (type) { + case TTF : + p = new TQPSPrinterFontTTF(engine, data); + break; + case PFB: + p = new TQPSPrinterFontPFB(engine, data); + break; + case PFA: + p = new TQPSPrinterFontPFA(engine, data); + break; + case NONE: + default: + +#ifndef QT_NO_TEXTCODEC + + if ( script == TQFont::Hiragana ) + p = new TQPSPrinterFontJapanese( engine ); + else if ( script == TQFont::Hangul ) + p = new TQPSPrinterFontKorean( engine ); + else if ( script == TQFont::Han ) { + TQTextCodec *lc = TQTextCodec::codecForLocale(); + switch( lc->mibEnum() ) { + case 2025: // GB2312 + case 57: // gb2312.1980-0 + case 113: // GBK + case -113: // gbk-0 + case 114: // GB18030 + case -114: // gb18030-0 + p = new TQPSPrinterFontSimplifiedChinese( engine ); + break; + case 2026: // Big5 + case -2026: // big5-0, big5.eten-0 + case 2101: // Big5-HKSCS + case -2101: // big5hkscs-0, hkscs-1 + p = new TQPSPrinterFontTraditionalChinese( engine ); + break; + default: + p = new TQPSPrinterFontJapanese( engine ); + } + } else +#endif + //qDebug("didnt find font for %s", xfontname.latin1()); + p = new TQPSPrinterFontNotFound( engine ); + break; + } + + if (p->postScriptFontName() == "Symbol") + p->setSymbol(); + + // this is needed to make sure we don't get the same postscriptname twice + TQDictIterator it( priv->fonts ); + for( it.toFirst(); it.current(); ++it ) { + if ( *(*it) == *p ) { +// qWarning("Post script driver: font already in dict"); + delete p; + p = *it; + return; + } + } + + //qDebug("inserting font %s in dict psname=%s", xfontname.latin1(), p->postScriptFontName().latin1() ); + priv->fonts.insert( xfontname, p ); +} + +// ================= END OF PS FONT METHODS ============ + + +TQPSPrinterPrivate::TQPSPrinterPrivate( TQPrinter *prt, int filedes ) + : buffer( 0 ), outDevice( 0 ), fd( filedes ), pageBuffer( 0 ), fonts(27, FALSE), fontBuffer(0), savedImage( 0 ), + dirtypen( FALSE ), dirtybrush( FALSE ), dirtyBkColor( FALSE ), bkMode( TQt::TransparentMode ), dirtyBkMode( FALSE ), +#ifndef QT_NO_TEXTCODEC + currentFontCodec( 0 ), +#endif + fm( TQFont() ), textY( 0 ) +{ + printer = prt; + headerFontNames.setAutoDelete( TRUE ); + pageFontNames.setAutoDelete( TRUE ); + fonts.setAutoDelete( TRUE ); + currentFontFile = 0; + scale = 1.; + scriptUsed = -1; + +#ifdef Q_WS_X11 + // append qsettings fontpath + TQSettings settings; + embedFonts = settings.readBoolEntry( "/qt/embedFonts", TRUE ); + + int npaths; + char** font_path; + font_path = XGetFontPath( qt_xdisplay(), &npaths); + bool xfsconfig_read = FALSE; + for (int i=0; i read its config + bool finished = FALSE; + TQFile f("/etc/X11/fs/config"); + if ( !f.exists() ) + f.setName("/usr/X11R6/lib/X11/fs/config"); + if ( !f.exists() ) + f.setName("/usr/X11/lib/X11/fs/config"); + if ( f.exists() ) { + f.open(IO_ReadOnly); + while(f.status()==IO_Ok && !finished) { + TQString fs; + f.readLine(fs, 1024); + fs=fs.stripWhiteSpace(); + if (fs.left(9)=="catalogue" && fs.contains('=')) { + fs=fs.mid(fs.find('=')+1).stripWhiteSpace(); + bool end = FALSE; + while( f.status()==IO_Ok && !end ) { + if ( fs[int(fs.length())-1] == ',' ) + fs = fs.left(fs.length()-1); + else + end = TRUE; + if (fs[0] != '#' && !fs.contains(":unscaled")) + fontpath += fs; + f.readLine(fs, 1024); + fs=fs.stripWhiteSpace(); + } + finished = TRUE; + } + } + f.close(); + } + xfsconfig_read = TRUE; + } else if(!strstr(font_path[i], ":unscaled")) { + // Fonts paths marked :unscaled are always bitmapped fonts + // -> we can as well ignore them now and save time + fontpath += font_path[i]; + } + } + XFreeFontPath(font_path); + + // append qsettings fontpath + TQStringList fp = settings.readListEntry( "/qt/fontPath", ':' ); + if ( !fp.isEmpty() ) + fontpath += fp; +#else + embedFonts = FALSE; +#endif +} + +TQPSPrinterPrivate::~TQPSPrinterPrivate() +{ + delete pageBuffer; +} + +void TQPSPrinterPrivate::setFont( const TQFont & fnt, int script ) +{ + TQFont f = fnt; + if ( f.rawMode() ) { + TQFont fnt( TQString::fromLatin1("Helvetica"), 12 ); + setFont( fnt, TQFont::Unicode ); + return; + } + if ( f.pointSize() == 0 ) { +#if defined(CHECK_RANGE) + qWarning( "TQPrinter: Cannot set a font with zero point size." ); +#endif + f.setPointSize(TQApplication::font().pointSize()); + if ( f.pointSize() == 0 ) + f.setPointSize( 11 ); + } + + TQPSPrinterFont ff( f, script, this ); + TQString ps = ff.postScriptFontName(); + + TQString s = ps; + s.append( ' ' ); + s.prepend( ' ' ); + + TQString key = ff.xfontname; + + if ( f.pointSize() != -1 ) + key += " " + toString( f.pointSize() ); + else + key += " px" + toString( f.pixelSize() ); + TQString * tmp; + if ( !buffer ) + tmp = pageFontNames.find( key ); + else + tmp = headerFontNames.find( key ); + + TQString fontName; + if ( tmp ) + fontName = *tmp; + + if ( fontName.isEmpty() ) { + fontName = ff.defineFont( pageStream, ps, f, key, this ); + } + pageStream << fontName << " F\n"; + + ps.append( ' ' ); + ps.prepend( ' ' ); + if ( !fontsUsed.contains( ps ) ) + fontsUsed += ps; + +#ifndef QT_NO_TEXTCODEC + TQTextCodec * codec = 0; +// ### +// #ifndef QT_NO_TEXTCODEC +// i = 0; +// do { +// if ( unicodevalues[i].cs == f.charSet() ) +// codec = TQTextCodec::codecForMib( unicodevalues[i++].mib ); +// } while( codec == 0 && unicodevalues[i++].cs != unicodevalues_LAST ); +// #endif + currentFontCodec = codec; +#endif + currentFont = fontName; + currentFontFile = ff.handle(); + scriptUsed = script; +} + + +static void ps_r7( TQTextStream& stream, const char * s, int l ) +{ + int i = 0; + uchar line[79]; + int col = 0; + + while( i < l ) { + line[col++] = s[i++]; + if ( col >= 76 ) { + line[col++] = '\n'; + line[col++] = '\0'; + stream << (const char *)line; + col = 0; + } + } + if ( col > 0 ) { + while( (col&3) != 0 ) + line[col++] = '%'; // use a comment as padding + line[col++] = '\n'; + line[col++] = '\0'; + stream << (const char *)line; + } +} + + +static const int quoteSize = 3; // 1-8 pixels +static const int maxQuoteLength = 4+16+32+64+128+256; // magic extended quote +static const int quoteReach = 10; // ... 1-1024 pixels back +static const int tableSize = 1024; // 2 ** quoteReach; +static const int numAttempts = 128; + +static const int hashSize = 71; + +static const int None = INT_MAX; + +/* puts the lowest numBits of data into the out array starting at postion (byte/bit). + Adjusts byte and bit to point ot the next position. + + Need to make sure the out array is long enough before calling the method. +*/ +static void emitBits( char *out, int & byte, int & bit, + int numBits, uint data ) +{ + int b = 0; + uint d = data; + while( b < numBits ) { + if ( bit == 0 ) + out[byte] = 0; + if ( d & 1 ) + out[byte] = (uchar)out[byte] | ( 1 << bit ); + d = d >> 1; + b++; + bit++; + if ( bit > 6 ) { + bit = 0; + byte++; + } + } +} + +//#define DEBUG_COMPRESS +#ifdef DEBUG_COMPRESS +#include +#endif + +static TQByteArray compress( const TQImage & image, bool gray ) { +#ifdef DEBUG_COMPRESS + TQTime t; + t.start(); + int sizeUncompressed[11]; + for( int i = 0; i < 11; i++ ) + sizeUncompressed[i] = 0; + int sizeCompressed[11]; + for( int i = 0; i < 11; i++ ) + sizeCompressed[i] = 0; +#endif + + int width = image.width(); + int height = image.height(); + int depth = image.depth(); + int size = width*height; + + int pastPixel[tableSize]; + int mostRecentPixel[hashSize]; + if ( depth == 1 ) + size = (width+7)/8*height; + else if ( !gray ) + size = size*3; + + unsigned char *pixel = new unsigned char[size+1]; + int i = 0; + if ( depth == 1 ) { + TQImage::Endian bitOrder = image.bitOrder(); + memset( pixel, 0xff, size ); + for( int y=0; y < height; y++ ) { + uchar * s = image.scanLine( y ); + for( int x=0; x < width; x++ ) { + // need to copy bit for bit... + bool b = ( bitOrder == TQImage::LittleEndian ) ? + (*(s + (x >> 3)) >> (x & 7)) & 1 : + (*(s + (x >> 3)) << (x & 7)) & 0x80 ; + if ( b ) + pixel[i >> 3] ^= (0x80 >> ( i & 7 )); + i++; + } + // we need to align to 8 bit here + i = (i+7) & 0xffffff8; + } + } else if ( depth == 8 ) { + for( int y=0; y < height; y++ ) { + uchar * s = image.scanLine( y ); + for( int x=0; x < width; x++ ) { + TQRgb rgb = image.color( s[x] ); + if ( gray ) { + pixel[i] = (unsigned char) qGray( rgb ); + i++; + } else { + pixel[i] = (unsigned char) qRed( rgb ); + pixel[i+1] = (unsigned char) qGreen( rgb ); + pixel[i+2] = (unsigned char) qBlue( rgb ); + i += 3; + } + } + } + } else { + bool alpha = image.hasAlphaBuffer(); + for( int y=0; y < height; y++ ) { + TQRgb * s = (TQRgb*)(image.scanLine( y )); + for( int x=0; x < width; x++ ) { + TQRgb rgb = (*s++); + if ( alpha && qAlpha( rgb ) < 0x40 ) // 25% alpha, convert to white - + rgb = qRgb( 0xff, 0xff, 0xff ); + if ( gray ) { + pixel[i] = (unsigned char) qGray( rgb ); + i++; + } else { + pixel[i] = (unsigned char) qRed( rgb ); + pixel[i+1] = (unsigned char) qGreen( rgb ); + pixel[i+2] = (unsigned char) qBlue( rgb ); + i += 3; + } + } + } + } + + pixel[size] = 0; + + /* this compression function emits blocks of data, where each + block is an unquoted series of pixels, or a quote from earlier + pixels. if the six-letter string "banana" were a six-pixel + image, it might be unquoted "ban" followed by a 3-pixel quote + from -2. note that the final "a" is then copied from the + second "a", which is copied from the first "a" in the same copy + operation. + + the scanning for quotable blocks uses a cobol-like loop and a + hash table: we know how many pixels we need to quote, hash the + first and last pixel we need, and then go backwards in time + looking for some spot where those pixels of those two colours + occur at the right distance from each other. + + when we find a spot, we'll try a string-compare of all the + intervening pixels. we only do a maximum of 128 both-ends + compares or 64 full-string compares. it's more important to be + fast than get the ultimate in compression. + + The format of the compressed stream is as follows: + // 2 bits step size for search and backreference ( 1 or 3 ) + 1 bit compressed or uncompressed block follows + + uncompressed block: + 3 bits size of block in bytes + size*8 bits data + + compressed block: + 3 bits compression header + 0-2 size of block is 1-3 bytes + 3-7 size of block is bigger, 4-8 additional bits specifying size follow + 0/4-8 additional size fields + 10 location of backreference + */ + + for( i=0; i < hashSize; i++ ) + mostRecentPixel[i] = None; + int index = 0; + int emittedUntil = 0; + char *out = (char *)malloc( 256 * sizeof( char ) ); + int outLen = 256; + int outOffset = 0; + int outBit = 0; + + /* we process pixels serially, emitting as necessary/possible. */ + while( index <= size ) { + int bestCandidate = None; + int bestLength = 0; + i = index % tableSize; + int h = pixel[index] % hashSize; + int start, end; + start = end = pastPixel[i] = mostRecentPixel[h]; + mostRecentPixel[h] = index; + /* if our first candidate quote is unusable, or we don't need + to quote because we've already emitted something for this + pixel, just skip. */ + if ( start < index - tableSize || index >= size || + emittedUntil > index) + start = end = None; + int attempts = 0; + /* scan for suitable quote candidates: not too far back, and + if we've found one that's as big as it can get, don't look + for more */ + while( start != None && end != None && + bestLength < maxQuoteLength && + start >= index - tableSize && + end >= index - tableSize + bestLength ) { + /* scan backwards, looking for something good enough to + try a (slow) string comparison. we maintain indexes to + the start and the end of the quote candidate here */ + while( start != None && end != None && + ( pixel[start] != pixel[index] || + pixel[end] != pixel[index+bestLength] ) ) { + if ( attempts++ > numAttempts ) { + start = None; + } else if ( pixel[end] % hashSize == + pixel[index+bestLength] % hashSize ) { + /* we move the area along the end index' chain */ + end = pastPixel[end%tableSize]; + start = end - bestLength; + } else if ( pixel[start] % hashSize == + pixel[index] % hashSize ) { + /* ... or along the start index' chain */ + start = pastPixel[start%tableSize]; + end = start + bestLength; + } else { +#if 0 + /* this should never happen: both the start and + the end pointers ran off their tracks. */ + qDebug( "oops! %06x %06x %06x %06x %5d %5d %5d %d", + pixel[start], pixel[end], + pixel[index], pixel[index+bestLength], + start, end, index, bestLength ); +#endif + /* but if it should happen, no problem. we'll just + say we found nothing, and the compression will + be a bit worse. */ + start = None; + } + /* if we've moved either index too far to use the + quote candidate, let's just give up here. there's + also a guard against "start" insanity. */ + if ( start < index - tableSize || start < 0 || start >= index ) + start = None; + if ( end < index - tableSize + bestLength || end < bestLength ) + end = None; + } + /* ok, now start and end point to an area of suitable + length whose first and last points match, or one/both + is/are set to None. */ + if ( start != None && end != None ) { + /* slow string compare... */ + int length = 0; + while( length < maxQuoteLength && + index+length < size && + pixel[start+length] == pixel[index+length] ) + length++; + /* if we've found something that overlaps the index + point, maybe we can move the quote point back? if + we're copying 10 pixels from 8 pixels back (an + overlap of 2), that'll be faster than copying from + 4 pixels back (an overlap of 6). */ + if ( start + length > index && length > 0 ) { + int d = index-start; + int equal = TRUE; + while( equal && start + length > index && + start > d && start-d >= index-tableSize ) { + int i = 0; + while( equal && i < d ) { + if( pixel[start+i] != pixel[start+i-d] ) + equal = FALSE; + i++; + } + if ( equal ) + start -= d; + } + } + /* if what we have is longer than the best previous + candidate, we'll use this one. */ + if ( length > bestLength ) { + attempts = 0; + bestCandidate = start; + bestLength = length; + if ( length < maxQuoteLength && index + length < size ) + end = mostRecentPixel[pixel[index+length]%hashSize]; + } else { + /* and if it ins't, we'll try some more. but we'll + count each string compare extra, since they're + so expensive. */ + attempts += 2; + if ( attempts > numAttempts ) { + start = None; + } else if ( pastPixel[start%tableSize] + bestLength < + pastPixel[end%tableSize] ) { + start = pastPixel[start%tableSize]; + end = start + bestLength; + } else { + end = pastPixel[end%tableSize]; + start = end - bestLength; + } + } + /* again, if we can't make use of the current quote + candidate, we don't try any more */ + if ( start < index - tableSize || start < 0 || start > size+1 ) + start = None; + if ( end < index - tableSize + bestLength || end < 0 || end > size+1 ) + end = None; + } + } + /* backreferences to 1 byte of data are actually more costly than + emitting the data directly, 2 bytes don't save much. */ + if ( bestCandidate != None && bestLength < 3 ) + bestCandidate = None; + /* at this point, bestCandidate is a candidate of bestLength + length, or else it's None. if we have such a candidate, or + we're at the end, we have to emit all unquoted data. */ + if ( index == size || bestCandidate != None ) { + /* we need a double loop, because there's a maximum length + on the "unquoted data" section. */ + while( emittedUntil < index ) { +#ifdef DEBUG_COMPRESS + int x = 0; + int bl = emittedUntil - index; + while ( (bl /= 2) ) + x++; + if ( x > 10 ) x = 10; + sizeUncompressed[x]++; +#endif + int l = TQMIN( 8, index - emittedUntil ); + if ( outOffset + l + 2 >= outLen ) { + outLen *= 2; + out = (char *) realloc( out, outLen ); + } + emitBits( out, outOffset, outBit, + 1, 0 ); + emitBits( out, outOffset, outBit, + quoteSize, l-1 ); + while( l-- ) { + emitBits( out, outOffset, outBit, + 8, pixel[emittedUntil] ); + emittedUntil++; + } + } + } + /* if we have some quoted data to output, do it. */ + if ( bestCandidate != None ) { +#ifdef DEBUG_COMPRESS + int x = 0; + int bl = bestLength; + while ( (bl /= 2) ) + x++; + if ( x > 10 ) x = 10; + sizeCompressed[x]++; +#endif + if ( outOffset + 4 >= outLen ) { + outLen *= 2; + out = (char *) realloc( out, outLen ); + } + emitBits( out, outOffset, outBit, + 1, 1 ); + int l = bestLength - 3; + const struct off_len { + int off; + int bits; + } ol_table [] = { + /* Warning: if you change the table here, change /uc in the PS code! */ + { 3, 0/*dummy*/ }, + { 16, 4 }, + { 32, 5 }, + { 64, 6 }, + { 128, 7 }, + { /*256*/ 0xfffffff, 8 }, + }; + + if ( l < ol_table[0].off ) { + emitBits( out, outOffset, outBit, + quoteSize, l ); + } else { + const off_len *ol = ol_table; + l -= ol->off; + ol++; + while ( l >= ol->off ) { + l -= ol->off; + ol++; + } + emitBits( out, outOffset, outBit, + quoteSize, ol->bits-1 ); + emitBits( out, outOffset, outBit, + ol->bits, l ); + } + emitBits( out, outOffset, outBit, + quoteReach, index - bestCandidate - 1 ); + emittedUntil += bestLength; + } + index++; + } + /* we've output all the data; time to clean up and finish off the + last characters. */ + if ( outBit ) + outOffset++; + i = 0; + /* we have to make sure the data is encoded in a stylish way :) */ + while( i < outOffset ) { + uchar c = out[i]; + c += 42; + if ( c > 'Z' && ( c != 't' || i == 0 || out[i-1] != 'Q' ) ) + c += 84; + out[i] = c; + i++; + } + TQByteArray outarr; + outarr.duplicate( out, outOffset ); + free( out ); + delete [] pixel; + +#ifdef DEBUG_COMPRESS + qDebug( "------------- image compression statistics ----------------" ); + qDebug(" compression time %d", t.elapsed() ); + qDebug( "Size dist of uncompressed blocks:" ); + qDebug( "\t%d\t%d\t%d\t%d\t%d\t%d\n", sizeUncompressed[0], sizeUncompressed[1], + sizeUncompressed[2], sizeUncompressed[3], sizeUncompressed[4], sizeUncompressed[5]); + qDebug( "\t%d\t%d\t%d\t%d\t%d\n", sizeUncompressed[6], sizeUncompressed[7], + sizeUncompressed[8], sizeUncompressed[9], sizeUncompressed[10] ); + qDebug( "Size dist of compressed blocks:" ); + qDebug( "\t%d\t%d\t%d\t%d\t%d\t%d\n", sizeCompressed[0], sizeCompressed[1], + sizeCompressed[2], sizeCompressed[3], sizeCompressed[4], sizeCompressed[5]); + qDebug( "\t%d\t%d\t%d\t%d\t%d\n", sizeCompressed[6], sizeCompressed[7], + sizeCompressed[8], sizeCompressed[9], sizeCompressed[10] ); + qDebug( "===> total compression ratio %d/%d = %f", outOffset, size, (float)outOffset/(float)size ); + qDebug( "-----------------------------------------------------------" ); +#endif + + return outarr; +} + +#undef XCOORD +#undef YCOORD +#undef WIDTH +#undef HEIGHT +#undef POINT +#undef RECT +#undef INT_ARG + +#define XCOORD(x) (float)(x) +#define YCOORD(y) (float)(y) +#define WIDTH(w) (float)(w) +#define HEIGHT(h) (float)(h) + +#define POINT(index) XCOORD(p[index].point->x()) << ' ' << \ + YCOORD(p[index].point->y()) << ' ' +#define RECT(index) XCOORD(p[index].rect->normalize().x()) << ' ' << \ + YCOORD(p[index].rect->normalize().y()) << ' ' << \ + WIDTH (p[index].rect->normalize().width()) << ' ' << \ + HEIGHT(p[index].rect->normalize().height()) << ' ' +#define INT_ARG(index) p[index].ival << ' ' + +static char returnbuffer[13]; +static const char * color( const TQColor &c, TQPrinter * printer ) +{ + if ( c == TQt::black ) + qstrcpy( returnbuffer, "B " ); + else if ( c == TQt::white ) + qstrcpy( returnbuffer, "W " ); + else if ( c.red() == c.green() && c.red() == c.blue() ) + sprintf( returnbuffer, "%d d2 ", c.red() ); + else if ( printer->colorMode() == TQPrinter::GrayScale ) + sprintf( returnbuffer, "%d d2 ", + qGray( c.red(), c.green(),c.blue() ) ); + else + sprintf( returnbuffer, "%d %d %d ", + c.red(), c.green(), c.blue() ); + return returnbuffer; +} + + +static const char * psCap( TQt::PenCapStyle p ) +{ + if ( p == TQt::SquareCap ) + return "2 "; + else if ( p == TQt::RoundCap ) + return "1 "; + return "0 "; +} + + +static const char * psJoin( TQt::PenJoinStyle p ) { + if ( p == TQt::BevelJoin ) + return "2 "; + else if ( p == TQt::RoundJoin ) + return "1 "; + return "0 "; +} + + + +void TQPSPrinterPrivate::drawImage( TQPainter *paint, float x, float y, float w, float h, + const TQImage &img, const TQImage &mask ) +{ + if ( !w || !h || img.isNull() ) return; + + int width = img.width(); + int height = img.height(); + float scaleX = (float)width/w; + float scaleY = (float)height/h; + + bool gray = (printer->colorMode() == TQPrinter::GrayScale) || + img.allGray(); + int splitSize = 21830 * (gray ? 3 : 1 ); + if ( width * height > splitSize ) { // 65535/3, tolerance for broken printers + int images, subheight; + images = ( width * height + splitSize - 1 ) / splitSize; + subheight = ( height + images-1 ) / images; + while ( subheight * width > splitSize ) { + images++; + subheight = ( height + images-1 ) / images; + } + int suby = 0; + while( suby < height ) { + drawImage(paint, x, y + suby/scaleY, w, TQMIN( subheight, height-suby )/scaleY, + img.copy( 0, suby, width, TQMIN( subheight, height-suby ) ), + mask.isNull() ? mask : mask.copy( 0, suby, width, TQMIN( subheight, height-suby ) )); + suby += subheight; + } + } else { + TQByteArray out; + int size = 0; + const char *bits; + + if ( !mask.isNull() ) { + out = ::compress( mask, TRUE ); + size = (width+7)/8*height; + pageStream << "/mask " << size << " string uc\n"; + ps_r7( pageStream, out, out.size() ); + pageStream << "d\n"; + } + if ( img.depth() == 1 ) { + size = (width+7)/8*height; + bits = "1 "; + } else if ( gray ) { + size = width*height; + bits = "8 "; + } else { + size = width*height*3; + bits = "24 "; + } + + out = ::compress( img, gray ); + pageStream << "/sl " << size << " string uc\n"; + ps_r7( pageStream, out, out.size() ); + pageStream << "d\n" + << width << ' ' << height << "[" << scaleX << " 0 0 " << scaleY << " 0 0]sl " + << bits << (!mask.isNull() ? "mask " : "false ") + << x << ' ' << y << " di\n"; + } +} + + +void TQPSPrinterPrivate::matrixSetup( TQPainter *paint ) +{ +#ifndef QT_NO_TRANSFORMATIONS + TQWMatrix tmp; + if ( paint->hasViewXForm() ) { + TQRect viewport = paint->viewport(); + TQRect window = paint->window(); + tmp.translate( viewport.x(), viewport.y() ); + tmp.scale( 1.0 * viewport.width() / window.width(), + 1.0 * viewport.height() / window.height() ); + tmp.translate( -window.x(), -window.y() ); + } + if ( paint->hasWorldXForm() ) { + tmp = paint->worldMatrix() * tmp; + } + pageStream << "[" + << tmp.m11() << ' ' << tmp.m12() << ' ' + << tmp.m21() << ' ' << tmp.m22() << ' ' + << tmp.dx() << ' ' << tmp.dy() + << "]ST\n"; +#else + TQPoint p(0,0); + p = paint->xForm(p); + pageStream << "[" + << 0 << ' ' << 0 << ' ' + << 0 << ' ' << 0 << ' ' + << p.x() << ' ' << p.y() + << "]ST\n"; +#endif + dirtyMatrix = FALSE; +} + +void TQPSPrinterPrivate::orientationSetup() +{ + if ( printer->orientation() == TQPrinter::Landscape ) + pageStream << "TQLS\n"; +} + + +void TQPSPrinterPrivate::emitHeader( bool finished ) +{ + TQString title = printer->docName(); + TQString creator = printer->creator(); + if ( !creator ) // default creator + creator = TQString::fromLatin1("TQt " QT_VERSION_STR); + outDevice = new TQFile(); + (void)((TQFile *)outDevice)->open( IO_WriteOnly, fd ); + outStream.setDevice( outDevice ); + outStream << "%!PS-Adobe-1.0"; + TQPaintDeviceMetrics m( printer ); + scale = 72. / ((float) m.logicalDpiY()); + uint mtop, mleft, mbottom, mright; + printer->margins( &mtop, &mleft, &mbottom, &mright ); + int width = m.width(); + int height = m.height(); + bool fullPage = printer->fullPage(); + if ( finished && pageCount == 1 && printer->numCopies() == 1 && + ( ( printer->fullPage() && qt_gen_epsf ) || + ( printer->outputToFile() && printer->outputFileName().endsWith( ".eps" ) ) ) + ) { + if ( !boundingBox.isValid() ) + boundingBox.setRect( 0, 0, width, height ); + if ( printer->orientation() == TQPrinter::Landscape ) { + if ( !fullPage ) + boundingBox.moveBy( -mleft, -mtop ); + outStream << " EPSF-3.0\n%%BoundingBox: " + << (int)(m.height() - boundingBox.bottom())*scale << " " // llx + << (int)(m.width() - boundingBox.right())*scale - 1 << " " // lly + << (int)(m.height() - boundingBox.top())*scale + 1 << " " // urx + << (int)(m.width() - boundingBox.left())*scale; // ury + } else { + if ( !fullPage ) + boundingBox.moveBy( mleft, -mtop ); + outStream << " EPSF-3.0\n%%BoundingBox: " + << (int)(boundingBox.left())*scale << " " + << (int)(m.height() - boundingBox.bottom())*scale - 1 << " " + << (int)(boundingBox.right())*scale + 1 << " " + << (int)(m.height() - boundingBox.top())*scale; + } + } else { + int w = width + (fullPage ? 0 : mleft + mright); + int h = height + (fullPage ? 0 : mtop + mbottom); + w = (int)(w*scale); + h = (int)(h*scale); + // set a bounding box according to the DSC + if ( printer->orientation() == TQPrinter::Landscape ) + outStream << "\n%%BoundingBox: 0 0 " << h << " " << w; + else + outStream << "\n%%BoundingBox: 0 0 " << w << " " << h; + } + outStream << "\n" << wrapDSC( "%%Creator: " + creator ); + if ( !!title ) + outStream << wrapDSC( "%%Title: " + title ); + outStream << "%%CreationDate: " << TQDateTime::currentDateTime().toString(); + outStream << "\n%%Orientation: "; + if ( printer->orientation() == TQPrinter::Landscape ) + outStream << "Landscape"; + else + outStream << "Portrait"; + if ( finished ) + outStream << "\n%%Pages: " << pageCount << "\n" + << wrapDSC( "%%DocumentFonts: " + fontsUsed ); + else + outStream << "%%Pages: (atend)" + << "\n%%DocumentFonts: (atend)"; + outStream << "\n%%EndComments\n"; + + outStream << "%%BeginProlog\n"; + const char * const prologLicense = "% Prolog copyright 1994-2006 Trolltech. " + "You may copy this prolog in any way\n" + "% that is directly related to this " + "document. For other use of this prolog,\n" + "% see your licensing agreement for TQt.\n"; + outStream << prologLicense << ps_header << "\n"; + + // we have to do this here, as scaling can affect this. + TQString lineStyles = "/LArr[" // Pen styles: + " [] []" // solid line + " [ w s ] [ s w ]" // dash line + " [ s s ] [ s s ]" // dot line + " [ m s s s ] [ s m s s ]" // dash dot line + " [ m s s s s ] [ s m s s s s ]" // dash dot dot line + " ] d\n"; + lineStyles.replace( TQRegExp( "w" ), toString( 10./scale ) ); + lineStyles.replace( TQRegExp( "m" ), toString( 5./scale ) ); + lineStyles.replace( TQRegExp( "s" ), toString( 3./scale ) ); + + outStream << lineStyles; + + outStream << "/pageinit {\n"; + if ( !printer->fullPage() ) { + if ( printer->orientation() == TQPrinter::Portrait ) + outStream << mleft*scale << " " + << mbottom*scale << " translate\n"; + else + outStream << mtop*scale << " " + << mleft*scale << " translate\n"; + } + if ( printer->orientation() == TQPrinter::Portrait ) { + outStream << "% " << m.widthMM() << "*" << m.heightMM() + << "mm (portrait)\n0 " << height*scale + << " translate " << scale << " -" << scale << " scale/defM matrix CM d } d\n"; + } else { + outStream << "% " << m.heightMM() << "*" << m.widthMM() + << " mm (landscape)\n 90 rotate " << scale << " -" << scale << " scale/defM matrix CM d } d\n"; + } + outStream << "%%EndProlog\n"; + + + outStream << "%%BeginSetup\n"; + if ( printer->numCopies() > 1 ) { + outStream << "/#copies " << printer->numCopies() << " def\n"; + outStream << "/NumCopies " << printer->numCopies() << " SPD\n"; + outStream << "/Collate " << (printer->collateCopies() ? "true" : "false") << " SPD\n"; + } + if ( fontBuffer->buffer().size() ) { + if ( pageCount == 1 || finished ) + outStream << "% Fonts and encodings used\n"; + else + outStream << "% Fonts and encodings used on pages 1-" + << pageCount << "\n"; + TQDictIterator it(fonts); + while (it.current()) { + it.current()->download(outStream,TRUE); // true means its global + ++it; + } + outStream.writeRawBytes( fontBuffer->buffer().data(), + fontBuffer->buffer().size() ); + } + outStream << "%%EndSetup\n"; + + outStream.writeRawBytes( buffer->buffer().data(), + buffer->buffer().size() ); + + delete buffer; + buffer = 0; + fontStream.unsetDevice(); + delete fontBuffer; + fontBuffer = 0; +} + + +/* Called whenever a restore has been done. Currently done at the top of a + new page and whenever clipping is turned off. */ +void TQPSPrinterPrivate::resetDrawingTools( TQPainter *paint ) +{ + TQPen defaultPen; // default drawing tools + TQBrush defaultBrush; + + TQColor c = paint->backgroundColor(); + if ( c != TQt::white ) + pageStream << color( c, printer ) << "BC\n"; + + if ( paint->backgroundMode() != TQt::TransparentMode ) + pageStream << "/OMo true d\n"; + + //currentUsed = currentSet; + //setFont( currentSet ); + currentFontFile = 0; + + TQBrush b = paint->brush(); + if ( b != defaultBrush ) { + if ( b == TQt::CustomPattern ) { +#if defined(CHECK_RANGE) + qWarning( "TQPrinter: Pixmap brush not supported" ); +#endif + } else { + cbrush = b; + } + } + + dirtypen = TRUE; + dirtybrush = TRUE; + + if ( paint->hasViewXForm() || paint->hasWorldXForm() ) + matrixSetup( paint ); +} + + +static void putRect( TQTextStream &stream, const TQRect &r ) +{ + stream << r.x() << " " + << r.y() << " " + << r.width() << " " + << r.height() << " "; +} + + +void TQPSPrinterPrivate::setClippingOff( TQPainter *paint ) +{ + pageStream << "CLO\n"; // clipping off, includes a restore + resetDrawingTools( paint ); // so drawing tools must be reset +} + + +void TQPSPrinterPrivate::clippingSetup( TQPainter *paint ) +{ + if ( paint->hasClipping() ) { + if ( !firstClipOnPage ) + setClippingOff( paint ); + const TQRegion rgn = paint->clipRegion(); + TQMemArray rects = rgn.rects(); + int i; + pageStream<< "CLSTART\n"; // start clipping + for( i = 0 ; i < (int)rects.size() ; i++ ) { + putRect( pageStream, rects[i] ); + pageStream << "ACR\n"; // add clip rect + if ( pageCount == 1 ) + boundingBox = boundingBox.unite( rects[i] ); + } + pageStream << "CLEND\n"; // end clipping + firstClipOnPage = FALSE; + } else { + if ( !firstClipOnPage ) // no need to turn off if first on page + setClippingOff( paint ); + // if we're painting without clipping, the bounding box must + // be everything. NOTE: this assumes that this function is + // only ever called when something is to be painted. + TQPaintDeviceMetrics m( printer ); + if ( !boundingBox.isValid() ) + boundingBox.setRect( 0, 0, m.width(), m.height() ); + } + dirtyClipping = FALSE; +} + +void TQPSPrinterPrivate::initPage(TQPainter *paint) +{ + + // a restore undefines all the fonts that have been defined + // inside the scope (normally within pages) and all the glyphs that + // have been added in the scope. + + TQDictIterator it(fonts); + while (it.current()) { + it.current()->restore(); + ++it; + } + if ( !buffer ) { + pageFontNames.clear(); + } + + pageStream.unsetDevice(); + if ( pageBuffer ) + delete pageBuffer; + pageBuffer = new TQBuffer(); + pageBuffer->open( IO_WriteOnly ); + pageStream.setEncoding( TQTextStream::Latin1 ); + pageStream.setDevice( pageBuffer ); + delete savedImage; + savedImage = 0; + textY = 0; + dirtyClipping = TRUE; + firstClipOnPage = TRUE; + + + resetDrawingTools( paint ); + dirtyNewPage = FALSE; + pageFontNumber = headerFontNumber; +} + +void TQPSPrinterPrivate::flushPage( bool last ) +{ + if ( last && !pageBuffer ) + return; + bool pageFonts = ( buffer == 0 ); + if ( buffer && +// ( last || pagesInBuffer++ > -1 || +// ( pagesInBuffer > 4 && buffer->size() > 262144 ) ) ) +#ifdef Q_WS_QWS + (last || buffer->size() > 2000000) // embedded is usually limited in memory +#else + (last || buffer->size() > 50000000) +#endif + ) { +// qDebug("emiting header at page %d", pageCount ); + emitHeader( last ); + } + outStream << "%%Page: " + << pageCount << ' ' << pageCount << endl + << "%%BeginPageSetup\n" + << "QI\n"; + if (!dirtyNewPage) { + if ( pageFonts ) { + //qDebug("page fonts for page %d", pageCount); + // we have already downloaded the header. Maybe we have page fonts here + TQDictIterator it(fonts); + while (it.current()) { + it.current()->download( outStream, FALSE ); // FALSE means its for the page only + ++it; + } + } + outStream << "%%EndPageSetup\n"; + if ( pageBuffer ) + outStream.writeRawBytes( pageBuffer->buffer().data(), + pageBuffer->buffer().size() ); + } + outStream << "\nQP\n"; + pageCount++; +} + +// ================ PSPrinter class ======================== + +TQPSPrinter::TQPSPrinter( TQPrinter *prt, int fd ) + : TQPaintDevice( TQInternal::Printer | TQInternal::ExternalDevice ) +{ + d = new TQPSPrinterPrivate( prt, fd ); +} + + +TQPSPrinter::~TQPSPrinter() +{ + if ( d->fd >= 0 ) +#if defined(_OS_WIN32_) + ::_close( d->fd ); +#else + ::close( d->fd ); +#endif + delete d; +} + + + +static void ignoreSigPipe(bool b) +{ + static struct sigaction *users_sigpipe_handler = 0; + + if (b) { + if (users_sigpipe_handler != 0) + return; // already ignoring sigpipe + + users_sigpipe_handler = new struct sigaction; + struct sigaction tmp_sigpipe_handler; + tmp_sigpipe_handler.sa_handler = SIG_IGN; + sigemptyset(&tmp_sigpipe_handler.sa_mask); + tmp_sigpipe_handler.sa_flags = 0; + + if (sigaction(SIGPIPE, &tmp_sigpipe_handler, users_sigpipe_handler) == -1) { + delete users_sigpipe_handler; + users_sigpipe_handler = 0; + } + } + else { + if (users_sigpipe_handler == 0) + return; // not ignoring sigpipe + + if (sigaction(SIGPIPE, users_sigpipe_handler, 0) == -1) + qWarning("TQPSPrinter: could not restore SIGPIPE handler"); + + delete users_sigpipe_handler; + users_sigpipe_handler = 0; + } +} + +bool TQPSPrinter::cmd( int c , TQPainter *paint, TQPDevCmdParam *p ) +{ + if ( c == PdcBegin ) { // start painting + d->pagesInBuffer = 0; + d->buffer = new TQBuffer(); + d->buffer->open( IO_WriteOnly ); + d->outStream.setEncoding( TQTextStream::Latin1 ); + d->outStream.setDevice( d->buffer ); + d->fontBuffer = new TQBuffer(); + d->fontBuffer->open( IO_WriteOnly ); + d->fontStream.setEncoding( TQTextStream::Latin1 ); + d->fontStream.setDevice( d->fontBuffer ); + d->headerFontNumber = 0; + d->pageCount = 1; // initialize state + d->dirtyMatrix = TRUE; + d->dirtyClipping = TRUE; + d->dirtyNewPage = TRUE; + d->firstClipOnPage = TRUE; + d->boundingBox = TQRect( 0, 0, -1, -1 ); + d->fontsUsed = TQString::fromLatin1(""); + + TQPaintDeviceMetrics m( d->printer ); + d->scale = 72. / ((float) m.logicalDpiY()); + + return TRUE; + } + + if ( c == PdcEnd ) { // painting done + bool pageCountAtEnd = (d->buffer != 0); + + // we're writing to lp/lpr through a pipe, we don't want to crash with SIGPIPE + // if lp/lpr dies + ignoreSigPipe(TRUE); + d->flushPage( TRUE ); + d->outStream << "%%Trailer\n"; + if ( pageCountAtEnd ) + d->outStream << "%%Pages: " << d->pageCount - 1 << "\n" << + wrapDSC( "%%DocumentFonts: " + d->fontsUsed ); + d->outStream << "%%EOF\n"; + ignoreSigPipe(FALSE); + + d->outStream.unsetDevice(); + if ( d->outDevice ) + d->outDevice->close(); + if ( d->fd >= 0 ) + ::close( d->fd ); + d->fd = -1; + delete d->outDevice; + d->outDevice = 0; + } + + if ( c >= PdcDrawFirst && c <= PdcDrawLast ) { + if ( !paint ) + return FALSE; // sanity + if ( d->dirtyNewPage ) + d->initPage( paint ); + if ( d->dirtyMatrix ) + d->matrixSetup( paint ); + if ( d->dirtyClipping ) // Must be after matrixSetup and initPage + d->clippingSetup( paint ); + if ( d->dirtypen ) { + // we special-case for narrow solid lines with the default + // cap and join styles + if ( d->cpen.style() == TQt::SolidLine && d->cpen.width() == 0 && + d->cpen.capStyle() == TQt::FlatCap && + d->cpen.joinStyle() == TQt::MiterJoin ) + d->pageStream << color( d->cpen.color(), d->printer ) << "P1\n"; + else + d->pageStream << (int)d->cpen.style() << ' ' << d->cpen.width() + << ' ' << color( d->cpen.color(), d->printer ) + << psCap( d->cpen.capStyle() ) + << psJoin( d->cpen.joinStyle() ) << "PE\n"; + d->dirtypen = FALSE; + } + if ( d->dirtybrush ) { + // we special-case for nobrush and solid white, since + // those are the two most common brushes + if ( d->cbrush.style() == TQt::NoBrush ) + d->pageStream << "NB\n"; + else if ( d->cbrush.style() == TQt::SolidPattern && + d->cbrush.color() == TQt::white ) + d->pageStream << "WB\n"; + else + d->pageStream << (int)d->cbrush.style() << ' ' + << color( d->cbrush.color(), d->printer ) << "BR\n"; + d->dirtybrush = FALSE; + } + if ( d->dirtyBkColor ) { + d->pageStream << color( d->bkColor, d->printer ) << "BC\n"; + d->dirtyBkColor = FALSE; + } + if ( d->dirtyBkMode ) { + if ( d->bkMode == TQt::TransparentMode ) + d->pageStream << "/OMo false d\n"; + else + d->pageStream << "/OMo true d\n"; + d->dirtyBkMode = FALSE; + } + } + + switch( c ) { + case PdcDrawPoint: + d->pageStream << POINT(0) << "P\n"; + break; + case PdcMoveTo: + d->pageStream << POINT(0) << "M\n"; + break; + case PdcLineTo: + d->pageStream << POINT(0) << "L\n"; + break; + case PdcDrawLine: + if ( p[0].point->y() == p[1].point->y() ) + d->pageStream << POINT(1) << p[0].point->x() << " HL\n"; + else if ( p[0].point->x() == p[1].point->x() ) + d->pageStream << POINT(1) << p[0].point->y() << " VL\n"; + else + d->pageStream << POINT(1) << POINT(0) << "DL\n"; + break; + case PdcDrawRect: + d->pageStream << RECT(0) << "R\n"; + break; + case PdcDrawRoundRect: + d->pageStream << RECT(0) << INT_ARG(1) << INT_ARG(2) << "RR\n"; + break; + case PdcDrawEllipse: + d->pageStream << RECT(0) << "E\n"; + break; + case PdcDrawArc: + d->pageStream << RECT(0) << INT_ARG(1) << INT_ARG(2) << "A\n"; + break; + case PdcDrawPie: + d->pageStream << RECT(0) << INT_ARG(1) << INT_ARG(2) << "PIE\n"; + break; + case PdcDrawChord: + d->pageStream << RECT(0) << INT_ARG(1) << INT_ARG(2) << "CH\n"; + break; + case PdcDrawLineSegments: + if ( p[0].ptarr->size() > 0 ) { + TQPointArray a = *p[0].ptarr; + TQPoint pt; + d->pageStream << "NP\n"; + for ( int i=0; i<(int)a.size(); i+=2 ) { + pt = a.point( i ); + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " MT\n"; + pt = a.point( i+1 ); + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " LT\n"; + } + d->pageStream << "QS\n"; + } + break; + case PdcDrawPolyline: + if ( p[0].ptarr->size() > 1 ) { + TQPointArray a = *p[0].ptarr; + TQPoint pt = a.point( 0 ); + d->pageStream << "NP\n" + << XCOORD(pt.x()) << ' ' << YCOORD(pt.y()) << " MT\n"; + for ( int i=1; i<(int)a.size(); i++ ) { + pt = a.point( i ); + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " LT\n"; + } + d->pageStream << "QS\n"; + } + break; + case PdcDrawPolygon: + if ( p[0].ptarr->size() > 2 ) { + TQPointArray a = *p[0].ptarr; + if ( p[1].ival ) + d->pageStream << "/WFi true d\n"; + TQPoint pt = a.point(0); + d->pageStream << "NP\n"; + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " MT\n"; + for( int i=1; i<(int)a.size(); i++) { + pt = a.point( i ); + d->pageStream << XCOORD(pt.x()) << ' ' + << YCOORD(pt.y()) << " LT\n"; + } + d->pageStream << "CP BF QS\n"; + if ( p[1].ival ) + d->pageStream << "/WFi false d\n"; + } + break; + case PdcDrawCubicBezier: + if ( p[0].ptarr->size() == 4 ) { + d->pageStream << "NP\n"; + TQPointArray a = *p[0].ptarr; + d->pageStream << XCOORD(a[0].x()) << ' ' + << YCOORD(a[0].y()) << " MT "; + for ( int i=1; i<4; i++ ) { + d->pageStream << XCOORD(a[i].x()) << ' ' + << YCOORD(a[i].y()) << ' '; + } + d->pageStream << "BZ\n"; + } + break; + case PdcDrawText2: + // we use drawTextItem instead + return TRUE; + case PdcDrawText2Formatted: + return TRUE; + case PdcDrawTextItem: { + const TQTextItem *ti = p[1].textItem; + TQScriptItem &si = ti->engine->items[ti->item]; + int len = ti->engine->length( ti->item ); + if ( si.isSpace || si.isObject ) + return FALSE; + + if ( d->currentSet != d->currentUsed || d->scriptUsed != si.analysis.script || !d->currentFontFile ) { + d->currentUsed = d->currentSet; + d->setFont( d->currentSet, si.analysis.script ); + } + if( d->currentFontFile ) // better not crash in case somethig goes wrong. + d->currentFontFile->drawText( d->pageStream, *p[0].point, ti->engine, ti->item, + ti->engine->string.mid( si.position, len ), d, paint); + return FALSE; + } + case PdcDrawPixmap: { + if ( p[1].pixmap->isNull() ) + break; + TQRect r = *p[0].rect; + TQImage img; + img = *(p[1].pixmap); + TQImage mask; + if ( p[1].pixmap->mask() ) + mask = *(p[1].pixmap->mask()); + d->drawImage(paint, r.x(), r.y(), r.width(), r.height(), img, mask); + break; + } + case PdcDrawImage: { + if ( p[1].image->isNull() ) + break; + TQRect r = *(p[0].rect); + TQImage img = *(p[1].image); + TQImage mask; +#ifndef QT_NO_IMAGE_DITHER_TO_1 + if ( img.hasAlphaBuffer() ) + mask = img.createAlphaMask(); +#endif + d->drawImage(paint, r.x(), r.y(), r.width(), r.height(), img, mask); + break; + } + case PdcSetBkColor: + { + if ( d->bkColor != *(p[0].color) ) { + d->bkColor = *(p[0].color); + d->dirtyBkColor = TRUE; + } + break; + } + case PdcSetBkMode: + { + if ( d->bkMode != p[0].ival ) { + d->bkMode = (TQt::BGMode) p[0].ival; + d->dirtyBkMode = TRUE; + } + break; + } + case PdcSetROP: +#if defined(CHECK_RANGE) + if ( p[0].ival != TQt::CopyROP ) + qWarning( "TQPrinter: Raster operation setting not supported" ); +#endif + break; + case PdcSetBrushOrigin: + break; + case PdcSetFont: + d->currentSet = *(p[0].font); + d->fm = paint->fontMetrics(); + // turn these off - they confuse the 'avoid font change' logic + d->currentSet.setUnderline( FALSE ); + d->currentSet.setStrikeOut( FALSE ); + break; + case PdcSetPen: + if ( d->cpen != *(p[0].pen) ) { + d->dirtypen = TRUE; + d->cpen = *(p[0].pen); + } + break; + case PdcSetBrush: + if ( p[0].brush->style() == TQt::CustomPattern ) { +#if defined(CHECK_RANGE) + qWarning( "TQPrinter: Pixmap brush not supported" ); +#endif + return FALSE; + } + if ( d->cbrush != *(p[0].brush) ) { + d->dirtybrush = TRUE; + d->cbrush = *(p[0].brush); + } + break; + case PdcSetTabStops: + case PdcSetTabArray: + return FALSE; + case PdcSetUnit: + break; + case PdcSetVXform: + case PdcSetWindow: + case PdcSetViewport: + case PdcSetWXform: + case PdcSetWMatrix: + case PdcRestoreWMatrix: + d->dirtyMatrix = TRUE; + break; + case PdcSetClip: + d->dirtyClipping = TRUE; + break; + case PdcSetClipRegion: + d->dirtyClipping = TRUE; + break; + case NewPage: + // we're writing to lp/lpr through a pipe, we don't want to crash with SIGPIPE + // if lp/lpr dies + ignoreSigPipe(TRUE); + d->flushPage(); + ignoreSigPipe(FALSE); + + d->dirtyNewPage = TRUE; + break; + case AbortPrinting: + break; + default: + break; + } + return TRUE; +} + +#endif // QT_NO_PRINTER diff --git a/src/kernel/qpsprinter.ps b/src/kernel/qpsprinter.ps new file mode 100644 index 000000000..4fbd98546 --- /dev/null +++ b/src/kernel/qpsprinter.ps @@ -0,0 +1,805 @@ +% the postscript header we use for our qpsprinter in uncompressed and commented form. +% use the makepsheader perl script to generate a compressed version of this header +% you can then paste into qpsprinter.cpp +% +% some compression of the code is done by the makepsheader script, so we don't need to +% write too criptically here. + +/d /def load def +/D {bind d} bind d +/d2 {dup dup} D +/B {0 d2} D +/W {255 d2} D +/ED {exch d} D +/D0 {0 ED} D +/LT {lineto} D +/MT {moveto} D +/S {stroke} D +/F {setfont} D +/SW {setlinewidth} D +/CP {closepath} D +/RL {rlineto} D +/NP {newpath} D +/CM {currentmatrix} D +/SM {setmatrix} D +/TR {translate} D +/SD {setdash} D +/SC {aload pop setrgbcolor} D +/CR {currentfile read pop} D +/i {index} D +/bs {bitshift} D +/scs {setcolorspace} D +/DB {dict dup begin} D +/DE {end d} D +/ie {ifelse} D +/sp {astore pop} D + +% ENDUNCOMPRESSED: Warning: leave this line in. +% Everything before this line will be left untouched by the compression + + + +/BSt 0 d % brush style +/LWi 1 d % line width +/PSt 1 d % pen style +/Cx 0 d % current x position +/Cy 0 d % current y position +/WFi false d % winding fill +/OMo false d % opaque mode (not transparent) + +/BCol [ 1 1 1 ] d % brush color +/PCol [ 0 0 0 ] d % pen color +/BkCol [ 1 1 1 ] d % background color +/BDArr [ % Brush dense patterns + 0.94 + 0.88 + 0.63 + 0.50 + 0.37 + 0.12 + 0.06 +] d +/defM matrix d + +/nS 0 d % number of saved painter states + +% LArr for the Pen styles is defined in emitHeader because of scaling + +% GPS: GetPenStyle +% Returns the line pattern (from pen style PSt). +% +% bool GPS pattern +% true : returns draw pattern +% false: returns fill pattern +/GPS { + PSt 1 ge PSt 5 le and % valid pen pattern? + { + { LArr PSt 1 sub 2 mul get } % draw pattern + { LArr PSt 2 mul 1 sub get } ifelse % opaque pattern + } + { [] } ifelse % out of range => solid line +} D + +% QS: QtStroke +% draw and fill current path +% +% - QS - +/QS { % stroke command + PSt 0 ne % != NO_PEN + { + gsave + LWi SW % set line width + true GPS 0 setdash S % draw line pattern + OMo PSt 1 ne and % opaque mode and not solid line? + { + BkCol SC + false GPS dup 0 get setdash S % fill in opaque pattern + } if + grestore + } if +} D + + + + +%% The following operations are used to read compressed data from the file +%% Until now this is only used for image compression + +% read 28 bits and leave them on tos +% +% - r28 num +/r28 { + % skip past whitespace and read one character + { currentfile read pop + dup 32 gt { exit } if + pop + } loop + % read three more + 3 { + currentfile read pop + } repeat + % make an accumulator + 0 + % for each character, shift the accumulator and add in the character + 4 { + 7 bitshift exch + dup 128 gt { 84 sub } if 42 sub 127 and + add + } repeat +} D + +/rA 0 d % accumulator +/rL 0 d % bits left + +% takes number of bits, leaves number +% +% num rB num +/rB { + rL 0 eq { + % if we have nothing, let's get something + /rA r28 d + /rL 28 d + } if + dup rL gt { + % if we don't have enough, take what we have and get more + rA exch rL sub rL exch + /rA 0 d /rL 0 d + rB exch bitshift add + } { + % else take some of what we have + dup rA 16#fffffff 3 -1 roll bitshift not and exch + % ... and update rL and rA + dup rL exch sub /rL ED + neg rA exch bitshift /rA ED + } ifelse +} D + +% uncompresses image data from currentfile until the string on the +% stack is full; leaves the string there. +% assumes that nothing could conceivably go wrong, ie. the compressed data has +% to be in the correct format and the length of the string has to be exactly right +% to hold the compressed data +% +% string uc string +% +%%% Warning: if you change the method here, change the table in qpsprinter.cpp:compress()! +/uc { + /rL 0 d + 0 + { % string pos + dup 2 index length ge { exit } if + 1 rB + 1 eq { % compressed + 3 rB % string pos bits + dup 3 ge { + 1 add dup rB % string pos bits extra + 1 index 5 ge { + 1 index 6 ge { + 1 index 7 ge { + 1 index 8 ge { + 128 add + } if + 64 add + } if + 32 add + } if + 16 add + } if + 3 add + exch pop + } if + 3 add + % string pos length + exch 10 rB 1 add + % string length pos dist + { + dup 3 index lt { + dup + } { + 2 index + } ifelse % string length pos dist length-this-time + 4 index 3 index 3 index sub 2 index getinterval + 5 index 4 index 3 -1 roll putinterval + dup 4 -1 roll add 3 1 roll + 4 -1 roll exch sub + dup 0 eq { exit } if + 3 1 roll + } loop % string pos dist length + pop pop + } { % uncompressed + 3 rB 1 add + { + 2 copy 8 rB put 1 add + } repeat + } ifelse + } loop + pop +} D + +%% image drawing routines + +/sl D0 % ### is this needed ? + +% defines for QCI +/QCIgray D0 /QCIcolor D0 /QCIindex D0 + +% this method prints color images if colorimage is available, otherwise +% converts the string to a grayscale image and uses the reular postscript image +% operator for printing. +% Arguments are the same as for the image operator: +% +% width height bits/sample matrix datasrc QCI - +/QCI { + /colorimage where { + pop + false 3 colorimage + }{ % the hard way, based on PD code by John Walker + exec /QCIcolor ED + /QCIgray QCIcolor length 3 idiv string d + 0 1 QCIcolor length 3 idiv 1 sub + { /QCIindex ED + /x QCIindex 3 mul d + QCIgray QCIindex + QCIcolor x get 0.30 mul + QCIcolor x 1 add get 0.59 mul + QCIcolor x 2 add get 0.11 mul + add add cvi + put + } for + QCIgray image + } ifelse +} D + +% general image drawing routine, used from the postscript driver +% +% Draws images with and without mask with 1, 8 and 24(rgb) bits depth. +% +% width height matrix image 1|8|24 mask|false x y di +% +% width and height specify the width/height of the image, +% matrix a transformation matrix, image a procedure holding the image data +% (same for mask) and x/y an additional translation. +% +% ### should move the translation into the matrix!!! +/di +{ + gsave + translate + 1 index 1 eq { % bitmap + false eq { % no mask, draw solid background + pop + true 3 1 roll % width height false matrix image + 4 index + 4 index + false + 4 index + 4 index + imagemask + BkCol SC + imagemask + } { + pop + false 3 1 roll % width height false matrix image + imagemask + } ifelse + } { + dup false ne { + % have a mask, see if we can use it + /languagelevel where { + pop + languagelevel 3 ge + } { false } ifelse + } { + false + } ifelse + + { + % languagelevel3, we can use image mask and dicts + + % store the image mask + /ma exch d + % select colorspace according to 8|24 bit depth and set the decode array /dc + 8 eq { + /dc [0 1] d + /DeviceGray + } { + /dc [0 1 0 1 0 1] d + /DeviceRGB + } ifelse + setcolorspace + % the image data + /im exch d + % transformation matrix + /mt exch d + % width and height + /h exch def + /w exch def + % the image dict + /id + 7 dict dup begin + /ImageType 1 d + /Width w d + /Height h d + /ImageMatrix mt d + /DataSource im d + /BitsPerComponent 8 d + /Decode dc d + end d + % the mask dictionary + /md + 7 dict dup begin + /ImageType 1 d + /Width w d + /Height h d + /ImageMatrix mt d + /DataSource ma d + /BitsPerComponent 1 d + /Decode [0 1] d + end d + % and the combined image dict + 4 dict dup begin + /ImageType 3 d + /DataDict id d + /MaskDict md d + /InterleaveType 3 d + end + image + } { + pop % no mask or can't use it, get rid of it + 8 % width height image 8|24 8 matrix + 4 1 roll + 8 eq { % grayscale + image + } { %color + QCI + } ifelse + } ifelse + } ifelse + grestore +} d + + + + +/BF { % brush fill + gsave + BSt 1 eq % solid brush? + { + BCol SC + WFi { fill } { eofill } ifelse + } if + BSt 2 ge BSt 8 le and % dense pattern? + { + BDArr BSt 2 sub get /sc ED + % the following line scales the brush color according to the pattern. the higher the pattern the lighter the color. + BCol + { + 1. exch sub sc mul 1. exch sub + } forall + 3 array astore + SC + WFi { fill } { eofill } ifelse + } if + BSt 9 ge BSt 14 le and % brush pattern? + { + WFi { clip } { eoclip } ifelse + defM SM + pathbbox % left upper right lower + 3 index 3 index translate + 4 2 roll % right lower left upper + 3 2 roll % right left upper lower + exch % left right lower upper + sub /h ED + sub /w ED + OMo { + NP + 0 0 MT + 0 h RL + w 0 RL + 0 h neg RL + CP + BkCol SC + fill + } if + BCol SC + 0.3 SW + NP + BSt 9 eq BSt 11 eq or % horiz or cross pattern + { 0 4 h + { dup 0 exch MT w exch LT } for + } if + BSt 10 eq BSt 11 eq or % vert or cross pattern + { 0 4 w + { dup 0 MT h LT } for + } if + BSt 12 eq BSt 14 eq or % F-diag or diag cross + { w h gt + { 0 6 w h add + { dup 0 MT h sub h LT } for + } { 0 6 w h add + { dup 0 exch MT w sub w exch LT } for + } ifelse + } if + BSt 13 eq BSt 14 eq or % B-diag or diag cross + { w h gt + { 0 6 w h add + { dup h MT h sub 0 LT } for + } { 0 6 w h add + { dup w exch MT w sub 0 exch LT } for + } ifelse + } if + S + } if + BSt 24 eq % CustomPattern + + { + } if + grestore +} D + +% for arc +/mat matrix d +/ang1 D0 /ang2 D0 +/w D0 /h D0 +/x D0 /y D0 + +/ARC { % Generic ARC function [ X Y W H ang1 ang2 ] + /ang2 ED /ang1 ED /h ED /w ED /y ED /x ED + mat CM pop + x w 2 div add y h 2 div add TR + 1 h w div neg scale + ang2 0 ge + {0 0 w 2 div ang1 ang1 ang2 add arc } + {0 0 w 2 div ang1 ang1 ang2 add arcn} ifelse + mat SM +} D + +/C D0 + +/P { % PdcDrawPoint [x y] + NP + MT + 0.5 0.5 rmoveto + 0 -1 RL + -1 0 RL + 0 1 RL + CP + fill +} D + +/M { % PdcMoveTo [x y] + /Cy ED /Cx ED +} D + +/L { % PdcLineTo [x y] + NP + Cx Cy MT + /Cy ED /Cx ED + Cx Cy LT + QS +} D + +/DL { % PdcDrawLine [x1 y1 x0 y0] + NP + MT + LT + QS +} D + +/HL { % PdcDrawLine [x1 y x0] + 1 index DL +} D + +/VL { % PdcDrawLine [x y1 y0] + 2 index exch DL +} D + +/R { % PdcDrawRect [x y w h] + /h ED /w ED /y ED /x ED + NP + x y MT + 0 h RL + w 0 RL + 0 h neg RL + CP + BF + QS +} D + +/ACR { % add clip rect + /h ED /w ED /y ED /x ED + x y MT + 0 h RL + w 0 RL + 0 h neg RL + CP +} D + +/xr D0 /yr D0 +/rx D0 /ry D0 /rx2 D0 /ry2 D0 + +/RR { % PdcDrawRoundRect [x y w h xr yr] + /yr ED /xr ED /h ED /w ED /y ED /x ED + xr 0 le yr 0 le or + {x y w h R} % Do rect if one of rounding values is less than 0. + {xr 100 ge yr 100 ge or + {x y w h E} % Do ellipse if both rounding values are larger than 100 + { + /rx xr w mul 200 div d + /ry yr h mul 200 div d + /rx2 rx 2 mul d + /ry2 ry 2 mul d + NP + x rx add y MT + x y rx2 ry2 180 -90 + x y h add ry2 sub rx2 ry2 270 -90 + x w add rx2 sub y h add ry2 sub rx2 ry2 0 -90 + x w add rx2 sub y rx2 ry2 90 -90 + ARC ARC ARC ARC + CP + BF + QS + } ifelse + } ifelse +} D + +/E { % PdcDrawEllipse [x y w h] + /h ED /w ED /y ED /x ED + mat CM pop + x w 2 div add y h 2 div add translate + 1 h w div scale + NP + 0 0 w 2 div 0 360 arc + mat SM + BF + QS +} D + +/A { % PdcDrawArc [x y w h ang1 ang2] + 16 div exch 16 div exch + NP + ARC + QS +} D + +/PIE { % PdcDrawPie [x y w h ang1 ang2] + /ang2 ED /ang1 ED /h ED /w ED /y ED /x ED + NP + x w 2 div add y h 2 div add MT + x y w h ang1 16 div ang2 16 div ARC + CP + BF + QS +} D + +/CH { % PdcDrawChord [x y w h ang1 ang2] + 16 div exch 16 div exch + NP + ARC + CP + BF + QS +} D + +/BZ { % PdcDrawCubicBezier [4 points] + curveto + QS +} D + +/CRGB { % Compute RGB [R G B] => R/255 G/255 B/255 + 255 div 3 1 roll + 255 div 3 1 roll + 255 div 3 1 roll +} D + + +/BC { % PdcSetBkColor [R G B] + CRGB + BkCol astore pop +} D + +/BR { % PdcSetBrush [style R G B] + CRGB + BCol astore pop + /BSt ED +} D + +/WB { % set white solid brush + 1 W BR +} D + +/NB { % set nobrush + 0 B BR +} D + +/PE { % PdcSetPen [style width R G B Cap Join] + setlinejoin setlinecap + CRGB + PCol astore pop + /LWi ED + /PSt ED + LWi 0 eq { 0.25 /LWi ED } if % ### 3.0 remove this line + PCol SC +} D + +/P1 { % PdcSetPen [R G B] + 1 0 5 2 roll 0 0 PE +} D + +/ST { % SET TRANSFORM [matrix] + defM setmatrix + concat +} D + +%% Font handling + +% the next three commands are for defining fonts. The first one +% tries to find the most suitable printer font out of a fontlist. +% if encoding is false the default one will be used. +/MF { % newname encoding fontlist + % this function tries to find a suitable postscript font. + % We try tquite hard not to get courier for a + % proportional font. The following takes an array of fonts. + % The algorithm will take the first font that + % gives a match (defined as not resulting in a courier font). + % each entry in the table is an array of the form [ /Fontname x-stretch slant ] + % x-strtch can be used to stretch/squeeze the font in x direction. + % This gives better results when eg substituting helvetica for arial + % slant is an optional slant. 0 is non slanted, 0.2 is a typical value for a syntetic oblique. + % encoding can be either an encoding vector of false if the default font encoding is requested. + true exch true exch % push a dummy on the stack, + { % so the loop over the array will leave a font in any case when exiting. + exch pop exch pop % (dummy | oldfont) (dummy | fontdict) fontarray + dup 0 get dup findfont % get the fontname from the array and load it + dup /FontName get % see if the font exists + 3 -1 roll eq { % see if fontname and the one provided are equal + exit + } if + } forall + exch % font fontarray + + % newname encoding font fontarray defines a postscript font + dup + 1 get /fxscale exch def % define scale, sland and encoding + 2 get /fslant exch def + exch /fencoding exch def + [ fxscale 0 fslant 1 0 0 ] makefont % transform font accordingly + fencoding false eq { % check if we have an encoding and use it if available + } { + dup maxlength dict begin % copy font + { + 1 index /FID ne % don't copy FID, as it's not allowed in PS Level 1 + {def}{pop pop}ifelse + } forall + /Encoding fencoding def % replace encoding + currentdict + end + } ifelse + definefont pop +} D + +% an embedded font. This is used for the base fonts of the composite font used later on. +/MFEmb { % newname encoding fontname + findfont dup length dict + begin + { + 1 index /FID ne + {d}{pop pop}ifelse + } forall + /Encoding ED currentdict + end + definefont pop +} D + +% DF: define font +% used to get a scaled version of an already loaded font +% +% newname pointsize fontmame DF - +/DF { + findfont + % get the fontsize on top of the stack and define font matrix + /fs 3 -1 roll d [ fs 0 0 fs -1 mul 0 0 ] + makefont + d +} D + +/ty 0 d +/Y { + /ty ED +} D + +/Tl { % draw underline/strikeout line: () w x y lw ->Tl-> () w x + gsave + setlinewidth + NP 1 index exch MT + 1 index 0 rlineto stroke + grestore +} D + +/XYT { % [string [x/y displacement array] width x] + ty MT % pops x + + /xyshow where { % interpreter has xyshow + pop pop + xyshow + } { % use ashow + exch pop % string cwidth + 1 index % string cwidth string + dup length 2 div exch % string cwidth length string !have to divide by 2 since we use unicode! + stringwidth pop % string cwidth length pwidth + 3 -1 roll % string length pwidth cwidth + exch sub exch div % string extraperchar + exch 0 exch % extraperchar 0 string + ashow + } ifelse +} D + +/AT { + ty MT % pops x + 1 index % string cwidth string + dup length 2 div exch % string cwidth length string !have to divide by 2 since we use unicode! + stringwidth pop % string cwidth length pwidth + 3 -1 roll % string length pwidth cwidth + exch sub exch div % string extraperchar + exch 0 exch % extraperchar 0 string + ashow +} D + +%% start of page +/QI { + /C save d + pageinit + /Cx 0 d % reset current x position + /Cy 0 d % reset current y position + /OMo false d +} D + +%% end of page +/QP { % show page + C restore + showpage +} D + +% merges one key value pair into the page device dict +% +% key value SPD - +/SPD { + /setpagedevice where { + 1 dict dup begin 3 1 roll def end + setpagedevice + } { pop pop } ifelse +} D + +/SV { % Save painter state + BSt LWi PSt Cx Cy WFi OMo BCol PCol BkCol + /nS nS 1 add d + gsave +} D + +/RS { % Restore painter state + nS 0 gt + { grestore + /BkCol ED /PCol ED /BCol ED /OMo ED /WFi ED + /Cy ED /Cx ED /PSt ED /LWi ED /BSt ED + /nS nS 1 sub d + } if +} D + +/CLSTART { % clipping start + /clipTmp matrix CM d % save current matrix + defM SM % Page default matrix + NP +} D + +/CLEND { % clipping end + clip + NP + clipTmp SM % restore the current matrix +} D + +/CLO { % clipping off + grestore % restore top of page state + gsave % save it back again + defM SM % set coordsys (defensive progr.) +} D + diff --git a/src/kernel/qpsprinter_p.h b/src/kernel/qpsprinter_p.h new file mode 100644 index 000000000..7a060c249 --- /dev/null +++ b/src/kernel/qpsprinter_p.h @@ -0,0 +1,92 @@ +/********************************************************************** +** +** Definition of internal TQPSPrinter class. +** TQPSPrinter implements PostScript (tm) output via TQPrinter. +** +** Created : 940927 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQPSPRINTER_P_H +#define TQPSPRINTER_P_H + + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of qpsprinter.cpp and qprinter_x11.cpp. +// This header file may change from version to version without notice, +// or even be removed. +// +// We mean it. +// +// + + +#ifndef QT_H +#include "qprinter.h" +#include "qtextstream.h" +#endif // QT_H + +#ifndef QT_NO_PRINTER + +class TQPSPrinterPrivate; + +class Q_EXPORT TQPSPrinter : public TQPaintDevice +{ +private: + // TQPrinter uses these + TQPSPrinter( TQPrinter *, int ); + ~TQPSPrinter(); + + bool cmd ( int, TQPainter *, TQPDevCmdParam * ); + + enum { NewPage = 100, AbortPrinting }; + + friend class TQPrinter; +private: + // not used by TQPrinter + TQPSPrinterPrivate *d; + + // Disabled copy constructor and operator= + TQPSPrinter( const TQPSPrinter & ); + TQPSPrinter &operator=( const TQPSPrinter & ); +}; + +#endif // QT_NO_PRINTER + +#endif // TQPSPRINTER_P_H diff --git a/src/kernel/qrect.cpp b/src/kernel/qrect.cpp new file mode 100644 index 000000000..2410c94c0 --- /dev/null +++ b/src/kernel/qrect.cpp @@ -0,0 +1,960 @@ +/**************************************************************************** +** +** Implementation of TQRect class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#define TQRECT_C +#include "qrect.h" +#include "qdatastream.h" + +/*! + \class TQRect + \brief The TQRect class defines a rectangle in the plane. + + \ingroup images + \ingroup graphics + \mainclass + + A rectangle is internally represented as an upper-left corner and + a bottom-right corner, but it is normally expressed as an + upper-left corner and a size. + + The coordinate type is TQCOORD (defined in \c qwindowdefs.h as \c + int). The minimum value of TQCOORD is TQCOORD_MIN (-2147483648) and + the maximum value is TQCOORD_MAX (2147483647). + + Note that the size (width and height) of a rectangle might be + different from what you are used to. If the top-left corner and + the bottom-right corner are the same, the height and the width of + the rectangle will both be 1. + + Generally, \e{width = right - left + 1} and \e{height = bottom - + top + 1}. We designed it this way to make it correspond to + rectangular spaces used by drawing functions in which the width + and height denote a number of pixels. For example, drawing a + rectangle with width and height 1 draws a single pixel. + + The default coordinate system has origin (0, 0) in the top-left + corner. The positive direction of the y axis is down, and the + positive x axis is from left to right. + + A TQRect can be constructed with a set of left, top, width and + height integers, from two TQPoints or from a TQPoint and a TQSize. + After creation the dimensions can be changed, e.g. with setLeft(), + setRight(), setTop() and setBottom(), or by setting sizes, e.g. + setWidth(), setHeight() and setSize(). The dimensions can also be + changed with the move functions, e.g. moveBy(), moveCenter(), + moveBottomRight(), etc. You can also add coordinates to a + rectangle with addCoords(). + + You can test to see if a TQRect contains a specific point with + contains(). You can also test to see if two TQRects intersect with + intersects() (see also intersect()). To get the bounding rectangle + of two TQRects use unite(). + + \sa TQPoint, TQSize +*/ + + +/***************************************************************************** + TQRect member functions + *****************************************************************************/ + +/*! + \fn TQRect::TQRect() + + Constructs an invalid rectangle. +*/ + +/*! + Constructs a rectangle with \a topLeft as the top-left corner and + \a bottomRight as the bottom-right corner. +*/ + +TQRect::TQRect( const TQPoint &topLeft, const TQPoint &bottomRight ) +{ + x1 = (TQCOORD)topLeft.x(); + y1 = (TQCOORD)topLeft.y(); + x2 = (TQCOORD)bottomRight.x(); + y2 = (TQCOORD)bottomRight.y(); +} + +/*! + Constructs a rectangle with \a topLeft as the top-left corner and + \a size as the rectangle size. +*/ + +TQRect::TQRect( const TQPoint &topLeft, const TQSize &size ) +{ + x1 = (TQCOORD)topLeft.x(); + y1 = (TQCOORD)topLeft.y(); + x2 = (TQCOORD)(x1+size.width()-1); + y2 = (TQCOORD)(y1+size.height()-1); +} + +/*! + \fn TQRect::TQRect( int left, int top, int width, int height ) + + Constructs a rectangle with the \a top, \a left corner and \a + width and \a height. + + Example (creates three identical rectangles): + \code + TQRect r1( TQPoint(100,200), TQPoint(110,215) ); + TQRect r2( TQPoint(100,200), TQSize(11,16) ); + TQRect r3( 100, 200, 11, 16 ); + \endcode +*/ + + +/*! + \fn bool TQRect::isNull() const + + Returns TRUE if the rectangle is a null rectangle; otherwise + returns FALSE. + + A null rectangle has both the width and the height set to 0, that + is right() == left() - 1 and bottom() == top() - 1. + + Note that if right() == left() and bottom() == top(), then the + rectangle has width 1 and height 1. + + A null rectangle is also empty. + + A null rectangle is not valid. + + \sa isEmpty(), isValid() +*/ + +/*! + \fn bool TQRect::isEmpty() const + + Returns TRUE if the rectangle is empty; otherwise returns FALSE. + + An empty rectangle has a left() \> right() or top() \> bottom(). + + An empty rectangle is not valid. \c{isEmpty() == !isValid()} + + \sa isNull(), isValid(), normalize() +*/ + +/*! + \fn bool TQRect::isValid() const + + Returns TRUE if the rectangle is valid; otherwise returns FALSE. + + A valid rectangle has a left() \<= right() and top() \<= bottom(). + + Note that non-trivial operations like intersections are not defined + for invalid rectangles. + + \c{isValid() == !isEmpty()} + + \sa isNull(), isEmpty(), normalize() +*/ + + +/*! + Returns a normalized rectangle, i.e. a rectangle that has a + non-negative width and height. + + It swaps left and right if left() \> right(), and swaps top and + bottom if top() \> bottom(). + + \sa isValid() +*/ + +TQRect TQRect::normalize() const +{ + TQRect r; + if ( x2 < x1 ) { // swap bad x values + r.x1 = x2; + r.x2 = x1; + } else { + r.x1 = x1; + r.x2 = x2; + } + if ( y2 < y1 ) { // swap bad y values + r.y1 = y2; + r.y2 = y1; + } else { + r.y1 = y1; + r.y2 = y2; + } + return r; +} + + +/*! + \fn int TQRect::left() const + + Returns the left coordinate of the rectangle. Identical to x(). + + \sa setLeft(), right(), topLeft(), bottomLeft() +*/ + +/*! + \fn int TQRect::top() const + + Returns the top coordinate of the rectangle. Identical to y(). + + \sa setTop(), bottom(), topLeft(), topRight() +*/ + +/*! + \fn int TQRect::right() const + + Returns the right coordinate of the rectangle. + + \sa setRight(), left(), topRight(), bottomRight() +*/ + +/*! + \fn int TQRect::bottom() const + + Returns the bottom coordinate of the rectangle. + + \sa setBottom(), top(), bottomLeft(), bottomRight() +*/ + +/*! + \fn TQCOORD &TQRect::rLeft() + + Returns a reference to the left coordinate of the rectangle. + + \sa rTop(), rRight(), rBottom() +*/ + +/*! + \fn TQCOORD &TQRect::rTop() + + Returns a reference to the top coordinate of the rectangle. + + \sa rLeft(), rRight(), rBottom() +*/ + +/*! + \fn TQCOORD &TQRect::rRight() + + Returns a reference to the right coordinate of the rectangle. + + \sa rLeft(), rTop(), rBottom() +*/ + +/*! + \fn TQCOORD &TQRect::rBottom() + + Returns a reference to the bottom coordinate of the rectangle. + + \sa rLeft(), rTop(), rRight() +*/ + +/*! + \fn int TQRect::x() const + + Returns the left coordinate of the rectangle. Identical to left(). + + \sa left(), y(), setX() +*/ + +/*! + \fn int TQRect::y() const + + Returns the top coordinate of the rectangle. Identical to top(). + + \sa top(), x(), setY() +*/ + +/*! + \fn void TQRect::setLeft( int pos ) + + Sets the left edge of the rectangle to \a pos. May change the + width, but will never change the right edge of the rectangle. + + Identical to setX(). + + \sa left(), setTop(), setWidth() +*/ + +/*! + \fn void TQRect::setTop( int pos ) + + Sets the top edge of the rectangle to \a pos. May change the + height, but will never change the bottom edge of the rectangle. + + Identical to setY(). + + \sa top(), setBottom(), setHeight() +*/ + +/*! + \fn void TQRect::setRight( int pos ) + + Sets the right edge of the rectangle to \a pos. May change the + width, but will never change the left edge of the rectangle. + + \sa right(), setLeft(), setWidth() +*/ + +/*! + \fn void TQRect::setBottom( int pos ) + + Sets the bottom edge of the rectangle to \a pos. May change the + height, but will never change the top edge of the rectangle. + + \sa bottom(), setTop(), setHeight() +*/ + +/*! + \fn void TQRect::setX( int x ) + + Sets the x position of the rectangle (its left end) to \a x. May + change the width, but will never change the right edge of the + rectangle. + + Identical to setLeft(). + + \sa x(), setY() +*/ + +/*! + \fn void TQRect::setY( int y ) + + Sets the y position of the rectangle (its top) to \a y. May change + the height, but will never change the bottom edge of the + rectangle. + + Identical to setTop(). + + \sa y(), setX() +*/ + +/*! + Set the top-left corner of the rectangle to \a p. May change + the size, but will the never change the bottom-right corner of + the rectangle. + + \sa topLeft(), moveTopLeft(), setBottomRight(), setTopRight(), setBottomLeft() +*/ +void TQRect::setTopLeft( const TQPoint &p ) +{ + setLeft( p.x() ); + setTop( p.y() ); +} + +/*! + Set the bottom-right corner of the rectangle to \a p. May change + the size, but will the never change the top-left corner of + the rectangle. + + \sa bottomRight(), moveBottomRight(), setTopLeft(), setTopRight(), setBottomLeft() +*/ +void TQRect::setBottomRight( const TQPoint &p ) +{ + setRight( p.x() ); + setBottom( p.y() ); +} + +/*! + Set the top-right corner of the rectangle to \a p. May change + the size, but will the never change the bottom-left corner of + the rectangle. + + \sa topRight(), moveTopRight(), setTopLeft(), setBottomRight(), setBottomLeft() +*/ +void TQRect::setTopRight( const TQPoint &p ) +{ + setRight( p.x() ); + setTop( p.y() ); +} + +/*! + Set the bottom-left corner of the rectangle to \a p. May change + the size, but will the never change the top-right corner of + the rectangle. + + \sa bottomLeft(), moveBottomLeft(), setTopLeft(), setBottomRight(), setTopRight() +*/ +void TQRect::setBottomLeft( const TQPoint &p ) +{ + setLeft( p.x() ); + setBottom( p.y() ); +} + +/*! + \fn TQPoint TQRect::topLeft() const + + Returns the top-left position of the rectangle. + + \sa setTopLeft(), moveTopLeft(), bottomRight(), left(), top() +*/ + +/*! + \fn TQPoint TQRect::bottomRight() const + + Returns the bottom-right position of the rectangle. + + \sa setBottomRight(), moveBottomRight(), topLeft(), right(), bottom() +*/ + +/*! + \fn TQPoint TQRect::topRight() const + + Returns the top-right position of the rectangle. + + \sa setTopRight(), moveTopRight(), bottomLeft(), top(), right() +*/ + +/*! + \fn TQPoint TQRect::bottomLeft() const + + Returns the bottom-left position of the rectangle. + + \sa setBottomLeft(), moveBottomLeft(), topRight(), bottom(), left() +*/ + +/*! + \fn TQPoint TQRect::center() const + + Returns the center point of the rectangle. + + \sa moveCenter(), topLeft(), bottomRight(), topRight(), bottomLeft() +*/ + + +/*! + Extracts the rectangle parameters as the position \a *x, \a *y and + width \a *w and height \a *h. + + \sa setRect(), coords() +*/ + +void TQRect::rect( int *x, int *y, int *w, int *h ) const +{ + *x = x1; + *y = y1; + *w = x2-x1+1; + *h = y2-y1+1; +} + +/*! + Extracts the rectangle parameters as the top-left point \a *xp1, + \a *yp1 and the bottom-right point \a *xp2, \a *yp2. + + \sa setCoords(), rect() +*/ + +void TQRect::coords( int *xp1, int *yp1, int *xp2, int *yp2 ) const +{ + *xp1 = x1; + *yp1 = y1; + *xp2 = x2; + *yp2 = y2; +} + + +/*! + Sets the left position of the rectangle to \a pos, leaving the + size unchanged. + + \sa left(), setLeft(), moveTop(), moveRight(), moveBottom() +*/ +void TQRect::moveLeft( int pos ) +{ + x2 += (TQCOORD)(pos - x1); + x1 = (TQCOORD)pos; +} + +/*! + Sets the top position of the rectangle to \a pos, leaving the + size unchanged. + + \sa top(), setTop(), moveLeft(), moveRight(), moveBottom() +*/ + +void TQRect::moveTop( int pos ) +{ + y2 += (TQCOORD)(pos - y1); + y1 = (TQCOORD)pos; +} + +/*! + Sets the right position of the rectangle to \a pos, leaving the + size unchanged. + + \sa right(), setRight(), moveLeft(), moveTop(), moveBottom() +*/ + +void TQRect::moveRight( int pos ) +{ + x1 += (TQCOORD)(pos - x2); + x2 = (TQCOORD)pos; +} + +/*! + Sets the bottom position of the rectangle to \a pos, leaving the + size unchanged. + + \sa bottom(), setBottom(), moveLeft(), moveTop(), moveRight() +*/ + +void TQRect::moveBottom( int pos ) +{ + y1 += (TQCOORD)(pos - y2); + y2 = (TQCOORD)pos; +} + +/*! + Sets the top-left position of the rectangle to \a p, leaving the + size unchanged. + + \sa topLeft(), setTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft() +*/ + +void TQRect::moveTopLeft( const TQPoint &p ) +{ + moveLeft( p.x() ); + moveTop( p.y() ); +} + +/*! + Sets the bottom-right position of the rectangle to \a p, leaving + the size unchanged. + + \sa bottomRight(), setBottomRight(), moveTopLeft(), moveTopRight(), moveBottomLeft() +*/ + +void TQRect::moveBottomRight( const TQPoint &p ) +{ + moveRight( p.x() ); + moveBottom( p.y() ); +} + +/*! + Sets the top-right position of the rectangle to \a p, leaving the + size unchanged. + + \sa topRight(), setTopRight(), moveTopLeft(), moveBottomRight(), moveBottomLeft() +*/ + +void TQRect::moveTopRight( const TQPoint &p ) +{ + moveRight( p.x() ); + moveTop( p.y() ); +} + +/*! + Sets the bottom-left position of the rectangle to \a p, leaving + the size unchanged. + + \sa bottomLeft(), setBottomLeft(), moveTopLeft(), moveBottomRight(), moveTopRight() +*/ + +void TQRect::moveBottomLeft( const TQPoint &p ) +{ + moveLeft( p.x() ); + moveBottom( p.y() ); +} + + +/*! + Sets the center point of the rectangle to \a p, leaving the size + unchanged. + + \sa center(), moveTopLeft(), moveBottomRight(), moveTopRight(), moveBottomLeft() +*/ + +void TQRect::moveCenter( const TQPoint &p ) +{ + TQCOORD w = x2 - x1; + TQCOORD h = y2 - y1; + x1 = (TQCOORD)(p.x() - w/2); + y1 = (TQCOORD)(p.y() - h/2); + x2 = x1 + w; + y2 = y1 + h; +} + + +/*! + Moves the rectangle \a dx along the x axis and \a dy along the y + axis, relative to the current position. Positive values move the + rectangle to the right and down. + + \sa moveTopLeft() +*/ + +void TQRect::moveBy( int dx, int dy ) +{ + x1 += (TQCOORD)dx; + y1 += (TQCOORD)dy; + x2 += (TQCOORD)dx; + y2 += (TQCOORD)dy; +} + +/*! + Sets the coordinates of the rectangle's top-left corner to \a (x, + y), and its size to \a (w, h). + + \sa rect(), setCoords() +*/ + +void TQRect::setRect( int x, int y, int w, int h ) +{ + x1 = (TQCOORD)x; + y1 = (TQCOORD)y; + x2 = (TQCOORD)(x+w-1); + y2 = (TQCOORD)(y+h-1); +} + +/*! + Sets the coordinates of the rectangle's top-left corner to \a + (xp1, yp1), and the coordinates of its bottom-right corner to \a + (xp2, yp2). + + \sa coords(), setRect() +*/ + +void TQRect::setCoords( int xp1, int yp1, int xp2, int yp2 ) +{ + x1 = (TQCOORD)xp1; + y1 = (TQCOORD)yp1; + x2 = (TQCOORD)xp2; + y2 = (TQCOORD)yp2; +} + +/*! + Adds \a xp1, \a yp1, \a xp2 and \a yp2 respectively to the + existing coordinates of the rectangle. +*/ + +void TQRect::addCoords( int xp1, int yp1, int xp2, int yp2 ) +{ + x1 += (TQCOORD)xp1; + y1 += (TQCOORD)yp1; + x2 += (TQCOORD)xp2; + y2 += (TQCOORD)yp2; +} + +/*! + \fn TQSize TQRect::size() const + + Returns the size of the rectangle. + + \sa width(), height() +*/ + +/*! + \fn int TQRect::width() const + + Returns the width of the rectangle. The width includes both the + left and right edges, i.e. width = right - left + 1. + + \sa height(), size(), setHeight() +*/ + +/*! + \fn int TQRect::height() const + + Returns the height of the rectangle. The height includes both the + top and bottom edges, i.e. height = bottom - top + 1. + + \sa width(), size(), setHeight() +*/ + +/*! + Sets the width of the rectangle to \a w. The right edge is + changed, but not the left edge. + + \sa width(), setLeft(), setRight(), setSize() +*/ + +void TQRect::setWidth( int w ) +{ + x2 = (TQCOORD)(x1 + w - 1); +} + +/*! + Sets the height of the rectangle to \a h. The top edge is not + moved, but the bottom edge may be moved. + + \sa height(), setTop(), setBottom(), setSize() +*/ + +void TQRect::setHeight( int h ) +{ + y2 = (TQCOORD)(y1 + h - 1); +} + +/*! + Sets the size of the rectangle to \a s. The top-left corner is not + moved. + + \sa size(), setWidth(), setHeight() +*/ + +void TQRect::setSize( const TQSize &s ) +{ + x2 = (TQCOORD)(s.width() +x1-1); + y2 = (TQCOORD)(s.height()+y1-1); +} + +/*! + Returns TRUE if the point \a p is inside or on the edge of the + rectangle; otherwise returns FALSE. + + If \a proper is TRUE, this function returns TRUE only if \a p is + inside (not on the edge). +*/ + +bool TQRect::contains( const TQPoint &p, bool proper ) const +{ + if ( proper ) + return p.x() > x1 && p.x() < x2 && + p.y() > y1 && p.y() < y2; + else + return p.x() >= x1 && p.x() <= x2 && + p.y() >= y1 && p.y() <= y2; +} + +/*! + \overload bool TQRect::contains( int x, int y, bool proper ) const + + Returns TRUE if the point \a x, \a y is inside this rectangle; + otherwise returns FALSE. + + If \a proper is TRUE, this function returns TRUE only if the point + is entirely inside (not on the edge). +*/ + +/*! + \overload bool TQRect::contains( int x, int y ) const + + Returns TRUE if the point \a x, \a y is inside this rectangle; + otherwise returns FALSE. +*/ + +/*! + \overload + + Returns TRUE if the rectangle \a r is inside this rectangle; + otherwise returns FALSE. + + If \a proper is TRUE, this function returns TRUE only if \a r is + entirely inside (not on the edge). + + \sa unite(), intersect(), intersects() +*/ + +bool TQRect::contains( const TQRect &r, bool proper ) const +{ + if ( proper ) + return r.x1 > x1 && r.x2 < x2 && r.y1 > y1 && r.y2 < y2; + else + return r.x1 >= x1 && r.x2 <= x2 && r.y1 >= y1 && r.y2 <= y2; +} + +/*! + Unites this rectangle with rectangle \a r. +*/ +TQRect& TQRect::operator|=(const TQRect &r) +{ + *this = *this | r; + return *this; +} + +/*! + Intersects this rectangle with rectangle \a r. +*/ +TQRect& TQRect::operator&=(const TQRect &r) +{ + *this = *this & r; + return *this; +} + + +/*! + Returns the bounding rectangle of this rectangle and rectangle \a + r. + + The bounding rectangle of a nonempty rectangle and an empty or + invalid rectangle is defined to be the nonempty rectangle. + + \sa operator|=(), operator&(), intersects(), contains() +*/ + +TQRect TQRect::operator|(const TQRect &r) const +{ + if ( isValid() ) { + if ( r.isValid() ) { + TQRect tmp; + tmp.setLeft( TQMIN( x1, r.x1 ) ); + tmp.setRight( TQMAX( x2, r.x2 ) ); + tmp.setTop( TQMIN( y1, r.y1 ) ); + tmp.setBottom( TQMAX( y2, r.y2 ) ); + return tmp; + } else { + return *this; + } + } else { + return r; + } +} + +/*! + Returns the bounding rectangle of this rectangle and rectangle \a + r. \c{r.unite(s)} is equivalent to \c{r|s}. +*/ +TQRect TQRect::unite( const TQRect &r ) const +{ + return *this | r; +} + + +/*! + Returns the intersection of this rectangle and rectangle \a r. + + Returns an empty rectangle if there is no intersection. + + \sa operator&=(), operator|(), isEmpty(), intersects(), contains() +*/ + +TQRect TQRect::operator&( const TQRect &r ) const +{ + TQRect tmp; + tmp.x1 = TQMAX( x1, r.x1 ); + tmp.x2 = TQMIN( x2, r.x2 ); + tmp.y1 = TQMAX( y1, r.y1 ); + tmp.y2 = TQMIN( y2, r.y2 ); + return tmp; +} + +/*! + Returns the intersection of this rectangle and rectangle \a r. + \c{r.intersect(s)} is equivalent to \c{r&s}. +*/ +TQRect TQRect::intersect( const TQRect &r ) const +{ + return *this & r; +} + +/*! + Returns TRUE if this rectangle intersects with rectangle \a r + (there is at least one pixel that is within both rectangles); + otherwise returns FALSE. + + \sa intersect(), contains() +*/ + +bool TQRect::intersects( const TQRect &r ) const +{ + return ( TQMAX( x1, r.x1 ) <= TQMIN( x2, r.x2 ) && + TQMAX( y1, r.y1 ) <= TQMIN( y2, r.y2 ) ); +} + + +/*! + \relates TQRect + + Returns TRUE if \a r1 and \a r2 are equal; otherwise returns FALSE. +*/ + +bool operator==( const TQRect &r1, const TQRect &r2 ) +{ + return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2; +} + +/*! + \relates TQRect + + Returns TRUE if \a r1 and \a r2 are different; otherwise returns FALSE. +*/ + +bool operator!=( const TQRect &r1, const TQRect &r2 ) +{ + return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2; +} + + +/***************************************************************************** + TQRect stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates TQRect + + Writes the TQRect, \a r, to the stream \a s, and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQRect &r ) +{ + if ( s.version() == 1 ) + s << (Q_INT16)r.left() << (Q_INT16)r.top() + << (Q_INT16)r.right() << (Q_INT16)r.bottom(); + else + s << (Q_INT32)r.left() << (Q_INT32)r.top() + << (Q_INT32)r.right() << (Q_INT32)r.bottom(); + return s; +} + +/*! + \relates TQRect + + Reads a TQRect from the stream \a s into rect \a r and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQRect &r ) +{ + if ( s.version() == 1 ) { + Q_INT16 x1, y1, x2, y2; + s >> x1; s >> y1; s >> x2; s >> y2; + r.setCoords( x1, y1, x2, y2 ); + } + else { + Q_INT32 x1, y1, x2, y2; + s >> x1; s >> y1; s >> x2; s >> y2; + r.setCoords( x1, y1, x2, y2 ); + } + return s; +} +#endif // QT_NO_DATASTREAM diff --git a/src/kernel/qrect.h b/src/kernel/qrect.h new file mode 100644 index 000000000..4da3da5cd --- /dev/null +++ b/src/kernel/qrect.h @@ -0,0 +1,276 @@ +/**************************************************************************** +** +** Definition of TQRect class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQRECT_H +#define TQRECT_H + +#ifndef QT_H +#include "qsize.h" +#endif // QT_H + +#if defined(topLeft) +#error "Macro definition of topLeft conflicts with TQRect" +// don't just silently undo people's defines: #undef topLeft +#endif + +class Q_EXPORT TQRect // rectangle class +{ +public: + TQRect() { x1 = y1 = 0; x2 = y2 = -1; } + TQRect( const TQPoint &topleft, const TQPoint &bottomright ); + TQRect( const TQPoint &topleft, const TQSize &size ); + TQRect( int left, int top, int width, int height ); + + bool isNull() const; + bool isEmpty() const; + bool isValid() const; + TQRect normalize() const; + + int left() const; + int top() const; + int right() const; + int bottom() const; + + TQCOORD &rLeft(); + TQCOORD &rTop(); + TQCOORD &rRight(); + TQCOORD &rBottom(); + + int x() const; + int y() const; + void setLeft( int pos ); + void setTop( int pos ); + void setRight( int pos ); + void setBottom( int pos ); + void setX( int x ); + void setY( int y ); + + void setTopLeft( const TQPoint &p ); + void setBottomRight( const TQPoint &p ); + void setTopRight( const TQPoint &p ); + void setBottomLeft( const TQPoint &p ); + + TQPoint topLeft() const; + TQPoint bottomRight() const; + TQPoint topRight() const; + TQPoint bottomLeft() const; + TQPoint center() const; + + void rect( int *x, int *y, int *w, int *h ) const; + void coords( int *x1, int *y1, int *x2, int *y2 ) const; + + void moveLeft( int pos ); + void moveTop( int pos ); + void moveRight( int pos ); + void moveBottom( int pos ); + void moveTopLeft( const TQPoint &p ); + void moveBottomRight( const TQPoint &p ); + void moveTopRight( const TQPoint &p ); + void moveBottomLeft( const TQPoint &p ); + void moveCenter( const TQPoint &p ); + void moveBy( int dx, int dy ); + + void setRect( int x, int y, int w, int h ); + void setCoords( int x1, int y1, int x2, int y2 ); + void addCoords( int x1, int y1, int x2, int y2 ); + + TQSize size() const; + int width() const; + int height() const; + void setWidth( int w ); + void setHeight( int h ); + void setSize( const TQSize &s ); + + TQRect operator|(const TQRect &r) const; + TQRect operator&(const TQRect &r) const; + TQRect& operator|=(const TQRect &r); + TQRect& operator&=(const TQRect &r); + + bool contains( const TQPoint &p, bool proper=FALSE ) const; + bool contains( int x, int y ) const; // inline methods, _don't_ merge these + bool contains( int x, int y, bool proper ) const; + bool contains( const TQRect &r, bool proper=FALSE ) const; + TQRect unite( const TQRect &r ) const; + TQRect intersect( const TQRect &r ) const; + bool intersects( const TQRect &r ) const; + + friend Q_EXPORT bool operator==( const TQRect &, const TQRect & ); + friend Q_EXPORT bool operator!=( const TQRect &, const TQRect & ); + +private: +#if defined(Q_WS_X11) || defined(Q_OS_TEMP) + friend void qt_setCoords( TQRect *r, int xp1, int yp1, int xp2, int yp2 ); +#endif +#if defined(Q_OS_MAC) + TQCOORD y1; + TQCOORD x1; + TQCOORD y2; + TQCOORD x2; +#else + TQCOORD x1; + TQCOORD y1; + TQCOORD x2; + TQCOORD y2; +#endif +}; + +Q_EXPORT bool operator==( const TQRect &, const TQRect & ); +Q_EXPORT bool operator!=( const TQRect &, const TQRect & ); + + +/***************************************************************************** + TQRect stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQRect & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQRect & ); +#endif + +/***************************************************************************** + TQRect inline member functions + *****************************************************************************/ + +inline TQRect::TQRect( int left, int top, int width, int height ) +{ + x1 = (TQCOORD)left; + y1 = (TQCOORD)top; + x2 = (TQCOORD)(left+width-1); + y2 = (TQCOORD)(top+height-1); +} + +inline bool TQRect::isNull() const +{ return x2 == x1-1 && y2 == y1-1; } + +inline bool TQRect::isEmpty() const +{ return x1 > x2 || y1 > y2; } + +inline bool TQRect::isValid() const +{ return x1 <= x2 && y1 <= y2; } + +inline int TQRect::left() const +{ return x1; } + +inline int TQRect::top() const +{ return y1; } + +inline int TQRect::right() const +{ return x2; } + +inline int TQRect::bottom() const +{ return y2; } + +inline TQCOORD &TQRect::rLeft() +{ return x1; } + +inline TQCOORD & TQRect::rTop() +{ return y1; } + +inline TQCOORD & TQRect::rRight() +{ return x2; } + +inline TQCOORD & TQRect::rBottom() +{ return y2; } + +inline int TQRect::x() const +{ return x1; } + +inline int TQRect::y() const +{ return y1; } + +inline void TQRect::setLeft( int pos ) +{ x1 = (TQCOORD)pos; } + +inline void TQRect::setTop( int pos ) +{ y1 = (TQCOORD)pos; } + +inline void TQRect::setRight( int pos ) +{ x2 = (TQCOORD)pos; } + +inline void TQRect::setBottom( int pos ) +{ y2 = (TQCOORD)pos; } + +inline void TQRect::setX( int x ) +{ x1 = (TQCOORD)x; } + +inline void TQRect::setY( int y ) +{ y1 = (TQCOORD)y; } + +inline TQPoint TQRect::topLeft() const +{ return TQPoint(x1, y1); } + +inline TQPoint TQRect::bottomRight() const +{ return TQPoint(x2, y2); } + +inline TQPoint TQRect::topRight() const +{ return TQPoint(x2, y1); } + +inline TQPoint TQRect::bottomLeft() const +{ return TQPoint(x1, y2); } + +inline TQPoint TQRect::center() const +{ return TQPoint((x1+x2)/2, (y1+y2)/2); } + +inline int TQRect::width() const +{ return x2 - x1 + 1; } + +inline int TQRect::height() const +{ return y2 - y1 + 1; } + +inline TQSize TQRect::size() const +{ return TQSize(x2-x1+1, y2-y1+1); } + +inline bool TQRect::contains( int x, int y, bool proper ) const +{ + if ( proper ) + return x > x1 && x < x2 && + y > y1 && y < y2; + else + return x >= x1 && x <= x2 && + y >= y1 && y <= y2; +} + +inline bool TQRect::contains( int x, int y ) const +{ + return x >= x1 && x <= x2 && + y >= y1 && y <= y2; +} +#define Q_DEFINED_QRECT +#include "qwinexport.h" +#endif // TQRECT_H diff --git a/src/kernel/qregion.cpp b/src/kernel/qregion.cpp new file mode 100644 index 000000000..5720d1209 --- /dev/null +++ b/src/kernel/qregion.cpp @@ -0,0 +1,383 @@ +/**************************************************************************** +** +** Implementation of TQRegion class +** +** Created : 950726 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qregion.h" +#include "qpointarray.h" +#include "qbuffer.h" +#include "qdatastream.h" + +// BEING REVISED: paul +/*! + \class TQRegion qregion.h + \brief The TQRegion class specifies a clip region for a painter. + + \ingroup images + \ingroup graphics + + TQRegion is used with TQPainter::setClipRegion() to limit the paint + area to what needs to be painted. There is also a + TQWidget::repaint() that takes a TQRegion parameter. TQRegion is the + best tool for reducing flicker. + + A region can be created from a rectangle, an ellipse, a polygon or + a bitmap. Complex regions may be created by combining simple + regions using unite(), intersect(), subtract() or eor() (exclusive + or). You can move a region using translate(). + + You can test whether a region isNull(), isEmpty() or if it + contains() a TQPoint or TQRect. The bounding rectangle is given by + boundingRect(). + + The function rects() gives a decomposition of the region into + rectangles. + + Example of using complex regions: + \code + void MyWidget::paintEvent( TQPaintEvent * ) + { + TQPainter p; // our painter + TQRegion r1( TQRect(100,100,200,80), // r1 = elliptic region + TQRegion::Ellipse ); + TQRegion r2( TQRect(100,120,90,30) ); // r2 = rectangular region + TQRegion r3 = r1.intersect( r2 ); // r3 = intersection + p.begin( this ); // start painting widget + p.setClipRegion( r3 ); // set clip region + ... // paint clipped graphics + p.end(); // painting done + } + \endcode + + TQRegion is an \link shclass.html implicitly shared\endlink class. + + \warning Due to window system limitations, the whole coordinate + space for a region is limited to the points between -32767 and + 32767 on Mac OS X and Windows 95/98/ME. + + \sa TQPainter::setClipRegion(), TQPainter::setClipRect() +*/ + + +/*! + \enum TQRegion::RegionType + + Specifies the shape of the region to be created. + + \value Rectangle the region covers the entire rectangle. + \value Ellipse the region is an ellipse inside the rectangle. +*/ + +/*! + \fn Region TQRegion::handle() const + + Returns the region's handle. +*/ + +/***************************************************************************** + TQRegion member functions + *****************************************************************************/ + +/*! + Constructs a rectangular or elliptic region. + + If \a t is \c Rectangle, the region is the filled rectangle (\a x, + \a y, \a w, \a h). If \a t is \c Ellipse, the region is the filled + ellipse with center at (\a x + \a w / 2, \a y + \a h / 2) and size + (\a w ,\a h ). +*/ +TQRegion::TQRegion( int x, int y, int w, int h, RegionType t ) +{ + TQRegion tmp(TQRect(x,y,w,h),t); + tmp.data->ref(); + data = tmp.data; +} + +/*! + Detaches from shared region data to make sure that this region is + the only one referring to the data. + + \sa copy(), \link shclass.html shared classes\endlink +*/ + +void TQRegion::detach() +{ + if ( data->count != 1 ) + *this = copy(); +} + +#ifndef QT_NO_DATASTREAM +/* + Executes region commands in the internal buffer and rebuilds the + original region. + + We do this when we read a region from the data stream. + + If \a ver is non-0, uses the format version \a ver on reading the + byte array. +*/ + +void TQRegion::exec( const TQByteArray &buffer, int ver ) +{ + TQBuffer buf( buffer ); + TQDataStream s( &buf ); + if ( ver ) + s.setVersion( ver ); + buf.open( IO_ReadOnly ); + TQRegion rgn; +#if defined(QT_CHECK_STATE) + int test_cnt = 0; +#endif + while ( !s.eof() ) { + Q_INT32 id; + if ( s.version() == 1 ) { + int id_int; + s >> id_int; + id = id_int; + } else { + s >> id; + } +#if defined(QT_CHECK_STATE) + if ( test_cnt > 0 && id != TQRGN_TRANSLATE ) + qWarning( "TQRegion::exec: Internal error" ); + test_cnt++; +#endif + if ( id == TQRGN_SETRECT || id == TQRGN_SETELLIPSE ) { + TQRect r; + s >> r; + rgn = TQRegion( r, id == TQRGN_SETRECT ? Rectangle : Ellipse ); + } else if ( id == TQRGN_SETPTARRAY_ALT || id == TQRGN_SETPTARRAY_WIND ) { + TQPointArray a; + s >> a; + rgn = TQRegion( a, id == TQRGN_SETPTARRAY_WIND ); + } else if ( id == TQRGN_TRANSLATE ) { + TQPoint p; + s >> p; + rgn.translate( p.x(), p.y() ); + } else if ( id >= TQRGN_OR && id <= TQRGN_XOR ) { + TQByteArray bop1, bop2; + TQRegion r1, r2; + s >> bop1; r1.exec( bop1 ); + s >> bop2; r2.exec( bop2 ); + switch ( id ) { + case TQRGN_OR: + rgn = r1.unite( r2 ); + break; + case TQRGN_AND: + rgn = r1.intersect( r2 ); + break; + case TQRGN_SUB: + rgn = r1.subtract( r2 ); + break; + case TQRGN_XOR: + rgn = r1.eor( r2 ); + break; + } + } else if ( id == TQRGN_RECTS ) { + // (This is the only form used in TQt 2.0) + Q_UINT32 n; + s >> n; + TQRect r; + for ( int i=0; i<(int)n; i++ ) { + s >> r; + rgn = rgn.unite( TQRegion(r) ); + } + } + } + buf.close(); + *this = rgn; +} + + +/***************************************************************************** + TQRegion stream functions + *****************************************************************************/ + +/*! + \relates TQRegion + + Writes the region \a r to the stream \a s and returns a reference + to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQRegion &r ) +{ + TQMemArray a = r.rects(); + if ( a.isEmpty() ) { + s << (Q_UINT32)0; + } else { + if ( s.version() == 1 ) { + int i; + for ( i=(int)a.size()-1; i>0; i-- ) { + s << (Q_UINT32)(12+i*24); + s << (int)TQRGN_OR; + } + for ( i=0; i<(int)a.size(); i++ ) { + s << (Q_UINT32)(4+8) << (int)TQRGN_SETRECT << a[i]; + } + } + else { + s << (Q_UINT32)(4+4+16*a.size()); // 16: storage size of TQRect + s << (Q_INT32)TQRGN_RECTS; + s << (Q_UINT32)a.size(); + for ( int i=0; i<(int)a.size(); i++ ) + s << a[i]; + } + } + return s; +} + +/*! + \relates TQRegion + + Reads a region from the stream \a s into \a r and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQRegion &r ) +{ + TQByteArray b; + s >> b; + r.exec( b, s.version() ); + return s; +} +#endif //QT_NO_DATASTREAM + +// These are not inline - they can be implemented better on some platforms +// (eg. Windows at least provides 3-variable operations). For now, simple. + + +/*! + Applies the unite() function to this region and \a r. \c r1|r2 is + equivalent to \c r1.unite(r2) + + \sa unite(), operator+() +*/ +const TQRegion TQRegion::operator|( const TQRegion &r ) const + { return unite(r); } + +/*! + Applies the unite() function to this region and \a r. \c r1+r2 is + equivalent to \c r1.unite(r2) + + \sa unite(), operator|() +*/ +const TQRegion TQRegion::operator+( const TQRegion &r ) const + { return unite(r); } + +/*! + Applies the intersect() function to this region and \a r. \c r1&r2 + is equivalent to \c r1.intersect(r2) + + \sa intersect() +*/ +const TQRegion TQRegion::operator&( const TQRegion &r ) const + { return intersect(r); } + +/*! + Applies the subtract() function to this region and \a r. \c r1-r2 + is equivalent to \c r1.subtract(r2) + + \sa subtract() +*/ +const TQRegion TQRegion::operator-( const TQRegion &r ) const + { return subtract(r); } + +/*! + Applies the eor() function to this region and \a r. \c r1^r2 is + equivalent to \c r1.eor(r2) + + \sa eor() +*/ +const TQRegion TQRegion::operator^( const TQRegion &r ) const + { return eor(r); } + +/*! + Applies the unite() function to this region and \a r and assigns + the result to this region. \c r1|=r2 is equivalent to \c + r1=r1.unite(r2) + + \sa unite() +*/ +TQRegion& TQRegion::operator|=( const TQRegion &r ) + { return *this = *this | r; } + +/*! + Applies the unite() function to this region and \a r and assigns + the result to this region. \c r1+=r2 is equivalent to \c + r1=r1.unite(r2) + + \sa intersect() +*/ +TQRegion& TQRegion::operator+=( const TQRegion &r ) + { return *this = *this + r; } + +/*! + Applies the intersect() function to this region and \a r and + assigns the result to this region. \c r1&=r2 is equivalent to \c + r1=r1.intersect(r2) + + \sa intersect() +*/ +TQRegion& TQRegion::operator&=( const TQRegion &r ) + { return *this = *this & r; } + +/*! + Applies the subtract() function to this region and \a r and + assigns the result to this region. \c r1-=r2 is equivalent to \c + r1=r1.subtract(r2) + + \sa subtract() +*/ +TQRegion& TQRegion::operator-=( const TQRegion &r ) + { return *this = *this - r; } + +/*! + Applies the eor() function to this region and \a r and + assigns the result to this region. \c r1^=r2 is equivalent to \c + r1=r1.eor(r2) + + \sa eor() +*/ +TQRegion& TQRegion::operator^=( const TQRegion &r ) + { return *this = *this ^ r; } + diff --git a/src/kernel/qregion.h b/src/kernel/qregion.h new file mode 100644 index 000000000..f0221a647 --- /dev/null +++ b/src/kernel/qregion.h @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** Definition of TQRegion class +** +** Created : 940514 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQREGION_H +#define TQREGION_H + +#ifndef QT_H +#include "qshared.h" +#include "qrect.h" +#endif // QT_H + +#ifdef Q_WS_X11 +struct TQRegionPrivate; +#endif + +class Q_EXPORT TQRegion +{ +public: + enum RegionType { Rectangle, Ellipse }; + + TQRegion(); + TQRegion( int x, int y, int w, int h, RegionType = Rectangle ); + TQRegion( const TQRect &, RegionType = Rectangle ); + TQRegion( const TQPointArray &, bool winding=FALSE ); + TQRegion( const TQRegion & ); + TQRegion( const TQBitmap & ); + ~TQRegion(); + TQRegion &operator=( const TQRegion & ); + + bool isNull() const; + bool isEmpty() const; + + bool contains( const TQPoint &p ) const; + bool contains( const TQRect &r ) const; + + void translate( int dx, int dy ); + + TQRegion unite( const TQRegion & ) const; + TQRegion intersect( const TQRegion &) const; + TQRegion subtract( const TQRegion & ) const; + TQRegion eor( const TQRegion & ) const; + + TQRect boundingRect() const; + TQMemArray rects() const; + void setRects( const TQRect *, int ); + + const TQRegion operator|( const TQRegion & ) const; + const TQRegion operator+( const TQRegion & ) const; + const TQRegion operator&( const TQRegion & ) const; + const TQRegion operator-( const TQRegion & ) const; + const TQRegion operator^( const TQRegion & ) const; + TQRegion& operator|=( const TQRegion & ); + TQRegion& operator+=( const TQRegion & ); + TQRegion& operator&=( const TQRegion & ); + TQRegion& operator-=( const TQRegion & ); + TQRegion& operator^=( const TQRegion & ); + + bool operator==( const TQRegion & ) const; + bool operator!=( const TQRegion &r ) const + { return !(operator==(r)); } + +#if defined(Q_WS_WIN) + HRGN handle() const { return data->rgn; } +#elif defined(Q_WS_X11) + Region handle() const { if(!data->rgn) updateX11Region(); return data->rgn; } +#elif defined(Q_WS_MAC) + RgnHandle handle(bool retquire_rgn=FALSE) const; +#elif defined(Q_WS_QWS) + // TQGfx_QWS needs this for region drawing + void * handle() const { return data->rgn; } +#endif + +#ifndef QT_NO_DATASTREAM + friend Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQRegion & ); + friend Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQRegion & ); +#endif +private: + TQRegion( bool ); + TQRegion copy() const; + void detach(); +#if defined(Q_WS_WIN) + TQRegion winCombine( const TQRegion &, int ) const; +#endif +#if defined(Q_WS_X11) + void updateX11Region() const; + void *clipRectangles( int &num ) const; + friend void *qt_getClipRects( const TQRegion &, int & ); +#endif + void exec( const TQByteArray &, int ver = 0 ); + struct TQRegionData : public TQShared { +#if defined(Q_WS_WIN) + HRGN rgn; +#elif defined(Q_WS_X11) + Region rgn; + void *xrectangles; + TQRegionPrivate *region; +#elif defined(Q_WS_MAC) + uint is_rect:1; + TQRect rect; + RgnHandle rgn; +#elif defined(Q_WS_QWS) + void * rgn; +#endif + bool is_null; + } *data; +#if defined(Q_WS_MAC) + friend struct qt_mac_rgn_data_cache; + friend TQRegionData *qt_mac_get_rgn_data(); + friend void qt_mac_free_rgn_data(TQRegionData *); + void rectifyRegion(); +#elif defined(Q_WS_WIN) + friend class TQETWidget; +#endif + +}; + + +#define TQRGN_SETRECT 1 // region stream commands +#define TQRGN_SETELLIPSE 2 // (these are internal) +#define TQRGN_SETPTARRAY_ALT 3 +#define TQRGN_SETPTARRAY_WIND 4 +#define TQRGN_TRANSLATE 5 +#define TQRGN_OR 6 +#define TQRGN_AND 7 +#define TQRGN_SUB 8 +#define TQRGN_XOR 9 +#define TQRGN_RECTS 10 + + +/***************************************************************************** + TQRegion stream functions + *****************************************************************************/ + +#ifndef QT_NO_DATASTREAM +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQRegion & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQRegion & ); +#endif + + +#endif // TQREGION_H diff --git a/src/kernel/qregion_x11.cpp b/src/kernel/qregion_x11.cpp new file mode 100644 index 000000000..ef44c08d6 --- /dev/null +++ b/src/kernel/qregion_x11.cpp @@ -0,0 +1,2898 @@ +/**************************************************************************** +** +** Implementation of TQRegion class for X11 +** +** Created : 940729 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qregion.h" +#include "qpointarray.h" +#include "qbuffer.h" +#include "qimage.h" +#include "qbitmap.h" +#include "qt_x11_p.h" + +#include + +// inline TQRect::setCoords +inline void qt_setCoords( TQRect *r, int xp1, int yp1, int xp2, int yp2 ) +{ + r->x1 = (TQCOORD)xp1; + r->y1 = (TQCOORD)yp1; + r->x2 = (TQCOORD)xp2; + r->y2 = (TQCOORD)yp2; +} + +/* + * clip region + */ + +struct TQRegionPrivate { + int numRects; + TQMemArray rects; + TQRect extents; + + TQRegionPrivate() { numRects = 0; } + TQRegionPrivate( const TQRect &r ) : rects(1) { + numRects = 1; + rects[0] = r; + extents = r; + } + + TQRegionPrivate( const TQRegionPrivate &r ) { + rects = r.rects.copy(); + numRects = r.numRects; + extents = r.extents; + } + + TQRegionPrivate &operator=( const TQRegionPrivate &r ) { + rects = r.rects.copy(); + numRects = r.numRects; + extents = r.extents; + return *this; + } + +}; + + +static void UnionRegion(TQRegionPrivate *reg1, TQRegionPrivate *reg2, TQRegionPrivate *newReg); +static void IntersectRegion(TQRegionPrivate *reg1, TQRegionPrivate *reg2, register TQRegionPrivate *newReg); +static void miRegionOp(register TQRegionPrivate *newReg, TQRegionPrivate *reg1, TQRegionPrivate *reg2, + void (*overlapFunc)(...), + void (*nonOverlap1Func)(...), + void (*nonOverlap2Func)(...)); +#define RectangleOut 0 +#define RectangleIn 1 +#define RectanglePart 2 +#define EvenOddRule 0 +#define WindingRule 1 + +// START OF region.h extract +/* $XConsortium: region.h,v 11.14 94/04/17 20:22:20 rws Exp $ */ +/************************************************************************ + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987 by Digital Etquipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSETQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ + +#ifndef _XREGION_H +#define _XREGION_H + +#include + +#ifndef MAX +#define MAX(a,b) (((a) > (b)) ? (a) : (b)) +#endif +#ifndef MIN +#define MIN(a,b) (((a) < (b)) ? (a) : (b)) +#endif + + +/* 1 if two BOXs overlap. + * 0 if two BOXs do not overlap. + * Remember, x2 and y2 are not in the region + */ +#define EXTENTCHECK(r1, r2) \ + ((r1)->right() >= (r2)->left() && \ + (r1)->left() <= (r2)->right() && \ + (r1)->bottom() >= (r2)->top() && \ + (r1)->top() <= (r2)->bottom()) + +/* + * update region extents + */ +#define EXTENTS(r,idRect){\ + if((r)->left() < (idRect)->extents.left())\ + (idRect)->extents.setLeft( (r)->left() );\ + if((r)->top() < (idRect)->extents.top())\ + (idRect)->extents.setTop( (r)->top() );\ + if((r)->right() > (idRect)->extents.right())\ + (idRect)->extents.setRight( (r)->right() );\ + if((r)->bottom() > (idRect)->extents.bottom())\ + (idRect)->extents.setBottom( (r)->bottom() );\ + } + +/* + * Check to see if there is enough memory in the present region. + */ +#define MEMCHECK(reg, rect, firstrect){\ + if ((reg)->numRects >= (int)((reg)->rects.size()-1)){\ + firstrect.resize(firstrect.size() * 2); \ + (rect) = (firstrect).data() + (reg)->numRects;\ + }\ + } + + +#define EMPTY_REGION(pReg) pReg->numRects = 0 + +#define REGION_NOT_EMPTY(pReg) pReg->numRects + +/* + * number of points to buffer before sending them off + * to scanlines() : Must be an even number + */ +#define NUMPTSTOBUFFER 200 + +/* + * used to allocate buffers for points and link + * the buffers together + */ +typedef struct _POINTBLOCK { + TQPoint pts[NUMPTSTOBUFFER]; + struct _POINTBLOCK *next; +} POINTBLOCK; + +#endif +// END OF region.h extract + +// START OF Region.c extract +/* $XConsortium: Region.c /main/30 1996/10/22 14:21:24 kaleb $ */ +/************************************************************************ + +Copyright (c) 1987, 1988 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987, 1988 by Digital Etquipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSETQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ +/* + * The functions in this file implement the Region abstraction, similar to one + * used in the X11 sample server. A Region is simply an area, as the name + * implies, and is implemented as a "y-x-banded" array of rectangles. To + * explain: Each Region is made up of a certain number of rectangles sorted + * by y coordinate first, and then by x coordinate. + * + * Furthermore, the rectangles are banded such that every rectangle with a + * given upper-left y coordinate (y1) will have the same lower-right y + * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it + * will span the entire vertical distance of the band. This means that some + * areas that could be merged into a taller rectangle will be represented as + * several shorter rectangles to account for shorter rectangles to its left + * or right but within its "vertical scope". + * + * An added constraint on the rectangles is that they must cover as much + * horizontal area as possible. E.g. no two rectangles in a band are allowed + * to touch. + * + * Whenever possible, bands will be merged together to cover a greater vertical + * distance (and thus reduce the number of rectangles). Two bands can be merged + * only if the bottom of one touches the top of the other and they have + * rectangles in the same places (of the same width, of course). This maintains + * the y-x-banding that's so nice to have... + */ +/* $XFree86: xc/lib/X11/Region.c,v 1.1.1.2.2.2 1998/10/04 15:22:50 hohndel Exp $ */ + +typedef void (*voidProcp)(...); + + +static +void UnionRectWithRegion(register const TQRect *rect, TQRegionPrivate *source, TQRegionPrivate *dest) +{ + TQRegionPrivate region; + + if (!rect->width() || !rect->height()) + return; + region.rects.resize(1); + region.numRects = 1; + region.rects[0] = *rect; + region.extents = *rect; + + UnionRegion(®ion, source, dest); + return; +} + +/*- + *----------------------------------------------------------------------- + * miSetExtents -- + * Reset the extents of a region to what they should be. Called by + * miSubtract and miIntersect b/c they can't figure it out along the + * way or do so easily, as miUnion can. + * + * Results: + * None. + * + * Side Effects: + * The region's 'extents' structure is overwritten. + * + *----------------------------------------------------------------------- + */ +static void +miSetExtents (TQRegionPrivate *pReg) +{ + register TQRect *pBox, + *pBoxEnd, + *pExtents; + + if (pReg->numRects == 0) + { + qt_setCoords(&pReg->extents, 0, 0, 0, 0); + return; + } + + pExtents = &pReg->extents; + pBox = pReg->rects.data(); + pBoxEnd = &pBox[pReg->numRects - 1]; + + /* + * Since pBox is the first rectangle in the region, it must have the + * smallest y1 and since pBoxEnd is the last rectangle in the region, + * it must have the largest y2, because of banding. Initialize x1 and + * x2 from pBox and pBoxEnd, resp., as good things to initialize them + * to... + */ + pExtents->setLeft( pBox->left() ); + pExtents->setTop( pBox->top() ); + pExtents->setRight( pBoxEnd->right() ); + pExtents->setBottom( pBoxEnd->bottom() ); + + Q_ASSERT(pExtents->top() <= pExtents->bottom()); + while (pBox <= pBoxEnd) + { + if (pBox->left() < pExtents->left()) + { + pExtents->setLeft( pBox->left() ); + } + if (pBox->right() > pExtents->right()) + { + pExtents->setRight( pBox->right() ); + } + pBox++; + } + Q_ASSERT(pExtents->left() <= pExtents->right()); +} + + +/* TranslateRegion(pRegion, x, y) + translates in place + added by raymond +*/ + +static +int +OffsetRegion(register TQRegionPrivate *pRegion, register int x, register int y) +{ + register int nbox; + register TQRect *pbox; + + pbox = pRegion->rects.data(); + nbox = pRegion->numRects; + + while(nbox--) + { + pbox->moveBy(x, y); + pbox++; + } + pRegion->extents.moveBy(x, y); + return 1; +} + +/*====================================================================== + * Region Intersection + *====================================================================*/ +/*- + *----------------------------------------------------------------------- + * miIntersectO -- + * Handle an overlapping band for miIntersect. + * + * Results: + * None. + * + * Side Effects: + * Rectangles may be added to the region. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static +int +miIntersectO (register TQRegionPrivate *pReg, register TQRect *r1, TQRect *r1End, + register TQRect *r2, TQRect *r2End, int y1, int y2) +{ + register int x1; + register int x2; + register TQRect *pNextRect; + + pNextRect = pReg->rects.data() + pReg->numRects; + + while ((r1 != r1End) && (r2 != r2End)) + { + x1 = TQMAX(r1->left(),r2->left()); + x2 = TQMIN(r1->right(),r2->right()); + + /* + * If there's any overlap between the two rectangles, add that + * overlap to the new region. + * There's no need to check for subsumption because the only way + * such a need could arise is if some region has two rectangles + * right next to each other. Since that should never happen... + */ + if (x1 <= x2) + { + Q_ASSERT(y1<=y2); + + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, x1, y1, x2, y2 ); + pReg->numRects++; + pNextRect++; + } + + /* + * Need to advance the pointers. Shift the one that extends + * to the right the least, since the other still has a chance to + * overlap with that region's next rectangle, if you see what I mean. + */ + if (r1->right() < r2->right()) + { + r1++; + } + else if (r2->right() < r1->right()) + { + r2++; + } + else + { + r1++; + r2++; + } + } + return 0; /* lint */ +} + +static +void +IntersectRegion(TQRegionPrivate *reg1, TQRegionPrivate *reg2, register TQRegionPrivate *newReg) +{ + /* check for trivial reject */ + if ( (!(reg1->numRects)) || (!(reg2->numRects)) || + (!EXTENTCHECK(®1->extents, ®2->extents))) + newReg->numRects = 0; + else + miRegionOp (newReg, reg1, reg2, + (voidProcp) miIntersectO, (voidProcp) NULL, (voidProcp) NULL); + + /* + * Can't alter newReg's extents before we call miRegionOp because + * it might be one of the source regions and miRegionOp depends + * on the extents of those regions being the same. Besides, this + * way there's no checking against rectangles that will be nuked + * due to coalescing, so we have to examine fewer rectangles. + */ + miSetExtents(newReg); + return; +} + +/*====================================================================== + * Generic Region Operator + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miCoalesce -- + * Attempt to merge the boxes in the current band with those in the + * previous one. Used only by miRegionOp. + * + * Results: + * The new index for the previous band. + * + * Side Effects: + * If coalescing takes place: + * - rectangles in the previous band will have their y2 fields + * altered. + * - pReg->numRects will be decreased. + * + *----------------------------------------------------------------------- + */ +/* static int*/ +static +int +miCoalesce (register TQRegionPrivate *pReg, int prevStart, int curStart) + //Region pReg; /* Region to coalesce */ + //prevStart; /* Index of start of previous band */ + //curStart; /* Index of start of current band */ +{ + register TQRect *pPrevBox; /* Current box in previous band */ + register TQRect *pCurBox; /* Current box in current band */ + register TQRect *pRegEnd; /* End of region */ + int curNumRects; /* Number of rectangles in current + * band */ + int prevNumRects; /* Number of rectangles in previous + * band */ + int bandY1; /* Y1 coordinate for current band */ + + pRegEnd = pReg->rects.data() + pReg->numRects; + + pPrevBox = pReg->rects.data() + prevStart; + prevNumRects = curStart - prevStart; + + /* + * Figure out how many rectangles are in the current band. Have to do + * this because multiple bands could have been added in miRegionOp + * at the end when one region has been exhausted. + */ + pCurBox = pReg->rects.data() + curStart; + bandY1 = pCurBox->top(); + for (curNumRects = 0; + (pCurBox != pRegEnd) && (pCurBox->top() == bandY1); + curNumRects++) + { + pCurBox++; + } + + if (pCurBox != pRegEnd) + { + /* + * If more than one band was added, we have to find the start + * of the last band added so the next coalescing job can start + * at the right place... (given when multiple bands are added, + * this may be pointless -- see above). + */ + pRegEnd--; + while ((pRegEnd-1)->top() == pRegEnd->top()) + { + pRegEnd--; + } + curStart = pRegEnd - pReg->rects.data(); + pRegEnd = pReg->rects.data() + pReg->numRects; + } + + if ((curNumRects == prevNumRects) && (curNumRects != 0)) { + pCurBox -= curNumRects; + /* + * The bands may only be coalesced if the bottom of the previous + * matches the top scanline of the current. + */ + if (pPrevBox->bottom() == pCurBox->top() - 1) + { + /* + * Make sure the bands have boxes in the same places. This + * assumes that boxes have been added in such a way that they + * cover the most area possible. I.e. two boxes in a band must + * have some horizontal space between them. + */ + do + { + if ((pPrevBox->left() != pCurBox->left()) || + (pPrevBox->right() != pCurBox->right())) + { + /* + * The bands don't line up so they can't be coalesced. + */ + return (curStart); + } + pPrevBox++; + pCurBox++; + prevNumRects -= 1; + } while (prevNumRects != 0); + + pReg->numRects -= curNumRects; + pCurBox -= curNumRects; + pPrevBox -= curNumRects; + + /* + * The bands may be merged, so set the bottom y of each box + * in the previous band to that of the corresponding box in + * the current band. + */ + do + { + pPrevBox->setBottom( pCurBox->bottom() ); + pPrevBox++; + pCurBox++; + curNumRects -= 1; + } while (curNumRects != 0); + + /* + * If only one band was added to the region, we have to backup + * curStart to the start of the previous band. + * + * If more than one band was added to the region, copy the + * other bands down. The assumption here is that the other bands + * came from the same region as the current one and no further + * coalescing can be done on them since it's all been done + * already... curStart is already in the right place. + */ + if (pCurBox == pRegEnd) + { + curStart = prevStart; + } + else + { + do + { + *pPrevBox++ = *pCurBox++; + } while (pCurBox != pRegEnd); + } + + } + } + return (curStart); +} + +/*- + *----------------------------------------------------------------------- + * miRegionOp -- + * Apply an operation to two regions. Called by miUnion, miInverse, + * miSubtract, miIntersect... + * + * Results: + * None. + * + * Side Effects: + * The new region is overwritten. + * + * Notes: + * The idea behind this function is to view the two regions as sets. + * Together they cover a rectangle of area that this function divides + * into horizontal bands where points are covered only by one region + * or by both. For the first case, the nonOverlapFunc is called with + * each the band and the band's upper and lower extents. For the + * second, the overlapFunc is called to process the entire band. It + * is responsible for clipping the rectangles in the band, though + * this function provides the boundaries. + * At the end of each band, the new region is coalesced, if possible, + * to reduce the number of rectangles in the region. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static void +miRegionOp(register TQRegionPrivate *newReg, TQRegionPrivate *reg1, TQRegionPrivate *reg2, + void (*overlapFunc)(...), + void (*nonOverlap1Func)(...), + void (*nonOverlap2Func)(...)) + //register Region newReg; /* Place to store result */ + //Region reg1; /* First region in operation */ + //Region reg2; /* 2d region in operation */ + //void (*overlapFunc)(); /* Function to call for over- + //* lapping bands */ + //void (*nonOverlap1Func)(); /* Function to call for non- + //* overlapping bands in region + //* 1 */ + //void (*nonOverlap2Func)(); /* Function to call for non- + //* overlapping bands in region + //* 2 */ +{ + register TQRect *r1; /* Pointer into first region */ + register TQRect *r2; /* Pointer into 2d region */ + TQRect *r1End; /* End of 1st region */ + TQRect *r2End; /* End of 2d region */ + register int ybot; /* Bottom of intersection */ + register int ytop; /* Top of intersection */ + int prevBand; /* Index of start of + * previous band in newReg */ + int curBand; /* Index of start of current + * band in newReg */ + register TQRect *r1BandEnd; /* End of current band in r1 */ + register TQRect *r2BandEnd; /* End of current band in r2 */ + int top; /* Top of non-overlapping + * band */ + int bot; /* Bottom of non-overlapping + * band */ + + /* + * Initialization: + * set r1, r2, r1End and r2End appropriately, preserve the important + * parts of the destination region until the end in case it's one of + * the two source regions, then mark the "new" region empty, allocating + * another array of rectangles for it to use. + */ + r1 = reg1->rects.data(); + r2 = reg2->rects.data(); + r1End = r1 + reg1->numRects; + r2End = r2 + reg2->numRects; + + TQMemArray oldRects = newReg->rects; + + newReg->rects.detach(); + EMPTY_REGION(newReg); + + /* + * Allocate a reasonable number of rectangles for the new region. The idea + * is to allocate enough so the individual functions don't need to + * reallocate and copy the array, which is time consuming, yet we don't + * have to worry about using too much memory. I hope to be able to + * nuke the realloc() at the end of this function eventually. + */ + newReg->rects.resize( TQMAX(reg1->numRects,reg2->numRects) * 2 ); + + /* + * Initialize ybot and ytop. + * In the upcoming loop, ybot and ytop serve different functions depending + * on whether the band being handled is an overlapping or non-overlapping + * band. + * In the case of a non-overlapping band (only one of the regions + * has points in the band), ybot is the bottom of the most recent + * intersection and thus clips the top of the rectangles in that band. + * ytop is the top of the next intersection between the two regions and + * serves to clip the bottom of the rectangles in the current band. + * For an overlapping band (where the two regions intersect), ytop clips + * the top of the rectangles of both regions and ybot clips the bottoms. + */ + if (reg1->extents.top() < reg2->extents.top()) + ybot = reg1->extents.top() - 1; + else + ybot = reg2->extents.top() - 1; + + /* + * prevBand serves to mark the start of the previous band so rectangles + * can be coalesced into larger rectangles. qv. miCoalesce, above. + * In the beginning, there is no previous band, so prevBand == curBand + * (curBand is set later on, of course, but the first band will always + * start at index 0). prevBand and curBand must be indices because of + * the possible expansion, and resultant moving, of the new region's + * array of rectangles. + */ + prevBand = 0; + + do + { + curBand = newReg->numRects; + + /* + * This algorithm proceeds one source-band (as opposed to a + * destination band, which is determined by where the two regions + * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the + * rectangle after the last one in the current band for their + * respective regions. + */ + r1BandEnd = r1; + while ((r1BandEnd != r1End) && (r1BandEnd->top() == r1->top())) + { + r1BandEnd++; + } + + r2BandEnd = r2; + while ((r2BandEnd != r2End) && (r2BandEnd->top() == r2->top())) + { + r2BandEnd++; + } + + /* + * First handle the band that doesn't intersect, if any. + * + * Note that attention is restricted to one band in the + * non-intersecting region at once, so if a region has n + * bands between the current position and the next place it overlaps + * the other, this entire loop will be passed through n times. + */ + if (r1->top() < r2->top()) + { + top = TQMAX(r1->top(),ybot+1); + bot = TQMIN(r1->bottom(),r2->top()-1); + + if ((nonOverlap1Func != (voidProcp)NULL) && bot >= top) + { + (* nonOverlap1Func) (newReg, r1, r1BandEnd, top, bot); + } + + ytop = r2->top(); + } + else if (r2->top() < r1->top()) + { + top = TQMAX(r2->top(),ybot+1); + bot = TQMIN(r2->bottom(),r1->top()-1); + + if ((nonOverlap2Func != (voidProcp)NULL) && bot >= top) + { + (* nonOverlap2Func) (newReg, r2, r2BandEnd, top, bot); + } + + ytop = r1->top(); + } + else + { + ytop = r1->top(); + } + + /* + * If any rectangles got added to the region, try and coalesce them + * with rectangles from the previous band. Note we could just do + * this test in miCoalesce, but some machines incur a not + * inconsiderable cost for function calls, so... + */ + if (newReg->numRects != curBand) + { + prevBand = miCoalesce (newReg, prevBand, curBand); + } + + /* + * Now see if we've hit an intersecting band. The two bands only + * intersect if ybot >= ytop + */ + ybot = TQMIN(r1->bottom(), r2->bottom()); + curBand = newReg->numRects; + if (ybot >= ytop) + { + (* overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot); + + } + + if (newReg->numRects != curBand) + { + prevBand = miCoalesce (newReg, prevBand, curBand); + } + + /* + * If we've finished with a band (y2 == ybot) we skip forward + * in the region to the next band. + */ + if (r1->bottom() == ybot) + { + r1 = r1BandEnd; + } + if (r2->bottom() == ybot) + { + r2 = r2BandEnd; + } + } while ((r1 != r1End) && (r2 != r2End)); + + /* + * Deal with whichever region still has rectangles left. + */ + curBand = newReg->numRects; + if (r1 != r1End) + { + if (nonOverlap1Func != (voidProcp)NULL) + { + do + { + r1BandEnd = r1; + while ((r1BandEnd < r1End) && (r1BandEnd->top() == r1->top())) + { + r1BandEnd++; + } + (* nonOverlap1Func) (newReg, r1, r1BandEnd, + TQMAX(r1->top(),ybot+1), r1->bottom()); + r1 = r1BandEnd; + } while (r1 != r1End); + } + } + else if ((r2 != r2End) && (nonOverlap2Func != (voidProcp)NULL)) + { + do + { + r2BandEnd = r2; + while ((r2BandEnd < r2End) && (r2BandEnd->top() == r2->top())) + { + r2BandEnd++; + } + (* nonOverlap2Func) (newReg, r2, r2BandEnd, + TQMAX(r2->top(),ybot+1), r2->bottom()); + r2 = r2BandEnd; + } while (r2 != r2End); + } + + if (newReg->numRects != curBand) + { + (void) miCoalesce (newReg, prevBand, curBand); + } + + /* + * A bit of cleanup. To keep regions from growing without bound, + * we shrink the array of rectangles to match the new number of + * rectangles in the region. This never goes to 0, however... + * + * Only do this stuff if the number of rectangles allocated is more than + * twice the number of rectangles in the region (a simple optimization...). + */ + if (newReg->numRects < (int)(newReg->rects.size() >> 1)) + { + if (REGION_NOT_EMPTY(newReg)) + { + newReg->rects.resize(newReg->numRects); + } + else + { + /* + * No point in doing the extra work involved in an realloc if + * the region is empty + */ + newReg->rects.resize(1); + } + } + return; +} + + +/*====================================================================== + * Region Union + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miUnionNonO -- + * Handle a non-overlapping band for the union operation. Just + * Adds the rectangles into the region. Doesn't have to check for + * subsumption or anything. + * + * Results: + * None. + * + * Side Effects: + * pReg->numRects is incremented and the final rectangles overwritten + * with the rectangles we're passed. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static +int +miUnionNonO (register TQRegionPrivate *pReg, register TQRect * r, + TQRect * rEnd, register int y1, register int y2) +{ + register TQRect * pNextRect; + + pNextRect = pReg->rects.data() + pReg->numRects; + + Q_ASSERT(y1 <= y2); + + while (r != rEnd) + { + Q_ASSERT(r->left() <= r->right()); + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, r->left(), y1, r->right(), y2 ); + pReg->numRects++; + pNextRect++; + + r++; + } + return 0; /* lint */ +} + + +/*- + *----------------------------------------------------------------------- + * miUnionO -- + * Handle an overlapping band for the union operation. Picks the + * left-most rectangle each time and merges it into the region. + * + * Results: + * None. + * + * Side Effects: + * Rectangles are overwritten in pReg->rects and pReg->numRects will + * be changed. + * + *----------------------------------------------------------------------- + */ + +/* static void*/ +static +int +miUnionO (register TQRegionPrivate *pReg, register TQRect *r1, TQRect *r1End, + register TQRect *r2, TQRect *r2End, register int y1, register int y2) +{ + register TQRect *pNextRect; + + pNextRect = pReg->rects.data() + pReg->numRects; + +#define MERGERECT(r) \ + if ((pReg->numRects != 0) && \ + (pNextRect[-1].top() == y1) && \ + (pNextRect[-1].bottom() == y2) && \ + (pNextRect[-1].right() >= r->left()-1)) { \ + if (pNextRect[-1].right() < r->right()) { \ + pNextRect[-1].setRight( r->right() ); \ + Q_ASSERT(pNextRect[-1].left() <= pNextRect[-1].right()); \ + } \ + } else { \ + MEMCHECK(pReg, pNextRect, pReg->rects) \ + qt_setCoords( pNextRect, r->left(), y1, r->right(), y2 ); \ + pReg->numRects++; \ + pNextRect++; \ + } \ + r++; + + Q_ASSERT (y1<=y2); + while ((r1 != r1End) && (r2 != r2End)) { + if (r1->left() < r2->left()) { + MERGERECT(r1) + } else { + MERGERECT(r2) + } + } + + if (r1 != r1End) + { + do + { + MERGERECT(r1) + } while (r1 != r1End); + } + else while (r2 != r2End) + { + MERGERECT(r2) + } + return 0; /* lint */ +} + +static void UnionRegion(TQRegionPrivate *reg1, TQRegionPrivate *reg2, TQRegionPrivate *newReg) +{ + /* checks all the simple cases */ + + /* + * Region 1 and 2 are the same or region 1 is empty + */ + if ( (reg1 == reg2) || (!(reg1->numRects)) ) + { + *newReg = *reg2; + return; + } + + /* + * if nothing to union (region 2 empty) + */ + if (!(reg2->numRects)) + { + *newReg = *reg1; + return; + } + + /* + * Region 1 completely subsumes region 2 + */ + if ((reg1->numRects == 1) && + (reg1->extents.left() <= reg2->extents.left()) && + (reg1->extents.top() <= reg2->extents.top()) && + (reg1->extents.right() >= reg2->extents.right()) && + (reg1->extents.bottom() >= reg2->extents.bottom())) + { + *newReg = *reg1; + return; + } + + /* + * Region 2 completely subsumes region 1 + */ + if ((reg2->numRects == 1) && + (reg2->extents.left() <= reg1->extents.left()) && + (reg2->extents.top() <= reg1->extents.top()) && + (reg2->extents.right() >= reg1->extents.right()) && + (reg2->extents.bottom() >= reg1->extents.bottom())) + { + *newReg = *reg2; + return; + } + + miRegionOp (newReg, reg1, reg2, (voidProcp) miUnionO, + (voidProcp) miUnionNonO, (voidProcp) miUnionNonO); + + qt_setCoords( &newReg->extents, + TQMIN(reg1->extents.left(), reg2->extents.left()), + TQMIN(reg1->extents.top(), reg2->extents.top()), + TQMAX(reg1->extents.right(), reg2->extents.right()), + TQMAX(reg1->extents.bottom(), reg2->extents.bottom()) ); + + return; +} + +/*====================================================================== + * Region Subtraction + *====================================================================*/ + +/*- + *----------------------------------------------------------------------- + * miSubtractNonO -- + * Deal with non-overlapping band for subtraction. Any parts from + * region 2 we discard. Anything from region 1 we add to the region. + * + * Results: + * None. + * + * Side Effects: + * pReg may be affected. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static +int +miSubtractNonO1 (register TQRegionPrivate *pReg, register TQRect *r, + TQRect *rEnd, register int y1, register int y2) +{ + register TQRect *pNextRect; + + pNextRect = pReg->rects.data() + pReg->numRects; + + Q_ASSERT(y1<=y2); + + while (r != rEnd) + { + Q_ASSERT(r->left()<=r->right()); + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, r->left(), y1, r->right(), y2 ); + pReg->numRects++; + pNextRect++; + + r++; + } + return 0; /* lint */ +} + +/*- + *----------------------------------------------------------------------- + * miSubtractO -- + * Overlapping band subtraction. x1 is the left-most point not yet + * checked. + * + * Results: + * None. + * + * Side Effects: + * pReg may have rectangles added to it. + * + *----------------------------------------------------------------------- + */ +/* static void*/ +static +int +miSubtractO (register TQRegionPrivate *pReg, register TQRect *r1, TQRect *r1End, + register TQRect *r2, TQRect *r2End, register int y1, register int y2) +{ + register TQRect *pNextRect; + register int x1; + + x1 = r1->left(); + + Q_ASSERT(y1<=y2); + pNextRect = pReg->rects.data() + pReg->numRects; + + while ((r1 != r1End) && (r2 != r2End)) + { + if (r2->right() < x1) + { + /* + * Subtrahend missed the boat: go to next subtrahend. + */ + r2++; + } + else if (r2->left() <= x1) + { + /* + * Subtrahend precedes minuend: nuke left edge of minuend. + */ + x1 = r2->right()+1; + if (x1 > r1->right()) + { + /* + * Minuend completely covered: advance to next minuend and + * reset left fence to edge of new minuend. + */ + r1++; + if (r1 != r1End) + x1 = r1->left(); + } + else + { + /* + * Subtrahend now used up since it doesn't extend beyond + * minuend + */ + r2++; + } + } + else if (r2->left() <= r1->right()) + { + /* + * Left part of subtrahend covers part of minuend: add uncovered + * part of minuend to region and skip to next subtrahend. + */ + Q_ASSERT(x1left()); + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, x1, y1, r2->left() - 1, y2 ); + pReg->numRects++; + pNextRect++; + + x1 = r2->right() + 1; + if (x1 > r1->right()) + { + /* + * Minuend used up: advance to new... + */ + r1++; + if (r1 != r1End) + x1 = r1->left(); + } + else + { + /* + * Subtrahend used up + */ + r2++; + } + } + else + { + /* + * Minuend used up: add any remaining piece before advancing. + */ + if (r1->right() >= x1) + { + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, x1, y1, r1->right(), y2 ); + pReg->numRects++; + pNextRect++; + } + r1++; + if ( r1 != r1End ) + x1 = r1->left(); + } + } + + /* + * Add remaining minuend rectangles to region. + */ + while (r1 != r1End) + { + Q_ASSERT(x1<=r1->right()); + MEMCHECK(pReg, pNextRect, pReg->rects) + qt_setCoords( pNextRect, x1, y1, r1->right(), y2 ); + pReg->numRects++; + pNextRect++; + + r1++; + if (r1 != r1End) + { + x1 = r1->left(); + } + } + return 0; /* lint */ +} + +/*- + *----------------------------------------------------------------------- + * miSubtract -- + * Subtract regS from regM and leave the result in regD. + * S stands for subtrahend, M for minuend and D for difference. + * + * Side Effects: + * regD is overwritten. + * + *----------------------------------------------------------------------- + */ + +static void SubtractRegion(TQRegionPrivate *regM, TQRegionPrivate *regS, register TQRegionPrivate *regD) +{ + /* check for trivial reject */ + if ( (!(regM->numRects)) || (!(regS->numRects)) || + (!EXTENTCHECK(®M->extents, ®S->extents)) ) + { + *regD = *regM; + return; + } + + miRegionOp (regD, regM, regS, (voidProcp) miSubtractO, + (voidProcp) miSubtractNonO1, (voidProcp) NULL); + + /* + * Can't alter newReg's extents before we call miRegionOp because + * it might be one of the source regions and miRegionOp depends + * on the extents of those regions being the unaltered. Besides, this + * way there's no checking against rectangles that will be nuked + * due to coalescing, so we have to examine fewer rectangles. + */ + miSetExtents (regD); +} + +static void XorRegion( TQRegionPrivate *sra, TQRegionPrivate *srb, TQRegionPrivate *dr ) +{ + TQRegionPrivate tra, trb; + + SubtractRegion(sra,srb,&tra); + SubtractRegion(srb,sra,&trb); + UnionRegion(&tra,&trb,dr); +} + +/* + * Check to see if two regions are equal + */ +static bool EqualRegion( TQRegionPrivate *r1, TQRegionPrivate *r2 ) +{ + int i; + + if( r1->numRects != r2->numRects ) return FALSE; + else if( r1->numRects == 0 ) return TRUE; + else if ( r1->extents.left() != r2->extents.left() || + r1->extents.right() != r2->extents.right() || + r1->extents.top() != r2->extents.top() || + r1->extents.bottom() != r2->extents.bottom() ) + return FALSE; + else { + TQRect *rr1 = r1->rects.data(); + TQRect *rr2 = r2->rects.data(); + for( i=0; i < r1->numRects; i++, rr1++, rr2++ ) { + if ( rr1->left() != rr2->left() || + rr1->right() != rr2->right() || + rr1->top() != rr2->top() || + rr1->bottom() != rr2->bottom() ) + return FALSE; + } + } + return TRUE; +} + +static bool PointInRegion( TQRegionPrivate *pRegion, int x, int y ) +{ + int i; + + if (pRegion->numRects == 0) + return FALSE; + if (!pRegion->extents.contains(x, y)) + return FALSE; + for (i=0; inumRects; i++) + { + if (pRegion->rects[i].contains(x, y)) + return TRUE; + } + return FALSE; +} + +static bool RectInRegion(register TQRegionPrivate *region, + int rx, int ry, unsigned int rwidth, unsigned int rheight) +{ + register TQRect *pbox; + register TQRect *pboxEnd; + TQRect rect(rx, ry, rwidth, rheight); + register TQRect *prect = ▭ + int partIn, partOut; + + /* this is (just) a useful optimization */ + if ((region->numRects == 0) || !EXTENTCHECK(®ion->extents, prect)) + return(RectangleOut); + + partOut = FALSE; + partIn = FALSE; + + /* can stop when both partOut and partIn are TRUE, or we reach prect->y2 */ + for (pbox = region->rects.data(), pboxEnd = pbox + region->numRects; + pbox < pboxEnd; + pbox++) + { + + if (pbox->bottom() < ry) + continue; /* getting up to speed or skipping remainder of band */ + + if (pbox->top() > ry) + { + partOut = TRUE; /* missed part of rectangle above */ + if (partIn || (pbox->top() > prect->bottom())) + break; + ry = pbox->top(); /* x guaranteed to be == prect->x1 */ + } + + if (pbox->right() < rx) + continue; /* not far enough over yet */ + + if (pbox->left() > rx) + { + partOut = TRUE; /* missed part of rectangle to left */ + if (partIn) + break; + } + + if (pbox->left() <= prect->right()) + { + partIn = TRUE; /* definitely overlap */ + if (partOut) + break; + } + + if (pbox->right() >= prect->right()) + { + ry = pbox->bottom() + 1; /* finished with this band */ + if (ry > prect->bottom()) + break; + rx = prect->left(); /* reset x out to left again */ + } else + { + /* + * Because boxes in a band are maximal width, if the first box + * to overlap the rectangle doesn't completely cover it in that + * band, the rectangle must be partially out, since some of it + * will be uncovered in that band. partIn will have been set true + * by now... + */ + break; + } + + } + + return(partIn ? ((ry <= prect->bottom()) ? RectanglePart : RectangleIn) : + RectangleOut); +} +// END OF Region.c extract +// START OF poly.h extract +/* $XConsortium: poly.h,v 1.4 94/04/17 20:22:19 rws Exp $ */ +/************************************************************************ + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987 by Digital Etquipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSETQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ + +/* + * This file contains a few macros to help track + * the edge of a filled object. The object is assumed + * to be filled in scanline order, and thus the + * algorithm used is an extension of Bresenham's line + * drawing algorithm which assumes that y is always the + * major axis. + * Since these pieces of code are the same for any filled shape, + * it is more convenient to gather the library in one + * place, but since these pieces of code are also in + * the inner loops of output primitives, procedure call + * overhead is out of the question. + * See the author for a derivation if needed. + */ + + +/* + * In scan converting polygons, we want to choose those pixels + * which are inside the polygon. Thus, we add .5 to the starting + * x coordinate for both left and right edges. Now we choose the + * first pixel which is inside the pgon for the left edge and the + * first pixel which is outside the pgon for the right edge. + * Draw the left pixel, but not the right. + * + * How to add .5 to the starting x coordinate: + * If the edge is moving to the right, then subtract dy from the + * error term from the general form of the algorithm. + * If the edge is moving to the left, then add dy to the error term. + * + * The reason for the difference between edges moving to the left + * and edges moving to the right is simple: If an edge is moving + * to the right, then we want the algorithm to flip immediately. + * If it is moving to the left, then we don't want it to flip until + * we traverse an entire pixel. + */ +#define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \ + int dx; /* local storage */ \ +\ + /* \ + * if the edge is horizontal, then it is ignored \ + * and assumed not to be processed. Otherwise, do this stuff. \ + */ \ + if ((dy) != 0) { \ + xStart = (x1); \ + dx = (x2) - xStart; \ + if (dx < 0) { \ + m = dx / (dy); \ + m1 = m - 1; \ + incr1 = -2 * dx + 2 * (dy) * m1; \ + incr2 = -2 * dx + 2 * (dy) * m; \ + d = 2 * m * (dy) - 2 * dx - 2 * (dy); \ + } else { \ + m = dx / (dy); \ + m1 = m + 1; \ + incr1 = 2 * dx - 2 * (dy) * m1; \ + incr2 = 2 * dx - 2 * (dy) * m; \ + d = -2 * m * (dy) + 2 * dx; \ + } \ + } \ +} + +#define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \ + if (m1 > 0) { \ + if (d > 0) { \ + minval += m1; \ + d += incr1; \ + } \ + else { \ + minval += m; \ + d += incr2; \ + } \ + } else {\ + if (d >= 0) { \ + minval += m1; \ + d += incr1; \ + } \ + else { \ + minval += m; \ + d += incr2; \ + } \ + } \ +} + + +/* + * This structure contains all of the information needed + * to run the bresenham algorithm. + * The variables may be hardcoded into the declarations + * instead of using this structure to make use of + * register declarations. + */ +typedef struct { + int minor_axis; /* minor axis */ + int d; /* decision variable */ + int m, m1; /* slope and slope+1 */ + int incr1, incr2; /* error increments */ +} BRESINFO; + + +#define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \ + BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \ + bres.m, bres.m1, bres.incr1, bres.incr2) + +#define BRESINCRPGONSTRUCT(bres) \ + BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2) + + + +/* + * These are the data structures needed to scan + * convert regions. Two different scan conversion + * methods are available -- the even-odd method, and + * the winding number method. + * The even-odd rule states that a point is inside + * the polygon if a ray drawn from that point in any + * direction will pass through an odd number of + * path segments. + * By the winding number rule, a point is decided + * to be inside the polygon if a ray drawn from that + * point in any direction passes through a different + * number of clockwise and counter-clockwise path + * segments. + * + * These data structures are adapted somewhat from + * the algorithm in (Foley/Van Dam) for scan converting + * polygons. + * The basic algorithm is to start at the top (smallest y) + * of the polygon, stepping down to the bottom of + * the polygon by incrementing the y coordinate. We + * keep a list of edges which the current scanline crosses, + * sorted by x. This list is called the Active Edge Table (AET) + * As we change the y-coordinate, we update each entry in + * in the active edge table to reflect the edges new xcoord. + * This list must be sorted at each scanline in case + * two edges intersect. + * We also keep a data structure known as the Edge Table (ET), + * which keeps track of all the edges which the current + * scanline has not yet reached. The ET is basically a + * list of ScanLineList structures containing a list of + * edges which are entered at a given scanline. There is one + * ScanLineList per scanline at which an edge is entered. + * When we enter a new edge, we move it from the ET to the AET. + * + * From the AET, we can implement the even-odd rule as in + * (Foley/Van Dam). + * The winding number rule is a little trickier. We also + * keep the EdgeTableEntries in the AET linked by the + * nextWETE (winding EdgeTableEntry) link. This allows + * the edges to be linked just as before for updating + * purposes, but only uses the edges linked by the nextWETE + * link as edges representing spans of the polygon to + * drawn (as with the even-odd rule). + */ + +/* + * for the winding number rule + */ +#define CLOCKWISE 1 +#define COUNTERCLOCKWISE -1 + +typedef struct _EdgeTableEntry { + int ymax; /* ycoord at which we exit this edge. */ + BRESINFO bres; /* Bresenham info to run the edge */ + struct _EdgeTableEntry *next; /* next in the list */ + struct _EdgeTableEntry *back; /* for insertion sort */ + struct _EdgeTableEntry *nextWETE; /* for winding num rule */ + int ClockWise; /* flag for winding number rule */ +} EdgeTableEntry; + + +typedef struct _ScanLineList{ + int scanline; /* the scanline represented */ + EdgeTableEntry *edgelist; /* header node */ + struct _ScanLineList *next; /* next in the list */ +} ScanLineList; + + +typedef struct { + int ymax; /* ymax for the polygon */ + int ymin; /* ymin for the polygon */ + ScanLineList scanlines; /* header node */ +} EdgeTable; + + +/* + * Here is a struct to help with storage allocation + * so we can allocate a big chunk at a time, and then take + * pieces from this heap when we need to. + */ +#define SLLSPERBLOCK 25 + +typedef struct _ScanLineListBlock { + ScanLineList SLLs[SLLSPERBLOCK]; + struct _ScanLineListBlock *next; +} ScanLineListBlock; + + + +/* + * + * a few macros for the inner loops of the fill code where + * performance considerations don't allow a procedure call. + * + * Evaluate the given edge at the given scanline. + * If the edge has expired, then we leave it and fix up + * the active edge table; otherwise, we increment the + * x value to be ready for the next scanline. + * The winding number rule is in effect, so we must notify + * the caller when the edge has been removed so he + * can reorder the Winding Active Edge Table. + */ +#define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \ + if (pAET->ymax == y) { /* leaving this edge */ \ + pPrevAET->next = pAET->next; \ + pAET = pPrevAET->next; \ + fixWAET = 1; \ + if (pAET) \ + pAET->back = pPrevAET; \ + } \ + else { \ + BRESINCRPGONSTRUCT(pAET->bres) \ + pPrevAET = pAET; \ + pAET = pAET->next; \ + } \ +} + + +/* + * Evaluate the given edge at the given scanline. + * If the edge has expired, then we leave it and fix up + * the active edge table; otherwise, we increment the + * x value to be ready for the next scanline. + * The even-odd rule is in effect. + */ +#define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \ + if (pAET->ymax == y) { /* leaving this edge */ \ + pPrevAET->next = pAET->next; \ + pAET = pPrevAET->next; \ + if (pAET) \ + pAET->back = pPrevAET; \ + } \ + else { \ + BRESINCRPGONSTRUCT(pAET->bres) \ + pPrevAET = pAET; \ + pAET = pAET->next; \ + } \ +} +// END OF poly.h extract +// START OF PolyReg.c extract +/* $XConsortium: PolyReg.c,v 11.23 94/11/17 21:59:37 converse Exp $ */ +/************************************************************************ + +Copyright (c) 1987 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall not be +used in advertising or otherwise to promote the sale, use or other dealings +in this Software without prior written authorization from the X Consortium. + + +Copyright 1987 by Digital Etquipment Corporation, Maynard, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of Digital not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSETQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +************************************************************************/ +/* $XFree86: xc/lib/X11/PolyReg.c,v 1.1.1.2.8.2 1998/10/04 15:22:49 hohndel Exp $ */ + +#define LARGE_COORDINATE 1000000 +#define SMALL_COORDINATE -LARGE_COORDINATE + +/* + * InsertEdgeInET + * + * Insert the given edge into the edge table. + * First we must find the correct bucket in the + * Edge table, then find the right slot in the + * bucket. Finally, we can insert it. + * + */ +static void +InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE, int scanline, + ScanLineListBlock **SLLBlock, int *iSLLBlock) +{ + register EdgeTableEntry *start, *prev; + register ScanLineList *pSLL, *pPrevSLL; + ScanLineListBlock *tmpSLLBlock; + + /* + * find the right bucket to put the edge into + */ + pPrevSLL = &ET->scanlines; + pSLL = pPrevSLL->next; + while (pSLL && (pSLL->scanline < scanline)) + { + pPrevSLL = pSLL; + pSLL = pSLL->next; + } + + /* + * reassign pSLL (pointer to ScanLineList) if necessary + */ + if ((!pSLL) || (pSLL->scanline > scanline)) + { + if (*iSLLBlock > SLLSPERBLOCK-1) + { + tmpSLLBlock = + (ScanLineListBlock *)malloc(sizeof(ScanLineListBlock)); + (*SLLBlock)->next = tmpSLLBlock; + tmpSLLBlock->next = (ScanLineListBlock *)NULL; + *SLLBlock = tmpSLLBlock; + *iSLLBlock = 0; + } + pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]); + + pSLL->next = pPrevSLL->next; + pSLL->edgelist = (EdgeTableEntry *)NULL; + pPrevSLL->next = pSLL; + } + pSLL->scanline = scanline; + + /* + * now insert the edge in the right bucket + */ + prev = (EdgeTableEntry *)NULL; + start = pSLL->edgelist; + while (start && (start->bres.minor_axis < ETE->bres.minor_axis)) + { + prev = start; + start = start->next; + } + ETE->next = start; + + if (prev) + prev->next = ETE; + else + pSLL->edgelist = ETE; +} + +/* + * CreateEdgeTable + * + * This routine creates the edge table for + * scan converting polygons. + * The Edge Table (ET) looks like: + * + * EdgeTable + * -------- + * | ymax | ScanLineLists + * |scanline|-->------------>-------------->... + * -------- |scanline| |scanline| + * |edgelist| |edgelist| + * --------- --------- + * | | + * | | + * V V + * list of ETEs list of ETEs + * + * where ETE is an EdgeTableEntry data structure, + * and there is one ScanLineList per scanline at + * which an edge is initially entered. + * + */ + +static void +CreateETandAET(register int count, register TQPoint *pts, + EdgeTable *ET, EdgeTableEntry *AET, register EdgeTableEntry *pETEs, + ScanLineListBlock *pSLLBlock) +{ + register TQPoint *top, *bottom; + register TQPoint *PrevPt, *CurrPt; + int iSLLBlock = 0; + int dy; + + if (count < 2) return; + + /* + * initialize the Active Edge Table + */ + AET->next = (EdgeTableEntry *)NULL; + AET->back = (EdgeTableEntry *)NULL; + AET->nextWETE = (EdgeTableEntry *)NULL; + AET->bres.minor_axis = SMALL_COORDINATE; + + /* + * initialize the Edge Table. + */ + ET->scanlines.next = (ScanLineList *)NULL; + ET->ymax = SMALL_COORDINATE; + ET->ymin = LARGE_COORDINATE; + pSLLBlock->next = (ScanLineListBlock *)NULL; + + PrevPt = &pts[count-1]; + + /* + * for each vertex in the array of points. + * In this loop we are dealing with two vertices at + * a time -- these make up one edge of the polygon. + */ + while (count--) + { + CurrPt = pts++; + + /* + * find out which point is above and which is below. + */ + if (PrevPt->y() > CurrPt->y() ) + { + bottom = PrevPt, top = CurrPt; + pETEs->ClockWise = 0; + } + else + { + bottom = CurrPt, top = PrevPt; + pETEs->ClockWise = 1; + } + + /* + * don't add horizontal edges to the Edge table. + */ + if ( bottom->y() != top->y() ) + { + pETEs->ymax = bottom->y()-1; /* -1 so we don't get last scanline */ + + /* + * initialize integer edge algorithm + */ + dy = bottom->y() - top->y(); + BRESINITPGONSTRUCT(dy, top->x(), bottom->x(), pETEs->bres) + + InsertEdgeInET(ET, pETEs, top->y(), &pSLLBlock, &iSLLBlock); + + if (PrevPt->y() > ET->ymax) + ET->ymax = PrevPt->y(); + if (PrevPt->y() < ET->ymin) + ET->ymin = PrevPt->y(); + pETEs++; + } + + PrevPt = CurrPt; + } +} + +/* + * loadAET + * + * This routine moves EdgeTableEntries from the + * EdgeTable into the Active Edge Table, + * leaving them sorted by smaller x coordinate. + * + */ + +static void +loadAET(register EdgeTableEntry *AET, register EdgeTableEntry *ETEs) +{ + register EdgeTableEntry *pPrevAET; + register EdgeTableEntry *tmp; + + pPrevAET = AET; + AET = AET->next; + while (ETEs) + { + while (AET && (AET->bres.minor_axis < ETEs->bres.minor_axis)) + { + pPrevAET = AET; + AET = AET->next; + } + tmp = ETEs->next; + ETEs->next = AET; + if (AET) + AET->back = ETEs; + ETEs->back = pPrevAET; + pPrevAET->next = ETEs; + pPrevAET = ETEs; + + ETEs = tmp; + } +} + +/* + * computeWAET + * + * This routine links the AET by the + * nextWETE (winding EdgeTableEntry) link for + * use by the winding number rule. The final + * Active Edge Table (AET) might look something + * like: + * + * AET + * ---------- --------- --------- + * |ymax | |ymax | |ymax | + * | ... | |... | |... | + * |next |->|next |->|next |->... + * |nextWETE| |nextWETE| |nextWETE| + * --------- --------- ^-------- + * | | | + * V-------------------> V---> ... + * + */ +static void +computeWAET(register EdgeTableEntry *AET) +{ + register EdgeTableEntry *pWETE; + register int inside = 1; + register int isInside = 0; + + AET->nextWETE = (EdgeTableEntry *)NULL; + pWETE = AET; + AET = AET->next; + while (AET) + { + if (AET->ClockWise) + isInside++; + else + isInside--; + + if ((!inside && !isInside) || + ( inside && isInside)) + { + pWETE->nextWETE = AET; + pWETE = AET; + inside = !inside; + } + AET = AET->next; + } + pWETE->nextWETE = (EdgeTableEntry *)NULL; +} + +/* + * InsertionSort + * + * Just a simple insertion sort using + * pointers and back pointers to sort the Active + * Edge Table. + * + */ + +static int +InsertionSort(register EdgeTableEntry *AET) +{ + register EdgeTableEntry *pETEchase; + register EdgeTableEntry *pETEinsert; + register EdgeTableEntry *pETEchaseBackTMP; + register int changed = 0; + + AET = AET->next; + while (AET) + { + pETEinsert = AET; + pETEchase = AET; + while (pETEchase->back->bres.minor_axis > AET->bres.minor_axis) + pETEchase = pETEchase->back; + + AET = AET->next; + if (pETEchase != pETEinsert) + { + pETEchaseBackTMP = pETEchase->back; + pETEinsert->back->next = AET; + if (AET) + AET->back = pETEinsert->back; + pETEinsert->next = pETEchase; + pETEchase->back->next = pETEinsert; + pETEchase->back = pETEinsert; + pETEinsert->back = pETEchaseBackTMP; + changed = 1; + } + } + return(changed); +} + +/* + * Clean up our act. + */ +static void +FreeStorage(register ScanLineListBlock *pSLLBlock) +{ + register ScanLineListBlock *tmpSLLBlock; + + while (pSLLBlock) + { + tmpSLLBlock = pSLLBlock->next; + free((char *)pSLLBlock); + pSLLBlock = tmpSLLBlock; + } +} + +/* + * Create an array of rectangles from a list of points. + * If indeed these things (POINTS, RECTS) are the same, + * then this proc is still needed, because it allocates + * storage for the array, which was allocated on the + * stack by the calling procedure. + * + */ +static int PtsToRegion(register int numFullPtBlocks, register int iCurPtBlock, + POINTBLOCK *FirstPtBlock, TQRegionPrivate *reg) +{ + register TQRect *rects; + register TQPoint *pts; + register POINTBLOCK *CurPtBlock; + register int i; + register TQRect *extents; + register int numRects; + + extents = ®->extents; + + numRects = ((numFullPtBlocks * NUMPTSTOBUFFER) + iCurPtBlock) >> 1; + + reg->rects.resize(numRects); + + CurPtBlock = FirstPtBlock; + rects = reg->rects.data() - 1; + numRects = 0; + extents->setLeft( INT_MAX ); + extents->setRight( INT_MIN ); + + for ( ; numFullPtBlocks >= 0; numFullPtBlocks--) { + /* the loop uses 2 points per iteration */ + i = NUMPTSTOBUFFER >> 1; + if (!numFullPtBlocks) + i = iCurPtBlock >> 1; + for (pts = CurPtBlock->pts; i--; pts += 2) { + if ( pts->x() == pts[1].x() ) + continue; + if (numRects && pts->x() == rects->left() && pts->y() == rects->bottom() + 1 && + pts[1].x() == rects->right() && + (numRects == 1 || rects[-1].top() != rects->top()) && + (i && pts[2].y() > pts[1].y() )) { + rects->setBottom( pts[1].y() ); + continue; + } + numRects++; + rects++; + qt_setCoords( rects, pts->x(), pts->y(), pts[1].x() - 1, pts[1].y() ); + if (rects->left() < extents->left()) + extents->setLeft( rects->left() ); + if (rects->right() > extents->right()) + extents->setRight( rects->right() ); + } + CurPtBlock = CurPtBlock->next; + } + + if (numRects) { + extents->setTop( reg->rects[0].top() ); + extents->setBottom( rects->bottom() ); + } else { + qt_setCoords(extents, 0, 0, 0, 0); + } + reg->numRects = numRects; + + return(TRUE); +} + +/* + * polytoregion + * + * Scan converts a polygon by returning a run-length + * encoding of the resultant bitmap -- the run-length + * encoding is in the form of an array of rectangles. + */ +static TQRegionPrivate *PolygonRegion(TQPoint *Pts, int Count, int rule) + //Point *Pts; /* the pts */ + //int Count; /* number of pts */ + //int rule; /* winding rule */ +{ + TQRegionPrivate *region; + register EdgeTableEntry *pAET; /* Active Edge Table */ + register int y; /* current scanline */ + register int iPts = 0; /* number of pts in buffer */ + register EdgeTableEntry *pWETE; /* Winding Edge Table Entry*/ + register ScanLineList *pSLL; /* current scanLineList */ + register TQPoint *pts; /* output buffer */ + EdgeTableEntry *pPrevAET; /* ptr to previous AET */ + EdgeTable ET; /* header node for ET */ + EdgeTableEntry AET; /* header node for AET */ + EdgeTableEntry *pETEs; /* EdgeTableEntries pool */ + ScanLineListBlock SLLBlock; /* header for scanlinelist */ + int fixWAET = FALSE; + POINTBLOCK FirstPtBlock, *curPtBlock; /* PtBlock buffers */ + POINTBLOCK *tmpPtBlock; + int numFullPtBlocks = 0; + + if ( !(region = new TQRegionPrivate) ) + return 0; + + /* special case a rectangle */ + pts = Pts; + if (((Count == 4) || + ((Count == 5) && (pts[4].x() == pts[0].x() ) && (pts[4].y() == pts[0].y() ))) && + (((pts[0].y() == pts[1].y()) && + (pts[1].x() == pts[2].x()) && + (pts[2].y() == pts[3].y()) && + (pts[3].x() == pts[0].x())) || + ((pts[0].x() == pts[1].x()) && + (pts[1].y() == pts[2].y()) && + (pts[2].x() == pts[3].x()) && + (pts[3].y() == pts[0].y())))) { + region->extents.setLeft( TQMIN(pts[0].x(), pts[2].x()) ); + region->extents.setTop( TQMIN(pts[0].y(), pts[2].y()) ); + region->extents.setRight( TQMAX(pts[0].x(), pts[2].x()) ); + region->extents.setBottom( TQMAX(pts[0].y(), pts[2].y()) ); + if ((region->extents.left() <= region->extents.right()) && + (region->extents.top() <= region->extents.bottom())) { + region->numRects = 1; + region->rects.resize(1); + region->rects[0] = region->extents; + } + return region; + } + + if (! (pETEs = (EdgeTableEntry *) + malloc((unsigned) (sizeof(EdgeTableEntry) * Count)))) + return 0; + + pts = FirstPtBlock.pts; + CreateETandAET(Count, Pts, &ET, &AET, pETEs, &SLLBlock); + pSLL = ET.scanlines.next; + curPtBlock = &FirstPtBlock; + + if (rule == EvenOddRule) { + /* + * for each scanline + */ + for (y = ET.ymin; y < ET.ymax; y++) { + /* + * Add a new edge to the active edge table when we + * get to the next edge. + */ + if (pSLL != NULL && y == pSLL->scanline) { + loadAET(&AET, pSLL->edgelist); + pSLL = pSLL->next; + } + pPrevAET = &AET; + pAET = AET.next; + + /* + * for each active edge + */ + while (pAET) { + pts->setX( pAET->bres.minor_axis ), pts->setY( y ); + pts++, iPts++; + + /* + * send out the buffer + */ + if (iPts == NUMPTSTOBUFFER) { + tmpPtBlock = (POINTBLOCK *)malloc(sizeof(POINTBLOCK)); + curPtBlock->next = tmpPtBlock; + curPtBlock = tmpPtBlock; + pts = curPtBlock->pts; + numFullPtBlocks++; + iPts = 0; + } + EVALUATEEDGEEVENODD(pAET, pPrevAET, y) + } + (void) InsertionSort(&AET); + } + } + else { + /* + * for each scanline + */ + for (y = ET.ymin; y < ET.ymax; y++) { + /* + * Add a new edge to the active edge table when we + * get to the next edge. + */ + if (pSLL != NULL && y == pSLL->scanline) { + loadAET(&AET, pSLL->edgelist); + computeWAET(&AET); + pSLL = pSLL->next; + } + pPrevAET = &AET; + pAET = AET.next; + pWETE = pAET; + + /* + * for each active edge + */ + while (pAET) { + /* + * add to the buffer only those edges that + * are in the Winding active edge table. + */ + if (pWETE == pAET) { + pts->setX( pAET->bres.minor_axis), pts->setY( y ); + pts++, iPts++; + + /* + * send out the buffer + */ + if (iPts == NUMPTSTOBUFFER) { + tmpPtBlock = (POINTBLOCK *)malloc(sizeof(POINTBLOCK)); + curPtBlock->next = tmpPtBlock; + curPtBlock = tmpPtBlock; + pts = curPtBlock->pts; + numFullPtBlocks++; iPts = 0; + } + pWETE = pWETE->nextWETE; + } + EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) + } + + /* + * recompute the winding active edge table if + * we just resorted or have exited an edge. + */ + if (InsertionSort(&AET) || fixWAET) { + computeWAET(&AET); + fixWAET = FALSE; + } + } + } + FreeStorage(SLLBlock.next); + (void) PtsToRegion(numFullPtBlocks, iPts, &FirstPtBlock, region); + for (curPtBlock = FirstPtBlock.next; --numFullPtBlocks >= 0;) { + tmpPtBlock = curPtBlock->next; + free((char *)curPtBlock); + curPtBlock = tmpPtBlock; + } + free((char *)pETEs); + return region; +} +// END OF PolyReg.c extract + +TQRegionPrivate *qt_bitmapToRegion(const TQBitmap& bitmap) +{ + TQImage image = bitmap.convertToImage(); + + TQRegionPrivate *region = new TQRegionPrivate; + TQRect xr; + +#define AddSpan \ + { \ + qt_setCoords( &xr, prev1, y, x-1, y ); \ + UnionRectWithRegion( &xr, region, region ); \ + } + + const int zero=0; + bool little = image.bitOrder() == TQImage::LittleEndian; + + int x, y; + for (y=0; yw-8 || byte!=all ) { + if ( little ) { + for ( int b=8; b>0 && x>= 1; + x++; + } + } else { + for ( int b=8; b>0 && xdata; + data->ref(); +} + +/*! \internal + Internal constructor that creates a null region. +*/ + +TQRegion::TQRegion( bool is_null ) +{ + data = new TQRegionData; + Q_CHECK_PTR( data ); + data->region = new TQRegionPrivate; + data->is_null = is_null; + data->rgn = 0; + data->xrectangles = 0; +} + +/*! + \overload + + Create a region based on the rectange \a r with region type \a t. + + If the rectangle is invalid a null region will be created. + + \sa TQRegion::RegionType +*/ + +TQRegion::TQRegion( const TQRect &r, RegionType t ) +{ + if ( r.isEmpty() ) { + if ( !empty_region ) { // avoid too many allocs + qAddPostRoutine( cleanup_empty_region ); + empty_region = new TQRegion( TRUE ); + Q_CHECK_PTR( empty_region ); + } + data = empty_region->data; + data->ref(); + } else { + data = new TQRegionData; + Q_CHECK_PTR( data ); + data->is_null = FALSE; + data->rgn = 0; + data->xrectangles = 0; + if ( t == Rectangle ) { // rectangular region + data->region = new TQRegionPrivate( r ); + } else if ( t == Ellipse ) { // elliptic region + TQPointArray a; + a.makeEllipse( r.x(), r.y(), r.width(), r.height() ); + data->region = PolygonRegion( (TQPoint*)a.data(), a.size(), + EvenOddRule ); + } + } +} + + +/*! + Constructs a polygon region from the point array \a a. + + If \a winding is TRUE, the polygon region is filled using the + winding algorithm, otherwise the default even-odd fill algorithm + is used. + + This constructor may create complex regions that will slow down + painting when used. +*/ + +TQRegion::TQRegion( const TQPointArray &a, bool winding ) +{ + if (a.size() > 2) { + data = new TQRegionData; + Q_CHECK_PTR( data ); + data->is_null = FALSE; + data->rgn = 0; + data->xrectangles = 0; + data->region = PolygonRegion( (TQPoint*)a.data(), a.size(), + winding ? WindingRule : EvenOddRule ); + } else { + if ( !empty_region ) { + qAddPostRoutine( cleanup_empty_region ); + empty_region = new TQRegion( TRUE ); + Q_CHECK_PTR( empty_region ); + } + data = empty_region->data; + data->ref(); + } +} + + +/*! + Constructs a new region which is equal to region \a r. +*/ + +TQRegion::TQRegion( const TQRegion &r ) +{ + data = r.data; + data->ref(); +} + + +/*! + Constructs a region from the bitmap \a bm. + + The resulting region consists of the pixels in bitmap \a bm that + are \c color1, as if each pixel was a 1 by 1 rectangle. + + This constructor may create complex regions that will slow down + painting when used. Note that drawing masked pixmaps can be done + much faster using TQPixmap::setMask(). +*/ +TQRegion::TQRegion( const TQBitmap & bm ) +{ + if ( bm.isNull() ) { + if ( !empty_region ) { // avoid too many allocs + qAddPostRoutine( cleanup_empty_region ); + empty_region = new TQRegion( TRUE ); + Q_CHECK_PTR( empty_region ); + } + data = empty_region->data; + data->ref(); + } else { + data = new TQRegionData; + Q_CHECK_PTR( data ); + data->is_null = FALSE; + data->rgn = 0; + data->xrectangles = 0; + data->region = qt_bitmapToRegion(bm); + } +} + +/*! + Destroys the region. +*/ + +TQRegion::~TQRegion() +{ + if ( data->deref() ) { + delete data->region; + if ( data->rgn ) + XDestroyRegion( data->rgn ); + if ( data->xrectangles ) + free( data->xrectangles ); + delete data; + } +} + + +/*! + Assigns \a r to this region and returns a reference to the region. +*/ + +TQRegion &TQRegion::operator=( const TQRegion &r ) +{ + r.data->ref(); // beware of r = r + if ( data->deref() ) { + delete data->region; + if ( data->rgn ) + XDestroyRegion( data->rgn ); + if ( data->xrectangles ) + free( data->xrectangles ); + delete data; + } + data = r.data; + return *this; +} + + +/*! + Returns a \link shclass.html deep copy\endlink of the region. + + \sa detach() +*/ + +TQRegion TQRegion::copy() const +{ + TQRegion r( data->is_null ); + *r.data->region = *data->region; + return r; +} + +/*! + Returns TRUE if the region is a null region; otherwise returns + FALSE. + + A null region is a region that has not been initialized. A null + region is always empty. + + \sa isEmpty() +*/ + +bool TQRegion::isNull() const +{ + return data->is_null; +} + + +/*! + Returns TRUE if the region is empty; otherwise returns FALSE. An + empty region is a region that contains no points. + + Example: + \code + TQRegion r1( 10, 10, 20, 20 ); + TQRegion r2( 40, 40, 20, 20 ); + TQRegion r3; + r1.isNull(); // FALSE + r1.isEmpty(); // FALSE + r3.isNull(); // TRUE + r3.isEmpty(); // TRUE + r3 = r1.intersect( r2 ); // r3 = intersection of r1 and r2 + r3.isNull(); // FALSE + r3.isEmpty(); // TRUE + r3 = r1.unite( r2 ); // r3 = union of r1 and r2 + r3.isNull(); // FALSE + r3.isEmpty(); // FALSE + \endcode + + \sa isNull() +*/ + +bool TQRegion::isEmpty() const +{ + return data->is_null || ( data->region->numRects == 0 ); +} + + +/*! + Returns TRUE if the region contains the point \a p; otherwise + returns FALSE. +*/ + +bool TQRegion::contains( const TQPoint &p ) const +{ + return PointInRegion( data->region, p.x(), p.y() ); +} + +/*! + \overload + + Returns TRUE if the region overlaps the rectangle \a r; otherwise + returns FALSE. +*/ + +bool TQRegion::contains( const TQRect &r ) const +{ + return RectInRegion( data->region, r.left(), r.top(), + r.width(), r.height() ) != RectangleOut; +} + + +/*! + Translates (moves) the region \a dx along the X axis and \a dy + along the Y axis. +*/ + +void TQRegion::translate( int dx, int dy ) +{ + if ( empty_region && data == empty_region->data ) + return; + detach(); + OffsetRegion( data->region, dx, dy ); + if ( data->xrectangles ) { + free( data->xrectangles ); + data->xrectangles = 0; + } +} + + +/*! + Returns a region which is the union of this region and \a r. + + \img runion.png Region Union + + The figure shows the union of two elliptical regions. +*/ + +TQRegion TQRegion::unite( const TQRegion &r ) const +{ + TQRegion result( FALSE ); + UnionRegion( data->region, r.data->region, result.data->region ); + return result; +} + +/*! + Returns a region which is the intersection of this region and \a r. + + \img rintersect.png Region Intersection + + The figure shows the intersection of two elliptical regions. +*/ + +TQRegion TQRegion::intersect( const TQRegion &r ) const +{ + TQRegion result( FALSE ); + IntersectRegion( data->region, r.data->region, result.data->region ); + return result; +} + +/*! + Returns a region which is \a r subtracted from this region. + + \img rsubtract.png Region Subtraction + + The figure shows the result when the ellipse on the right is + subtracted from the ellipse on the left. (\c left-right ) +*/ + +TQRegion TQRegion::subtract( const TQRegion &r ) const +{ + TQRegion result( FALSE ); + SubtractRegion( data->region, r.data->region, result.data->region ); + return result; +} + +/*! + Returns a region which is the exclusive or (XOR) of this region + and \a r. + + \img rxor.png Region XORed + + The figure shows the exclusive or of two elliptical regions. +*/ + +TQRegion TQRegion::eor( const TQRegion &r ) const +{ + TQRegion result( FALSE ); + XorRegion( data->region, r.data->region, result.data->region ); + return result; +} + +/*! + Returns the bounding rectangle of this region. An empty region + gives a rectangle that is TQRect::isNull(). +*/ + +TQRect TQRegion::boundingRect() const +{ + return data->region->extents; +} + + +/*! + Returns an array of non-overlapping rectangles that make up the + region. + + The union of all the rectangles is equal to the original region. +*/ + +TQMemArray TQRegion::rects() const +{ + TQMemArray rects; + rects.duplicate( data->region->rects, data->region->numRects ); + return rects; +} + +/*! + Sets the region to be the given set of rectangles. The rectangles + \e must be optimal Y-X sorted bands as follows: +
    +
  • The rectangles must not intersect +
  • All rectangles with a given top coordinate must have the same height. +
  • No two rectangles may abut horizontally (they should be combined + into a single wider rectangle in that case). +
  • The rectangles must be sorted ascendingly by Y as the major sort key + and X as the minor sort key. +
+ \internal + Only some platforms have that restriction (TQWS and X11). +*/ +void TQRegion::setRects( const TQRect *rects, int num ) +{ + *this = TQRegion( FALSE ); + if ( !rects || (num == 1 && rects->isEmpty()) ) + num = 0; + + data->region->rects.duplicate( rects, num ); + data->region->numRects = num; + if ( num == 0 ) { + data->region->extents = TQRect(); + } else { + int left = INT_MAX, right = INT_MIN, top = INT_MAX, bottom = INT_MIN; + int i; + for ( i = 0; i < num; i++ ) { + left = TQMIN( rects[i].left(), left ); + right = TQMAX( rects[i].right(), right ); + top = TQMIN( rects[i].top(), top ); + bottom = TQMAX( rects[i].bottom(), bottom ); + } + data->region->extents = TQRect( TQPoint(left, top), TQPoint(right, bottom) ); + } +} + +/*! + Returns TRUE if the region is equal to \a r; otherwise returns + FALSE. +*/ + +bool TQRegion::operator==( const TQRegion &r ) const +{ + return data == r.data ? + TRUE : EqualRegion( data->region, r.data->region ); +} + +/*! + \fn bool TQRegion::operator!=( const TQRegion &r ) const + + Returns TRUE if the region is different from \a r; otherwise + returns FALSE. +*/ + +/* + This is how X represents regions internally. +*/ + +struct BOX { + short x1, x2, y1, y2; +}; + +struct _XRegion { + long size; + long numRects; + BOX *rects; + BOX extents; +}; + + +void TQRegion::updateX11Region() const +{ + data->rgn = XCreateRegion(); + + for( int i = 0; i < data->region->numRects; i++ ) { + XRectangle r; + const TQRect &rect = data->region->rects[i]; + r.x = TQMAX( SHRT_MIN, rect.x() ); + r.y = TQMAX( SHRT_MIN, rect.y() ); + r.width = TQMIN( USHRT_MAX, rect.width() ); + r.height = TQMIN( USHRT_MAX, rect.height() ); + XUnionRectWithRegion( &r, data->rgn, data->rgn ); + } +} + + +void *TQRegion::clipRectangles( int &num ) const +{ + if ( !data->xrectangles ) { + XRectangle *r = (XRectangle *) malloc( data->region->numRects * sizeof( XRectangle ) ); + data->xrectangles = r; + for( int i = 0; i < data->region->numRects; i++ ) { + const TQRect &rect = data->region->rects[i]; + r->x = TQMAX( SHRT_MIN, rect.x() ); + r->y = TQMAX( SHRT_MIN, rect.y() ); + r->width = TQMIN( USHRT_MAX, rect.width() ); + r->height = TQMIN( USHRT_MAX, rect.height() ); + r++; + } + } + num = data->region->numRects; + return data->xrectangles; +} diff --git a/src/kernel/qrichtext.cpp b/src/kernel/qrichtext.cpp new file mode 100644 index 000000000..82ce63ea0 --- /dev/null +++ b/src/kernel/qrichtext.cpp @@ -0,0 +1,8258 @@ +/**************************************************************************** +** +** Implementation of the internal TQt classes dealing with rich text +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qrichtext_p.h" + +#ifndef QT_NO_RICHTEXT + + +#include "qstringlist.h" +#include "qfont.h" +#include "qtextstream.h" +#include "qfile.h" +#include "qapplication.h" +#include "qmap.h" +#include "qfileinfo.h" +#include "qstylesheet.h" +#include "qmime.h" +#include "qimage.h" +#include "qdragobject.h" +#include "qpaintdevicemetrics.h" +#include "qpainter.h" +#include "qdrawutil.h" +#include "qcursor.h" +#include "qptrstack.h" +#include "qptrdict.h" +#include "qstyle.h" +#include "qcleanuphandler.h" +#include "qtextengine_p.h" +#include + +#include + +static TQTextCursor* richTextExportStart = 0; +static TQTextCursor* richTextExportEnd = 0; + +class TQTextFormatCollection; + +const int border_tolerance = 2; + +#ifdef Q_WS_WIN +#include "qt_windows.h" +#endif + +#define TQChar_linesep TQChar(0x2028U) + +static inline bool is_printer( TQPainter *p ) +{ + if ( !p || !p->device() ) + return FALSE; + return p->device()->devType() == TQInternal::Printer; +} + +static inline int scale( int value, TQPainter *painter ) +{ + if ( is_printer( painter ) ) { + TQPaintDeviceMetrics metrics( painter->device() ); +#if defined(Q_WS_X11) + value = value * metrics.logicalDpiY() / + TQPaintDevice::x11AppDpiY( painter->device()->x11Screen() ); +#elif defined (Q_WS_WIN) + HDC hdc = GetDC( 0 ); + int gdc = GetDeviceCaps( hdc, LOGPIXELSY ); + if ( gdc ) + value = value * metrics.logicalDpiY() / gdc; + ReleaseDC( 0, hdc ); +#elif defined (Q_WS_MAC) + value = value * metrics.logicalDpiY() / 75; // ##### FIXME +#elif defined (Q_WS_QWS) + value = value * metrics.logicalDpiY() / 75; +#endif + } + return value; +} + + +inline bool isBreakable( TQTextString *string, int pos ) +{ + if (string->at(pos).nobreak) + return FALSE; + return (pos < string->length()-1 && string->at(pos+1).softBreak); +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +void TQTextCommandHistory::addCommand( TQTextCommand *cmd ) +{ + if ( current < (int)history.count() - 1 ) { + TQPtrList commands; + commands.setAutoDelete( FALSE ); + + for( int i = 0; i <= current; ++i ) { + commands.insert( i, history.at( 0 ) ); + history.take( 0 ); + } + + commands.append( cmd ); + history.clear(); + history = commands; + history.setAutoDelete( TRUE ); + } else { + history.append( cmd ); + } + + if ( (int)history.count() > steps ) + history.removeFirst(); + else + ++current; +} + +TQTextCursor *TQTextCommandHistory::undo( TQTextCursor *c ) +{ + if ( current > -1 ) { + TQTextCursor *c2 = history.at( current )->unexecute( c ); + --current; + return c2; + } + return 0; +} + +TQTextCursor *TQTextCommandHistory::redo( TQTextCursor *c ) +{ + if ( current > -1 ) { + if ( current < (int)history.count() - 1 ) { + ++current; + return history.at( current )->execute( c ); + } + } else { + if ( history.count() > 0 ) { + ++current; + return history.at( current )->execute( c ); + } + } + return 0; +} + +bool TQTextCommandHistory::isUndoAvailable() +{ + return current > -1; +} + +bool TQTextCommandHistory::isRedoAvailable() +{ + return current > -1 && current < (int)history.count() - 1 || current == -1 && history.count() > 0; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextDeleteCommand::TQTextDeleteCommand( TQTextDocument *d, int i, int idx, const TQMemArray &str, + const TQByteArray& oldStyleInfo ) + : TQTextCommand( d ), id( i ), index( idx ), parag( 0 ), text( str ), styleInformation( oldStyleInfo ) +{ + for ( int j = 0; j < (int)text.size(); ++j ) { + if ( text[ j ].format() ) + text[ j ].format()->addRef(); + } +} + +TQTextDeleteCommand::TQTextDeleteCommand( TQTextParagraph *p, int idx, const TQMemArray &str ) + : TQTextCommand( 0 ), id( -1 ), index( idx ), parag( p ), text( str ) +{ + for ( int i = 0; i < (int)text.size(); ++i ) { + if ( text[ i ].format() ) + text[ i ].format()->addRef(); + } +} + +TQTextDeleteCommand::~TQTextDeleteCommand() +{ + for ( int i = 0; i < (int)text.size(); ++i ) { + if ( text[ i ].format() ) + text[ i ].format()->removeRef(); + } + text.resize( 0 ); +} + +TQTextCursor *TQTextDeleteCommand::execute( TQTextCursor *c ) +{ + TQTextParagraph *s = doc ? doc->paragAt( id ) : parag; + if ( !s ) { + qWarning( "can't locate parag at %d, last parag: %d", id, doc->lastParagraph()->paragId() ); + return 0; + } + + cursor.setParagraph( s ); + cursor.setIndex( index ); + int len = text.size(); + if ( c ) + *c = cursor; + if ( doc ) { + doc->setSelectionStart( TQTextDocument::Temp, cursor ); + for ( int i = 0; i < len; ++i ) + cursor.gotoNextLetter(); + doc->setSelectionEnd( TQTextDocument::Temp, cursor ); + doc->removeSelectedText( TQTextDocument::Temp, &cursor ); + if ( c ) + *c = cursor; + } else { + s->remove( index, len ); + } + + return c; +} + +TQTextCursor *TQTextDeleteCommand::unexecute( TQTextCursor *c ) +{ + TQTextParagraph *s = doc ? doc->paragAt( id ) : parag; + if ( !s ) { + qWarning( "can't locate parag at %d, last parag: %d", id, doc->lastParagraph()->paragId() ); + return 0; + } + + cursor.setParagraph( s ); + cursor.setIndex( index ); + TQString str = TQTextString::toString( text ); + cursor.insert( str, TRUE, &text ); + if ( c ) + *c = cursor; + cursor.setParagraph( s ); + cursor.setIndex( index ); + +#ifndef QT_NO_DATASTREAM + if ( !styleInformation.isEmpty() ) { + TQDataStream styleStream( styleInformation, IO_ReadOnly ); + int num; + styleStream >> num; + TQTextParagraph *p = s; + while ( num-- && p ) { + p->readStyleInformation( styleStream ); + p = p->next(); + } + } +#endif + s = cursor.paragraph(); + while ( s ) { + s->format(); + s->setChanged( TRUE ); + if ( s == c->paragraph() ) + break; + s = s->next(); + } + + return &cursor; +} + +TQTextFormatCommand::TQTextFormatCommand( TQTextDocument *d, int sid, int sidx, int eid, int eidx, + const TQMemArray &old, TQTextFormat *f, int fl ) + : TQTextCommand( d ), startId( sid ), startIndex( sidx ), endId( eid ), endIndex( eidx ), format( f ), oldFormats( old ), flags( fl ) +{ + format = d->formatCollection()->format( f ); + for ( int j = 0; j < (int)oldFormats.size(); ++j ) { + if ( oldFormats[ j ].format() ) + oldFormats[ j ].format()->addRef(); + } +} + +TQTextFormatCommand::~TQTextFormatCommand() +{ + format->removeRef(); + for ( int j = 0; j < (int)oldFormats.size(); ++j ) { + if ( oldFormats[ j ].format() ) + oldFormats[ j ].format()->removeRef(); + } +} + +TQTextCursor *TQTextFormatCommand::execute( TQTextCursor *c ) +{ + TQTextParagraph *sp = doc->paragAt( startId ); + TQTextParagraph *ep = doc->paragAt( endId ); + if ( !sp || !ep ) + return c; + + TQTextCursor start( doc ); + start.setParagraph( sp ); + start.setIndex( startIndex ); + TQTextCursor end( doc ); + end.setParagraph( ep ); + end.setIndex( endIndex ); + + doc->setSelectionStart( TQTextDocument::Temp, start ); + doc->setSelectionEnd( TQTextDocument::Temp, end ); + doc->setFormat( TQTextDocument::Temp, format, flags ); + doc->removeSelection( TQTextDocument::Temp ); + if ( endIndex == ep->length() ) + end.gotoLeft(); + *c = end; + return c; +} + +TQTextCursor *TQTextFormatCommand::unexecute( TQTextCursor *c ) +{ + TQTextParagraph *sp = doc->paragAt( startId ); + TQTextParagraph *ep = doc->paragAt( endId ); + if ( !sp || !ep ) + return 0; + + int idx = startIndex; + int fIndex = 0; + while ( fIndex < int(oldFormats.size()) ) { + if ( oldFormats.at( fIndex ).c == '\n' ) { + if ( idx > 0 ) { + if ( idx < sp->length() && fIndex > 0 ) + sp->setFormat( idx, 1, oldFormats.at( fIndex - 1 ).format() ); + if ( sp == ep ) + break; + sp = sp->next(); + idx = 0; + } + fIndex++; + } + if ( oldFormats.at( fIndex ).format() ) + sp->setFormat( idx, 1, oldFormats.at( fIndex ).format() ); + idx++; + fIndex++; + if ( fIndex >= (int)oldFormats.size() ) + break; + if ( idx >= sp->length() ) { + if ( sp == ep ) + break; + sp = sp->next(); + idx = 0; + } + } + + TQTextCursor end( doc ); + end.setParagraph( ep ); + end.setIndex( endIndex ); + if ( endIndex == ep->length() ) + end.gotoLeft(); + *c = end; + return c; +} + +TQTextStyleCommand::TQTextStyleCommand( TQTextDocument *d, int fParag, int lParag, const TQByteArray& beforeChange ) + : TQTextCommand( d ), firstParag( fParag ), lastParag( lParag ), before( beforeChange ) +{ + after = readStyleInformation( d, fParag, lParag ); +} + + +TQByteArray TQTextStyleCommand::readStyleInformation( TQTextDocument* doc, int fParag, int lParag ) +{ + TQByteArray style; +#ifndef QT_NO_DATASTREAM + TQTextParagraph *p = doc->paragAt( fParag ); + if ( !p ) + return style; + TQDataStream styleStream( style, IO_WriteOnly ); + int num = lParag - fParag + 1; + styleStream << num; + while ( num -- && p ) { + p->writeStyleInformation( styleStream ); + p = p->next(); + } +#endif + return style; +} + +void TQTextStyleCommand::writeStyleInformation( TQTextDocument* doc, int fParag, const TQByteArray& style ) +{ +#ifndef QT_NO_DATASTREAM + TQTextParagraph *p = doc->paragAt( fParag ); + if ( !p ) + return; + TQDataStream styleStream( style, IO_ReadOnly ); + int num; + styleStream >> num; + while ( num-- && p ) { + p->readStyleInformation( styleStream ); + p = p->next(); + } +#endif +} + +TQTextCursor *TQTextStyleCommand::execute( TQTextCursor *c ) +{ + writeStyleInformation( doc, firstParag, after ); + return c; +} + +TQTextCursor *TQTextStyleCommand::unexecute( TQTextCursor *c ) +{ + writeStyleInformation( doc, firstParag, before ); + return c; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextCursor::TQTextCursor( TQTextDocument *d ) + : idx( 0 ), tmpX( -1 ), ox( 0 ), oy( 0 ), + valid( TRUE ) +{ + para = d ? d->firstParagraph() : 0; +} + +TQTextCursor::TQTextCursor( const TQTextCursor &c ) +{ + ox = c.ox; + oy = c.oy; + idx = c.idx; + para = c.para; + tmpX = c.tmpX; + indices = c.indices; + paras = c.paras; + xOffsets = c.xOffsets; + yOffsets = c.yOffsets; + valid = c.valid; +} + +TQTextCursor &TQTextCursor::operator=( const TQTextCursor &c ) +{ + ox = c.ox; + oy = c.oy; + idx = c.idx; + para = c.para; + tmpX = c.tmpX; + indices = c.indices; + paras = c.paras; + xOffsets = c.xOffsets; + yOffsets = c.yOffsets; + valid = c.valid; + + return *this; +} + +bool TQTextCursor::operator==( const TQTextCursor &c ) const +{ + return para == c.para && idx == c.idx; +} + +int TQTextCursor::totalOffsetX() const +{ + int xoff = ox; + for ( TQValueStack::ConstIterator xit = xOffsets.begin(); xit != xOffsets.end(); ++xit ) + xoff += *xit; + return xoff; +} + +int TQTextCursor::totalOffsetY() const +{ + int yoff = oy; + for ( TQValueStack::ConstIterator yit = yOffsets.begin(); yit != yOffsets.end(); ++yit ) + yoff += *yit; + return yoff; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +void TQTextCursor::gotoIntoNested( const TQPoint &globalPos ) +{ + if ( !para ) + return; + Q_ASSERT( para->at( idx )->isCustom() ); + push(); + ox = 0; + int bl, y; + para->lineHeightOfChar( idx, &bl, &y ); + oy = y + para->rect().y(); + ox = para->at( idx )->x; + TQTextDocument* doc = document(); + para->at( idx )->customItem()->enterAt( this, doc, para, idx, ox, oy, globalPos-TQPoint(ox,oy) ); +} +#endif + +void TQTextCursor::invalidateNested() +{ + if ( nestedDepth() ) { + TQValueStack::Iterator it = paras.begin(); + TQValueStack::Iterator it2 = indices.begin(); + for ( ; it != paras.end(); ++it, ++it2 ) { + if ( *it == para ) + continue; + (*it)->invalidate( 0 ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( (*it)->at( *it2 )->isCustom() ) + (*it)->at( *it2 )->customItem()->invalidate(); +#endif + } + } +} + +void TQTextCursor::insert( const TQString &str, bool checkNewLine, TQMemArray *formatting ) +{ + tmpX = -1; + bool justInsert = TRUE; + TQString s( str ); +#if defined(Q_WS_WIN) + if ( checkNewLine ) { + int i = 0; + while ( ( i = s.find( '\r', i ) ) != -1 ) + s.remove( i ,1 ); + } +#endif + if ( checkNewLine ) + justInsert = s.find( '\n' ) == -1; + if ( justInsert ) { // we ignore new lines and insert all in the current para at the current index + para->insert( idx, s.unicode(), s.length() ); + if ( formatting ) { + for ( int i = 0; i < (int)s.length(); ++i ) { + if ( formatting->at( i ).format() ) { + formatting->at( i ).format()->addRef(); + para->string()->setFormat( idx + i, formatting->at( i ).format(), TRUE ); + } + } + } + idx += s.length(); + } else { // we split at new lines + int start = -1; + int end; + int y = para->rect().y() + para->rect().height(); + int lastIndex = 0; + do { + end = s.find( '\n', start + 1 ); // find line break + if ( end == -1 ) // didn't find one, so end of line is end of string + end = s.length(); + int len = (start == -1 ? end : end - start - 1); + if ( len > 0 ) // insert the line + para->insert( idx, s.unicode() + start + 1, len ); + else + para->invalidate( 0 ); + if ( formatting ) { // set formats to the chars of the line + for ( int i = 0; i < len; ++i ) { + if ( formatting->at( i + lastIndex ).format() ) { + formatting->at( i + lastIndex ).format()->addRef(); + para->string()->setFormat( i + idx, formatting->at( i + lastIndex ).format(), TRUE ); + } + } + lastIndex += len; + } + start = end; // next start is at the end of this line + idx += len; // increase the index of the cursor to the end of the inserted text + if ( s[end] == '\n' ) { // if at the end was a line break, break the line + splitAndInsertEmptyParagraph( FALSE, TRUE ); + para->setEndState( -1 ); + para->prev()->format( -1, FALSE ); + lastIndex++; + } + + } while ( end < (int)s.length() ); + + para->format( -1, FALSE ); + int dy = para->rect().y() + para->rect().height() - y; + TQTextParagraph *p = para; + p->setParagId( p->prev() ? p->prev()->paragId() + 1 : 0 ); + p = p->next(); + while ( p ) { + p->setParagId( p->prev()->paragId() + 1 ); + p->move( dy ); + p->invalidate( 0 ); + p->setEndState( -1 ); + p = p->next(); + } + } + + int h = para->rect().height(); + para->format( -1, TRUE ); + if ( h != para->rect().height() ) + invalidateNested(); + else if ( para->document() && para->document()->parent() ) + para->document()->nextDoubleBuffered = TRUE; + + fixCursorPosition(); +} + +void TQTextCursor::gotoLeft() +{ + if ( para->string()->isRightToLeft() ) + gotoNextLetter(); + else + gotoPreviousLetter(); +} + +void TQTextCursor::gotoPreviousLetter() +{ + tmpX = -1; + + if ( idx > 0 ) { + idx = para->string()->previousCursorPosition( idx ); +#ifndef QT_NO_TEXTCUSTOMITEM + const TQTextStringChar *tsc = para->at( idx ); + if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) + processNesting( EnterEnd ); +#endif + } else if ( para->prev() ) { + para = para->prev(); + while ( !para->isVisible() && para->prev() ) + para = para->prev(); + idx = para->length() - 1; + } else if ( nestedDepth() ) { + pop(); + processNesting( Prev ); + if ( idx == -1 ) { + pop(); + if ( idx > 0 ) { + idx = para->string()->previousCursorPosition( idx ); +#ifndef QT_NO_TEXTCUSTOMITEM + const TQTextStringChar *tsc = para->at( idx ); + if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) + processNesting( EnterEnd ); +#endif + } else if ( para->prev() ) { + para = para->prev(); + idx = para->length() - 1; + } + } + } +} + +void TQTextCursor::push() +{ + indices.push( idx ); + paras.push( para ); + xOffsets.push( ox ); + yOffsets.push( oy ); +} + +void TQTextCursor::pop() +{ + if ( indices.isEmpty() ) + return; + idx = indices.pop(); + para = paras.pop(); + ox = xOffsets.pop(); + oy = yOffsets.pop(); +} + +void TQTextCursor::restoreState() +{ + while ( !indices.isEmpty() ) + pop(); +} + +bool TQTextCursor::place( const TQPoint &p, TQTextParagraph *s, bool link, bool loosePlacing, bool matchBetweenCharacters ) +{ + TQPoint pos( p ); + TQRect r; + TQTextParagraph *str = s; + if ( pos.y() < s->rect().y() ) { + pos.setY( s->rect().y() ); +#ifdef Q_WS_MACX + pos.setX( s->rect().x() ); +#endif + } + while ( s ) { + r = s->rect(); + r.setWidth( document() ? document()->width() : TQWIDGETSIZE_MAX ); + if ( s->isVisible() ) + str = s; + if ( pos.y() >= r.y() && pos.y() <= r.y() + r.height() ) + break; + if ( loosePlacing == TRUE && !s->next() ) { +#ifdef Q_WS_MACX + pos.setX( s->rect().x() + s->rect().width() ); +#endif + break; + } + s = s->next(); + } + + if ( !s || !str ) + return FALSE; + + s = str; + + setParagraph( s ); + int y = s->rect().y(); + int lines = s->lines(); + TQTextStringChar *chr = 0; + int index = 0; + int i = 0; + int cy = 0; + int ch = 0; + for ( ; i < lines; ++i ) { + chr = s->lineStartOfLine( i, &index ); + cy = s->lineY( i ); + ch = s->lineHeight( i ); + if ( !chr ) + return FALSE; + if ( pos.y() <= y + cy + ch ) + break; + } + int nextLine; + if ( i < lines - 1 ) + s->lineStartOfLine( i+1, &nextLine ); + else + nextLine = s->length(); + i = index; + int x = s->rect().x(); + if ( pos.x() < x ) + pos.setX( x + 1 ); + int cw; + int curpos = -1; + int dist = 10000000; + bool inCustom = FALSE; + while ( i < nextLine ) { + chr = s->at(i); + int cpos = x + chr->x; + cw = s->string()->width( i ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( chr->isCustom() && chr->customItem()->isNested() ) { + if ( pos.x() >= cpos && pos.x() <= cpos + cw && + pos.y() >= y + cy && pos.y() <= y + cy + chr->height() ) { + inCustom = TRUE; + curpos = i; + break; + } + } else +#endif + { + if( chr->rightToLeft ) + cpos += cw; + int d = cpos - pos.x(); + bool dm = d < 0 ? !chr->rightToLeft : chr->rightToLeft; + if ( ( matchBetweenCharacters == TRUE && (TQABS( d ) < dist || (dist == d && dm == TRUE )) && para->string()->validCursorPosition( i ) ) || + ( matchBetweenCharacters == FALSE && ( d == 0 || dm == TRUE ) ) ) { + dist = TQABS( d ); + if ( !link || ( pos.x() >= x + chr->x && ( loosePlacing == TRUE || pos.x() < cpos ) ) ) + curpos = i; + } + } + i++; + } + if ( curpos == -1 ) { + if ( loosePlacing == TRUE ) + curpos = s->length()-1; + else + return FALSE; + } + setIndex( curpos ); + +#ifndef QT_NO_TEXTCUSTOMITEM + if ( inCustom && para->document() && para->at( curpos )->isCustom() && para->at( curpos )->customItem()->isNested() ) { + TQTextDocument *oldDoc = para->document(); + gotoIntoNested( pos ); + if ( oldDoc == para->document() ) + return TRUE; + TQPoint p( pos.x() - offsetX(), pos.y() - offsetY() ); + if ( !place( p, document()->firstParagraph(), link ) ) + pop(); + } +#endif + return TRUE; +} + +bool TQTextCursor::processNesting( Operation op ) +{ + if ( !para->document() ) + return FALSE; + TQTextDocument* doc = para->document(); + push(); + ox = para->at( idx )->x; + int bl, y; + para->lineHeightOfChar( idx, &bl, &y ); + oy = y + para->rect().y(); + bool ok = FALSE; + +#ifndef QT_NO_TEXTCUSTOMITEM + switch ( op ) { + case EnterBegin: + ok = para->at( idx )->customItem()->enter( this, doc, para, idx, ox, oy ); + break; + case EnterEnd: + ok = para->at( idx )->customItem()->enter( this, doc, para, idx, ox, oy, TRUE ); + break; + case Next: + ok = para->at( idx )->customItem()->next( this, doc, para, idx, ox, oy ); + break; + case Prev: + ok = para->at( idx )->customItem()->prev( this, doc, para, idx, ox, oy ); + break; + case Down: + ok = para->at( idx )->customItem()->down( this, doc, para, idx, ox, oy ); + break; + case Up: + ok = para->at( idx )->customItem()->up( this, doc, para, idx, ox, oy ); + break; + } + if ( !ok ) +#endif + pop(); + return ok; +} + +void TQTextCursor::gotoRight() +{ + if ( para->string()->isRightToLeft() ) + gotoPreviousLetter(); + else + gotoNextLetter(); +} + +void TQTextCursor::gotoNextLetter() +{ + tmpX = -1; + +#ifndef QT_NO_TEXTCUSTOMITEM + const TQTextStringChar *tsc = para->at( idx ); + if ( tsc && tsc->isCustom() && tsc->customItem()->isNested() ) { + if ( processNesting( EnterBegin ) ) + return; + } +#endif + + if ( idx < para->length() - 1 ) { + idx = para->string()->nextCursorPosition( idx ); + } else if ( para->next() ) { + para = para->next(); + while ( !para->isVisible() && para->next() ) + para = para->next(); + idx = 0; + } else if ( nestedDepth() ) { + pop(); + processNesting( Next ); + if ( idx == -1 ) { + pop(); + if ( idx < para->length() - 1 ) { + idx = para->string()->nextCursorPosition( idx ); + } else if ( para->next() ) { + para = para->next(); + idx = 0; + } + } + } +} + +void TQTextCursor::gotoUp() +{ + int indexOfLineStart; + int line; + TQTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); + if ( !c ) + return; + + if (tmpX < 0) + tmpX = x(); + + if ( indexOfLineStart == 0 ) { + if ( !para->prev() ) { + if ( !nestedDepth() ) + return; + pop(); + processNesting( Up ); + if ( idx == -1 ) { + pop(); + if ( !para->prev() ) + return; + idx = tmpX = 0; + } else { + tmpX = -1; + return; + } + } + TQTextParagraph *p = para->prev(); + while ( p && !p->isVisible() ) + p = p->prev(); + if ( p ) + para = p; + int lastLine = para->lines() - 1; + if ( !para->lineStartOfLine( lastLine, &indexOfLineStart ) ) + return; + idx = indexOfLineStart; + while (idx < para->length()-1 && para->at(idx)->x < tmpX) + ++idx; + if (idx > indexOfLineStart && + para->at(idx)->x - tmpX > tmpX - para->at(idx-1)->x) + --idx; + } else { + --line; + int oldIndexOfLineStart = indexOfLineStart; + if ( !para->lineStartOfLine( line, &indexOfLineStart ) ) + return; + idx = indexOfLineStart; + while (idx < oldIndexOfLineStart-1 && para->at(idx)->x < tmpX) + ++idx; + if (idx > indexOfLineStart && + para->at(idx)->x - tmpX > tmpX - para->at(idx-1)->x) + --idx; + } + fixCursorPosition(); +} + +void TQTextCursor::gotoDown() +{ + int indexOfLineStart; + int line; + TQTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); + if ( !c ) + return; + + if (tmpX < 0) + tmpX = x(); + if ( line == para->lines() - 1 ) { + if ( !para->next() ) { + if ( !nestedDepth() ) + return; + pop(); + processNesting( Down ); + if ( idx == -1 ) { + pop(); + if ( !para->next() ) + return; + idx = tmpX = 0; + } else { + tmpX = -1; + return; + } + } + TQTextParagraph *s = para->next(); + while ( s && !s->isVisible() ) + s = s->next(); + if ( s ) + para = s; + if ( !para->lineStartOfLine( 0, &indexOfLineStart ) ) + return; + int end; + if ( para->lines() == 1 ) + end = para->length(); + else + para->lineStartOfLine( 1, &end ); + + idx = indexOfLineStart; + while (idx < end-1 && para->at(idx)->x < tmpX) + ++idx; + if (idx > indexOfLineStart && + para->at(idx)->x - tmpX > tmpX - para->at(idx-1)->x) + --idx; + } else { + ++line; + int end; + if ( line == para->lines() - 1 ) + end = para->length(); + else + para->lineStartOfLine( line + 1, &end ); + if ( !para->lineStartOfLine( line, &indexOfLineStart ) ) + return; + idx = indexOfLineStart; + while (idx < end-1 && para->at(idx)->x < tmpX) + ++idx; + if (idx > indexOfLineStart && + para->at(idx)->x - tmpX > tmpX - para->at(idx-1)->x) + --idx; + } + fixCursorPosition(); +} + +void TQTextCursor::gotoLineEnd() +{ + tmpX = -1; + int indexOfLineStart; + int line; + TQTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); + if ( !c ) + return; + + if ( line == para->lines() - 1 ) { + idx = para->length() - 1; + } else { + c = para->lineStartOfLine( ++line, &indexOfLineStart ); + indexOfLineStart--; + idx = indexOfLineStart; + } +} + +void TQTextCursor::gotoLineStart() +{ + tmpX = -1; + int indexOfLineStart; + int line; + TQTextStringChar *c = para->lineStartOfChar( idx, &indexOfLineStart, &line ); + if ( !c ) + return; + + idx = indexOfLineStart; +} + +void TQTextCursor::gotoHome() +{ + if ( topParagraph()->document() ) + gotoPosition( topParagraph()->document()->firstParagraph() ); + else + gotoLineStart(); +} + +void TQTextCursor::gotoEnd() +{ + if ( topParagraph()->document() && topParagraph()->document()->lastParagraph()->isValid() ) + gotoPosition( topParagraph()->document()->lastParagraph(), + topParagraph()->document()->lastParagraph()->length() - 1); + else + gotoLineEnd(); +} + +void TQTextCursor::gotoPageUp( int visibleHeight ) +{ + int targetY = globalY() - visibleHeight; + TQTextParagraph* old; int index; + do { + old = para; index = idx; + gotoUp(); + } while ( (old != para || index != idx) && globalY() > targetY ); +} + +void TQTextCursor::gotoPageDown( int visibleHeight ) +{ + int targetY = globalY() + visibleHeight; + TQTextParagraph* old; int index; + do { + old = para; index = idx; + gotoDown(); + } while ( (old != para || index != idx) && globalY() < targetY ); +} + +void TQTextCursor::gotoWordRight() +{ + if ( para->string()->isRightToLeft() ) + gotoPreviousWord(); + else + gotoNextWord(); +} + +void TQTextCursor::gotoWordLeft() +{ + if ( para->string()->isRightToLeft() ) + gotoNextWord(); + else + gotoPreviousWord(); +} + +static bool is_seperator( const TQChar &c, bool onlySpace ) +{ + if ( onlySpace ) + return c.isSpace(); + return c.isSpace() || + c == '\t' || + c == '.' || + c == ',' || + c == ':' || + c == ';' || + c == '-' || + c == '<' || + c == '>' || + c == '[' || + c == ']' || + c == '(' || + c == ')' || + c == '{' || + c == '}'; +} + +void TQTextCursor::gotoPreviousWord( bool onlySpace ) +{ + gotoPreviousLetter(); + tmpX = -1; + TQTextString *s = para->string(); + bool allowSame = FALSE; + if ( idx == ((int)s->length()-1) ) + return; + for ( int i = idx; i >= 0; --i ) { + if ( is_seperator( s->at( i ).c, onlySpace ) ) { + if ( !allowSame ) + continue; + idx = i + 1; + return; + } + if ( !allowSame && !is_seperator( s->at( i ).c, onlySpace ) ) + allowSame = TRUE; + } + idx = 0; +} + +void TQTextCursor::gotoNextWord( bool onlySpace ) +{ + tmpX = -1; + TQTextString *s = para->string(); + bool allowSame = FALSE; + for ( int i = idx; i < (int)s->length(); ++i ) { + if ( !is_seperator( s->at( i ).c, onlySpace ) ) { + if ( !allowSame ) + continue; + idx = i; + return; + } + if ( !allowSame && is_seperator( s->at( i ).c, onlySpace ) ) + allowSame = TRUE; + + } + + if ( idx < ((int)s->length()-1) ) { + gotoLineEnd(); + } else if ( para->next() ) { + TQTextParagraph *p = para->next(); + while ( p && !p->isVisible() ) + p = p->next(); + if ( s ) { + para = p; + idx = 0; + } + } else { + gotoLineEnd(); + } +} + +bool TQTextCursor::atParagStart() +{ + return idx == 0; +} + +bool TQTextCursor::atParagEnd() +{ + return idx == para->length() - 1; +} + +void TQTextCursor::splitAndInsertEmptyParagraph( bool ind, bool updateIds ) +{ + if ( !para->document() ) + return; + tmpX = -1; + TQTextFormat *f = 0; + if ( para->document()->useFormatCollection() ) { + f = para->at( idx )->format(); + if ( idx == para->length() - 1 && idx > 0 ) + f = para->at( idx - 1 )->format(); + if ( f->isMisspelled() ) { + f->removeRef(); + f = para->document()->formatCollection()->format( f->font(), f->color() ); + } + } + + if ( atParagEnd() ) { + TQTextParagraph *n = para->next(); + TQTextParagraph *s = para->document()->createParagraph( para->document(), para, n, updateIds ); + if ( f ) + s->setFormat( 0, 1, f, TRUE ); + s->copyParagData( para ); + if ( ind ) { + int oi, ni; + s->indent( &oi, &ni ); + para = s; + idx = ni; + } else { + para = s; + idx = 0; + } + } else if ( atParagStart() ) { + TQTextParagraph *p = para->prev(); + TQTextParagraph *s = para->document()->createParagraph( para->document(), p, para, updateIds ); + if ( f ) + s->setFormat( 0, 1, f, TRUE ); + s->copyParagData( para ); + if ( ind ) { + s->indent(); + s->format(); + indent(); + para->format(); + } + } else { + TQString str = para->string()->toString().mid( idx, 0xFFFFFF ); + TQTextParagraph *n = para->next(); + TQTextParagraph *s = para->document()->createParagraph( para->document(), para, n, updateIds ); + s->copyParagData( para ); + s->remove( 0, 1 ); + s->append( str, TRUE ); + for ( uint i = 0; i < str.length(); ++i ) { + TQTextStringChar* tsc = para->at( idx + i ); + s->setFormat( i, 1, tsc->format(), TRUE ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( tsc->isCustom() ) { + TQTextCustomItem * item = tsc->customItem(); + s->at( i )->setCustomItem( item ); + tsc->loseCustomItem(); + } +#endif + if ( tsc->isAnchor() ) + s->at( i )->setAnchor( tsc->anchorName(), + tsc->anchorHref() ); + } + para->truncate( idx ); + if ( ind ) { + int oi, ni; + s->indent( &oi, &ni ); + para = s; + idx = ni; + } else { + para = s; + idx = 0; + } + } + + invalidateNested(); +} + +bool TQTextCursor::remove() +{ + tmpX = -1; + if ( !atParagEnd() ) { + int next = para->string()->nextCursorPosition( idx ); + para->remove( idx, next-idx ); + int h = para->rect().height(); + para->format( -1, TRUE ); + if ( h != para->rect().height() ) + invalidateNested(); + else if ( para->document() && para->document()->parent() ) + para->document()->nextDoubleBuffered = TRUE; + return FALSE; + } else if ( para->next() ) { + para->join( para->next() ); + invalidateNested(); + return TRUE; + } + return FALSE; +} + +/* needed to implement backspace the correct way */ +bool TQTextCursor::removePreviousChar() +{ + tmpX = -1; + if ( !atParagStart() ) { + para->remove( idx-1, 1 ); + int h = para->rect().height(); + idx--; + // shouldn't be needed, just to make sure. + fixCursorPosition(); + para->format( -1, TRUE ); + if ( h != para->rect().height() ) + invalidateNested(); + else if ( para->document() && para->document()->parent() ) + para->document()->nextDoubleBuffered = TRUE; + return FALSE; + } else if ( para->prev() ) { + para = para->prev(); + para->join( para->next() ); + invalidateNested(); + return TRUE; + } + return FALSE; +} + +void TQTextCursor::indent() +{ + int oi = 0, ni = 0; + para->indent( &oi, &ni ); + if ( oi == ni ) + return; + + if ( idx >= oi ) + idx += ni - oi; + else + idx = ni; +} + +void TQTextCursor::fixCursorPosition() +{ + // searches for the closest valid cursor position + if ( para->string()->validCursorPosition( idx ) ) + return; + + int lineIdx; + TQTextStringChar *start = para->lineStartOfChar( idx, &lineIdx, 0 ); + int x = para->string()->at( idx ).x; + int diff = TQABS(start->x - x); + int best = lineIdx; + + TQTextStringChar *c = start; + ++c; + + TQTextStringChar *end = ¶->string()->at( para->length()-1 ); + while ( c <= end && !c->lineStart ) { + int xp = c->x; + if ( c->rightToLeft ) + xp += para->string()->width( lineIdx + (c-start) ); + int ndiff = TQABS(xp - x); + if ( ndiff < diff && para->string()->validCursorPosition(lineIdx + (c-start)) ) { + diff = ndiff; + best = lineIdx + (c-start); + } + ++c; + } + idx = best; +} + + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextDocument::TQTextDocument( TQTextDocument *p ) + : par( p ), parentPar( 0 ) +#ifndef QT_NO_TEXTCUSTOMITEM + , tc( 0 ) +#endif + , tArray( 0 ), tStopWidth( 0 ) +{ + fCollection = par ? par->fCollection : new TQTextFormatCollection; + init(); +} + +void TQTextDocument::init() +{ + oTextValid = TRUE; + mightHaveCustomItems = FALSE; + if ( par ) + par->insertChild( this ); + pProcessor = 0; + useFC = TRUE; + pFormatter = 0; + indenter = 0; + fParag = 0; + txtFormat = TQt::AutoText; + preferRichText = FALSE; + pages = FALSE; + focusIndicator.parag = 0; + minw = 0; + wused = 0; + minwParag = curParag = 0; + align = AlignAuto; + nSelections = 1; + + setStyleSheet( TQStyleSheet::defaultSheet() ); +#ifndef QT_NO_MIME + factory_ = TQMimeSourceFactory::defaultFactory(); +#endif + contxt = TQString::null; + + underlLinks = par ? par->underlLinks : TRUE; + backBrush = 0; + buf_pixmap = 0; + nextDoubleBuffered = FALSE; + + if ( par ) + withoutDoubleBuffer = par->withoutDoubleBuffer; + else + withoutDoubleBuffer = FALSE; + + lParag = fParag = createParagraph( this, 0, 0 ); + + cx = 0; + cy = 2; + if ( par ) + cx = cy = 0; + cw = 600; + vw = 0; + flow_ = new TQTextFlow; + flow_->setWidth( cw ); + + leftmargin = rightmargin = 4; + scaleFontsFactor = 1; + + + selectionColors[ Standard ] = TQApplication::palette().color( TQPalette::Active, TQColorGroup::Highlight ); + selectionText[ Standard ] = TRUE; + selectionText[ IMSelectionText ] = TRUE; + selectionText[ IMCompositionText ] = FALSE; + commandHistory = new TQTextCommandHistory( 100 ); + tStopWidth = formatCollection()->defaultFormat()->width( 'x' ) * 8; +} + +TQTextDocument::~TQTextDocument() +{ + delete commandHistory; + if ( par ) + par->removeChild( this ); + clear(); + delete flow_; + if ( !par ) { + delete pFormatter; + delete fCollection; + } + delete pProcessor; + delete buf_pixmap; + delete indenter; + delete backBrush; + delete [] tArray; +} + +void TQTextDocument::clear( bool createEmptyParag ) +{ + while ( fParag ) { + TQTextParagraph *p = fParag->next(); + delete fParag; + fParag = p; + } + if ( flow_ ) + flow_->clear(); + fParag = lParag = 0; + if ( createEmptyParag ) + fParag = lParag = createParagraph( this ); + focusIndicator.parag = 0; + selections.clear(); + oText = TQString::null; + oTextValid = FALSE; +} + +int TQTextDocument::widthUsed() const +{ + return wused + 2*border_tolerance; +} + +int TQTextDocument::height() const +{ + int h = 0; + if ( lParag ) + h = lParag->rect().top() + lParag->rect().height() + 1; + int fh = flow_->boundingRect().bottom(); + return TQMAX( h, fh ); +} + + + +TQTextParagraph *TQTextDocument::createParagraph( TQTextDocument *d, TQTextParagraph *pr, TQTextParagraph *nx, bool updateIds ) +{ + return new TQTextParagraph( d, pr, nx, updateIds ); +} + +bool TQTextDocument::setMinimumWidth( int needed, int used, TQTextParagraph *p ) +{ + if ( needed == -1 ) { + minw = 0; + wused = 0; + p = 0; + } + if ( p == minwParag ) { + if (minw > needed) { + TQTextParagraph *tp = fParag; + while (tp) { + if (tp != p && tp->minwidth > needed) { + needed = tp->minwidth; + minwParag = tp; + } + tp = tp->n; + } + } + minw = needed; + emit minimumWidthChanged( minw ); + } else if ( needed > minw ) { + minw = needed; + minwParag = p; + emit minimumWidthChanged( minw ); + } + wused = TQMAX( wused, used ); + wused = TQMAX( wused, minw ); + cw = TQMAX( minw, cw ); + return TRUE; +} + +void TQTextDocument::setPlainText( const TQString &text ) +{ + preferRichText = FALSE; + clear(); + oTextValid = TRUE; + oText = text; + + int lastNl = 0; + int nl = text.find( '\n' ); + if ( nl == -1 ) { + lParag = createParagraph( this, lParag, 0 ); + if ( !fParag ) + fParag = lParag; + TQString s = text; + if ( !s.isEmpty() ) { + if ( s[ (int)s.length() - 1 ] == '\r' ) + s.remove( s.length() - 1, 1 ); + lParag->append( s ); + } + } else { + for (;;) { + lParag = createParagraph( this, lParag, 0 ); + if ( !fParag ) + fParag = lParag; + int l = nl - lastNl; + if ( l > 0 ) { + if (text.unicode()[nl-1] == '\r') + l--; + TQConstString cs(text.unicode()+lastNl, l); + lParag->append( cs.string() ); + } + if ( nl == (int)text.length() ) + break; + lastNl = nl + 1; + nl = text.find( '\n', nl + 1 ); + if ( nl == -1 ) + nl = text.length(); + } + } + if ( !lParag ) + lParag = fParag = createParagraph( this, 0, 0 ); +} + +struct Q_EXPORT TQTextDocumentTag { + TQTextDocumentTag(){} + TQTextDocumentTag( const TQString&n, const TQStyleSheetItem* s, const TQTextFormat& f ) + :name(n),style(s), format(f), alignment(TQt::AlignAuto), direction(TQChar::DirON),liststyle(TQStyleSheetItem::ListDisc) { + wsm = TQStyleSheetItem::WhiteSpaceNormal; + } + TQString name; + const TQStyleSheetItem* style; + TQString anchorHref; + TQStyleSheetItem::WhiteSpaceMode wsm; + TQTextFormat format; + int alignment : 16; + int direction : 5; + TQStyleSheetItem::ListStyle liststyle; + + TQTextDocumentTag( const TQTextDocumentTag& t ) { + name = t.name; + style = t.style; + anchorHref = t.anchorHref; + wsm = t.wsm; + format = t.format; + alignment = t.alignment; + direction = t.direction; + liststyle = t.liststyle; + } + TQTextDocumentTag& operator=(const TQTextDocumentTag& t) { + name = t.name; + style = t.style; + anchorHref = t.anchorHref; + wsm = t.wsm; + format = t.format; + alignment = t.alignment; + direction = t.direction; + liststyle = t.liststyle; + return *this; + } + + Q_DUMMY_COMPARISON_OPERATOR(TQTextDocumentTag) +}; + + +#define NEWPAR do{ if ( !hasNewPar) { \ + if ( !textEditMode && curpar && curpar->length()>1 && curpar->at( curpar->length()-2)->c == TQChar_linesep ) \ + curpar->remove( curpar->length()-2, 1 ); \ + curpar = createParagraph( this, curpar, curpar->next() ); styles.append( vec ); vec = 0;} \ + hasNewPar = TRUE; \ + curpar->rtext = TRUE; \ + curpar->align = curtag.alignment; \ + curpar->lstyle = curtag.liststyle; \ + curpar->litem = ( curtag.style->displayMode() == TQStyleSheetItem::DisplayListItem ); \ + curpar->str->setDirection( (TQChar::Direction)curtag.direction ); \ + space = TRUE; \ + tabExpansionColumn = 0; \ + delete vec; vec = new TQPtrVector( (uint)tags.count() + 1); \ + int i = 0; \ + for ( TQValueStack::Iterator it = tags.begin(); it != tags.end(); ++it ) \ + vec->insert( i++, (*it).style ); \ + vec->insert( i, curtag.style ); \ + }while(FALSE); + + +void TQTextDocument::setRichText( const TQString &text, const TQString &context, const TQTextFormat *initialFormat ) +{ + preferRichText = TRUE; + if ( !context.isEmpty() ) + setContext( context ); + clear(); + fParag = lParag = createParagraph( this ); + oTextValid = TRUE; + oText = text; + setRichTextInternal( text, 0, initialFormat ); + fParag->rtext = TRUE; +} + +void TQTextDocument::setRichTextInternal( const TQString &text, TQTextCursor* cursor, const TQTextFormat *initialFormat ) +{ + TQTextParagraph* curpar = lParag; + int pos = 0; + TQValueStack tags; + if ( !initialFormat ) + initialFormat = formatCollection()->defaultFormat(); + TQTextDocumentTag initag( "", sheet_->item(""), *initialFormat ); + if ( bodyText.isValid() ) + initag.format.setColor( bodyText ); + TQTextDocumentTag curtag = initag; + bool space = TRUE; + bool canMergeLi = FALSE; + + bool textEditMode = FALSE; + int tabExpansionColumn = 0; + + const TQChar* doc = text.unicode(); + int length = text.length(); + bool hasNewPar = curpar->length() <= 1; + TQString anchorName; + + // style sheet handling for margin and line spacing calculation below + TQTextParagraph* stylesPar = curpar; + TQPtrVector* vec = 0; + TQPtrList< TQPtrVector > styles; + styles.setAutoDelete( TRUE ); + + if ( cursor ) { + cursor->splitAndInsertEmptyParagraph(); + TQTextCursor tmp = *cursor; + tmp.gotoPreviousLetter(); + stylesPar = curpar = tmp.paragraph(); + hasNewPar = TRUE; + textEditMode = TRUE; + } else { + NEWPAR; + } + + // set rtext spacing to FALSE for the initial paragraph. + curpar->rtext = FALSE; + + TQString wellKnownTags = "br hr wsp table qt body meta title"; + + while ( pos < length ) { + if ( hasPrefix(doc, length, pos, '<' ) ){ + if ( !hasPrefix( doc, length, pos+1, TQChar('/') ) ) { + // open tag + TQMap attr; + bool emptyTag = FALSE; + TQString tagname = parseOpenTag(doc, length, pos, attr, emptyTag); + if ( tagname.isEmpty() ) + continue; // nothing we could do with this, probably parse error + + const TQStyleSheetItem* nstyle = sheet_->item(tagname); + + if ( nstyle ) { + // we might have to close some 'forgotten' tags + while ( !nstyle->allowedInContext( curtag.style ) ) { + TQString msg; + msg.sprintf( "TQText Warning: Document not valid ( '%s' not allowed in '%s' #%d)", + tagname.ascii(), curtag.style->name().ascii(), pos); + sheet_->error( msg ); + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + + /* special handling for p and li for HTML + compatibility. We do not want to embed blocks in + p, and we do not want new blocks inside non-empty + lis. Plus we want to merge empty lis sometimes. */ + if( nstyle->displayMode() == TQStyleSheetItem::DisplayListItem ) { + canMergeLi = TRUE; + } else if ( nstyle->displayMode() == TQStyleSheetItem::DisplayBlock ) { + while ( curtag.style->name() == "p" ) { + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + + if ( curtag.style->displayMode() == TQStyleSheetItem::DisplayListItem ) { + // we are in a li and a new block comes along + if ( nstyle->name() == "ul" || nstyle->name() == "ol" ) + hasNewPar = FALSE; // we want an empty li (like most browsers) + if ( !hasNewPar ) { + /* do not add new blocks inside + non-empty lis */ + while ( curtag.style->displayMode() == TQStyleSheetItem::DisplayListItem ) { + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + } else if ( canMergeLi ) { + /* we have an empty li and a block + comes along, merge them */ + nstyle = curtag.style; + } + canMergeLi = FALSE; + } + } + } + +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextCustomItem* custom = 0; +#else + bool custom = FALSE; +#endif + + // some well-known tags, some have a nstyle, some not + if ( wellKnownTags.find( tagname ) != -1 ) { + if ( tagname == "br" ) { + emptyTag = space = TRUE; + int index = TQMAX( curpar->length(),1) - 1; + TQTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + curpar->append( TQChar_linesep ); + curpar->setFormat( index, 1, &format ); + hasNewPar = false; + } else if ( tagname == "hr" ) { + emptyTag = space = TRUE; +#ifndef QT_NO_TEXTCUSTOMITEM + custom = sheet_->tag( tagname, attr, contxt, *factory_ , emptyTag, this ); +#endif + } else if ( tagname == "table" ) { + emptyTag = space = TRUE; +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + curpar->setAlignment( curtag.alignment ); + custom = parseTable( attr, format, doc, length, pos, curpar ); +#endif + } else if ( tagname == "qt" || tagname == "body" ) { + if ( attr.contains( "bgcolor" ) ) { + TQBrush *b = new TQBrush( TQColor( attr["bgcolor"] ) ); + setPaper( b ); + } + if ( attr.contains( "background" ) ) { +#ifndef QT_NO_MIME + TQImage img; + TQString bg = attr["background"]; + const TQMimeSource* m = factory_->data( bg, contxt ); + if ( !m ) { + qWarning("TQRichText: no mimesource for %s", bg.latin1() ); + } else { + if ( !TQImageDrag::decode( m, img ) ) { + qWarning("TQTextImage: cannot decode %s", bg.latin1() ); + } + } + if ( !img.isNull() ) { + TQBrush *b = new TQBrush( TQColor(), TQPixmap( img ) ); + setPaper( b ); + } +#endif + } + if ( attr.contains( "text" ) ) { + TQColor c( attr["text"] ); + initag.format.setColor( c ); + curtag.format.setColor( c ); + bodyText = c; + } + if ( attr.contains( "link" ) ) + linkColor = TQColor( attr["link"] ); + if ( attr.contains( "title" ) ) + attribs.replace( "title", attr["title"] ); + + if ( textEditMode ) { + if ( attr.contains("style" ) ) { + TQString a = attr["style"]; + for ( int s = 0; s < a.contains(';')+1; s++ ) { + TQString style = a.section( ';', s, s ); + if ( style.startsWith("font-size:" ) && style.endsWith("pt") ) { + scaleFontsFactor = double( formatCollection()->defaultFormat()->fn.pointSize() ) / + style.mid( 10, style.length() - 12 ).toInt(); + } + } + } + nstyle = 0; // ignore body in textEditMode + } + // end qt- and body-tag handling + } else if ( tagname == "meta" ) { + if ( attr["name"] == "qrichtext" && attr["content"] == "1" ) + textEditMode = TRUE; + } else if ( tagname == "title" ) { + TQString title; + while ( pos < length ) { + if ( hasPrefix( doc, length, pos, TQChar('<') ) && hasPrefix( doc, length, pos+1, TQChar('/') ) && + parseCloseTag( doc, length, pos ) == "title" ) + break; + title += doc[ pos ]; + ++pos; + } + attribs.replace( "title", title ); + } + } // end of well-known tag handling + +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !custom ) // try generic custom item + custom = sheet_->tag( tagname, attr, contxt, *factory_ , emptyTag, this ); +#endif + if ( !nstyle && !custom ) // we have no clue what this tag could be, ignore it + continue; + + if ( custom ) { +#ifndef QT_NO_TEXTCUSTOMITEM + int index = TQMAX( curpar->length(),1) - 1; + TQTextFormat format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + curpar->append( TQChar('*') ); + TQTextFormat* f = formatCollection()->format( &format ); + curpar->setFormat( index, 1, f ); + curpar->at( index )->setCustomItem( custom ); + if ( !curtag.anchorHref.isEmpty() ) + curpar->at(index)->setAnchor( TQString::null, curtag.anchorHref ); + if ( !anchorName.isEmpty() ) { + curpar->at(index)->setAnchor( anchorName, curpar->at(index)->anchorHref() ); + anchorName = TQString::null; + } + registerCustomItem( custom, curpar ); + hasNewPar = FALSE; +#endif + } else if ( !emptyTag ) { + /* if we do nesting, push curtag on the stack, + otherwise reinint curag. */ + if ( curtag.style->name() != tagname || nstyle->selfNesting() ) { + tags.push( curtag ); + } else { + if ( !tags.isEmpty() ) + curtag = tags.top(); + else + curtag = initag; + } + + curtag.name = tagname; + curtag.style = nstyle; + curtag.name = tagname; + curtag.style = nstyle; + if ( nstyle->whiteSpaceMode() != TQStyleSheetItem::WhiteSpaceModeUndefined ) + curtag.wsm = nstyle->whiteSpaceMode(); + + /* netscape compatibility: eat a newline and only a newline if a pre block starts */ + if ( curtag.wsm == TQStyleSheetItem::WhiteSpacePre && + nstyle->displayMode() == TQStyleSheetItem::DisplayBlock ) + eat( doc, length, pos, '\n' ); + + /* ignore whitespace for inline elements if there + was already one*/ + if ( !textEditMode && + (curtag.wsm == TQStyleSheetItem::WhiteSpaceNormal + || curtag.wsm == TQStyleSheetItem::WhiteSpaceNoWrap) + && ( space || nstyle->displayMode() != TQStyleSheetItem::DisplayInline ) ) + eatSpace( doc, length, pos ); + + curtag.format = curtag.format.makeTextFormat( nstyle, attr, scaleFontsFactor ); + if ( nstyle->isAnchor() ) { + if ( !anchorName.isEmpty() ) + anchorName += "#" + attr["name"]; + else + anchorName = attr["name"]; + curtag.anchorHref = attr["href"]; + } + + if ( nstyle->alignment() != TQStyleSheetItem::Undefined ) + curtag.alignment = nstyle->alignment(); + + if ( nstyle->listStyle() != TQStyleSheetItem::ListStyleUndefined ) + curtag.liststyle = nstyle->listStyle(); + + if ( nstyle->displayMode() == TQStyleSheetItem::DisplayBlock + || nstyle->displayMode() == TQStyleSheetItem::DisplayListItem ) { + + if ( nstyle->name() == "ol" || nstyle->name() == "ul" || nstyle->name() == "li") { + TQString type = attr["type"]; + if ( !type.isEmpty() ) { + if ( type == "1" ) { + curtag.liststyle = TQStyleSheetItem::ListDecimal; + } else if ( type == "a" ) { + curtag.liststyle = TQStyleSheetItem::ListLowerAlpha; + } else if ( type == "A" ) { + curtag.liststyle = TQStyleSheetItem::ListUpperAlpha; + } else { + type = type.lower(); + if ( type == "square" ) + curtag.liststyle = TQStyleSheetItem::ListSquare; + else if ( type == "disc" ) + curtag.liststyle = TQStyleSheetItem::ListDisc; + else if ( type == "circle" ) + curtag.liststyle = TQStyleSheetItem::ListCircle; + } + } + } + + + /* Internally we treat ordered and bullet + lists the same for margin calculations. In + order to have fast pointer compares in the + xMargin() functions we restrict ourselves to +
    . Once we calculate the margins in the + parser rathern than later, the unelegance of + this approach goes awy + */ + if ( nstyle->name() == "ul" ) + curtag.style = sheet_->item( "ol" ); + + if ( attr.contains( "align" ) ) { + TQString align = attr["align"].lower(); + if ( align == "center" ) + curtag.alignment = TQt::AlignCenter; + else if ( align == "right" ) + curtag.alignment = TQt::AlignRight; + else if ( align == "justify" ) + curtag.alignment = TQt::AlignJustify; + } + if ( attr.contains( "dir" ) ) { + TQString dir = attr["dir"]; + if ( dir == "rtl" ) + curtag.direction = TQChar::DirR; + else if ( dir == "ltr" ) + curtag.direction = TQChar::DirL; + } + + NEWPAR; + + if ( curtag.style->displayMode() == TQStyleSheetItem::DisplayListItem ) { + if ( attr.contains( "value " ) ) + curpar->setListValue( attr["value"].toInt() ); + } + + if ( attr.contains( "style" ) ) { + TQString a = attr["style"]; + bool ok = TRUE; + for ( int s = 0; ok && s < a.contains(';')+1; s++ ) { + TQString style = a.section( ';', s, s ); + if ( style.startsWith("margin-top:" ) && style.endsWith("px") ) + curpar->utm = 1+style.mid(11, style.length() - 13).toInt(&ok); + else if ( style.startsWith("margin-bottom:" ) && style.endsWith("px") ) + curpar->ubm = 1+style.mid(14, style.length() - 16).toInt(&ok); + else if ( style.startsWith("margin-left:" ) && style.endsWith("px") ) + curpar->ulm = 1+style.mid(12, style.length() - 14).toInt(&ok); + else if ( style.startsWith("margin-right:" ) && style.endsWith("px") ) + curpar->urm = 1+style.mid(13, style.length() - 15).toInt(&ok); + else if ( style.startsWith("text-indent:" ) && style.endsWith("px") ) + curpar->uflm = 1+style.mid(12, style.length() - 14).toInt(&ok); + } + if ( !ok ) // be pressmistic + curpar->utm = curpar->ubm = curpar->urm = curpar->ulm = 0; + } + } + } + } else { + TQString tagname = parseCloseTag( doc, length, pos ); + if ( tagname.isEmpty() ) + continue; // nothing we could do with this, probably parse error + if ( !sheet_->item( tagname ) ) // ignore unknown tags + continue; + if ( tagname == "li" ) + continue; + + // we close a block item. Since the text may continue, we need to have a new paragraph + bool needNewPar = curtag.style->displayMode() == TQStyleSheetItem::DisplayBlock + || curtag.style->displayMode() == TQStyleSheetItem::DisplayListItem; + + + // html slopiness: handle unbalanched tag closing + while ( curtag.name != tagname ) { + TQString msg; + msg.sprintf( "TQText Warning: Document not valid ( '%s' not closed before '%s' #%d)", + curtag.name.ascii(), tagname.ascii(), pos); + sheet_->error( msg ); + if ( tags.isEmpty() ) + break; + curtag = tags.pop(); + } + + + // close the tag + if ( !tags.isEmpty() ) + curtag = tags.pop(); + else + curtag = initag; + + if ( needNewPar ) { + if ( textEditMode && (tagname == "p" || tagname == "div" ) ) // preserve empty paragraphs + hasNewPar = FALSE; + NEWPAR; + } + } + } else { + // normal contents + TQString s; + TQChar c; + while ( pos < length && !hasPrefix(doc, length, pos, TQChar('<') ) ){ + if ( textEditMode ) { + // text edit mode: we handle all white space but ignore newlines + c = parseChar( doc, length, pos, TQStyleSheetItem::WhiteSpacePre ); + if ( c == TQChar_linesep ) + break; + } else { + int l = pos; + c = parseChar( doc, length, pos, curtag.wsm ); + + // in white space pre mode: treat any space as non breakable + // and expand tabs to eight character wide columns. + if ( curtag.wsm == TQStyleSheetItem::WhiteSpacePre ) { + if ( c == '\t' ) { + c = ' '; + while( (++tabExpansionColumn)%8 ) + s += c; + } + if ( c == TQChar_linesep ) + tabExpansionColumn = 0; + else + tabExpansionColumn++; + + } + if ( c == ' ' || c == TQChar_linesep ) { + /* avoid overlong paragraphs by forcing a new + paragraph after 4096 characters. This case can + occur when loading undiscovered plain text + documents in rich text mode. Instead of hanging + forever, we do the trick. + */ + if ( curtag.wsm == TQStyleSheetItem::WhiteSpaceNormal && s.length() > 4096 ) do { + if ( doc[l] == '\n' ) { + hasNewPar = FALSE; // for a new paragraph ... + NEWPAR; + hasNewPar = FALSE; // ... and make it non-reusable + c = '\n'; // make sure we break below + break; + } + } while ( ++l < pos ); + } + } + + if ( c == '\n' ) + break; // break on newlines, pre delievers a TQChar_linesep + + bool c_isSpace = c.isSpace() && c.unicode() != 0x00a0U && !textEditMode; + + if ( curtag.wsm == TQStyleSheetItem::WhiteSpaceNormal && c_isSpace && space ) + continue; + if ( c == '\r' ) + continue; + space = c_isSpace; + s += c; + } + if ( !s.isEmpty() && curtag.style->displayMode() != TQStyleSheetItem::DisplayNone ) { + hasNewPar = FALSE; + int index = TQMAX( curpar->length(),1) - 1; + curpar->append( s ); + if (curtag.wsm != TQStyleSheetItem::WhiteSpaceNormal) { + TQTextString *str = curpar->string(); + for (uint i = index; i < index + s.length(); ++i) + str->at(i).nobreak = TRUE; + } + + TQTextFormat* f = formatCollection()->format( &curtag.format ); + curpar->setFormat( index, s.length(), f, FALSE ); // do not use collection because we have done that already + f->ref += s.length() -1; // that what friends are for... + if ( !curtag.anchorHref.isEmpty() ) { + for ( int i = 0; i < int(s.length()); i++ ) + curpar->at(index + i)->setAnchor( TQString::null, curtag.anchorHref ); + } + if ( !anchorName.isEmpty() ) { + for ( int i = 0; i < int(s.length()); i++ ) + curpar->at(index + i)->setAnchor( anchorName, curpar->at(index + i)->anchorHref() ); + anchorName = TQString::null; + } + } + } + } + + if ( hasNewPar && curpar != fParag && !cursor && stylesPar != curpar ) { + // cleanup unused last paragraphs + curpar = curpar->p; + delete curpar->n; + } + + if ( !anchorName.isEmpty() ) { + curpar->at(curpar->length() - 1)->setAnchor( anchorName, curpar->at( curpar->length() - 1 )->anchorHref() ); + anchorName = TQString::null; + } + + + setRichTextMarginsInternal( styles, stylesPar ); + + if ( cursor ) { + cursor->gotoPreviousLetter(); + cursor->remove(); + } + delete vec; +} + +void TQTextDocument::setRichTextMarginsInternal( TQPtrList< TQPtrVector >& styles, TQTextParagraph* stylesPar ) +{ + // margin and line spacing calculation + TQPtrVector* prevStyle = 0; + TQPtrVector* curStyle = styles.first(); + TQPtrVector* nextStyle = styles.next(); + while ( stylesPar ) { + if ( !curStyle ) { + stylesPar = stylesPar->next(); + prevStyle = curStyle; + curStyle = nextStyle; + nextStyle = styles.next(); + continue; + } + + int i, mar; + TQStyleSheetItem* mainStyle = curStyle->size() ? (*curStyle)[curStyle->size()-1] : 0; + if ( mainStyle && mainStyle->displayMode() == TQStyleSheetItem::DisplayListItem ) + stylesPar->setListItem( TRUE ); + int numLists = 0; + for ( i = 0; i < (int)curStyle->size(); ++i ) { + if ( (*curStyle)[ i ]->displayMode() == TQStyleSheetItem::DisplayBlock + && (*curStyle)[ i ]->listStyle() != TQStyleSheetItem::ListStyleUndefined ) + numLists++; + } + stylesPar->ldepth = numLists; + if ( stylesPar->next() && nextStyle ) { + // also set the depth of the next paragraph, retquired for the margin calculation + numLists = 0; + for ( i = 0; i < (int)nextStyle->size(); ++i ) { + if ( (*nextStyle)[ i ]->displayMode() == TQStyleSheetItem::DisplayBlock + && (*nextStyle)[ i ]->listStyle() != TQStyleSheetItem::ListStyleUndefined ) + numLists++; + } + stylesPar->next()->ldepth = numLists; + } + + // do the top margin + TQStyleSheetItem* item = mainStyle; + int m; + if (stylesPar->utm > 0 ) { + m = stylesPar->utm-1; + stylesPar->utm = 0; + } else { + m = TQMAX(0, item->margin( TQStyleSheetItem::MarginTop ) ); + if ( stylesPar->ldepth ) + if ( item->displayMode() == TQStyleSheetItem::DisplayListItem ) + m /= stylesPar->ldepth * stylesPar->ldepth; + else + m = 0; + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( prevStyle && i < (int) prevStyle->size() && + ( item->displayMode() == TQStyleSheetItem::DisplayBlock && + (*prevStyle)[ i ] == item ) ) + break; + // emulate CSS2' standard 0 vertical margin for multiple ul or ol tags + if ( item->listStyle() != TQStyleSheetItem::ListStyleUndefined && + ( ( i> 0 && (*curStyle)[ i-1 ] == item ) || (*curStyle)[i+1] == item ) ) + continue; + mar = TQMAX( 0, item->margin( TQStyleSheetItem::MarginTop ) ); + m = TQMAX( m, mar ); + } + stylesPar->utm = m - stylesPar->topMargin(); + + // do the bottom margin + item = mainStyle; + if (stylesPar->ubm > 0 ) { + m = stylesPar->ubm-1; + stylesPar->ubm = 0; + } else { + m = TQMAX(0, item->margin( TQStyleSheetItem::MarginBottom ) ); + if ( stylesPar->ldepth ) + if ( item->displayMode() == TQStyleSheetItem::DisplayListItem ) + m /= stylesPar->ldepth * stylesPar->ldepth; + else + m = 0; + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( nextStyle && i < (int) nextStyle->size() && + ( item->displayMode() == TQStyleSheetItem::DisplayBlock && + (*nextStyle)[ i ] == item ) ) + break; + // emulate CSS2' standard 0 vertical margin for multiple ul or ol tags + if ( item->listStyle() != TQStyleSheetItem::ListStyleUndefined && + ( ( i> 0 && (*curStyle)[ i-1 ] == item ) || (*curStyle)[i+1] == item ) ) + continue; + mar = TQMAX(0, item->margin( TQStyleSheetItem::MarginBottom ) ); + m = TQMAX( m, mar ); + } + stylesPar->ubm = m - stylesPar->bottomMargin(); + + // do the left margin, simplyfied + item = mainStyle; + if (stylesPar->ulm > 0 ) { + m = stylesPar->ulm-1; + stylesPar->ulm = 0; + } else { + m = TQMAX( 0, item->margin( TQStyleSheetItem::MarginLeft ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + m += TQMAX( 0, item->margin( TQStyleSheetItem::MarginLeft ) ); + } + stylesPar->ulm = m - stylesPar->leftMargin(); + + // do the right margin, simplyfied + item = mainStyle; + if (stylesPar->urm > 0 ) { + m = stylesPar->urm-1; + stylesPar->urm = 0; + } else { + m = TQMAX( 0, item->margin( TQStyleSheetItem::MarginRight ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + m += TQMAX( 0, item->margin( TQStyleSheetItem::MarginRight ) ); + } + stylesPar->urm = m - stylesPar->rightMargin(); + + // do the first line margin, which really should be called text-indent + item = mainStyle; + if (stylesPar->uflm > 0 ) { + m = stylesPar->uflm-1; + stylesPar->uflm = 0; + } else { + m = TQMAX( 0, item->margin( TQStyleSheetItem::MarginFirstLine ) ); + } + for ( i = (int)curStyle->size() - 2 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + mar = TQMAX( 0, item->margin( TQStyleSheetItem::MarginFirstLine ) ); + m = TQMAX( m, mar ); + } + stylesPar->uflm =m - stylesPar->firstLineMargin(); + + // do the bogus line "spacing", which really is just an extra margin + item = mainStyle; + for ( i = (int)curStyle->size() - 1 ; i >= 0; --i ) { + item = (*curStyle)[ i ]; + if ( item->lineSpacing() != TQStyleSheetItem::Undefined ) { + stylesPar->ulinespacing = item->lineSpacing(); + if ( formatCollection() && + stylesPar->ulinespacing < formatCollection()->defaultFormat()->height() ) + stylesPar->ulinespacing += formatCollection()->defaultFormat()->height(); + break; + } + } + + stylesPar = stylesPar->next(); + prevStyle = curStyle; + curStyle = nextStyle; + nextStyle = styles.next(); + } +} + +void TQTextDocument::setText( const TQString &text, const TQString &context ) +{ + focusIndicator.parag = 0; + selections.clear(); + if ( txtFormat == TQt::AutoText && TQStyleSheet::mightBeRichText( text ) || + txtFormat == TQt::RichText ) + setRichText( text, context ); + else + setPlainText( text ); +} + +TQString TQTextDocument::plainText() const +{ + TQString buffer; + TQString s; + TQTextParagraph *p = fParag; + while ( p ) { + if ( !p->mightHaveCustomItems ) { + const TQTextString *ts = p->string(); // workaround VC++ and Borland + s = ts->toString(); // with FALSE we don't fix spaces (nbsp) + } else { + for ( int i = 0; i < p->length() - 1; ++i ) { +#ifndef QT_NO_TEXTCUSTOMITEM + if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + s += "\n"; + TQTextTable *t = (TQTextTable*)p->at( i )->customItem(); + TQPtrList cells = t->tableCells(); + for ( TQTextTableCell *c = cells.first(); c; c = cells.next() ) + s += c->richText()->plainText() + "\n"; + s += "\n"; + } + } else +#endif + { + s += p->at( i )->c; + } + } + } + s.remove( s.length() - 1, 1 ); + if ( p->next() ) + s += "\n"; + buffer += s; + p = p->next(); + } + return buffer; +} + +static TQString align_to_string( int a ) +{ + if ( a & TQt::AlignRight ) + return " align=\"right\""; + if ( a & TQt::AlignHCenter ) + return " align=\"center\""; + if ( a & TQt::AlignJustify ) + return " align=\"justify\""; + return TQString::null; +} + +static TQString direction_to_string( int d ) +{ + if ( d != TQChar::DirON ) + return ( d == TQChar::DirL? " dir=\"ltr\"" : " dir=\"rtl\"" ); + return TQString::null; +} + +static TQString list_value_to_string( int v ) +{ + if ( v != -1 ) + return " listvalue=\"" + TQString::number( v ) + "\""; + return TQString::null; +} + +static TQString list_style_to_string( int v ) +{ + switch( v ) { + case TQStyleSheetItem::ListDecimal: return "\"1\""; + case TQStyleSheetItem::ListLowerAlpha: return "\"a\""; + case TQStyleSheetItem::ListUpperAlpha: return "\"A\""; + case TQStyleSheetItem::ListDisc: return "\"disc\""; + case TQStyleSheetItem::ListSquare: return "\"square\""; + case TQStyleSheetItem::ListCircle: return "\"circle\""; + default: + return TQString::null; + } +} + +static inline bool list_is_ordered( int v ) +{ + return v == TQStyleSheetItem::ListDecimal || + v == TQStyleSheetItem::ListLowerAlpha || + v == TQStyleSheetItem::ListUpperAlpha; +} + + +static TQString margin_to_string( TQStyleSheetItem* style, int t, int b, int l, int r, int fl ) +{ + TQString s; + if ( l > 0 ) + s += TQString(!!s?";":"") + "margin-left:" + TQString::number(l+TQMAX(0,style->margin(TQStyleSheetItem::MarginLeft))) + "px"; + if ( r > 0 ) + s += TQString(!!s?";":"") + "margin-right:" + TQString::number(r+TQMAX(0,style->margin(TQStyleSheetItem::MarginRight))) + "px"; + if ( t > 0 ) + s += TQString(!!s?";":"") + "margin-top:" + TQString::number(t+TQMAX(0,style->margin(TQStyleSheetItem::MarginTop))) + "px"; + if ( b > 0 ) + s += TQString(!!s?";":"") + "margin-bottom:" + TQString::number(b+TQMAX(0,style->margin(TQStyleSheetItem::MarginBottom))) + "px"; + if ( fl > 0 ) + s += TQString(!!s?";":"") + "text-indent:" + TQString::number(fl+TQMAX(0,style->margin(TQStyleSheetItem::MarginFirstLine))) + "px"; + if ( !!s ) + return " style=\"" + s + "\""; + return TQString::null; +} + +TQString TQTextDocument::richText() const +{ + TQString s = ""; + if ( !par ) { + s += "defaultFormat()->font().pointSize() ); + s += "pt;font-family:"; + s += formatCollection()->defaultFormat()->font().family(); + s +="\">"; + } + TQTextParagraph* p = fParag; + + TQStyleSheetItem* item_p = styleSheet()->item("p"); + TQStyleSheetItem* item_div = styleSheet()->item("div"); + TQStyleSheetItem* item_ul = styleSheet()->item("ul"); + TQStyleSheetItem* item_ol = styleSheet()->item("ol"); + TQStyleSheetItem* item_li = styleSheet()->item("li"); + if ( !item_p || !item_div || !item_ul || !item_ol || !item_li ) { + qWarning( "TQTextEdit: cannot export HTML due to insufficient stylesheet (lack of p, div, ul, ol, or li)" ); + return TQString::null; + } + int pastListDepth = 0; + int listDepth = 0; +#if 0 + int futureListDepth = 0; +#endif + TQMemArray listStyles(10); + + while ( p ) { + listDepth = p->listDepth(); + if ( listDepth < pastListDepth ) { + for ( int i = pastListDepth; i > listDepth; i-- ) + s += list_is_ordered( listStyles[i] ) ? "
" : ""; + s += '\n'; + } else if ( listDepth > pastListDepth ) { + s += '\n'; + listStyles.resize( TQMAX( (int)listStyles.size(), listDepth+1 ) ); + TQString list_type; + listStyles[listDepth] = p->listStyle(); + if ( !list_is_ordered( p->listStyle() ) || item_ol->listStyle() != p->listStyle() ) + list_type = " type=" + list_style_to_string( p->listStyle() ); + for ( int i = pastListDepth; i < listDepth; i++ ) { + s += list_is_ordered( p->listStyle() ) ? ""; + } + } else { + s += '\n'; + } + + TQString ps = p->richText(); + +#if 0 + // for the bottom margin we need to know whether we are at the end of a list + futureListDepth = 0; + if ( listDepth > 0 && p->next() ) + futureListDepth = p->next()->listDepth(); +#endif + + if ( richTextExportStart && richTextExportStart->paragraph() ==p && + richTextExportStart->index() == 0 ) + s += ""; + + if ( p->isListItem() ) { + s += "listStyle() != listStyles[listDepth] ) + s += " type=" + list_style_to_string( p->listStyle() ); + s +=align_to_string( p->alignment() ); + s += margin_to_string( item_li, p->utm, p->ubm, p->ulm, p->urm, p->uflm ); + s += list_value_to_string( p->listValue() ); + s += direction_to_string( p->direction() ); + s +=">"; + s += ps; + s += ""; + } else if ( p->listDepth() ) { + s += "alignment() ); + s += margin_to_string( item_div, p->utm, p->ubm, p->ulm, p->urm, p->uflm ); + s +=direction_to_string( p->direction() ); + s += ">"; + s += ps; + s += ""; + } else { + // normal paragraph item + s += "alignment() ); + s += margin_to_string( item_p, p->utm, p->ubm, p->ulm, p->urm, p->uflm ); + s +=direction_to_string( p->direction() ); + s += ">"; + s += ps; + s += "

"; + } + pastListDepth = listDepth; + p = p->next(); + } + while ( listDepth > 0 ) { + s += list_is_ordered( listStyles[listDepth] ) ? "" : ""; + listDepth--; + } + + if ( !par ) + s += "\n\n"; + + return s; +} + +TQString TQTextDocument::text() const +{ + if ( txtFormat == TQt::AutoText && preferRichText || txtFormat == TQt::RichText ) + return richText(); + return plainText(); +} + +TQString TQTextDocument::text( int parag ) const +{ + TQTextParagraph *p = paragAt( parag ); + if ( !p ) + return TQString::null; + + if ( txtFormat == TQt::AutoText && preferRichText || txtFormat == TQt::RichText ) + return p->richText(); + else + return p->string()->toString(); +} + +void TQTextDocument::invalidate() +{ + TQTextParagraph *s = fParag; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); + } +} + +void TQTextDocument::selectionStart( int id, int ¶gId, int &index ) +{ + TQMap::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return; + TQTextDocumentSelection &sel = *it; + paragId = !sel.swapped ? sel.startCursor.paragraph()->paragId() : sel.endCursor.paragraph()->paragId(); + index = !sel.swapped ? sel.startCursor.index() : sel.endCursor.index(); +} + +TQTextCursor TQTextDocument::selectionStartCursor( int id) +{ + TQMap::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return TQTextCursor( this ); + TQTextDocumentSelection &sel = *it; + if ( sel.swapped ) + return sel.endCursor; + return sel.startCursor; +} + +TQTextCursor TQTextDocument::selectionEndCursor( int id) +{ + TQMap::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return TQTextCursor( this ); + TQTextDocumentSelection &sel = *it; + if ( !sel.swapped ) + return sel.endCursor; + return sel.startCursor; +} + +void TQTextDocument::selectionEnd( int id, int ¶gId, int &index ) +{ + TQMap::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return; + TQTextDocumentSelection &sel = *it; + paragId = sel.swapped ? sel.startCursor.paragraph()->paragId() : sel.endCursor.paragraph()->paragId(); + index = sel.swapped ? sel.startCursor.index() : sel.endCursor.index(); +} + +void TQTextDocument::addSelection( int id ) +{ + nSelections = TQMAX( nSelections, id + 1 ); +} + +static void setSelectionEndHelper( int id, TQTextDocumentSelection &sel, TQTextCursor &start, TQTextCursor &end ) +{ + TQTextCursor c1 = start; + TQTextCursor c2 = end; + if ( sel.swapped ) { + c1 = end; + c2 = start; + } + + c1.paragraph()->removeSelection( id ); + c2.paragraph()->removeSelection( id ); + if ( c1.paragraph() != c2.paragraph() ) { + c1.paragraph()->setSelection( id, c1.index(), c1.paragraph()->length() - 1 ); + c2.paragraph()->setSelection( id, 0, c2.index() ); + } else { + c1.paragraph()->setSelection( id, TQMIN( c1.index(), c2.index() ), TQMAX( c1.index(), c2.index() ) ); + } + + sel.startCursor = start; + sel.endCursor = end; + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() ) + sel.swapped = sel.startCursor.index() > sel.endCursor.index(); +} + +bool TQTextDocument::setSelectionEnd( int id, const TQTextCursor &cursor ) +{ + TQMap::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return FALSE; + TQTextDocumentSelection &sel = *it; + + TQTextCursor start = sel.startCursor; + TQTextCursor end = cursor; + + if ( start == end ) { + removeSelection( id ); + setSelectionStart( id, cursor ); + return TRUE; + } + + if ( sel.endCursor.paragraph() == end.paragraph() ) { + setSelectionEndHelper( id, sel, start, end ); + return TRUE; + } + + bool inSelection = FALSE; + TQTextCursor c( this ); + TQTextCursor tmp = sel.startCursor; + if ( sel.swapped ) + tmp = sel.endCursor; + tmp.restoreState(); + TQTextCursor tmp2 = cursor; + tmp2.restoreState(); + c.setParagraph( tmp.paragraph()->paragId() < tmp2.paragraph()->paragId() ? tmp.paragraph() : tmp2.paragraph() ); + bool hadStart = FALSE; + bool hadEnd = FALSE; + bool hadStartParag = FALSE; + bool hadEndParag = FALSE; + bool hadOldStart = FALSE; + bool hadOldEnd = FALSE; + bool leftSelection = FALSE; + sel.swapped = FALSE; + for ( ;; ) { + if ( c == start ) + hadStart = TRUE; + if ( c == end ) + hadEnd = TRUE; + if ( c.paragraph() == start.paragraph() ) + hadStartParag = TRUE; + if ( c.paragraph() == end.paragraph() ) + hadEndParag = TRUE; + if ( c == sel.startCursor ) + hadOldStart = TRUE; + if ( c == sel.endCursor ) + hadOldEnd = TRUE; + + if ( !sel.swapped && + ( hadEnd && !hadStart || + hadEnd && hadStart && start.paragraph() == end.paragraph() && start.index() > end.index() ) ) + sel.swapped = TRUE; + + if ( c == end && hadStartParag || + c == start && hadEndParag ) { + TQTextCursor tmp = c; + tmp.restoreState(); + if ( tmp.paragraph() != c.paragraph() ) { + int sstart = tmp.paragraph()->selectionStart( id ); + tmp.paragraph()->removeSelection( id ); + tmp.paragraph()->setSelection( id, sstart, tmp.index() ); + } + } + + if ( inSelection && + ( c == end && hadStart || c == start && hadEnd ) ) + leftSelection = TRUE; + else if ( !leftSelection && !inSelection && ( hadStart || hadEnd ) ) + inSelection = TRUE; + + bool noSelectionAnymore = hadOldStart && hadOldEnd && leftSelection && !inSelection && !c.paragraph()->hasSelection( id ) && c.atParagEnd(); + c.paragraph()->removeSelection( id ); + if ( inSelection ) { + if ( c.paragraph() == start.paragraph() && start.paragraph() == end.paragraph() ) { + c.paragraph()->setSelection( id, TQMIN( start.index(), end.index() ), TQMAX( start.index(), end.index() ) ); + } else if ( c.paragraph() == start.paragraph() && !hadEndParag ) { + c.paragraph()->setSelection( id, start.index(), c.paragraph()->length() - 1 ); + } else if ( c.paragraph() == end.paragraph() && !hadStartParag ) { + c.paragraph()->setSelection( id, end.index(), c.paragraph()->length() - 1 ); + } else if ( c.paragraph() == end.paragraph() && hadEndParag ) { + c.paragraph()->setSelection( id, 0, end.index() ); + } else if ( c.paragraph() == start.paragraph() && hadStartParag ) { + c.paragraph()->setSelection( id, 0, start.index() ); + } else { + c.paragraph()->setSelection( id, 0, c.paragraph()->length() - 1 ); + } + } + + if ( leftSelection ) + inSelection = FALSE; + + if ( noSelectionAnymore ) + break; + // *ugle*hack optimization + TQTextParagraph *p = c.paragraph(); + if ( p->mightHaveCustomItems || p == start.paragraph() || p == end.paragraph() || p == lastParagraph() ) { + c.gotoNextLetter(); + if ( p == lastParagraph() && c.atParagEnd() ) + break; + } else { + if ( p->document()->parent() ) + do { + c.gotoNextLetter(); + } while ( c.paragraph() == p ); + else + c.setParagraph( p->next() ); + } + } + + if ( !sel.swapped ) + sel.startCursor.paragraph()->setSelection( id, sel.startCursor.index(), sel.startCursor.paragraph()->length() - 1 ); + + sel.startCursor = start; + sel.endCursor = end; + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() ) + sel.swapped = sel.startCursor.index() > sel.endCursor.index(); + + setSelectionEndHelper( id, sel, start, end ); + + return TRUE; +} + +void TQTextDocument::selectAll( int id ) +{ + removeSelection( id ); + + TQTextDocumentSelection sel; + sel.swapped = FALSE; + TQTextCursor c( this ); + + c.setParagraph( fParag ); + c.setIndex( 0 ); + sel.startCursor = c; + + c.setParagraph( lParag ); + c.setIndex( lParag->length() - 1 ); + sel.endCursor = c; + + selections.insert( id, sel ); + + TQTextParagraph *p = fParag; + while ( p ) { + p->setSelection( id, 0, p->length() - 1 ); + p = p->next(); + } + + for ( TQTextDocument *d = childList.first(); d; d = childList.next() ) + d->selectAll( id ); +} + +bool TQTextDocument::removeSelection( int id ) +{ + if ( !selections.contains( id ) ) + return FALSE; + + TQTextDocumentSelection &sel = selections[ id ]; + + TQTextCursor start = sel.swapped ? sel.endCursor : sel.startCursor; + TQTextCursor end = sel.swapped ? sel.startCursor : sel.endCursor; + TQTextParagraph* p = 0; + while ( start != end ) { + if ( p != start.paragraph() ) { + p = start.paragraph(); + p->removeSelection( id ); + //### avoid endless loop by all means necessary, did somebody mention refactoring? + if ( !parent() && p == lParag ) + break; + } + start.gotoNextLetter(); + } + p = start.paragraph(); + p->removeSelection( id ); + selections.remove( id ); + return TRUE; +} + +TQString TQTextDocument::selectedText( int id, bool asRichText ) const +{ + TQMap::ConstIterator it = selections.find( id ); + if ( it == selections.end() ) + return TQString::null; + + TQTextDocumentSelection sel = *it; + + + TQTextCursor c1 = sel.startCursor; + TQTextCursor c2 = sel.endCursor; + if ( sel.swapped ) { + c2 = sel.startCursor; + c1 = sel.endCursor; + } + + /* 3.0.3 improvement: Make it possible to get a reasonable + selection inside a table. This approach is very conservative: + make sure that both cursors have the same depth level and point + to paragraphs within the same text document. + + Meaning if you select text in two table cells, you will get the + entire table. This is still far better than the 3.0.2, where + you always got the entire table. + + ### Fix this properly when refactoring + */ + while ( c2.nestedDepth() > c1.nestedDepth() ) + c2.oneUp(); + while ( c1.nestedDepth() > c2.nestedDepth() ) + c1.oneUp(); + while ( c1.nestedDepth() && c2.nestedDepth() && + c1.paragraph()->document() != c2.paragraph()->document() ) { + c1.oneUp(); + c2.oneUp(); + } + // do not trust sel_swapped with tables. Fix this properly when refactoring as well + if ( c1.paragraph()->paragId() > c2.paragraph()->paragId() || + (c1.paragraph() == c2.paragraph() && c1.index() > c2.index() ) ) { + TQTextCursor tmp = c1; + c2 = c1; + c1 = tmp; + } + + // end selection 3.0.3 improvement + + if ( asRichText && !parent() ) { + richTextExportStart = &c1; + richTextExportEnd = &c2; + + TQString sel = richText(); + int from = sel.find( "" ); + if ( from >= 0 ) { + from += 20; + // find the previous span and move it into the start fragment before we clip it + TQString prevspan; + int pspan = sel.findRev( " sel.findRev( "', pspan ); + prevspan = sel.mid( pspan, spanend - pspan + 1 ); + } + int to = sel.findRev( "" ); + if ( from <= to ) + sel = "" + prevspan + sel.mid( from, to - from ); + } + richTextExportStart = richTextExportEnd = 0; + return sel; + } + + TQString s; + if ( c1.paragraph() == c2.paragraph() ) { + TQTextParagraph *p = c1.paragraph(); + int end = c2.index(); + if ( p->at( TQMAX( 0, end - 1 ) )->isCustom() ) + ++end; + if ( !p->mightHaveCustomItems ) { + s += p->string()->toString().mid( c1.index(), end - c1.index() ); + } else { + for ( int i = c1.index(); i < end; ++i ) { +#ifndef QT_NO_TEXTCUSTOMITEM + if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + s += "\n"; + TQTextTable *t = (TQTextTable*)p->at( i )->customItem(); + TQPtrList cells = t->tableCells(); + for ( TQTextTableCell *c = cells.first(); c; c = cells.next() ) + s += c->richText()->plainText() + "\n"; + s += "\n"; + } + } else +#endif + { + s += p->at( i )->c; + } + } + } + } else { + TQTextParagraph *p = c1.paragraph(); + int start = c1.index(); + while ( p ) { + int end = p == c2.paragraph() ? c2.index() : p->length() - 1; + if ( p == c2.paragraph() && p->at( TQMAX( 0, end - 1 ) )->isCustom() ) + ++end; + if ( !p->mightHaveCustomItems ) { + s += p->string()->toString().mid( start, end - start ); + if ( p != c2.paragraph() ) + s += "\n"; + } else { + for ( int i = start; i < end; ++i ) { +#ifndef QT_NO_TEXTCUSTOMITEM + if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + s += "\n"; + TQTextTable *t = (TQTextTable*)p->at( i )->customItem(); + TQPtrList cells = t->tableCells(); + for ( TQTextTableCell *c = cells.first(); c; c = cells.next() ) + s += c->richText()->plainText() + "\n"; + s += "\n"; + } + } else +#endif + { + s += p->at( i )->c; + } + } + } + start = 0; + if ( p == c2.paragraph() ) + break; + p = p->next(); + } + } + // ### workaround for plain text export until we get proper + // mime types: turn unicode line seperators into the more + // widely understood \n. Makes copy and pasting code snipplets + // from within Assistent possible + TQChar* uc = (TQChar*) s.unicode(); + for ( uint ii = 0; ii < s.length(); ii++ ) { + if ( uc[(int)ii] == TQChar_linesep ) + uc[(int)ii] = TQChar('\n'); + else if ( uc[(int)ii] == TQChar::nbsp ) + uc[(int)ii] = TQChar(' '); + } + return s; +} + +void TQTextDocument::setFormat( int id, TQTextFormat *f, int flags ) +{ + TQMap::ConstIterator it = selections.find( id ); + if ( it == selections.end() ) + return; + + TQTextDocumentSelection sel = *it; + + TQTextCursor c1 = sel.startCursor; + TQTextCursor c2 = sel.endCursor; + if ( sel.swapped ) { + c2 = sel.startCursor; + c1 = sel.endCursor; + } + + c2.restoreState(); + c1.restoreState(); + + if ( c1.paragraph() == c2.paragraph() ) { + c1.paragraph()->setFormat( c1.index(), c2.index() - c1.index(), f, TRUE, flags ); + return; + } + + c1.paragraph()->setFormat( c1.index(), c1.paragraph()->length() - c1.index(), f, TRUE, flags ); + TQTextParagraph *p = c1.paragraph()->next(); + while ( p && p != c2.paragraph() ) { + p->setFormat( 0, p->length(), f, TRUE, flags ); + p = p->next(); + } + c2.paragraph()->setFormat( 0, c2.index(), f, TRUE, flags ); +} + +void TQTextDocument::removeSelectedText( int id, TQTextCursor *cursor ) +{ + TQMap::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return; + + TQTextDocumentSelection sel = *it; + TQTextCursor c1 = sel.startCursor; + TQTextCursor c2 = sel.endCursor; + if ( sel.swapped ) { + c2 = sel.startCursor; + c1 = sel.endCursor; + } + + // ### no support for editing tables yet + if ( c1.nestedDepth() || c2.nestedDepth() ) + return; + + c2.restoreState(); + c1.restoreState(); + + *cursor = c1; + removeSelection( id ); + + if ( c1.paragraph() == c2.paragraph() ) { + c1.paragraph()->remove( c1.index(), c2.index() - c1.index() ); + return; + } + + if ( c1.paragraph() == fParag && c1.index() == 0 && + c2.paragraph() == lParag && c2.index() == lParag->length() - 1 ) + cursor->setValid( FALSE ); + + bool didGoLeft = FALSE; + if ( c1.index() == 0 && c1.paragraph() != fParag ) { + cursor->gotoPreviousLetter(); + didGoLeft = cursor->isValid(); + } + + c1.paragraph()->remove( c1.index(), c1.paragraph()->length() - 1 - c1.index() ); + TQTextParagraph *p = c1.paragraph()->next(); + int dy = 0; + TQTextParagraph *tmp; + while ( p && p != c2.paragraph() ) { + tmp = p->next(); + dy -= p->rect().height(); + delete p; + p = tmp; + } + c2.paragraph()->remove( 0, c2.index() ); + while ( p ) { + p->move( dy ); + p->invalidate( 0 ); + p->setEndState( -1 ); + p = p->next(); + } + + + c1.paragraph()->join( c2.paragraph() ); + + if ( didGoLeft ) + cursor->gotoNextLetter(); +} + +void TQTextDocument::indentSelection( int id ) +{ + TQMap::Iterator it = selections.find( id ); + if ( it == selections.end() ) + return; + + TQTextDocumentSelection sel = *it; + TQTextParagraph *startParag = sel.startCursor.paragraph(); + TQTextParagraph *endParag = sel.endCursor.paragraph(); + if ( sel.endCursor.paragraph()->paragId() < sel.startCursor.paragraph()->paragId() ) { + endParag = sel.startCursor.paragraph(); + startParag = sel.endCursor.paragraph(); + } + + TQTextParagraph *p = startParag; + while ( p && p != endParag ) { + p->indent(); + p = p->next(); + } +} + +void TQTextDocument::addCommand( TQTextCommand *cmd ) +{ + commandHistory->addCommand( cmd ); +} + +TQTextCursor *TQTextDocument::undo( TQTextCursor *c ) +{ + return commandHistory->undo( c ); +} + +TQTextCursor *TQTextDocument::redo( TQTextCursor *c ) +{ + return commandHistory->redo( c ); +} + +bool TQTextDocument::find( TQTextCursor& cursor, const TQString &expr, bool cs, bool wo, bool forward ) +{ + removeSelection( Standard ); + TQTextParagraph *p = 0; + if ( expr.isEmpty() ) + return FALSE; + for (;;) { + if ( p != cursor.paragraph() ) { + p = cursor.paragraph(); + TQString s = cursor.paragraph()->string()->toString(); + int start = cursor.index(); + for ( ;; ) { + int res = forward ? s.find( expr, start, cs ) : s.findRev( expr, start, cs ); + int end = res + expr.length(); + if ( res == -1 || ( !forward && start <= res ) ) + break; + if ( !wo || ( ( res == 0 || s[ res - 1 ].isSpace() || s[ res - 1 ].isPunct() ) && + ( end == (int)s.length() || s[ end ].isSpace() || s[ end ].isPunct() ) ) ) { + removeSelection( Standard ); + cursor.setIndex( forward ? end : res ); + setSelectionStart( Standard, cursor ); + cursor.setIndex( forward ? res : end ); + setSelectionEnd( Standard, cursor ); + if ( !forward ) + cursor.setIndex( res ); + return TRUE; + } + start = res + (forward ? 1 : -1); + } + } + if ( forward ) { + if ( cursor.paragraph() == lastParagraph() && cursor.atParagEnd() ) + break; + cursor.gotoNextLetter(); + } else { + if ( cursor.paragraph() == firstParagraph() && cursor.atParagStart() ) + break; + cursor.gotoPreviousLetter(); + } + } + return FALSE; +} + +void TQTextDocument::setTextFormat( TQt::TextFormat f ) +{ + txtFormat = f; + if ( fParag == lParag && fParag->length() <= 1 ) + fParag->rtext = ( f == TQt::RichText ); +} + +TQt::TextFormat TQTextDocument::textFormat() const +{ + return txtFormat; +} + +bool TQTextDocument::inSelection( int selId, const TQPoint &pos ) const +{ + TQMap::ConstIterator it = selections.find( selId ); + if ( it == selections.end() ) + return FALSE; + + TQTextDocumentSelection sel = *it; + TQTextParagraph *startParag = sel.startCursor.paragraph(); + TQTextParagraph *endParag = sel.endCursor.paragraph(); + if ( sel.startCursor.paragraph() == sel.endCursor.paragraph() && + sel.startCursor.paragraph()->selectionStart( selId ) == sel.endCursor.paragraph()->selectionEnd( selId ) ) + return FALSE; + if ( sel.endCursor.paragraph()->paragId() < sel.startCursor.paragraph()->paragId() ) { + endParag = sel.startCursor.paragraph(); + startParag = sel.endCursor.paragraph(); + } + + TQTextParagraph *p = startParag; + while ( p ) { + if ( p->rect().contains( pos ) ) { + bool inSel = FALSE; + int selStart = p->selectionStart( selId ); + int selEnd = p->selectionEnd( selId ); + int y = 0; + int h = 0; + for ( int i = 0; i < p->length(); ++i ) { + if ( i == selStart ) + inSel = TRUE; + if ( i == selEnd ) + break; + if ( p->at( i )->lineStart ) { + y = (*p->lineStarts.find( i ))->y; + h = (*p->lineStarts.find( i ))->h; + } + if ( pos.y() - p->rect().y() >= y && pos.y() - p->rect().y() <= y + h ) { + if ( inSel && pos.x() >= p->at( i )->x && + pos.x() <= p->at( i )->x + p->at( i )->format()->width( p->at( i )->c ) ) + return TRUE; + } + } + } + if ( pos.y() < p->rect().y() ) + break; + if ( p == endParag ) + break; + p = p->next(); + } + + return FALSE; +} + +void TQTextDocument::doLayout( TQPainter *p, int w ) +{ + minw = wused = 0; + if ( !is_printer( p ) ) + p = 0; + withoutDoubleBuffer = ( p != 0 ); + TQPainter * oldPainter = TQTextFormat::painter(); + TQTextFormat::setPainter( p ); + tStopWidth = formatCollection()->defaultFormat()->width( 'x' ) * 8; + flow_->setWidth( w ); + cw = w; + vw = w; + TQTextParagraph *parag = fParag; + while ( parag ) { + parag->invalidate( 0 ); + if ( p ) + parag->adjustToPainter( p ); + parag->format(); + parag = parag->next(); + } + TQTextFormat::setPainter( oldPainter ); +} + +TQPixmap *TQTextDocument::bufferPixmap( const TQSize &s ) +{ + if ( !buf_pixmap ) + buf_pixmap = new TQPixmap( s.expandedTo( TQSize(1,1) ) ); + else if ( buf_pixmap->size() != s ) + buf_pixmap->resize( s.expandedTo( buf_pixmap->size() ) ); + return buf_pixmap; +} + +void TQTextDocument::draw( TQPainter *p, const TQRect &rect, const TQColorGroup &cg, const TQBrush *paper ) +{ + if ( !firstParagraph() ) + return; + + if ( paper ) { + p->setBrushOrigin( -int( p->translationX() ), + -int( p->translationY() ) ); + + p->fillRect( rect, *paper ); + } + + TQPainter * oldPainter = TQTextFormat::painter(); + TQTextFormat::setPainter( p ); + + if ( formatCollection()->defaultFormat()->color() != cg.text() ) + setDefaultFormat( formatCollection()->defaultFormat()->font(), cg.text() ); + + TQTextParagraph *parag = firstParagraph(); + while ( parag ) { + if ( !parag->isValid() ) + parag->format(); + int y = parag->rect().y(); + TQRect pr( parag->rect() ); + pr.setX( 0 ); + pr.setWidth( TQWIDGETSIZE_MAX ); + if ( !rect.isNull() && !rect.intersects( pr ) ) { + parag = parag->next(); + continue; + } + p->translate( 0, y ); + if ( rect.isValid() ) + parag->paint( *p, cg, 0, FALSE, rect.x(), rect.y(), rect.width(), rect.height() ); + else + parag->paint( *p, cg, 0, FALSE ); + p->translate( 0, -y ); + parag = parag->next(); + if ( !flow()->isEmpty() ) + flow()->drawFloatingItems( p, rect.x(), rect.y(), rect.width(), rect.height(), cg, FALSE ); + } + TQTextFormat::setPainter(oldPainter); +} + +void TQTextDocument::drawParagraph( TQPainter *p, TQTextParagraph *parag, int cx, int cy, int cw, int ch, + TQPixmap *&doubleBuffer, const TQColorGroup &cg, + bool drawCursor, TQTextCursor *cursor, bool resetChanged ) +{ + TQPainter *painter = 0; + if ( resetChanged ) + parag->setChanged( FALSE ); + TQRect ir( parag->rect() ); +#ifndef QT_NO_TEXTCUSTOMITEM + if (!parag->tableCell()) +#endif + ir.setWidth(width()); + + bool uDoubleBuffer = useDoubleBuffer( parag, p ); + + if ( uDoubleBuffer ) { + painter = new TQPainter; + if ( cx >= 0 && cy >= 0 ) + ir = ir.intersect( TQRect( cx, cy, cw, ch ) ); + if ( !doubleBuffer || + ir.width() > doubleBuffer->width() || + ir.height() > doubleBuffer->height() ) { + doubleBuffer = bufferPixmap( ir.size() ); + painter->begin( doubleBuffer ); + } else { + painter->begin( doubleBuffer ); + } + } else { + painter = p; + painter->translate( ir.x(), ir.y() ); + } + + painter->setBrushOrigin( -ir.x(), -ir.y() ); + + if ( uDoubleBuffer || is_printer( painter ) ) + painter->fillRect( TQRect( 0, 0, ir.width(), ir.height() ), parag->backgroundBrush( cg ) ); + else if ( cursor && cursor->paragraph() == parag ) + painter->fillRect( TQRect( parag->at( cursor->index() )->x, 0, 2, ir.height() ), + parag->backgroundBrush( cg ) ); + + painter->translate( -( ir.x() - parag->rect().x() ), + -( ir.y() - parag->rect().y() ) ); + parag->paint( *painter, cg, drawCursor ? cursor : 0, TRUE, cx, cy, cw, ch ); + + if ( uDoubleBuffer ) { + delete painter; + painter = 0; + p->drawPixmap( ir.topLeft(), *doubleBuffer, TQRect( TQPoint( 0, 0 ), ir.size() ) ); + } else { + painter->translate( -ir.x(), -ir.y() ); + } + + parag->document()->nextDoubleBuffered = FALSE; +} + +TQTextParagraph *TQTextDocument::draw( TQPainter *p, int cx, int cy, int cw, int ch, const TQColorGroup &cg, + bool onlyChanged, bool drawCursor, TQTextCursor *cursor, bool resetChanged ) +{ + if ( withoutDoubleBuffer || par && par->withoutDoubleBuffer ) { + withoutDoubleBuffer = TRUE; + TQRect r; + draw( p, r, cg ); + return 0; + } + withoutDoubleBuffer = FALSE; + + if ( !firstParagraph() ) + return 0; + + TQPainter * oldPainter = TQTextFormat::painter(); + TQTextFormat::setPainter( p ); + if ( formatCollection()->defaultFormat()->color() != cg.text() ) + setDefaultFormat( formatCollection()->defaultFormat()->font(), cg.text() ); + + if ( cx < 0 && cy < 0 ) { + cx = 0; + cy = 0; + cw = width(); + ch = height(); + } + + TQTextParagraph *lastFormatted = 0; + TQTextParagraph *parag = firstParagraph(); + + TQPixmap *doubleBuffer = 0; + + while ( parag ) { + lastFormatted = parag; + if ( !parag->isValid() ) + parag->format(); + + TQRect pr = parag->rect(); + pr.setWidth( parag->document()->width() ); + if ( pr.y() > cy + ch ) + goto floating; + TQRect clipr( cx, cy, cw, ch ); + if ( !pr.intersects( clipr ) || ( onlyChanged && !parag->hasChanged() ) ) { + pr.setWidth( parag->document()->width() ); + parag = parag->next(); + continue; + } + + drawParagraph( p, parag, cx, cy, cw, ch, doubleBuffer, cg, drawCursor, cursor, resetChanged ); + parag = parag->next(); + } + + parag = lastParagraph(); + + floating: + if ( parag->rect().y() + parag->rect().height() < parag->document()->height() ) { + if ( !parag->document()->parent() ) { + TQRect fillRect = TQRect( 0, parag->rect().y() + parag->rect().height(), parag->document()->width(), + parag->document()->height() - ( parag->rect().y() + parag->rect().height() ) ); + if ( TQRect( cx, cy, cw, ch ).intersects( fillRect ) ) + p->fillRect( fillRect, cg.brush( TQColorGroup::Base ) ); + } + if ( !flow()->isEmpty() ) { + TQRect cr( cx, cy, cw, ch ); + flow()->drawFloatingItems( p, cr.x(), cr.y(), cr.width(), cr.height(), cg, FALSE ); + } + } + + if ( buf_pixmap && buf_pixmap->height() > 300 ) { + delete buf_pixmap; + buf_pixmap = 0; + } + + TQTextFormat::setPainter(oldPainter); + return lastFormatted; +} + +/* + #### this function only sets the default font size in the format collection + */ +void TQTextDocument::setDefaultFormat( const TQFont &font, const TQColor &color ) +{ + bool reformat = font != fCollection->defaultFormat()->font(); + for ( TQTextDocument *d = childList.first(); d; d = childList.next() ) + d->setDefaultFormat( font, color ); + fCollection->updateDefaultFormat( font, color, sheet_ ); + + if ( !reformat ) + return; + tStopWidth = formatCollection()->defaultFormat()->width( 'x' ) * 8; + + // invalidate paragraphs and custom items + TQTextParagraph *p = fParag; + while ( p ) { + p->invalidate( 0 ); +#ifndef QT_NO_TEXTCUSTOMITEM + for ( int i = 0; i < p->length() - 1; ++i ) + if ( p->at( i )->isCustom() ) + p->at( i )->customItem()->invalidate(); +#endif + p = p->next(); + } +} + +#ifndef QT_NO_TEXTCUSTOMITEM +void TQTextDocument::registerCustomItem( TQTextCustomItem *i, TQTextParagraph *p ) +{ + if ( i && i->placement() != TQTextCustomItem::PlaceInline ) { + flow_->registerFloatingItem( i ); + p->registerFloatingItem( i ); + } + if (i) i->setParagraph( p ); + p->mightHaveCustomItems = mightHaveCustomItems = TRUE; +} + +void TQTextDocument::unregisterCustomItem( TQTextCustomItem *i, TQTextParagraph *p ) +{ + p->unregisterFloatingItem( i ); + i->setParagraph( 0 ); + flow_->unregisterFloatingItem( i ); +} +#endif + +bool TQTextDocument::hasFocusParagraph() const +{ + return !!focusIndicator.parag; +} + +TQString TQTextDocument::focusHref() const +{ + return focusIndicator.href; +} + +TQString TQTextDocument::focusName() const +{ + return focusIndicator.name; +} + +bool TQTextDocument::focusNextPrevChild( bool next ) +{ + if ( !focusIndicator.parag ) { + if ( next ) { + focusIndicator.parag = fParag; + focusIndicator.start = 0; + focusIndicator.len = 0; + } else { + focusIndicator.parag = lParag; + focusIndicator.start = lParag->length(); + focusIndicator.len = 0; + } + } else { + focusIndicator.parag->setChanged( TRUE ); + } + focusIndicator.href = TQString::null; + focusIndicator.name = TQString::null; + + if ( next ) { + TQTextParagraph *p = focusIndicator.parag; + int index = focusIndicator.start + focusIndicator.len; + while ( p ) { + for ( int i = index; i < p->length(); ++i ) { + if ( p->at( i )->isAnchor() ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = p->at( i )->anchorHref(); + focusIndicator.name = p->at( i )->anchorName(); + while ( i < p->length() ) { + if ( !p->at( i )->isAnchor() ) + return TRUE; + focusIndicator.len++; + i++; + } +#ifndef QT_NO_TEXTCUSTOMITEM + } else if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + TQTextTable *t = (TQTextTable*)p->at( i )->customItem(); + TQPtrList cells = t->tableCells(); + // first try to continue + TQTextTableCell *c; + bool resetCells = TRUE; + for ( c = cells.first(); c; c = cells.next() ) { + if ( c->richText()->hasFocusParagraph() ) { + if ( c->richText()->focusNextPrevChild( next ) ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = c->richText()->focusHref(); + focusIndicator.name = c->richText()->focusName(); + return TRUE; + } else { + resetCells = FALSE; + c = cells.next(); + break; + } + } + } + // now really try + if ( resetCells ) + c = cells.first(); + for ( ; c; c = cells.next() ) { + if ( c->richText()->focusNextPrevChild( next ) ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = c->richText()->focusHref(); + focusIndicator.name = c->richText()->focusName(); + return TRUE; + } + } + } +#endif + } + } + index = 0; + p = p->next(); + } + } else { + TQTextParagraph *p = focusIndicator.parag; + int index = focusIndicator.start - 1; + if ( focusIndicator.len == 0 && index < focusIndicator.parag->length() - 1 ) + index++; + while ( p ) { + for ( int i = index; i >= 0; --i ) { + if ( p->at( i )->isAnchor() ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = p->at( i )->anchorHref(); + focusIndicator.name = p->at( i )->anchorName(); + while ( i >= -1 ) { + if ( i < 0 || !p->at( i )->isAnchor() ) { + focusIndicator.start++; + return TRUE; + } + if ( i < 0 ) + break; + focusIndicator.len++; + focusIndicator.start--; + i--; + } +#ifndef QT_NO_TEXTCUSTOMITEM + } else if ( p->at( i )->isCustom() ) { + if ( p->at( i )->customItem()->isNested() ) { + TQTextTable *t = (TQTextTable*)p->at( i )->customItem(); + TQPtrList cells = t->tableCells(); + // first try to continue + TQTextTableCell *c; + bool resetCells = TRUE; + for ( c = cells.last(); c; c = cells.prev() ) { + if ( c->richText()->hasFocusParagraph() ) { + if ( c->richText()->focusNextPrevChild( next ) ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = c->richText()->focusHref(); + focusIndicator.name = c->richText()->focusName(); + return TRUE; + } else { + resetCells = FALSE; + c = cells.prev(); + break; + } + } + if ( cells.at() == 0 ) + break; + } + // now really try + if ( resetCells ) + c = cells.last(); + for ( ; c; c = cells.prev() ) { + if ( c->richText()->focusNextPrevChild( next ) ) { + p->setChanged( TRUE ); + focusIndicator.parag = p; + focusIndicator.start = i; + focusIndicator.len = 0; + focusIndicator.href = c->richText()->focusHref(); + focusIndicator.name = c->richText()->focusName(); + return TRUE; + } + if ( cells.at() == 0 ) + break; + } + } +#endif + } + } + p = p->prev(); + if ( p ) + index = p->length() - 1; + } + } + + focusIndicator.parag = 0; + + return FALSE; +} + +int TQTextDocument::length() const +{ + int l = -1; + TQTextParagraph *p = fParag; + while ( p ) { + l += p->length(); + p = p->next(); + } + return TQMAX(0,l); +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +int TQTextFormat::width( const TQChar &c ) const +{ + if ( c.unicode() == 0xad ) // soft hyphen + return 0; + if ( !pntr || !pntr->isActive() ) { + if ( c == '\t' ) + return fm.width( ' ' ); + if ( ha == AlignNormal ) { + int w; + if ( c.row() ) + w = fm.width( c ); + else + w = widths[ c.unicode() ]; + if ( w == 0 && !c.row() ) { + w = fm.width( c ); + ( (TQTextFormat*)this )->widths[ c.unicode() ] = w; + } + return w; + } else { + TQFont f( fn ); + if ( usePixelSizes ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + TQFontMetrics fm_( f ); + return fm_.width( c ); + } + } + + TQFont f( fn ); + if ( ha != AlignNormal ) { + if ( usePixelSizes ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + } + applyFont( f ); + + return pntr_fm->width( c ); +} + +int TQTextFormat::width( const TQString &str, int pos ) const +{ + int w = 0; + if ( str.unicode()[ pos ].unicode() == 0xad ) + return w; + if ( !pntr || !pntr->isActive() ) { + if ( ha == AlignNormal ) { + w = fm.charWidth( str, pos ); + } else { + TQFont f( fn ); + if ( usePixelSizes ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + TQFontMetrics fm_( f ); + w = fm_.charWidth( str, pos ); + } + } else { + TQFont f( fn ); + if ( ha != AlignNormal ) { + if ( usePixelSizes ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + } + applyFont( f ); + w = pntr_fm->charWidth( str, pos ); + } + return w; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextString::TQTextString() +{ + bidiDirty = TRUE; + bidi = FALSE; + rightToLeft = FALSE; + dir = TQChar::DirON; +} + +TQTextString::TQTextString( const TQTextString &s ) +{ + bidiDirty = TRUE; + bidi = s.bidi; + rightToLeft = s.rightToLeft; + dir = s.dir; + data = s.data; + data.detach(); + for ( int i = 0; i < (int)data.size(); ++i ) { + TQTextFormat *f = data[i].format(); + if ( f ) + f->addRef(); + } +} + +void TQTextString::insert( int index, const TQString &s, TQTextFormat *f ) +{ + insert( index, s.unicode(), s.length(), f ); +} + +void TQTextString::insert( int index, const TQChar *unicode, int len, TQTextFormat *f ) +{ + int os = data.size(); + data.resize( data.size() + len, TQGArray::SpeedOptim ); + if ( index < os ) { + memmove( data.data() + index + len, data.data() + index, + sizeof( TQTextStringChar ) * ( os - index ) ); + } + TQTextStringChar *ch = data.data() + index; + for ( int i = 0; i < len; ++i ) { + ch->x = 0; + ch->lineStart = 0; + ch->d.format = 0; + ch->nobreak = FALSE; + ch->type = TQTextStringChar::Regular; + ch->d.format = f; + ch->rightToLeft = 0; + ch->c = unicode[i]; + ++ch; + } + bidiDirty = TRUE; +} + +TQTextString::~TQTextString() +{ + clear(); +} + +void TQTextString::insert( int index, TQTextStringChar *c, bool doAddRefFormat ) +{ + int os = data.size(); + data.resize( data.size() + 1, TQGArray::SpeedOptim ); + if ( index < os ) { + memmove( data.data() + index + 1, data.data() + index, + sizeof( TQTextStringChar ) * ( os - index ) ); + } + TQTextStringChar &ch = data[ (int)index ]; + ch.c = c->c; + ch.x = 0; + ch.lineStart = 0; + ch.rightToLeft = 0; + ch.d.format = 0; + ch.type = TQTextStringChar::Regular; + ch.nobreak = FALSE; + if ( doAddRefFormat && c->format() ) + c->format()->addRef(); + ch.setFormat( c->format() ); + bidiDirty = TRUE; +} + +int TQTextString::appendParagraphs( TQTextParagraph *start, TQTextParagraph *end ) +{ + int paragCount = 0; + int newLength = data.size(); + TQTextParagraph *p = start; + for (; p != end; p = p->next()) { + newLength += p->length(); + ++paragCount; + } + + const int oldLength = data.size(); + data.resize(newLength, TQGArray::SpeedOptim); + + TQTextStringChar *d = &data[oldLength]; + for (p = start; p != end; p = p->next()) { + const TQTextStringChar * const src = p->at(0); + int i = 0; + for (; i < p->length() - 1; ++i) { + d[i].c = src[i].c; + d[i].x = 0; + d[i].lineStart = 0; + d[i].rightToLeft = 0; + d[i].type = TQTextStringChar::Regular; + d[i].nobreak = FALSE; + d[i].d.format = src[i].format(); + if (d[i].d.format) + d[i].d.format->addRef(); + } + d[i].x = 0; + d[i].lineStart = 0; + d[i].nobreak = FALSE; + d[i].type = TQTextStringChar::Regular; + d[i].d.format = 0; + d[i].rightToLeft = 0; + d[i].c = '\n'; + d += p->length(); + } + + bidiDirty = TRUE; + return paragCount; +} + +void TQTextString::truncate( int index ) +{ + index = TQMAX( index, 0 ); + index = TQMIN( index, (int)data.size() - 1 ); + if ( index < (int)data.size() ) { + for ( int i = index + 1; i < (int)data.size(); ++i ) { + TQTextStringChar &ch = data[ i ]; +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !(ch.type == TQTextStringChar::Regular) ) { + delete ch.customItem(); + if ( ch.d.custom->format ) + ch.d.custom->format->removeRef(); + delete ch.d.custom; + ch.d.custom = 0; + } else +#endif + if ( ch.format() ) { + ch.format()->removeRef(); + } + } + } + data.truncate( index ); + bidiDirty = TRUE; +} + +void TQTextString::remove( int index, int len ) +{ + for ( int i = index; i < (int)data.size() && i - index < len; ++i ) { + TQTextStringChar &ch = data[ i ]; +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !(ch.type == TQTextStringChar::Regular) ) { + delete ch.customItem(); + if ( ch.d.custom->format ) + ch.d.custom->format->removeRef(); + delete ch.d.custom; + ch.d.custom = 0; + } else +#endif + if ( ch.format() ) { + ch.format()->removeRef(); + } + } + memmove( data.data() + index, data.data() + index + len, + sizeof( TQTextStringChar ) * ( data.size() - index - len ) ); + data.resize( data.size() - len, TQGArray::SpeedOptim ); + bidiDirty = TRUE; +} + +void TQTextString::clear() +{ + for ( int i = 0; i < (int)data.count(); ++i ) { + TQTextStringChar &ch = data[ i ]; +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !(ch.type == TQTextStringChar::Regular) ) { + if ( ch.customItem() && ch.customItem()->placement() == TQTextCustomItem::PlaceInline ) + delete ch.customItem(); + if ( ch.d.custom->format ) + ch.d.custom->format->removeRef(); + delete ch.d.custom; + ch.d.custom = 0; + } else +#endif + if ( ch.format() ) { + ch.format()->removeRef(); + } + } + data.resize( 0 ); + bidiDirty = TRUE; +} + +void TQTextString::setFormat( int index, TQTextFormat *f, bool useCollection ) +{ + TQTextStringChar &ch = data[ index ]; + if ( useCollection && ch.format() ) + ch.format()->removeRef(); + ch.setFormat( f ); +} + +void TQTextString::checkBidi() const +{ + TQTextString *that = (TQTextString *)this; + that->bidiDirty = FALSE; + int length = data.size(); + if ( !length ) { + that->bidi = FALSE; + that->rightToLeft = dir == TQChar::DirR; + return; + } + const TQTextStringChar *start = data.data(); + const TQTextStringChar *end = start + length; + + ((TQTextString *)this)->stringCache = toString(data); + + + // determines the properties we need for layouting + TQTextEngine textEngine( toString(), 0 ); + textEngine.direction = (TQChar::Direction) dir; + textEngine.itemize(TQTextEngine::SingleLine); + const TQCharAttributes *ca = textEngine.attributes() + length-1; + TQTextStringChar *ch = (TQTextStringChar *)end - 1; + TQScriptItem *item = &textEngine.items[textEngine.items.size()-1]; + unsigned char bidiLevel = item->analysis.bidiLevel; + if ( bidiLevel ) + that->bidi = TRUE; + int pos = length-1; + while ( ch >= start ) { + if ( item->position > pos ) { + --item; + Q_ASSERT( item >= &textEngine.items[0] ); + Q_ASSERT( item < &textEngine.items[textEngine.items.size()] ); + bidiLevel = item->analysis.bidiLevel; + if ( bidiLevel ) + that->bidi = TRUE; + } + ch->softBreak = ca->softBreak; + ch->whiteSpace = ca->whiteSpace; + ch->charStop = ca->charStop; + ch->wordStop = ca->wordStop; + ch->bidiLevel = bidiLevel; + ch->rightToLeft = (bidiLevel%2); + --ch; + --ca; + --pos; + } + + if ( dir == TQChar::DirR ) { + that->bidi = TRUE; + that->rightToLeft = TRUE; + } else if ( dir == TQChar::DirL ) { + that->rightToLeft = FALSE; + } else { + that->rightToLeft = (textEngine.direction == TQChar::DirR); + } +} + +void TQTextDocument::setStyleSheet( TQStyleSheet *s ) +{ + if ( !s ) + return; + sheet_ = s; + list_tm = list_bm = par_tm = par_bm = 12; + list_lm = 40; + li_tm = li_bm = 0; + TQStyleSheetItem* item = s->item( "ol" ); + if ( item ) { + list_tm = TQMAX(0,item->margin( TQStyleSheetItem::MarginTop )); + list_bm = TQMAX(0,item->margin( TQStyleSheetItem::MarginBottom )); + list_lm = TQMAX(0,item->margin( TQStyleSheetItem::MarginLeft )); + } + if ( (item = s->item( "li" ) ) ) { + li_tm = TQMAX(0,item->margin( TQStyleSheetItem::MarginTop )); + li_bm = TQMAX(0,item->margin( TQStyleSheetItem::MarginBottom )); + } + if ( (item = s->item( "p" ) ) ) { + par_tm = TQMAX(0,item->margin( TQStyleSheetItem::MarginTop )); + par_bm = TQMAX(0,item->margin( TQStyleSheetItem::MarginBottom )); + } +} + +void TQTextDocument::setUnderlineLinks( bool b ) { + underlLinks = b; + for ( TQTextDocument *d = childList.first(); d; d = childList.next() ) + d->setUnderlineLinks( b ); +} + +void TQTextStringChar::setFormat( TQTextFormat *f ) +{ + if ( type == Regular ) { + d.format = f; + } else { +#ifndef QT_NO_TEXTCUSTOMITEM + if ( !d.custom ) { + d.custom = new CustomData; + d.custom->custom = 0; + } + d.custom->format = f; +#endif + } +} + +#ifndef QT_NO_TEXTCUSTOMITEM +void TQTextStringChar::setCustomItem( TQTextCustomItem *i ) +{ + if ( type == Regular ) { + TQTextFormat *f = format(); + d.custom = new CustomData; + d.custom->format = f; + } else { + delete d.custom->custom; + } + d.custom->custom = i; + type = (type == Anchor ? CustomAnchor : Custom); +} + +void TQTextStringChar::loseCustomItem() +{ + if ( type == Custom ) { + TQTextFormat *f = d.custom->format; + d.custom->custom = 0; + delete d.custom; + type = Regular; + d.format = f; + } else if ( type == CustomAnchor ) { + d.custom->custom = 0; + type = Anchor; + } +} + +#endif + +TQString TQTextStringChar::anchorName() const +{ + if ( type == Regular ) + return TQString::null; + else + return d.custom->anchorName; +} + +TQString TQTextStringChar::anchorHref() const +{ + if ( type == Regular ) + return TQString::null; + else + return d.custom->anchorHref; +} + +void TQTextStringChar::setAnchor( const TQString& name, const TQString& href ) +{ + if ( type == Regular ) { + TQTextFormat *f = format(); + d.custom = new CustomData; +#ifndef QT_NO_TEXTCUSTOMITEM + d.custom->custom = 0; +#endif + d.custom->format = f; + type = Anchor; + } else if ( type == Custom ) { + type = CustomAnchor; + } + d.custom->anchorName = name; + d.custom->anchorHref = href; +} + + +int TQTextString::width( int idx ) const +{ + int w = 0; + TQTextStringChar *c = &at( idx ); + if ( !c->charStop || c->c.unicode() == 0xad || c->c.unicode() == 0x2028 ) + return 0; +#ifndef QT_NO_TEXTCUSTOMITEM + if( c->isCustom() ) { + if( c->customItem()->placement() == TQTextCustomItem::PlaceInline ) + w = c->customItem()->width; + } else +#endif + { + int r = c->c.row(); + if(r < 0x06 +#ifndef Q_WS_WIN + // Uniscribe's handling of Asian makes the condition below fail. + || (r > 0x1f && !(r > 0xd7 && r < 0xe0)) +#endif + ) { + w = c->format()->width( c->c ); + } else { + w = c->format()->width(toString(), idx); + } + } + return w; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextParagraph::TQTextParagraph( TQTextDocument *d, TQTextParagraph *pr, TQTextParagraph *nx, bool updateIds ) + : p( pr ), n( nx ), docOrPseudo( d ), + changed(FALSE), firstFormat(TRUE), firstPProcess(TRUE), needPreProcess(FALSE), fullWidth(TRUE), + lastInFrame(FALSE), visible(TRUE), breakable(TRUE), movedDown(FALSE), + mightHaveCustomItems(FALSE), hasdoc( d != 0 ), litem(FALSE), rtext(FALSE), + align( 0 ), lstyle( TQStyleSheetItem::ListDisc ), invalid( 0 ), mSelections( 0 ), +#ifndef QT_NO_TEXTCUSTOMITEM + mFloatingItems( 0 ), +#endif + utm( 0 ), ubm( 0 ), ulm( 0 ), urm( 0 ), uflm( 0 ), ulinespacing( 0 ), + tabStopWidth(0), minwidth(0), tArray(0), eData( 0 ), ldepth( 0 ) +{ + lstyle = TQStyleSheetItem::ListDisc; + if ( !hasdoc ) + docOrPseudo = new TQTextParagraphPseudoDocument; + bgcol = 0; + list_val = -1; + paintdevice = 0; + TQTextFormat* defFormat = formatCollection()->defaultFormat(); + if ( !hasdoc ) { + tabStopWidth = defFormat->width( 'x' ) * 8; + pseudoDocument()->commandHistory = new TQTextCommandHistory( 100 ); + } + + if ( p ) + p->n = this; + if ( n ) + n->p = this; + + if ( !p && hasdoc ) + document()->setFirstParagraph( this ); + if ( !n && hasdoc ) + document()->setLastParagraph( this ); + + state = -1; + + if ( p ) + id = p->id + 1; + else + id = 0; + if ( n && updateIds ) { + TQTextParagraph *s = n; + while ( s ) { + s->id = s->p->id + 1; + s->invalidateStyleCache(); + s = s->n; + } + } + + str = new TQTextString(); + TQChar ch(' '); + str->insert( 0, &ch, 1, formatCollection()->defaultFormat() ); +} + +TQTextParagraph::~TQTextParagraph() +{ + delete str; + if ( hasdoc ) { + register TQTextDocument *doc = document(); + if ( this == doc->minwParag ) { + doc->minwParag = 0; + doc->minw = 0; + } + if ( this == doc->curParag ) + doc->curParag = 0; + } else { + delete pseudoDocument(); + } + delete [] tArray; + delete eData; + TQMap::Iterator it = lineStarts.begin(); + for ( ; it != lineStarts.end(); ++it ) + delete *it; + if ( mSelections ) + delete mSelections; +#ifndef QT_NO_TEXTCUSTOMITEM + if ( mFloatingItems ) + delete mFloatingItems; +#endif + if ( p ) + p->setNext( n ); + if ( n ) + n->setPrev( p ); + delete bgcol; +} + +void TQTextParagraph::setNext( TQTextParagraph *s ) +{ + n = s; + if ( !n && hasdoc ) + document()->setLastParagraph( this ); +} + +void TQTextParagraph::setPrev( TQTextParagraph *s ) +{ + p = s; + if ( !p && hasdoc ) + document()->setFirstParagraph( this ); +} + +void TQTextParagraph::invalidate( int chr ) +{ + if ( invalid < 0 ) + invalid = chr; + else + invalid = TQMIN( invalid, chr ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( mFloatingItems ) { + for ( TQTextCustomItem *i = mFloatingItems->first(); i; i = mFloatingItems->next() ) + i->ypos = -1; + } +#endif + invalidateStyleCache(); +} + +void TQTextParagraph::invalidateStyleCache() +{ + if ( list_val < 0 ) + list_val = -1; +} + + +void TQTextParagraph::insert( int index, const TQString &s ) +{ + insert( index, s.unicode(), s.length() ); +} + +void TQTextParagraph::insert( int index, const TQChar *unicode, int len ) +{ + if ( hasdoc && !document()->useFormatCollection() && document()->preProcessor() ) + str->insert( index, unicode, len, + document()->preProcessor()->format( TQTextPreProcessor::Standard ) ); + else + str->insert( index, unicode, len, formatCollection()->defaultFormat() ); + invalidate( index ); + needPreProcess = TRUE; +} + +void TQTextParagraph::truncate( int index ) +{ + str->truncate( index ); + insert( length(), " " ); + needPreProcess = TRUE; +} + +void TQTextParagraph::remove( int index, int len ) +{ + if ( index + len - str->length() > 0 ) + return; +#ifndef QT_NO_TEXTCUSTOMITEM + for ( int i = index; i < index + len; ++i ) { + TQTextStringChar *c = at( i ); + if ( hasdoc && c->isCustom() ) { + document()->unregisterCustomItem( c->customItem(), this ); + } + } +#endif + str->remove( index, len ); + invalidate( 0 ); + needPreProcess = TRUE; +} + +void TQTextParagraph::join( TQTextParagraph *s ) +{ + int oh = r.height() + s->r.height(); + n = s->n; + if ( n ) + n->p = this; + else if ( hasdoc ) + document()->setLastParagraph( this ); + + int start = str->length(); + if ( length() > 0 && at( length() - 1 )->c == ' ' ) { + remove( length() - 1, 1 ); + --start; + } + append( s->str->toString(), TRUE ); + + for ( int i = 0; i < s->length(); ++i ) { + if ( !hasdoc || document()->useFormatCollection() ) { + s->str->at( i ).format()->addRef(); + str->setFormat( i + start, s->str->at( i ).format(), TRUE ); + } +#ifndef QT_NO_TEXTCUSTOMITEM + if ( s->str->at( i ).isCustom() ) { + TQTextCustomItem * item = s->str->at( i ).customItem(); + str->at( i + start ).setCustomItem( item ); + s->str->at( i ).loseCustomItem(); + if ( hasdoc ) { + document()->unregisterCustomItem( item, s ); + document()->registerCustomItem( item, this ); + } + } + if ( s->str->at( i ).isAnchor() ) { + str->at( i + start ).setAnchor( s->str->at( i ).anchorName(), + s->str->at( i ).anchorHref() ); + } +#endif + } + + if ( !extraData() && s->extraData() ) { + setExtraData( s->extraData() ); + s->setExtraData( 0 ); + } else if ( extraData() && s->extraData() ) { + extraData()->join( s->extraData() ); + } + delete s; + invalidate( 0 ); + r.setHeight( oh ); + needPreProcess = TRUE; + if ( n ) { + TQTextParagraph *s = n; + s->invalidate( 0 ); + while ( s ) { + s->id = s->p->id + 1; + s->state = -1; + s->needPreProcess = TRUE; + s->changed = TRUE; + s->invalidateStyleCache(); + s = s->n; + } + } + format(); + state = -1; +} + +void TQTextParagraph::move( int &dy ) +{ + if ( dy == 0 ) + return; + changed = TRUE; + r.moveBy( 0, dy ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( mFloatingItems ) { + for ( TQTextCustomItem *i = mFloatingItems->first(); i; i = mFloatingItems->next() ) + i->ypos += dy; + } +#endif + if ( p ) + p->lastInFrame = TRUE; + + // do page breaks if retquired + if ( hasdoc && document()->isPageBreakEnabled() ) { + int shift; + if ( ( shift = document()->formatter()->formatVertically( document(), this ) ) ) { + if ( p ) + p->setChanged( TRUE ); + dy += shift; + } + } +} + +void TQTextParagraph::format( int start, bool doMove ) +{ + if ( !str || str->length() == 0 || !formatter() ) + return; + + if ( hasdoc && + document()->preProcessor() && + ( needPreProcess || state == -1 ) ) + document()->preProcessor()->process( document(), this, invalid <= 0 ? 0 : invalid ); + needPreProcess = FALSE; + + if ( invalid == -1 ) + return; + + r.moveTopLeft( TQPoint( documentX(), p ? p->r.y() + p->r.height() : documentY() ) ); + if ( p ) + p->lastInFrame = FALSE; + + movedDown = FALSE; + bool formattedAgain = FALSE; + + formatAgain: + + r.setWidth( documentWidth() ); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( hasdoc && mFloatingItems ) { + for ( TQTextCustomItem *i = mFloatingItems->first(); i; i = mFloatingItems->next() ) { + i->ypos = r.y(); + if ( i->placement() == TQTextCustomItem::PlaceRight ) { + i->xpos = r.x() + r.width() - i->width; + } + } + } +#endif + TQMap oldLineStarts = lineStarts; + lineStarts.clear(); + int y = formatter()->format( document(), this, start, oldLineStarts ); + + + r.setWidth( TQMAX( r.width(), formatter()->minimumWidth() ) ); + + + TQMap::Iterator it = oldLineStarts.begin(); + + for ( ; it != oldLineStarts.end(); ++it ) + delete *it; + + if ( !hasdoc ) { // qt_format_text bounding rect handling + it = lineStarts.begin(); + int usedw = 0; + for ( ; it != lineStarts.end(); ++it ) + usedw = TQMAX( usedw, (*it)->w ); + if ( r.width() <= 0 ) { + // if the user specifies an invalid rect, this means that the + // bounding box should grow to the width that the text actually + // needs + r.setWidth( usedw ); + } else { + r.setWidth( TQMIN( usedw, r.width() ) ); + } + } + + if ( y != r.height() ) + r.setHeight( y ); + + if ( !visible ) { + r.setHeight( 0 ); + } else { + int minw = minwidth = formatter()->minimumWidth(); + int wused = formatter()->widthUsed(); + wused = TQMAX( minw, wused ); + if ( hasdoc ) { + document()->setMinimumWidth( minw, wused, this ); + } else { + pseudoDocument()->minw = TQMAX( pseudoDocument()->minw, minw ); + pseudoDocument()->wused = TQMAX( pseudoDocument()->wused, wused ); + } + } + + // do page breaks if retquired + if ( hasdoc && document()->isPageBreakEnabled() ) { + int shift = document()->formatter()->formatVertically( document(), this ); + if ( shift && !formattedAgain ) { + formattedAgain = TRUE; + goto formatAgain; + } + } + + if ( n && doMove && n->invalid == -1 && r.y() + r.height() != n->r.y() ) { + int dy = ( r.y() + r.height() ) - n->r.y(); + TQTextParagraph *s = n; + bool makeInvalid = p && p->lastInFrame; + while ( s && dy ) { + if ( !s->isFullWidth() ) + makeInvalid = TRUE; + if ( makeInvalid ) + s->invalidate( 0 ); + s->move( dy ); + if ( s->lastInFrame ) + makeInvalid = TRUE; + s = s->n; + } + } + + firstFormat = FALSE; + changed = TRUE; + invalid = -1; + //##### string()->setTextChanged( FALSE ); +} + +int TQTextParagraph::lineHeightOfChar( int i, int *bl, int *y ) const +{ + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + TQMap::ConstIterator it = lineStarts.end(); + --it; + for ( ;; ) { + if ( i >= it.key() ) { + if ( bl ) + *bl = ( *it )->baseLine; + if ( y ) + *y = ( *it )->y; + return ( *it )->h; + } + if ( it == lineStarts.begin() ) + break; + --it; + } + + qWarning( "TQTextParagraph::lineHeightOfChar: couldn't find lh for %d", i ); + return 15; +} + +TQTextStringChar *TQTextParagraph::lineStartOfChar( int i, int *index, int *line ) const +{ + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + int l = (int)lineStarts.count() - 1; + TQMap::ConstIterator it = lineStarts.end(); + --it; + for ( ;; ) { + if ( i >= it.key() ) { + if ( index ) + *index = it.key(); + if ( line ) + *line = l; + return &str->at( it.key() ); + } + if ( it == lineStarts.begin() ) + break; + --it; + --l; + } + + qWarning( "TQTextParagraph::lineStartOfChar: couldn't find %d", i ); + return 0; +} + +int TQTextParagraph::lines() const +{ + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + return (int)lineStarts.count(); +} + +TQTextStringChar *TQTextParagraph::lineStartOfLine( int line, int *index ) const +{ + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + if ( line >= 0 && line < (int)lineStarts.count() ) { + TQMap::ConstIterator it = lineStarts.begin(); + while ( line-- > 0 ) + ++it; + int i = it.key(); + if ( index ) + *index = i; + return &str->at( i ); + } + + qWarning( "TQTextParagraph::lineStartOfLine: couldn't find %d", line ); + return 0; +} + +int TQTextParagraph::leftGap() const +{ + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + if ( str->length() == 0) + return 0; + + int line = 0; + int x = str->length() ? str->at(0).x : 0; /* set x to x of first char */ + if ( str->isBidi() ) { + for ( int i = 1; i < str->length()-1; ++i ) + x = TQMIN(x, str->at(i).x); + return x; + } + + TQMap::ConstIterator it = lineStarts.begin(); + while (line < (int)lineStarts.count()) { + int i = it.key(); /* char index */ + x = TQMIN(x, str->at(i).x); + ++it; + ++line; + } + return x; +} + +void TQTextParagraph::setFormat( int index, int len, TQTextFormat *f, bool useCollection, int flags ) +{ + if ( !f ) + return; + if ( index < 0 ) + index = 0; + if ( index > str->length() - 1 ) + index = str->length() - 1; + if ( index + len >= str->length() ) + len = str->length() - index; + + TQTextFormatCollection *fc = 0; + if ( useCollection ) + fc = formatCollection(); + TQTextFormat *of; + for ( int i = 0; i < len; ++i ) { + of = str->at( i + index ).format(); + if ( !changed && ( !of || f->key() != of->key() ) ) + changed = TRUE; + if ( invalid == -1 && + ( f->font().family() != of->font().family() || + f->font().pointSize() != of->font().pointSize() || + f->font().weight() != of->font().weight() || + f->font().italic() != of->font().italic() || + f->vAlign() != of->vAlign() ) ) { + invalidate( 0 ); + } + if ( flags == -1 || flags == TQTextFormat::Format || !fc ) { + if ( fc ) + f = fc->format( f ); + str->setFormat( i + index, f, useCollection ); + } else { + TQTextFormat *fm = fc->format( of, f, flags ); + str->setFormat( i + index, fm, useCollection ); + } + } +} + +void TQTextParagraph::indent( int *oldIndent, int *newIndent ) +{ + if ( !hasdoc || !document()->indent() || isListItem() ) { + if ( oldIndent ) + *oldIndent = 0; + if ( newIndent ) + *newIndent = 0; + if ( oldIndent && newIndent ) + *newIndent = *oldIndent; + return; + } + document()->indent()->indent( document(), this, oldIndent, newIndent ); +} + +void TQTextParagraph::paint( TQPainter &painter, const TQColorGroup &cg, TQTextCursor *cursor, bool drawSelections, + int clipx, int clipy, int clipw, int cliph ) +{ + if ( !visible ) + return; + int i, y, h, baseLine, xstart, xend = 0; + i = y =h = baseLine = 0; + TQRect cursorRect; + drawSelections &= ( mSelections != 0 ); + // macintosh full-width selection style + bool fullWidthStyle = TQApplication::style().styleHint(TQStyle::SH_RichText_FullWidthSelection); + int fullSelectionWidth = 0; + if ( drawSelections && fullWidthStyle ) + fullSelectionWidth = (hasdoc ? document()->width() : r.width()); + + TQString qstr = str->toString(); + // detach string + qstr.setLength(qstr.length()); + // ### workaround so that \n are not drawn, actually this should + // be fixed in TQFont somewhere (under Windows you get ugly boxes + // otherwise) + TQChar* uc = (TQChar*) qstr.unicode(); + for ( uint ii = 0; ii < qstr.length(); ii++ ) + if ( uc[(int)ii]== '\n' || uc[(int)ii] == '\t' ) + uc[(int)ii] = 0x20; + + int line = -1; + int paintStart = 0; + TQTextStringChar *chr = 0; + TQTextStringChar *nextchr = at( 0 ); + for ( i = 0; i < length(); i++ ) { + chr = nextchr; + if ( i < length()-1 ) + nextchr = at( i+1 ); + + // we flush at end of document + bool flush = (i == length()-1); + bool ignoreSoftHyphen = FALSE; + if ( !flush ) { + // we flush at end of line + flush |= nextchr->lineStart; + // we flush on format changes + flush |= ( nextchr->format() != chr->format() ); + // we flush on link changes + flush |= ( nextchr->isLink() != chr->isLink() ); + // we flush on start of run + flush |= ( nextchr->bidiLevel != chr->bidiLevel ); + // we flush on bidi changes + flush |= ( nextchr->rightToLeft != chr->rightToLeft ); + // we flush before and after tabs + flush |= ( chr->c == '\t' || nextchr->c == '\t' ); + // we flush on soft hypens + if (chr->c.unicode() == 0xad) { + flush = TRUE; + if (!nextchr->lineStart) + ignoreSoftHyphen = TRUE; + } + // we flush on custom items + flush |= chr->isCustom(); + // we flush before custom items + flush |= nextchr->isCustom(); + // when painting justified, we flush on spaces + if ((alignment() & TQt::AlignJustify) == TQt::AlignJustify ) + flush |= chr->whiteSpace; + } + + // init a new line + if ( chr->lineStart ) { + ++line; + paintStart = i; + lineInfo( line, y, h, baseLine ); + if ( clipy != -1 && cliph != 0 && y + r.y() - h > clipy + cliph ) { // outside clip area, leave + break; + } + + // if this is the first line and we are a list item, draw the the bullet label + if ( line == 0 && isListItem() ) { + int x = chr->x; + if (str->isBidi()) { + if (str->isRightToLeft()) { + x = chr->x + str->width(0); + for (int k = 1; k < length(); ++k) { + if (str->at(k).lineStart) + break; + x = TQMAX(x, str->at(k).x + str->width(k)); + } + } else { + x = chr->x; + for (int k = 1; k < length(); ++k) { + if (str->at(k).lineStart) + break; + x = TQMIN(x, str->at(k).x); + } + } + } + drawLabel( &painter, x, y, 0, 0, baseLine, cg ); + } + } + + // check for cursor mark + if ( cursor && this == cursor->paragraph() && i == cursor->index() ) { + TQTextStringChar *c = i == 0 ? chr : chr - 1; + cursorRect.setRect( cursor->x() , y + baseLine - c->format()->ascent(), + 1, c->format()->height() ); + } + + if ( flush ) { // something changed, draw what we have so far + if ( chr->rightToLeft ) { + xstart = chr->x; + xend = at( paintStart )->x + str->width( paintStart ); + } else { + xstart = at( paintStart )->x; + xend = chr->x; + if ( i < length() - 1 ) { + if ( !str->at( i + 1 ).lineStart && + str->at( i + 1 ).rightToLeft == chr->rightToLeft ) + xend = str->at( i + 1 ).x; + else + xend += str->width( i ); + } + } + + if ( (clipx == -1 || clipw <= 0 || (xend >= clipx && xstart <= clipx + clipw)) && + ( clipy == -1 || clipy < y+r.y()+h ) ) { + if ( !chr->isCustom() ) + drawString( painter, qstr, paintStart, i - paintStart + (ignoreSoftHyphen ? 0 : 1), xstart, y, + baseLine, xend-xstart, h, drawSelections, fullSelectionWidth, + chr, cg, chr->rightToLeft ); +#ifndef QT_NO_TEXTCUSTOMITEM + else if ( chr->customItem()->placement() == TQTextCustomItem::PlaceInline ) { + bool inSelection = FALSE; + if (drawSelections) { + TQMap::ConstIterator it = mSelections->find( TQTextDocument::Standard ); + inSelection = (it != mSelections->end() && (*it).start <= i && (*it).end > i); + } + chr->customItem()->draw( &painter, chr->x, y, + clipx == -1 ? clipx : (clipx - r.x()), + clipy == -1 ? clipy : (clipy - r.y()), + clipw, cliph, cg, inSelection ); + } +#endif + } + paintStart = i+1; + } + + } + + // time to draw the cursor + const int cursor_extent = 4; + if ( !cursorRect.isNull() && cursor && + ((clipx == -1 || clipw == -1) || (cursorRect.right()+cursor_extent >= clipx && cursorRect.left()-cursor_extent <= clipx + clipw)) ) { + painter.fillRect( cursorRect, cg.color( TQColorGroup::Text ) ); + painter.save(); + if ( string()->isBidi() ) { + if ( at( cursor->index() )->rightToLeft ) { + painter.setPen( TQt::black ); + painter.drawLine( cursorRect.x(), cursorRect.y(), cursorRect.x() - cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + painter.drawLine( cursorRect.x(), cursorRect.y() + cursor_extent, cursorRect.x() - cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + } else { + painter.setPen( TQt::black ); + painter.drawLine( cursorRect.x(), cursorRect.y(), cursorRect.x() + cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + painter.drawLine( cursorRect.x(), cursorRect.y() + cursor_extent, cursorRect.x() + cursor_extent / 2, cursorRect.y() + cursor_extent / 2 ); + } + } + painter.restore(); + } +} + +//#define BIDI_DEBUG + +void TQTextParagraph::setColorForSelection( TQColor &color, TQPainter &painter, + const TQColorGroup& cg, int selection ) +{ + if (selection < 0) + return; + color = ( hasdoc && selection != TQTextDocument::Standard ) ? + document()->selectionColor( selection ) : + cg.color( TQColorGroup::Highlight ); + if ( selection == TQTextDocument::IMCompositionText ) { +#ifndef Q_WS_MACX + int h1, s1, v1, h2, s2, v2; + cg.color( TQColorGroup::Base ).hsv( &h1, &s1, &v1 ); + cg.color( TQColorGroup::Background ).hsv( &h2, &s2, &v2 ); + color.setHsv( h1, s1, ( v1 + v2 ) / 2 ); +#else + color = TQt::lightGray; +#endif + painter.setPen( cg.color( TQColorGroup::Text ) ); + } else if ( selection == TQTextDocument::IMSelectionText ) { + color = cg.color( TQColorGroup::Dark ); + painter.setPen( cg.color( TQColorGroup::BrightText ) ); + } else if ( !hasdoc || document()->invertSelectionText( selection ) ) { + painter.setPen( cg.color( TQColorGroup::HighlightedText ) ); + } +} + +void TQTextParagraph::drawString( TQPainter &painter, const TQString &str, int start, int len, int xstart, + int y, int baseLine, int w, int h, bool drawSelections, int fullSelectionWidth, + TQTextStringChar *formatChar, const TQColorGroup& cg, + bool rightToLeft ) +{ + bool plainText = hasdoc ? document()->textFormat() == TQt::PlainText : FALSE; + TQTextFormat* format = formatChar->format(); + + if ( !plainText || hasdoc && format->color() != document()->formatCollection()->defaultFormat()->color() ) + painter.setPen( TQPen( format->color() ) ); + else + painter.setPen( cg.text() ); + painter.setFont( format->font() ); + + if ( hasdoc && formatChar->isAnchor() && !formatChar->anchorHref().isEmpty() ) { + if ( format->useLinkColor() ) + painter.setPen(document()->linkColor.isValid() ? document()->linkColor : cg.link()); + if ( document()->underlineLinks() ) { + TQFont fn = format->font(); + fn.setUnderline( TRUE ); + painter.setFont( fn ); + } + } + + TQPainter::TextDirection dir = rightToLeft ? TQPainter::RTL : TQPainter::LTR; + + int real_length = len; + if (len && dir != TQPainter::RTL && start + len == length() ) // don't draw the last character (trailing space) + len--; + if (len && str.unicode()[start+len-1] == TQChar_linesep) + len--; + + + TQTextFormat::VerticalAlignment vAlign = format->vAlign(); + if ( vAlign != TQTextFormat::AlignNormal ) { + // sub or superscript + TQFont f( painter.font() ); + if ( format->fontSizesInPixels() ) + f.setPixelSize( ( f.pixelSize() * 2 ) / 3 ); + else + f.setPointSize( ( f.pointSize() * 2 ) / 3 ); + painter.setFont( f ); + int h = painter.fontMetrics().height(); + baseLine += (vAlign == TQTextFormat::AlignSubScript) ? h/6 : -h/2; + } + + bool allSelected = FALSE; + if (drawSelections) { + TQMap::ConstIterator it = mSelections->find( TQTextDocument::Standard ); + allSelected = (it != mSelections->end() && (*it).start <= start && (*it).end >= start+len); + } + if (!allSelected) + painter.drawText(xstart, y + baseLine, str, start, len, dir); + +#ifdef BIDI_DEBUG + painter.save(); + painter.setPen ( TQt::red ); + painter.drawLine( xstart, y, xstart, y + baseLine ); + painter.drawLine( xstart, y + baseLine/2, xstart + 10, y + baseLine/2 ); + int w = 0; + int i = 0; + while( i < len ) + w += painter.fontMetrics().charWidth( str, start + i++ ); + painter.setPen ( TQt::blue ); + painter.drawLine( xstart + w - 1, y, xstart + w - 1, y + baseLine ); + painter.drawLine( xstart + w - 1, y + baseLine/2, xstart + w - 1 - 10, y + baseLine/2 ); + painter.restore(); +#endif + + // check if we are in a selection and draw it + if (drawSelections) { + TQMap::ConstIterator it = mSelections->end(); + while ( it != mSelections->begin() ) { + --it; + int selStart = (*it).start; + int selEnd = (*it).end; + int tmpw = w; + + selStart = TQMAX(selStart, start); + int real_selEnd = TQMIN(selEnd, start+real_length); + selEnd = TQMIN(selEnd, start+len); + bool extendRight = FALSE; + bool extendLeft = FALSE; + bool selWrap = (real_selEnd == length()-1 && n && n->hasSelection(it.key())); + if (selWrap || this->str->at(real_selEnd).lineStart) { + extendRight = (fullSelectionWidth != 0); + if (!extendRight && !rightToLeft) + tmpw += painter.fontMetrics().width(' '); + } + if (fullSelectionWidth && (selStart == 0 || this->str->at(selStart).lineStart)) { + extendLeft = TRUE; + } + if (this->str->isRightToLeft() != rightToLeft) + extendLeft = extendRight = FALSE; + + if (this->str->isRightToLeft()) { + bool tmp = extendLeft; + extendLeft = extendRight; + extendRight = tmp; + } + + if (selStart < real_selEnd || + selWrap && fullSelectionWidth && extendRight && + // don't draw the standard selection on a printer= + (it.key() != TQTextDocument::Standard || !is_printer( &painter))) { + int selection = it.key(); + TQColor color; + setColorForSelection( color, painter, cg, selection ); + if (selStart != start || selEnd != start + len || selWrap) { + // have to clip + painter.save(); + int cs, ce; + if (rightToLeft) { + cs = (selEnd != start + len) ? + this->str->at(this->str->previousCursorPosition(selEnd)).x : xstart; + ce = (selStart != start) ? + this->str->at(this->str->previousCursorPosition(selStart)).x : xstart+tmpw; + } else { + cs = (selStart != start) ? this->str->at(selStart).x : xstart; + ce = (selEnd != start + len) ? this->str->at(selEnd).x : xstart+tmpw; + } + TQRect r(cs, y, ce-cs, h); + if (extendLeft) + r.setLeft(0); + if (extendRight) + r.setRight(fullSelectionWidth); + TQRegion reg(r); + if ( painter.hasClipping() ) + reg &= painter.clipRegion(TQPainter::CoordPainter); + painter.setClipRegion(reg, TQPainter::CoordPainter); + } + int xleft = xstart; + if ( extendLeft ) { + tmpw += xstart; + xleft = 0; + } + if ( extendRight ) + tmpw = fullSelectionWidth - xleft; + painter.fillRect( xleft, y, tmpw, h, color ); + painter.drawText( xstart, y + baseLine, str, start, len, dir ); + // draw preedit's underline + if (selection == TQTextDocument::IMCompositionText) + painter.drawLine(xstart, y + baseLine + 1, xstart + w, y + baseLine + 1); + if (selStart != start || selEnd != start + len || selWrap) + painter.restore(); + } + } + } + + if ( format->isMisspelled() ) { + painter.save(); + painter.setPen( TQPen( TQt::red, 1, TQt::DotLine ) ); + painter.drawLine( xstart, y + baseLine + 1, xstart + w, y + baseLine + 1 ); + painter.restore(); + } + + if ( hasdoc && formatChar->isAnchor() && !formatChar->anchorHref().isEmpty() && + document()->focusIndicator.parag == this && + ( document()->focusIndicator.start >= start && + document()->focusIndicator.start + document()->focusIndicator.len <= start + len || + document()->focusIndicator.start <= start && + document()->focusIndicator.start + document()->focusIndicator.len >= start + len ) ) + painter.drawWinFocusRect( TQRect( xstart, y, w, h ) ); +} + +void TQTextParagraph::drawLabel( TQPainter* p, int x, int y, int w, int h, int base, const TQColorGroup& cg ) +{ + TQRect r ( x, y, w, h ); + TQStyleSheetItem::ListStyle s = listStyle(); + + p->save(); + TQTextFormat *format = at( 0 )->format(); + if ( format ) { + p->setPen( format->color() ); + p->setFont( format->font() ); + } + TQFontMetrics fm( p->fontMetrics() ); + int size = fm.lineSpacing() / 3; + + bool rtl = str->isRightToLeft(); + + switch ( s ) { + case TQStyleSheetItem::ListDecimal: + case TQStyleSheetItem::ListLowerAlpha: + case TQStyleSheetItem::ListUpperAlpha: + { + if ( list_val == -1 ) { // uninitialised list value, calcluate the right one + int depth = listDepth(); + list_val--; + // ### evil, square and expensive. This needs to be done when formatting, not when painting + TQTextParagraph* s = prev(); + int depth_s; + while ( s && (depth_s = s->listDepth()) >= depth ) { + if ( depth_s == depth && s->isListItem() ) + list_val--; + s = s->prev(); + } + } + + int n = list_val; + if ( n < -1 ) + n = -n - 1; + TQString l; + switch ( s ) { + case TQStyleSheetItem::ListLowerAlpha: + if ( n < 27 ) { + l = TQChar( ('a' + (char) (n-1))); + break; + } + case TQStyleSheetItem::ListUpperAlpha: + if ( n < 27 ) { + l = TQChar( ('A' + (char) (n-1))); + break; + } + break; + default: //TQStyleSheetItem::ListDecimal: + l.setNum( n ); + break; + } + if (rtl) + l.prepend(" ."); + else + l += TQString::fromLatin1(". "); + int x = ( rtl ? r.left() : r.right() - fm.width(l)); + p->drawText( x, r.top() + base, l ); + } + break; + case TQStyleSheetItem::ListSquare: + { + int x = rtl ? r.left() + size : r.right() - size*2; + TQRect er( x, r.top() + fm.height() / 2 - size / 2, size, size ); + p->fillRect( er , cg.brush( TQColorGroup::Text ) ); + } + break; + case TQStyleSheetItem::ListCircle: + { + int x = rtl ? r.left() + size : r.right() - size*2; + TQRect er( x, r.top() + fm.height() / 2 - size / 2, size, size); + p->drawEllipse( er ); + } + break; + case TQStyleSheetItem::ListDisc: + default: + { + p->setBrush( cg.brush( TQColorGroup::Text )); + int x = rtl ? r.left() + size : r.right() - size*2; + TQRect er( x, r.top() + fm.height() / 2 - size / 2, size, size); + p->drawEllipse( er ); + p->setBrush( TQt::NoBrush ); + } + break; + } + + p->restore(); +} + +#ifndef QT_NO_DATASTREAM +void TQTextParagraph::readStyleInformation( TQDataStream& stream ) +{ + int int_align, int_lstyle; + uchar uchar_litem, uchar_rtext, uchar_dir; + stream >> int_align >> int_lstyle >> utm >> ubm >> ulm >> urm >> uflm + >> ulinespacing >> ldepth >> uchar_litem >> uchar_rtext >> uchar_dir; + align = int_align; lstyle = (TQStyleSheetItem::ListStyle) int_lstyle; + litem = uchar_litem; rtext = uchar_rtext; str->setDirection( (TQChar::Direction)uchar_dir ); + TQTextParagraph* s = prev() ? prev() : this; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); + } +} + +void TQTextParagraph::writeStyleInformation( TQDataStream& stream ) const +{ + stream << (int) align << (int) lstyle << utm << ubm << ulm << urm << uflm << ulinespacing << ldepth << (uchar)litem << (uchar)rtext << (uchar)str->direction(); +} +#endif + + +void TQTextParagraph::setListItem( bool li ) +{ + if ( (bool)litem == li ) + return; + litem = li; + changed = TRUE; + TQTextParagraph* s = prev() ? prev() : this; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); + } +} + +void TQTextParagraph::setListDepth( int depth ) { + if ( !hasdoc || depth == ldepth ) + return; + ldepth = depth; + TQTextParagraph* s = prev() ? prev() : this; + while ( s ) { + s->invalidate( 0 ); + s = s->next(); + } +} + +int *TQTextParagraph::tabArray() const +{ + int *ta = tArray; + if ( !ta && hasdoc ) + ta = document()->tabArray(); + return ta; +} + +int TQTextParagraph::nextTab( int, int x ) +{ + int *ta = tArray; + if ( hasdoc ) { + if ( !ta ) + ta = document()->tabArray(); + tabStopWidth = document()->tabStopWidth(); + } + if ( ta ) { + int i = 0; + while ( ta[ i ] ) { + if ( ta[ i ] >= x ) + return tArray[ i ]; + ++i; + } + return tArray[ 0 ]; + } else { + int d; + if ( tabStopWidth != 0 ) + d = x / tabStopWidth; + else + return x; + return tabStopWidth * ( d + 1 ); + } +} + +void TQTextParagraph::adjustToPainter( TQPainter *p ) +{ +#ifndef QT_NO_TEXTCUSTOMITEM + for ( int i = 0; i < length(); ++i ) { + if ( at( i )->isCustom() ) + at( i )->customItem()->adjustToPainter( p ); + } +#endif +} + +TQTextFormatCollection *TQTextParagraph::formatCollection() const +{ + if ( hasdoc ) + return document()->formatCollection(); + TQTextFormatCollection* fc = &pseudoDocument()->collection; + if ( paintdevice != fc->paintDevice() ) + fc->setPaintDevice( paintdevice ); + return fc; +} + +TQString TQTextParagraph::richText() const +{ + TQString s; + TQTextStringChar *formatChar = 0; + TQString spaces; + bool doStart = richTextExportStart && richTextExportStart->paragraph() == this; + bool doEnd = richTextExportEnd && richTextExportEnd->paragraph() == this; + int i; + TQString lastAnchorName; + for ( i = 0; i < length()-1; ++i ) { + if ( doStart && i && richTextExportStart->index() == i ) + s += ""; + if ( doEnd && richTextExportEnd->index() == i ) + s += ""; + TQTextStringChar *c = &str->at( i ); + if ( c->isAnchor() && !c->anchorName().isEmpty() && c->anchorName() != lastAnchorName ) { + lastAnchorName = c->anchorName(); + if ( c->anchorName().contains( '#' ) ) { + TQStringList l = TQStringList::split( '#', c->anchorName() ); + for ( TQStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) + s += ""; + } else { + s += "anchorName() + "\">"; + } + } + if ( !formatChar ) { + s += c->format()->makeFormatChangeTags( formatCollection()->defaultFormat(), + 0, TQString::null, c->anchorHref() ); + formatChar = c; + } else if ( ( formatChar->format()->key() != c->format()->key() ) || + (c->anchorHref() != formatChar->anchorHref() ) ) { + s += c->format()->makeFormatChangeTags( formatCollection()->defaultFormat(), + formatChar->format() , formatChar->anchorHref(), c->anchorHref() ); + formatChar = c; + } + if ( c->c == '<' ) + s += "<"; + else if ( c->c == '>' ) + s += ">"; + else if ( c->c =='&' ) + s += "&"; + else if ( c->c =='\"' ) + s += """; +#ifndef QT_NO_TEXTCUSTOMITEM + else if ( c->isCustom() ) + s += c->customItem()->richText(); +#endif + else if ( c->c == '\n' || c->c == TQChar_linesep ) + s += "
"; // space on purpose for compatibility with Netscape, Lynx & Co. + else + s += c->c; + } + if ( doEnd && richTextExportEnd->index() == i ) + s += ""; + if ( formatChar ) + s += formatChar->format()->makeFormatEndTags( formatCollection()->defaultFormat(), formatChar->anchorHref() ); + return s; +} + +void TQTextParagraph::addCommand( TQTextCommand *cmd ) +{ + if ( !hasdoc ) + pseudoDocument()->commandHistory->addCommand( cmd ); + else + document()->commands()->addCommand( cmd ); +} + +TQTextCursor *TQTextParagraph::undo( TQTextCursor *c ) +{ + if ( !hasdoc ) + return pseudoDocument()->commandHistory->undo( c ); + return document()->commands()->undo( c ); +} + +TQTextCursor *TQTextParagraph::redo( TQTextCursor *c ) +{ + if ( !hasdoc ) + return pseudoDocument()->commandHistory->redo( c ); + return document()->commands()->redo( c ); +} + +int TQTextParagraph::topMargin() const +{ + int m = 0; + if ( rtext ) { + m = isListItem() ? (document()->li_tm/TQMAX(1,listDepth()*listDepth())) : + ( listDepth() ? 0 : document()->par_tm ); + if ( listDepth() == 1 &&( !prev() || prev()->listDepth() < listDepth() ) ) + m = TQMAX( m, document()->list_tm ); + } + m += utm; + return scale( m, TQTextFormat::painter() ); +} + +int TQTextParagraph::bottomMargin() const +{ + int m = 0; + if ( rtext ) { + m = isListItem() ? (document()->li_bm/TQMAX(1,listDepth()*listDepth())) : + ( listDepth() ? 0 : document()->par_bm ); + if ( listDepth() == 1 &&( !next() || next()->listDepth() < listDepth() ) ) + m = TQMAX( m, document()->list_bm ); + } + m += ubm; + return scale( m, TQTextFormat::painter() ); +} + +int TQTextParagraph::leftMargin() const +{ + int m = ulm; + if ( listDepth() && !string()->isRightToLeft() ) + m += listDepth() * document()->list_lm; + return scale( m, TQTextFormat::painter() ); +} + +int TQTextParagraph::firstLineMargin() const +{ + int m = uflm; + return scale( m, TQTextFormat::painter() ); +} + +int TQTextParagraph::rightMargin() const +{ + int m = urm; + if ( listDepth() && string()->isRightToLeft() ) + m += listDepth() * document()->list_lm; + return scale( m, TQTextFormat::painter() ); +} + +int TQTextParagraph::lineSpacing() const +{ + int l = ulinespacing; + l = scale( l, TQTextFormat::painter() ); + return l; +} + +void TQTextParagraph::copyParagData( TQTextParagraph *parag ) +{ + rtext = parag->rtext; + lstyle = parag->lstyle; + ldepth = parag->ldepth; + litem = parag->litem; + align = parag->align; + utm = parag->utm; + ubm = parag->ubm; + urm = parag->urm; + ulm = parag->ulm; + uflm = parag->uflm; + ulinespacing = parag->ulinespacing; + TQColor *c = parag->backgroundColor(); + if ( c ) + setBackgroundColor( *c ); + str->setDirection( parag->str->direction() ); +} + +void TQTextParagraph::show() +{ + if ( visible || !hasdoc ) + return; + visible = TRUE; +} + +void TQTextParagraph::hide() +{ + if ( !visible || !hasdoc ) + return; + visible = FALSE; +} + +void TQTextParagraph::setDirection( TQChar::Direction d ) +{ + if ( str && str->direction() != d ) { + str->setDirection( d ); + invalidate( 0 ); + } +} + +TQChar::Direction TQTextParagraph::direction() const +{ + return (str ? str->direction() : TQChar::DirON ); +} + +void TQTextParagraph::setChanged( bool b, bool recursive ) +{ + changed = b; + if ( recursive ) { + if ( document() && document()->parentParagraph() ) + document()->parentParagraph()->setChanged( b, recursive ); + } +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +TQTextPreProcessor::TQTextPreProcessor() +{ +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextFormatter::TQTextFormatter() + : thisminw(0), thiswused(0), wrapEnabled( TRUE ), wrapColumn( -1 ), biw( FALSE ) +{ +} + +TQTextLineStart *TQTextFormatter::formatLine( TQTextParagraph *parag, TQTextString *string, TQTextLineStart *line, + TQTextStringChar *startChar, TQTextStringChar *lastChar, int align, int space ) +{ + if ( lastChar < startChar ) + return new TQTextLineStart; +#ifndef QT_NO_COMPLEXTEXT + if( string->isBidi() ) + return bidiReorderLine( parag, string, line, startChar, lastChar, align, space ); +#endif + int start = (startChar - &string->at(0)); + int last = (lastChar - &string->at(0) ); + + // ignore white space at the end of the line. + TQTextStringChar *ch = lastChar; + while ( ch > startChar && ch->whiteSpace ) { + space += ch->format()->width( ' ' ); + --ch; + } + + if (space < 0) + space = 0; + + // do alignment Auto == Left in this case + if ( align & TQt::AlignHCenter || align & TQt::AlignRight ) { + if ( align & TQt::AlignHCenter ) + space /= 2; + for ( int j = start; j <= last; ++j ) + string->at( j ).x += space; + } else if ( align & TQt::AlignJustify ) { + int numSpaces = 0; + // End at "last-1", the last space ends up with a width of 0 + for ( int j = last-1; j >= start; --j ) { + // Start at last tab, if any. + TQTextStringChar &ch = string->at( j ); + if ( ch.c == '\t' ) { + start = j+1; + break; + } + if(ch.whiteSpace) + numSpaces++; + } + int toAdd = 0; + for ( int k = start + 1; k <= last; ++k ) { + TQTextStringChar &ch = string->at( k ); + if( numSpaces && ch.whiteSpace ) { + int s = space / numSpaces; + toAdd += s; + space -= s; + numSpaces--; + } + string->at( k ).x += toAdd; + } + } + + if ( last >= 0 && last < string->length() ) + line->w = string->at( last ).x + string->width( last ); + else + line->w = 0; + + return new TQTextLineStart; +} + +#ifndef QT_NO_COMPLEXTEXT + +#ifdef BIDI_DEBUG +#include +#endif + +// collects one line of the paragraph and transforms it to visual order +TQTextLineStart *TQTextFormatter::bidiReorderLine( TQTextParagraph * /*parag*/, TQTextString *text, TQTextLineStart *line, + TQTextStringChar *startChar, TQTextStringChar *lastChar, int align, int space ) +{ + // ignore white space at the end of the line. + int endSpaces = 0; + while ( lastChar > startChar && lastChar->whiteSpace ) { + space += lastChar->format()->width( ' ' ); + --lastChar; + ++endSpaces; + } + + int start = (startChar - &text->at(0)); + int last = (lastChar - &text->at(0) ); + + int length = lastChar - startChar + 1; + + + int x = startChar->x; + + unsigned char _levels[256]; + int _visual[256]; + + unsigned char *levels = _levels; + int *visual = _visual; + + if ( length > 255 ) { + levels = (unsigned char *)malloc( length*sizeof( unsigned char ) ); + visual = (int *)malloc( length*sizeof( int ) ); + } + + //qDebug("bidiReorderLine: length=%d (%d-%d)", length, start, last ); + + TQTextStringChar *ch = startChar; + unsigned char *l = levels; + while ( ch <= lastChar ) { + //qDebug( " level: %d", ch->bidiLevel ); + *(l++) = (ch++)->bidiLevel; + } + + TQTextEngine::bidiReorder( length, levels, visual ); + + // now construct the reordered string out of the runs... + + int numSpaces = 0; + // set the correct alignment. This is a bit messy.... + if( align == TQt::AlignAuto ) { + // align according to directionality of the paragraph... + if ( text->isRightToLeft() ) + align = TQt::AlignRight; + } + + // This is not really correct, but as we can't make the scrollbar move to the left of the origin, + // this ensures all text can be scrolled to and read. + if (space < 0) + space = 0; + + if ( align & TQt::AlignHCenter ) + x += space/2; + else if ( align & TQt::AlignRight ) + x += space; + else if ( align & TQt::AlignJustify ) { + // End at "last-1", the last space ends up with a width of 0 + for ( int j = last-1; j >= start; --j ) { + // Start at last tab, if any. + TQTextStringChar &ch = text->at( j ); + if ( ch.c == '\t' ) { + start = j+1; + break; + } + if(ch.whiteSpace) + numSpaces++; + } + } + + int toAdd = 0; + int xorig = x; + TQTextStringChar *lc = startChar + visual[0]; + for ( int i = 0; i < length; i++ ) { + TQTextStringChar *ch = startChar + visual[i]; + if (numSpaces && ch->whiteSpace) { + int s = space / numSpaces; + toAdd += s; + space -= s; + numSpaces--; + } + + if (lc->format() != ch->format() && !ch->c.isSpace() + && lc->format()->font().italic() && !ch->format()->font().italic()) { + int rb = lc->format()->fontMetrics().rightBearing(lc->c); + if (rb < 0) + x -= rb; + } + + ch->x = x + toAdd; + ch->rightToLeft = ch->bidiLevel % 2; + //qDebug("visual: %d (%x) placed at %d rightToLeft=%d", visual[i], ch->c.unicode(), x +toAdd, ch->rightToLeft ); + int ww = 0; + if ( ch->c.unicode() >= 32 || ch->c == '\t' || ch->c == '\n' || ch->isCustom() ) { + ww = text->width( start+visual[i] ); + } else { + ww = ch->format()->width( ' ' ); + } + x += ww; + lc = ch; + } + x += toAdd; + + while ( endSpaces-- ) { + ++lastChar; + int sw = lastChar->format()->width( ' ' ); + if ( text->isRightToLeft() ) { + xorig -= sw; + lastChar->x = xorig; + ch->rightToLeft = TRUE; + } else { + lastChar->x = x; + x += sw; + ch->rightToLeft = FALSE; + } + } + + line->w = x; + + if ( length > 255 ) { + free( levels ); + free( visual ); + } + + return new TQTextLineStart; +} +#endif + + +void TQTextFormatter::insertLineStart( TQTextParagraph *parag, int index, TQTextLineStart *ls ) +{ + TQMap::Iterator it; + if ( ( it = parag->lineStartList().find( index ) ) == parag->lineStartList().end() ) { + parag->lineStartList().insert( index, ls ); + } else { + delete *it; + parag->lineStartList().remove( it ); + parag->lineStartList().insert( index, ls ); + } +} + + +/* Standard pagebreak algorithm using TQTextFlow::adjustFlow. Returns + the shift of the paragraphs bottom line. + */ +int TQTextFormatter::formatVertically( TQTextDocument* doc, TQTextParagraph* parag ) +{ + int oldHeight = parag->rect().height(); + TQMap& lineStarts = parag->lineStartList(); + TQMap::Iterator it = lineStarts.begin(); + int h = parag->prev() ? TQMAX(parag->prev()->bottomMargin(),parag->topMargin() ) / 2: 0; + for ( ; it != lineStarts.end() ; ++it ) { + TQTextLineStart * ls = it.data(); + ls->y = h; + TQTextStringChar *c = ¶g->string()->at(it.key()); +#ifndef QT_NO_TEXTCUSTOMITEM + if ( c && c->customItem() && c->customItem()->ownLine() ) { + int h = c->customItem()->height; + c->customItem()->pageBreak( parag->rect().y() + ls->y + ls->baseLine - h, doc->flow() ); + int delta = c->customItem()->height - h; + ls->h += delta; + if ( delta ) + parag->setMovedDown( TRUE ); + } else +#endif + { + + int shift = doc->flow()->adjustFlow( parag->rect().y() + ls->y, ls->w, ls->h ); + ls->y += shift; + if ( shift ) + parag->setMovedDown( TRUE ); + } + h = ls->y + ls->h; + } + int m = parag->bottomMargin(); + if ( !parag->next() ) + m = 0; + else + m = TQMAX(m, parag->next()->topMargin() ) / 2; + h += m; + parag->setHeight( h ); + return h - oldHeight; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextFormatterBreakInWords::TQTextFormatterBreakInWords() +{ +} + +#define SPACE(s) s + +int TQTextFormatterBreakInWords::format( TQTextDocument *doc,TQTextParagraph *parag, + int start, const TQMap & ) +{ + // make sure bidi information is correct. + (void )parag->string()->isBidi(); + + TQTextStringChar *c = 0; + TQTextStringChar *firstChar = 0; + int left = doc ? parag->leftMargin() + doc->leftMargin() : 0; + int x = left + ( doc ? parag->firstLineMargin() : 0 ); + int dw = parag->documentVisibleWidth() - ( doc ? doc->rightMargin() : 0 ); + int y = parag->prev() ? TQMAX(parag->prev()->bottomMargin(),parag->topMargin()) / 2: 0; + int h = y; + int len = parag->length(); + if ( doc ) + x = doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), x, 4 ); + int rm = parag->rightMargin(); + int w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + bool fullWidth = TRUE; + int minw = 0; + int wused = 0; + bool wrapEnabled = isWrapEnabled( parag ); + + start = 0; //######### what is the point with start?! (Matthias) + if ( start == 0 ) + c = ¶g->string()->at( 0 ); + + int i = start; + TQTextLineStart *lineStart = new TQTextLineStart( y, y, 0 ); + insertLineStart( parag, 0, lineStart ); + + TQPainter *painter = TQTextFormat::painter(); + + int col = 0; + int ww = 0; + TQChar lastChr; + for ( ; i < len; ++i, ++col ) { + if ( c ) + lastChr = c->c; + c = ¶g->string()->at( i ); + // ### the lines below should not be needed + if ( painter ) + c->format()->setPainter( painter ); + if ( i > 0 ) { + c->lineStart = 0; + } else { + c->lineStart = 1; + firstChar = c; + } + if ( c->c.unicode() >= 32 || c->isCustom() ) { + ww = parag->string()->width( i ); + } else if ( c->c == '\t' ) { + int nx = parag->nextTab( i, x - left ) + left; + if ( nx < x ) + ww = w - x; + else + ww = nx - x; + } else { + ww = c->format()->width( ' ' ); + } + +#ifndef QT_NO_TEXTCUSTOMITEM + if ( c->isCustom() && c->customItem()->ownLine() ) { + x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + c->customItem()->resize( w - x ); + w = dw; + y += h; + h = c->height(); + lineStart = new TQTextLineStart( y, h, h ); + insertLineStart( parag, i, lineStart ); + c->lineStart = 1; + firstChar = c; + x = 0xffffff; + continue; + } +#endif + + if ( wrapEnabled && + ( wrapAtColumn() == -1 && x + ww > w || + wrapAtColumn() != -1 && col >= wrapAtColumn() ) ) { + x = doc ? parag->document()->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw; + y += h; + h = c->height(); + lineStart = formatLine( parag, parag->string(), lineStart, firstChar, c-1 ); + lineStart->y = y; + insertLineStart( parag, i, lineStart ); + lineStart->baseLine = c->ascent(); + lineStart->h = c->height(); + c->lineStart = 1; + firstChar = c; + col = 0; + if ( wrapAtColumn() != -1 ) + minw = TQMAX( minw, w ); + } else if ( lineStart ) { + lineStart->baseLine = TQMAX( lineStart->baseLine, c->ascent() ); + h = TQMAX( h, c->height() ); + lineStart->h = h; + } + + c->x = x; + x += ww; + wused = TQMAX( wused, x ); + } + + int m = parag->bottomMargin(); + if ( !parag->next() ) + m = 0; + else + m = TQMAX(m, parag->next()->topMargin() ) / 2; + parag->setFullWidth( fullWidth ); + y += h + m; + if ( doc ) + minw += doc->rightMargin(); + if ( !wrapEnabled ) + minw = TQMAX(minw, wused); + + thisminw = minw; + thiswused = wused; + return y; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextFormatterBreakWords::TQTextFormatterBreakWords() +{ +} + +#define DO_FLOW( lineStart ) do{ if ( doc && doc->isPageBreakEnabled() ) { \ + int yflow = lineStart->y + parag->rect().y();\ + int shift = doc->flow()->adjustFlow( yflow, dw, lineStart->h ); \ + lineStart->y += shift;\ + y += shift;\ + }}while(FALSE) + +int TQTextFormatterBreakWords::format( TQTextDocument *doc, TQTextParagraph *parag, + int start, const TQMap & ) +{ + // make sure bidi information is correct. + (void )parag->string()->isBidi(); + + TQTextStringChar *c = 0; + TQTextStringChar *firstChar = 0; + TQTextString *string = parag->string(); + int left = doc ? parag->leftMargin() + doc->leftMargin() : 0; + int x = left + ( doc ? parag->firstLineMargin() : 0 ); + int y = parag->prev() ? TQMAX(parag->prev()->bottomMargin(),parag->topMargin()) / 2: 0; + int h = y; + int len = parag->length(); + if ( doc ) + x = doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), x, 0 ); + int dw = parag->documentVisibleWidth() - ( doc ? ( left != x ? 0 : doc->rightMargin() ) : 0 ); + + int curLeft = x; + int rm = parag->rightMargin(); + int rdiff = doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 0 ) : 0; + int w = dw - rdiff; + bool fullWidth = TRUE; + int marg = left + rdiff; + int minw = 0; + int wused = 0; + int tminw = marg; + int linespacing = doc ? parag->lineSpacing() : 0; + bool wrapEnabled = isWrapEnabled( parag ); + + start = 0; + + int i = start; + TQTextLineStart *lineStart = new TQTextLineStart( y, y, 0 ); + insertLineStart( parag, 0, lineStart ); + int lastBreak = -1; + int tmpBaseLine = 0, tmph = 0; + bool lastWasNonInlineCustom = FALSE; + + int align = parag->alignment(); + if ( align == TQt::AlignAuto && doc && doc->alignment() != TQt::AlignAuto ) + align = doc->alignment(); + + align &= TQt::AlignHorizontal_Mask; + + // ### hack. The last char in the paragraph is always invisible, + // ### and somehow sometimes has a wrong format. It changes + // ### between // layouting and printing. This corrects some + // ### layouting errors in BiDi mode due to this. + if ( len > 1 ) { + c = ¶g->string()->at(len - 1); + if (!c->isAnchor()) { + if (c->format()) + c->format()->removeRef(); + c->setFormat( string->at( len - 2 ).format() ); + if (c->format()) + c->format()->addRef(); + } + } + + c = ¶g->string()->at( 0 ); + + TQPainter *painter = TQTextFormat::painter(); + int col = 0; + int ww = 0; + TQChar lastChr = c->c; + TQTextFormat *lastFormat = c->format(); + for ( ; i < len; ++i, ++col ) { + if ( i ) { + c = ¶g->string()->at(i-1); + lastChr = c->c; + lastFormat = c->format(); + } + bool lastWasOwnLineCustomItem = lastBreak == -2; + bool hadBreakableChar = lastBreak != -1; + bool lastWasHardBreak = lastChr == TQChar_linesep; + + // ### next line should not be needed + if ( painter ) + c->format()->setPainter( painter ); + c = &string->at( i ); + + if (lastFormat != c->format() && !c->c.isSpace() + && lastFormat->font().italic() && !c->format()->font().italic()) { + int rb = lastFormat->fontMetrics().rightBearing(lastChr); + if (rb < 0) + x -= rb; + } + + if ( i > 0 && (x > curLeft || ww == 0) || lastWasNonInlineCustom ) { + c->lineStart = 0; + } else { + c->lineStart = 1; + firstChar = c; + } + + // ignore non spacing marks for column count. + if (col != 0 && ::category(c->c) == TQChar::Mark_NonSpacing) + --col; + +#ifndef QT_NO_TEXTCUSTOMITEM + lastWasNonInlineCustom = ( c->isCustom() && c->customItem()->placement() != TQTextCustomItem::PlaceInline ); +#endif + + if ( c->c.unicode() >= 32 || c->isCustom() ) { + ww = string->width( i ); + } else if ( c->c == '\t' ) { + if ( align == TQt::AlignRight || align == TQt::AlignCenter ) { + // we can not (yet) do tabs + ww = c->format()->width(' ' ); + } else { + int tabx = lastWasHardBreak ? (left + ( doc ? parag->firstLineMargin() : 0 )) : x; + int nx = parag->nextTab( i, tabx - left ) + left; + if ( nx < tabx ) // strrrange... + ww = 0; + else + ww = nx - tabx; + } + } else { + ww = c->format()->width( ' ' ); + } + +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextCustomItem* ci = c->customItem(); + if ( c->isCustom() && ci->ownLine() ) { + TQTextLineStart *lineStart2 = formatLine( parag, string, lineStart, firstChar, c-1, align, SPACE(w - x - ww) ); + x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + ci->resize(w - x); + if ( ci->width < w - x ) { + if ( align & TQt::AlignHCenter ) + x = ( w - ci->width ) / 2; + else if ( align & TQt::AlignRight ) { + x = w - ci->width; + } + } + c->x = x; + curLeft = x; + if ( i == 0 || !isBreakable(string, i-1) || + string->at( i - 1 ).lineStart == 0 ) { + y += TQMAX( h, TQMAX( tmph, linespacing ) ); + tmph = c->height(); + h = tmph; + lineStart = lineStart2; + lineStart->y = y; + insertLineStart( parag, i, lineStart ); + c->lineStart = 1; + firstChar = c; + } else { + tmph = c->height(); + h = tmph; + delete lineStart2; + } + lineStart->h = h; + lineStart->baseLine = h; + tmpBaseLine = lineStart->baseLine; + lastBreak = -2; + x = w; + minw = TQMAX( minw, tminw ); + + int tw = ci->minimumWidth() + ( doc ? doc->leftMargin() : 0 ); + if ( tw < TQWIDGETSIZE_MAX ) + tminw = tw; + else + tminw = marg; + wused = TQMAX( wused, ci->width ); + continue; + } else if ( c->isCustom() && ci->placement() != TQTextCustomItem::PlaceInline ) { + int tw = ci->minimumWidth(); + if ( tw < TQWIDGETSIZE_MAX ) + minw = TQMAX( minw, tw ); + } +#endif + // we break if + // 1. the last character was a hard break (TQChar_linesep) or + // 2. the last charater was a own-line custom item (eg. table or ruler) or + // 3. wrapping was enabled, it was not a space and following + // condition is true: We either had a breakable character + // previously or we ar allowed to break in words and - either + // we break at w pixels and the current char would exceed that + // or - we break at a column and the current character would + // exceed that. + if ( lastWasHardBreak || lastWasOwnLineCustomItem || + ( wrapEnabled && + ( (!c->c.isSpace() && (hadBreakableChar || allowBreakInWords()) && + ( (wrapAtColumn() == -1 && x + ww > w) || + (wrapAtColumn() != -1 && col >= wrapAtColumn()) ) ) ) + ) + ) { + if ( wrapAtColumn() != -1 ) + minw = TQMAX( minw, x + ww ); + // if a break was forced (no breakable char, hard break or own line custom item), break immediately.... + if ( !hadBreakableChar || lastWasHardBreak || lastWasOwnLineCustomItem ) { + if ( lineStart ) { + lineStart->baseLine = TQMAX( lineStart->baseLine, tmpBaseLine ); + h = TQMAX( h, tmph ); + lineStart->h = h; + DO_FLOW( lineStart ); + } + lineStart = formatLine( parag, string, lineStart, firstChar, c-1, align, SPACE(w - x) ); + x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + if ( !doc && c->c == '\t' ) { // qt_format_text tab handling + int nx = parag->nextTab( i, x - left ) + left; + if ( nx < x ) + ww = w - x; + else + ww = nx - x; + } + curLeft = x; + y += TQMAX( h, linespacing ); + tmph = c->height(); + h = 0; + lineStart->y = y; + insertLineStart( parag, i, lineStart ); + lineStart->baseLine = c->ascent(); + lineStart->h = c->height(); + c->lineStart = 1; + firstChar = c; + tmpBaseLine = lineStart->baseLine; + lastBreak = -1; + col = 0; + if ( allowBreakInWords() || lastWasHardBreak ) { + minw = TQMAX(minw, tminw); + tminw = marg + ww; + } + } else { // ... otherwise if we had a breakable char, break there + DO_FLOW( lineStart ); + c->x = x; + i = lastBreak; + lineStart = formatLine( parag, string, lineStart, firstChar, parag->at( lastBreak ),align, SPACE(w - string->at( i+1 ).x) ); + x = doc ? doc->flow()->adjustLMargin( y + parag->rect().y(), parag->rect().height(), left, 4 ) : left; + w = dw - ( doc ? doc->flow()->adjustRMargin( y + parag->rect().y(), parag->rect().height(), rm, 4 ) : 0 ); + if ( !doc && c->c == '\t' ) { // qt_format_text tab handling + int nx = parag->nextTab( i, x - left ) + left; + if ( nx < x ) + ww = w - x; + else + ww = nx - x; + } + curLeft = x; + y += TQMAX( h, linespacing ); + tmph = c->height(); + h = tmph; + lineStart->y = y; + insertLineStart( parag, i + 1, lineStart ); + lineStart->baseLine = c->ascent(); + lineStart->h = c->height(); + c->lineStart = 1; + firstChar = c; + tmpBaseLine = lineStart->baseLine; + lastBreak = -1; + col = 0; + minw = TQMAX(minw, tminw); + tminw = marg; + continue; + } + } else if (lineStart && isBreakable(string, i)) { + if ( len <= 2 || i < len - 1 ) { + tmpBaseLine = TQMAX( tmpBaseLine, c->ascent() ); + tmph = TQMAX( tmph, c->height() ); + } + minw = TQMAX( minw, tminw ); + + tminw = marg + ww; + lineStart->baseLine = TQMAX( lineStart->baseLine, tmpBaseLine ); + h = TQMAX( h, tmph ); + lineStart->h = h; + if ( i < len - 2 || c->c != ' ' ) + lastBreak = i; + } else { + tminw += ww; + int cascent = c->ascent(); + int cheight = c->height(); + int belowBaseLine = TQMAX( tmph - tmpBaseLine, cheight-cascent ); + tmpBaseLine = TQMAX( tmpBaseLine, cascent ); + tmph = tmpBaseLine + belowBaseLine; + } + + c->x = x; + x += ww; + wused = TQMAX( wused, x ); + } + + if ( lineStart ) { + lineStart->baseLine = TQMAX( lineStart->baseLine, tmpBaseLine ); + h = TQMAX( h, tmph ); + lineStart->h = h; + // last line in a paragraph is not justified + if ( align == TQt::AlignJustify ) + align = TQt::AlignAuto; + DO_FLOW( lineStart ); + lineStart = formatLine( parag, string, lineStart, firstChar, c, align, SPACE(w - x) ); + delete lineStart; + } + + minw = TQMAX( minw, tminw ); + if ( doc ) + minw += doc->rightMargin(); + + int m = parag->bottomMargin(); + if ( !parag->next() ) + m = 0; + else + m = TQMAX(m, parag->next()->topMargin() ) / 2; + parag->setFullWidth( fullWidth ); + y += TQMAX( h, linespacing ) + m; + + wused += rm; + if ( !wrapEnabled || wrapAtColumn() != -1 ) + minw = TQMAX(minw, wused); + + // This is the case where we are breaking wherever we darn well please + // in cases like that, the minw should not be the length of the entire + // word, because we necessarily want to show the word on the whole line. + // example: word wrap in iconview + if ( allowBreakInWords() && minw > wused ) + minw = wused; + + thisminw = minw; + thiswused = wused; + return y; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextIndent::TQTextIndent() +{ +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +TQTextFormatCollection::TQTextFormatCollection() + : cKey( 307 ), paintdevice( 0 ) +{ + defFormat = new TQTextFormat( TQApplication::font(), + TQApplication::palette().color( TQPalette::Active, TQColorGroup::Text ) ); + lastFormat = cres = 0; + cflags = -1; + cKey.setAutoDelete( TRUE ); + cachedFormat = 0; +} + +TQTextFormatCollection::~TQTextFormatCollection() +{ + delete defFormat; +} + +void TQTextFormatCollection::setPaintDevice( TQPaintDevice *pd ) +{ + paintdevice = pd; + +#if defined(Q_WS_X11) + int scr = ( paintdevice ) ? paintdevice->x11Screen() : TQPaintDevice::x11AppScreen(); + + defFormat->fn.x11SetScreen( scr ); + defFormat->update(); + + TQDictIterator it( cKey ); + TQTextFormat *format; + while ( ( format = it.current() ) != 0 ) { + ++it; + format->fn.x11SetScreen( scr ); + format->update(); + } +#endif // Q_WS_X11 +} + +TQTextFormat *TQTextFormatCollection::format( TQTextFormat *f ) +{ + if ( f->parent() == this || f == defFormat ) { + lastFormat = f; + lastFormat->addRef(); + return lastFormat; + } + + if ( f == lastFormat || ( lastFormat && f->key() == lastFormat->key() ) ) { + lastFormat->addRef(); + return lastFormat; + } + + TQTextFormat *fm = cKey.find( f->key() ); + if ( fm ) { + lastFormat = fm; + lastFormat->addRef(); + return lastFormat; + } + + if ( f->key() == defFormat->key() ) + return defFormat; + + lastFormat = createFormat( *f ); + lastFormat->collection = this; + cKey.insert( lastFormat->key(), lastFormat ); + return lastFormat; +} + +TQTextFormat *TQTextFormatCollection::format( TQTextFormat *of, TQTextFormat *nf, int flags ) +{ + if ( cres && kof == of->key() && knf == nf->key() && cflags == flags ) { + cres->addRef(); + return cres; + } + + cres = createFormat( *of ); + kof = of->key(); + knf = nf->key(); + cflags = flags; + if ( flags & TQTextFormat::Bold ) + cres->fn.setBold( nf->fn.bold() ); + if ( flags & TQTextFormat::Italic ) + cres->fn.setItalic( nf->fn.italic() ); + if ( flags & TQTextFormat::Underline ) + cres->fn.setUnderline( nf->fn.underline() ); + if ( flags & TQTextFormat::StrikeOut ) + cres->fn.setStrikeOut( nf->fn.strikeOut() ); + if ( flags & TQTextFormat::Family ) + cres->fn.setFamily( nf->fn.family() ); + if ( flags & TQTextFormat::Size ) { + if ( of->usePixelSizes ) + cres->fn.setPixelSize( nf->fn.pixelSize() ); + else + cres->fn.setPointSize( nf->fn.pointSize() ); + } + if ( flags & TQTextFormat::Color ) + cres->col = nf->col; + if ( flags & TQTextFormat::Misspelled ) + cres->missp = nf->missp; + if ( flags & TQTextFormat::VAlign ) + cres->ha = nf->ha; + cres->update(); + + TQTextFormat *fm = cKey.find( cres->key() ); + if ( !fm ) { + cres->collection = this; + cKey.insert( cres->key(), cres ); + } else { + delete cres; + cres = fm; + cres->addRef(); + } + + return cres; +} + +TQTextFormat *TQTextFormatCollection::format( const TQFont &f, const TQColor &c ) +{ + if ( cachedFormat && cfont == f && ccol == c ) { + cachedFormat->addRef(); + return cachedFormat; + } + + TQString key = TQTextFormat::getKey( f, c, FALSE, TQTextFormat::AlignNormal ); + cachedFormat = cKey.find( key ); + cfont = f; + ccol = c; + + if ( cachedFormat ) { + cachedFormat->addRef(); + return cachedFormat; + } + + if ( key == defFormat->key() ) + return defFormat; + + cachedFormat = createFormat( f, c ); + cachedFormat->collection = this; + cKey.insert( cachedFormat->key(), cachedFormat ); + if ( cachedFormat->key() != key ) + qWarning("ASSERT: keys for format not identical: '%s '%s'", cachedFormat->key().latin1(), key.latin1() ); + return cachedFormat; +} + +void TQTextFormatCollection::remove( TQTextFormat *f ) +{ + if ( lastFormat == f ) + lastFormat = 0; + if ( cres == f ) + cres = 0; + if ( cachedFormat == f ) + cachedFormat = 0; + if (cKey.find(f->key()) == f) + cKey.remove( f->key() ); +} + +#define UPDATE( up, lo, rest ) \ + if ( font.lo##rest() != defFormat->fn.lo##rest() && fm->fn.lo##rest() == defFormat->fn.lo##rest() ) \ + fm->fn.set##up##rest( font.lo##rest() ) + +void TQTextFormatCollection::updateDefaultFormat( const TQFont &font, const TQColor &color, TQStyleSheet *sheet ) +{ + TQDictIterator it( cKey ); + TQTextFormat *fm; + bool usePixels = font.pointSize() == -1; + bool changeSize = usePixels ? font.pixelSize() != defFormat->fn.pixelSize() : + font.pointSize() != defFormat->fn.pointSize(); + int base = usePixels ? font.pixelSize() : font.pointSize(); + while ( ( fm = it.current() ) ) { + ++it; + UPDATE( F, f, amily ); + UPDATE( W, w, eight ); + UPDATE( B, b, old ); + UPDATE( I, i, talic ); + UPDATE( U, u, nderline ); + if ( changeSize ) { + fm->stdSize = base; + fm->usePixelSizes = usePixels; + if ( usePixels ) + fm->fn.setPixelSize( fm->stdSize ); + else + fm->fn.setPointSize( fm->stdSize ); + sheet->scaleFont( fm->fn, fm->logicalFontSize ); + } + if ( color.isValid() && color != defFormat->col && fm->col == defFormat->col ) + fm->col = color; + fm->update(); + } + + defFormat->fn = font; + defFormat->col = color; + defFormat->update(); + defFormat->stdSize = base; + defFormat->usePixelSizes = usePixels; + + updateKeys(); +} + +// the keys in cKey have changed, rebuild the hashtable +void TQTextFormatCollection::updateKeys() +{ + if ( cKey.isEmpty() ) + return; + cKey.setAutoDelete( FALSE ); + TQTextFormat** formats = new TQTextFormat*[ cKey.count() + 1 ]; + TQTextFormat **f = formats; + TQDictIterator it( cKey ); + while ( ( *f = it.current() ) ) { + ++it; + ++f; + } + cKey.clear(); + for ( f = formats; *f; f++ ) + cKey.insert( (*f)->key(), *f ); + cKey.setAutoDelete( TRUE ); + delete [] formats; +} + + + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +void TQTextFormat::setBold( bool b ) +{ + if ( b == fn.bold() ) + return; + fn.setBold( b ); + update(); +} + +void TQTextFormat::setMisspelled( bool b ) +{ + if ( b == (bool)missp ) + return; + missp = b; + update(); +} + +void TQTextFormat::setVAlign( VerticalAlignment a ) +{ + if ( a == ha ) + return; + ha = a; + update(); +} + +void TQTextFormat::setItalic( bool b ) +{ + if ( b == fn.italic() ) + return; + fn.setItalic( b ); + update(); +} + +void TQTextFormat::setUnderline( bool b ) +{ + if ( b == fn.underline() ) + return; + fn.setUnderline( b ); + update(); +} + +void TQTextFormat::setStrikeOut( bool b ) +{ + if ( b == fn.strikeOut() ) + return; + fn.setStrikeOut( b ); + update(); +} + +void TQTextFormat::setFamily( const TQString &f ) +{ + if ( f == fn.family() ) + return; + fn.setFamily( f ); + update(); +} + +void TQTextFormat::setPointSize( int s ) +{ + if ( s == fn.pointSize() ) + return; + fn.setPointSize( s ); + usePixelSizes = FALSE; + update(); +} + +void TQTextFormat::setFont( const TQFont &f ) +{ + if ( f == fn && !k.isEmpty() ) + return; + fn = f; + update(); +} + +void TQTextFormat::setColor( const TQColor &c ) +{ + if ( c == col ) + return; + col = c; + update(); +} + +TQString TQTextFormat::makeFormatChangeTags( TQTextFormat* defaultFormat, TQTextFormat *f, + const TQString& oldAnchorHref, const TQString& anchorHref ) const +{ + TQString tag; + if ( f ) + tag += f->makeFormatEndTags( defaultFormat, oldAnchorHref ); + + if ( !anchorHref.isEmpty() ) + tag += ""; + + if ( font() != defaultFormat->font() + || vAlign() != defaultFormat->vAlign() + || color().rgb() != defaultFormat->color().rgb() ) { + TQString s; + if ( font().family() != defaultFormat->font().family() ) + s += TQString(!!s?";":"") + "font-family:" + fn.family(); + if ( font().italic() && font().italic() != defaultFormat->font().italic() ) + s += TQString(!!s?";":"") + "font-style:" + (font().italic() ? "italic" : "normal"); + if ( font().pointSize() != defaultFormat->font().pointSize() ) + s += TQString(!!s?";":"") + "font-size:" + TQString::number( fn.pointSize() ) + "pt"; + if ( font().weight() != defaultFormat->font().weight() ) + s += TQString(!!s?";":"") + "font-weight:" + TQString::number( fn.weight() * 8 ); + TQString textDecoration; + bool none = FALSE; + if ( font().underline() != defaultFormat->font().underline() ) { + if (font().underline()) + textDecoration = "underline"; + else + none = TRUE; + } + if ( font().overline() != defaultFormat->font().overline() ) { + if (font().overline()) + textDecoration += " overline"; + else + none = TRUE; + } + if ( font().strikeOut() != defaultFormat->font().strikeOut() ) { + if (font().strikeOut()) + textDecoration += " line-through"; + else + none = TRUE; + } + if (none && textDecoration.isEmpty()) + textDecoration = "none"; + if (!textDecoration.isEmpty()) + s += TQString(!!s?";":"") + "text-decoration:" + textDecoration; + if ( vAlign() != defaultFormat->vAlign() ) { + s += TQString(!!s?";":"") + "vertical-align:"; + if ( vAlign() == TQTextFormat::AlignSuperScript ) + s += "super"; + else if ( vAlign() == TQTextFormat::AlignSubScript ) + s += "sub"; + else + s += "normal"; + } + if ( color().rgb() != defaultFormat->color().rgb() ) + s += TQString(!!s?";":"") + "color:" + col.name(); + if ( !s.isEmpty() ) + tag += ""; + } + + return tag; +} + +TQString TQTextFormat::makeFormatEndTags( TQTextFormat* defaultFormat, const TQString& anchorHref ) const +{ + TQString tag; + if ( font().family() != defaultFormat->font().family() + || font().pointSize() != defaultFormat->font().pointSize() + || font().weight() != defaultFormat->font().weight() + || font().italic() != defaultFormat->font().italic() + || font().underline() != defaultFormat->font().underline() + || font().strikeOut() != defaultFormat->font().strikeOut() + || vAlign() != defaultFormat->vAlign() + || color().rgb() != defaultFormat->color().rgb() ) + tag += ""; + if ( !anchorHref.isEmpty() ) + tag += ""; + return tag; +} + +TQTextFormat TQTextFormat::makeTextFormat( const TQStyleSheetItem *style, const TQMap& attr, double scaleFontsFactor ) const +{ + TQTextFormat format(*this); + if (!style ) + return format; + + if ( !style->isAnchor() && style->color().isValid() ) { + // the style is not an anchor and defines a color. + // It might be used inside an anchor and it should + // override the link color. + format.linkColor = FALSE; + } + switch ( style->verticalAlignment() ) { + case TQStyleSheetItem::VAlignBaseline: + format.setVAlign( TQTextFormat::AlignNormal ); + break; + case TQStyleSheetItem::VAlignSuper: + format.setVAlign( TQTextFormat::AlignSuperScript ); + break; + case TQStyleSheetItem::VAlignSub: + format.setVAlign( TQTextFormat::AlignSubScript ); + break; + } + + if ( style->fontWeight() != TQStyleSheetItem::Undefined ) + format.fn.setWeight( style->fontWeight() ); + if ( style->fontSize() != TQStyleSheetItem::Undefined ) { + format.fn.setPointSize( style->fontSize() ); + } else if ( style->logicalFontSize() != TQStyleSheetItem::Undefined ) { + format.logicalFontSize = style->logicalFontSize(); + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } else if ( style->logicalFontSizeStep() ) { + format.logicalFontSize += style->logicalFontSizeStep(); + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } + if ( !style->fontFamily().isEmpty() ) + format.fn.setFamily( style->fontFamily() ); + if ( style->color().isValid() ) + format.col = style->color(); + if ( style->definesFontItalic() ) + format.fn.setItalic( style->fontItalic() ); + if ( style->definesFontUnderline() ) + format.fn.setUnderline( style->fontUnderline() ); + if ( style->definesFontStrikeOut() ) + format.fn.setStrikeOut( style->fontStrikeOut() ); + + + if ( style->name() == "font") { + if ( attr.contains("color") ) { + TQString s = attr["color"]; + if ( !s.isEmpty() ) { + format.col.setNamedColor( s ); + format.linkColor = FALSE; + } + } + if ( attr.contains("face") ) { + TQString a = attr["face"]; + TQString family = a.section( ',', 0, 0 ); + if ( !!family ) + format.fn.setFamily( family ); + } + if ( attr.contains("size") ) { + TQString a = attr["size"]; + int n = a.toInt(); + if ( a[0] == '+' || a[0] == '-' ) + n += 3; + format.logicalFontSize = n; + if ( format.usePixelSizes ) + format.fn.setPixelSize( format.stdSize ); + else + format.fn.setPointSize( format.stdSize ); + style->styleSheet()->scaleFont( format.fn, format.logicalFontSize ); + } + } + if ( attr.contains("style" ) ) { + TQString a = attr["style"]; + for ( int s = 0; s < a.contains(';')+1; s++ ) { + TQString style = a.section( ';', s, s ); + if ( style.startsWith("font-size:" ) && style.endsWith("pt") ) { + format.logicalFontSize = 0; + int size = int( scaleFontsFactor * style.mid( 10, style.length() - 12 ).toDouble() ); + format.setPointSize( size ); + } else if ( style.startsWith("font-style:" ) ) { + TQString s = style.mid( 11 ).stripWhiteSpace(); + if ( s == "normal" ) + format.fn.setItalic( FALSE ); + else if ( s == "italic" || s == "oblique" ) + format.fn.setItalic( TRUE ); + } else if ( style.startsWith("font-weight:" ) ) { + TQString s = style.mid( 12 ); + bool ok = TRUE; + int n = s.toInt( &ok ); + if ( ok ) + format.fn.setWeight( n/8 ); + } else if ( style.startsWith("font-family:" ) ) { + TQString family = style.mid(12).section(',',0,0); + family.replace( '\"', ' ' ); + family.replace( '\'', ' ' ); + family = family.stripWhiteSpace(); + format.fn.setFamily( family ); + } else if ( style.startsWith("text-decoration:" ) ) { + TQString s = style.mid( 16 ); + format.fn.setOverline( s.find("overline") != -1 ); + format.fn.setStrikeOut( s.find("line-through") != -1 ); + format.fn.setUnderline( s.find("underline") != -1 ); + } else if ( style.startsWith("vertical-align:" ) ) { + TQString s = style.mid( 15 ).stripWhiteSpace(); + if ( s == "sub" ) + format.setVAlign( TQTextFormat::AlignSubScript ); + else if ( s == "super" ) + format.setVAlign( TQTextFormat::AlignSuperScript ); + else + format.setVAlign( TQTextFormat::AlignNormal ); + } else if ( style.startsWith("color:" ) ) { + format.col.setNamedColor( style.mid(6) ); + format.linkColor = FALSE; + } + } + } + + format.update(); + return format; +} + +#ifndef QT_NO_TEXTCUSTOMITEM + +struct TQPixmapInt +{ + TQPixmapInt() : ref( 0 ) {} + TQPixmap pm; + int ref; + Q_DUMMY_COMPARISON_OPERATOR(TQPixmapInt) +}; + +static TQMap *pixmap_map = 0; + +TQTextImage::TQTextImage( TQTextDocument *p, const TQMap &attr, const TQString& context, + TQMimeSourceFactory &factory ) + : TQTextCustomItem( p ) +{ + width = height = 0; + if ( attr.contains("width") ) + width = attr["width"].toInt(); + if ( attr.contains("height") ) + height = attr["height"].toInt(); + + reg = 0; + TQString imageName = attr["src"]; + + if (!imageName) + imageName = attr["source"]; + + if ( !imageName.isEmpty() ) { + imgId = TQString( "%1,%2,%3,%4" ).arg( imageName ).arg( width ).arg( height ).arg( (ulong)&factory ); + if ( !pixmap_map ) + pixmap_map = new TQMap; + if ( pixmap_map->contains( imgId ) ) { + TQPixmapInt& pmi = pixmap_map->operator[](imgId); + pm = pmi.pm; + pmi.ref++; + width = pm.width(); + height = pm.height(); + } else { + TQImage img; + const TQMimeSource* m = + factory.data( imageName, context ); + if ( !m ) { + qWarning("TQTextImage: no mimesource for %s", imageName.latin1() ); + } + else { + if ( !TQImageDrag::decode( m, img ) ) { + qWarning("TQTextImage: cannot decode %s", imageName.latin1() ); + } + } + + if ( !img.isNull() ) { + if ( width == 0 ) { + width = img.width(); + if ( height != 0 ) { + width = img.width() * height / img.height(); + } + } + if ( height == 0 ) { + height = img.height(); + if ( width != img.width() ) { + height = img.height() * width / img.width(); + } + } + if ( img.width() != width || img.height() != height ){ +#ifndef QT_NO_IMAGE_SMOOTHSCALE + img = img.smoothScale(width, height); +#endif + width = img.width(); + height = img.height(); + } + pm.convertFromImage( img ); + } + if ( !pm.isNull() ) { + TQPixmapInt& pmi = pixmap_map->operator[](imgId); + pmi.pm = pm; + pmi.ref++; + } + } + if ( pm.mask() ) { + TQRegion mask( *pm.mask() ); + TQRegion all( 0, 0, pm.width(), pm.height() ); + reg = new TQRegion( all.subtract( mask ) ); + } + } + + if ( pm.isNull() && (width*height)==0 ) + width = height = 50; + + place = PlaceInline; + if ( attr["align"] == "left" ) + place = PlaceLeft; + else if ( attr["align"] == "right" ) + place = PlaceRight; + + tmpwidth = width; + tmpheight = height; + + attributes = attr; +} + +TQTextImage::~TQTextImage() +{ + if ( pixmap_map && pixmap_map->contains( imgId ) ) { + TQPixmapInt& pmi = pixmap_map->operator[](imgId); + pmi.ref--; + if ( !pmi.ref ) { + pixmap_map->remove( imgId ); + if ( pixmap_map->isEmpty() ) { + delete pixmap_map; + pixmap_map = 0; + } + } + } + delete reg; +} + +TQString TQTextImage::richText() const +{ + TQString s; + s += "::ConstIterator it = attributes.begin(); + for ( ; it != attributes.end(); ++it ) { + s += it.key() + "="; + if ( (*it).find( ' ' ) != -1 ) + s += "\"" + *it + "\"" + " "; + else + s += *it + " "; + } + s += ">"; + return s; +} + +void TQTextImage::adjustToPainter( TQPainter* p ) +{ + width = scale( tmpwidth, p ); + height = scale( tmpheight, p ); +} + +#if !defined(Q_WS_X11) +#include +#include +static TQPixmap *qrt_selection = 0; +static TQSingleCleanupHandler qrt_cleanup_pixmap; +static void qrt_createSelectionPixmap( const TQColorGroup &cg ) +{ + qrt_selection = new TQPixmap( 2, 2 ); + qrt_cleanup_pixmap.set( &qrt_selection ); + qrt_selection->fill( TQt::color0 ); + TQBitmap m( 2, 2 ); + m.fill( TQt::color1 ); + TQPainter p( &m ); + p.setPen( TQt::color0 ); + for ( int j = 0; j < 2; ++j ) { + p.drawPoint( j % 2, j ); + } + p.end(); + qrt_selection->setMask( m ); + qrt_selection->fill( cg.highlight() ); +} +#endif + +void TQTextImage::draw( TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected ) +{ + if ( placement() != PlaceInline ) { + x = xpos; + y = ypos; + } + + if ( pm.isNull() ) { + p->fillRect( x , y, width, height, cg.dark() ); + return; + } + + if ( is_printer( p ) ) { + p->drawPixmap( TQRect( x, y, width, height ), pm ); + return; + } + + if ( placement() != PlaceInline && !TQRect( xpos, ypos, width, height ).intersects( TQRect( cx, cy, cw, ch ) ) ) + return; + + if ( placement() == PlaceInline ) + p->drawPixmap( x , y, pm ); + else + p->drawPixmap( cx , cy, pm, cx - x, cy - y, cw, ch ); + + if ( selected && placement() == PlaceInline && is_printer( p ) ) { +#if defined(Q_WS_X11) + p->fillRect( TQRect( TQPoint( x, y ), pm.size() ), TQBrush( cg.highlight(), TQBrush::Dense4Pattern) ); +#else // in WIN32 Dense4Pattern doesn't work correctly (transparency problem), so work around it + if ( !qrt_selection ) + qrt_createSelectionPixmap( cg ); + p->drawTiledPixmap( x, y, pm.width(), pm.height(), *qrt_selection ); +#endif + } +} + +void TQTextHorizontalLine::adjustToPainter( TQPainter* p ) +{ + height = scale( tmpheight, p ); +} + + +TQTextHorizontalLine::TQTextHorizontalLine( TQTextDocument *p, const TQMap &attr, + const TQString &, + TQMimeSourceFactory & ) + : TQTextCustomItem( p ) +{ + height = tmpheight = 8; + if ( attr.find( "color" ) != attr.end() ) + color = TQColor( *attr.find( "color" ) ); + shade = attr.find( "noshade" ) == attr.end(); +} + +TQTextHorizontalLine::~TQTextHorizontalLine() +{ +} + +TQString TQTextHorizontalLine::richText() const +{ + return "
"; +} + +void TQTextHorizontalLine::draw( TQPainter* p, int x, int y, int , int , int , int , const TQColorGroup& cg, bool selected ) +{ + TQRect r( x, y, width, height); + if ( is_printer( p ) || !shade ) { + TQPen oldPen = p->pen(); + if ( !color.isValid() ) + p->setPen( TQPen( cg.text(), is_printer( p ) ? height/8 : TQMAX( 2, height/4 ) ) ); + else + p->setPen( TQPen( color, is_printer( p ) ? height/8 : TQMAX( 2, height/4 ) ) ); + p->drawLine( r.left()-1, y + height / 2, r.right() + 1, y + height / 2 ); + p->setPen( oldPen ); + } else { + TQColorGroup g( cg ); + if ( color.isValid() ) + g.setColor( TQColorGroup::Dark, color ); + if ( selected ) + p->fillRect( r, g.highlight() ); + qDrawShadeLine( p, r.left() - 1, y + height / 2, r.right() + 1, y + height / 2, g, TRUE, height / 8 ); + } +} +#endif //QT_NO_TEXTCUSTOMITEM + +/*****************************************************************/ +// Small set of utility functions to make the parser a bit simpler +// + +bool TQTextDocument::hasPrefix(const TQChar* doc, int length, int pos, TQChar c) +{ + if ( pos + 1 > length ) + return FALSE; + return doc[ pos ].lower() == c.lower(); +} + +bool TQTextDocument::hasPrefix( const TQChar* doc, int length, int pos, const TQString& s ) +{ + if ( pos + (int) s.length() > length ) + return FALSE; + for ( int i = 0; i < (int)s.length(); i++ ) { + if ( doc[ pos + i ].lower() != s[ i ].lower() ) + return FALSE; + } + return TRUE; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +static bool qt_is_cell_in_use( TQPtrList& cells, int row, int col ) +{ + for ( TQTextTableCell* c = cells.first(); c; c = cells.next() ) { + if ( row >= c->row() && row < c->row() + c->rowspan() + && col >= c->column() && col < c->column() + c->colspan() ) + return TRUE; + } + return FALSE; +} + +TQTextCustomItem* TQTextDocument::parseTable( const TQMap &attr, const TQTextFormat &fmt, + const TQChar* doc, int length, int& pos, TQTextParagraph *curpar ) +{ + + TQTextTable* table = new TQTextTable( this, attr ); + int row = -1; + int col = -1; + + TQString rowbgcolor; + TQString rowalign; + TQString tablebgcolor = attr["bgcolor"]; + + TQPtrList multicells; + + TQString tagname; + (void) eatSpace(doc, length, pos); + while ( pos < length) { + if (hasPrefix(doc, length, pos, TQChar('<')) ){ + if (hasPrefix(doc, length, pos+1, TQChar('/'))) { + tagname = parseCloseTag( doc, length, pos ); + if ( tagname == "table" ) { + return table; + } + } else { + TQMap attr2; + bool emptyTag = FALSE; + tagname = parseOpenTag( doc, length, pos, attr2, emptyTag ); + if ( tagname == "tr" ) { + rowbgcolor = attr2["bgcolor"]; + rowalign = attr2["align"]; + row++; + col = -1; + } + else if ( tagname == "td" || tagname == "th" ) { + col++; + while ( qt_is_cell_in_use( multicells, row, col ) ) { + col++; + } + + if ( row >= 0 && col >= 0 ) { + const TQStyleSheetItem* s = sheet_->item(tagname); + if ( !attr2.contains("bgcolor") ) { + if (!rowbgcolor.isEmpty() ) + attr2["bgcolor"] = rowbgcolor; + else if (!tablebgcolor.isEmpty() ) + attr2["bgcolor"] = tablebgcolor; + } + if ( !attr2.contains("align") ) { + if (!rowalign.isEmpty() ) + attr2["align"] = rowalign; + } + + // extract the cell contents + int end = pos; + while ( end < length + && !hasPrefix( doc, length, end, "richText()->parentPar = curpar; + if ( cell->colspan() > 1 || cell->rowspan() > 1 ) + multicells.append( cell ); + col += cell->colspan()-1; + pos = end; + } + } + } + + } else { + ++pos; + } + } + return table; +} +#endif // QT_NO_TEXTCUSTOMITEM + +bool TQTextDocument::eatSpace(const TQChar* doc, int length, int& pos, bool includeNbsp ) +{ + int old_pos = pos; + while (pos < length && doc[pos].isSpace() && ( includeNbsp || (doc[pos] != TQChar::nbsp ) ) ) + pos++; + return old_pos < pos; +} + +bool TQTextDocument::eat(const TQChar* doc, int length, int& pos, TQChar c) +{ + bool ok = pos < length && doc[pos] == c; + if ( ok ) + pos++; + return ok; +} +/*****************************************************************/ + +struct Entity { + const char * name; + Q_UINT16 code; +}; + +static const Entity entitylist [] = { + { "AElig", 0x00c6 }, + { "Aacute", 0x00c1 }, + { "Acirc", 0x00c2 }, + { "Agrave", 0x00c0 }, + { "Alpha", 0x0391 }, + { "AMP", 38 }, + { "Aring", 0x00c5 }, + { "Atilde", 0x00c3 }, + { "Auml", 0x00c4 }, + { "Beta", 0x0392 }, + { "Ccedil", 0x00c7 }, + { "Chi", 0x03a7 }, + { "Dagger", 0x2021 }, + { "Delta", 0x0394 }, + { "ETH", 0x00d0 }, + { "Eacute", 0x00c9 }, + { "Ecirc", 0x00ca }, + { "Egrave", 0x00c8 }, + { "Epsilon", 0x0395 }, + { "Eta", 0x0397 }, + { "Euml", 0x00cb }, + { "Gamma", 0x0393 }, + { "GT", 62 }, + { "Iacute", 0x00cd }, + { "Icirc", 0x00ce }, + { "Igrave", 0x00cc }, + { "Iota", 0x0399 }, + { "Iuml", 0x00cf }, + { "Kappa", 0x039a }, + { "Lambda", 0x039b }, + { "LT", 60 }, + { "Mu", 0x039c }, + { "Ntilde", 0x00d1 }, + { "Nu", 0x039d }, + { "OElig", 0x0152 }, + { "Oacute", 0x00d3 }, + { "Ocirc", 0x00d4 }, + { "Ograve", 0x00d2 }, + { "Omega", 0x03a9 }, + { "Omicron", 0x039f }, + { "Oslash", 0x00d8 }, + { "Otilde", 0x00d5 }, + { "Ouml", 0x00d6 }, + { "Phi", 0x03a6 }, + { "Pi", 0x03a0 }, + { "Prime", 0x2033 }, + { "Psi", 0x03a8 }, + { "TQUOT", 34 }, + { "Rho", 0x03a1 }, + { "Scaron", 0x0160 }, + { "Sigma", 0x03a3 }, + { "THORN", 0x00de }, + { "Tau", 0x03a4 }, + { "Theta", 0x0398 }, + { "Uacute", 0x00da }, + { "Ucirc", 0x00db }, + { "Ugrave", 0x00d9 }, + { "Upsilon", 0x03a5 }, + { "Uuml", 0x00dc }, + { "Xi", 0x039e }, + { "Yacute", 0x00dd }, + { "Yuml", 0x0178 }, + { "Zeta", 0x0396 }, + { "aacute", 0x00e1 }, + { "acirc", 0x00e2 }, + { "acute", 0x00b4 }, + { "aelig", 0x00e6 }, + { "agrave", 0x00e0 }, + { "alefsym", 0x2135 }, + { "alpha", 0x03b1 }, + { "amp", 38 }, + { "and", 0x22a5 }, + { "ang", 0x2220 }, + { "apos", 0x0027 }, + { "aring", 0x00e5 }, + { "asymp", 0x2248 }, + { "atilde", 0x00e3 }, + { "auml", 0x00e4 }, + { "bdquo", 0x201e }, + { "beta", 0x03b2 }, + { "brvbar", 0x00a6 }, + { "bull", 0x2022 }, + { "cap", 0x2229 }, + { "ccedil", 0x00e7 }, + { "cedil", 0x00b8 }, + { "cent", 0x00a2 }, + { "chi", 0x03c7 }, + { "circ", 0x02c6 }, + { "clubs", 0x2663 }, + { "cong", 0x2245 }, + { "copy", 0x00a9 }, + { "crarr", 0x21b5 }, + { "cup", 0x222a }, + { "curren", 0x00a4 }, + { "dArr", 0x21d3 }, + { "dagger", 0x2020 }, + { "darr", 0x2193 }, + { "deg", 0x00b0 }, + { "delta", 0x03b4 }, + { "diams", 0x2666 }, + { "divide", 0x00f7 }, + { "eacute", 0x00e9 }, + { "ecirc", 0x00ea }, + { "egrave", 0x00e8 }, + { "empty", 0x2205 }, + { "emsp", 0x2003 }, + { "ensp", 0x2002 }, + { "epsilon", 0x03b5 }, + { "equiv", 0x2261 }, + { "eta", 0x03b7 }, + { "eth", 0x00f0 }, + { "euml", 0x00eb }, + { "euro", 0x20ac }, + { "exist", 0x2203 }, + { "fnof", 0x0192 }, + { "forall", 0x2200 }, + { "frac12", 0x00bd }, + { "frac14", 0x00bc }, + { "frac34", 0x00be }, + { "frasl", 0x2044 }, + { "gamma", 0x03b3 }, + { "ge", 0x2265 }, + { "gt", 62 }, + { "hArr", 0x21d4 }, + { "harr", 0x2194 }, + { "hearts", 0x2665 }, + { "hellip", 0x2026 }, + { "iacute", 0x00ed }, + { "icirc", 0x00ee }, + { "iexcl", 0x00a1 }, + { "igrave", 0x00ec }, + { "image", 0x2111 }, + { "infin", 0x221e }, + { "int", 0x222b }, + { "iota", 0x03b9 }, + { "iquest", 0x00bf }, + { "isin", 0x2208 }, + { "iuml", 0x00ef }, + { "kappa", 0x03ba }, + { "lArr", 0x21d0 }, + { "lambda", 0x03bb }, + { "lang", 0x2329 }, + { "laquo", 0x00ab }, + { "larr", 0x2190 }, + { "lceil", 0x2308 }, + { "ldquo", 0x201c }, + { "le", 0x2264 }, + { "lfloor", 0x230a }, + { "lowast", 0x2217 }, + { "loz", 0x25ca }, + { "lrm", 0x200e }, + { "lsaquo", 0x2039 }, + { "lsquo", 0x2018 }, + { "lt", 60 }, + { "macr", 0x00af }, + { "mdash", 0x2014 }, + { "micro", 0x00b5 }, + { "middot", 0x00b7 }, + { "minus", 0x2212 }, + { "mu", 0x03bc }, + { "nabla", 0x2207 }, + { "nbsp", 0x00a0 }, + { "ndash", 0x2013 }, + { "ne", 0x2260 }, + { "ni", 0x220b }, + { "not", 0x00ac }, + { "notin", 0x2209 }, + { "nsub", 0x2284 }, + { "ntilde", 0x00f1 }, + { "nu", 0x03bd }, + { "oacute", 0x00f3 }, + { "ocirc", 0x00f4 }, + { "oelig", 0x0153 }, + { "ograve", 0x00f2 }, + { "oline", 0x203e }, + { "omega", 0x03c9 }, + { "omicron", 0x03bf }, + { "oplus", 0x2295 }, + { "or", 0x22a6 }, + { "ordf", 0x00aa }, + { "ordm", 0x00ba }, + { "oslash", 0x00f8 }, + { "otilde", 0x00f5 }, + { "otimes", 0x2297 }, + { "ouml", 0x00f6 }, + { "para", 0x00b6 }, + { "part", 0x2202 }, + { "percnt", 0x0025 }, + { "permil", 0x2030 }, + { "perp", 0x22a5 }, + { "phi", 0x03c6 }, + { "pi", 0x03c0 }, + { "piv", 0x03d6 }, + { "plusmn", 0x00b1 }, + { "pound", 0x00a3 }, + { "prime", 0x2032 }, + { "prod", 0x220f }, + { "prop", 0x221d }, + { "psi", 0x03c8 }, + { "quot", 34 }, + { "rArr", 0x21d2 }, + { "radic", 0x221a }, + { "rang", 0x232a }, + { "raquo", 0x00bb }, + { "rarr", 0x2192 }, + { "rceil", 0x2309 }, + { "rdquo", 0x201d }, + { "real", 0x211c }, + { "reg", 0x00ae }, + { "rfloor", 0x230b }, + { "rho", 0x03c1 }, + { "rlm", 0x200f }, + { "rsaquo", 0x203a }, + { "rsquo", 0x2019 }, + { "sbquo", 0x201a }, + { "scaron", 0x0161 }, + { "sdot", 0x22c5 }, + { "sect", 0x00a7 }, + { "shy", 0x00ad }, + { "sigma", 0x03c3 }, + { "sigmaf", 0x03c2 }, + { "sim", 0x223c }, + { "spades", 0x2660 }, + { "sub", 0x2282 }, + { "sube", 0x2286 }, + { "sum", 0x2211 }, + { "sup1", 0x00b9 }, + { "sup2", 0x00b2 }, + { "sup3", 0x00b3 }, + { "sup", 0x2283 }, + { "supe", 0x2287 }, + { "szlig", 0x00df }, + { "tau", 0x03c4 }, + { "there4", 0x2234 }, + { "theta", 0x03b8 }, + { "thetasym", 0x03d1 }, + { "thinsp", 0x2009 }, + { "thorn", 0x00fe }, + { "tilde", 0x02dc }, + { "times", 0x00d7 }, + { "trade", 0x2122 }, + { "uArr", 0x21d1 }, + { "uacute", 0x00fa }, + { "uarr", 0x2191 }, + { "ucirc", 0x00fb }, + { "ugrave", 0x00f9 }, + { "uml", 0x00a8 }, + { "upsih", 0x03d2 }, + { "upsilon", 0x03c5 }, + { "uuml", 0x00fc }, + { "weierp", 0x2118 }, + { "xi", 0x03be }, + { "yacute", 0x00fd }, + { "yen", 0x00a5 }, + { "yuml", 0x00ff }, + { "zeta", 0x03b6 }, + { "zwj", 0x200d }, + { "zwnj", 0x200c }, + { "", 0x0000 } +}; + + + + + +static TQMap *html_map = 0; +static void qt_cleanup_html_map() +{ + delete html_map; + html_map = 0; +} + +static TQMap *htmlMap() +{ + if ( !html_map ) { + html_map = new TQMap; + qAddPostRoutine( qt_cleanup_html_map ); + + const Entity *ent = entitylist; + while( ent->code ) { + html_map->insert( ent->name, TQChar(ent->code) ); + ent++; + } + } + return html_map; +} + +TQChar TQTextDocument::parseHTMLSpecialChar(const TQChar* doc, int length, int& pos) +{ + TQString s; + pos++; + int recoverpos = pos; + while ( pos < length && doc[pos] != ';' && !doc[pos].isSpace() && pos < recoverpos + 8 ) { + s += doc[pos]; + pos++; + } + if (doc[pos] != ';' && !doc[pos].isSpace() ) { + pos = recoverpos; + return '&'; + } + pos++; + + if ( s.length() > 1 && s[0] == '#') { + int off = 1; + int base = 10; + if (s[1] == 'x') { + off = 2; + base = 16; + } + bool ok; + int num = s.mid(off).toInt(&ok, base); + if ( num == 151 ) // ### hack for designer manual + return '-'; + if (ok) + return num; + } else { + TQMap::Iterator it = htmlMap()->find(s); + if ( it != htmlMap()->end() ) { + return *it; + } + } + + pos = recoverpos; + return '&'; +} + +TQString TQTextDocument::parseWord(const TQChar* doc, int length, int& pos, bool lower) +{ + TQString s; + + if (doc[pos] == '"') { + pos++; + while ( pos < length && doc[pos] != '"' ) { + if ( doc[pos] == '&' ) { + s += parseHTMLSpecialChar( doc, length, pos ); + } else { + s += doc[pos]; + pos++; + } + } + eat(doc, length, pos, '"'); + } else if (doc[pos] == '\'') { + pos++; + while ( pos < length && doc[pos] != '\'' ) { + s += doc[pos]; + pos++; + } + eat(doc, length, pos, '\''); + } else { + static TQString term = TQString::fromLatin1("/>"); + while ( pos < length + && doc[pos] != '>' + && !hasPrefix(doc, length, pos, term) + && doc[pos] != '<' + && doc[pos] != '=' + && !doc[pos].isSpace() ) + { + if ( doc[pos] == '&' ) { + s += parseHTMLSpecialChar( doc, length, pos ); + } else { + s += doc[pos]; + pos++; + } + } + if (lower) + s = s.lower(); + } + return s; +} + +TQChar TQTextDocument::parseChar(const TQChar* doc, int length, int& pos, TQStyleSheetItem::WhiteSpaceMode wsm ) +{ + if ( pos >= length ) + return TQChar::null; + + TQChar c = doc[pos++]; + + if (c == '<' ) + return TQChar::null; + + if ( c.isSpace() && c != TQChar::nbsp ) { + if ( wsm == TQStyleSheetItem::WhiteSpacePre ) { + if ( c == '\n' ) + return TQChar_linesep; + else + return c; + } else { // non-pre mode: collapse whitespace except nbsp + while ( pos< length && + doc[pos].isSpace() && doc[pos] != TQChar::nbsp ) + pos++; + return ' '; + } + } + else if ( c == '&' ) + return parseHTMLSpecialChar( doc, length, --pos ); + else + return c; +} + +TQString TQTextDocument::parseOpenTag(const TQChar* doc, int length, int& pos, + TQMap &attr, bool& emptyTag) +{ + emptyTag = FALSE; + pos++; + if ( hasPrefix(doc, length, pos, '!') ) { + if ( hasPrefix( doc, length, pos+1, "--")) { + pos += 3; + // eat comments + TQString pref = TQString::fromLatin1("-->"); + while ( !hasPrefix(doc, length, pos, pref ) && pos < length ) + pos++; + if ( hasPrefix(doc, length, pos, pref ) ) { + pos += 3; + eatSpace(doc, length, pos, TRUE); + } + emptyTag = TRUE; + return TQString::null; + } + else { + // eat strange internal tags + while ( !hasPrefix(doc, length, pos, '>') && pos < length ) + pos++; + if ( hasPrefix(doc, length, pos, '>') ) { + pos++; + eatSpace(doc, length, pos, TRUE); + } + return TQString::null; + } + } + + TQString tag = parseWord(doc, length, pos ); + eatSpace(doc, length, pos, TRUE); + static TQString term = TQString::fromLatin1("/>"); + static TQString s_TRUE = TQString::fromLatin1("TRUE"); + + while (doc[pos] != '>' && ! (emptyTag = hasPrefix(doc, length, pos, term) )) { + TQString key = parseWord(doc, length, pos ); + eatSpace(doc, length, pos, TRUE); + if ( key.isEmpty()) { + // error recovery + while ( pos < length && doc[pos] != '>' ) + pos++; + break; + } + TQString value; + if (hasPrefix(doc, length, pos, '=') ){ + pos++; + eatSpace(doc, length, pos); + value = parseWord(doc, length, pos, FALSE); + } + else + value = s_TRUE; + attr.insert(key.lower(), value ); + eatSpace(doc, length, pos, TRUE); + } + + if (emptyTag) { + eat(doc, length, pos, '/'); + eat(doc, length, pos, '>'); + } + else + eat(doc, length, pos, '>'); + + return tag; +} + +TQString TQTextDocument::parseCloseTag( const TQChar* doc, int length, int& pos ) +{ + pos++; + pos++; + TQString tag = parseWord(doc, length, pos ); + eatSpace(doc, length, pos, TRUE); + eat(doc, length, pos, '>'); + return tag; +} + +TQTextFlow::TQTextFlow() +{ + w = pagesize = 0; +} + +TQTextFlow::~TQTextFlow() +{ + clear(); +} + +void TQTextFlow::clear() +{ +#ifndef QT_NO_TEXTCUSTOMITEM + leftItems.setAutoDelete( TRUE ); + rightItems.setAutoDelete( TRUE ); + leftItems.clear(); + rightItems.clear(); + leftItems.setAutoDelete( FALSE ); + rightItems.setAutoDelete( FALSE ); +#endif +} + +void TQTextFlow::setWidth( int width ) +{ + w = width; +} + +int TQTextFlow::adjustLMargin( int yp, int, int margin, int space ) +{ +#ifndef QT_NO_TEXTCUSTOMITEM + for ( TQTextCustomItem* item = leftItems.first(); item; item = leftItems.next() ) { + if ( item->ypos == -1 ) + continue; + if ( yp >= item->ypos && yp < item->ypos + item->height ) + margin = TQMAX( margin, item->xpos + item->width + space ); + } +#endif + return margin; +} + +int TQTextFlow::adjustRMargin( int yp, int, int margin, int space ) +{ +#ifndef QT_NO_TEXTCUSTOMITEM + for ( TQTextCustomItem* item = rightItems.first(); item; item = rightItems.next() ) { + if ( item->ypos == -1 ) + continue; + if ( yp >= item->ypos && yp < item->ypos + item->height ) + margin = TQMAX( margin, w - item->xpos - space ); + } +#endif + return margin; +} + + +int TQTextFlow::adjustFlow( int y, int /*w*/, int h ) +{ + if ( pagesize > 0 ) { // check pages + int yinpage = y % pagesize; + if ( yinpage <= border_tolerance ) + return border_tolerance - yinpage; + else + if ( yinpage + h > pagesize - border_tolerance ) + return ( pagesize - yinpage ) + border_tolerance; + } + return 0; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +void TQTextFlow::unregisterFloatingItem( TQTextCustomItem* item ) +{ + leftItems.removeRef( item ); + rightItems.removeRef( item ); +} + +void TQTextFlow::registerFloatingItem( TQTextCustomItem* item ) +{ + if ( item->placement() == TQTextCustomItem::PlaceRight ) { + if ( !rightItems.contains( item ) ) + rightItems.append( item ); + } else if ( item->placement() == TQTextCustomItem::PlaceLeft && + !leftItems.contains( item ) ) { + leftItems.append( item ); + } +} +#endif // QT_NO_TEXTCUSTOMITEM + +TQRect TQTextFlow::boundingRect() const +{ + TQRect br; +#ifndef QT_NO_TEXTCUSTOMITEM + TQPtrListIterator l( leftItems ); + while( l.current() ) { + br = br.unite( l.current()->geometry() ); + ++l; + } + TQPtrListIterator r( rightItems ); + while( r.current() ) { + br = br.unite( r.current()->geometry() ); + ++r; + } +#endif + return br; +} + + +void TQTextFlow::drawFloatingItems( TQPainter* p, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected ) +{ +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextCustomItem *item; + for ( item = leftItems.first(); item; item = leftItems.next() ) { + if ( item->xpos == -1 || item->ypos == -1 ) + continue; + item->draw( p, item->xpos, item->ypos, cx, cy, cw, ch, cg, selected ); + } + + for ( item = rightItems.first(); item; item = rightItems.next() ) { + if ( item->xpos == -1 || item->ypos == -1 ) + continue; + item->draw( p, item->xpos, item->ypos, cx, cy, cw, ch, cg, selected ); + } +#endif +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#ifndef QT_NO_TEXTCUSTOMITEM +void TQTextCustomItem::pageBreak( int /*y*/ , TQTextFlow* /*flow*/ ) +{ +} +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +TQTextTable::TQTextTable( TQTextDocument *p, const TQMap & attr ) + : TQTextCustomItem( p ) +{ + cells.setAutoDelete( FALSE ); + cellspacing = 2; + if ( attr.contains("cellspacing") ) + cellspacing = attr["cellspacing"].toInt(); + cellpadding = 1; + if ( attr.contains("cellpadding") ) + cellpadding = attr["cellpadding"].toInt(); + border = innerborder = 0; + if ( attr.contains("border" ) ) { + TQString s( attr["border"] ); + if ( s == "TRUE" ) + border = 1; + else + border = attr["border"].toInt(); + } + us_b = border; + + innerborder = us_ib = border ? 1 : 0; + + if ( border ) + cellspacing += 2; + + us_ib = innerborder; + us_cs = cellspacing; + us_cp = cellpadding; + outerborder = cellspacing + border; + us_ob = outerborder; + layout = new TQGridLayout( 1, 1, cellspacing ); + + fixwidth = 0; + stretch = 0; + if ( attr.contains("width") ) { + bool b; + TQString s( attr["width"] ); + int w = s.toInt( &b ); + if ( b ) { + fixwidth = w; + } else { + s = s.stripWhiteSpace(); + if ( s.length() > 1 && s[ (int)s.length()-1 ] == '%' ) + stretch = s.left( s.length()-1).toInt(); + } + } + us_fixwidth = fixwidth; + + place = PlaceInline; + if ( attr["align"] == "left" ) + place = PlaceLeft; + else if ( attr["align"] == "right" ) + place = PlaceRight; + cachewidth = 0; + attributes = attr; + pageBreakFor = -1; +} + +TQTextTable::~TQTextTable() +{ + delete layout; +} + +TQString TQTextTable::richText() const +{ + TQString s; + s = "::ConstIterator it = attributes.begin(); + for ( ; it != attributes.end(); ++it ) + s += it.key() + "=" + *it + " "; + s += ">\n"; + + int lastRow = -1; + bool needEnd = FALSE; + TQPtrListIterator it2( cells ); + while ( it2.current() ) { + TQTextTableCell *cell = it2.current(); + ++it2; + if ( lastRow != cell->row() ) { + if ( lastRow != -1 ) + s += "\n"; + s += ""; + lastRow = cell->row(); + needEnd = TRUE; + } + s += "attributes.begin(); + for ( ; it != cell->attributes.end(); ++it ) + s += " " + it.key() + "=" + *it; + s += ">"; + s += cell->richText()->richText(); + s += ""; + } + if ( needEnd ) + s += "\n"; + s += "
\n"; + return s; +} + +void TQTextTable::setParagraph(TQTextParagraph *p) +{ + for ( TQTextTableCell* cell = cells.first(); cell; cell = cells.next() ) + cell->richText()->parentPar = p; + TQTextCustomItem::setParagraph(p); +} + +void TQTextTable::adjustToPainter( TQPainter* p ) +{ + cellspacing = scale( us_cs, p ); + cellpadding = scale( us_cp, p ); + border = scale( us_b , p ); + innerborder = scale( us_ib, p ); + outerborder = scale( us_ob ,p ); + fixwidth = scale( us_fixwidth, p); + width = 0; + cachewidth = 0; + for ( TQTextTableCell* cell = cells.first(); cell; cell = cells.next() ) + cell->adjustToPainter( p ); +} + +void TQTextTable::adjustCells( int y , int shift ) +{ + TQPtrListIterator it( cells ); + TQTextTableCell* cell; + bool enlarge = FALSE; + while ( ( cell = it.current() ) ) { + ++it; + TQRect r = cell->geometry(); + if ( y <= r.top() ) { + r.moveBy(0, shift ); + cell->setGeometry( r ); + enlarge = TRUE; + } else if ( y <= r.bottom() ) { + r.rBottom() += shift; + cell->setGeometry( r ); + enlarge = TRUE; + } + } + if ( enlarge ) + height += shift; +} + +void TQTextTable::pageBreak( int yt, TQTextFlow* flow ) +{ + if ( flow->pageSize() <= 0 ) + return; + if ( layout && pageBreakFor > 0 && pageBreakFor != yt ) { + layout->invalidate(); + int h = layout->heightForWidth( width-2*outerborder ); + layout->setGeometry( TQRect(0, 0, width-2*outerborder, h) ); + height = layout->geometry().height()+2*outerborder; + } + pageBreakFor = yt; + TQPtrListIterator it( cells ); + TQTextTableCell* cell; + while ( ( cell = it.current() ) ) { + ++it; + int y = yt + outerborder + cell->geometry().y(); + int shift = flow->adjustFlow( y - cellspacing, width, cell->richText()->height() + 2*cellspacing ); + adjustCells( y - outerborder - yt, shift ); + } +} + + +void TQTextTable::draw(TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected ) +{ + if ( placement() != PlaceInline ) { + x = xpos; + y = ypos; + } + + for (TQTextTableCell* cell = cells.first(); cell; cell = cells.next() ) { + if ( cx < 0 && cy < 0 || + TQRect( cx, cy, cw, ch ).intersects( TQRect( x + outerborder + cell->geometry().x(), + y + outerborder + cell->geometry().y(), + cell->geometry().width(), cell->geometry().height() ) ) ) { + cell->draw( p, x+outerborder, y+outerborder, cx, cy, cw, ch, cg, selected ); + if ( border ) { + TQRect r( x+outerborder+cell->geometry().x() - innerborder, + y+outerborder+cell->geometry().y() - innerborder, + cell->geometry().width() + 2 * innerborder, + cell->geometry().height() + 2 * innerborder ); + if ( is_printer( p ) ) { + TQPen oldPen = p->pen(); + TQRect r2 = r; + r2.addCoords( innerborder/2, innerborder/2, -innerborder/2, -innerborder/2 ); + p->setPen( TQPen( cg.text(), innerborder ) ); + p->drawRect( r2 ); + p->setPen( oldPen ); + } else { + int s = TQMAX( cellspacing-2*innerborder, 0); + if ( s ) { + p->fillRect( r.left()-s, r.top(), s+1, r.height(), cg.button() ); + p->fillRect( r.right(), r.top(), s+1, r.height(), cg.button() ); + p->fillRect( r.left()-s, r.top()-s, r.width()+2*s, s, cg.button() ); + p->fillRect( r.left()-s, r.bottom(), r.width()+2*s, s, cg.button() ); + } + qDrawShadePanel( p, r, cg, TRUE, innerborder ); + } + } + } + } + if ( border ) { + TQRect r ( x, y, width, height ); + if ( is_printer( p ) ) { + TQRect r2 = r; + r2.addCoords( border/2, border/2, -border/2, -border/2 ); + TQPen oldPen = p->pen(); + p->setPen( TQPen( cg.text(), border ) ); + p->drawRect( r2 ); + p->setPen( oldPen ); + } else { + int s = border+TQMAX( cellspacing-2*innerborder, 0); + if ( s ) { + p->fillRect( r.left(), r.top(), s, r.height(), cg.button() ); + p->fillRect( r.right()-s, r.top(), s, r.height(), cg.button() ); + p->fillRect( r.left(), r.top(), r.width(), s, cg.button() ); + p->fillRect( r.left(), r.bottom()-s, r.width(), s, cg.button() ); + } + qDrawShadePanel( p, r, cg, FALSE, border ); + } + } + +} + +int TQTextTable::minimumWidth() const +{ + return fixwidth ? fixwidth : ((layout ? layout->minimumSize().width() : 0) + 2 * outerborder); +} + +void TQTextTable::resize( int nwidth ) +{ + if ( fixwidth && cachewidth != 0 ) + return; + if ( nwidth == cachewidth ) + return; + + + cachewidth = nwidth; + int w = nwidth; + + format( w ); + + if ( stretch ) + nwidth = nwidth * stretch / 100; + + width = nwidth; + layout->invalidate(); + int shw = layout->sizeHint().width() + 2*outerborder; + int mw = layout->minimumSize().width() + 2*outerborder; + if ( stretch ) + width = TQMAX( mw, nwidth ); + else + width = TQMAX( mw, TQMIN( nwidth, shw ) ); + + if ( fixwidth ) + width = fixwidth; + + layout->invalidate(); + mw = layout->minimumSize().width() + 2*outerborder; + width = TQMAX( width, mw ); + + int h = layout->heightForWidth( width-2*outerborder ); + layout->setGeometry( TQRect(0, 0, width-2*outerborder, h) ); + height = layout->geometry().height()+2*outerborder; +} + +void TQTextTable::format( int w ) +{ + for ( int i = 0; i < (int)cells.count(); ++i ) { + TQTextTableCell *cell = cells.at( i ); + TQRect r = cell->geometry(); + r.setWidth( w - 2*outerborder ); + cell->setGeometry( r ); + } +} + +void TQTextTable::addCell( TQTextTableCell* cell ) +{ + cells.append( cell ); + layout->addMultiCell( cell, cell->row(), cell->row() + cell->rowspan()-1, + cell->column(), cell->column() + cell->colspan()-1 ); +} + +bool TQTextTable::enter( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd ) +{ + currCell.remove( c ); + if ( !atEnd ) + return next( c, doc, parag, idx, ox, oy ); + currCell.insert( c, cells.count() ); + return prev( c, doc, parag, idx, ox, oy ); +} + +bool TQTextTable::enterAt( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy, const TQPoint &pos ) +{ + currCell.remove( c ); + int lastCell = -1; + int lastY = -1; + int i; + for ( i = 0; i < (int)cells.count(); ++i ) { + TQTextTableCell *cell = cells.at( i ); + if ( !cell ) + continue; + TQRect r( cell->geometry().x(), + cell->geometry().y(), + cell->geometry().width() + 2 * innerborder + 2 * outerborder, + cell->geometry().height() + 2 * innerborder + 2 * outerborder ); + + if ( r.left() <= pos.x() && r.right() >= pos.x() ) { + if ( cell->geometry().y() > lastY ) { + lastCell = i; + lastY = cell->geometry().y(); + } + if ( r.top() <= pos.y() && r.bottom() >= pos.y() ) { + currCell.insert( c, i ); + break; + } + } + } + if ( i == (int) cells.count() ) + return FALSE; // no cell found + + if ( currCell.find( c ) == currCell.end() ) { + if ( lastY != -1 ) + currCell.insert( c, lastCell ); + else + return FALSE; + } + + TQTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( !cell ) + return FALSE; + doc = cell->richText(); + parag = doc->firstParagraph(); + idx = 0; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +bool TQTextTable::next( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ) +{ + int cc = -1; + if ( currCell.find( c ) != currCell.end() ) + cc = *currCell.find( c ); + if ( cc > (int)cells.count() - 1 || cc < 0 ) + cc = -1; + currCell.remove( c ); + currCell.insert( c, ++cc ); + if ( cc >= (int)cells.count() ) { + currCell.insert( c, 0 ); + TQTextCustomItem::next( c, doc, parag, idx, ox, oy ); + TQTextTableCell *cell = cells.first(); + if ( !cell ) + return FALSE; + doc = cell->richText(); + idx = -1; + return TRUE; + } + + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + TQTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( !cell ) + return FALSE; + doc = cell->richText(); + parag = doc->firstParagraph(); + idx = 0; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +bool TQTextTable::prev( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ) +{ + int cc = -1; + if ( currCell.find( c ) != currCell.end() ) + cc = *currCell.find( c ); + if ( cc > (int)cells.count() - 1 || cc < 0 ) + cc = cells.count(); + currCell.remove( c ); + currCell.insert( c, --cc ); + if ( cc < 0 ) { + currCell.insert( c, 0 ); + TQTextCustomItem::prev( c, doc, parag, idx, ox, oy ); + TQTextTableCell *cell = cells.first(); + if ( !cell ) + return FALSE; + doc = cell->richText(); + idx = -1; + return TRUE; + } + + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + TQTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( !cell ) + return FALSE; + doc = cell->richText(); + parag = doc->lastParagraph(); + idx = parag->length() - 1; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +bool TQTextTable::down( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ) +{ + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + TQTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( cell->row_ == layout->numRows() - 1 ) { + currCell.insert( c, 0 ); + TQTextCustomItem::down( c, doc, parag, idx, ox, oy ); + TQTextTableCell *cell = cells.first(); + if ( !cell ) + return FALSE; + doc = cell->richText(); + idx = -1; + return TRUE; + } + + int oldRow = cell->row_; + int oldCol = cell->col_; + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + int cc = *currCell.find( c ); + for ( int i = cc; i < (int)cells.count(); ++i ) { + cell = cells.at( i ); + if ( cell->row_ > oldRow && cell->col_ == oldCol ) { + currCell.insert( c, i ); + break; + } + } + doc = cell->richText(); + if ( !cell ) + return FALSE; + parag = doc->firstParagraph(); + idx = 0; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +bool TQTextTable::up( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ) +{ + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + TQTextTableCell *cell = cells.at( *currCell.find( c ) ); + if ( cell->row_ == 0 ) { + currCell.insert( c, 0 ); + TQTextCustomItem::up( c, doc, parag, idx, ox, oy ); + TQTextTableCell *cell = cells.first(); + if ( !cell ) + return FALSE; + doc = cell->richText(); + idx = -1; + return TRUE; + } + + int oldRow = cell->row_; + int oldCol = cell->col_; + if ( currCell.find( c ) == currCell.end() ) + return FALSE; + int cc = *currCell.find( c ); + for ( int i = cc; i >= 0; --i ) { + cell = cells.at( i ); + if ( cell->row_ < oldRow && cell->col_ == oldCol ) { + currCell.insert( c, i ); + break; + } + } + doc = cell->richText(); + if ( !cell ) + return FALSE; + parag = doc->lastParagraph(); + idx = parag->length() - 1; + ox += cell->geometry().x() + cell->horizontalAlignmentOffset() + outerborder + parent->x(); + oy += cell->geometry().y() + cell->verticalAlignmentOffset() + outerborder; + return TRUE; +} + +TQTextTableCell::TQTextTableCell( TQTextTable* table, + int row, int column, + const TQMap &attr, + const TQStyleSheetItem* /*style*/, // ### use them + const TQTextFormat& fmt, const TQString& context, + TQMimeSourceFactory &factory, TQStyleSheet *sheet, + const TQString& doc) +{ + cached_width = -1; + cached_sizehint = -1; + + maxw = TQWIDGETSIZE_MAX; + minw = 0; + + parent = table; + row_ = row; + col_ = column; + stretch_ = 0; + richtext = new TQTextDocument( table->parent ); + richtext->formatCollection()->setPaintDevice( table->parent->formatCollection()->paintDevice() ); + richtext->bodyText = fmt.color(); + richtext->setTableCell( this ); + TQString a = *attr.find( "align" ); + if ( !a.isEmpty() ) { + a = a.lower(); + if ( a == "left" ) + richtext->setAlignment( TQt::AlignLeft ); + else if ( a == "center" ) + richtext->setAlignment( TQt::AlignHCenter ); + else if ( a == "right" ) + richtext->setAlignment( TQt::AlignRight ); + } + align = 0; + TQString va = *attr.find( "valign" ); + if ( !va.isEmpty() ) { + va = va.lower(); + if ( va == "top" ) + align |= TQt::AlignTop; + else if ( va == "center" || va == "middle" ) + align |= TQt::AlignVCenter; + else if ( va == "bottom" ) + align |= TQt::AlignBottom; + } + richtext->setFormatter( table->parent->formatter() ); + richtext->setUseFormatCollection( table->parent->useFormatCollection() ); + richtext->setMimeSourceFactory( &factory ); + richtext->setStyleSheet( sheet ); + richtext->setRichText( doc, context, &fmt ); + rowspan_ = 1; + colspan_ = 1; + if ( attr.contains("colspan") ) + colspan_ = attr["colspan"].toInt(); + if ( attr.contains("rowspan") ) + rowspan_ = attr["rowspan"].toInt(); + + background = 0; + if ( attr.contains("bgcolor") ) { + background = new TQBrush(TQColor( attr["bgcolor"] )); + } + + + hasFixedWidth = FALSE; + if ( attr.contains("width") ) { + bool b; + TQString s( attr["width"] ); + int w = s.toInt( &b ); + if ( b ) { + maxw = w; + minw = maxw; + hasFixedWidth = TRUE; + } else { + s = s.stripWhiteSpace(); + if ( s.length() > 1 && s[ (int)s.length()-1 ] == '%' ) + stretch_ = s.left( s.length()-1).toInt(); + } + } + + attributes = attr; + + parent->addCell( this ); +} + +TQTextTableCell::~TQTextTableCell() +{ + delete background; + background = 0; + delete richtext; + richtext = 0; +} + +TQSize TQTextTableCell::sizeHint() const +{ + int extra = 2 * ( parent->innerborder + parent->cellpadding + border_tolerance); + int used = richtext->widthUsed() + extra; + + if (stretch_ ) { + int w = parent->width * stretch_ / 100 - 2*parent->cellspacing - 2*parent->cellpadding; + return TQSize( TQMIN( w, maxw ), 0 ).expandedTo( minimumSize() ); + } + + return TQSize( used, 0 ).expandedTo( minimumSize() ); +} + +TQSize TQTextTableCell::minimumSize() const +{ + int extra = 2 * ( parent->innerborder + parent->cellpadding + border_tolerance); + return TQSize( TQMAX( richtext->minimumWidth() + extra, minw), 0 ); +} + +TQSize TQTextTableCell::maximumSize() const +{ + return TQSize( maxw, TQWIDGETSIZE_MAX ); +} + +TQSizePolicy::ExpandData TQTextTableCell::expanding() const +{ + return TQSizePolicy::BothDirections; +} + +bool TQTextTableCell::isEmpty() const +{ + return FALSE; +} +void TQTextTableCell::setGeometry( const TQRect& r ) +{ + int extra = 2 * ( parent->innerborder + parent->cellpadding ); + if ( r.width() != cached_width ) + richtext->doLayout( TQTextFormat::painter(), r.width() - extra ); + cached_width = r.width(); + geom = r; +} + +TQRect TQTextTableCell::geometry() const +{ + return geom; +} + +bool TQTextTableCell::hasHeightForWidth() const +{ + return TRUE; +} + +int TQTextTableCell::heightForWidth( int w ) const +{ + int extra = 2 * ( parent->innerborder + parent->cellpadding ); + w = TQMAX( minw, w ); + + if ( cached_width != w ) { + TQTextTableCell* that = (TQTextTableCell*) this; + that->richtext->doLayout( TQTextFormat::painter(), w - extra ); + that->cached_width = w; + } + return richtext->height() + extra; +} + +void TQTextTableCell::adjustToPainter( TQPainter* p ) +{ + TQTextParagraph *parag = richtext->firstParagraph(); + while ( parag ) { + parag->adjustToPainter( p ); + parag = parag->next(); + } +} + +int TQTextTableCell::horizontalAlignmentOffset() const +{ + return parent->cellpadding; +} + +int TQTextTableCell::verticalAlignmentOffset() const +{ + if ( (align & TQt::AlignVCenter ) == TQt::AlignVCenter ) + return ( geom.height() - richtext->height() ) / 2; + else if ( ( align & TQt::AlignBottom ) == TQt::AlignBottom ) + return geom.height() - parent->cellpadding - richtext->height() ; + return parent->cellpadding; +} + +void TQTextTableCell::draw( TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool ) +{ + if ( cached_width != geom.width() ) { + int extra = 2 * ( parent->innerborder + parent->cellpadding ); + richtext->doLayout( p, geom.width() - extra ); + cached_width = geom.width(); + } + TQColorGroup g( cg ); + if ( background ) + g.setBrush( TQColorGroup::Base, *background ); + else if ( richtext->paper() ) + g.setBrush( TQColorGroup::Base, *richtext->paper() ); + + p->save(); + p->translate( x + geom.x(), y + geom.y() ); + if ( background ) + p->fillRect( 0, 0, geom.width(), geom.height(), *background ); + else if ( richtext->paper() ) + p->fillRect( 0, 0, geom.width(), geom.height(), *richtext->paper() ); + + p->translate( horizontalAlignmentOffset(), verticalAlignmentOffset() ); + + TQRegion r; + if ( cx >= 0 && cy >= 0 ) + richtext->draw( p, cx - ( x + horizontalAlignmentOffset() + geom.x() ), + cy - ( y + geom.y() + verticalAlignmentOffset() ), + cw, ch, g, FALSE, FALSE, 0 ); + else + richtext->draw( p, -1, -1, -1, -1, g, FALSE, FALSE, 0 ); + + p->restore(); +} +#endif + +#endif //QT_NO_RICHTEXT diff --git a/src/kernel/qrichtext_p.cpp b/src/kernel/qrichtext_p.cpp new file mode 100644 index 000000000..3880ef50f --- /dev/null +++ b/src/kernel/qrichtext_p.cpp @@ -0,0 +1,636 @@ +/**************************************************************************** +** +** Implementation of the internal TQt classes dealing with rich text +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qrichtext_p.h" + +#ifndef QT_NO_RICHTEXT + +TQTextCommand::~TQTextCommand() {} +TQTextCommand::Commands TQTextCommand::type() const { return Invalid; } + + +#ifndef QT_NO_TEXTCUSTOMITEM +TQTextCustomItem::~TQTextCustomItem() {} +void TQTextCustomItem::adjustToPainter( TQPainter* p){ if ( p ) width = 0; } +TQTextCustomItem::Placement TQTextCustomItem::placement() const { return PlaceInline; } + +bool TQTextCustomItem::ownLine() const { return FALSE; } +void TQTextCustomItem::resize( int nwidth ){ width = nwidth; } +void TQTextCustomItem::invalidate() {} + +bool TQTextCustomItem::isNested() const { return FALSE; } +int TQTextCustomItem::minimumWidth() const { return 0; } + +TQString TQTextCustomItem::richText() const { return TQString::null; } + +bool TQTextCustomItem::enter( TQTextCursor *, TQTextDocument*&, TQTextParagraph *&, int &, int &, int &, bool ) +{ + return TRUE; +} +bool TQTextCustomItem::enterAt( TQTextCursor *, TQTextDocument *&, TQTextParagraph *&, int &, int &, int &, const TQPoint & ) +{ + return TRUE; +} +bool TQTextCustomItem::next( TQTextCursor *, TQTextDocument *&, TQTextParagraph *&, int &, int &, int & ) +{ + return TRUE; +} +bool TQTextCustomItem::prev( TQTextCursor *, TQTextDocument *&, TQTextParagraph *&, int &, int &, int & ) +{ + return TRUE; +} +bool TQTextCustomItem::down( TQTextCursor *, TQTextDocument *&, TQTextParagraph *&, int &, int &, int & ) +{ + return TRUE; +} +bool TQTextCustomItem::up( TQTextCursor *, TQTextDocument *&, TQTextParagraph *&, int &, int &, int & ) +{ + return TRUE; +} +#endif // QT_NO_TEXTCUSTOMITEM + +void TQTextFlow::setPageSize( int ps ) { pagesize = ps; } +#ifndef QT_NO_TEXTCUSTOMITEM +bool TQTextFlow::isEmpty() { return leftItems.isEmpty() && rightItems.isEmpty(); } +#else +bool TQTextFlow::isEmpty() { return TRUE; } +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +void TQTextTableCell::invalidate() { cached_width = -1; cached_sizehint = -1; } + +void TQTextTable::invalidate() { cachewidth = -1; } +#endif + +TQTextParagraphData::~TQTextParagraphData() {} +void TQTextParagraphData::join( TQTextParagraphData * ) {} + +TQTextFormatter::~TQTextFormatter() {} +void TQTextFormatter::setWrapEnabled( bool b ) { wrapEnabled = b; } +void TQTextFormatter::setWrapAtColumn( int c ) { wrapColumn = c; } + + + +int TQTextCursor::x() const +{ + if ( idx >= para->length() ) + return 0; + TQTextStringChar *c = para->at( idx ); + int curx = c->x; + if ( !c->rightToLeft && + c->c.isSpace() && + idx > 0 && + para->at( idx - 1 )->c != '\t' && + !c->lineStart && + ( para->alignment() & TQt::AlignJustify ) == TQt::AlignJustify ) + curx = para->at( idx - 1 )->x + para->string()->width( idx - 1 ); + if ( c->rightToLeft ) + curx += para->string()->width( idx ); + return curx; +} + +int TQTextCursor::y() const +{ + int dummy, line; + para->lineStartOfChar( idx, &dummy, &line ); + return para->lineY( line ); +} + +int TQTextCursor::globalX() const { return totalOffsetX() + para->rect().x() + x(); } +int TQTextCursor::globalY() const { return totalOffsetY() + para->rect().y() + y(); } + +TQTextDocument *TQTextCursor::document() const +{ + return para ? para->document() : 0; +} + +void TQTextCursor::gotoPosition( TQTextParagraph* p, int index ) +{ + if ( para && p != para ) { + while ( !indices.isEmpty() && para->document() != p->document() ) + pop(); + Q_ASSERT( indices.isEmpty() || para->document() == p->document() ); + } + para = p; + if ( index < 0 || index >= para->length() ) { +#if defined(QT_CHECK_RANGE) + qWarning( "TQTextCursor::gotoParagraph Index: %d out of range", index ); +#endif + if ( index < 0 || para->length() == 0 ) + index = 0; + else + index = para->length() - 1; + } + + tmpX = -1; + idx = index; + fixCursorPosition(); +} + +bool TQTextDocument::hasSelection( int id, bool visible ) const +{ + return ( selections.find( id ) != selections.end() && + ( !visible || + ( (TQTextDocument*)this )->selectionStartCursor( id ) != + ( (TQTextDocument*)this )->selectionEndCursor( id ) ) ); +} + +void TQTextDocument::setSelectionStart( int id, const TQTextCursor &cursor ) +{ + TQTextDocumentSelection sel; + sel.startCursor = cursor; + sel.endCursor = cursor; + sel.swapped = FALSE; + selections[ id ] = sel; +} + +TQTextParagraph *TQTextDocument::paragAt( int i ) const +{ + TQTextParagraph* p = curParag; + if ( !p || p->paragId() > i ) + p = fParag; + while ( p && p->paragId() != i ) + p = p->next(); + ((TQTextDocument*)this)->curParag = p; + return p; +} + + +TQTextFormat::~TQTextFormat() +{ +} + +TQTextFormat::TQTextFormat() + : fm( TQFontMetrics( fn ) ), linkColor( TRUE ), logicalFontSize( 3 ), stdSize( qApp->font().pointSize() ) +{ + ref = 0; + + usePixelSizes = FALSE; + if ( stdSize == -1 ) { + stdSize = qApp->font().pixelSize(); + usePixelSizes = TRUE; + } + + missp = FALSE; + ha = AlignNormal; + collection = 0; +} + +TQTextFormat::TQTextFormat( const TQStyleSheetItem *style ) + : fm( TQFontMetrics( fn ) ), linkColor( TRUE ), logicalFontSize( 3 ), stdSize( qApp->font().pointSize() ) +{ + ref = 0; + + usePixelSizes = FALSE; + if ( stdSize == -1 ) { + stdSize = qApp->font().pixelSize(); + usePixelSizes = TRUE; + } + + missp = FALSE; + ha = AlignNormal; + collection = 0; + fn = TQFont( style->fontFamily(), + style->fontSize(), + style->fontWeight(), + style->fontItalic() ); + fn.setUnderline( style->fontUnderline() ); + fn.setStrikeOut( style->fontStrikeOut() ); + col = style->color(); + fm = TQFontMetrics( fn ); + leftBearing = fm.minLeftBearing(); + rightBearing = fm.minRightBearing(); + hei = fm.lineSpacing(); + asc = fm.ascent() + (fm.leading()+1)/2; + dsc = fm.descent(); + missp = FALSE; + ha = AlignNormal; + memset( widths, 0, 256 ); + generateKey(); + addRef(); +} + +TQTextFormat::TQTextFormat( const TQFont &f, const TQColor &c, TQTextFormatCollection *parent ) + : fn( f ), col( c ), fm( TQFontMetrics( f ) ), linkColor( TRUE ), + logicalFontSize( 3 ), stdSize( f.pointSize() ) +{ + ref = 0; + usePixelSizes = FALSE; + if ( stdSize == -1 ) { + stdSize = f.pixelSize(); + usePixelSizes = TRUE; + } + collection = parent; + leftBearing = fm.minLeftBearing(); + rightBearing = fm.minRightBearing(); + hei = fm.lineSpacing(); + asc = fm.ascent() + (fm.leading()+1)/2; + dsc = fm.descent(); + missp = FALSE; + ha = AlignNormal; + memset( widths, 0, 256 ); + generateKey(); + addRef(); +} + +TQTextFormat::TQTextFormat( const TQTextFormat &f ) + : fm( f.fm ) +{ + ref = 0; + collection = 0; + fn = f.fn; + col = f.col; + leftBearing = f.leftBearing; + rightBearing = f.rightBearing; + memset( widths, 0, 256 ); + hei = f.hei; + asc = f.asc; + dsc = f.dsc; + stdSize = f.stdSize; + usePixelSizes = f.usePixelSizes; + logicalFontSize = f.logicalFontSize; + missp = f.missp; + ha = f.ha; + k = f.k; + linkColor = f.linkColor; + addRef(); +} + +TQTextFormat& TQTextFormat::operator=( const TQTextFormat &f ) +{ + ref = 0; + collection = f.collection; + fn = f.fn; + col = f.col; + fm = f.fm; + leftBearing = f.leftBearing; + rightBearing = f.rightBearing; + memset( widths, 0, 256 ); + hei = f.hei; + asc = f.asc; + dsc = f.dsc; + stdSize = f.stdSize; + usePixelSizes = f.usePixelSizes; + logicalFontSize = f.logicalFontSize; + missp = f.missp; + ha = f.ha; + k = f.k; + linkColor = f.linkColor; + addRef(); + return *this; +} + +void TQTextFormat::update() +{ + fm = TQFontMetrics( fn ); + leftBearing = fm.minLeftBearing(); + rightBearing = fm.minRightBearing(); + hei = fm.lineSpacing(); + asc = fm.ascent() + (fm.leading()+1)/2; + dsc = fm.descent(); + memset( widths, 0, 256 ); + generateKey(); +} + + +TQPainter* TQTextFormat::pntr = 0; +TQFontMetrics* TQTextFormat::pntr_fm = 0; +int TQTextFormat::pntr_ldg=-1; +int TQTextFormat::pntr_asc=-1; +int TQTextFormat::pntr_hei=-1; +int TQTextFormat::pntr_dsc=-1; + +void TQTextFormat::setPainter( TQPainter *p ) +{ + pntr = p; +} + +TQPainter* TQTextFormat::painter() +{ + return pntr; +} + +void TQTextFormat::applyFont( const TQFont &f ) +{ + TQFontMetrics fm( pntr->fontMetrics() ); + if ( !pntr_fm + || pntr_fm->painter != pntr + || pntr_fm->d != fm.d + || !pntr->font().isCopyOf( f ) ) { + pntr->setFont( f ); + delete pntr_fm; + pntr_fm = new TQFontMetrics( pntr->fontMetrics() ); + pntr_ldg = pntr_fm->leading(); + pntr_asc = pntr_fm->ascent()+(pntr_ldg+1)/2; + pntr_hei = pntr_fm->lineSpacing(); + pntr_dsc = -1; + } +} + +int TQTextFormat::minLeftBearing() const +{ + if ( !pntr || !pntr->isActive() ) + return leftBearing; + applyFont( fn ); + return pntr_fm->minLeftBearing(); +} + +int TQTextFormat::minRightBearing() const +{ + if ( !pntr || !pntr->isActive() ) + return rightBearing; + applyFont( fn ); + return pntr_fm->minRightBearing(); +} + +int TQTextFormat::height() const +{ + if ( !pntr || !pntr->isActive() ) + return hei; + applyFont( fn ); + return pntr_hei; +} + +int TQTextFormat::ascent() const +{ + if ( !pntr || !pntr->isActive() ) + return asc; + applyFont( fn ); + return pntr_asc; +} + +int TQTextFormat::descent() const +{ + if ( !pntr || !pntr->isActive() ) + return dsc; + applyFont( fn ); + if ( pntr_dsc < 0 ) + pntr_dsc = pntr_fm->descent(); + return pntr_dsc; +} + +int TQTextFormat::leading() const +{ + if ( !pntr || !pntr->isActive() ) + return fm.leading(); + applyFont( fn ); + return pntr_ldg; +} + +void TQTextFormat::generateKey() +{ + k = getKey( fn, col, isMisspelled(), vAlign() ); +} + +TQString TQTextFormat::getKey( const TQFont &fn, const TQColor &col, bool misspelled, VerticalAlignment a ) +{ + TQString k = fn.key(); + k += '/'; + k += TQString::number( (uint)col.rgb() ); + k += '/'; + k += TQString::number( (int)misspelled ); + k += '/'; + k += TQString::number( (int)a ); + return k; +} + +TQString TQTextString::toString( const TQMemArray &data ) +{ + TQString s; + int l = data.size(); + s.setUnicode( 0, l ); + TQTextStringChar *c = data.data(); + TQChar *uc = (TQChar *)s.unicode(); + while ( l-- ) + *(uc++) = (c++)->c; + + return s; +} + +void TQTextParagraph::setSelection( int id, int start, int end ) +{ + TQMap::ConstIterator it = selections().find( id ); + if ( it != mSelections->end() ) { + if ( start == ( *it ).start && end == ( *it ).end ) + return; + } + + TQTextParagraphSelection sel; + sel.start = start; + sel.end = end; + (*mSelections)[ id ] = sel; + setChanged( TRUE, TRUE ); +} + +void TQTextParagraph::removeSelection( int id ) +{ + if ( !hasSelection( id ) ) + return; + if ( mSelections ) + mSelections->remove( id ); + setChanged( TRUE, TRUE ); +} + +int TQTextParagraph::selectionStart( int id ) const +{ + if ( !mSelections ) + return -1; + TQMap::ConstIterator it = mSelections->find( id ); + if ( it == mSelections->end() ) + return -1; + return ( *it ).start; +} + +int TQTextParagraph::selectionEnd( int id ) const +{ + if ( !mSelections ) + return -1; + TQMap::ConstIterator it = mSelections->find( id ); + if ( it == mSelections->end() ) + return -1; + return ( *it ).end; +} + +bool TQTextParagraph::hasSelection( int id ) const +{ + return mSelections ? mSelections->contains( id ) : FALSE; +} + +bool TQTextParagraph::fullSelected( int id ) const +{ + if ( !mSelections ) + return FALSE; + TQMap::ConstIterator it = mSelections->find( id ); + if ( it == mSelections->end() ) + return FALSE; + return ( *it ).start == 0 && ( *it ).end == str->length() - 1; +} + +int TQTextParagraph::lineY( int l ) const +{ + if ( l > (int)lineStarts.count() - 1 ) { + qWarning( "TQTextParagraph::lineY: line %d out of range!", l ); + return 0; + } + + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + TQMap::ConstIterator it = lineStarts.begin(); + while ( l-- > 0 ) + ++it; + return ( *it )->y; +} + +int TQTextParagraph::lineBaseLine( int l ) const +{ + if ( l > (int)lineStarts.count() - 1 ) { + qWarning( "TQTextParagraph::lineBaseLine: line %d out of range!", l ); + return 10; + } + + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + TQMap::ConstIterator it = lineStarts.begin(); + while ( l-- > 0 ) + ++it; + return ( *it )->baseLine; +} + +int TQTextParagraph::lineHeight( int l ) const +{ + if ( l > (int)lineStarts.count() - 1 ) { + qWarning( "TQTextParagraph::lineHeight: line %d out of range!", l ); + return 15; + } + + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + TQMap::ConstIterator it = lineStarts.begin(); + while ( l-- > 0 ) + ++it; + return ( *it )->h; +} + +void TQTextParagraph::lineInfo( int l, int &y, int &h, int &bl ) const +{ + if ( l > (int)lineStarts.count() - 1 ) { + qWarning( "TQTextParagraph::lineInfo: line %d out of range!", l ); + qDebug( "%d %d", (int)lineStarts.count() - 1, l ); + y = 0; + h = 15; + bl = 10; + return; + } + + if ( !isValid() ) + ( (TQTextParagraph*)this )->format(); + + TQMap::ConstIterator it = lineStarts.begin(); + while ( l-- > 0 ) + ++it; + y = ( *it )->y; + h = ( *it )->h; + bl = ( *it )->baseLine; +} + + +void TQTextParagraph::setAlignment( int a ) +{ + if ( a == (int)align ) + return; + align = a; + invalidate( 0 ); +} + +TQTextFormatter *TQTextParagraph::formatter() const +{ + if ( hasdoc ) + return document()->formatter(); + if ( pseudoDocument()->pFormatter ) + return pseudoDocument()->pFormatter; + return ( ( (TQTextParagraph*)this )->pseudoDocument()->pFormatter = new TQTextFormatterBreakWords ); +} + +void TQTextParagraph::setTabArray( int *a ) +{ + delete [] tArray; + tArray = a; +} + +void TQTextParagraph::setTabStops( int tw ) +{ + if ( hasdoc ) + document()->setTabStops( tw ); + else + tabStopWidth = tw; +} + +TQMap &TQTextParagraph::selections() const +{ + if ( !mSelections ) + ((TQTextParagraph *)this)->mSelections = new TQMap; + return *mSelections; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +TQPtrList &TQTextParagraph::floatingItems() const +{ + if ( !mFloatingItems ) + ((TQTextParagraph *)this)->mFloatingItems = new TQPtrList; + return *mFloatingItems; +} +#endif + +TQTextStringChar::~TQTextStringChar() +{ + if ( format() ) + format()->removeRef(); + if ( type ) // not Regular + delete d.custom; +} + +TQTextParagraphPseudoDocument::TQTextParagraphPseudoDocument():pFormatter(0),commandHistory(0), minw(0),wused(0),collection(){} +TQTextParagraphPseudoDocument::~TQTextParagraphPseudoDocument(){ delete pFormatter; delete commandHistory; } + + +#endif //QT_NO_RICHTEXT diff --git a/src/kernel/qrichtext_p.h b/src/kernel/qrichtext_p.h new file mode 100644 index 000000000..b203044af --- /dev/null +++ b/src/kernel/qrichtext_p.h @@ -0,0 +1,2141 @@ +/**************************************************************************** +** +** Definition of internal rich text classes +** +** Created : 990124 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQRICHTEXT_P_H +#define TQRICHTEXT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of a number of TQt sources files. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qstring.h" +#include "qptrlist.h" +#include "qrect.h" +#include "qfontmetrics.h" +#include "qintdict.h" +#include "qmap.h" +#include "qstringlist.h" +#include "qfont.h" +#include "qcolor.h" +#include "qsize.h" +#include "qvaluelist.h" +#include "qvaluestack.h" +#include "qobject.h" +#include "qdict.h" +#include "qpixmap.h" +#include "qstylesheet.h" +#include "qptrvector.h" +#include "qpainter.h" +#include "qlayout.h" +#include "qobject.h" +#include "qapplication.h" +#endif // QT_H + +#ifndef QT_NO_RICHTEXT + +class TQTextDocument; +class TQTextString; +class TQTextPreProcessor; +class TQTextFormat; +class TQTextCursor; +class TQTextParagraph; +class TQTextFormatter; +class TQTextIndent; +class TQTextFormatCollection; +class TQStyleSheetItem; +#ifndef QT_NO_TEXTCUSTOMITEM +class TQTextCustomItem; +#endif +class TQTextFlow; +struct TQBidiContext; + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT TQTextStringChar +{ + friend class TQTextString; + +public: + // this is never called, initialize variables in TQTextString::insert()!!! + TQTextStringChar() : nobreak(FALSE), lineStart( 0 ), type( Regular ) {d.format=0;} + ~TQTextStringChar(); + + struct CustomData + { + TQTextFormat *format; +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextCustomItem *custom; +#endif + TQString anchorName; + TQString anchorHref; + }; + enum Type { Regular=0, Custom=1, Anchor=2, CustomAnchor=3 }; + + TQChar c; + // this is the same struct as in qtextengine_p.h. Don't change! + uchar softBreak :1; // Potential linebreak point + uchar whiteSpace :1; // A unicode whitespace character, except NBSP, ZWNBSP + uchar charStop :1; // Valid cursor position (for left/right arrow) + uchar wordStop :1; // Valid cursor position (for ctrl + left/right arrow) + uchar nobreak :1; + + uchar lineStart : 1; + uchar /*Type*/ type : 2; + uchar bidiLevel :7; + uchar rightToLeft : 1; + + int x; + union { + TQTextFormat* format; + CustomData* custom; + } d; + + + int height() const; + int ascent() const; + int descent() const; + bool isCustom() const { return (type & Custom) != 0; } + TQTextFormat *format() const; +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextCustomItem *customItem() const; +#endif + void setFormat( TQTextFormat *f ); +#ifndef QT_NO_TEXTCUSTOMITEM + void setCustomItem( TQTextCustomItem *i ); +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM + void loseCustomItem(); +#endif + + + bool isAnchor() const { return ( type & Anchor) != 0; } + bool isLink() const { return isAnchor() && !!d.custom->anchorHref; } + TQString anchorName() const; + TQString anchorHref() const; + void setAnchor( const TQString& name, const TQString& href ); + +private: + TQTextStringChar &operator=( const TQTextStringChar & ) { + //abort(); + return *this; + } + TQTextStringChar( const TQTextStringChar & ) { + } + friend class TQTextParagraph; +}; + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQMemArray; +// MOC_SKIP_END +#endif + +class Q_EXPORT TQTextString +{ +public: + + TQTextString(); + TQTextString( const TQTextString &s ); + virtual ~TQTextString(); + + static TQString toString( const TQMemArray &data ); + TQString toString() const; + + inline TQTextStringChar &at( int i ) const { return data[ i ]; } + inline int length() const { return data.size(); } + + int width( int idx ) const; + + void insert( int index, const TQString &s, TQTextFormat *f ); + void insert( int index, const TQChar *unicode, int len, TQTextFormat *f ); + void insert( int index, TQTextStringChar *c, bool doAddRefFormat = FALSE ); + void truncate( int index ); + void remove( int index, int len ); + void clear(); + + void setFormat( int index, TQTextFormat *f, bool useCollection ); + + void setBidi( bool b ) { bidi = b; } + bool isBidi() const; + bool isRightToLeft() const; + TQChar::Direction direction() const; + void setDirection( TQChar::Direction d ) { dir = d; bidiDirty = TRUE; } + + TQMemArray rawData() const { return data.copy(); } + + void operator=( const TQString &s ) { clear(); insert( 0, s, 0 ); } + void operator+=( const TQString &s ) { insert( length(), s, 0 ); } + void prepend( const TQString &s ) { insert( 0, s, 0 ); } + int appendParagraphs( TQTextParagraph *start, TQTextParagraph *end ); + + // return next and previous valid cursor positions. + bool validCursorPosition( int idx ); + int nextCursorPosition( int idx ); + int previousCursorPosition( int idx ); + +private: + void checkBidi() const; + + TQMemArray data; + TQString stringCache; + uint bidiDirty : 1; + uint bidi : 1; // true when the paragraph has right to left characters + uint rightToLeft : 1; + uint dir : 5; +}; + +inline bool TQTextString::isBidi() const +{ + if ( bidiDirty ) + checkBidi(); + return bidi; +} + +inline bool TQTextString::isRightToLeft() const +{ + if ( bidiDirty ) + checkBidi(); + return rightToLeft; +} + +inline TQString TQTextString::toString() const +{ + if(bidiDirty) + checkBidi(); + return stringCache; +} + +inline TQChar::Direction TQTextString::direction() const +{ + return (TQChar::Direction) dir; +} + +inline int TQTextString::nextCursorPosition( int next ) +{ + if ( bidiDirty ) + checkBidi(); + + const TQTextStringChar *c = data.data(); + int len = length(); + + if ( next < len - 1 ) { + next++; + while ( next < len - 1 && !c[next].charStop ) + next++; + } + return next; +} + +inline int TQTextString::previousCursorPosition( int prev ) +{ + if ( bidiDirty ) + checkBidi(); + + const TQTextStringChar *c = data.data(); + + if ( prev ) { + prev--; + while ( prev && !c[prev].charStop ) + prev--; + } + return prev; +} + +inline bool TQTextString::validCursorPosition( int idx ) +{ + if ( bidiDirty ) + checkBidi(); + + return (at( idx ).charStop); +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQValueStack; +Q_TEMPLATE_EXTERN template class Q_EXPORT TQValueStack; +Q_TEMPLATE_EXTERN template class Q_EXPORT TQValueStack; +// MOC_SKIP_END +#endif + +class Q_EXPORT TQTextCursor +{ +public: + TQTextCursor( TQTextDocument *d = 0 ); + TQTextCursor( const TQTextCursor &c ); + TQTextCursor &operator=( const TQTextCursor &c ); + virtual ~TQTextCursor() {} + + bool operator==( const TQTextCursor &c ) const; + bool operator!=( const TQTextCursor &c ) const { return !(*this == c); } + + inline TQTextParagraph *paragraph() const { return para; } + + TQTextDocument *document() const; + int index() const; + + void gotoPosition( TQTextParagraph* p, int index = 0); + void setIndex( int index ) { gotoPosition(paragraph(), index ); } + void setParagraph( TQTextParagraph*p ) { gotoPosition(p, 0 ); } + + void gotoLeft(); + void gotoRight(); + void gotoNextLetter(); + void gotoPreviousLetter(); + void gotoUp(); + void gotoDown(); + void gotoLineEnd(); + void gotoLineStart(); + void gotoHome(); + void gotoEnd(); + void gotoPageUp( int visibleHeight ); + void gotoPageDown( int visibleHeight ); + void gotoNextWord( bool onlySpace = FALSE ); + void gotoPreviousWord( bool onlySpace = FALSE ); + void gotoWordLeft(); + void gotoWordRight(); + + void insert( const TQString &s, bool checkNewLine, TQMemArray *formatting = 0 ); + void splitAndInsertEmptyParagraph( bool ind = TRUE, bool updateIds = TRUE ); + bool remove(); + bool removePreviousChar(); + void indent(); + + bool atParagStart(); + bool atParagEnd(); + + int x() const; // x in current paragraph + int y() const; // y in current paragraph + + int globalX() const; + int globalY() const; + + TQTextParagraph *topParagraph() const { return paras.isEmpty() ? para : paras.first(); } + int offsetX() const { return ox; } // inner document offset + int offsetY() const { return oy; } // inner document offset + int totalOffsetX() const; // total document offset + int totalOffsetY() const; // total document offset + + bool place( const TQPoint &pos, TQTextParagraph *s ) { return place( pos, s, FALSE ); } + bool place( const TQPoint &pos, TQTextParagraph *s, bool link ) { return place( pos, s, link, TRUE, TRUE ); } + bool place( const TQPoint &pos, TQTextParagraph *s, bool link, bool loosePlacing, bool matchBetweenCharacters ); + void restoreState(); + + + int nestedDepth() const { return (int)indices.count(); } //### size_t/int cast + void oneUp() { if ( !indices.isEmpty() ) pop(); } + void setValid( bool b ) { valid = b; } + bool isValid() const { return valid; } + + void fixCursorPosition(); +private: + enum Operation { EnterBegin, EnterEnd, Next, Prev, Up, Down }; + + void push(); + void pop(); + bool processNesting( Operation op ); + void invalidateNested(); + void gotoIntoNested( const TQPoint &globalPos ); + + TQTextParagraph *para; + int idx, tmpX; + int ox, oy; + TQValueStack indices; + TQValueStack paras; + TQValueStack xOffsets; + TQValueStack yOffsets; + uint valid : 1; + +}; + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT TQTextCommand +{ +public: + enum Commands { Invalid, Insert, Delete, Format, Style }; + + TQTextCommand( TQTextDocument *d ) : doc( d ), cursor( d ) {} + virtual ~TQTextCommand(); + + virtual Commands type() const; + + virtual TQTextCursor *execute( TQTextCursor *c ) = 0; + virtual TQTextCursor *unexecute( TQTextCursor *c ) = 0; + +protected: + TQTextDocument *doc; + TQTextCursor cursor; + +}; + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQPtrList; +// MOC_SKIP_END +#endif + +class Q_EXPORT TQTextCommandHistory +{ +public: + TQTextCommandHistory( int s ) : current( -1 ), steps( s ) { history.setAutoDelete( TRUE ); } + virtual ~TQTextCommandHistory(); + + void clear() { history.clear(); current = -1; } + + void addCommand( TQTextCommand *cmd ); + TQTextCursor *undo( TQTextCursor *c ); + TQTextCursor *redo( TQTextCursor *c ); + + bool isUndoAvailable(); + bool isRedoAvailable(); + + void setUndoDepth( int d ) { steps = d; } + int undoDepth() const { return steps; } + + int historySize() const { return history.count(); } + int currentPosition() const { return current; } + +private: + TQPtrList history; + int current, steps; + +}; + +inline TQTextCommandHistory::~TQTextCommandHistory() +{ + clear(); +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#ifndef QT_NO_TEXTCUSTOMITEM +class Q_EXPORT TQTextCustomItem +{ +public: + TQTextCustomItem( TQTextDocument *p ) + : xpos(0), ypos(-1), width(-1), height(0), parent( p ) + {} + virtual ~TQTextCustomItem(); + virtual void draw(TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected ) = 0; + + virtual void adjustToPainter( TQPainter* ); + + enum Placement { PlaceInline = 0, PlaceLeft, PlaceRight }; + virtual Placement placement() const; + bool placeInline() { return placement() == PlaceInline; } + + virtual bool ownLine() const; + virtual void resize( int nwidth ); + virtual void invalidate(); + virtual int ascent() const { return height; } + + virtual bool isNested() const; + virtual int minimumWidth() const; + + virtual TQString richText() const; + + int xpos; // used for floating items + int ypos; // used for floating items + int width; + int height; + + TQRect geometry() const { return TQRect( xpos, ypos, width, height ); } + + virtual bool enter( TQTextCursor *, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd = FALSE ); + virtual bool enterAt( TQTextCursor *, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy, const TQPoint & ); + virtual bool next( TQTextCursor *, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool prev( TQTextCursor *, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool down( TQTextCursor *, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool up( TQTextCursor *, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ); + + virtual void setParagraph( TQTextParagraph *p ) { parag = p; } + TQTextParagraph *paragraph() const { return parag; } + + TQTextDocument *parent; + TQTextParagraph *parag; + + virtual void pageBreak( int y, TQTextFlow* flow ); +}; +#endif + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +//Q_TEMPLATE_EXTERN template class Q_EXPORT TQMap; +// MOC_SKIP_END +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +class Q_EXPORT TQTextImage : public TQTextCustomItem +{ +public: + TQTextImage( TQTextDocument *p, const TQMap &attr, const TQString& context, + TQMimeSourceFactory &factory ); + virtual ~TQTextImage(); + + Placement placement() const { return place; } + void adjustToPainter( TQPainter* ); + int minimumWidth() const { return width; } + + TQString richText() const; + + void draw( TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected ); + +private: + TQRegion* reg; + TQPixmap pm; + Placement place; + int tmpwidth, tmpheight; + TQMap attributes; + TQString imgId; + +}; +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +class Q_EXPORT TQTextHorizontalLine : public TQTextCustomItem +{ +public: + TQTextHorizontalLine( TQTextDocument *p, const TQMap &attr, const TQString& context, + TQMimeSourceFactory &factory ); + virtual ~TQTextHorizontalLine(); + + void adjustToPainter( TQPainter* ); + void draw(TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected ); + TQString richText() const; + + bool ownLine() const { return TRUE; } + +private: + int tmpheight; + TQColor color; + bool shade; + +}; +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQPtrList; +// MOC_SKIP_END +#endif +#endif + +class Q_EXPORT TQTextFlow +{ + friend class TQTextDocument; +#ifndef QT_NO_TEXTCUSTOMITEM + friend class TQTextTableCell; +#endif + +public: + TQTextFlow(); + virtual ~TQTextFlow(); + + virtual void setWidth( int width ); + int width() const; + + virtual void setPageSize( int ps ); + int pageSize() const { return pagesize; } + + virtual int adjustLMargin( int yp, int h, int margin, int space ); + virtual int adjustRMargin( int yp, int h, int margin, int space ); + +#ifndef QT_NO_TEXTCUSTOMITEM + virtual void registerFloatingItem( TQTextCustomItem* item ); + virtual void unregisterFloatingItem( TQTextCustomItem* item ); +#endif + virtual TQRect boundingRect() const; + virtual void drawFloatingItems(TQPainter* p, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected ); + + virtual int adjustFlow( int y, int w, int h ); // adjusts y according to the defined pagesize. Returns the shift. + + virtual bool isEmpty(); + + void clear(); + +private: + int w; + int pagesize; + +#ifndef QT_NO_TEXTCUSTOMITEM + TQPtrList leftItems; + TQPtrList rightItems; +#endif +}; + +inline int TQTextFlow::width() const { return w; } + +#ifndef QT_NO_TEXTCUSTOMITEM +class TQTextTable; + +class Q_EXPORT TQTextTableCell : public TQLayoutItem +{ + friend class TQTextTable; + +public: + TQTextTableCell( TQTextTable* table, + int row, int column, + const TQMap &attr, + const TQStyleSheetItem* style, + const TQTextFormat& fmt, const TQString& context, + TQMimeSourceFactory &factory, TQStyleSheet *sheet, const TQString& doc ); + virtual ~TQTextTableCell(); + + TQSize sizeHint() const ; + TQSize minimumSize() const ; + TQSize maximumSize() const ; + TQSizePolicy::ExpandData expanding() const; + bool isEmpty() const; + void setGeometry( const TQRect& ) ; + TQRect geometry() const; + + bool hasHeightForWidth() const; + int heightForWidth( int ) const; + + void adjustToPainter( TQPainter* ); + + int row() const { return row_; } + int column() const { return col_; } + int rowspan() const { return rowspan_; } + int colspan() const { return colspan_; } + int stretch() const { return stretch_; } + + TQTextDocument* richText() const { return richtext; } + TQTextTable* table() const { return parent; } + + void draw( TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected ); + + TQBrush *backGround() const { return background; } + virtual void invalidate(); + + int verticalAlignmentOffset() const; + int horizontalAlignmentOffset() const; + +private: + TQRect geom; + TQTextTable* parent; + TQTextDocument* richtext; + int row_; + int col_; + int rowspan_; + int colspan_; + int stretch_; + int maxw; + int minw; + bool hasFixedWidth; + TQBrush *background; + int cached_width; + int cached_sizehint; + TQMap attributes; + int align; +}; +#endif + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQPtrList; +Q_TEMPLATE_EXTERN template class Q_EXPORT TQMap; +// MOC_SKIP_END +#endif + +#ifndef QT_NO_TEXTCUSTOMITEM +class Q_EXPORT TQTextTable: public TQTextCustomItem +{ + friend class TQTextTableCell; + +public: + TQTextTable( TQTextDocument *p, const TQMap &attr ); + virtual ~TQTextTable(); + + void adjustToPainter( TQPainter *p ); + void pageBreak( int y, TQTextFlow* flow ); + void draw( TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, + const TQColorGroup& cg, bool selected ); + + bool noErase() const { return TRUE; } + bool ownLine() const { return TRUE; } + Placement placement() const { return place; } + bool isNested() const { return TRUE; } + void resize( int nwidth ); + virtual void invalidate(); + + virtual bool enter( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy, bool atEnd = FALSE ); + virtual bool enterAt( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy, const TQPoint &pos ); + virtual bool next( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool prev( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool down( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ); + virtual bool up( TQTextCursor *c, TQTextDocument *&doc, TQTextParagraph *¶g, int &idx, int &ox, int &oy ); + + TQString richText() const; + + int minimumWidth() const; + + TQPtrList tableCells() const { return cells; } + + bool isStretching() const { return stretch; } + void setParagraph(TQTextParagraph *p); + +private: + void format( int w ); + void addCell( TQTextTableCell* cell ); + +private: + TQGridLayout* layout; + TQPtrList cells; + int cachewidth; + int fixwidth; + int cellpadding; + int cellspacing; + int border; + int outerborder; + int stretch; + int innerborder; + int us_cp, us_ib, us_b, us_ob, us_cs; + int us_fixwidth; + TQMap attributes; + TQMap currCell; + Placement place; + void adjustCells( int y , int shift ); + int pageBreakFor; +}; +#endif +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#ifndef QT_NO_TEXTCUSTOMITEM +class TQTextTableCell; +class TQTextParagraph; +#endif + +struct Q_EXPORT TQTextDocumentSelection +{ + TQTextCursor startCursor, endCursor; + bool swapped; + Q_DUMMY_COMPARISON_OPERATOR(TQTextDocumentSelection) +}; + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQMap; +//Q_TEMPLATE_EXTERN template class Q_EXPORT TQMap; +Q_TEMPLATE_EXTERN template class Q_EXPORT TQMap; +Q_TEMPLATE_EXTERN template class Q_EXPORT TQPtrList; +// MOC_SKIP_END +#endif + +class Q_EXPORT TQTextDocument : public TQObject +{ + Q_OBJECT + +#ifndef QT_NO_TEXTCUSTOMITEM + friend class TQTextTableCell; +#endif + friend class TQTextCursor; + friend class TQTextEdit; + friend class TQTextParagraph; + friend class TQTextTable; + +public: + enum SelectionIds { + Standard = 0, + IMSelectionText = 31998, + IMCompositionText = 31999, // this must be higher! + Temp = 32000 // This selection must not be drawn, it's used e.g. by undo/redo to + // remove multiple lines with removeSelectedText() + }; + + TQTextDocument( TQTextDocument *p ); + virtual ~TQTextDocument(); + + TQTextDocument *parent() const { return par; } + TQTextParagraph *parentParagraph() const { return parentPar; } + + void setText( const TQString &text, const TQString &context ); + TQMap attributes() const { return attribs; } + void setAttributes( const TQMap &attr ) { attribs = attr; } + + TQString text() const; + TQString text( int parag ) const; + TQString originalText() const; + + int x() const; + int y() const; + int width() const; + int widthUsed() const; + int visibleWidth() const; + int height() const; + void setWidth( int w ); + int minimumWidth() const; + bool setMinimumWidth( int needed, int used = -1, TQTextParagraph *parag = 0 ); + + void setY( int y ); + int leftMargin() const; + void setLeftMargin( int lm ); + int rightMargin() const; + void setRightMargin( int rm ); + + TQTextParagraph *firstParagraph() const; + TQTextParagraph *lastParagraph() const; + void setFirstParagraph( TQTextParagraph *p ); + void setLastParagraph( TQTextParagraph *p ); + + void invalidate(); + + void setPreProcessor( TQTextPreProcessor *sh ); + TQTextPreProcessor *preProcessor() const; + + void setFormatter( TQTextFormatter *f ); + TQTextFormatter *formatter() const; + + void setIndent( TQTextIndent *i ); + TQTextIndent *indent() const; + + TQColor selectionColor( int id ) const; + bool invertSelectionText( int id ) const; + void setSelectionColor( int id, const TQColor &c ); + void setInvertSelectionText( int id, bool b ); + bool hasSelection( int id, bool visible = FALSE ) const; + void setSelectionStart( int id, const TQTextCursor &cursor ); + bool setSelectionEnd( int id, const TQTextCursor &cursor ); + void selectAll( int id ); + bool removeSelection( int id ); + void selectionStart( int id, int ¶gId, int &index ); + TQTextCursor selectionStartCursor( int id ); + TQTextCursor selectionEndCursor( int id ); + void selectionEnd( int id, int ¶gId, int &index ); + void setFormat( int id, TQTextFormat *f, int flags ); + int numSelections() const { return nSelections; } + void addSelection( int id ); + + TQString selectedText( int id, bool asRichText = FALSE ) const; + void removeSelectedText( int id, TQTextCursor *cursor ); + void indentSelection( int id ); + + TQTextParagraph *paragAt( int i ) const; + + void addCommand( TQTextCommand *cmd ); + TQTextCursor *undo( TQTextCursor *c = 0 ); + TQTextCursor *redo( TQTextCursor *c = 0 ); + TQTextCommandHistory *commands() const { return commandHistory; } + + TQTextFormatCollection *formatCollection() const; + + bool find( TQTextCursor &cursor, const TQString &expr, bool cs, bool wo, bool forward); + + void setTextFormat( TQt::TextFormat f ); + TQt::TextFormat textFormat() const; + + bool inSelection( int selId, const TQPoint &pos ) const; + + TQStyleSheet *styleSheet() const { return sheet_; } +#ifndef QT_NO_MIME + TQMimeSourceFactory *mimeSourceFactory() const { return factory_; } +#endif + TQString context() const { return contxt; } + + void setStyleSheet( TQStyleSheet *s ); + void setDefaultFormat( const TQFont &font, const TQColor &color ); +#ifndef QT_NO_MIME + void setMimeSourceFactory( TQMimeSourceFactory *f ) { if ( f ) factory_ = f; } +#endif + void setContext( const TQString &c ) { if ( !c.isEmpty() ) contxt = c; } + + void setUnderlineLinks( bool b ); + bool underlineLinks() const { return underlLinks; } + + void setPaper( TQBrush *brush ) { if ( backBrush ) delete backBrush; backBrush = brush; } + TQBrush *paper() const { return backBrush; } + + void doLayout( TQPainter *p, int w ); + void draw( TQPainter *p, const TQRect& rect, const TQColorGroup &cg, const TQBrush *paper = 0 ); + bool useDoubleBuffer( TQTextParagraph *parag, TQPainter *p ); + + void drawParagraph( TQPainter *p, TQTextParagraph *parag, int cx, int cy, int cw, int ch, + TQPixmap *&doubleBuffer, const TQColorGroup &cg, + bool drawCursor, TQTextCursor *cursor, bool resetChanged = TRUE ); + TQTextParagraph *draw( TQPainter *p, int cx, int cy, int cw, int ch, const TQColorGroup &cg, + bool onlyChanged = FALSE, bool drawCursor = FALSE, TQTextCursor *cursor = 0, + bool resetChanged = TRUE ); + +#ifndef QT_NO_TEXTCUSTOMITEM + void registerCustomItem( TQTextCustomItem *i, TQTextParagraph *p ); + void unregisterCustomItem( TQTextCustomItem *i, TQTextParagraph *p ); +#endif + + void setFlow( TQTextFlow *f ); + void takeFlow(); + TQTextFlow *flow() const { return flow_; } + bool isPageBreakEnabled() const { return pages; } + void setPageBreakEnabled( bool b ) { pages = b; } + + void setUseFormatCollection( bool b ) { useFC = b; } + bool useFormatCollection() const { return useFC; } + +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextTableCell *tableCell() const { return tc; } + void setTableCell( TQTextTableCell *c ) { tc = c; } +#endif + + void setPlainText( const TQString &text ); + void setRichText( const TQString &text, const TQString &context, const TQTextFormat *initialFormat = 0 ); + TQString richText() const; + TQString plainText() const; + + bool focusNextPrevChild( bool next ); + + int alignment() const; + void setAlignment( int a ); + + int *tabArray() const; + int tabStopWidth() const; + void setTabArray( int *a ); + void setTabStops( int tw ); + + void setUndoDepth( int d ) { commandHistory->setUndoDepth( d ); } + int undoDepth() const { return commandHistory->undoDepth(); } + + int length() const; + void clear( bool createEmptyParag = FALSE ); + + virtual TQTextParagraph *createParagraph( TQTextDocument *d, TQTextParagraph *pr = 0, TQTextParagraph *nx = 0, bool updateIds = TRUE ); + void insertChild( TQObject *o ) { TQObject::insertChild( o ); } + void removeChild( TQObject *o ) { TQObject::removeChild( o ); } + void insertChild( TQTextDocument *d ) { childList.append( d ); } + void removeChild( TQTextDocument *d ) { childList.removeRef( d ); } + TQPtrList children() const { return childList; } + + bool hasFocusParagraph() const; + TQString focusHref() const; + TQString focusName() const; + + void invalidateOriginalText() { oTextValid = FALSE; oText = ""; } + +signals: + void minimumWidthChanged( int ); + +private: + void init(); + TQPixmap *bufferPixmap( const TQSize &s ); + // HTML parser + bool hasPrefix(const TQChar* doc, int length, int pos, TQChar c); + bool hasPrefix(const TQChar* doc, int length, int pos, const TQString& s); +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextCustomItem* parseTable( const TQMap &attr, const TQTextFormat &fmt, + const TQChar* doc, int length, int& pos, TQTextParagraph *curpar ); +#endif + bool eatSpace(const TQChar* doc, int length, int& pos, bool includeNbsp = FALSE ); + bool eat(const TQChar* doc, int length, int& pos, TQChar c); + TQString parseOpenTag(const TQChar* doc, int length, int& pos, TQMap &attr, bool& emptyTag); + TQString parseCloseTag( const TQChar* doc, int length, int& pos ); + TQChar parseHTMLSpecialChar(const TQChar* doc, int length, int& pos); + TQString parseWord(const TQChar* doc, int length, int& pos, bool lower = TRUE); + TQChar parseChar(const TQChar* doc, int length, int& pos, TQStyleSheetItem::WhiteSpaceMode wsm ); + void setRichTextInternal( const TQString &text, TQTextCursor* cursor = 0, const TQTextFormat *initialFormat = 0 ); + void setRichTextMarginsInternal( TQPtrList< TQPtrVector >& styles, TQTextParagraph* stylesPar ); + +private: + struct Q_EXPORT Focus { + TQTextParagraph *parag; + int start, len; + TQString href; + TQString name; + }; + + int cx, cy, cw, vw; + TQTextParagraph *fParag, *lParag; + TQTextPreProcessor *pProcessor; + TQMap selectionColors; + TQMap selections; + TQMap selectionText; + TQTextCommandHistory *commandHistory; + TQTextFormatter *pFormatter; + TQTextIndent *indenter; + TQTextFormatCollection *fCollection; + TQt::TextFormat txtFormat; + uint preferRichText : 1; + uint pages : 1; + uint useFC : 1; + uint withoutDoubleBuffer : 1; + uint underlLinks : 1; + uint nextDoubleBuffered : 1; + uint oTextValid : 1; + uint mightHaveCustomItems : 1; + int align; + int nSelections; + TQTextFlow *flow_; + TQTextDocument *par; + TQTextParagraph *parentPar; +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextTableCell *tc; +#endif + TQBrush *backBrush; + TQPixmap *buf_pixmap; + Focus focusIndicator; + int minw; + int wused; + int leftmargin; + int rightmargin; + TQTextParagraph *minwParag, *curParag; + TQStyleSheet* sheet_; +#ifndef QT_NO_MIME + TQMimeSourceFactory* factory_; +#endif + TQString contxt; + TQMap attribs; + int *tArray; + int tStopWidth; + int uDepth; + TQString oText; + TQPtrList childList; + TQColor linkColor, bodyText; + double scaleFontsFactor; + + short list_tm,list_bm, list_lm, li_tm, li_bm, par_tm, par_bm; +#if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator= + TQTextDocument( const TQTextDocument & ); + TQTextDocument &operator=( const TQTextDocument & ); +#endif +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +class Q_EXPORT TQTextDeleteCommand : public TQTextCommand +{ +public: + TQTextDeleteCommand( TQTextDocument *d, int i, int idx, const TQMemArray &str, + const TQByteArray& oldStyle ); + TQTextDeleteCommand( TQTextParagraph *p, int idx, const TQMemArray &str ); + virtual ~TQTextDeleteCommand(); + + Commands type() const { return Delete; } + TQTextCursor *execute( TQTextCursor *c ); + TQTextCursor *unexecute( TQTextCursor *c ); + +protected: + int id, index; + TQTextParagraph *parag; + TQMemArray text; + TQByteArray styleInformation; + +}; + +class Q_EXPORT TQTextInsertCommand : public TQTextDeleteCommand +{ +public: + TQTextInsertCommand( TQTextDocument *d, int i, int idx, const TQMemArray &str, + const TQByteArray& oldStyleInfo ) + : TQTextDeleteCommand( d, i, idx, str, oldStyleInfo ) {} + TQTextInsertCommand( TQTextParagraph *p, int idx, const TQMemArray &str ) + : TQTextDeleteCommand( p, idx, str ) {} + virtual ~TQTextInsertCommand() {} + + Commands type() const { return Insert; } + TQTextCursor *execute( TQTextCursor *c ) { return TQTextDeleteCommand::unexecute( c ); } + TQTextCursor *unexecute( TQTextCursor *c ) { return TQTextDeleteCommand::execute( c ); } + +}; + +class Q_EXPORT TQTextFormatCommand : public TQTextCommand +{ +public: + TQTextFormatCommand( TQTextDocument *d, int sid, int sidx, int eid, int eidx, const TQMemArray &old, TQTextFormat *f, int fl ); + virtual ~TQTextFormatCommand(); + + Commands type() const { return Format; } + TQTextCursor *execute( TQTextCursor *c ); + TQTextCursor *unexecute( TQTextCursor *c ); + +protected: + int startId, startIndex, endId, endIndex; + TQTextFormat *format; + TQMemArray oldFormats; + int flags; + +}; + +class Q_EXPORT TQTextStyleCommand : public TQTextCommand +{ +public: + TQTextStyleCommand( TQTextDocument *d, int fParag, int lParag, const TQByteArray& beforeChange ); + virtual ~TQTextStyleCommand() {} + + Commands type() const { return Style; } + TQTextCursor *execute( TQTextCursor *c ); + TQTextCursor *unexecute( TQTextCursor *c ); + + static TQByteArray readStyleInformation( TQTextDocument* d, int fParag, int lParag ); + static void writeStyleInformation( TQTextDocument* d, int fParag, const TQByteArray& style ); + +private: + int firstParag, lastParag; + TQByteArray before; + TQByteArray after; +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +struct Q_EXPORT TQTextParagraphSelection +{ + TQTextParagraphSelection() : start(0), end(0) { } + int start, end; + Q_DUMMY_COMPARISON_OPERATOR(TQTextParagraphSelection) +}; + +struct Q_EXPORT TQTextLineStart +{ + TQTextLineStart() : y( 0 ), baseLine( 0 ), h( 0 ) + { } + TQTextLineStart( int y_, int bl, int h_ ) : y( y_ ), baseLine( bl ), h( h_ ), + w( 0 ) + { } + +public: + int y, baseLine, h; + int w; +}; + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQMap; +Q_TEMPLATE_EXTERN template class Q_EXPORT TQMap; +// MOC_SKIP_END +#endif + +class Q_EXPORT TQTextParagraphData +{ +public: + TQTextParagraphData() {} + virtual ~TQTextParagraphData(); + virtual void join( TQTextParagraphData * ); +}; + +class TQTextParagraphPseudoDocument; + +class TQSyntaxHighlighter; + +class Q_EXPORT TQTextParagraph +{ + friend class TQTextDocument; + friend class TQTextCursor; + friend class TQSyntaxHighlighter; + +public: + TQTextParagraph( TQTextDocument *d, TQTextParagraph *pr = 0, TQTextParagraph *nx = 0, bool updateIds = TRUE ); + ~TQTextParagraph(); + + TQTextString *string() const; + TQTextStringChar *at( int i ) const; // maybe remove later + int leftGap() const; + int length() const; // maybe remove later + + void setListStyle( TQStyleSheetItem::ListStyle ls ) { lstyle = ls; changed = TRUE; } + TQStyleSheetItem::ListStyle listStyle() const { return (TQStyleSheetItem::ListStyle)lstyle; } + void setListItem( bool li ); + bool isListItem() const { return litem; } + void setListValue( int v ) { list_val = v; } + int listValue() const { return list_val > 0 ? list_val : -1; } + + void setListDepth( int depth ); + int listDepth() const { return ldepth; } + +// void setFormat( TQTextFormat *fm ); +// TQTextFormat *paragFormat() const; + + inline TQTextDocument *document() const { + if (hasdoc) return (TQTextDocument*) docOrPseudo; + return 0; + } + TQTextParagraphPseudoDocument *pseudoDocument() const; + + TQRect rect() const; + void setHeight( int h ) { r.setHeight( h ); } + void show(); + void hide(); + bool isVisible() const { return visible; } + + TQTextParagraph *prev() const; + TQTextParagraph *next() const; + void setPrev( TQTextParagraph *s ); + void setNext( TQTextParagraph *s ); + + void insert( int index, const TQString &s ); + void insert( int index, const TQChar *unicode, int len ); + void append( const TQString &s, bool reallyAtEnd = FALSE ); + void truncate( int index ); + void remove( int index, int len ); + void join( TQTextParagraph *s ); + + void invalidate( int chr ); + + void move( int &dy ); + void format( int start = -1, bool doMove = TRUE ); + + bool isValid() const; + bool hasChanged() const; + void setChanged( bool b, bool recursive = FALSE ); + + int lineHeightOfChar( int i, int *bl = 0, int *y = 0 ) const; + TQTextStringChar *lineStartOfChar( int i, int *index = 0, int *line = 0 ) const; + int lines() const; + TQTextStringChar *lineStartOfLine( int line, int *index = 0 ) const; + int lineY( int l ) const; + int lineBaseLine( int l ) const; + int lineHeight( int l ) const; + void lineInfo( int l, int &y, int &h, int &bl ) const; + + void setSelection( int id, int start, int end ); + void removeSelection( int id ); + int selectionStart( int id ) const; + int selectionEnd( int id ) const; + bool hasSelection( int id ) const; + bool hasAnySelection() const; + bool fullSelected( int id ) const; + + void setEndState( int s ); + int endState() const; + + void setParagId( int i ); + int paragId() const; + + bool firstPreProcess() const; + void setFirstPreProcess( bool b ); + + void indent( int *oldIndent = 0, int *newIndent = 0 ); + + void setExtraData( TQTextParagraphData *data ); + TQTextParagraphData *extraData() const; + + TQMap &lineStartList(); + + void setFormat( int index, int len, TQTextFormat *f, bool useCollection = TRUE, int flags = -1 ); + + void setAlignment( int a ); + int alignment() const; + + void paint( TQPainter &painter, const TQColorGroup &cg, TQTextCursor *cursor = 0, bool drawSelections = FALSE, + int clipx = -1, int clipy = -1, int clipw = -1, int cliph = -1 ); + + int topMargin() const; + int bottomMargin() const; + int leftMargin() const; + int firstLineMargin() const; + int rightMargin() const; + int lineSpacing() const; + +#ifndef QT_NO_TEXTCUSTOMITEM + void registerFloatingItem( TQTextCustomItem *i ); + void unregisterFloatingItem( TQTextCustomItem *i ); +#endif + + void setFullWidth( bool b ) { fullWidth = b; } + bool isFullWidth() const { return fullWidth; } + +#ifndef QT_NO_TEXTCUSTOMITEM + TQTextTableCell *tableCell() const; +#endif + + TQBrush *background() const; + + int documentWidth() const; + int documentVisibleWidth() const; + int documentX() const; + int documentY() const; + TQTextFormatCollection *formatCollection() const; + TQTextFormatter *formatter() const; + + int nextTab( int i, int x ); + int *tabArray() const; + void setTabArray( int *a ); + void setTabStops( int tw ); + + void adjustToPainter( TQPainter *p ); + + void setNewLinesAllowed( bool b ); + bool isNewLinesAllowed() const; + + TQString richText() const; + + void addCommand( TQTextCommand *cmd ); + TQTextCursor *undo( TQTextCursor *c = 0 ); + TQTextCursor *redo( TQTextCursor *c = 0 ); + TQTextCommandHistory *commands() const; + void copyParagData( TQTextParagraph *parag ); + + void setBreakable( bool b ) { breakable = b; } + bool isBreakable() const { return breakable; } + + void setBackgroundColor( const TQColor &c ); + TQColor *backgroundColor() const { return bgcol; } + void clearBackgroundColor(); + + void setMovedDown( bool b ) { movedDown = b; } + bool wasMovedDown() const { return movedDown; } + + void setDirection( TQChar::Direction d ); + TQChar::Direction direction() const; + void setPaintDevice( TQPaintDevice *pd ) { paintdevice = pd; } + + void readStyleInformation( TQDataStream& stream ); + void writeStyleInformation( TQDataStream& stream ) const; + +protected: + void setColorForSelection( TQColor &c, TQPainter &p, const TQColorGroup& cg, int selection ); + void drawLabel( TQPainter* p, int x, int y, int w, int h, int base, const TQColorGroup& cg ); + void drawString( TQPainter &painter, const TQString &str, int start, int len, int xstart, + int y, int baseLine, int w, int h, bool drawSelections, int fullSelectionWidth, + TQTextStringChar *formatChar, const TQColorGroup& cg, + bool rightToLeft ); + +private: + TQMap &selections() const; +#ifndef QT_NO_TEXTCUSTOMITEM + TQPtrList &floatingItems() const; +#endif + TQBrush backgroundBrush( const TQColorGroup&cg ) { if ( bgcol ) return *bgcol; return cg.brush( TQColorGroup::Base ); } + void invalidateStyleCache(); + + TQMap lineStarts; + TQRect r; + TQTextParagraph *p, *n; + void *docOrPseudo; + uint changed : 1; + uint firstFormat : 1; + uint firstPProcess : 1; + uint needPreProcess : 1; + uint fullWidth : 1; + uint lastInFrame : 1; + uint visible : 1; + uint breakable : 1; + uint movedDown : 1; + uint mightHaveCustomItems : 1; + uint hasdoc : 1; + uint litem : 1; // whether the paragraph is a list item + uint rtext : 1; // whether the paragraph needs rich text margin + int align : 4; + uint /*TQStyleSheetItem::ListStyle*/ lstyle : 4; + int invalid; + int state, id; + TQTextString *str; + TQMap *mSelections; +#ifndef QT_NO_TEXTCUSTOMITEM + TQPtrList *mFloatingItems; +#endif + short utm, ubm, ulm, urm, uflm, ulinespacing; + short tabStopWidth; + int minwidth; + int *tArray; + TQTextParagraphData *eData; + short list_val; + ushort ldepth; + TQColor *bgcol; + TQPaintDevice *paintdevice; +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT TQTextFormatter +{ +public: + TQTextFormatter(); + virtual ~TQTextFormatter(); + + virtual int format( TQTextDocument *doc, TQTextParagraph *parag, int start, const TQMap &oldLineStarts ) = 0; + virtual int formatVertically( TQTextDocument* doc, TQTextParagraph* parag ); + + bool isWrapEnabled( TQTextParagraph *p ) const { if ( !wrapEnabled ) return FALSE; if ( p && !p->isBreakable() ) return FALSE; return TRUE;} + int wrapAtColumn() const { return wrapColumn;} + virtual void setWrapEnabled( bool b ); + virtual void setWrapAtColumn( int c ); + virtual void setAllowBreakInWords( bool b ) { biw = b; } + bool allowBreakInWords() const { return biw; } + + int minimumWidth() const { return thisminw; } + int widthUsed() const { return thiswused; } + +protected: + virtual TQTextLineStart *formatLine( TQTextParagraph *parag, TQTextString *string, TQTextLineStart *line, TQTextStringChar *start, + TQTextStringChar *last, int align = TQt::AlignAuto, int space = 0 ); +#ifndef QT_NO_COMPLEXTEXT + virtual TQTextLineStart *bidiReorderLine( TQTextParagraph *parag, TQTextString *string, TQTextLineStart *line, TQTextStringChar *start, + TQTextStringChar *last, int align, int space ); +#endif + void insertLineStart( TQTextParagraph *parag, int index, TQTextLineStart *ls ); + + int thisminw; + int thiswused; + +private: + bool wrapEnabled; + int wrapColumn; + bool biw; + +#ifdef HAVE_THAI_BREAKS + static TQCString *thaiCache; + static TQTextString *cachedString; + static ThBreakIterator *thaiIt; +#endif +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT TQTextFormatterBreakInWords : public TQTextFormatter +{ +public: + TQTextFormatterBreakInWords(); + virtual ~TQTextFormatterBreakInWords() {} + + int format( TQTextDocument *doc, TQTextParagraph *parag, int start, const TQMap &oldLineStarts ); + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT TQTextFormatterBreakWords : public TQTextFormatter +{ +public: + TQTextFormatterBreakWords(); + virtual ~TQTextFormatterBreakWords() {} + + int format( TQTextDocument *doc, TQTextParagraph *parag, int start, const TQMap &oldLineStarts ); + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT TQTextIndent +{ +public: + TQTextIndent(); + virtual ~TQTextIndent() {} + + virtual void indent( TQTextDocument *doc, TQTextParagraph *parag, int *oldIndent = 0, int *newIndent = 0 ) = 0; + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT TQTextPreProcessor +{ +public: + enum Ids { + Standard = 0 + }; + + TQTextPreProcessor(); + virtual ~TQTextPreProcessor() {} + + virtual void process( TQTextDocument *doc, TQTextParagraph *, int, bool = TRUE ) = 0; + virtual TQTextFormat *format( int id ) = 0; + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +class Q_EXPORT TQTextFormat +{ + friend class TQTextFormatCollection; + friend class TQTextDocument; + +public: + enum Flags { + NoFlags, + Bold = 1, + Italic = 2, + Underline = 4, + Family = 8, + Size = 16, + Color = 32, + Misspelled = 64, + VAlign = 128, + StrikeOut= 256, + Font = Bold | Italic | Underline | Family | Size | StrikeOut, + Format = Font | Color | Misspelled | VAlign + }; + + enum VerticalAlignment { AlignNormal, AlignSuperScript, AlignSubScript }; + + TQTextFormat(); + virtual ~TQTextFormat(); + + TQTextFormat( const TQStyleSheetItem *s ); + TQTextFormat( const TQFont &f, const TQColor &c, TQTextFormatCollection *parent = 0 ); + TQTextFormat( const TQTextFormat &fm ); + TQTextFormat makeTextFormat( const TQStyleSheetItem *style, const TQMap& attr, double scaleFontsFactor ) const; + TQTextFormat& operator=( const TQTextFormat &fm ); + TQColor color() const; + TQFont font() const; + TQFontMetrics fontMetrics() const { return fm; } + bool isMisspelled() const; + VerticalAlignment vAlign() const; + int minLeftBearing() const; + int minRightBearing() const; + int width( const TQChar &c ) const; + int width( const TQString &str, int pos ) const; + int height() const; + int ascent() const; + int descent() const; + int leading() const; + bool useLinkColor() const; + + void setBold( bool b ); + void setItalic( bool b ); + void setUnderline( bool b ); + void setStrikeOut( bool b ); + void setFamily( const TQString &f ); + void setPointSize( int s ); + void setFont( const TQFont &f ); + void setColor( const TQColor &c ); + void setMisspelled( bool b ); + void setVAlign( VerticalAlignment a ); + + bool operator==( const TQTextFormat &f ) const; + TQTextFormatCollection *parent() const; + const TQString &key() const; + + static TQString getKey( const TQFont &f, const TQColor &c, bool misspelled, VerticalAlignment vAlign ); + + void addRef(); + void removeRef(); + + TQString makeFormatChangeTags( TQTextFormat* defaultFormat, TQTextFormat *f, const TQString& oldAnchorHref, const TQString& anchorHref ) const; + TQString makeFormatEndTags( TQTextFormat* defaultFormat, const TQString& anchorHref ) const; + + static void setPainter( TQPainter *p ); + static TQPainter* painter(); + + bool fontSizesInPixels() { return usePixelSizes; } + +protected: + virtual void generateKey(); + +private: + void update(); + static void applyFont( const TQFont &f ); + +private: + TQFont fn; + TQColor col; + TQFontMetrics fm; + uint missp : 1; + uint linkColor : 1; + uint usePixelSizes : 1; + int leftBearing, rightBearing; + VerticalAlignment ha; + uchar widths[ 256 ]; + int hei, asc, dsc; + TQTextFormatCollection *collection; + int ref; + TQString k; + int logicalFontSize; + int stdSize; + static TQPainter *pntr; + static TQFontMetrics *pntr_fm; + static int pntr_asc; + static int pntr_hei; + static int pntr_ldg; + static int pntr_dsc; + +}; + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +#if defined(Q_TEMPLATEDLL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQDict; +// MOC_SKIP_END +#endif + +class Q_EXPORT TQTextFormatCollection +{ + friend class TQTextDocument; + friend class TQTextFormat; + +public: + TQTextFormatCollection(); + virtual ~TQTextFormatCollection(); + + void setDefaultFormat( TQTextFormat *f ); + TQTextFormat *defaultFormat() const; + virtual TQTextFormat *format( TQTextFormat *f ); + virtual TQTextFormat *format( TQTextFormat *of, TQTextFormat *nf, int flags ); + virtual TQTextFormat *format( const TQFont &f, const TQColor &c ); + virtual void remove( TQTextFormat *f ); + virtual TQTextFormat *createFormat( const TQTextFormat &f ) { return new TQTextFormat( f ); } + virtual TQTextFormat *createFormat( const TQFont &f, const TQColor &c ) { return new TQTextFormat( f, c, this ); } + + void updateDefaultFormat( const TQFont &font, const TQColor &c, TQStyleSheet *sheet ); + + TQPaintDevice *paintDevice() const { return paintdevice; } + void setPaintDevice( TQPaintDevice * ); + +private: + void updateKeys(); + +private: + TQTextFormat *defFormat, *lastFormat, *cachedFormat; + TQDict cKey; + TQTextFormat *cres; + TQFont cfont; + TQColor ccol; + TQString kof, knf; + int cflags; + + TQPaintDevice *paintdevice; +}; + +class Q_EXPORT TQTextParagraphPseudoDocument +{ +public: + TQTextParagraphPseudoDocument(); + ~TQTextParagraphPseudoDocument(); + TQRect docRect; + TQTextFormatter *pFormatter; + TQTextCommandHistory *commandHistory; + int minw; + int wused; + TQTextFormatCollection collection; +}; + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline int TQTextParagraph::length() const +{ + return str->length(); +} + +inline TQRect TQTextParagraph::rect() const +{ + return r; +} + +inline int TQTextCursor::index() const +{ + return idx; +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline int TQTextDocument::x() const +{ + return cx; +} + +inline int TQTextDocument::y() const +{ + return cy; +} + +inline int TQTextDocument::width() const +{ + return TQMAX( cw, flow_->width() ); +} + +inline int TQTextDocument::visibleWidth() const +{ + return vw; +} + +inline TQTextParagraph *TQTextDocument::firstParagraph() const +{ + return fParag; +} + +inline TQTextParagraph *TQTextDocument::lastParagraph() const +{ + return lParag; +} + +inline void TQTextDocument::setFirstParagraph( TQTextParagraph *p ) +{ + fParag = p; +} + +inline void TQTextDocument::setLastParagraph( TQTextParagraph *p ) +{ + lParag = p; +} + +inline void TQTextDocument::setWidth( int w ) +{ + cw = TQMAX( w, minw ); + flow_->setWidth( cw ); + vw = w; +} + +inline int TQTextDocument::minimumWidth() const +{ + return minw; +} + +inline void TQTextDocument::setY( int y ) +{ + cy = y; +} + +inline int TQTextDocument::leftMargin() const +{ + return leftmargin; +} + +inline void TQTextDocument::setLeftMargin( int lm ) +{ + leftmargin = lm; +} + +inline int TQTextDocument::rightMargin() const +{ + return rightmargin; +} + +inline void TQTextDocument::setRightMargin( int rm ) +{ + rightmargin = rm; +} + +inline TQTextPreProcessor *TQTextDocument::preProcessor() const +{ + return pProcessor; +} + +inline void TQTextDocument::setPreProcessor( TQTextPreProcessor * sh ) +{ + pProcessor = sh; +} + +inline void TQTextDocument::setFormatter( TQTextFormatter *f ) +{ + delete pFormatter; + pFormatter = f; +} + +inline TQTextFormatter *TQTextDocument::formatter() const +{ + return pFormatter; +} + +inline void TQTextDocument::setIndent( TQTextIndent *i ) +{ + indenter = i; +} + +inline TQTextIndent *TQTextDocument::indent() const +{ + return indenter; +} + +inline TQColor TQTextDocument::selectionColor( int id ) const +{ + return selectionColors[ id ]; +} + +inline bool TQTextDocument::invertSelectionText( int id ) const +{ + return selectionText[ id ]; +} + +inline void TQTextDocument::setSelectionColor( int id, const TQColor &c ) +{ + selectionColors[ id ] = c; +} + +inline void TQTextDocument::setInvertSelectionText( int id, bool b ) +{ + selectionText[ id ] = b; +} + +inline TQTextFormatCollection *TQTextDocument::formatCollection() const +{ + return fCollection; +} + +inline int TQTextDocument::alignment() const +{ + return align; +} + +inline void TQTextDocument::setAlignment( int a ) +{ + align = a; +} + +inline int *TQTextDocument::tabArray() const +{ + return tArray; +} + +inline int TQTextDocument::tabStopWidth() const +{ + return tStopWidth; +} + +inline void TQTextDocument::setTabArray( int *a ) +{ + tArray = a; +} + +inline void TQTextDocument::setTabStops( int tw ) +{ + tStopWidth = tw; +} + +inline TQString TQTextDocument::originalText() const +{ + if ( oTextValid ) + return oText; + return text(); +} + +inline void TQTextDocument::setFlow( TQTextFlow *f ) +{ + if ( flow_ ) + delete flow_; + flow_ = f; +} + +inline void TQTextDocument::takeFlow() +{ + flow_ = 0; +} + +inline bool TQTextDocument::useDoubleBuffer( TQTextParagraph *parag, TQPainter *p ) +{ + return ( !parag->document()->parent() || parag->document()->nextDoubleBuffered ) && + ( !p || !p->device() || p->device()->devType() != TQInternal::Printer ); +} + +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline TQColor TQTextFormat::color() const +{ + return col; +} + +inline TQFont TQTextFormat::font() const +{ + return fn; +} + +inline bool TQTextFormat::isMisspelled() const +{ + return missp; +} + +inline TQTextFormat::VerticalAlignment TQTextFormat::vAlign() const +{ + return ha; +} + +inline bool TQTextFormat::operator==( const TQTextFormat &f ) const +{ + return k == f.k; +} + +inline TQTextFormatCollection *TQTextFormat::parent() const +{ + return collection; +} + +inline void TQTextFormat::addRef() +{ + ref++; +} + +inline void TQTextFormat::removeRef() +{ + ref--; + if ( !collection ) + return; + if ( this == collection->defFormat ) + return; + if ( ref == 0 ) + collection->remove( this ); +} + +inline const TQString &TQTextFormat::key() const +{ + return k; +} + +inline bool TQTextFormat::useLinkColor() const +{ + return linkColor; +} + +inline TQTextStringChar *TQTextParagraph::at( int i ) const +{ + return &str->at( i ); +} + +inline bool TQTextParagraph::isValid() const +{ + return invalid == -1; +} + +inline bool TQTextParagraph::hasChanged() const +{ + return changed; +} + +inline void TQTextParagraph::setBackgroundColor( const TQColor & c ) +{ + delete bgcol; + bgcol = new TQColor( c ); + setChanged( TRUE ); +} + +inline void TQTextParagraph::clearBackgroundColor() +{ + delete bgcol; bgcol = 0; setChanged( TRUE ); +} + +inline void TQTextParagraph::append( const TQString &s, bool reallyAtEnd ) +{ + if ( reallyAtEnd ) + insert( str->length(), s ); + else + insert( TQMAX( str->length() - 1, 0 ), s ); +} + +inline TQTextParagraph *TQTextParagraph::prev() const +{ + return p; +} + +inline TQTextParagraph *TQTextParagraph::next() const +{ + return n; +} + +inline bool TQTextParagraph::hasAnySelection() const +{ + return mSelections ? !selections().isEmpty() : FALSE; +} + +inline void TQTextParagraph::setEndState( int s ) +{ + if ( s == state ) + return; + state = s; +} + +inline int TQTextParagraph::endState() const +{ + return state; +} + +inline void TQTextParagraph::setParagId( int i ) +{ + id = i; +} + +inline int TQTextParagraph::paragId() const +{ + if ( id == -1 ) + qWarning( "invalid parag id!!!!!!!! (%p)", (void*)this ); + return id; +} + +inline bool TQTextParagraph::firstPreProcess() const +{ + return firstPProcess; +} + +inline void TQTextParagraph::setFirstPreProcess( bool b ) +{ + firstPProcess = b; +} + +inline TQMap &TQTextParagraph::lineStartList() +{ + return lineStarts; +} + +inline TQTextString *TQTextParagraph::string() const +{ + return str; +} + +inline TQTextParagraphPseudoDocument *TQTextParagraph::pseudoDocument() const +{ + if ( hasdoc ) + return 0; + return (TQTextParagraphPseudoDocument*) docOrPseudo; +} + + +#ifndef QT_NO_TEXTCUSTOMITEM +inline TQTextTableCell *TQTextParagraph::tableCell() const +{ + return hasdoc ? document()->tableCell () : 0; +} +#endif + +inline TQTextCommandHistory *TQTextParagraph::commands() const +{ + return hasdoc ? document()->commands() : pseudoDocument()->commandHistory; +} + + +inline int TQTextParagraph::alignment() const +{ + return align; +} + +#ifndef QT_NO_TEXTCUSTOMITEM +inline void TQTextParagraph::registerFloatingItem( TQTextCustomItem *i ) +{ + floatingItems().append( i ); +} + +inline void TQTextParagraph::unregisterFloatingItem( TQTextCustomItem *i ) +{ + floatingItems().removeRef( i ); +} +#endif + +inline TQBrush *TQTextParagraph::background() const +{ +#ifndef QT_NO_TEXTCUSTOMITEM + return tableCell() ? tableCell()->backGround() : 0; +#else + return 0; +#endif +} + +inline int TQTextParagraph::documentWidth() const +{ + return hasdoc ? document()->width() : pseudoDocument()->docRect.width(); +} + +inline int TQTextParagraph::documentVisibleWidth() const +{ + return hasdoc ? document()->visibleWidth() : pseudoDocument()->docRect.width(); +} + +inline int TQTextParagraph::documentX() const +{ + return hasdoc ? document()->x() : pseudoDocument()->docRect.x(); +} + +inline int TQTextParagraph::documentY() const +{ + return hasdoc ? document()->y() : pseudoDocument()->docRect.y(); +} + +inline void TQTextParagraph::setExtraData( TQTextParagraphData *data ) +{ + eData = data; +} + +inline TQTextParagraphData *TQTextParagraph::extraData() const +{ + return eData; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline void TQTextFormatCollection::setDefaultFormat( TQTextFormat *f ) +{ + defFormat = f; +} + +inline TQTextFormat *TQTextFormatCollection::defaultFormat() const +{ + return defFormat; +} + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +inline TQTextFormat *TQTextStringChar::format() const +{ + return (type == Regular) ? d.format : d.custom->format; +} + + +#ifndef QT_NO_TEXTCUSTOMITEM +inline TQTextCustomItem *TQTextStringChar::customItem() const +{ + return isCustom() ? d.custom->custom : 0; +} +#endif + +inline int TQTextStringChar::height() const +{ +#ifndef QT_NO_TEXTCUSTOMITEM + return !isCustom() ? format()->height() : ( customItem()->placement() == TQTextCustomItem::PlaceInline ? customItem()->height : 0 ); +#else + return format()->height(); +#endif +} + +inline int TQTextStringChar::ascent() const +{ +#ifndef QT_NO_TEXTCUSTOMITEM + return !isCustom() ? format()->ascent() : ( customItem()->placement() == TQTextCustomItem::PlaceInline ? customItem()->ascent() : 0 ); +#else + return format()->ascent(); +#endif +} + +inline int TQTextStringChar::descent() const +{ +#ifndef QT_NO_TEXTCUSTOMITEM + return !isCustom() ? format()->descent() : 0; +#else + return format()->descent(); +#endif +} + +#endif //QT_NO_RICHTEXT + +#endif diff --git a/src/kernel/qscriptengine.cpp b/src/kernel/qscriptengine.cpp new file mode 100644 index 000000000..dd1f3105d --- /dev/null +++ b/src/kernel/qscriptengine.cpp @@ -0,0 +1,1624 @@ +/**************************************************************************** +** +** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qscriptengine_p.h" + +#include "qstring.h" +#include "qrect.h" +#include "qfont.h" +#include +#include "qtextengine_p.h" +#include "qfontengine_p.h" +#include + +#undef None +#undef Pre +#undef Above +#undef Below + +const int Prealloc = 256; +template +class TQVarLengthArray +{ +public: + inline explicit TQVarLengthArray(int size = 0); + inline ~TQVarLengthArray() { + if (ptr != reinterpret_cast(array)) + free(ptr); + } + + inline int size() const { return s; } + inline int count() const { return s; } + inline bool isEmpty() const { return (s == 0); } + inline void resize(int size); + inline void clear() { resize(0); } + + inline int capacity() const { return a; } + inline void reserve(int size); + + inline T &operator[](int idx) { + Q_ASSERT(idx >= 0 && idx < s); + return ptr[idx]; + } + inline const T &operator[](int idx) const { + Q_ASSERT(idx >= 0 && idx < s); + return ptr[idx]; + } + + inline void append(const T &t) { + const int idx = s; + resize(idx + 1); + ptr[idx] = t; + } + + inline T *data() { return ptr; } + inline const T *data() const { return ptr; } + inline const T * constData() const { return ptr; } + +private: + void realloc(int size, int alloc); + + int a; + int s; + T *ptr; + Q_UINT64 array[((Prealloc * sizeof(T)) / sizeof(Q_UINT64)) + 1]; +}; + +template +Q_INLINE_TEMPLATES TQVarLengthArray::TQVarLengthArray(int asize) + : s(asize) { + if (s > Prealloc) { + ptr = reinterpret_cast(malloc(s * sizeof(T))); + a = s; + } else { + ptr = reinterpret_cast(array); + a = Prealloc; + } +} + + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Basic processing +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +static inline void positionCluster(TQShaperItem *item, int gfrom, int glast) +{ + int nmarks = glast - gfrom; + if (nmarks <= 0) { + qWarning("positionCluster: no marks to position!"); + return; + } + + TQFontEngine *f = item->font; + + glyph_metrics_t baseInfo = f->boundingBox(item->glyphs[gfrom]); + + if (item->script == TQFont::Hebrew) + // we need to attach below the baseline, because of the hebrew iud. + baseInfo.height = TQMAX(baseInfo.height, -baseInfo.y); + + TQRect baseRect(baseInfo.x, baseInfo.y, baseInfo.width, baseInfo.height); + +// qDebug("---> positionCluster: cluster from %d to %d", gfrom, glast); +// qDebug("baseInfo: %f/%f (%f/%f) off=%f/%f", baseInfo.x, baseInfo.y, baseInfo.width, baseInfo.height, baseInfo.xoff, baseInfo.yoff); + + int size = (f->ascent()/10); + int offsetBase = (size - 4) / 4 + TQMIN(size, 4) + 1; +// qDebug("offset = %f", offsetBase); + + bool rightToLeft = item->flags & TQTextEngine::RightToLeft; + + int i; + unsigned char lastCmb = 0; + TQRect attachmentRect; + + for(i = 1; i <= nmarks; i++) { + glyph_t mark = item->glyphs[gfrom+i]; + TQPoint p; + glyph_metrics_t markInfo = f->boundingBox(mark); + TQRect markRect(markInfo.x, markInfo.y, markInfo.width, markInfo.height); +// qDebug("markInfo: %f/%f (%f/%f) off=%f/%f", markInfo.x, markInfo.y, markInfo.width, markInfo.height, markInfo.xoff, markInfo.yoff); + + int offset = offsetBase; + unsigned char cmb = item->attributes[gfrom+i].combiningClass; + + // ### maybe the whole position determination should move down to heuristicSetGlyphAttributes. Would save some + // bits in the glyphAttributes structure. + if (cmb < 200) { + // fixed position classes. We approximate by mapping to one of the others. + // currently I added only the ones for arabic, hebrew, lao and thai. + + // for Lao and Thai marks with class 0, see below (heuristicSetGlyphAttributes) + + // add a bit more offset to arabic, a bit hacky + if (cmb >= 27 && cmb <= 36 && offset < 3) + offset +=1; + // below + if ((cmb >= 10 && cmb <= 18) || + cmb == 20 || cmb == 22 || + cmb == 29 || cmb == 32) + cmb = TQChar::Combining_Below; + // above + else if (cmb == 23 || cmb == 27 || cmb == 28 || + cmb == 30 || cmb == 31 || (cmb >= 33 && cmb <= 36)) + cmb = TQChar::Combining_Above; + //below-right + else if (cmb == 9 || cmb == 103 || cmb == 118) + cmb = TQChar::Combining_BelowRight; + // above-right + else if (cmb == 24 || cmb == 107 || cmb == 122) + cmb = TQChar::Combining_AboveRight; + else if (cmb == 25) + cmb = TQChar::Combining_AboveLeft; + // fixed: + // 19 21 + + } + + // combining marks of different class don't interact. Reset the rectangle. + if (cmb != lastCmb) { + //qDebug("resetting rect"); + attachmentRect = baseRect; + } + + switch(cmb) { + case TQChar::Combining_DoubleBelow: + // ### wrong in rtl context! + case TQChar::Combining_BelowLeft: + p += TQPoint(0, offset); + case TQChar::Combining_BelowLeftAttached: + p += attachmentRect.bottomLeft() - markRect.topLeft(); + break; + case TQChar::Combining_Below: + p += TQPoint(0, offset); + case TQChar::Combining_BelowAttached: + p += attachmentRect.bottomLeft() - markRect.topLeft(); + p += TQPoint((attachmentRect.width() - markRect.width())/2 , 0); + break; + case TQChar::Combining_BelowRight: + p += TQPoint(0, offset); + case TQChar::Combining_BelowRightAttached: + p += attachmentRect.bottomRight() - markRect.topRight(); + break; + case TQChar::Combining_Left: + p += TQPoint(-offset, 0); + case TQChar::Combining_LeftAttached: + break; + case TQChar::Combining_Right: + p += TQPoint(offset, 0); + case TQChar::Combining_RightAttached: + break; + case TQChar::Combining_DoubleAbove: + // ### wrong in RTL context! + case TQChar::Combining_AboveLeft: + p += TQPoint(0, -offset); + case TQChar::Combining_AboveLeftAttached: + p += attachmentRect.topLeft() - markRect.bottomLeft(); + break; + case TQChar::Combining_Above: + p += TQPoint(0, -offset); + case TQChar::Combining_AboveAttached: + p += attachmentRect.topLeft() - markRect.bottomLeft(); + p += TQPoint((attachmentRect.width() - markRect.width())/2 , 0); + break; + case TQChar::Combining_AboveRight: + p += TQPoint(0, -offset); + case TQChar::Combining_AboveRightAttached: + p += attachmentRect.topRight() - markRect.bottomRight(); + break; + + case TQChar::Combining_IotaSubscript: + default: + break; + } +// qDebug("char=%x combiningClass = %d offset=%d/%d", mark, cmb, p.x(), p.y()); + markRect.moveBy(p.x(), p.y()); + attachmentRect |= markRect; + lastCmb = cmb; + if (rightToLeft) { + item->offsets[gfrom+i].x = p.x(); + item->offsets[gfrom+i].y = p.y(); + } else { + item->offsets[gfrom+i].x = p.x() - baseInfo.xoff; + item->offsets[gfrom+i].y = p.y() - baseInfo.yoff; + } + item->advances[gfrom+i] = 0; + } + item->has_positioning = TRUE; +} + + +void qt_heuristicPosition(TQShaperItem *item) +{ + int cEnd = -1; + int i = item->num_glyphs; + while (i--) { + if (cEnd == -1 && item->attributes[i].mark) { + cEnd = i; + } else if (cEnd != -1 && !item->attributes[i].mark) { + positionCluster(item, i, cEnd); + cEnd = -1; + } + } +} + + + +// set the glyph attributes heuristically. Assumes a 1 to 1 relationship between chars and glyphs +// and no reordering. +// also computes logClusters heuristically +static void heuristicSetGlyphAttributes(TQShaperItem *item, const TQChar *uc, int length) +{ + // justification is missing here!!!!! + + if ( item->num_glyphs != length ) + qWarning("TQScriptEngine::heuristicSetGlyphAttributes: char length and num glyphs disagree" ); + + unsigned short *logClusters = item->log_clusters; + + int i; + for (i = 0; i < length; ++i) + logClusters[i] = i; + + // first char in a run is never (treated as) a mark + int cStart = 0; + item->attributes[0].mark = FALSE; + item->attributes[0].clusterStart = TRUE; + item->attributes[0].combiningClass = 0; + if (qIsZeroWidthChar(uc[0].unicode())) { + item->attributes[0].zeroWidth = TRUE; + item->advances[0] = 0; + item->has_positioning = TRUE; + } else { + item->attributes[0].zeroWidth = FALSE; + } + + int lastCat = ::category(uc[0]); + for (i = 1; i < length; ++i) { + int cat = ::category(uc[i]); + if (qIsZeroWidthChar(uc[i].unicode())) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = TRUE; + item->attributes[i].zeroWidth = TRUE; + item->attributes[i].combiningClass = 0; + cStart = i; + item->advances[i] = 0; + item->has_positioning = TRUE; + } else if (cat != TQChar::Mark_NonSpacing) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = TRUE; + item->attributes[i].combiningClass = 0; + cStart = i; + } else { + int cmb = ::combiningClass(uc[i]); + + if (cmb == 0) { + // Fix 0 combining classes + if ((uc[i].unicode() & 0xff00) == 0x0e00) { + // thai or lao + unsigned char col = uc[i].cell(); + if (col == 0x31 || + col == 0x34 || + col == 0x35 || + col == 0x36 || + col == 0x37 || + col == 0x47 || + col == 0x4c || + col == 0x4d || + col == 0x4e) { + cmb = TQChar::Combining_AboveRight; + } else if (col == 0xb1 || + col == 0xb4 || + col == 0xb5 || + col == 0xb6 || + col == 0xb7 || + col == 0xbb || + col == 0xcc || + col == 0xcd) { + cmb = TQChar::Combining_Above; + } else if (col == 0xbc) { + cmb = TQChar::Combining_Below; + } + } + } + + item->attributes[i].mark = TRUE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].combiningClass = cmb; + logClusters[i] = cStart; + item->advances[i] = 0; + item->has_positioning = TRUE; + } + + if (lastCat == TQChar::Separator_Space) + item->attributes[i-1].justification = GlyphAttributes::Space; + else if (cat != TQChar::Mark_NonSpacing) + item->attributes[i-1].justification = GlyphAttributes::Character; + else + item->attributes[i-1].justification = GlyphAttributes::NoJustification; + + lastCat = cat; + } +} + +static void heuristicSetGlyphAttributes(TQShaperItem *item) +{ + heuristicSetGlyphAttributes(item, item->string->unicode() + item->from, item->length); +} + + +static bool basic_shape(TQShaperItem *item) +{ + if (item->font->stringToCMap(item->string->unicode()+item->from, item->length, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + + heuristicSetGlyphAttributes(item); + qt_heuristicPosition(item); + return TRUE; +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Middle eastern languages +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +// Uniscribe also defines dlig for Hebrew, but we leave this out for now, as it's mostly +// ligatures one does not want in modern Hebrew (as lam-alef ligatures). +enum { + CcmpProperty = 0x1 +}; +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) +static const TQOpenType::Features hebrew_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + {0, 0} +}; +#endif +/* Hebrew shaping. In the non opentype case we try to use the + presentation forms specified for Hebrew. Especially for the + ligatures with Dagesh this gives much better results than we could + achieve manually. +*/ +static bool hebrew_shape(TQShaperItem *item) +{ + Q_ASSERT(item->script == TQFont::Hebrew); + +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + TQOpenType *openType = item->font->openType(); + + if (openType && openType->supportsScript(item->script)) { + openType->selectScript(item->script, hebrew_features); + + if (item->font->stringToCMap(item->string->unicode()+item->from, item->length, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + + heuristicSetGlyphAttributes(item); + openType->shape(item); + return openType->positionAndAdd(item); + } +#endif + + enum { + Dagesh = 0x5bc, + ShinDot = 0x5c1, + SinDot = 0x5c2, + Patah = 0x5b7, + Qamats = 0x5b8, + Holam = 0x5b9, + Rafe = 0x5bf + }; + unsigned short chars[512]; + TQChar *shapedChars = item->length > 256 ? (TQChar *)::malloc(2*item->length * sizeof(TQChar)) : (TQChar *)chars; + + const TQChar *uc = item->string->unicode() + item->from; + unsigned short *logClusters = item->log_clusters; + + *shapedChars = *uc; + logClusters[0] = 0; + int slen = 1; + int cluster_start = 0; + int i; + for (i = 1; i < item->length; ++i) { + ushort base = shapedChars[slen-1].unicode(); + ushort shaped = 0; + bool invalid = FALSE; + if (uc[i].unicode() == Dagesh) { + if (base >= 0x5d0 + && base <= 0x5ea + && base != 0x5d7 + && base != 0x5dd + && base != 0x5df + && base != 0x5e2 + && base != 0x5e5) { + shaped = base - 0x5d0 + 0xfb30; + } else if (base == 0xfb2a || base == 0xfb2b /* Shin with Shin or Sin dot */) { + shaped = base + 2; + } else { + invalid = TRUE; + } + } else if (uc[i].unicode() == ShinDot) { + if (base == 0x05e9) + shaped = 0xfb2a; + else if (base == 0xfb49) + shaped = 0xfb2c; + else + invalid = TRUE; + } else if (uc[i].unicode() == SinDot) { + if (base == 0x05e9) + shaped = 0xfb2b; + else if (base == 0xfb49) + shaped = 0xfb2d; + else + invalid = TRUE; + } else if (uc[i].unicode() == Patah) { + if (base == 0x5d0) + shaped = 0xfb2e; + } else if (uc[i].unicode() == Qamats) { + if (base == 0x5d0) + shaped = 0xfb2f; + } else if (uc[i].unicode() == Holam) { + if (base == 0x5d5) + shaped = 0xfb4b; + } else if (uc[i].unicode() == Rafe) { + if (base == 0x5d1) + shaped = 0xfb4c; + else if (base == 0x5db) + shaped = 0xfb4d; + else if (base == 0x5e4) + shaped = 0xfb4e; + } + + if (invalid) { + shapedChars[slen] = 0x25cc; + item->attributes[slen].clusterStart = TRUE; + item->attributes[slen].mark = FALSE; + item->attributes[slen].combiningClass = 0; + cluster_start = slen; + ++slen; + } + if (shaped) { + if (item->font->canRender((TQChar *)&shaped, 1)) { + shapedChars[slen-1] = TQChar(shaped); + } else + shaped = 0; + } + if (!shaped) { + shapedChars[slen] = uc[i]; + if (::category(uc[i]) != TQChar::Mark_NonSpacing) { + item->attributes[slen].clusterStart = TRUE; + item->attributes[slen].mark = FALSE; + item->attributes[slen].combiningClass = 0; + cluster_start = slen; + } else { + item->attributes[slen].clusterStart = FALSE; + item->attributes[slen].mark = TRUE; + item->attributes[slen].combiningClass = ::combiningClass(uc[i]); + } + ++slen; + } + logClusters[i] = cluster_start; + } + + if (item->font->stringToCMap(shapedChars, slen, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + for (i = 0; i < item->num_glyphs; ++i) { + if (item->attributes[i].mark) + item->advances[i] = 0; + } + qt_heuristicPosition(item); + + if (item->length > 256) + ::free(shapedChars); + return TRUE; +} + +// these groups correspond to the groups defined in the Unicode standard. +// Some of these groups are equal whith regards to both joining and line breaking behaviour, +// and thus have the same enum value +// +// I'm not sure the mapping of syriac to arabic enums is correct with regards to justification, but as +// I couldn't find any better document I'll hope for the best. +enum ArabicGroup { + // NonJoining + ArabicNone, + ArabicSpace, + // Transparent + Transparent, + // Causing + Center, + Kashida, + + // Arabic + // Dual + Beh, + Noon, + Meem = Noon, + Heh = Noon, + KnottedHeh = Noon, + HehGoal = Noon, + SwashKaf = Noon, + Yeh, + Hah, + Seen, + Sad = Seen, + Tah, + Kaf = Tah, + Gaf = Tah, + Lam = Tah, + Ain, + Feh = Ain, + Qaf = Ain, + // Right + Alef, + Waw, + Dal, + TehMarbuta = Dal, + Reh, + HamzaOnHehGoal, + YehWithTail = HamzaOnHehGoal, + YehBarre = HamzaOnHehGoal, + + // Syriac + // Dual + Beth = Beh, + Gamal = Ain, + Heth = Noon, + Teth = Hah, + Yudh = Noon, + Kaph = Noon, + Lamadh = Lam, + Mim = Noon, + Nun = Noon, + Semakh = Noon, + FinalSemakh = Noon, + SyriacE = Ain, + Pe = Ain, + ReversedPe = Hah, + Qaph = Noon, + Shin = Noon, + Fe = Ain, + + // Right + Alaph = Alef, + Dalath = Dal, + He = Dal, + SyriacWaw = Waw, + Zain = Alef, + YudhHe = Waw, + Sadhe = HamzaOnHehGoal, + Taw = Dal, + + // Compiler bug? Otherwise ArabicGroupsEnd would be equal to Dal + 1. + Dummy = HamzaOnHehGoal, + ArabicGroupsEnd +}; + +static const unsigned char arabic_group[0x150] = { + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + ArabicNone, ArabicNone, Alef, Alef, + Waw, Alef, Yeh, Alef, + Beh, TehMarbuta, Beh, Beh, + Hah, Hah, Hah, Dal, + + Dal, Reh, Reh, Seen, + Seen, Sad, Sad, Tah, + Tah, Ain, Ain, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + // 0x640 + Kashida, Feh, Qaf, Kaf, + Lam, Meem, Noon, Heh, + Waw, Yeh, Yeh, Transparent, + Transparent, Transparent, Transparent, Transparent, + + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, Beh, Qaf, + + Transparent, Alef, Alef, Alef, + ArabicNone, Alef, Waw, Waw, + Yeh, Beh, Beh, Beh, + Beh, Beh, Beh, Beh, + + // 0x680 + Beh, Hah, Hah, Hah, + Hah, Hah, Hah, Hah, + Dal, Dal, Dal, Dal, + Dal, Dal, Dal, Dal, + + Dal, Reh, Reh, Reh, + Reh, Reh, Reh, Reh, + Reh, Reh, Seen, Seen, + Seen, Sad, Sad, Tah, + + Ain, Feh, Feh, Feh, + Feh, Feh, Feh, Qaf, + Qaf, Gaf, SwashKaf, Gaf, + Kaf, Kaf, Kaf, Gaf, + + Gaf, Gaf, Gaf, Gaf, + Gaf, Lam, Lam, Lam, + Lam, Noon, Noon, Noon, + Noon, Noon, KnottedHeh, Hah, + + // 0x6c0 + TehMarbuta, HehGoal, HamzaOnHehGoal, HamzaOnHehGoal, + Waw, Waw, Waw, Waw, + Waw, Waw, Waw, Waw, + Yeh, YehWithTail, Yeh, Waw, + + Yeh, Yeh, YehBarre, YehBarre, + ArabicNone, TehMarbuta, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, ArabicNone, ArabicNone, Transparent, + + Transparent, Transparent, Transparent, Transparent, + Transparent, ArabicNone, ArabicNone, Transparent, + Transparent, ArabicNone, Transparent, Transparent, + Transparent, Transparent, Dal, Reh, + + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, Seen, Sad, + Ain, ArabicNone, ArabicNone, KnottedHeh, + + // 0x700 + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + ArabicNone, ArabicNone, ArabicNone, ArabicNone, + + Alaph, Transparent, Beth, Gamal, + Gamal, Dalath, Dalath, He, + SyriacWaw, Zain, Heth, Teth, + Teth, Yudh, YudhHe, Kaph, + + Lamadh, Mim, Nun, Semakh, + FinalSemakh, SyriacE, Pe, ReversedPe, + Sadhe, Qaph, Dalath, Shin, + Taw, Beth, Gamal, Dalath, + + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, Transparent, + Transparent, Transparent, Transparent, ArabicNone, + ArabicNone, Zain, Kaph, Fe, +}; + +static inline ArabicGroup arabicGroup(unsigned short uc) +{ + if (uc >= 0x0600 && uc < 0x750) + return (ArabicGroup) arabic_group[uc-0x600]; + else if (uc == 0x200d) + return Center; + else if (::category(uc) == TQChar::Separator_Space) + return ArabicSpace; + else + return ArabicNone; +} + + +/* + Arabic shaping obeys a number of rules according to the joining classes (see Unicode book, section on + arabic). + + Each unicode char has a joining class (right, dual (left&right), center (joincausing) or transparent). + transparent joining is not encoded in TQChar::joining(), but applies to all combining marks and format marks. + + Right join-causing: dual + center + Left join-causing: dual + right + center + + Rules are as follows (for a string already in visual order, as we have it here): + + R1 Transparent characters do not affect joining behaviour. + R2 A right joining character, that has a right join-causing char on the right will get form XRight + (R3 A left joining character, that has a left join-causing char on the left will get form XLeft) + Note: the above rule is meaningless, as there are no pure left joining characters defined in Unicode + R4 A dual joining character, that has a left join-causing char on the left and a right join-causing char on + the right will get form XMedial + R5 A dual joining character, that has a right join causing char on the right, and no left join causing char on the left + will get form XRight + R6 A dual joining character, that has a left join causing char on the left, and no right join causing char on the right + will get form XLeft + R7 Otherwise the character will get form XIsolated + + Additionally we have to do the minimal ligature support for lam-alef ligatures: + + L1 Transparent characters do not affect ligature behaviour. + L2 Any sequence of Alef(XRight) + Lam(XMedial) will form the ligature Alef.Lam(XLeft) + L3 Any sequence of Alef(XRight) + Lam(XLeft) will form the ligature Alef.Lam(XIsolated) + + The state table below handles rules R1-R7. +*/ + +enum Shape { + XIsolated, + XFinal, + XInitial, + XMedial, + // intermediate state + XCausing +}; + + +enum Joining { + JNone, + JCausing, + JDual, + JRight, + JTransparent +}; + + +static const Joining joining_for_group[ArabicGroupsEnd] = { + // NonJoining + JNone, // ArabicNone + JNone, // ArabicSpace + // Transparent + JTransparent, // Transparent + // Causing + JCausing, // Center + JCausing, // Kashida + // Dual + JDual, // Beh + JDual, // Noon + JDual, // Yeh + JDual, // Hah + JDual, // Seen + JDual, // Tah + JDual, // Ain + // Right + JRight, // Alef + JRight, // Waw + JRight, // Dal + JRight, // Reh + JRight // HamzaOnHehGoal +}; + + +struct JoiningPair { + Shape form1; + Shape form2; +}; + +static const JoiningPair joining_table[5][4] = +// None, Causing, Dual, Right +{ + { { XIsolated, XIsolated }, { XIsolated, XCausing }, { XIsolated, XInitial }, { XIsolated, XIsolated } }, // XIsolated + { { XFinal, XIsolated }, { XFinal, XCausing }, { XFinal, XInitial }, { XFinal, XIsolated } }, // XFinal + { { XIsolated, XIsolated }, { XInitial, XCausing }, { XInitial, XMedial }, { XInitial, XFinal } }, // XInitial + { { XFinal, XIsolated }, { XMedial, XCausing }, { XMedial, XMedial }, { XMedial, XFinal } }, // XMedial + { { XIsolated, XIsolated }, { XIsolated, XCausing }, { XIsolated, XMedial }, { XIsolated, XFinal } }, // XCausing +}; + + +/* +According to http://www.microsoft.com/middleeast/Arabicdev/IE6/KBase.asp + +1. Find the priority of the connecting opportunities in each word +2. Add expansion at the highest priority connection opportunity +3. If more than one connection opportunity have the same highest value, + use the opportunity closest to the end of the word. + +Following is a chart that provides the priority for connection +opportunities and where expansion occurs. The character group names +are those in table 6.6 of the UNICODE 2.0 book. + + +PrioritY Glyph Condition Kashida Location + +Arabic_Kashida User inserted Kashida The user entered a Kashida in a position. After the user + (Shift+j or Shift+[E with hat]) Thus, it is the highest priority to insert an inserted kashida + automatic kashida. + +Arabic_Seen Seen, Sad Connecting to the next character. After the character. + (Initial or medial form). + +Arabic_HaaDal Teh Marbutah, Haa, Dal Connecting to previous character. Before the final form + of these characters. + +Arabic_Alef Alef, Tah, Lam, Connecting to previous character. Before the final form + Kaf and Gaf of these characters. + +Arabic_BaRa Reh, Yeh Connected to medial Beh Before preceding medial Baa + +Arabic_Waw Waw, Ain, Qaf, Feh Connecting to previous character. Before the final form of + these characters. + +Arabic_Normal Other connecting Connecting to previous character. Before the final form + characters of these characters. + + + +This seems to imply that we have at most one kashida point per arabic word. + +*/ + +struct TQArabicProperties { + unsigned char shape; + unsigned char justification; +}; + + +static void getArabicProperties(const unsigned short *chars, int len, TQArabicProperties *properties) +{ +// qDebug("arabicSyriacOpenTypeShape: properties:"); + int lastPos = 0; + int lastGroup = ArabicNone; + + ArabicGroup group = arabicGroup(chars[0]); + Joining j = joining_for_group[group]; + Shape shape = joining_table[XIsolated][j].form2; + properties[0].justification = GlyphAttributes::NoJustification; + + for (int i = 1; i < len; ++i) { + // #### fix handling for spaces and punktuation + properties[i].justification = GlyphAttributes::NoJustification; + + group = arabicGroup(chars[i]); + j = joining_for_group[group]; + + if (j == JTransparent) { + properties[i].shape = XIsolated; + continue; + } + + properties[lastPos].shape = joining_table[shape][j].form1; + shape = joining_table[shape][j].form2; + + switch(lastGroup) { + case Seen: + if (properties[lastPos].shape == XInitial || properties[lastPos].shape == XMedial) + properties[i-1].justification = GlyphAttributes::Arabic_Seen; + break; + case Hah: + if (properties[lastPos].shape == XFinal) + properties[lastPos-1].justification = GlyphAttributes::Arabic_HaaDal; + break; + case Alef: + if (properties[lastPos].shape == XFinal) + properties[lastPos-1].justification = GlyphAttributes::Arabic_Alef; + break; + case Ain: + if (properties[lastPos].shape == XFinal) + properties[lastPos-1].justification = GlyphAttributes::Arabic_Waw; + break; + case Noon: + if (properties[lastPos].shape == XFinal) + properties[lastPos-1].justification = GlyphAttributes::Arabic_Normal; + break; + case ArabicNone: + break; + + default: + Q_ASSERT(FALSE); + } + + lastGroup = ArabicNone; + + switch(group) { + case ArabicNone: + case Transparent: + // ### Center should probably be treated as transparent when it comes to justification. + case Center: + break; + case ArabicSpace: + properties[i].justification = GlyphAttributes::Arabic_Space; + break; + case Kashida: + properties[i].justification = GlyphAttributes::Arabic_Kashida; + break; + case Seen: + lastGroup = Seen; + break; + + case Hah: + case Dal: + lastGroup = Hah; + break; + + case Alef: + case Tah: + lastGroup = Alef; + break; + + case Yeh: + case Reh: + if (properties[lastPos].shape == XMedial && arabicGroup(chars[lastPos]) == Beh) + properties[lastPos-1].justification = GlyphAttributes::Arabic_BaRa; + break; + + case Ain: + case Waw: + lastGroup = Ain; + break; + + case Noon: + case Beh: + case HamzaOnHehGoal: + lastGroup = Noon; + break; + case ArabicGroupsEnd: + Q_ASSERT(FALSE); + } + + lastPos = i; + } + properties[lastPos].shape = joining_table[shape][JNone].form1; + + +// for (int i = 0; i < len; ++i) +// qDebug("arabic properties(%d): uc=%x shape=%d, justification=%d", i, chars[i], properties[i].shape, properties[i].justification); +} + + + + + + +// The unicode to unicode shaping codec. +// does only presentation forms B at the moment, but that should be enough for +// simple display +static const ushort arabicUnicodeMapping[256][2] = { + // base of shaped forms, and number-1 of them (0 for non shaping, + // 1 for right binding and 3 for dual binding + + // These are just the glyphs available in Unicode, + // some characters are in R class, but have no glyphs in Unicode. + + { 0x0600, 0 }, // 0x0600 + { 0x0601, 0 }, // 0x0601 + { 0x0602, 0 }, // 0x0602 + { 0x0603, 0 }, // 0x0603 + { 0x0604, 0 }, // 0x0604 + { 0x0605, 0 }, // 0x0605 + { 0x0606, 0 }, // 0x0606 + { 0x0607, 0 }, // 0x0607 + { 0x0608, 0 }, // 0x0608 + { 0x0609, 0 }, // 0x0609 + { 0x060A, 0 }, // 0x060A + { 0x060B, 0 }, // 0x060B + { 0x060C, 0 }, // 0x060C + { 0x060D, 0 }, // 0x060D + { 0x060E, 0 }, // 0x060E + { 0x060F, 0 }, // 0x060F + + { 0x0610, 0 }, // 0x0610 + { 0x0611, 0 }, // 0x0611 + { 0x0612, 0 }, // 0x0612 + { 0x0613, 0 }, // 0x0613 + { 0x0614, 0 }, // 0x0614 + { 0x0615, 0 }, // 0x0615 + { 0x0616, 0 }, // 0x0616 + { 0x0617, 0 }, // 0x0617 + { 0x0618, 0 }, // 0x0618 + { 0x0619, 0 }, // 0x0619 + { 0x061A, 0 }, // 0x061A + { 0x061B, 0 }, // 0x061B + { 0x061C, 0 }, // 0x061C + { 0x061D, 0 }, // 0x061D + { 0x061E, 0 }, // 0x061E + { 0x061F, 0 }, // 0x061F + + { 0x0620, 0 }, // 0x0620 + { 0xFE80, 0 }, // 0x0621 HAMZA + { 0xFE81, 1 }, // 0x0622 R ALEF WITH MADDA ABOVE + { 0xFE83, 1 }, // 0x0623 R ALEF WITH HAMZA ABOVE + { 0xFE85, 1 }, // 0x0624 R WAW WITH HAMZA ABOVE + { 0xFE87, 1 }, // 0x0625 R ALEF WITH HAMZA BELOW + { 0xFE89, 3 }, // 0x0626 D YEH WITH HAMZA ABOVE + { 0xFE8D, 1 }, // 0x0627 R ALEF + { 0xFE8F, 3 }, // 0x0628 D BEH + { 0xFE93, 1 }, // 0x0629 R TEH MARBUTA + { 0xFE95, 3 }, // 0x062A D TEH + { 0xFE99, 3 }, // 0x062B D THEH + { 0xFE9D, 3 }, // 0x062C D JEEM + { 0xFEA1, 3 }, // 0x062D D HAH + { 0xFEA5, 3 }, // 0x062E D KHAH + { 0xFEA9, 1 }, // 0x062F R DAL + + { 0xFEAB, 1 }, // 0x0630 R THAL + { 0xFEAD, 1 }, // 0x0631 R REH + { 0xFEAF, 1 }, // 0x0632 R ZAIN + { 0xFEB1, 3 }, // 0x0633 D SEEN + { 0xFEB5, 3 }, // 0x0634 D SHEEN + { 0xFEB9, 3 }, // 0x0635 D SAD + { 0xFEBD, 3 }, // 0x0636 D DAD + { 0xFEC1, 3 }, // 0x0637 D TAH + { 0xFEC5, 3 }, // 0x0638 D ZAH + { 0xFEC9, 3 }, // 0x0639 D AIN + { 0xFECD, 3 }, // 0x063A D GHAIN + { 0x063B, 0 }, // 0x063B + { 0x063C, 0 }, // 0x063C + { 0x063D, 0 }, // 0x063D + { 0x063E, 0 }, // 0x063E + { 0x063F, 0 }, // 0x063F + + { 0x0640, 0 }, // 0x0640 C TATWEEL // ### Join Causing, only one glyph + { 0xFED1, 3 }, // 0x0641 D FEH + { 0xFED5, 3 }, // 0x0642 D TQAF + { 0xFED9, 3 }, // 0x0643 D KAF + { 0xFEDD, 3 }, // 0x0644 D LAM + { 0xFEE1, 3 }, // 0x0645 D MEEM + { 0xFEE5, 3 }, // 0x0646 D NOON + { 0xFEE9, 3 }, // 0x0647 D HEH + { 0xFEED, 1 }, // 0x0648 R WAW + { 0x0649, 3 }, // 0x0649 ALEF MAKSURA // ### Dual, glyphs not consecutive, handle in code. + { 0xFEF1, 3 }, // 0x064A D YEH + { 0x064B, 0 }, // 0x064B + { 0x064C, 0 }, // 0x064C + { 0x064D, 0 }, // 0x064D + { 0x064E, 0 }, // 0x064E + { 0x064F, 0 }, // 0x064F + + { 0x0650, 0 }, // 0x0650 + { 0x0651, 0 }, // 0x0651 + { 0x0652, 0 }, // 0x0652 + { 0x0653, 0 }, // 0x0653 + { 0x0654, 0 }, // 0x0654 + { 0x0655, 0 }, // 0x0655 + { 0x0656, 0 }, // 0x0656 + { 0x0657, 0 }, // 0x0657 + { 0x0658, 0 }, // 0x0658 + { 0x0659, 0 }, // 0x0659 + { 0x065A, 0 }, // 0x065A + { 0x065B, 0 }, // 0x065B + { 0x065C, 0 }, // 0x065C + { 0x065D, 0 }, // 0x065D + { 0x065E, 0 }, // 0x065E + { 0x065F, 0 }, // 0x065F + + { 0x0660, 0 }, // 0x0660 + { 0x0661, 0 }, // 0x0661 + { 0x0662, 0 }, // 0x0662 + { 0x0663, 0 }, // 0x0663 + { 0x0664, 0 }, // 0x0664 + { 0x0665, 0 }, // 0x0665 + { 0x0666, 0 }, // 0x0666 + { 0x0667, 0 }, // 0x0667 + { 0x0668, 0 }, // 0x0668 + { 0x0669, 0 }, // 0x0669 + { 0x066A, 0 }, // 0x066A + { 0x066B, 0 }, // 0x066B + { 0x066C, 0 }, // 0x066C + { 0x066D, 0 }, // 0x066D + { 0x066E, 0 }, // 0x066E + { 0x066F, 0 }, // 0x066F + + { 0x0670, 0 }, // 0x0670 + { 0xFB50, 1 }, // 0x0671 R ALEF WASLA + { 0x0672, 0 }, // 0x0672 + { 0x0673, 0 }, // 0x0673 + { 0x0674, 0 }, // 0x0674 + { 0x0675, 0 }, // 0x0675 + { 0x0676, 0 }, // 0x0676 + { 0x0677, 0 }, // 0x0677 + { 0x0678, 0 }, // 0x0678 + { 0xFB66, 3 }, // 0x0679 D TTEH + { 0xFB5E, 3 }, // 0x067A D TTEHEH + { 0xFB52, 3 }, // 0x067B D BEEH + { 0x067C, 0 }, // 0x067C + { 0x067D, 0 }, // 0x067D + { 0xFB56, 3 }, // 0x067E D PEH + { 0xFB62, 3 }, // 0x067F D TEHEH + + { 0xFB5A, 3 }, // 0x0680 D BEHEH + { 0x0681, 0 }, // 0x0681 + { 0x0682, 0 }, // 0x0682 + { 0xFB76, 3 }, // 0x0683 D NYEH + { 0xFB72, 3 }, // 0x0684 D DYEH + { 0x0685, 0 }, // 0x0685 + { 0xFB7A, 3 }, // 0x0686 D TCHEH + { 0xFB7E, 3 }, // 0x0687 D TCHEHEH + { 0xFB88, 1 }, // 0x0688 R DDAL + { 0x0689, 0 }, // 0x0689 + { 0x068A, 0 }, // 0x068A + { 0x068B, 0 }, // 0x068B + { 0xFB84, 1 }, // 0x068C R DAHAL + { 0xFB82, 1 }, // 0x068D R DDAHAL + { 0xFB86, 1 }, // 0x068E R DUL + { 0x068F, 0 }, // 0x068F + + { 0x0690, 0 }, // 0x0690 + { 0xFB8C, 1 }, // 0x0691 R RREH + { 0x0692, 0 }, // 0x0692 + { 0x0693, 0 }, // 0x0693 + { 0x0694, 0 }, // 0x0694 + { 0x0695, 0 }, // 0x0695 + { 0x0696, 0 }, // 0x0696 + { 0x0697, 0 }, // 0x0697 + { 0xFB8A, 1 }, // 0x0698 R JEH + { 0x0699, 0 }, // 0x0699 + { 0x069A, 0 }, // 0x069A + { 0x069B, 0 }, // 0x069B + { 0x069C, 0 }, // 0x069C + { 0x069D, 0 }, // 0x069D + { 0x069E, 0 }, // 0x069E + { 0x069F, 0 }, // 0x069F + + { 0x06A0, 0 }, // 0x06A0 + { 0x06A1, 0 }, // 0x06A1 + { 0x06A2, 0 }, // 0x06A2 + { 0x06A3, 0 }, // 0x06A3 + { 0xFB6A, 3 }, // 0x06A4 D VEH + { 0x06A5, 0 }, // 0x06A5 + { 0xFB6E, 3 }, // 0x06A6 D PEHEH + { 0x06A7, 0 }, // 0x06A7 + { 0x06A8, 0 }, // 0x06A8 + { 0xFB8E, 3 }, // 0x06A9 D KEHEH + { 0x06AA, 0 }, // 0x06AA + { 0x06AB, 0 }, // 0x06AB + { 0x06AC, 0 }, // 0x06AC + { 0xFBD3, 3 }, // 0x06AD D NG + { 0x06AE, 0 }, // 0x06AE + { 0xFB92, 3 }, // 0x06AF D GAF + + { 0x06B0, 0 }, // 0x06B0 + { 0xFB9A, 3 }, // 0x06B1 D NGOEH + { 0x06B2, 0 }, // 0x06B2 + { 0xFB96, 3 }, // 0x06B3 D GUEH + { 0x06B4, 0 }, // 0x06B4 + { 0x06B5, 0 }, // 0x06B5 + { 0x06B6, 0 }, // 0x06B6 + { 0x06B7, 0 }, // 0x06B7 + { 0x06B8, 0 }, // 0x06B8 + { 0x06B9, 0 }, // 0x06B9 + { 0xFB9E, 1 }, // 0x06BA R NOON GHUNNA + { 0xFBA0, 3 }, // 0x06BB D RNOON + { 0x06BC, 0 }, // 0x06BC + { 0x06BD, 0 }, // 0x06BD + { 0xFBAA, 3 }, // 0x06BE D HEH DOACHASHMEE + { 0x06BF, 0 }, // 0x06BF + + { 0xFBA4, 1 }, // 0x06C0 R HEH WITH YEH ABOVE + { 0xFBA6, 3 }, // 0x06C1 D HEH GOAL + { 0x06C2, 0 }, // 0x06C2 + { 0x06C3, 0 }, // 0x06C3 + { 0x06C4, 0 }, // 0x06C4 + { 0xFBE0, 1 }, // 0x06C5 R KIRGHIZ OE + { 0xFBD9, 1 }, // 0x06C6 R OE + { 0xFBD7, 1 }, // 0x06C7 R U + { 0xFBDB, 1 }, // 0x06C8 R YU + { 0xFBE2, 1 }, // 0x06C9 R KIRGHIZ YU + { 0x06CA, 0 }, // 0x06CA + { 0xFBDE, 1 }, // 0x06CB R VE + { 0xFBFC, 3 }, // 0x06CC D FARSI YEH + { 0x06CD, 0 }, // 0x06CD + { 0x06CE, 0 }, // 0x06CE + { 0x06CF, 0 }, // 0x06CF + + { 0xFBE4, 3 }, // 0x06D0 D E + { 0x06D1, 0 }, // 0x06D1 + { 0xFBAE, 1 }, // 0x06D2 R YEH BARREE + { 0xFBB0, 1 }, // 0x06D3 R YEH BARREE WITH HAMZA ABOVE + { 0x06D4, 0 }, // 0x06D4 + { 0x06D5, 0 }, // 0x06D5 + { 0x06D6, 0 }, // 0x06D6 + { 0x06D7, 0 }, // 0x06D7 + { 0x06D8, 0 }, // 0x06D8 + { 0x06D9, 0 }, // 0x06D9 + { 0x06DA, 0 }, // 0x06DA + { 0x06DB, 0 }, // 0x06DB + { 0x06DC, 0 }, // 0x06DC + { 0x06DD, 0 }, // 0x06DD + { 0x06DE, 0 }, // 0x06DE + { 0x06DF, 0 }, // 0x06DF + + { 0x06E0, 0 }, // 0x06E0 + { 0x06E1, 0 }, // 0x06E1 + { 0x06E2, 0 }, // 0x06E2 + { 0x06E3, 0 }, // 0x06E3 + { 0x06E4, 0 }, // 0x06E4 + { 0x06E5, 0 }, // 0x06E5 + { 0x06E6, 0 }, // 0x06E6 + { 0x06E7, 0 }, // 0x06E7 + { 0x06E8, 0 }, // 0x06E8 + { 0x06E9, 0 }, // 0x06E9 + { 0x06EA, 0 }, // 0x06EA + { 0x06EB, 0 }, // 0x06EB + { 0x06EC, 0 }, // 0x06EC + { 0x06ED, 0 }, // 0x06ED + { 0x06EE, 0 }, // 0x06EE + { 0x06EF, 0 }, // 0x06EF + + { 0x06F0, 0 }, // 0x06F0 + { 0x06F1, 0 }, // 0x06F1 + { 0x06F2, 0 }, // 0x06F2 + { 0x06F3, 0 }, // 0x06F3 + { 0x06F4, 0 }, // 0x06F4 + { 0x06F5, 0 }, // 0x06F5 + { 0x06F6, 0 }, // 0x06F6 + { 0x06F7, 0 }, // 0x06F7 + { 0x06F8, 0 }, // 0x06F8 + { 0x06F9, 0 }, // 0x06F9 + { 0x06FA, 0 }, // 0x06FA + { 0x06FB, 0 }, // 0x06FB + { 0x06FC, 0 }, // 0x06FC + { 0x06FD, 0 }, // 0x06FD + { 0x06FE, 0 }, // 0x06FE + { 0x06FF, 0 } // 0x06FF +}; + +// the arabicUnicodeMapping does not work for U+0649 ALEF MAKSURA, this table does +static const ushort alefMaksura[4] = {0xFEEF, 0xFEF0, 0xFBE8, 0xFBE9}; + +// this is a bit tricky. Alef always binds to the right, so the second parameter descibing the shape +// of the lam can be either initial of medial. So initial maps to the isolated form of the ligature, +// medial to the final form +static const ushort arabicUnicodeLamAlefMapping[6][4] = { + { 0xfffd, 0xfffd, 0xfef5, 0xfef6 }, // 0x622 R Alef with Madda above + { 0xfffd, 0xfffd, 0xfef7, 0xfef8 }, // 0x623 R Alef with Hamza above + { 0xfffd, 0xfffd, 0xfffd, 0xfffd }, // 0x624 // Just to fill the table ;-) + { 0xfffd, 0xfffd, 0xfef9, 0xfefa }, // 0x625 R Alef with Hamza below + { 0xfffd, 0xfffd, 0xfffd, 0xfffd }, // 0x626 // Just to fill the table ;-) + { 0xfffd, 0xfffd, 0xfefb, 0xfefc } // 0x627 R Alef +}; + +static inline int getShape(uchar cell, int shape) +{ + // the arabicUnicodeMapping does not work for U+0649 ALEF MAKSURA, handle this here + uint ch = (cell != 0x49) + ? (shape ? arabicUnicodeMapping[cell][0] + shape : 0x600+cell) + : alefMaksura[shape] ; + return ch; +} + + +/* + Two small helper functions for arabic shaping. +*/ +static inline const TQChar prevChar(const TQString *str, int pos) +{ + //qDebug("leftChar: pos=%d", pos); + pos--; + const TQChar *ch = str->unicode() + pos; + while(pos > -1) { + if(::category(*ch) != TQChar::Mark_NonSpacing) + return *ch; + pos--; + ch--; + } + return TQChar::replacement; +} + +static inline const TQChar nextChar(const TQString *str, int pos) +{ + pos++; + int len = str->length(); + const TQChar *ch = str->unicode() + pos; + while(pos < len) { + //qDebug("rightChar: %d isLetter=%d, joining=%d", pos, ch.isLetter(), ch.joining()); + if(::category(*ch) != TQChar::Mark_NonSpacing) + return *ch; + // assume it's a transparent char, this might not be 100% correct + pos++; + ch++; + } + return TQChar::replacement; +} + + +static void shapedString(const TQString *uc, int from, int len, TQChar *shapeBuffer, int *shapedLength, + bool reverse, GlyphAttributes *attributes, unsigned short *logClusters) +{ + Q_ASSERT((int)uc->length() >= from + len); + + if(len == 0) { + *shapedLength = 0; + return; + } + + TQVarLengthArray props(len + 2); + TQArabicProperties *properties = props.data(); + int f = from; + int l = len; + if (from > 0) { + --f; + ++l; + ++properties; + } + if (f + l < (int)uc->length()) { + ++l; + } + getArabicProperties((const unsigned short *)(uc->unicode()+f), l, props.data()); + + const TQChar *ch = uc->unicode() + from; + TQChar *data = shapeBuffer; + int clusterStart = 0; + + for (int i = 0; i < len; i++) { + uchar r = ch->row(); + int gpos = data - shapeBuffer; + + if (r != 0x06) { + if (r == 0x20) { + uchar c = ch->cell(); + if (c == 0x0c || c == 0x0d) + // remove ZWJ and ZWNJ + goto skip; + } + if (reverse) + *data = mirroredChar(*ch); + else + *data = *ch; + } else { + uchar c = ch->cell(); + int pos = i + from; + int shape = properties[i].shape; +// qDebug("mapping U+%x to shape %d glyph=0x%x", ch->unicode(), shape, getShape(c, shape)); + // take care of lam-alef ligatures (lam right of alef) + ushort map; + switch (c) { + case 0x44: { // lam + const TQChar pch = nextChar(uc, pos); + if (pch.row() == 0x06) { + switch (pch.cell()) { + case 0x22: + case 0x23: + case 0x25: + case 0x27: +// qDebug(" lam of lam-alef ligature"); + map = arabicUnicodeLamAlefMapping[pch.cell() - 0x22][shape]; + goto next; + default: + break; + } + } + break; + } + case 0x22: // alef with madda + case 0x23: // alef with hamza above + case 0x25: // alef with hamza below + case 0x27: // alef + if (prevChar(uc, pos).unicode() == 0x0644) { + // have a lam alef ligature + //qDebug(" alef of lam-alef ligature"); + goto skip; + } + default: + break; + } + map = getShape(c, shape); + next: + *data = map; + } + // ##### Fixme + //attributes[gpos].zeroWidth = zeroWidth; + if (::category(*ch) == TQChar::Mark_NonSpacing) { + attributes[gpos].mark = TRUE; +// qDebug("glyph %d (char %d) is mark!", gpos, i); + } else { + attributes[gpos].mark = FALSE; + clusterStart = data - shapeBuffer; + } + attributes[gpos].clusterStart = !attributes[gpos].mark; + attributes[gpos].combiningClass = combiningClass(*ch); + attributes[gpos].justification = properties[i].justification; +// qDebug("data[%d] = %x (from %x)", gpos, (uint)data->unicode(), ch->unicode()); + data++; + skip: + ch++; + logClusters[i] = clusterStart; + } + *shapedLength = data - shapeBuffer; +} + +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + +enum { + InitProperty = 0x2, + IsolProperty = 0x4, + FinaProperty = 0x8, + MediProperty = 0x10, + RligProperty = 0x20, + CaltProperty = 0x40, + LigaProperty = 0x80, + DligProperty = 0x100, + CswhProperty = 0x200, + MsetProperty = 0x400 +}; + +static const TQOpenType::Features arabic_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('i', 's', 'o', 'l'), IsolProperty }, + { FT_MAKE_TAG('f', 'i', 'n', 'a'), FinaProperty }, + { FT_MAKE_TAG('m', 'e', 'd', 'i'), MediProperty }, + { FT_MAKE_TAG('i', 'n', 'i', 't'), InitProperty }, + { FT_MAKE_TAG('r', 'l', 'i', 'g'), RligProperty }, + { FT_MAKE_TAG('c', 'a', 'l', 't'), CaltProperty }, + { FT_MAKE_TAG('l', 'i', 'g', 'a'), LigaProperty }, + { FT_MAKE_TAG('d', 'l', 'i', 'g'), DligProperty }, + { FT_MAKE_TAG('c', 's', 'w', 'h'), CswhProperty }, + // mset is used in old Win95 fonts that don't have a 'mark' positioning table. + { FT_MAKE_TAG('m', 's', 'e', 't'), MsetProperty }, + {0, 0} +}; + +static const TQOpenType::Features syriac_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('i', 's', 'o', 'l'), IsolProperty }, + { FT_MAKE_TAG('f', 'i', 'n', 'a'), FinaProperty }, + { FT_MAKE_TAG('f', 'i', 'n', '2'), FinaProperty }, + { FT_MAKE_TAG('f', 'i', 'n', '3'), FinaProperty }, + { FT_MAKE_TAG('m', 'e', 'd', 'i'), MediProperty }, + { FT_MAKE_TAG('m', 'e', 'd', '2'), MediProperty }, + { FT_MAKE_TAG('i', 'n', 'i', 't'), InitProperty }, + { FT_MAKE_TAG('r', 'l', 'i', 'g'), RligProperty }, + { FT_MAKE_TAG('c', 'a', 'l', 't'), CaltProperty }, + { FT_MAKE_TAG('l', 'i', 'g', 'a'), LigaProperty }, + { FT_MAKE_TAG('d', 'l', 'i', 'g'), DligProperty }, + {0, 0} +}; + +static bool arabicSyriacOpenTypeShape(TQOpenType *openType, TQShaperItem *item, bool *ot_ok) +{ + *ot_ok = true; + + openType->selectScript(item->script, item->script == TQFont::Arabic ? arabic_features : syriac_features); + int nglyphs = item->num_glyphs; + if (item->font->stringToCMap(item->string->unicode()+item->from, item->length, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + heuristicSetGlyphAttributes(item); + + unsigned short *logClusters = item->log_clusters; + const unsigned short *uc = (const unsigned short *)item->string->unicode() + item->from; + + TQVarLengthArray props(item->length+2); + TQArabicProperties *properties = props.data(); + int f = 0; + int l = item->length; + if (item->from > 0) { + --f; + ++l; + ++properties; + } + if (f + l < (int)item->string->length()) { + ++l; + } + getArabicProperties((const unsigned short *)(uc+f), l, props.data()); + + TQVarLengthArray apply(item->num_glyphs); + + + // Hack to remove ZWJ and ZWNJ from rendered output. + int j = 0; + for (int i = 0; i < item->num_glyphs; i++) { + if (uc[i] == 0x200c || uc[i] == 0x200d) + continue; + item->glyphs[j] = item->glyphs[i]; + item->attributes[j] = item->attributes[i]; + item->advances[j] = item->advances[i]; + item->offsets[j] = item->offsets[i]; + properties[j] = properties[i]; + item->attributes[j].justification = properties[i].justification; + logClusters[i] = logClusters[j]; + ++j; + } + item->num_glyphs = j; + + for (int i = 0; i < item->num_glyphs; i++) { + apply[i] = 0; + + if (properties[i].shape == XIsolated) + apply[i] |= MediProperty|FinaProperty|InitProperty; + else if (properties[i].shape == XMedial) + apply[i] |= IsolProperty|FinaProperty|InitProperty; + else if (properties[i].shape == XFinal) + apply[i] |= IsolProperty|MediProperty|InitProperty; + else if (properties[i].shape == XInitial) + apply[i] |= IsolProperty|MediProperty|FinaProperty; + } + + if (!openType->shape(item, apply.data())) { + *ot_ok = false; + return false; + } + item->num_glyphs = nglyphs; + return openType->positionAndAdd(item); +} + +#endif + +// #### stil missing: identify invalid character combinations +static bool arabic_shape(TQShaperItem *item) +{ + Q_ASSERT(item->script == TQFont::Arabic); + +#if defined(Q_WS_X11) && !defined(QT_NO_XFTFREETYPE) + TQOpenType *openType = item->font->openType(); + + if (openType && openType->supportsScript(TQFont::Arabic)) { + bool ot_ok; + if (arabicSyriacOpenTypeShape(openType, item, &ot_ok)) + return true; + if (ot_ok) + return false; + // fall through to the non OT code + } +#endif + + TQVarLengthArray shapedChars(item->length); + + int slen; + shapedString(item->string, item->from, item->length, (TQChar *)shapedChars.data(), &slen, + item->flags & TQTextEngine::RightToLeft, + item->attributes, item->log_clusters); + + if (item->font->stringToCMap((TQChar *)shapedChars.data(), slen, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + + for (int i = 0; i < slen; ++i) + if (item->attributes[i].mark) + item->advances[i] = 0; + qt_heuristicPosition(item); + return TRUE; +} + +#if defined(Q_WS_X11) +# include "qscriptengine_x11.cpp" +#elif defined(Q_WS_WIN) +# include "qscriptengine_win.cpp" +#elif defined(Q_WS_MAC) +# include "qscriptengine_mac.cpp" +#elif defined(Q_WS_QWS) +# include "qscriptengine_qws.cpp" +#endif diff --git a/src/kernel/qscriptengine_p.h b/src/kernel/qscriptengine_p.h new file mode 100644 index 000000000..93464b00c --- /dev/null +++ b/src/kernel/qscriptengine_p.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the $MODULE$ of the TQt Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +****************************************************************************/ + +#ifndef TQSCRIPTENGINE_P_H +#define TQSCRIPTENGINE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qtextengine_p.h" + +class TQString; +struct TQCharAttributes; + +struct TQShaperItem { + int script; + const TQString *string; + int from; + int length; + TQFontEngine *font; + glyph_t *glyphs; + advance_t *advances; + qoffset_t *offsets; + GlyphAttributes *attributes; + int num_glyphs; // in: available glyphs out: glyphs used/needed + unsigned short *log_clusters; + int flags; + bool has_positioning; +}; + +// return true if ok. +typedef bool (*ShapeFunction)(TQShaperItem *item); +typedef void (*AttributeFunction)(int script, const TQString &, int, int, TQCharAttributes *); + +struct q_scriptEngine { + ShapeFunction shape; + AttributeFunction charAttributes; +}; + +extern const q_scriptEngine scriptEngines[]; + +#endif // TQSCRIPTENGINE_P_H diff --git a/src/kernel/qscriptengine_x11.cpp b/src/kernel/qscriptengine_x11.cpp new file mode 100644 index 000000000..20b01dc5b --- /dev/null +++ b/src/kernel/qscriptengine_x11.cpp @@ -0,0 +1,3752 @@ +/**************************************************************************** +** +** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +// ------------------------------------------------------------------------------------------------------------------ +// +// Continuation of middle eastern languages +// +// ------------------------------------------------------------------------------------------------------------------ + +// #### stil missing: identify invalid character combinations +static bool syriac_shape(TQShaperItem *item) +{ + Q_ASSERT(item->script == TQFont::Syriac); + +#ifndef QT_NO_XFTFREETYPE + TQOpenType *openType = item->font->openType(); + if (openType && openType->supportsScript(TQFont::Syriac)) { + bool ot_ok; + if (arabicSyriacOpenTypeShape(openType, item, &ot_ok)) + return true; + if (ot_ok) + return false; + // fall through to the non OT code + } +#endif + return basic_shape(item); +} + + +static bool thaana_shape(TQShaperItem *item) +{ + Q_ASSERT(item->script == TQFont::Thaana); + +#ifndef QT_NO_XFTFREETYPE + TQOpenType *openType = item->font->openType(); + + if (openType && openType->supportsScript(item->script)) { + openType->selectScript(TQFont::Thaana); + if (item->font->stringToCMap(item->string->unicode()+item->from, item->length, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + heuristicSetGlyphAttributes(item); + openType->shape(item); + return openType->positionAndAdd(item); + } +#endif + return basic_shape(item); +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Indic languages +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +enum Form { + Invalid = 0x0, + Unknown = Invalid, + Consonant, + Nukta, + Halant, + Matra, + VowelMark, + StressMark, + IndependentVowel, + LengthMark, + Control, + Other +}; + +static const unsigned char indicForms[0xe00-0x900] = { + // Devangari + Invalid, VowelMark, VowelMark, VowelMark, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Matra, Matra, Matra, + Matra, Matra, Matra, Matra, + Matra, Halant, Unknown, Unknown, + + Other, StressMark, StressMark, StressMark, + StressMark, Unknown, Unknown, Unknown, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + IndependentVowel, IndependentVowel, VowelMark, VowelMark, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Consonant, + Consonant, Consonant /* ??? */, Consonant, Consonant, + + // Bengali + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, Invalid, IndependentVowel, + + IndependentVowel, Invalid, Invalid, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Invalid, Consonant, Invalid, + Invalid, Invalid, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Invalid, Invalid, Matra, + Matra, Invalid, Invalid, Matra, + Matra, Halant, Consonant, Unknown, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, VowelMark, + Invalid, Invalid, Invalid, Invalid, + Consonant, Consonant, Invalid, Consonant, + + IndependentVowel, IndependentVowel, VowelMark, VowelMark, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Consonant, Consonant, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Gurmukhi + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, Invalid, + Invalid, Invalid, Invalid, IndependentVowel, + + IndependentVowel, Invalid, Invalid, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Invalid, Consonant, Consonant, + Invalid, Consonant, Consonant, Invalid, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Invalid, + Invalid, Invalid, Invalid, Matra, + Matra, Invalid, Invalid, Matra, + Matra, Halant, Unknown, Unknown, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Unknown, Unknown, Unknown, + Invalid, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Invalid, + + Other, Other, Invalid, Invalid, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + StressMark, StressMark, Consonant, Consonant, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Gujarati + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, Invalid, IndependentVowel, + + IndependentVowel, IndependentVowel, Invalid, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Invalid, Consonant, Consonant, + Invalid, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Matra, Invalid, Matra, + Matra, Matra, Invalid, Matra, + Matra, Halant, Unknown, Unknown, + + Other, Unknown, Unknown, Unknown, + Unknown, Unknown, Unknown, Unknown, + Unknown, Unknown, Unknown, Unknown, + Unknown, Unknown, Unknown, Unknown, + + IndependentVowel, IndependentVowel, VowelMark, VowelMark, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Oriya + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, Invalid, IndependentVowel, + + IndependentVowel, Invalid, Invalid, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Invalid, Consonant, Consonant, + Invalid, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Invalid, Invalid, Invalid, Matra, + Matra, Invalid, Invalid, Matra, + Matra, Halant, Unknown, Unknown, + + Other, Invalid, Invalid, Invalid, + Invalid, Unknown, LengthMark, LengthMark, + Invalid, Invalid, Invalid, Invalid, + Consonant, Consonant, Invalid, Consonant, + + IndependentVowel, IndependentVowel, Invalid, Invalid, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Consonant, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + //Tamil + Invalid, Invalid, VowelMark, Other, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, Invalid, + Invalid, Invalid, IndependentVowel, IndependentVowel, + + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Invalid, Invalid, + Invalid, Consonant, Consonant, Invalid, + Consonant, Invalid, Consonant, Consonant, + + Invalid, Invalid, Invalid, Consonant, + Consonant, Invalid, Invalid, Invalid, + Consonant, Consonant, Consonant, Invalid, + Invalid, Invalid, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Invalid, Invalid, Matra, Matra, + + Matra, Matra, Matra, Invalid, + Invalid, Invalid, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Halant, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, LengthMark, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Telugu + Invalid, VowelMark, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Invalid, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Invalid, Invalid, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Halant, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, LengthMark, Matra, Invalid, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + + IndependentVowel, IndependentVowel, Invalid, Invalid, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Kannada + Invalid, Invalid, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Invalid, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Nukta, Other, Matra, Matra, + + Matra, Matra, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Halant, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, LengthMark, LengthMark, Invalid, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Consonant, Invalid, + + IndependentVowel, IndependentVowel, VowelMark, VowelMark, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Malayalam + Invalid, Invalid, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + + IndependentVowel, Invalid, IndependentVowel, IndependentVowel, + IndependentVowel, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Unknown, Unknown, + Invalid, Invalid, Matra, Matra, + + Matra, Matra, Matra, Matra, + Invalid, Invalid, Matra, Matra, + Matra, Invalid, Matra, Matra, + Matra, Halant, Invalid, Invalid, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, LengthMark, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + + IndependentVowel, IndependentVowel, Invalid, Invalid, + Invalid, Invalid, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, + + // Sinhala + Invalid, Invalid, VowelMark, VowelMark, + Invalid, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + + IndependentVowel, IndependentVowel, IndependentVowel, IndependentVowel, + IndependentVowel, IndependentVowel, IndependentVowel, Invalid, + Invalid, Invalid, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + + Consonant, Consonant, Invalid, Consonant, + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Consonant, + Invalid, Consonant, Invalid, Invalid, + + Consonant, Consonant, Consonant, Consonant, + Consonant, Consonant, Consonant, Invalid, + Invalid, Invalid, Halant, Invalid, + Invalid, Invalid, Invalid, Matra, + + Matra, Matra, Matra, Matra, + Matra, Invalid, Matra, Invalid, + Matra, Matra, Matra, Matra, + Matra, Matra, Matra, Matra, + + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + Invalid, Invalid, Invalid, Invalid, + + Invalid, Invalid, Matra, Matra, + Other, Other, Other, Other, + Other, Other, Other, Other, + Other, Other, Other, Other, +}; + +enum Position { + None, + Pre, + Above, + Below, + Post, + Split, + Base, + Reph, + Vattu, + Inherit +}; + +static const unsigned char indicPosition[0xe00-0x900] = { + // Devanagari + None, Above, Above, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + Below, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, Post, Pre, + + Post, Below, Below, Below, + Below, Above, Above, Above, + Above, Post, Post, Post, + Post, None, None, None, + + None, Above, Below, Above, + Above, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Bengali + None, Above, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + Below, None, None, Post, + + Below, None, None, None, + None, None, None, None, + None, None, None, None, + Below, None, Post, Pre, + + Post, Below, Below, Below, + Below, None, None, Pre, + Pre, None, None, Split, + Split, Below, None, None, + + None, None, None, None, + None, None, None, Post, + None, None, None, None, + None, None, None, None, + + None, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Gurmukhi + None, Above, Above, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, Post, + + Below, None, None, None, + None, Below, None, None, + None, Below, None, None, + Below, None, Post, Pre, + + Post, Below, Below, None, + None, None, None, Above, + Above, None, None, Above, + Above, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + Above, Above, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Gujarati + None, Above, Above, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + Below, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, Post, Pre, + + Post, Below, Below, Below, + Below, Above, None, Above, + Above, Post, None, Post, + Post, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Oriya + None, Above, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + Below, None, None, None, + Below, None, None, None, + Below, Below, Below, Post, + + Below, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, Post, Above, + + Post, Below, Below, Below, + None, None, None, Pre, + Split, None, None, Split, + Split, None, None, None, + + None, None, None, None, + None, None, Above, Post, + None, None, None, None, + None, None, None, Post, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, Below, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Tamil + None, None, Above, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, Post, Post, + + Above, Below, Below, None, + None, None, Pre, Pre, + Pre, None, Split, Split, + Split, Halant, None, None, + + None, None, None, None, + None, None, None, Post, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Telugu + None, Post, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, Below, Below, Below, + Below, Below, Below, Below, + Below, Below, Below, Below, + + Below, Below, Below, Below, + Below, Below, Below, Below, + Below, None, Below, Below, + Below, Below, Below, Below, + + Below, None, Below, Below, + None, Below, Below, Below, + Below, Below, None, None, + None, None, Post, Above, + + Above, Post, Post, Post, + Post, None, Above, Above, + Split, None, Post, Above, + Above, Halant, None, None, + + None, None, None, None, + None, Above, Below, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Kannada + None, None, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, Below, Below, Below, + Below, Below, Below, Below, + Below, Below, Below, Below, + + Below, Below, Below, Below, + Below, Below, Below, Below, + Below, Below, Below, Below, + Below, Below, Below, Below, + + Below, None, Below, Below, + None, Below, Below, Below, + Below, Below, None, None, + None, None, Post, Above, + + Split, Post, Post, Post, + Post, None, Above, Split, + Split, None, Split, Split, + Above, Halant, None, None, + + None, None, None, None, + None, Post, Post, None, + None, None, None, None, + None, None, Below, None, + + None, None, Below, Below, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Malayalam + None, None, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, Post, + + Post, None, Below, None, + None, Post, None, None, + None, None, None, None, + None, None, Post, Post, + + Post, Post, Post, Post, + None, None, Pre, Pre, + Pre, None, Split, Split, + Split, Halant, None, None, + + None, None, None, None, + None, None, None, Post, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + // Sinhala + None, None, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, Post, + + Post, Post, Above, Above, + Below, None, Below, None, + Post, Pre, Split, Pre, + Split, Split, Split, Post, + + None, None, None, None, + None, None, None, None, + None, None, None, None, + None, None, None, None, + + None, None, Post, Post, + None, None, None, None, + None, None, None, None, + None, None, None, None +}; + +static inline Form form(unsigned short uc) { + if (uc < 0x900 || uc > 0xdff) { + if (uc == 0x25cc) + return Consonant; + if (uc == 0x200c || uc == 0x200d) + return Control; + return Other; + } + return (Form)indicForms[uc-0x900]; +} + +static inline Position indic_position(unsigned short uc) { + if (uc < 0x900 || uc > 0xdff) + return None; + return (Position) indicPosition[uc-0x900]; +} + + +enum IndicScriptProperties { + HasReph = 0x01, + HasSplit = 0x02 +}; + +const uchar scriptProperties[10] = { + // Devanagari, + HasReph, + // Bengali, + HasReph|HasSplit, + // Gurmukhi, + 0, + // Gujarati, + HasReph, + // Oriya, + HasReph|HasSplit, + // Tamil, + HasSplit, + // Telugu, + HasSplit, + // Kannada, + HasSplit|HasReph, + // Malayalam, + HasSplit, + // Sinhala, + HasSplit +}; + +struct IndicOrdering { + Form form; + Position position; +}; + +static const IndicOrdering devanagari_order [] = { + { Consonant, Below }, + { Matra, Below }, + { VowelMark, Below }, + { StressMark, Below }, + { Matra, Above }, + { Matra, Post }, + { Consonant, Reph }, + { VowelMark, Above }, + { StressMark, Above }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering bengali_order [] = { + { Consonant, Below }, + { Matra, Below }, + { Matra, Above }, + { Consonant, Reph }, + { VowelMark, Above }, + { Consonant, Post }, + { Matra, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering gurmukhi_order [] = { + { Consonant, Below }, + { Matra, Below }, + { Matra, Above }, + { Consonant, Post }, + { Matra, Post }, + { VowelMark, Above }, + { (Form)0, None } +}; + +static const IndicOrdering tamil_order [] = { + { Matra, Above }, + { Matra, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering telugu_order [] = { + { Matra, Above }, + { Matra, Below }, + { Matra, Post }, + { Consonant, Below }, + { Consonant, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering kannada_order [] = { + { Matra, Above }, + { Matra, Post }, + { Consonant, Below }, + { Consonant, Post }, + { LengthMark, Post }, + { Consonant, Reph }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering malayalam_order [] = { + { Consonant, Below }, + { Matra, Below }, + { Consonant, Reph }, + { Consonant, Post }, + { Matra, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering sinhala_order [] = { + { Matra, Below }, + { Matra, Above }, + { Matra, Post }, + { VowelMark, Post }, + { (Form)0, None } +}; + +static const IndicOrdering * const indic_order[] = { + devanagari_order, // Devanagari + bengali_order, // Bengali + gurmukhi_order, // Gurmukhi + devanagari_order, // Gujarati + bengali_order, // Oriya + tamil_order, // Tamil + telugu_order, // Telugu + kannada_order, // Kannada + malayalam_order, // Malayalam + sinhala_order // Sinhala +}; + + + +// vowel matras that have to be split into two parts. +static const unsigned short split_matras[] = { + // matra, split1, split2 + + // bengalis + 0x9cb, 0x9c7, 0x9be, + 0x9cc, 0x9c7, 0x9d7, + // oriya + 0xb48, 0xb47, 0xb56, + 0xb4b, 0xb47, 0xb3e, + 0xb4c, 0xb47, 0xb57, + // tamil + 0xbca, 0xbc6, 0xbbe, + 0xbcb, 0xbc7, 0xbbe, + 0xbcc, 0xbc6, 0xbd7, + // telugu + 0xc48, 0xc46, 0xc56, + // kannada + 0xcc0, 0xcbf, 0xcd5, + 0xcc7, 0xcc6, 0xcd5, + 0xcc8, 0xcc6, 0xcd6, + 0xcca, 0xcc6, 0xcc2, + 0xccb, 0xcca, 0xcd5, + // malayalam + 0xd4a, 0xd46, 0xd3e, + 0xd4b, 0xd47, 0xd3e, + 0xd4c, 0xd46, 0xd57, + // sinhala + 0xdda, 0xdd9, 0xdca, + 0xddc, 0xdd9, 0xdcf, + 0xddd, 0xddc, 0xdca, + 0xdde, 0xdd9, 0xddf, + 0xffff +}; + +static inline void splitMatra(unsigned short *reordered, int matra, int &len, int &base) +{ + unsigned short matra_uc = reordered[matra]; + //qDebug("matra=%d, reordered[matra]=%x", matra, reordered[matra]); + + const unsigned short *split = split_matras; + while (split[0] < matra_uc) + split += 3; + + assert(*split == matra_uc); + ++split; + + if (indic_position(*split) == Pre) { + reordered[matra] = split[1]; + memmove(reordered + 1, reordered, len*sizeof(unsigned short)); + reordered[0] = split[0]; + base++; + } else { + memmove(reordered + matra + 1, reordered + matra, (len-matra)*sizeof(unsigned short)); + reordered[matra] = split[0]; + reordered[matra+1] = split[1]; + } + len++; +} + +enum IndicProperties { + // these two are already defined +// CcmpProperty = 0x1, +// InitProperty = 0x2, + NuktaProperty = 0x4, + AkhantProperty = 0x8, + RephProperty = 0x10, + PreFormProperty = 0x20, + BelowFormProperty = 0x40, + AboveFormProperty = 0x80, + HalfFormProperty = 0x100, + PostFormProperty = 0x200, + VattuProperty = 0x400, + PreSubstProperty = 0x800, + BelowSubstProperty = 0x1000, + AboveSubstProperty = 0x2000, + PostSubstProperty = 0x4000, + HalantProperty = 0x8000, + CligProperty = 0x10000 +}; + +#ifndef QT_NO_XFTFREETYPE +static const TQOpenType::Features indic_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('i', 'n', 'i', 't'), InitProperty }, + { FT_MAKE_TAG('n', 'u', 'k', 't'), NuktaProperty }, + { FT_MAKE_TAG('a', 'k', 'h', 'n'), AkhantProperty }, + { FT_MAKE_TAG('r', 'p', 'h', 'f'), RephProperty }, + { FT_MAKE_TAG('b', 'l', 'w', 'f'), BelowFormProperty }, + { FT_MAKE_TAG('h', 'a', 'l', 'f'), HalfFormProperty }, + { FT_MAKE_TAG('p', 's', 't', 'f'), PostFormProperty }, + { FT_MAKE_TAG('v', 'a', 't', 'u'), VattuProperty }, + { FT_MAKE_TAG('p', 'r', 'e', 's'), PreSubstProperty }, + { FT_MAKE_TAG('b', 'l', 'w', 's'), BelowSubstProperty }, + { FT_MAKE_TAG('a', 'b', 'v', 's'), AboveSubstProperty }, + { FT_MAKE_TAG('p', 's', 't', 's'), PostSubstProperty }, + { FT_MAKE_TAG('h', 'a', 'l', 'n'), HalantProperty }, + { 0, 0 } +}; +#endif + +// #define INDIC_DEBUG +#ifdef INDIC_DEBUG +#define IDEBUG qDebug +#else +#define IDEBUG if(0) qDebug +#endif + +#ifdef INDIC_DEBUG +static TQString propertiesToString(int properties) +{ + TQString res; + properties = ~properties; + if (properties & CcmpProperty) + res += "Ccmp "; + if (properties & InitProperty) + res += "Init "; + if (properties & NuktaProperty) + res += "Nukta "; + if (properties & AkhantProperty) + res += "Akhant "; + if (properties & RephProperty) + res += "Reph "; + if (properties & PreFormProperty) + res += "PreForm "; + if (properties & BelowFormProperty) + res += "BelowForm "; + if (properties & AboveFormProperty) + res += "AboveForm "; + if (properties & HalfFormProperty) + res += "HalfForm "; + if (properties & PostFormProperty) + res += "PostForm "; + if (properties & VattuProperty) + res += "Vattu "; + if (properties & PreSubstProperty) + res += "PreSubst "; + if (properties & BelowSubstProperty) + res += "BelowSubst "; + if (properties & AboveSubstProperty) + res += "AboveSubst "; + if (properties & PostSubstProperty) + res += "PostSubst "; + if (properties & HalantProperty) + res += "Halant "; + if (properties & CligProperty) + res += "Clig "; + return res; +} +#endif + +static bool indic_shape_syllable(TQOpenType *openType, TQShaperItem *item, bool invalid) +{ + Q_UNUSED(openType) + int script = item->script; + Q_ASSERT(script >= TQFont::Devanagari && script <= TQFont::Sinhala); + const unsigned short script_base = 0x0900 + 0x80*(script-TQFont::Devanagari); + const unsigned short ra = script_base + 0x30; + const unsigned short halant = script_base + 0x4d; + const unsigned short nukta = script_base + 0x3c; + + int len = item->length; + IDEBUG(">>>>> indic shape: from=%d, len=%d invalid=%d", item->from, item->length, invalid); + + if (item->num_glyphs < len+4) { + item->num_glyphs = len+4; + return FALSE; + } + + TQVarLengthArray reordered(len+4); + TQVarLengthArray position(len+4); + + unsigned char properties = scriptProperties[script-TQFont::Devanagari]; + + if (invalid) { + *reordered.data() = 0x25cc; + memcpy(reordered.data()+1, item->string->unicode() + item->from, len*sizeof(TQChar)); + len++; + } else { + memcpy(reordered.data(), item->string->unicode() + item->from, len*sizeof(TQChar)); + } + if (reordered[len-1] == 0x200c) // zero width non joiner + len--; + + int i; + int base = 0; + int reph = -1; + +#ifdef INDIC_DEBUG + IDEBUG("original:"); + for (i = 0; i < len; i++) { + IDEBUG(" %d: %4x", i, reordered[i]); + } +#endif + + if (len != 1) { + unsigned short *uc = reordered.data(); + bool beginsWithRa = FALSE; + + // Rule 1: find base consonant + // + // The shaping engine finds the base consonant of the + // syllable, using the following algorithm: starting from the + // end of the syllable, move backwards until a consonant is + // found that does not have a below-base or post-base form + // (post-base forms have to follow below-base forms), or + // arrive at the first consonant. The consonant stopped at + // will be the base. + // + // * If the syllable starts with Ra + H (in a script that has + // 'Reph'), Ra is excluded from candidates for base + // consonants. + // + // * In Kannada and Telugu, the base consonant cannot be + // farther than 3 consonants from the end of the syllable. + // #### replace the HasReph property by testing if the feature exists in the font! + if (form(*uc) == Consonant || (script == TQFont::Bengali && form(*uc) == IndependentVowel)) { + beginsWithRa = (properties & HasReph) && ((len > 2) && *uc == ra && *(uc+1) == halant); + + if (beginsWithRa && form(*(uc+2)) == Control) + beginsWithRa = FALSE; + + base = (beginsWithRa ? 2 : 0); + IDEBUG(" length = %d, beginsWithRa = %d, base=%d", len, beginsWithRa, base); + + int lastConsonant = 0; + int matra = -1; + // we remember: + // * the last consonant since we need it for rule 2 + // * the matras position for rule 3 and 4 + + // figure out possible base glyphs + memset(position.data(), 0, len); + if (script == TQFont::Devanagari || script == TQFont::Gujarati) { + bool vattu = FALSE; + for (i = base; i < len; ++i) { + position[i] = form(uc[i]); + if (position[i] == Consonant) { + lastConsonant = i; + vattu = (!vattu && uc[i] == ra); + if (vattu) { + IDEBUG("excluding vattu glyph at %d from base candidates", i); + position[i] = Vattu; + } + } else if (position[i] == Matra) { + matra = i; + } + } + } else { + for (i = base; i < len; ++i) { + position[i] = form(uc[i]); + if (position[i] == Consonant) + lastConsonant = i; + else if (matra < 0 && position[i] == Matra) + matra = i; + } + } + int skipped = 0; + Position pos = Post; + for (i = len-1; i > base; i--) { + if (position[i] != Consonant && (position[i] != Control || script == TQFont::Kannada)) + continue; + + Position charPosition = indic_position(uc[i]); + if (pos == Post && charPosition == Post) { + pos = Post; + } else if ((pos == Post || pos == Below) && charPosition == Below) { + if (script == TQFont::Devanagari || script == TQFont::Gujarati) + base = i; + pos = Below; + } else { + base = i; + break; + } + if (skipped == 2 && (script == TQFont::Kannada || script == TQFont::Telugu)) { + base = i; + break; + } + ++skipped; + } + + IDEBUG(" base consonant at %d skipped=%d, lastConsonant=%d", base, skipped, lastConsonant); + + // Rule 2: + // + // If the base consonant is not the last one, Uniscribe + // moves the halant from the base consonant to the last + // one. + if (lastConsonant > base) { + int halantPos = 0; + if (uc[base+1] == halant) + halantPos = base + 1; + else if (uc[base+1] == nukta && uc[base+2] == halant) + halantPos = base + 2; + if (halantPos > 0) { + IDEBUG(" moving halant from %d to %d!", base+1, lastConsonant); + for (i = halantPos; i < lastConsonant; i++) + uc[i] = uc[i+1]; + uc[lastConsonant] = halant; + } + } + + // Rule 3: + // + // If the syllable starts with Ra + H, Uniscribe moves + // this combination so that it follows either: + + // * the post-base 'matra' (if any) or the base consonant + // (in scripts that show similarity to Devanagari, i.e., + // Devanagari, Gujarati, Bengali) + // * the base consonant (other scripts) + // * the end of the syllable (Kannada) + + Position matra_position = None; + if (matra > 0) + matra_position = indic_position(uc[matra]); + IDEBUG(" matra at %d with form %d, base=%d", matra, matra_position, base); + + if (beginsWithRa && base != 0) { + int toPos = base+1; + if (toPos < len && uc[toPos] == nukta) + toPos++; + if (toPos < len && uc[toPos] == halant) + toPos++; + if (toPos < len && uc[toPos] == 0x200d) + toPos++; + if (toPos < len-1 && uc[toPos] == ra && uc[toPos+1] == halant) + toPos += 2; + if (script == TQFont::Devanagari || script == TQFont::Gujarati || script == TQFont::Bengali) { + if (matra_position == Post || matra_position == Split) { + toPos = matra+1; + matra -= 2; + } + } else if (script == TQFont::Kannada) { + toPos = len; + matra -= 2; + } + + IDEBUG("moving leading ra+halant to position %d", toPos); + for (i = 2; i < toPos; i++) + uc[i-2] = uc[i]; + uc[toPos-2] = ra; + uc[toPos-1] = halant; + base -= 2; + if (properties & HasReph) + reph = toPos-2; + } + + // Rule 4: + + // Uniscribe splits two- or three-part matras into their + // parts. This splitting is a character-to-character + // operation). + // + // Uniscribe describes some moving operations for these + // matras here. For shaping however all pre matras need + // to be at the begining of the syllable, so we just move + // them there now. + if (matra_position == Split) { + splitMatra(uc, matra, len, base); + // Handle three-part matras (0xccb in Kannada) + matra_position = indic_position(uc[matra]); + if (matra_position == Split) + splitMatra(uc, matra, len, base); + } else if (matra_position == Pre) { + unsigned short m = uc[matra]; + while (matra--) + uc[matra+1] = uc[matra]; + uc[0] = m; + base++; + } + } + + // Rule 5: + // + // Uniscribe classifies consonants and 'matra' parts as + // pre-base, above-base (Reph), below-base or post-base. This + // classification exists on the character code level and is + // language-dependent, not font-dependent. + for (i = 0; i < base; ++i) + position[i] = Pre; + position[base] = Base; + for (i = base+1; i < len; ++i) { + position[i] = indic_position(uc[i]); + // #### replace by adjusting table + if (uc[i] == nukta || uc[i] == halant) + position[i] = Inherit; + } + if (reph > 0) { + // recalculate reph, it might have changed. + for (i = base+1; i < len; ++i) + if (uc[i] == ra) + reph = i; + position[reph] = Reph; + position[reph+1] = Inherit; + } + + // all reordering happens now to the chars after the base + int fixed = base+1; + if (fixed < len && uc[fixed] == nukta) + fixed++; + if (fixed < len && uc[fixed] == halant) + fixed++; + if (fixed < len && uc[fixed] == 0x200d) + fixed++; + +#ifdef INDIC_DEBUG + for (i = fixed; i < len; ++i) + IDEBUG("position[%d] = %d, form=%d", i, position[i], form(uc[i])); +#endif + // we continuosly position the matras and vowel marks and increase the fixed + // until we reached the end. + const IndicOrdering *finalOrder = indic_order[script-TQFont::Devanagari]; + + IDEBUG(" reordering pass:"); + //IDEBUG(" base=%d fixed=%d", base, fixed); + int toMove = 0; + while (finalOrder[toMove].form && fixed < len-1) { + //IDEBUG(" fixed = %d, moving form %d with pos %d", fixed, finalOrder[toMove].form, finalOrder[toMove].position); + for (i = fixed; i < len; i++) { + if (form(uc[i]) == finalOrder[toMove].form && + position[i] == finalOrder[toMove].position) { + // need to move this glyph + int to = fixed; + if (i < len-1 && position[i+1] == Inherit) { + IDEBUG(" moving two chars from %d to %d", i, to); + unsigned short ch = uc[i]; + unsigned short ch2 = uc[i+1]; + unsigned char pos = position[i]; + for (int j = i+1; j > to+1; j--) { + uc[j] = uc[j-2]; + position[j] = position[j-2]; + } + uc[to] = ch; + uc[to+1] = ch2; + position[to] = pos; + position[to+1] = pos; + fixed += 2; + } else { + IDEBUG(" moving one char from %d to %d", i, to); + unsigned short ch = uc[i]; + unsigned char pos = position[i]; + for (int j = i; j > to; j--) { + uc[j] = uc[j-1]; + position[j] = position[j-1]; + } + uc[to] = ch; + position[to] = pos; + fixed++; + } + } + } + toMove++; + } + + } + + if (reph > 0) { + // recalculate reph, it might have changed. + for (i = base+1; i < len; ++i) + if (reordered[i] == ra) + reph = i; + } + + if (item->font->stringToCMap((const TQChar *)reordered.data(), len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + + + IDEBUG(" base=%d, reph=%d", base, reph); + IDEBUG("reordered:"); + for (i = 0; i < len; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + IDEBUG(" %d: %4x", i, reordered[i]); + } + + // now we have the syllable in the right order, and can start running it through open type. + + bool control = FALSE; + for (i = 0; i < len; ++i) + control |= (form(reordered[i]) == Control); + +#ifndef QT_NO_XFTFREETYPE + if (openType) { + + // we need to keep track of where the base glyph is for some + // scripts and use the cluster feature for this. This + // also means we have to correct the logCluster output from + // the open type engine manually afterwards. for indic this + // is rather simple, as all chars just point to the first + // glyph in the syllable. + TQVarLengthArray clusters(len); + TQVarLengthArray properties(len); + + for (i = 0; i < len; ++i) + clusters[i] = i; + + // features we should always apply + for (i = 0; i < len; ++i) + properties[i] = ~(CcmpProperty + | NuktaProperty + | VattuProperty + | PreSubstProperty + | BelowSubstProperty + | AboveSubstProperty + | HalantProperty + | PositioningProperties); + + // Ccmp always applies + // Init + if (item->from == 0 + || !(item->string->unicode()[item->from-1].isLetter() || item->string->unicode()[item->from-1].isMark())) + properties[0] &= ~InitProperty; + + // Nukta always applies + // Akhant + for (i = 0; i <= base; ++i) + properties[i] &= ~AkhantProperty; + // Reph + if (reph >= 0) { + properties[reph] &= ~RephProperty; + properties[reph+1] &= ~RephProperty; + } + // BelowForm + for (i = base+1; i < len; ++i) + properties[i] &= ~BelowFormProperty; + + if (script == TQFont::Devanagari || script == TQFont::Gujarati) { + // vattu glyphs need this aswell + bool vattu = FALSE; + for (i = base-2; i > 1; --i) { + if (form(reordered[i]) == Consonant) { + vattu = (!vattu && reordered[i] == ra); + if (vattu) { + IDEBUG("forming vattu ligature at %d", i); + properties[i] &= ~BelowFormProperty; + properties[i+1] &= ~BelowFormProperty; + } + } + } + } + // HalfFormProperty + for (i = 0; i < base; ++i) + properties[i] &= ~HalfFormProperty; + if (control) { + for (i = 2; i < len; ++i) { + if (reordered[i] == 0x200d /* ZWJ */) { + properties[i-1] &= ~HalfFormProperty; + properties[i-2] &= ~HalfFormProperty; + } else if (reordered[i] == 0x200c /* ZWNJ */) { + properties[i-1] &= ~HalfFormProperty; + properties[i-2] &= ~HalfFormProperty; + } + } + } + // PostFormProperty + for (i = base+1; i < len; ++i) + properties[i] &= ~PostFormProperty; + // vattu always applies + // pres always applies + // blws always applies + // abvs always applies + + // psts + // ### this looks slightly different from before, but I believe it's correct + if (reordered[len-1] != halant || base != len-2) + properties[base] &= ~PostSubstProperty; + for (i = base+1; i < len; ++i) + properties[i] &= ~PostSubstProperty; + + // halant always applies + +#ifdef INDIC_DEBUG + { + IDEBUG("OT properties:"); + for (int i = 0; i < len; ++i) + qDebug(" i: %s", ::propertiesToString(properties[i]).toLatin1().data()); + } +#endif + + // initialize + item->log_clusters = clusters.data(); + openType->shape(item, properties.data()); + + int newLen = openType->len(); + OTL_GlyphItem otl_glyphs = openType->glyphs(); + + // move the left matra back to it's correct position in malayalam and tamil + if ((script == TQFont::Malayalam || script == TQFont::Tamil) && (form(reordered[0]) == Matra)) { +// qDebug("reordering matra, len=%d", newLen); + // need to find the base in the shaped string and move the matra there + int basePos = 0; + while (basePos < newLen && (int)otl_glyphs[basePos].cluster <= base) + basePos++; + --basePos; + if (basePos < newLen && basePos > 1) { +// qDebug("moving prebase matra to position %d in syllable newlen=%d", basePos, newLen); + OTL_GlyphItemRec m = otl_glyphs[0]; + --basePos; + for (i = 0; i < basePos; ++i) + otl_glyphs[i] = otl_glyphs[i+1]; + otl_glyphs[basePos] = m; + } + } + + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + + if (control) { + IDEBUG("found a control char in the syllable"); + int i = 0, j = 0; + while (i < item->num_glyphs) { + if (form(reordered[otl_glyphs[i].cluster]) == Control) { + ++i; + if (i >= item->num_glyphs) + break; + } + item->glyphs[j] = item->glyphs[i]; + ++i; + ++j; + } + item->num_glyphs = j; + } + + } +#endif + + item->attributes[0].clusterStart = TRUE; + IDEBUG("<<<<<<"); + return TRUE; +} + + +/* syllables are of the form: + + (Consonant Nukta? Halant)* Consonant Matra? VowelMark? StressMark? + (Consonant Nukta? Halant)* Consonant Halant + IndependentVowel VowelMark? StressMark? + + We return syllable boundaries on invalid combinations aswell +*/ +static int indic_nextSyllableBoundary(int script, const TQString &s, int start, int end, bool *invalid) +{ + *invalid = FALSE; + IDEBUG("indic_nextSyllableBoundary: start=%d, end=%d", start, end); + const TQChar *uc = s.unicode()+start; + + int pos = 0; + Form state = form(uc[pos].unicode()); + IDEBUG("state[%d]=%d (uc=%4x)", pos, state, uc[pos].unicode()); + pos++; + + if (state != Consonant && state != IndependentVowel) { + if (state != Other) + *invalid = TRUE; + goto finish; + } + + while (pos < end - start) { + Form newState = form(uc[pos].unicode()); + IDEBUG("state[%d]=%d (uc=%4x)", pos, newState, uc[pos].unicode()); + switch(newState) { + case Control: + newState = state; + if (state == Halant && uc[pos].unicode() == 0x200d /* ZWJ */) + break; + // the control character should be the last char in the item + ++pos; + goto finish; + case Consonant: + if (state == Halant && (script != TQFont::Sinhala || uc[pos-1].unicode() == 0x200d /* ZWJ */)) + break; + goto finish; + case Halant: + if (state == Nukta || state == Consonant) + break; + // Bengali has a special exception allowing the combination Vowel_A/E + Halant + Ya + if (script == TQFont::Bengali && pos == 1 && + (uc[0].unicode() == 0x0985 || uc[0].unicode() == 0x098f)) + break; + goto finish; + case Nukta: + if (state == Consonant) + break; + goto finish; + case StressMark: + if (state == VowelMark) + break; + // fall through + case VowelMark: + if (state == Matra || state == IndependentVowel) + break; + // fall through + case Matra: + if (state == Consonant || state == Nukta) + break; + // ### not sure if this is correct. If it is, does it apply only to Bengali or should + // it work for all Indic languages? + // the combination Independent_A + Vowel Sign AA is allowed. + if (script == TQFont::Bengali && uc[pos].unicode() == 0x9be && uc[pos-1].unicode() == 0x985) + break; + if (script == TQFont::Tamil && state == Matra) { + if (uc[pos-1].unicode() == 0x0bc6 && + (uc[pos].unicode() == 0xbbe || uc[pos].unicode() == 0xbd7)) + break; + if (uc[pos-1].unicode() == 0x0bc7 && uc[pos].unicode() == 0xbbe) + break; + } + goto finish; + + case LengthMark: + case IndependentVowel: + case Invalid: + case Other: + goto finish; + } + state = newState; + pos++; + } + finish: + return pos+start; +} + +static bool indic_shape(TQShaperItem *item) +{ + Q_ASSERT(item->script >= TQFont::Devanagari && item->script <= TQFont::Sinhala); + +#ifndef QT_NO_XFTFREETYPE + TQOpenType *openType = item->font->openType(); + if (openType) + openType->selectScript(item->script, indic_features); +#else + TQOpenType *openType = 0; +#endif + unsigned short *logClusters = item->log_clusters; + + TQShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + IDEBUG("indic_shape: from %d length %d", item->from, item->length); + while (sstart < end) { + bool invalid; + int send = indic_nextSyllableBoundary(item->script, *item->string, sstart, end, &invalid); + IDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, + invalid ? "TRUE" : "FALSE"); + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!indic_shape_syllable(openType, &syllable, invalid)) { + IDEBUG("syllable shaping failed, syllable requests %d glyphs", syllable.num_glyphs); + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + + // fix logcluster array + IDEBUG("syllable:"); + int i; + for (i = first_glyph; i < first_glyph + syllable.num_glyphs; ++i) + IDEBUG(" %d -> glyph %x", i, item->glyphs[i]); + IDEBUG(" logclusters:"); + for (i = sstart; i < send; ++i) { + IDEBUG(" %d -> glyph %d", i, first_glyph); + logClusters[i-item->from] = first_glyph; + } + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; +} + + +static void indic_attributes(int script, const TQString &text, int from, int len, TQCharAttributes *attributes) +{ + int end = from + len; + const TQChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while (i < len) { + bool invalid; + int boundary = indic_nextSyllableBoundary(script, text, from+i, end, &invalid) - from; + attributes[i].charStop = TRUE; + + if (boundary > len-1) boundary = len; + i++; + while (i < boundary) { + attributes[i].charStop = FALSE; + ++uc; + ++i; + } + assert(i == boundary); + } + + +} + + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Thai and Lao +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +#include +#include + + +static void thaiWordBreaks(const TQChar *string, const int len, TQCharAttributes *attributes) +{ +#ifndef QT_NO_TEXTCODEC + typedef int (*th_brk_def)(const char*, int[], int); + static TQTextCodec *thaiCodec = TQTextCodec::codecForMib(2259); + static th_brk_def th_brk = 0; + +#ifndef QT_NO_LIBRARY + /* load libthai dynamically */ + if (!th_brk && thaiCodec) { + th_brk = (th_brk_def)TQLibrary::resolve("thai", "th_brk"); + if (!th_brk) + thaiCodec = 0; + } +#endif + + if (!th_brk) + return; + + TQCString cstr = thaiCodec->fromUnicode(TQConstString(string, len).string()); + + int brp[128]; + int *break_positions = brp; + int numbreaks = th_brk(cstr.data(), break_positions, 128); + if (numbreaks > 128) { + break_positions = new int[numbreaks]; + numbreaks = th_brk(cstr.data(),break_positions, numbreaks); + } + + attributes[0].softBreak = TRUE; + int i; + for (i = 1; i < len; ++i) + attributes[i].softBreak = FALSE; + + for (i = 0; i < numbreaks; ++i) + attributes[break_positions[i]].softBreak = TRUE; + + if (break_positions != brp) + delete [] break_positions; +#endif +} + + +static void thai_attributes( int script, const TQString &text, int from, int len, TQCharAttributes *attributes ) +{ + Q_UNUSED(script); + Q_ASSERT(script == TQFont::Thai); + thaiWordBreaks(text.unicode() + from, len, attributes); +} + + + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Tibetan +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +// tibetan syllables are of the form: +// head position consonant +// first sub-joined consonant +// ....intermediate sub-joined consonants (if any) +// last sub-joined consonant +// sub-joined vowel (a-chung U+0F71) +// standard or compound vowel sign (or 'virama' for devanagari transliteration) + +enum TibetanForm { + TibetanOther, + TibetanHeadConsonant, + TibetanSubjoinedConsonant, + TibetanSubjoinedVowel, + TibetanVowel +}; + +// this table starts at U+0f40 +static const unsigned char tibetanForm[0x80] = { + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, TibetanHeadConsonant, + TibetanOther, TibetanOther, TibetanOther, TibetanOther, + + TibetanOther, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanVowel, TibetanVowel, TibetanVowel, TibetanVowel, + TibetanOther, TibetanOther, TibetanOther, TibetanOther, + TibetanOther, TibetanOther, TibetanOther, TibetanOther, + + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, TibetanSubjoinedConsonant, + TibetanSubjoinedConsonant, TibetanOther, TibetanOther, TibetanOther +}; + + +static inline TibetanForm tibetan_form(const TQChar &c) +{ + return (TibetanForm)tibetanForm[c.unicode() - 0x0f40]; +} + +#ifndef QT_NO_XFTFREETYPE +static const TQOpenType::Features tibetan_features[] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('a', 'b', 'v', 's'), AboveSubstProperty }, + { FT_MAKE_TAG('b', 'l', 'w', 's'), BelowSubstProperty }, + {0, 0} +}; +#endif + +static bool tibetan_shape_syllable(TQOpenType *openType, TQShaperItem *item, bool invalid) +{ + Q_UNUSED(openType) + int len = item->length; + + if (item->num_glyphs < item->length + 4) { + item->num_glyphs = item->length + 4; + return FALSE; + } + + int i; + TQVarLengthArray reordered(len+4); + + const TQChar *str = item->string->unicode() + item->from; + if (invalid) { + *reordered.data() = 0x25cc; + memcpy(reordered.data()+1, str, len*sizeof(TQChar)); + len++; + str = (TQChar *)reordered.data(); + } + + if (item->font->stringToCMap(str, len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + + for (i = 0; i < item->length; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + IDEBUG(" %d: %4x", i, str[i].unicode()); + } + + // now we have the syllable in the right order, and can start running it through open type. + +#ifndef QT_NO_XFTFREETYPE + if (openType && openType->supportsScript(TQFont::Tibetan)) { + openType->selectScript(TQFont::Tibetan, tibetan_features); + + openType->shape(item); + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + } +#endif + + item->attributes[0].clusterStart = TRUE; + return TRUE; +} + + +static int tibetan_nextSyllableBoundary(const TQString &s, int start, int end, bool *invalid) +{ + const TQChar *uc = s.unicode() + start; + + int pos = 0; + TibetanForm state = tibetan_form(*uc); + +// qDebug("state[%d]=%d (uc=%4x)", pos, state, uc[pos].unicode()); + pos++; + + if (state != TibetanHeadConsonant) { + if (state != TibetanOther) + *invalid = TRUE; + goto finish; + } + + while (pos < end - start) { + TibetanForm newState = tibetan_form(uc[pos]); + switch(newState) { + case TibetanSubjoinedConsonant: + case TibetanSubjoinedVowel: + if (state != TibetanHeadConsonant && + state != TibetanSubjoinedConsonant) + goto finish; + state = newState; + break; + case TibetanVowel: + if (state != TibetanHeadConsonant && + state != TibetanSubjoinedConsonant && + state != TibetanSubjoinedVowel) + goto finish; + break; + case TibetanOther: + case TibetanHeadConsonant: + goto finish; + } + pos++; + } + +finish: + *invalid = FALSE; + return start+pos; +} + +static bool tibetan_shape(TQShaperItem *item) +{ + Q_ASSERT(item->script == TQFont::Tibetan); + +#ifndef QT_NO_XFTFREETYPE + TQOpenType *openType = item->font->openType(); + if (openType && !openType->supportsScript(item->script)) + openType = 0; +#else + TQOpenType *openType = 0; +#endif + unsigned short *logClusters = item->log_clusters; + + TQShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + while (sstart < end) { + bool invalid; + int send = tibetan_nextSyllableBoundary(*(item->string), sstart, end, &invalid); + IDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, + invalid ? "TRUE" : "FALSE"); + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!tibetan_shape_syllable(openType, &syllable, invalid)) { + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + + // fix logcluster array + for (int i = sstart; i < send; ++i) + logClusters[i-item->from] = first_glyph; + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; +} + +static void tibetan_attributes(int script, const TQString &text, int from, int len, TQCharAttributes *attributes) +{ + Q_UNUSED(script); + + int end = from + len; + const TQChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while (i < len) { + bool invalid; + int boundary = tibetan_nextSyllableBoundary(text, from+i, end, &invalid) - from; + + attributes[i].charStop = TRUE; + + if (boundary > len-1) boundary = len; + i++; + while (i < boundary) { + attributes[i].charStop = FALSE; + ++uc; + ++i; + } + assert(i == boundary); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Khmer +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + + +// Vocabulary +// Base -> A consonant or an independent vowel in its full (not subscript) form. It is the +// center of the syllable, it can be surrounded by coeng (subscript) consonants, vowels, +// split vowels, signs... but there is only one base in a syllable, it has to be coded as +// the first character of the syllable. +// split vowel --> vowel that has two parts placed separately (e.g. Before and after the consonant). +// Khmer language has five of them. Khmer split vowels either have one part before the +// base and one after the base or they have a part before the base and a part above the base. +// The first part of all Khmer split vowels is the same character, identical to +// the glyph of Khmer dependent vowel SRA EI +// coeng --> modifier used in Khmer to construct coeng (subscript) consonants +// Differently than indian languages, the coeng modifies the consonant that follows it, +// not the one preceding it Each consonant has two forms, the base form and the subscript form +// the base form is the normal one (using the consonants code-point), the subscript form is +// displayed when the combination coeng + consonant is encountered. +// Consonant of type 1 -> A consonant which has subscript for that only occupies space under a base consonant +// Consonant of type 2.-> Its subscript form occupies space under and before the base (only one, RO) +// Consonant of Type 3 -> Its subscript form occupies space under and after the base (KHO, CHHO, THHO, BA, YO, SA) +// Consonant shifter -> Khmer has to series of consonants. The same dependent vowel has different sounds +// if it is attached to a consonant of the first series or a consonant of the second series +// Most consonants have an equivalent in the other series, but some of theme exist only in +// one series (for example SA). If we want to use the consonant SA with a vowel sound that +// can only be done with a vowel sound that corresponds to a vowel accompanying a consonant +// of the other series, then we need to use a consonant shifter: TRIISAP or MUSIKATOAN +// x17C9 y x17CA. TRIISAP changes a first series consonant to second series sound and +// MUSIKATOAN a second series consonant to have a first series vowel sound. +// Consonant shifter are both normally supercript marks, but, when they are followed by a +// superscript, they change shape and take the form of subscript dependent vowel SRA U. +// If they are in the same syllable as a coeng consonant, Unicode 3.0 says that they +// should be typed before the coeng. Unicode 4.0 breaks the standard and says that it should +// be placed after the coeng consonant. +// Dependent vowel -> In khmer dependent vowels can be placed above, below, before or after the base +// Each vowel has its own position. Only one vowel per syllable is allowed. +// Signs -> Khmer has above signs and post signs. Only one above sign and/or one post sign are +// Allowed in a syllable. +// +// +// order is important here! This order must be the same that is found in each horizontal +// line in the statetable for Khmer (see khmerStateTable) . +// +enum KhmerCharClassValues { + CC_RESERVED = 0, + CC_CONSONANT = 1, // Consonant of type 1 or independent vowel + CC_CONSONANT2 = 2, // Consonant of type 2 + CC_CONSONANT3 = 3, // Consonant of type 3 + CC_ZERO_WIDTH_NJ_MARK = 4, // Zero Width non joiner character (0x200C) + CC_CONSONANT_SHIFTER = 5, + CC_ROBAT = 6, // Khmer special diacritic accent -treated differently in state table + CC_COENG = 7, // Subscript consonant combining character + CC_DEPENDENT_VOWEL = 8, + CC_SIGN_ABOVE = 9, + CC_SIGN_AFTER = 10, + CC_ZERO_WIDTH_J_MARK = 11, // Zero width joiner character + CC_COUNT = 12 // This is the number of character classes +}; + + +enum KhmerCharClassFlags { + CF_CLASS_MASK = 0x0000FFFF, + + CF_CONSONANT = 0x01000000, // flag to speed up comparing + CF_SPLIT_VOWEL = 0x02000000, // flag for a split vowel -> the first part is added in front of the syllable + CF_DOTTED_CIRCLE = 0x04000000, // add a dotted circle if a character with this flag is the first in a syllable + CF_COENG = 0x08000000, // flag to speed up comparing + CF_SHIFTER = 0x10000000, // flag to speed up comparing + CF_ABOVE_VOWEL = 0x20000000, // flag to speed up comparing + + // position flags + CF_POS_BEFORE = 0x00080000, + CF_POS_BELOW = 0x00040000, + CF_POS_ABOVE = 0x00020000, + CF_POS_AFTER = 0x00010000, + CF_POS_MASK = 0x000f0000 +}; + + +// Characters that get refered to by name +enum KhmerChar { + C_SIGN_ZWNJ = 0x200C, + C_SIGN_ZWJ = 0x200D, + C_DOTTED_CIRCLE = 0x25CC, + C_RO = 0x179A, + C_VOWEL_AA = 0x17B6, + C_SIGN_NIKAHIT = 0x17C6, + C_VOWEL_E = 0x17C1, + C_COENG = 0x17D2 +}; + + +// simple classes, they are used in the statetable (in this file) to control the length of a syllable +// they are also used to know where a character should be placed (location in reference to the base character) +// and also to know if a character, when independently displayed, should be displayed with a dotted-circle to +// indicate error in syllable construction +// +enum { + _xx = CC_RESERVED, + _sa = CC_SIGN_ABOVE | CF_DOTTED_CIRCLE | CF_POS_ABOVE, + _sp = CC_SIGN_AFTER | CF_DOTTED_CIRCLE| CF_POS_AFTER, + _c1 = CC_CONSONANT | CF_CONSONANT, + _c2 = CC_CONSONANT2 | CF_CONSONANT, + _c3 = CC_CONSONANT3 | CF_CONSONANT, + _rb = CC_ROBAT | CF_POS_ABOVE | CF_DOTTED_CIRCLE, + _cs = CC_CONSONANT_SHIFTER | CF_DOTTED_CIRCLE | CF_SHIFTER, + _dl = CC_DEPENDENT_VOWEL | CF_POS_BEFORE | CF_DOTTED_CIRCLE, + _db = CC_DEPENDENT_VOWEL | CF_POS_BELOW | CF_DOTTED_CIRCLE, + _da = CC_DEPENDENT_VOWEL | CF_POS_ABOVE | CF_DOTTED_CIRCLE | CF_ABOVE_VOWEL, + _dr = CC_DEPENDENT_VOWEL | CF_POS_AFTER | CF_DOTTED_CIRCLE, + _co = CC_COENG | CF_COENG | CF_DOTTED_CIRCLE, + + // split vowel + _va = _da | CF_SPLIT_VOWEL, + _vr = _dr | CF_SPLIT_VOWEL +}; + + +// Character class: a character class value +// ORed with character class flags. +// +typedef unsigned long KhmerCharClass; + + +// Character class tables +// _xx character does not combine into syllable, such as numbers, puntuation marks, non-Khmer signs... +// _sa Sign placed above the base +// _sp Sign placed after the base +// _c1 Consonant of type 1 or independent vowel (independent vowels behave as type 1 consonants) +// _c2 Consonant of type 2 (only RO) +// _c3 Consonant of type 3 +// _rb Khmer sign robat u17CC. combining mark for subscript consonants +// _cd Consonant-shifter +// _dl Dependent vowel placed before the base (left of the base) +// _db Dependent vowel placed below the base +// _da Dependent vowel placed above the base +// _dr Dependent vowel placed behind the base (right of the base) +// _co Khmer combining mark COENG u17D2, combines with the consonant or independent vowel following +// it to create a subscript consonant or independent vowel +// _va Khmer split vowel in wich the first part is before the base and the second one above the base +// _vr Khmer split vowel in wich the first part is before the base and the second one behind (right of) the base +// +static const KhmerCharClass khmerCharClasses[] = { + _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, // 1780 - 178F + _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c2, _c1, _c1, _c1, _c3, _c3, // 1790 - 179F + _c1, _c3, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, // 17A0 - 17AF + _c1, _c1, _c1, _c1, _dr, _dr, _dr, _da, _da, _da, _da, _db, _db, _db, _va, _vr, // 17B0 - 17BF + _vr, _dl, _dl, _dl, _vr, _vr, _sa, _sp, _sp, _cs, _cs, _sa, _rb, _sa, _sa, _sa, // 17C0 - 17CF + _sa, _sa, _co, _sa, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _sa, _xx, _xx // 17D0 - 17DF +}; + +// this enum must reflect the range of khmerCharClasses +enum KhmerCharClassesRange { + KhmerFirstChar = 0x1780, + KhmerLastChar = 0x17df +}; + +// Below we define how a character in the input string is either in the khmerCharClasses table +// (in which case we get its type back), a ZWJ or ZWNJ (two characters that may appear +// within the syllable, but are not in the table) we also get their type back, or an unknown object +// in which case we get _xx (CC_RESERVED) back +// +static inline KhmerCharClass getKhmerCharClass(const TQChar &uc) +{ + if (uc.unicode() == C_SIGN_ZWJ) { + return CC_ZERO_WIDTH_J_MARK; + } + + if (uc.unicode() == C_SIGN_ZWNJ) { + return CC_ZERO_WIDTH_NJ_MARK; + } + + if (uc.unicode() < KhmerFirstChar || uc.unicode() > KhmerLastChar) { + return CC_RESERVED; + } + + return khmerCharClasses[uc.unicode() - KhmerFirstChar]; +} + + +// The stateTable is used to calculate the end (the length) of a well +// formed Khmer Syllable. +// +// Each horizontal line is ordered exactly the same way as the values in KhmerClassTable +// CharClassValues. This coincidence of values allows the follow up of the table. +// +// Each line corresponds to a state, which does not necessarily need to be a type +// of component... for example, state 2 is a base, with is always a first character +// in the syllable, but the state could be produced a consonant of any type when +// it is the first character that is analysed (in ground state). +// +// Differentiating 3 types of consonants is necessary in order to +// forbid the use of certain combinations, such as having a second +// coeng after a coeng RO, +// The inexistent possibility of having a type 3 after another type 3 is permitted, +// eliminating it would very much complicate the table, and it does not create typing +// problems, as the case above. +// +// The table is tquite complex, in order to limit the number of coeng consonants +// to 2 (by means of the table). +// +// There a peculiarity, as far as Unicode is concerned: +// - The consonant-shifter is considered in two possible different +// locations, the one considered in Unicode 3.0 and the one considered in +// Unicode 4.0. (there is a backwards compatibility problem in this standard). +// +// +// xx independent character, such as a number, punctuation sign or non-khmer char +// +// c1 Khmer consonant of type 1 or an independent vowel +// that is, a letter in which the subscript for is only under the +// base, not taking any space to the right or to the left +// +// c2 Khmer consonant of type 2, the coeng form takes space under +// and to the left of the base (only RO is of this type) +// +// c3 Khmer consonant of type 3. Its subscript form takes space under +// and to the right of the base. +// +// cs Khmer consonant shifter +// +// rb Khmer robat +// +// co coeng character (u17D2) +// +// dv dependent vowel (including split vowels, they are treated in the same way). +// even if dv is not defined above, the component that is really tested for is +// KhmerClassTable::CC_DEPENDENT_VOWEL, which is common to all dependent vowels +// +// zwj Zero Width joiner +// +// zwnj Zero width non joiner +// +// sa above sign +// +// sp post sign +// +// there are lines with equal content but for an easier understanding +// (and maybe change in the future) we did not join them +// +static const signed char khmerStateTable[][CC_COUNT] = +{ + // xx c1 c2 c3 zwnj cs rb co dv sa sp zwj + { 1, 2, 2, 2, 1, 1, 1, 6, 1, 1, 1, 2}, // 0 - ground state + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 1 - exit state (or sign to the right of the syllable) + {-1, -1, -1, -1, 3, 4, 5, 6, 16, 17, 1, -1}, // 2 - Base consonant + {-1, -1, -1, -1, -1, 4, -1, -1, 16, -1, -1, -1}, // 3 - First ZWNJ before a register shifter It can only be followed by a shifter or a vowel + {-1, -1, -1, -1, 15, -1, -1, 6, 16, 17, 1, 14}, // 4 - First register shifter + {-1, -1, -1, -1, -1, -1, -1, -1, 20, -1, 1, -1}, // 5 - Robat + {-1, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1}, // 6 - First Coeng + {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14}, // 7 - First consonant of type 1 after coeng + {-1, -1, -1, -1, 12, 13, -1, -1, 16, 17, 1, 14}, // 8 - First consonant of type 2 after coeng + {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14}, // 9 - First consonant or type 3 after ceong + {-1, 11, 11, 11, -1, -1, -1, -1, -1, -1, -1, -1}, // 10 - Second Coeng (no register shifter before) + {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14}, // 11 - Second coeng consonant (or ind. vowel) no register shifter before + {-1, -1, -1, -1, -1, 13, -1, -1, 16, -1, -1, -1}, // 12 - Second ZWNJ before a register shifter + {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14}, // 13 - Second register shifter + {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, // 14 - ZWJ before vowel + {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, // 15 - ZWNJ before vowel + {-1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 1, 18}, // 16 - dependent vowel + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 18}, // 17 - sign above + {-1, -1, -1, -1, -1, -1, -1, 19, -1, -1, -1, -1}, // 18 - ZWJ after vowel + {-1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1}, // 19 - Third coeng + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1}, // 20 - dependent vowel after a Robat +}; + + +// #define KHMER_DEBUG +#ifdef KHMER_DEBUG +#define KHDEBUG qDebug +#else +#define KHDEBUG if(0) qDebug +#endif + +// Given an input string of characters and a location in which to start looking +// calculate, using the state table, which one is the last character of the syllable +// that starts in the starting position. +// +static inline int khmer_nextSyllableBoundary(const TQString &s, int start, int end, bool *invalid) +{ + *invalid = FALSE; + const TQChar *uc = s.unicode() + start; + int state = 0; + int pos = start; + + while (pos < end) { + KhmerCharClass charClass = getKhmerCharClass(*uc); + if (pos == start) { + *invalid = (charClass > 0) && ! (charClass & CF_CONSONANT); + } + state = khmerStateTable[state][charClass & CF_CLASS_MASK]; + + KHDEBUG("state[%d]=%d class=%8lx (uc=%4x)", pos - start, state, + charClass, uc->unicode() ); + + if (state < 0) { + break; + } + ++uc; + ++pos; + } + return pos; +} + + +#ifndef QT_NO_XFTFREETYPE +static const TQOpenType::Features khmer_features[] = { + { FT_MAKE_TAG( 'p', 'r', 'e', 'f' ), PreFormProperty }, + { FT_MAKE_TAG( 'b', 'l', 'w', 'f' ), BelowFormProperty }, + { FT_MAKE_TAG( 'a', 'b', 'v', 'f' ), AboveFormProperty }, + { FT_MAKE_TAG( 'p', 's', 't', 'f' ), PostFormProperty }, + { FT_MAKE_TAG( 'p', 'r', 'e', 's' ), PreSubstProperty }, + { FT_MAKE_TAG( 'b', 'l', 'w', 's' ), BelowSubstProperty }, + { FT_MAKE_TAG( 'a', 'b', 'v', 's' ), AboveSubstProperty }, + { FT_MAKE_TAG( 'p', 's', 't', 's' ), PostSubstProperty }, + { FT_MAKE_TAG( 'c', 'l', 'i', 'g' ), CligProperty }, + { 0, 0 } +}; +#endif + + +static bool khmer_shape_syllable(TQOpenType *openType, TQShaperItem *item) +{ +#ifndef QT_NO_XFTFREETYPE + if (openType) + openType->selectScript(TQFont::Khmer, khmer_features); +#endif + // according to the specs this is the max length one can get + // ### the real value should be smaller + assert(item->length < 13); + + KHDEBUG("syllable from %d len %d, str='%s'", item->from, item->length, + item->string->mid(item->from, item->length).utf8().data()); + + int len = 0; + int syllableEnd = item->from + item->length; + unsigned short reordered[16]; + unsigned char properties[16]; + enum { + AboveForm = 0x01, + PreForm = 0x02, + PostForm = 0x04, + BelowForm = 0x08 + }; + memset(properties, 0, 16*sizeof(unsigned char)); + +#ifdef KHMER_DEBUG + qDebug("original:"); + for (int i = from; i < syllableEnd; i++) { + qDebug(" %d: %4x", i, string[i].unicode()); + } +#endif + + // write a pre vowel or the pre part of a split vowel first + // and look out for coeng + ro. RO is the only vowel of type 2, and + // therefore the only one that retquires saving space before the base. + // + int coengRo = -1; // There is no Coeng Ro, if found this value will change + int i; + for (i = item->from; i < syllableEnd; i += 1) { + KhmerCharClass charClass = getKhmerCharClass(item->string->at(i)); + + // if a split vowel, write the pre part. In Khmer the pre part + // is the same for all split vowels, same glyph as pre vowel C_VOWEL_E + if (charClass & CF_SPLIT_VOWEL) { + reordered[len] = C_VOWEL_E; + properties[len] = PreForm; + ++len; + break; // there can be only one vowel + } + // if a vowel with pos before write it out + if (charClass & CF_POS_BEFORE) { + reordered[len] = item->string->at(i).unicode(); + properties[len] = PreForm; + ++len; + break; // there can be only one vowel + } + // look for coeng + ro and remember position + // works because coeng + ro is always in front of a vowel (if there is a vowel) + // and because CC_CONSONANT2 is enough to identify it, as it is the only consonant + // with this flag + if ( (charClass & CF_COENG) && (i + 1 < syllableEnd) && + ( (getKhmerCharClass(item->string->at(i+1)) & CF_CLASS_MASK) == CC_CONSONANT2) ) { + coengRo = i; + } + } + + // write coeng + ro if found + if (coengRo > -1) { + reordered[len] = C_COENG; + properties[len] = PreForm; + ++len; + reordered[len] = C_RO; + properties[len] = PreForm; + ++len; + } + + // shall we add a dotted circle? + // If in the position in which the base should be (first char in the string) there is + // a character that has the Dotted circle flag (a character that cannot be a base) + // then write a dotted circle + if (getKhmerCharClass(item->string->at(item->from)) & CF_DOTTED_CIRCLE) { + reordered[len] = C_DOTTED_CIRCLE; + ++len; + } + + // copy what is left to the output, skipping before vowels and + // coeng Ro if they are present + for (i = item->from; i < syllableEnd; i += 1) { + TQChar uc = item->string->at(i); + KhmerCharClass charClass = getKhmerCharClass(uc); + + // skip a before vowel, it was already processed + if (charClass & CF_POS_BEFORE) { + continue; + } + + // skip coeng + ro, it was already processed + if (i == coengRo) { + i += 1; + continue; + } + + switch (charClass & CF_POS_MASK) + { + case CF_POS_ABOVE : + reordered[len] = uc.unicode(); + properties[len] = AboveForm; + ++len; + break; + + case CF_POS_AFTER : + reordered[len] = uc.unicode(); + properties[len] = PostForm; + ++len; + break; + + case CF_POS_BELOW : + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + + default: + // assign the correct flags to a coeng consonant + // Consonants of type 3 are taged as Post forms and those type 1 as below forms + if ( (charClass & CF_COENG) && i + 1 < syllableEnd ) { + unsigned char property = (getKhmerCharClass(item->string->at(i+1)) & CF_CLASS_MASK) == CC_CONSONANT3 ? + PostForm : BelowForm; + reordered[len] = uc.unicode(); + properties[len] = property; + ++len; + i += 1; + reordered[len] = item->string->at(i).unicode(); + properties[len] = property; + ++len; + break; + } + + // if a shifter is followed by an above vowel change the shifter to below form, + // an above vowel can have two possible positions i + 1 or i + 3 + // (position i+1 corresponds to unicode 3, position i+3 to Unicode 4) + // and there is an extra rule for C_VOWEL_AA + C_SIGN_NIKAHIT also for two + // different positions, right after the shifter or after a vowel (Unicode 4) + if ( (charClass & CF_SHIFTER) && (i + 1 < syllableEnd) ) { + if (getKhmerCharClass(item->string->at(i+1)) & CF_ABOVE_VOWEL ) { + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + } + if (i + 2 < syllableEnd && + (item->string->at(i+1).unicode() == C_VOWEL_AA) && + (item->string->at(i+2).unicode() == C_SIGN_NIKAHIT) ) + { + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + } + if (i + 3 < syllableEnd && (getKhmerCharClass(item->string->at(i+3)) & CF_ABOVE_VOWEL) ) { + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + } + if (i + 4 < syllableEnd && + (item->string->at(i+3).unicode() == C_VOWEL_AA) && + (item->string->at(i+4).unicode() == C_SIGN_NIKAHIT) ) + { + reordered[len] = uc.unicode(); + properties[len] = BelowForm; + ++len; + break; + } + } + + // default - any other characters + reordered[len] = uc.unicode(); + ++len; + break; + } // switch + } // for + + if (item->font->stringToCMap((const TQChar *)reordered, len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + + KHDEBUG("after shaping: len=%d", len); + for (i = 0; i < len; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + KHDEBUG(" %d: %4x property=%x", i, reordered[i], properties[i]); + } + + // now we have the syllable in the right order, and can start running it through open type. + +#ifndef QT_NO_XFTFREETYPE + if (openType) { + unsigned short logClusters[16]; + for (int i = 0; i < len; ++i) + logClusters[i] = i; + + uint where[16]; + + for (int i = 0; i < len; ++i) { + where[i] = ~(PreSubstProperty + | BelowSubstProperty + | AboveSubstProperty + | PostSubstProperty + | CligProperty + | PositioningProperties); + if (properties[i] == PreForm) + where[i] &= ~PreFormProperty; + else if (properties[i] == BelowForm) + where[i] &= ~BelowFormProperty; + else if (properties[i] == AboveForm) + where[i] &= ~AboveFormProperty; + else if (properties[i] == PostForm) + where[i] &= ~PostFormProperty; + } + + openType->shape(item, where); + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + } else +#endif + { + KHDEBUG("Not using openType"); + Q_UNUSED(openType); + } + + item->attributes[0].clusterStart = TRUE; + return TRUE; +} + +static bool khmer_shape(TQShaperItem *item) +{ + assert(item->script == TQFont::Khmer); + +#ifndef QT_NO_XFTFREETYPE + TQOpenType *openType = item->font->openType(); + if (openType && !openType->supportsScript(item->script)) + openType = 0; +#else + TQOpenType *openType = 0; +#endif + unsigned short *logClusters = item->log_clusters; + + TQShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + KHDEBUG("khmer_shape: from %d length %d", item->from, item->length); + while (sstart < end) { + bool invalid; + int send = khmer_nextSyllableBoundary(*item->string, sstart, end, &invalid); + KHDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, + invalid ? "TRUE" : "FALSE"); + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!khmer_shape_syllable(openType, &syllable)) { + KHDEBUG("syllable shaping failed, syllable requests %d glyphs", syllable.num_glyphs); + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + + // fix logcluster array + KHDEBUG("syllable:"); + int i; + for (i = first_glyph; i < first_glyph + syllable.num_glyphs; ++i) + KHDEBUG(" %d -> glyph %x", i, item->glyphs[i]); + KHDEBUG(" logclusters:"); + for (i = sstart; i < send; ++i) { + KHDEBUG(" %d -> glyph %d", i, first_glyph); + logClusters[i-item->from] = first_glyph; + } + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; +} + +static void khmer_attributes( int script, const TQString &text, int from, int len, TQCharAttributes *attributes ) +{ + Q_UNUSED(script); + + int end = from + len; + const TQChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while ( i < len ) { + bool invalid; + int boundary = khmer_nextSyllableBoundary( text, from+i, end, &invalid ) - from; + + attributes[i].charStop = TRUE; + + if ( boundary > len-1 ) boundary = len; + i++; + while ( i < boundary ) { + attributes[i].charStop = FALSE; + ++uc; + ++i; + } + assert( i == boundary ); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Myanmar +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +enum MymrCharClassValues +{ + Mymr_CC_RESERVED = 0, + Mymr_CC_CONSONANT = 1, /* Consonant of type 1, that has subscript form */ + Mymr_CC_CONSONANT2 = 2, /* Consonant of type 2, that has no subscript form */ + Mymr_CC_NGA = 3, /* Consonant NGA */ + Mymr_CC_YA = 4, /* Consonant YA */ + Mymr_CC_RA = 5, /* Consonant RA */ + Mymr_CC_WA = 6, /* Consonant WA */ + Mymr_CC_HA = 7, /* Consonant HA */ + Mymr_CC_IND_VOWEL = 8, /* Independent vowel */ + Mymr_CC_ZERO_WIDTH_NJ_MARK = 9, /* Zero Width non joiner character (0x200C) */ + Mymr_CC_VIRAMA = 10, /* Subscript consonant combining character */ + Mymr_CC_PRE_VOWEL = 11, /* Dependent vowel, prebase (Vowel e) */ + Mymr_CC_BELOW_VOWEL = 12, /* Dependent vowel, prebase (Vowel u, uu) */ + Mymr_CC_ABOVE_VOWEL = 13, /* Dependent vowel, prebase (Vowel i, ii, ai) */ + Mymr_CC_POST_VOWEL = 14, /* Dependent vowel, prebase (Vowel aa) */ + Mymr_CC_SIGN_ABOVE = 15, + Mymr_CC_SIGN_BELOW = 16, + Mymr_CC_SIGN_AFTER = 17, + Mymr_CC_ZERO_WIDTH_J_MARK = 18, /* Zero width joiner character */ + Mymr_CC_COUNT = 19 /* This is the number of character classes */ +}; + +enum MymrCharClassFlags +{ + Mymr_CF_CLASS_MASK = 0x0000FFFF, + + Mymr_CF_CONSONANT = 0x01000000, /* flag to speed up comparing */ + Mymr_CF_MEDIAL = 0x02000000, /* flag to speed up comparing */ + Mymr_CF_IND_VOWEL = 0x04000000, /* flag to speed up comparing */ + Mymr_CF_DEP_VOWEL = 0x08000000, /* flag to speed up comparing */ + Mymr_CF_DOTTED_CIRCLE = 0x10000000, /* add a dotted circle if a character with this flag is the first in a syllable */ + Mymr_CF_VIRAMA = 0x20000000, /* flag to speed up comparing */ + + /* position flags */ + Mymr_CF_POS_BEFORE = 0x00080000, + Mymr_CF_POS_BELOW = 0x00040000, + Mymr_CF_POS_ABOVE = 0x00020000, + Mymr_CF_POS_AFTER = 0x00010000, + Mymr_CF_POS_MASK = 0x000f0000, + + Mymr_CF_AFTER_KINZI = 0x00100000 +}; + +/* Characters that get refrered to by name */ +enum MymrChar +{ + Mymr_C_SIGN_ZWNJ = 0x200C, + Mymr_C_SIGN_ZWJ = 0x200D, + Mymr_C_DOTTED_CIRCLE = 0x25CC, + Mymr_C_RA = 0x101B, + Mymr_C_YA = 0x101A, + Mymr_C_NGA = 0x1004, + Mymr_C_VOWEL_E = 0x1031, + Mymr_C_VIRAMA = 0x1039 +}; + +enum +{ + Mymr_xx = Mymr_CC_RESERVED, + Mymr_c1 = Mymr_CC_CONSONANT | Mymr_CF_CONSONANT | Mymr_CF_POS_BELOW, + Mymr_c2 = Mymr_CC_CONSONANT2 | Mymr_CF_CONSONANT, + Mymr_ng = Mymr_CC_NGA | Mymr_CF_CONSONANT | Mymr_CF_POS_ABOVE, + Mymr_ya = Mymr_CC_YA | Mymr_CF_CONSONANT | Mymr_CF_MEDIAL | Mymr_CF_POS_AFTER | Mymr_CF_AFTER_KINZI, + Mymr_ra = Mymr_CC_RA | Mymr_CF_CONSONANT | Mymr_CF_MEDIAL | Mymr_CF_POS_BEFORE, + Mymr_wa = Mymr_CC_WA | Mymr_CF_CONSONANT | Mymr_CF_MEDIAL | Mymr_CF_POS_BELOW, + Mymr_ha = Mymr_CC_HA | Mymr_CF_CONSONANT | Mymr_CF_MEDIAL | Mymr_CF_POS_BELOW, + Mymr_id = Mymr_CC_IND_VOWEL | Mymr_CF_IND_VOWEL, + Mymr_vi = Mymr_CC_VIRAMA | Mymr_CF_VIRAMA | Mymr_CF_POS_ABOVE | Mymr_CF_DOTTED_CIRCLE, + Mymr_dl = Mymr_CC_PRE_VOWEL | Mymr_CF_DEP_VOWEL | Mymr_CF_POS_BEFORE | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI, + Mymr_db = Mymr_CC_BELOW_VOWEL | Mymr_CF_DEP_VOWEL | Mymr_CF_POS_BELOW | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI, + Mymr_da = Mymr_CC_ABOVE_VOWEL | Mymr_CF_DEP_VOWEL | Mymr_CF_POS_ABOVE | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI, + Mymr_dr = Mymr_CC_POST_VOWEL | Mymr_CF_DEP_VOWEL | Mymr_CF_POS_AFTER | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI, + Mymr_sa = Mymr_CC_SIGN_ABOVE | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_POS_ABOVE | Mymr_CF_AFTER_KINZI, + Mymr_sb = Mymr_CC_SIGN_BELOW | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_POS_BELOW | Mymr_CF_AFTER_KINZI, + Mymr_sp = Mymr_CC_SIGN_AFTER | Mymr_CF_DOTTED_CIRCLE | Mymr_CF_AFTER_KINZI +}; + + +typedef int MymrCharClass; + + +static const MymrCharClass mymrCharClasses[] = +{ + Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_ng, Mymr_c1, Mymr_c1, Mymr_c1, + Mymr_c1, Mymr_c1, Mymr_c2, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, /* 1000 - 100F */ + Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, Mymr_c1, + Mymr_c1, Mymr_c1, Mymr_ya, Mymr_ra, Mymr_c1, Mymr_wa, Mymr_c1, Mymr_ha, /* 1010 - 101F */ + Mymr_c2, Mymr_c2, Mymr_xx, Mymr_id, Mymr_id, Mymr_id, Mymr_id, Mymr_id, + Mymr_xx, Mymr_id, Mymr_id, Mymr_xx, Mymr_dr, Mymr_da, Mymr_da, Mymr_db, /* 1020 - 102F */ + Mymr_db, Mymr_dl, Mymr_da, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_sa, Mymr_sb, + Mymr_sp, Mymr_vi, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, /* 1030 - 103F */ + Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, + Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, /* 1040 - 104F */ + Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, + Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, Mymr_xx, /* 1050 - 105F */ +}; + +static MymrCharClass +getMyanmarCharClass (const TQChar &ch) +{ + if (ch.unicode() == Mymr_C_SIGN_ZWJ) + return Mymr_CC_ZERO_WIDTH_J_MARK; + + if (ch.unicode() == Mymr_C_SIGN_ZWNJ) + return Mymr_CC_ZERO_WIDTH_NJ_MARK; + + if (ch.unicode() < 0x1000 || ch.unicode() > 0x105f) + return Mymr_CC_RESERVED; + + return mymrCharClasses[ch.unicode() - 0x1000]; +} + +static const signed char mymrStateTable[][Mymr_CC_COUNT] = +{ +// xx c1, c2 ng ya ra wa ha id zwnj vi dl db da dr sa sb sp zwj + { 1, 4, 4, 2, 4, 4, 4, 4, 24, 1, 27, 17, 18, 19, 20, 21, 1, 1, 4}, // 0 - ground state + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 1 - exit state (or sp to the right of the syllable) + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 17, 18, 19, 20, 21, -1, -1, 4}, // 2 - NGA + {-1, 4, 4, 4, 4, 4, 4, 4, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 3 - Virama after NGA + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 17, 18, 19, 20, 21, 1, 1, -1}, // 4 - Base consonant + {-2, 6, -2, -2, 7, 8, 9, 10, -2, 23, -2, -2, -2, -2, -2, -2, -2, -2, -2}, // 5 - First virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 25, 17, 18, 19, 20, 21, -1, -1, -1}, // 6 - c1 after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 17, 18, 19, 20, 21, -1, -1, -1}, // 7 - ya after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 17, 18, 19, 20, 21, -1, -1, -1}, // 8 - ra after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 17, 18, 19, 20, 21, -1, -1, -1}, // 9 - wa after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 18, 19, 20, 21, -1, -1, -1}, // 10 - ha after virama + {-1, -1, -1, -1, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 11 - Virama after NGA+zwj + {-2, -2, -2, -2, -2, -2, 13, 14, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, // 12 - Second virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, 17, 18, 19, 20, 21, -1, -1, -1}, // 13 - wa after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 18, 19, 20, 21, -1, -1, -1}, // 14 - ha after virama + {-2, -2, -2, -2, -2, -2, -2, 16, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, // 15 - Third virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 18, 19, 20, 21, -1, -1, -1}, // 16 - ha after virama + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 20, 21, 1, 1, -1}, // 17 - dl, Dependent vowel e + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 19, -1, 21, 1, 1, -1}, // 18 - db, Dependent vowel u,uu + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1}, // 19 - da, Dependent vowel i,ii,ai + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 22, -1, -1, -1, -1, -1, 1, 1, -1}, // 20 - dr, Dependent vowel aa + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1}, // 21 - sa, Sign anusvara + {-1, -1, -1, -1, -1, -1, -1, -1, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 22 - atha + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, -1}, // 23 - zwnj for atha + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1}, // 24 - Independent vowel + {-2, -2, -2, -2, 26, 26, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, // 25 - Virama after subscript consonant + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 17, 18, 19, 20, 21, -1, 1, -1}, // 26 - ra/ya after subscript consonant + virama + {-1, 6, -1, -1, 7, 8, 9, 10, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 27 - Virama after ground state +// exit state -2 is for invalid order of medials and combination of invalids +// with virama where virama should treat as start of next syllable +}; + + + +// #define MYANMAR_DEBUG +#ifdef MYANMAR_DEBUG +#define MMDEBUG qDebug +#else +#define MMDEBUG if(0) qDebug +#endif + +// Given an input string of characters and a location in which to start looking +// calculate, using the state table, which one is the last character of the syllable +// that starts in the starting position. +// +static inline int myanmar_nextSyllableBoundary(const TQString &s, int start, int end, bool *invalid) +{ + *invalid = FALSE; + const TQChar *uc = s.unicode() + start; + int state = 0; + int pos = start; + + while (pos < end) { + MymrCharClass charClass = getMyanmarCharClass(*uc); + state = mymrStateTable[state][charClass & Mymr_CF_CLASS_MASK]; + if (pos == start) + *invalid = charClass & Mymr_CF_DOTTED_CIRCLE; + + MMDEBUG("state[%d]=%d class=%8x (uc=%4x)", pos - start, state, charClass, uc->unicode() ); + + if (state < 0) { + if (state < -1) + --pos; + break; + } + ++uc; + ++pos; + } + return pos; +} + + +#ifndef QT_NO_XFTFREETYPE +// ###### might have to change order of above and below forms and substitutions, +// but according to Unicode below comes before above +static const TQOpenType::Features myanmar_features[] = { + { FT_MAKE_TAG( 'p', 'r', 'e', 'f' ), PreFormProperty }, + { FT_MAKE_TAG( 'b', 'l', 'w', 'f' ), BelowFormProperty }, + { FT_MAKE_TAG( 'a', 'b', 'v', 'f' ), AboveFormProperty }, + { FT_MAKE_TAG( 'p', 's', 't', 'f' ), PostFormProperty }, + { FT_MAKE_TAG( 'p', 'r', 'e', 's' ), PreSubstProperty }, + { FT_MAKE_TAG( 'b', 'l', 'w', 's' ), BelowSubstProperty }, + { FT_MAKE_TAG( 'a', 'b', 'v', 's' ), AboveSubstProperty }, + { FT_MAKE_TAG( 'p', 's', 't', 's' ), PostSubstProperty }, + { FT_MAKE_TAG( 'r', 'l', 'i', 'g' ), CligProperty }, // Myanmar1 uses this instead of the other features + { 0, 0 } +}; +#endif + + +// Visual order before shaping should be: +// +// [Vowel Mark E] +// [Virama + Medial Ra] +// [Base] +// [Virama + Consonant] +// [Nga + Virama] (Kinzi) ### should probably come before post forms (medial ya) +// [Vowels] +// [Marks] +// +// This means that we can keep the logical order apart from having to +// move the pre vowel, medial ra and kinzi + +static bool myanmar_shape_syllable(TQOpenType *openType, TQShaperItem *item, bool invalid) +{ +#ifndef QT_NO_XFTFREETYPE + if (openType) + openType->selectScript(TQFont::Myanmar, myanmar_features); +#endif + // according to the table the max length of a syllable should be around 14 chars + assert(item->length < 32); + + MMDEBUG("\nsyllable from %d len %d, str='%s'", item->from, item->length, + item->string->mid(item->from, item->length).utf8().data()); + + const TQChar *uc = item->string->unicode() + item->from; +#ifdef MYANMAR_DEBUG + qDebug("original:"); + for (int i = 0; i < item->length; i++) { + qDebug(" %d: %4x", i, uc[i].unicode()); + } +#endif + int vowel_e = -1; + int kinzi = -1; + int medial_ra = -1; + int base = -1; + int i; + for (i = 0; i < item->length; ++i) { + ushort chr = uc[i].unicode(); + + if (chr == Mymr_C_VOWEL_E) { + vowel_e = i; + continue; + } + if (i == 0 + && chr == Mymr_C_NGA + && i + 2 < item->length + && uc[i+1].unicode() == Mymr_C_VIRAMA) { + int mc = getMyanmarCharClass(uc[i+2]); + //MMDEBUG("maybe kinzi: mc=%x", mc); + if ((mc & Mymr_CF_CONSONANT) == Mymr_CF_CONSONANT) { + kinzi = i; + continue; + } + } + if (base >= 0 + && chr == Mymr_C_VIRAMA + && i + 1 < item->length + && uc[i+1].unicode() == Mymr_C_RA) { + medial_ra = i; + continue; + } + if (base < 0) + base = i; + } + + MMDEBUG("\n base=%d, vowel_e=%d, kinzi=%d, medial_ra=%d", base, vowel_e, kinzi, medial_ra); + int len = 0; + unsigned short reordered[32]; + unsigned char properties[32]; + enum { + AboveForm = 0x01, + PreForm = 0x02, + PostForm = 0x04, + BelowForm = 0x08 + }; + memset(properties, 0, 32*sizeof(unsigned char)); + + // write vowel_e if found + if (vowel_e >= 0) { + reordered[0] = Mymr_C_VOWEL_E; + len = 1; + } + // write medial_ra + if (medial_ra >= 0) { + reordered[len] = Mymr_C_VIRAMA; + reordered[len+1] = Mymr_C_RA; + properties[len] = PreForm; + properties[len+1] = PreForm; + len += 2; + } + + // shall we add a dotted circle? + // If in the position in which the base should be (first char in the string) there is + // a character that has the Dotted circle flag (a character that cannot be a base) + // then write a dotted circle + if (invalid) { + reordered[len] = C_DOTTED_CIRCLE; + ++len; + } + + bool lastWasVirama = FALSE; + int basePos = -1; + // copy the rest of the syllable to the output, inserting the kinzi + // at the correct place + for (i = 0; i < item->length; ++i) { + if (i == vowel_e) + continue; + if (i == medial_ra || i == kinzi) { + ++i; + continue; + } + + ushort chr = uc[i].unicode(); + MymrCharClass cc = getMyanmarCharClass(uc[i]); + if (kinzi >= 0 && i > base && (cc & Mymr_CF_AFTER_KINZI)) { + reordered[len] = Mymr_C_NGA; + reordered[len+1] = Mymr_C_VIRAMA; + properties[len-1] = AboveForm; + properties[len] = AboveForm; + len += 2; + kinzi = -1; + } + + if (lastWasVirama) { + int prop = 0; + switch(cc & Mymr_CF_POS_MASK) { + case Mymr_CF_POS_BEFORE: + prop = PreForm; + break; + case Mymr_CF_POS_BELOW: + prop = BelowForm; + break; + case Mymr_CF_POS_ABOVE: + prop = AboveForm; + break; + case Mymr_CF_POS_AFTER: + prop = PostForm; + break; + default: + break; + } + properties[len-1] = prop; + properties[len] = prop; + if(basePos >= 0 && basePos == len-2) + properties[len-2] = prop; + } + lastWasVirama = (chr == Mymr_C_VIRAMA); + if(i == base) + basePos = len; + + if ((chr != Mymr_C_SIGN_ZWNJ && chr != Mymr_C_SIGN_ZWJ) || !len) { + reordered[len] = chr; + ++len; + } + } + if (kinzi >= 0) { + reordered[len] = Mymr_C_NGA; + reordered[len+1] = Mymr_C_VIRAMA; + properties[len] = AboveForm; + properties[len+1] = AboveForm; + len += 2; + } + + if (item->font->stringToCMap((const TQChar *)reordered, len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + + MMDEBUG("after shaping: len=%d", len); + for (i = 0; i < len; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + MMDEBUG(" %d: %4x property=%x", i, reordered[i], properties[i]); + } + + // now we have the syllable in the right order, and can start running it through open type. + +#ifndef QT_NO_XFTFREETYPE + if (openType) { + unsigned short logClusters[32]; + for (int i = 0; i < len; ++i) + logClusters[i] = i; + + uint where[32]; + + for (int i = 0; i < len; ++i) { + where[i] = ~(PreSubstProperty + | BelowSubstProperty + | AboveSubstProperty + | PostSubstProperty + | CligProperty + | PositioningProperties); + if (properties[i] == PreForm) + where[i] &= ~PreFormProperty; + else if (properties[i] == BelowForm) + where[i] &= ~BelowFormProperty; + else if (properties[i] == AboveForm) + where[i] &= ~AboveFormProperty; + else if (properties[i] == PostForm) + where[i] &= ~PostFormProperty; + } + + openType->shape(item, where); + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + } else +#endif + { + MMDEBUG("Not using openType"); + Q_UNUSED(openType); + } + + item->attributes[0].clusterStart = TRUE; + return TRUE; +} + +static bool myanmar_shape(TQShaperItem *item) +{ + assert(item->script == TQFont::Myanmar); + +#ifndef QT_NO_XFTFREETYPE + TQOpenType *openType = item->font->openType(); + if (openType && !openType->supportsScript(item->script)) + openType = 0; +#else + TQOpenType *openType = 0; +#endif + unsigned short *logClusters = item->log_clusters; + + TQShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + MMDEBUG("myanmar_shape: from %d length %d", item->from, item->length); + while (sstart < end) { + bool invalid; + int send = myanmar_nextSyllableBoundary(*item->string, sstart, end, &invalid); + MMDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart, + invalid ? "TRUE" : "FALSE"); + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!myanmar_shape_syllable(openType, &syllable, invalid)) { + MMDEBUG("syllable shaping failed, syllable requests %d glyphs", syllable.num_glyphs); + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + + // fix logcluster array + MMDEBUG("syllable:"); + int i; + for (i = first_glyph; i < first_glyph + syllable.num_glyphs; ++i) + MMDEBUG(" %d -> glyph %x", i, item->glyphs[i]); + MMDEBUG(" logclusters:"); + for (i = sstart; i < send; ++i) { + MMDEBUG(" %d -> glyph %d", i, first_glyph); + logClusters[i-item->from] = first_glyph; + } + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; +} + +static void myanmar_attributes( int script, const TQString &text, int from, int len, TQCharAttributes *attributes ) +{ + Q_UNUSED(script); + + int end = from + len; + const TQChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while ( i < len ) { + bool invalid; + int boundary = myanmar_nextSyllableBoundary( text, from+i, end, &invalid ) - from; + + attributes[i].charStop = TRUE; + attributes[i].softBreak = TRUE; + + if ( boundary > len-1 ) boundary = len; + i++; + while ( i < boundary ) { + attributes[i].charStop = FALSE; + attributes[i].softBreak = FALSE; + ++uc; + ++i; + } + assert( i == boundary ); + } +} + +// -------------------------------------------------------------------------------------------------------------------------------------------- +// +// Hangul +// +// -------------------------------------------------------------------------------------------------------------------------------------------- + +// Hangul is a syllable based script. Unicode reserves a large range +// for precomposed hangul, where syllables are already precomposed to +// their final glyph shape. In addition, a so called jamo range is +// defined, that can be used to express old Hangul. Modern hangul +// syllables can also be expressed as jamo, and should be composed +// into syllables. The operation is rather simple and mathematical. + +// Every hangul jamo is classified as being either a Leading consonant +// (L), and intermediat Vowel (V) or a trailing consonant (T). Modern +// hangul syllables (the ones in the precomposed area can be of type +// LV or LVT. +// +// Syllable breaks do _not_ occur between: +// +// L L, V or precomposed +// V, LV V, T +// LVT, T T +// +// A standard syllable is of the form L+V+T*. The above rules allow +// nonstandard syllables L*V*T*. To transform them into standard +// syllables fill characers L_f and V_f can be inserted. + +enum { + Hangul_SBase = 0xac00, + Hangul_LBase = 0x1100, + Hangul_VBase = 0x1161, + Hangul_TBase = 0x11a7, + Hangul_SCount = 11172, + Hangul_LCount = 19, + Hangul_VCount = 21, + Hangul_TCount = 28, + Hangul_NCount = 21*28 +}; + +static inline bool hangul_isPrecomposed(unsigned short uc) { + return (uc >= Hangul_SBase && uc < Hangul_SBase + Hangul_SCount); +} + +static inline bool hangul_isLV(unsigned short uc) { + return ((uc - Hangul_SBase) % Hangul_TCount == 0); +} + +enum HangulType { + L, + V, + T, + LV, + LVT, + X +}; + +static inline HangulType hangul_type(unsigned short uc) { + if (uc > Hangul_SBase && uc < Hangul_SBase + Hangul_SCount) + return hangul_isLV(uc) ? LV : LVT; + if (uc < Hangul_LBase || uc > 0x11ff) + return X; + if (uc < Hangul_VBase) + return L; + if (uc < Hangul_TBase) + return V; + return T; +} + +static int hangul_nextSyllableBoundary(const TQString &s, int start, int end) +{ + const TQChar *uc = s.unicode() + start; + + HangulType state = hangul_type(uc->unicode()); + int pos = 1; + + while (pos < end - start) { + HangulType newState = hangul_type(uc[pos].unicode()); + switch(newState) { + case X: + goto finish; + case L: + case V: + case T: + if (state > newState) + goto finish; + state = newState; + break; + case LV: + if (state > L) + goto finish; + state = V; + break; + case LVT: + if (state > L) + goto finish; + state = T; + } + ++pos; + } + + finish: + return start+pos; +} + +#ifndef QT_NO_XFTFREETYPE +static const TQOpenType::Features hangul_features [] = { + { FT_MAKE_TAG('c', 'c', 'm', 'p'), CcmpProperty }, + { FT_MAKE_TAG('l', 'j', 'm', 'o'), CcmpProperty }, + { FT_MAKE_TAG('j', 'j', 'm', 'o'), CcmpProperty }, + { FT_MAKE_TAG('t', 'j', 'm', 'o'), CcmpProperty }, + { 0, 0 } +}; +#endif + +static bool hangul_shape_syllable(TQOpenType *openType, TQShaperItem *item) +{ + Q_UNUSED(openType) + const TQChar *ch = item->string->unicode() + item->from; + + int i; + unsigned short composed = 0; + // see if we can compose the syllable into a modern hangul + if (item->length == 2) { + int LIndex = ch[0].unicode() - Hangul_LBase; + int VIndex = ch[1].unicode() - Hangul_VBase; + if (LIndex >= 0 && LIndex < Hangul_LCount && + VIndex >= 0 && VIndex < Hangul_VCount) + composed = (LIndex * Hangul_VCount + VIndex) * Hangul_TCount + Hangul_SBase; + } else if (item->length == 3) { + int LIndex = ch[0].unicode() - Hangul_LBase; + int VIndex = ch[1].unicode() - Hangul_VBase; + int TIndex = ch[2].unicode() - Hangul_TBase; + if (LIndex >= 0 && LIndex < Hangul_LCount && + VIndex >= 0 && VIndex < Hangul_VCount && + TIndex >= 0 && TIndex < Hangul_TCount) + composed = (LIndex * Hangul_VCount + VIndex) * Hangul_TCount + TIndex + Hangul_SBase; + } + + + int len = item->length; + TQChar c(composed); + + // ### icc says 'chars' is unused + // const TQChar *chars = ch; + + // if we have a modern hangul use the composed form + if (composed) { + // chars = &c; + len = 1; + } + + if (item->font->stringToCMap(ch, len, item->glyphs, item->advances, + &item->num_glyphs, item->flags & TQTextEngine::RightToLeft) != TQFontEngine::NoError) + return FALSE; + for (i = 0; i < len; i++) { + item->attributes[i].mark = FALSE; + item->attributes[i].clusterStart = FALSE; + item->attributes[i].justification = 0; + item->attributes[i].zeroWidth = FALSE; + IDEBUG(" %d: %4x", i, ch[i].unicode()); + } + +#ifndef QT_NO_XFTFREETYPE + if (openType && !composed) { + + TQVarLengthArray logClusters(len); + for (i = 0; i < len; ++i) + logClusters[i] = i; + item->log_clusters = logClusters.data(); + + openType->shape(item); + if (!openType->positionAndAdd(item, FALSE)) + return FALSE; + + } +#endif + + item->attributes[0].clusterStart = TRUE; + return TRUE; +} + +static bool hangul_shape(TQShaperItem *item) +{ + Q_ASSERT(item->script == TQFont::Hangul); + + const TQChar *uc = item->string->unicode() + item->from; + + bool allPrecomposed = TRUE; + for (int i = 0; i < item->length; ++i) { + if (!hangul_isPrecomposed(uc[i].unicode())) { + allPrecomposed = FALSE; + break; + } + } + + if (!allPrecomposed) { +#ifndef QT_NO_XFTFREETYPE + TQOpenType *openType = item->font->openType(); + if (openType && !openType->supportsScript(item->script)) + openType = 0; + if (openType) + openType->selectScript(TQFont::Hangul, hangul_features); +#else + TQOpenType *openType = 0; +#endif + + unsigned short *logClusters = item->log_clusters; + + TQShaperItem syllable = *item; + int first_glyph = 0; + + int sstart = item->from; + int end = sstart + item->length; + while (sstart < end) { + int send = hangul_nextSyllableBoundary(*(item->string), sstart, end); + + syllable.from = sstart; + syllable.length = send-sstart; + syllable.glyphs = item->glyphs + first_glyph; + syllable.offsets = item->offsets + first_glyph; + syllable.advances = item->advances + first_glyph; + syllable.attributes = item->attributes + first_glyph; + syllable.num_glyphs = item->num_glyphs - first_glyph; + if (!hangul_shape_syllable(openType, &syllable)) { + item->num_glyphs += syllable.num_glyphs; + return FALSE; + } + item->has_positioning |= syllable.has_positioning; + // fix logcluster array + for (int i = sstart; i < send; ++i) + logClusters[i-item->from] = first_glyph; + sstart = send; + first_glyph += syllable.num_glyphs; + } + item->num_glyphs = first_glyph; + return TRUE; + } + + return basic_shape(item); +} + +static void hangul_attributes(int script, const TQString &text, int from, int len, TQCharAttributes *attributes) +{ + Q_UNUSED(script); + + int end = from + len; + const TQChar *uc = text.unicode() + from; + attributes += from; + int i = 0; + while (i < len) { + int boundary = hangul_nextSyllableBoundary(text, from+i, end) - from; + + attributes[i].charStop = TRUE; + + if (boundary > len-1) boundary = len; + i++; + while (i < boundary) { + attributes[i].charStop = FALSE; + ++uc; + ++i; + } + assert(i == boundary); + } +} + +// ----------------------------------------------------------------------------------------------- +// +// The script engine jump table +// +// ----------------------------------------------------------------------------------------------- + +const q_scriptEngine scriptEngines[] = { + // Latin, + { basic_shape, 0 }, + // Greek, + { basic_shape, 0 }, + // Cyrillic, + { basic_shape, 0 }, + // Armenian, + { basic_shape, 0 }, + // Georgian, + { basic_shape, 0 }, + // Runic, + { basic_shape, 0 }, + // Ogham, + { basic_shape, 0 }, + // SpacingModifiers, + { basic_shape, 0 }, + // CombiningMarks, + { basic_shape, 0 }, + + // // Middle Eastern Scripts + // Hebrew, + { hebrew_shape, 0 }, + // Arabic, + { arabic_shape, 0 }, + // Syriac, + { syriac_shape, 0 }, + // Thaana, + { thaana_shape, 0 }, + + // // South and Southeast Asian Scripts + // Devanagari, + { indic_shape, indic_attributes }, + // Bengali, + { indic_shape, indic_attributes }, + // Gurmukhi, + { indic_shape, indic_attributes }, + // Gujarati, + { indic_shape, indic_attributes }, + // Oriya, + { indic_shape, indic_attributes }, + // Tamil, + { indic_shape, indic_attributes }, + // Telugu, + { indic_shape, indic_attributes }, + // Kannada, + { indic_shape, indic_attributes }, + // Malayalam, + { indic_shape, indic_attributes }, + // Sinhala, + { indic_shape, indic_attributes }, + // Thai, + { basic_shape, thai_attributes }, + // Lao, + { basic_shape, thai_attributes }, + // Tibetan, + { tibetan_shape, tibetan_attributes }, + // Myanmar, + { myanmar_shape, myanmar_attributes }, + // Khmer, + { khmer_shape, khmer_attributes }, + + // // East Asian Scripts + // Han, + { basic_shape, 0 }, + // Hiragana, + { basic_shape, 0 }, + // Katakana, + { basic_shape, 0 }, + // Hangul, + { hangul_shape, hangul_attributes }, + // Bopomofo, + { basic_shape, 0 }, + // Yi, + { basic_shape, 0 }, + + // // Additional Scripts + // Ethiopic, + { basic_shape, 0 }, + // Cherokee, + { basic_shape, 0 }, + // CanadianAboriginal, + { basic_shape, 0 }, + // Mongolian, + { basic_shape, 0 }, + + // // Symbols + // CurrencySymbols, + { basic_shape, 0 }, + // LetterlikeSymbols, + { basic_shape, 0 }, + // NumberForms, + { basic_shape, 0 }, + // MathematicalOperators, + { basic_shape, 0 }, + // TechnicalSymbols, + { basic_shape, 0 }, + // GeometricSymbols, + { basic_shape, 0 }, + // MiscellaneousSymbols, + { basic_shape, 0 }, + // EnclosedAndSquare, + { basic_shape, 0 }, + // Braille, + { basic_shape, 0 }, + + // Unicode, + { basic_shape, 0 }, + //Tagalog, + { basic_shape, 0 }, + //Hanunoo, + { basic_shape, 0 }, + //Buhid, + { basic_shape, 0 }, + //Tagbanwa, + { basic_shape, 0 }, + // KatakanaHalfWidth + { basic_shape, 0 }, + // Limbu + { basic_shape, 0 }, + // TaiLe + { basic_shape, 0 } +}; diff --git a/src/kernel/qsession.h b/src/kernel/qsession.h new file mode 100644 index 000000000..3c01f6ab8 --- /dev/null +++ b/src/kernel/qsession.h @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Definition of TQSession class +** +** Created : 990510 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSESSION_H +#define TQSESSION_H + +#ifndef QT_H +#endif // QT_H + +#endif diff --git a/src/kernel/qsessionmanager.h b/src/kernel/qsessionmanager.h new file mode 100644 index 000000000..0a9446a76 --- /dev/null +++ b/src/kernel/qsessionmanager.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Definition of TQSessionManager class +** +** Created : 990510 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSESSIONMANAGER_H +#define TQSESSIONMANAGER_H + +#ifndef QT_H +#include "qobject.h" +#include "qwindowdefs.h" +#include "qstring.h" +#include "qstringlist.h" +#endif // QT_H +#ifndef QT_NO_SESSIONMANAGER + +class TQSessionManagerData; + +class Q_EXPORT TQSessionManager : public TQObject +{ + Q_OBJECT + TQSessionManager( TQApplication *app, TQString &id, TQString &key ); + ~TQSessionManager(); +public: + TQString sessionId() const; + TQString sessionKey() const; +#if defined(Q_WS_X11) || defined(Q_WS_MAC) + void* handle() const; +#endif + + bool allowsInteraction(); + bool allowsErrorInteraction(); + void release(); + + void cancel(); + + enum RestartHint { + RestartIfRunning, + RestartAnyway, + RestartImmediately, + RestartNever + }; + void setRestartHint( RestartHint ); + RestartHint restartHint() const; + + void setRestartCommand( const TQStringList& ); + TQStringList restartCommand() const; + void setDiscardCommand( const TQStringList& ); + TQStringList discardCommand() const; + + void setManagerProperty( const TQString& name, const TQString& value ); + void setManagerProperty( const TQString& name, const TQStringList& value ); + + bool isPhase2() const; + void requestPhase2(); + +private: + friend class TQApplication; + friend class TQBaseApplication; + TQSessionManagerData* d; +}; + +#endif // QT_NO_SESSIONMANAGER +#endif // TQSESSIONMANAGER_H diff --git a/src/kernel/qsharedmemory_p.cpp b/src/kernel/qsharedmemory_p.cpp new file mode 100644 index 000000000..be0bf536c --- /dev/null +++ b/src/kernel/qsharedmemory_p.cpp @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Provides a standardised interface to shared memory +** +** Created : 020124 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** Licensees holding valid TQt Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qsharedmemory_p.h" + +#if !defined(QT_QWS_NO_SHM) + +#if defined(QT_POSIX_QSHM) +#include +#include + +TQSharedMemory::TQSharedMemory (int size, TQString filename, char c ) +{ + shmSize = size; + shmFile = filename; + character = c; + shmFile.append(c); +} + +bool TQSharedMemory::create () +{ + shmFD = shm_open (shmFile.latin1 (), O_RDWR | O_EXCL | O_CREAT, 0666); + if (shmFD == -1) + return FALSE; + else if (ftruncate (shmFD, shmSize) == -1) + { + close (shmFD); + return FALSE; + } + + return TRUE; +} + +void TQSharedMemory::destroy () +{ + shm_unlink (shmFile.latin1 ()); +} + +bool TQSharedMemory::attach () +{ + shmBase = mmap (0, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, shmFD, 0); + + if (shmBase == MAP_FAILED) + return FALSE; + + close (shmFD); + return TRUE; +} + +void TQSharedMemory::detach () +{ + munmap (shmBase, shmSize); +} + +void TQSharedMemory::setPermissions (mode_t mode) +{ + mprotect (shmBase, shmSize, mode); // Provide defines to make prot work properly +} + +int TQSharedMemory::size() +{ + struct stat buf; + int rc = fstat (shmFD, &buf); + if (rc != -1) + return buf.st_size; + else + return rc; +} + +#else // Assume SysV for backwards compat +#include + +TQSharedMemory::TQSharedMemory (int size, TQString filename, char c ) +{ + shmSize = size; + shmFile = filename; + character = c; + key = ftok (shmFile.latin1 (), c); + idInitted = FALSE; + shmId = -1; +} + +bool TQSharedMemory::create () +{ + shmId = shmget (key, shmSize, IPC_CREAT | 0666); + if (shmId == -1) + return FALSE; + else + return TRUE; +} + +void TQSharedMemory::destroy () +{ + if (shmId != -1) { + struct shmid_ds shm; + shmctl (shmId, IPC_RMID, &shm); + } +} + +bool TQSharedMemory::attach () +{ + if (shmId == -1) + shmId = shmget (key, shmSize, 0); + + shmBase = shmat (shmId, 0, 0); + if ((int) shmBase == -1 || shmBase == 0) + return FALSE; + else + return TRUE; +} + +void TQSharedMemory::detach () +{ + shmdt (shmBase); +} + +void TQSharedMemory::setPermissions (mode_t mode) +{ + struct shmid_ds shm; + shmctl (shmId, IPC_STAT, &shm); + shm.shm_perm.mode = mode; + shmctl (shmId, IPC_SET, &shm); +} + +int TQSharedMemory::size () +{ + struct shmid_ds shm; + shmctl (shmId, IPC_STAT, &shm); + return shm.shm_segsz; +} + +#endif + +#endif diff --git a/src/kernel/qsharedmemory_p.h b/src/kernel/qsharedmemory_p.h new file mode 100644 index 000000000..fcae3ea42 --- /dev/null +++ b/src/kernel/qsharedmemory_p.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Includes system files for shared memory +** +** Created : 020124 +** +** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** Licensees holding valid TQt Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSHAREDMEMORY_P_H +#define TQSHAREDMEMORY_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the TQt API. It exists for the convenience +// of qapplication_qws.cpp and qgfxvnc_qws.cpp. This header file may +// change from version to version without notice, or even be removed. +// +// We mean it. +// +// + +#ifndef QT_H +#include "qstring.h" +#endif // QT_H + +#if !defined (QT_QWS_NO_SHM) + +#include +#include + +class TQSharedMemory { +public: + TQSharedMemory(){}; + TQSharedMemory(int, TQString, char c = 'Q'); + ~TQSharedMemory(){}; + + bool create(); + void destroy(); + + bool attach(); + void detach(); + + void setPermissions(mode_t mode); + int size(); + void * base() { return shmBase; }; + +private: + void *shmBase; + int shmSize; + TQString shmFile; + char character; +#if defined(QT_POSIX_QSHM) + int shmFD; +#else + int shmId; + key_t key; + int idInitted; +#endif +}; + +#endif + +#endif diff --git a/src/kernel/qsignal.cpp b/src/kernel/qsignal.cpp new file mode 100644 index 000000000..b4f68f58b --- /dev/null +++ b/src/kernel/qsignal.cpp @@ -0,0 +1,257 @@ +/**************************************************************************** +** +** Implementation of TQSignal class +** +** Created : 941201 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qsignal.h" +#include "qmetaobject.h" +#include "qguardedptr.h" + +/*! + \class TQSignal qsignal.h + \brief The TQSignal class can be used to send signals for classes + that don't inherit TQObject. + + \ingroup io + \ingroup misc + + If you want to send signals from a class that does not inherit + TQObject, you can create an internal TQSignal object to emit the + signal. You must also provide a function that connects the signal + to an outside object slot. This is how we have implemented + signals in the TQMenuData class, which is not a TQObject. + + In general, we recommend inheriting TQObject instead. TQObject + provides much more functionality. + + You can set a single TQVariant parameter for the signal with + setValue(). + + Note that TQObject is a \e private base class of TQSignal, i.e. you + cannot call any TQObject member functions from a TQSignal object. + + Example: + \code + #include + + class MyClass + { + public: + MyClass(); + ~MyClass(); + + void doSomething(); + + void connect( TQObject *receiver, const char *member ); + + private: + TQSignal *sig; + }; + + MyClass::MyClass() + { + sig = new TQSignal; + } + + MyClass::~MyClass() + { + delete sig; + } + + void MyClass::doSomething() + { + // ... does something + sig->activate(); // emits the signal + } + + void MyClass::connect( TQObject *receiver, const char *member ) + { + sig->connect( receiver, member ); + } + \endcode +*/ + +/*! + Constructs a signal object called \a name, with the parent object + \a parent. These arguments are passed directly to TQObject. +*/ + +TQSignal::TQSignal( TQObject *parent, const char *name ) + : TQObject( parent, name ) +{ + isSignal = TRUE; +#ifndef QT_NO_VARIANT + val = 0; +#endif +} + +/*! + Destroys the signal. All connections are removed, as is the case + with all TQObjects. +*/ +TQSignal::~TQSignal() +{ +} +#ifndef QT_NO_VARIANT +// Returns TRUE if it matches ".+(.*int.*" +static inline bool intSignature( const char *member ) +{ + TQCString s( member ); + int p = s.find( '(' ); + return p > 0 && p < s.findRev( "int" ); +} +#endif +/*! + Connects the signal to \a member in object \a receiver. + + \sa disconnect(), TQObject::connect() +*/ + +bool TQSignal::connect( const TQObject *receiver, const char *member ) +{ +#ifndef QT_NO_VARIANT + if ( intSignature( member ) ) +#endif + return TQObject::connect( (TQObject *)this, SIGNAL(intSignal(int)), receiver, member ); +#ifndef QT_NO_VARIANT + return TQObject::connect( (TQObject *)this, SIGNAL(signal(const TQVariant&)), + receiver, member ); +#endif +} + +/*! + Disonnects the signal from \a member in object \a receiver. + + \sa connect(), TQObject::disconnect() +*/ + +bool TQSignal::disconnect( const TQObject *receiver, const char *member ) +{ + if (!member) + return TQObject::disconnect( (TQObject *)this, 0, receiver, member); +#ifndef QT_NO_VARIANT + if ( intSignature( member ) ) +#endif + return TQObject::disconnect( (TQObject *)this, SIGNAL(intSignal(int)), receiver, member ); +#ifndef QT_NO_VARIANT + return TQObject::disconnect( (TQObject *)this, SIGNAL(signal(const TQVariant&)), + receiver, member ); +#endif +} + + +/*! + \fn bool TQSignal::isBlocked() const + \obsolete + Returns TRUE if the signal is blocked, or FALSE if it is not blocked. + + The signal is not blocked by default. + + \sa block(), TQObject::signalsBlocked() +*/ + +/*! + \fn void TQSignal::block( bool b ) + \obsolete + Blocks the signal if \a b is TRUE, or unblocks the signal if \a b is FALSE. + + An activated signal disappears into hyperspace if it is blocked. + + \sa isBlocked(), activate(), TQObject::blockSignals() +*/ + + +/*! + \fn void TQSignal::activate() + + Emits the signal. If the platform supports TQVariant and a + parameter has been set with setValue(), this value is passed in + the signal. +*/ +void TQSignal::activate() +{ +#ifndef QT_NO_VARIANT + /* Create this TQGuardedPtr on this, if we get destroyed after the intSignal (but before the variant signal) + we cannot just emit the signal (because val has been destroyed already) */ + TQGuardedPtr me = this; + if( me ) + emit intSignal( val.toInt() ); + if( me ) + emit signal( val ); +#else + emit intSignal(0); +#endif +} + +#ifndef QT_NO_VARIANT +/*! + Sets the signal's parameter to \a value +*/ +void TQSignal::setValue( const TQVariant &value ) +{ + val = value; +} + +/*! + Returns the signal's parameter +*/ +TQVariant TQSignal::value() const +{ + return val; +} +/*! \fn void TQSignal::signal( const TQVariant & ) + \internal +*/ +/*! \fn void TQSignal::intSignal( int ) + \internal +*/ + +#ifndef QT_NO_COMPAT +/*! \obsolete */ +void TQSignal::setParameter( int value ) +{ + val = value; +} + +/*! \obsolete */ +int TQSignal::parameter() const +{ + return val.toInt(); +} +#endif +#endif //QT_NO_VARIANT diff --git a/src/kernel/qsignal.h b/src/kernel/qsignal.h new file mode 100644 index 000000000..9a4ef4008 --- /dev/null +++ b/src/kernel/qsignal.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Definition of TQSignal class +** +** Created : 941201 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSIGNAL_H +#define TQSIGNAL_H + +#ifndef QT_H +#include "qvariant.h" +#include "qobject.h" +#endif // QT_H + + +class Q_EXPORT TQSignal : public TQObject +{ + Q_OBJECT + +public: + TQSignal( TQObject *parent=0, const char *name=0 ); + ~TQSignal(); + + bool connect( const TQObject *receiver, const char *member ); + bool disconnect( const TQObject *receiver, const char *member=0 ); + + void activate(); + +#ifndef QT_NO_COMPAT + bool isBlocked() const { return TQObject::signalsBlocked(); } + void block( bool b ) { TQObject::blockSignals( b ); } +#ifndef QT_NO_VARIANT + void setParameter( int value ); + int parameter() const; +#endif +#endif + +#ifndef QT_NO_VARIANT + void setValue( const TQVariant &value ); + TQVariant value() const; +#endif +signals: +#ifndef QT_NO_VARIANT + void signal( const TQVariant& ); +#endif + void intSignal( int ); + +private: +#ifndef QT_NO_VARIANT + TQVariant val; +#endif +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQSignal( const TQSignal & ); + TQSignal &operator=( const TQSignal & ); +#endif +}; + + +#endif // TQSIGNAL_H diff --git a/src/kernel/qsignalmapper.cpp b/src/kernel/qsignalmapper.cpp new file mode 100644 index 000000000..9deeb193e --- /dev/null +++ b/src/kernel/qsignalmapper.cpp @@ -0,0 +1,183 @@ +/**************************************************************************** +** +** Implementation of TQSignalMapper class +** +** Created : 980503 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qsignalmapper.h" +#ifndef QT_NO_SIGNALMAPPER +#include "qptrdict.h" + +struct TQSignalMapperRec { + TQSignalMapperRec() + { + has_int = 0; + str_id = TQString::null; + } + + uint has_int:1; + + int int_id; + TQString str_id; + // extendable to other types of identification +}; + +class TQSignalMapperData { +public: + TQSignalMapperData() + { + dict.setAutoDelete( TRUE ); + } + + TQPtrDict dict; +}; + +/*! + \class TQSignalMapper qsignalmapper.h + \brief The TQSignalMapper class bundles signals from identifiable senders. + + \ingroup io + + This class collects a set of parameterless signals, and re-emits + them with integer or string parameters corresponding to the object + that sent the signal. +*/ + +/*! + Constructs a TQSignalMapper called \a name, with parent \a parent. + Like all TQObjects, it will be deleted when the parent is deleted. +*/ +TQSignalMapper::TQSignalMapper( TQObject* parent, const char* name ) : + TQObject( parent, name ) +{ + d = new TQSignalMapperData; +} + +/*! + Destroys the TQSignalMapper. +*/ +TQSignalMapper::~TQSignalMapper() +{ + delete d; +} + +/*! + Adds a mapping so that when map() is signaled from the given \a + sender, the signal mapped(\a identifier) is emitted. + + There may be at most one integer identifier for each object. +*/ +void TQSignalMapper::setMapping( const TQObject* sender, int identifier ) +{ + TQSignalMapperRec* rec = getRec(sender); + rec->int_id = identifier; + rec->has_int = 1; +} + +/*! + \overload + + Adds a mapping so that when map() is signaled from the given \a + sender, the signal mapper(\a identifier) is emitted. + + There may be at most one string identifier for each object, and it + may not be empty. +*/ +void TQSignalMapper::setMapping( const TQObject* sender, const TQString &identifier ) +{ + TQSignalMapperRec* rec = getRec(sender); + rec->str_id = identifier; +} + +/*! + Removes all mappings for \a sender. This is done automatically + when mapped objects are destroyed. +*/ +void TQSignalMapper::removeMappings( const TQObject* sender ) +{ + d->dict.remove((void*)sender); +} + +void TQSignalMapper::removeMapping() +{ + removeMappings(sender()); +} + +/*! + This slot emits signals based on which object sends signals to it. +*/ +void TQSignalMapper::map() +{ + const TQObject* s = sender(); + TQSignalMapperRec* rec = d->dict.find( (void*)s ); + if ( rec ) { + if ( rec->has_int ) + emit mapped( rec->int_id ); + if ( !rec->str_id.isEmpty() ) + emit mapped( rec->str_id ); + } +} + +TQSignalMapperRec* TQSignalMapper::getRec( const TQObject* sender ) +{ + TQSignalMapperRec* rec = d->dict.find( (void*)sender ); + if (!rec) { + rec = new TQSignalMapperRec; + d->dict.insert( (void*)sender, rec ); + connect( sender, SIGNAL(destroyed()), this, SLOT(removeMapping()) ); + } + return rec; +} + +/*! + \fn void TQSignalMapper::mapped(int) + + This signal is emitted when map() is signaled from an object that + has an integer mapping set. + + \sa setMapping() +*/ + +/*! + \overload void TQSignalMapper::mapped(const TQString&) + + This signal is emitted when map() is signaled from an object that + has a string mapping set. + + \sa setMapping() +*/ +#endif //QT_NO_SIGNALMAPPER diff --git a/src/kernel/qsignalmapper.h b/src/kernel/qsignalmapper.h new file mode 100644 index 000000000..26df12d46 --- /dev/null +++ b/src/kernel/qsignalmapper.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Definition of TQSignalMapper class +** +** Created : 980503 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSIGNALMAPPER_H +#define TQSIGNALMAPPER_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H +#ifndef QT_NO_SIGNALMAPPER +class TQSignalMapperData; +struct TQSignalMapperRec; + + +class Q_EXPORT TQSignalMapper : public TQObject { + Q_OBJECT +public: + TQSignalMapper( TQObject* parent, const char* name=0 ); + ~TQSignalMapper(); + + virtual void setMapping( const TQObject* sender, int identifier ); + virtual void setMapping( const TQObject* sender, const TQString &identifier ); + void removeMappings( const TQObject* sender ); + +signals: + void mapped(int); + void mapped(const TQString &); + +public slots: + void map(); + +private: + TQSignalMapperData* d; + TQSignalMapperRec* getRec( const TQObject* ); + +private slots: + void removeMapping(); +}; + +#endif // QT_NO_SIGNALMAPPER +#endif // TQSIGNALMAPPER_H diff --git a/src/kernel/qsignalslotimp.h b/src/kernel/qsignalslotimp.h new file mode 100644 index 000000000..5c6c95f82 --- /dev/null +++ b/src/kernel/qsignalslotimp.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Definition of signal/slot collections etc. +** +** Created : 980821 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSIGNALSLOTIMP_H +#define TQSIGNALSLOTIMP_H + +#ifndef QT_H +#include "qconnection.h" +#include "qptrlist.h" +#include "qptrvector.h" +#endif // QT_H + +class Q_EXPORT TQConnectionList : public TQPtrList +{ +public: + TQConnectionList() : TQPtrList() {} + TQConnectionList( const TQConnectionList &list ) : TQPtrList(list) {} + ~TQConnectionList() { clear(); } + TQConnectionList &operator=(const TQConnectionList &list) + { return (TQConnectionList&)TQPtrList::operator=(list); } +}; + +class Q_EXPORT TQConnectionListIt : public TQPtrListIterator +{ +public: + TQConnectionListIt( const TQConnectionList &l ) : TQPtrListIterator(l) {} + TQConnectionListIt &operator=(const TQConnectionListIt &i) + { return (TQConnectionListIt&)TQPtrListIterator::operator=(i); } +}; + +#if defined(Q_TEMPLATEDLL) && defined(Q_CC_INTEL) +// MOC_SKIP_BEGIN +Q_TEMPLATE_EXTERN template class Q_EXPORT TQPtrVector; +#define Q_EXPORTED_QPTRVECTORCONNECTTIONLIST_TEMPLATES +// MOC_SKIP_END +#endif + +class Q_EXPORT TQSignalVec : public TQPtrVector +{ +public: + TQSignalVec(int size=17 ) + : TQPtrVector(size) {} + TQSignalVec( const TQSignalVec &dict ) + : TQPtrVector(dict) {} + ~TQSignalVec() { clear(); } + TQSignalVec &operator=(const TQSignalVec &dict) + { return (TQSignalVec&)TQPtrVector::operator=(dict); } + TQConnectionList* at( uint index ) const { + return index >= size()? 0 : TQPtrVector::at(index); + } + bool insert( uint index, const TQConnectionList* d ) { + if (index >= size() ) + resize( 2*index + 1); + return TQPtrVector::insert(index, d); + } +}; + +#define Q_DEFINED_QCONNECTION_LIST +#include "qwinexport.h" +#endif // TQSIGNALSLOTIMP_H diff --git a/src/kernel/qsimplerichtext.cpp b/src/kernel/qsimplerichtext.cpp new file mode 100644 index 000000000..62450883b --- /dev/null +++ b/src/kernel/qsimplerichtext.cpp @@ -0,0 +1,421 @@ +/**************************************************************************** +** +** Implementation of the TQSimpleRichText class +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qsimplerichtext.h" + +#ifndef QT_NO_RICHTEXT +#include "qrichtext_p.h" +#include "qapplication.h" + +class TQSimpleRichTextData +{ +public: + TQTextDocument *doc; + TQFont font; + int cachedWidth; + bool cachedWidthWithPainter; + void adjustSize(TQPainter *p = 0); +}; + +// Pull this private function in from qglobal.cpp +extern unsigned int qt_int_sqrt( unsigned int n ); + +void TQSimpleRichTextData::adjustSize(TQPainter *p) { + TQFontMetrics fm( font ); + int mw = fm.width( 'x' ) * 80; + int w = mw; + doc->doLayout(p, w); + if ( doc->widthUsed() != 0 ) { + w = qt_int_sqrt( 5 * doc->height() * doc->widthUsed() / 3 ); + doc->doLayout(p, TQMIN(w, mw)); + + if ( w*3 < 5*doc->height() ) { + w = qt_int_sqrt( 2 * doc->height() * doc->widthUsed() ); + doc->doLayout(p, TQMIN(w, mw)); + } + } + cachedWidth = doc->width(); + cachedWidthWithPainter = FALSE; +} + +/*! + \class TQSimpleRichText qsimplerichtext.h + \brief The TQSimpleRichText class provides a small displayable piece of rich text. + + \ingroup text + \mainclass + + This class encapsulates simple rich text usage in which a string + is interpreted as rich text and can be drawn. This is particularly + useful if you want to display some rich text in a custom widget. A + TQStyleSheet is needed to interpret the tags and format the rich + text. TQt provides a default HTML-like style sheet, but you may + define custom style sheets. + + Once created, the rich text object can be queried for its width(), + height(), and the actual width used (see widthUsed()). Most + importantly, it can be drawn on any given TQPainter with draw(). + TQSimpleRichText can also be used to implement hypertext or active + text facilities by using anchorAt(). A hit test through inText() + makes it possible to use simple rich text for text objects in + editable drawing canvases. + + Once constructed from a string the contents cannot be changed, + only resized. If the contents change, just throw the rich text + object away and make a new one with the new contents. + + For large documents use TQTextEdit or TQTextBrowser. For very small + items of rich text you can use a TQLabel. + + If you are using TQSimpleRichText to print in high resolution you + should call setWidth(TQPainter, int) so that the content will be + laid out properly on the page. +*/ + +/*! + Constructs a TQSimpleRichText from the rich text string \a text and + the font \a fnt. + + The font is used as a basis for the text rendering. When using + rich text rendering on a widget \e w, you would normally specify + the widget's font, for example: + + \code + TQSimpleRichText myrichtext( contents, mywidget->font() ); + \endcode + + \a context is the optional context of the rich text object. This + becomes important if \a text contains relative references, for + example within image tags. TQSimpleRichText always uses the default + mime source factory (see \l{TQMimeSourceFactory::defaultFactory()}) + to resolve those references. The context will then be used to + calculate the absolute path. See + TQMimeSourceFactory::makeAbsolute() for details. + + The \a sheet is an optional style sheet. If it is 0, the default + style sheet will be used (see \l{TQStyleSheet::defaultSheet()}). +*/ + +TQSimpleRichText::TQSimpleRichText( const TQString& text, const TQFont& fnt, + const TQString& context, const TQStyleSheet* sheet ) +{ + d = new TQSimpleRichTextData; + d->cachedWidth = -1; + d->cachedWidthWithPainter = FALSE; + d->font = fnt; + d->doc = new TQTextDocument( 0 ); + d->doc->setTextFormat( TQt::RichText ); + d->doc->setLeftMargin( 0 ); + d->doc->setRightMargin( 0 ); + d->doc->setFormatter( new TQTextFormatterBreakWords ); + d->doc->setStyleSheet( (TQStyleSheet*)sheet ); + d->doc->setDefaultFormat( fnt, TQColor() ); + d->doc->setText( text, context ); +} + + +/*! + Constructs a TQSimpleRichText from the rich text string \a text and + the font \a fnt. + + This is a slightly more complex constructor for TQSimpleRichText + that takes an additional mime source factory \a factory, a page + break parameter \a pageBreak and a bool \a linkUnderline. \a + linkColor is only provided for compatibility, but has no effect, + as TQColorGroup's TQColorGroup::link() color is used now. + + \a context is the optional context of the rich text object. This + becomes important if \a text contains relative references, for + example within image tags. TQSimpleRichText always uses the default + mime source factory (see \l{TQMimeSourceFactory::defaultFactory()}) + to resolve those references. The context will then be used to + calculate the absolute path. See + TQMimeSourceFactory::makeAbsolute() for details. + + The \a sheet is an optional style sheet. If it is 0, the default + style sheet will be used (see \l{TQStyleSheet::defaultSheet()}). + + This constructor is useful for creating a TQSimpleRichText object + suitable for printing. Set \a pageBreak to be the height of the + contents area of the pages. +*/ + +TQSimpleRichText::TQSimpleRichText( const TQString& text, const TQFont& fnt, + const TQString& context, const TQStyleSheet* sheet, + const TQMimeSourceFactory* factory, int pageBreak, + const TQColor& /*linkColor*/, bool linkUnderline ) +{ + d = new TQSimpleRichTextData; + d->cachedWidth = -1; + d->cachedWidthWithPainter = FALSE; + d->font = fnt; + d->doc = new TQTextDocument( 0 ); + d->doc->setTextFormat( TQt::RichText ); + d->doc->setFormatter( new TQTextFormatterBreakWords ); + d->doc->setStyleSheet( (TQStyleSheet*)sheet ); + d->doc->setDefaultFormat( fnt, TQColor() ); + d->doc->flow()->setPageSize( pageBreak ); + d->doc->setPageBreakEnabled( TRUE ); +#ifndef QT_NO_MIME + d->doc->setMimeSourceFactory( (TQMimeSourceFactory*)factory ); +#endif + d->doc->setUnderlineLinks( linkUnderline ); + d->doc->setText( text, context ); +} + +/*! + Destroys the rich text object, freeing memory. +*/ + +TQSimpleRichText::~TQSimpleRichText() +{ + delete d->doc; + delete d; +} + +/*! + \overload + + Sets the width of the rich text object to \a w pixels. + + \sa height(), adjustSize() +*/ + +void TQSimpleRichText::setWidth( int w ) +{ + if ( w == d->cachedWidth && !d->cachedWidthWithPainter ) + return; + d->doc->formatter()->setAllowBreakInWords( d->doc->isPageBreakEnabled() ); + d->cachedWidth = w; + d->cachedWidthWithPainter = FALSE; + d->doc->doLayout( 0, w ); +} + +/*! + Sets the width of the rich text object to \a w pixels, + recalculating the layout as if it were to be drawn with painter \a + p. + + Passing a painter is useful when you intend drawing on devices + other than the screen, for example a TQPrinter. + + \sa height(), adjustSize() +*/ + +void TQSimpleRichText::setWidth( TQPainter *p, int w ) +{ + if ( w == d->cachedWidth && d->cachedWidthWithPainter ) + return; + d->doc->formatter()->setAllowBreakInWords( d->doc->isPageBreakEnabled() || + p && p->device() && + p->device()->devType() == TQInternal::Printer ); + p->save(); + d->cachedWidth = w; + d->cachedWidthWithPainter = TRUE; + d->doc->doLayout( p, w ); + p->restore(); +} + +/*! + Returns the set width of the rich text object in pixels. + + \sa widthUsed() +*/ + +int TQSimpleRichText::width() const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + return d->doc->width(); +} + +/*! + Returns the width in pixels that is actually used by the rich text + object. This can be smaller or wider than the set width. + + It may be wider, for example, if the text contains images or + non-breakable words that are already wider than the available + space. It's smaller when the object only consists of lines that do + not fill the width completely. + + \sa width() +*/ + +int TQSimpleRichText::widthUsed() const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + return d->doc->widthUsed(); +} + +/*! + Returns the height of the rich text object in pixels. + + \sa setWidth() +*/ + +int TQSimpleRichText::height() const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + return d->doc->height(); +} + +/*! + Adjusts the richt text object to a reasonable size. + + \sa setWidth() +*/ + +void TQSimpleRichText::adjustSize() +{ + d->adjustSize(); +} + +/*! + Draws the formatted text with painter \a p, at position (\a x, \a + y), clipped to \a clipRect. The clipping rectangle is given in the + rich text object's coordinates translated by (\a x, \a y). Passing + an null rectangle results in no clipping. Colors from the color + group \a cg are used as needed, and if not 0, \a *paper is used as + the background brush. + + Note that the display code is highly optimized to reduce flicker, + so passing a brush for \a paper is preferable to simply clearing + the area to be painted and then calling this without a brush. +*/ + +void TQSimpleRichText::draw( TQPainter *p, int x, int y, const TQRect& clipRect, + const TQColorGroup& cg, const TQBrush* paper ) const +{ + p->save(); + if ( d->cachedWidth < 0 ) + d->adjustSize(p); + + TQRect r = clipRect; + if ( !r.isNull() ) + r.moveBy( -x, -y ); + + if ( paper ) + d->doc->setPaper( new TQBrush( *paper ) ); + TQColorGroup g = cg; + if ( d->doc->paper() ) + g.setBrush( TQColorGroup::Base, *d->doc->paper() ); + + if ( !clipRect.isNull() ) + p->setClipRect( clipRect, TQPainter::CoordPainter ); + p->translate( x, y ); + d->doc->draw( p, r, g, paper ); + p->translate( -x, -y ); + p->restore(); +} + + +/*! \fn void TQSimpleRichText::draw( TQPainter *p, int x, int y, const TQRegion& clipRegion, + const TQColorGroup& cg, const TQBrush* paper ) const + + \obsolete + + Use the version with clipRect instead. The region version has + problems with larger documents on some platforms (on X11 regions + internally are represented with 16bit coordinates). +*/ + + + +/*! + Returns the context of the rich text object. If no context has + been specified in the constructor, a null string is returned. The + context is the path to use to look up relative links, such as + image tags and anchor references. +*/ + +TQString TQSimpleRichText::context() const +{ + return d->doc->context(); +} + +/*! + Returns the anchor at the requested position, \a pos. An empty + string is returned if no anchor is specified for this position. +*/ + +TQString TQSimpleRichText::anchorAt( const TQPoint& pos ) const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + TQTextCursor c( d->doc ); + c.place( pos, d->doc->firstParagraph(), TRUE ); + return c.paragraph()->at( c.index() )->anchorHref(); +} + +/*! + Returns TRUE if \a pos is within a text line of the rich text + object; otherwise returns FALSE. +*/ + +bool TQSimpleRichText::inText( const TQPoint& pos ) const +{ + if ( d->cachedWidth < 0 ) + d->adjustSize(); + if ( pos.y() > d->doc->height() ) + return FALSE; + TQTextCursor c( d->doc ); + c.place( pos, d->doc->firstParagraph() ); + return c.totalOffsetX() + c.paragraph()->at( c.index() )->x + + c.paragraph()->at( c.index() )->format()->width( c.paragraph()->at( c.index() )->c ) > pos.x(); +} + +/*! + Sets the default font for the rich text object to \a f +*/ + +void TQSimpleRichText::setDefaultFont( const TQFont &f ) +{ + if ( d->font == f ) + return; + d->font = f; + d->cachedWidth = -1; + d->cachedWidthWithPainter = FALSE; + d->doc->setDefaultFormat( f, TQColor() ); + d->doc->setText( d->doc->originalText(), d->doc->context() ); +} + +#endif //QT_NO_RICHTEXT diff --git a/src/kernel/qsimplerichtext.h b/src/kernel/qsimplerichtext.h new file mode 100644 index 000000000..9890f91c0 --- /dev/null +++ b/src/kernel/qsimplerichtext.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Definition of the TQSimpleRichText class +** +** Created : 990101 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSIMPLERICHTEXT_H +#define TQSIMPLERICHTEXT_H + +#ifndef QT_H +#include "qnamespace.h" +#include "qstring.h" +#include "qregion.h" +#endif // QT_H + +#ifndef QT_NO_RICHTEXT + +class TQPainter; +class TQWidget; +class TQStyleSheet; +class TQBrush; +class TQMimeSourceFactory; +class TQSimpleRichTextData; + +class Q_EXPORT TQSimpleRichText +{ +public: + TQSimpleRichText( const TQString& text, const TQFont& fnt, + const TQString& context = TQString::null, const TQStyleSheet* sheet = 0); + TQSimpleRichText( const TQString& text, const TQFont& fnt, + const TQString& context, const TQStyleSheet* sheet, + const TQMimeSourceFactory* factory, int pageBreak = -1, + const TQColor& linkColor = TQt::blue, bool linkUnderline = TRUE ); + ~TQSimpleRichText(); + + void setWidth( int ); + void setWidth( TQPainter*, int ); + void setDefaultFont( const TQFont &f ); + int width() const; + int widthUsed() const; + int height() const; + void adjustSize(); + + void draw( TQPainter* p, int x, int y, const TQRect& clipRect, + const TQColorGroup& cg, const TQBrush* paper = 0) const; + + // obsolete + void draw( TQPainter* p, int x, int y, const TQRegion& clipRegion, + const TQColorGroup& cg, const TQBrush* paper = 0) const { + draw( p, x, y, clipRegion.boundingRect(), cg, paper ); + } + + TQString context() const; + TQString anchorAt( const TQPoint& pos ) const; + + bool inText( const TQPoint& pos ) const; + +private: + TQSimpleRichTextData* d; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQSimpleRichText( const TQSimpleRichText & ); + TQSimpleRichText &operator=( const TQSimpleRichText & ); +#endif +}; + +#endif // QT_NO_RICHTEXT + +#endif // TQSIMPLERICHTEXT_H diff --git a/src/kernel/qsize.cpp b/src/kernel/qsize.cpp new file mode 100644 index 000000000..197496f99 --- /dev/null +++ b/src/kernel/qsize.cpp @@ -0,0 +1,432 @@ +/**************************************************************************** +** +** Implementation of TQSize class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qsize.h" +#include "qdatastream.h" + + +/*! + \class TQSize + \brief The TQSize class defines the size of a two-dimensional object. + + \ingroup images + \ingroup graphics + + A size is specified by a width and a height. + + The coordinate type is TQCOORD (defined in \c as \c int). + The minimum value of TQCOORD is TQCOORD_MIN (-2147483648) and the maximum + value is TQCOORD_MAX (2147483647). + + The size can be set in the constructor and changed with setWidth() + and setHeight(), or using operator+=(), operator-=(), operator*=() + and operator/=(), etc. You can swap the width and height with + transpose(). You can get a size which holds the maximum height and + width of two sizes using expandedTo(), and the minimum height and + width of two sizes using boundedTo(). + + + \sa TQPoint, TQRect +*/ + + +/***************************************************************************** + TQSize member functions + *****************************************************************************/ + +/*! + \fn TQSize::TQSize() + Constructs a size with invalid (negative) width and height. +*/ + +/*! + \fn TQSize::TQSize( int w, int h ) + Constructs a size with width \a w and height \a h. +*/ + +/*! + \fn bool TQSize::isNull() const + Returns TRUE if the width is 0 and the height is 0; otherwise + returns FALSE. +*/ + +/*! + \fn bool TQSize::isEmpty() const + Returns TRUE if the width is less than or equal to 0, or the height is + less than or equal to 0; otherwise returns FALSE. +*/ + +/*! + \fn bool TQSize::isValid() const + Returns TRUE if the width is equal to or greater than 0 and the height is + equal to or greater than 0; otherwise returns FALSE. +*/ + +/*! + \fn int TQSize::width() const + Returns the width. + \sa height() +*/ + +/*! + \fn int TQSize::height() const + Returns the height. + \sa width() +*/ + +/*! + \fn void TQSize::setWidth( int w ) + Sets the width to \a w. + \sa width(), setHeight() +*/ + +/*! + \fn void TQSize::setHeight( int h ) + Sets the height to \a h. + \sa height(), setWidth() +*/ + +/*! + Swaps the values of width and height. +*/ + +void TQSize::transpose() +{ + TQCOORD tmp = wd; + wd = ht; + ht = tmp; +} + +/*! \enum TQSize::ScaleMode + + This enum type defines the different ways of scaling a size. + + \img scaling.png + + \value ScaleFree The size is scaled freely. The ratio is not preserved. + \value ScaleMin The size is scaled to a rectangle as large as possible + inside a given rectangle, preserving the aspect ratio. + \value ScaleMax The size is scaled to a rectangle as small as possible + outside a given rectangle, preserving the aspect ratio. + + \sa TQSize::scale(), TQImage::scale(), TQImage::smoothScale() +*/ + +/*! + Scales the size to a rectangle of width \a w and height \a h according + to the ScaleMode \a mode. + + \list + \i If \a mode is \c ScaleFree, the size is set to (\a w, \a h). + \i If \a mode is \c ScaleMin, the current size is scaled to a rectangle + as large as possible inside (\a w, \a h), preserving the aspect ratio. + \i If \a mode is \c ScaleMax, the current size is scaled to a rectangle + as small as possible outside (\a w, \a h), preserving the aspect ratio. + \endlist + + Example: + \code + TQSize t1( 10, 12 ); + t1.scale( 60, 60, TQSize::ScaleFree ); + // t1 is (60, 60) + + TQSize t2( 10, 12 ); + t2.scale( 60, 60, TQSize::ScaleMin ); + // t2 is (50, 60) + + TQSize t3( 10, 12 ); + t3.scale( 60, 60, TQSize::ScaleMax ); + // t3 is (60, 72) + \endcode +*/ +void TQSize::scale( int w, int h, ScaleMode mode ) +{ + if ( mode == ScaleFree ) { + wd = (TQCOORD)w; + ht = (TQCOORD)h; + } else { + bool useHeight = TRUE; + int w0 = width(); + int h0 = height(); + int rw = h * w0 / h0; + + if ( mode == ScaleMin ) { + useHeight = ( rw <= w ); + } else { // mode == ScaleMax + useHeight = ( rw >= w ); + } + + if ( useHeight ) { + wd = (TQCOORD)rw; + ht = (TQCOORD)h; + } else { + wd = (TQCOORD)w; + ht = (TQCOORD)( w * h0 / w0 ); + } + } +} + +/*! + \overload + + Equivalent to scale(\a{s}.width(), \a{s}.height(), \a mode). +*/ +void TQSize::scale( const TQSize &s, ScaleMode mode ) +{ + scale( s.width(), s.height(), mode ); +} + +/*! + \fn TQCOORD &TQSize::rwidth() + Returns a reference to the width. + + Using a reference makes it possible to directly manipulate the width. + + Example: + \code + TQSize s( 100, 10 ); + s.rwidth() += 20; // s becomes (120,10) + \endcode + + \sa rheight() +*/ + +/*! + \fn TQCOORD &TQSize::rheight() + Returns a reference to the height. + + Using a reference makes it possible to directly manipulate the height. + + Example: + \code + TQSize s( 100, 10 ); + s.rheight() += 5; // s becomes (100,15) + \endcode + + \sa rwidth() +*/ + +/*! + \fn TQSize &TQSize::operator+=( const TQSize &s ) + + Adds \a s to the size and returns a reference to this size. + + Example: + \code + TQSize s( 3, 7 ); + TQSize r( -1, 4 ); + s += r; // s becomes (2,11) +\endcode +*/ + +/*! + \fn TQSize &TQSize::operator-=( const TQSize &s ) + + Subtracts \a s from the size and returns a reference to this size. + + Example: + \code + TQSize s( 3, 7 ); + TQSize r( -1, 4 ); + s -= r; // s becomes (4,3) + \endcode +*/ + +/*! + \fn TQSize &TQSize::operator*=( int c ) + Multiplies both the width and height by \a c and returns a reference to + the size. +*/ + +/*! + \overload TQSize &TQSize::operator*=( double c ) + + Multiplies both the width and height by \a c and returns a reference to + the size. + + Note that the result is truncated. +*/ + +/*! + \fn bool operator==( const TQSize &s1, const TQSize &s2 ) + \relates TQSize + Returns TRUE if \a s1 and \a s2 are equal; otherwise returns FALSE. +*/ + +/*! + \fn bool operator!=( const TQSize &s1, const TQSize &s2 ) + \relates TQSize + Returns TRUE if \a s1 and \a s2 are different; otherwise returns FALSE. +*/ + +/*! + \fn const TQSize operator+( const TQSize &s1, const TQSize &s2 ) + \relates TQSize + Returns the sum of \a s1 and \a s2; each component is added separately. +*/ + +/*! + \fn const TQSize operator-( const TQSize &s1, const TQSize &s2 ) + \relates TQSize + Returns \a s2 subtracted from \a s1; each component is + subtracted separately. +*/ + +/*! + \fn const TQSize operator*( const TQSize &s, int c ) + \relates TQSize + Multiplies \a s by \a c and returns the result. +*/ + +/*! + \overload const TQSize operator*( int c, const TQSize &s ) + \relates TQSize + Multiplies \a s by \a c and returns the result. +*/ + +/*! + \overload const TQSize operator*( const TQSize &s, double c ) + \relates TQSize + Multiplies \a s by \a c and returns the result. +*/ + +/*! + \overload const TQSize operator*( double c, const TQSize &s ) + \relates TQSize + Multiplies \a s by \a c and returns the result. +*/ + +/*! + \fn TQSize &TQSize::operator/=( int c ) + Divides both the width and height by \a c and returns a reference to the + size. +*/ + +/*! + \fn TQSize &TQSize::operator/=( double c ) + \overload + Divides both the width and height by \a c and returns a reference to the + size. + + Note that the result is truncated. +*/ + +/*! + \fn const TQSize operator/( const TQSize &s, int c ) + \relates TQSize + Divides \a s by \a c and returns the result. +*/ + +/*! + \fn const TQSize operator/( const TQSize &s, double c ) + \relates TQSize + \overload + Divides \a s by \a c and returns the result. + + Note that the result is truncated. +*/ + +/*! + \fn TQSize TQSize::expandedTo( const TQSize & otherSize ) const + + Returns a size with the maximum width and height of this size and + \a otherSize. +*/ + +/*! + \fn TQSize TQSize::boundedTo( const TQSize & otherSize ) const + + Returns a size with the minimum width and height of this size and + \a otherSize. +*/ + + +void TQSize::warningDivByZero() +{ +#if defined(QT_CHECK_MATH) + qWarning( "TQSize: Division by zero error" ); +#endif +} + + +/***************************************************************************** + TQSize stream functions + *****************************************************************************/ +#ifndef QT_NO_DATASTREAM +/*! + \relates TQSize + Writes the size \a sz to the stream \a s and returns a reference to + the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator<<( TQDataStream &s, const TQSize &sz ) +{ + if ( s.version() == 1 ) + s << (Q_INT16)sz.width() << (Q_INT16)sz.height(); + else + s << (Q_INT32)sz.width() << (Q_INT32)sz.height(); + return s; +} + +/*! + \relates TQSize + Reads the size from the stream \a s into size \a sz and returns a + reference to the stream. + + \sa \link datastreamformat.html Format of the TQDataStream operators \endlink +*/ + +TQDataStream &operator>>( TQDataStream &s, TQSize &sz ) +{ + if ( s.version() == 1 ) { + Q_INT16 w, h; + s >> w; sz.rwidth() = w; + s >> h; sz.rheight() = h; + } + else { + Q_INT32 w, h; + s >> w; sz.rwidth() = w; + s >> h; sz.rheight() = h; + } + return s; +} +#endif // QT_NO_DATASTREAM diff --git a/src/kernel/qsize.h b/src/kernel/qsize.h new file mode 100644 index 000000000..380f3b85e --- /dev/null +++ b/src/kernel/qsize.h @@ -0,0 +1,237 @@ +/**************************************************************************** +** +** Definition of TQSize class +** +** Created : 931028 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSIZE_H +#define TQSIZE_H + +#ifndef QT_H +#include "qpoint.h" // ### change to qwindowdefs.h? +#endif // QT_H + +class Q_EXPORT TQSize +// ### Make TQSize inherit TQt in TQt 4.0 +{ +public: + // ### Move this enum to qnamespace.h in TQt 4.0 + enum ScaleMode { + ScaleFree, + ScaleMin, + ScaleMax + }; + + TQSize(); + TQSize( int w, int h ); + + bool isNull() const; + bool isEmpty() const; + bool isValid() const; + + int width() const; + int height() const; + void setWidth( int w ); + void setHeight( int h ); + void transpose(); + + void scale( int w, int h, ScaleMode mode ); + void scale( const TQSize &s, ScaleMode mode ); + + TQSize expandedTo( const TQSize & ) const; + TQSize boundedTo( const TQSize & ) const; + + TQCOORD &rwidth(); + TQCOORD &rheight(); + + TQSize &operator+=( const TQSize & ); + TQSize &operator-=( const TQSize & ); + TQSize &operator*=( int c ); + TQSize &operator*=( double c ); + TQSize &operator/=( int c ); + TQSize &operator/=( double c ); + + friend inline bool operator==( const TQSize &, const TQSize & ); + friend inline bool operator!=( const TQSize &, const TQSize & ); + friend inline const TQSize operator+( const TQSize &, const TQSize & ); + friend inline const TQSize operator-( const TQSize &, const TQSize & ); + friend inline const TQSize operator*( const TQSize &, int ); + friend inline const TQSize operator*( int, const TQSize & ); + friend inline const TQSize operator*( const TQSize &, double ); + friend inline const TQSize operator*( double, const TQSize & ); + friend inline const TQSize operator/( const TQSize &, int ); + friend inline const TQSize operator/( const TQSize &, double ); + +private: + static void warningDivByZero(); + + TQCOORD wd; + TQCOORD ht; +}; + + +/***************************************************************************** + TQSize stream functions + *****************************************************************************/ + +Q_EXPORT TQDataStream &operator<<( TQDataStream &, const TQSize & ); +Q_EXPORT TQDataStream &operator>>( TQDataStream &, TQSize & ); + + +/***************************************************************************** + TQSize inline functions + *****************************************************************************/ + +inline TQSize::TQSize() +{ wd = ht = -1; } + +inline TQSize::TQSize( int w, int h ) +{ wd=(TQCOORD)w; ht=(TQCOORD)h; } + +inline bool TQSize::isNull() const +{ return wd==0 && ht==0; } + +inline bool TQSize::isEmpty() const +{ return wd<1 || ht<1; } + +inline bool TQSize::isValid() const +{ return wd>=0 && ht>=0; } + +inline int TQSize::width() const +{ return wd; } + +inline int TQSize::height() const +{ return ht; } + +inline void TQSize::setWidth( int w ) +{ wd=(TQCOORD)w; } + +inline void TQSize::setHeight( int h ) +{ ht=(TQCOORD)h; } + +inline TQCOORD &TQSize::rwidth() +{ return wd; } + +inline TQCOORD &TQSize::rheight() +{ return ht; } + +inline TQSize &TQSize::operator+=( const TQSize &s ) +{ wd+=s.wd; ht+=s.ht; return *this; } + +inline TQSize &TQSize::operator-=( const TQSize &s ) +{ wd-=s.wd; ht-=s.ht; return *this; } + +inline TQSize &TQSize::operator*=( int c ) +{ wd*=(TQCOORD)c; ht*=(TQCOORD)c; return *this; } + +inline TQSize &TQSize::operator*=( double c ) +{ wd=(TQCOORD)(wd*c); ht=(TQCOORD)(ht*c); return *this; } + +inline bool operator==( const TQSize &s1, const TQSize &s2 ) +{ return s1.wd == s2.wd && s1.ht == s2.ht; } + +inline bool operator!=( const TQSize &s1, const TQSize &s2 ) +{ return s1.wd != s2.wd || s1.ht != s2.ht; } + +inline const TQSize operator+( const TQSize & s1, const TQSize & s2 ) +{ return TQSize(s1.wd+s2.wd, s1.ht+s2.ht); } + +inline const TQSize operator-( const TQSize &s1, const TQSize &s2 ) +{ return TQSize(s1.wd-s2.wd, s1.ht-s2.ht); } + +inline const TQSize operator*( const TQSize &s, int c ) +{ return TQSize(s.wd*c, s.ht*c); } + +inline const TQSize operator*( int c, const TQSize &s ) +{ return TQSize(s.wd*c, s.ht*c); } + +inline const TQSize operator*( const TQSize &s, double c ) +{ return TQSize((TQCOORD)(s.wd*c), (TQCOORD)(s.ht*c)); } + +inline const TQSize operator*( double c, const TQSize &s ) +{ return TQSize((TQCOORD)(s.wd*c), (TQCOORD)(s.ht*c)); } + +inline TQSize &TQSize::operator/=( int c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0 ) + warningDivByZero(); +#endif + wd/=(TQCOORD)c; ht/=(TQCOORD)c; + return *this; +} + +inline TQSize &TQSize::operator/=( double c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0.0 ) + warningDivByZero(); +#endif + wd=(TQCOORD)(wd/c); ht=(TQCOORD)(ht/c); + return *this; +} + +inline const TQSize operator/( const TQSize &s, int c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0 ) + TQSize::warningDivByZero(); +#endif + return TQSize(s.wd/c, s.ht/c); +} + +inline const TQSize operator/( const TQSize &s, double c ) +{ +#if defined(QT_CHECK_MATH) + if ( c == 0.0 ) + TQSize::warningDivByZero(); +#endif + return TQSize((TQCOORD)(s.wd/c), (TQCOORD)(s.ht/c)); +} + +inline TQSize TQSize::expandedTo( const TQSize & otherSize ) const +{ + return TQSize( TQMAX(wd,otherSize.wd), TQMAX(ht,otherSize.ht) ); +} + +inline TQSize TQSize::boundedTo( const TQSize & otherSize ) const +{ + return TQSize( TQMIN(wd,otherSize.wd), TQMIN(ht,otherSize.ht) ); +} + + +#endif // TQSIZE_H diff --git a/src/kernel/qsizegrip.cpp b/src/kernel/qsizegrip.cpp new file mode 100644 index 000000000..2cbc39e08 --- /dev/null +++ b/src/kernel/qsizegrip.cpp @@ -0,0 +1,286 @@ +/**************************************************************************** +** +** Implementation of TQSizeGrip class +** +** Created : 980119 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qsizegrip.h" + +#ifndef QT_NO_SIZEGRIP + +#include "qpainter.h" +#include "qapplication.h" +#include "qstyle.h" + +#if defined(Q_WS_X11) +#include "qt_x11_p.h" +extern Atom qt_sizegrip; // defined in qapplication_x11.cpp +#elif defined (Q_WS_WIN ) +#include "qobjectlist.h" +#include "qt_windows.h" +#elif defined(Q_WS_MAC) +bool qt_mac_update_sizer(TQWidget *, int); //qwidget_mac.cpp +#endif + + +static TQWidget *qt_sizegrip_topLevelWidget( TQWidget* w) +{ + TQWidget *p = w->parentWidget(); + while ( !w->isTopLevel() && p && !p->inherits("TQWorkspace") ) { + w = p; + p = p->parentWidget(); + } + return w; +} + +static TQWidget* qt_sizegrip_workspace( TQWidget* w ) +{ + while ( w && !w->inherits("TQWorkspace" ) ) { + if ( w->isTopLevel() ) + return 0; + w = w->parentWidget(); + } + return w; +} + + +/*! + \class TQSizeGrip qsizegrip.h + + \brief The TQSizeGrip class provides a corner-grip for resizing a top-level window. + + \ingroup application + \ingroup basic + \ingroup appearance + + This widget works like the standard Windows resize handle. In the + X11 version this resize handle generally works differently from + the one provided by the system; we hope to reduce this difference + in the future. + + Put this widget anywhere in a widget tree and the user can use it + to resize the top-level window. Generally, this should be in the + lower right-hand corner. Note that TQStatusBar already uses this + widget, so if you have a status bar (e.g. you are using + TQMainWindow), then you don't need to use this widget explicitly. + + + + \sa TQStatusBar +*/ + + +/*! + Constructs a resize corner called \a name, as a child widget of \a + parent. +*/ +TQSizeGrip::TQSizeGrip( TQWidget * parent, const char* name ) + : TQWidget( parent, name ) +{ +#ifndef QT_NO_CURSOR +#ifndef Q_WS_MAC + if ( TQApplication::reverseLayout() ) + setCursor( sizeBDiagCursor ); + else + setCursor( sizeFDiagCursor ); +#endif +#endif + setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Fixed ) ); +#if defined(Q_WS_X11) + if ( !qt_sizegrip_workspace( this ) ) { + WId id = winId(); + XChangeProperty(qt_xdisplay(), topLevelWidget()->winId(), + qt_sizegrip, XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&id, 1); + } +#endif + tlw = qt_sizegrip_topLevelWidget( this ); + if ( tlw ) + tlw->installEventFilter( this ); + installEventFilter( this ); //for binary compatibility fix in 4.0 with an event() ### +} + + +/*! + Destroys the size grip. +*/ +TQSizeGrip::~TQSizeGrip() +{ +#if defined(Q_WS_X11) + if ( !TQApplication::closingDown() && parentWidget() ) { + WId id = None; + XChangeProperty(qt_xdisplay(), topLevelWidget()->winId(), + qt_sizegrip, XA_WINDOW, 32, PropModeReplace, + (unsigned char *)&id, 1); + } +#endif +} + +/*! + Returns the size grip's size hint; this is a small size. +*/ +TQSize TQSizeGrip::sizeHint() const +{ + return (style().sizeFromContents(TQStyle::CT_SizeGrip, this, TQSize(13, 13)). + expandedTo(TQApplication::globalStrut())); +} + +/*! + Paints the resize grip. Resize grips are usually rendered as small + diagonal textured lines in the lower-right corner. The event is in + \a e. +*/ +void TQSizeGrip::paintEvent( TQPaintEvent *e ) +{ + TQPainter painter( this ); + painter.setClipRegion(e->region()); + style().drawPrimitive(TQStyle::PE_SizeGrip, &painter, rect(), colorGroup()); +} + +/*! + Primes the resize operation. The event is in \a e. +*/ +void TQSizeGrip::mousePressEvent( TQMouseEvent * e ) +{ + p = e->globalPos(); + s = qt_sizegrip_topLevelWidget(this)->size(); +} + + +/*! + Resizes the top-level widget containing this widget. The event is + in \a e. +*/ +void TQSizeGrip::mouseMoveEvent( TQMouseEvent * e ) +{ + if ( e->state() != LeftButton ) + return; + + TQWidget* tlw = qt_sizegrip_topLevelWidget(this); + if ( tlw->testWState(WState_ConfigPending) ) + return; + + TQPoint np( e->globalPos() ); + + TQWidget* ws = qt_sizegrip_workspace( this ); + if ( ws ) { + TQPoint tmp( ws->mapFromGlobal( np ) ); + if ( tmp.x() > ws->width() ) + tmp.setX( ws->width() ); + if ( tmp.y() > ws->height() ) + tmp.setY( ws->height() ); + np = ws->mapToGlobal( tmp ); + } + + int w; + int h = np.y() - p.y() + s.height(); + + if ( TQApplication::reverseLayout() ) + w = s.width() - ( np.x() - p.x() ); + else + w = np.x() - p.x() + s.width(); + + if ( w < 1 ) + w = 1; + if ( h < 1 ) + h = 1; + TQSize ms( tlw->minimumSizeHint() ); + ms = ms.expandedTo( minimumSize() ); + if ( w < ms.width() ) + w = ms.width(); + if ( h < ms.height() ) + h = ms.height(); + + if (TQApplication::reverseLayout()) { + tlw->resize( w, h ); + if (tlw->size() == TQSize(w,h)) + tlw->move( tlw->x() + ( np.x()-p.x() ), tlw->y() ); + } else { + tlw->resize( w, h ); + } +#ifdef Q_WS_WIN + MSG msg; + while( PeekMessage( &msg, winId(), WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE ) ) + ; +#endif + TQApplication::syncX(); + + if ( TQApplication::reverseLayout() && tlw->size() == TQSize(w,h) ) { + s.rwidth() = tlw->size().width(); + p.rx() = np.x(); + } +} + +/*! \reimp */ +bool TQSizeGrip::eventFilter( TQObject *o, TQEvent *e ) +{ + if ( o == tlw ) { + switch ( e->type() ) { +#ifndef Q_WS_MAC + /* The size grip goes no where on Mac OS X when you maximize! --Sam */ + case TQEvent::ShowMaximized: +#endif + case TQEvent::ShowFullScreen: + hide(); + break; + case TQEvent::ShowNormal: + show(); + break; + default: + break; + } + } else if(o == this) { +#if defined(Q_WS_MAC) + switch(e->type()) { + case TQEvent::Hide: + case TQEvent::Show: + if(!TQApplication::closingDown() && parentWidget() && !qt_sizegrip_workspace(this)) { + if(TQWidget *w = qt_sizegrip_topLevelWidget(this)) { + if(w->isTopLevel()) + qt_mac_update_sizer(w, e->type() == TQEvent::Hide ? -1 : 1); + } + } + break; + default: + break; + } + #endif + } + return FALSE; +} + +#endif //QT_NO_SIZEGRIP diff --git a/src/kernel/qsizegrip.h b/src/kernel/qsizegrip.h new file mode 100644 index 000000000..e6a3feacb --- /dev/null +++ b/src/kernel/qsizegrip.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Definition of TQSizeGrip class +** +** Created : 980316 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSIZEGRIP_H +#define TQSIZEGRIP_H + +#ifndef QT_H +#include "qwidget.h" +#endif // QT_H + +#ifndef QT_NO_SIZEGRIP + +class Q_EXPORT TQSizeGrip: public TQWidget +{ + Q_OBJECT +public: + TQSizeGrip( TQWidget* parent, const char* name=0 ); + ~TQSizeGrip(); + + TQSize sizeHint() const; + +protected: + void paintEvent( TQPaintEvent * ); + void mousePressEvent( TQMouseEvent * ); + void mouseMoveEvent( TQMouseEvent * ); + + bool eventFilter( TQObject *, TQEvent * ); + +private: + TQPoint p; + TQSize s; + int d; + TQWidget *tlw; +}; + +#endif //QT_NO_SIZEGRIP +#endif diff --git a/src/kernel/qsizepolicy.h b/src/kernel/qsizepolicy.h new file mode 100644 index 000000000..0a98fbc66 --- /dev/null +++ b/src/kernel/qsizepolicy.h @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Definition of the TQSizePolicy class +** +** Created : 980929 +** +** Copyright (C) 1998-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSIZEPOLICY_H +#define TQSIZEPOLICY_H + +#ifndef QT_H +#include "qglobal.h" +#endif // QT_H + +// Documentation is in qabstractlayout.cpp. + +class Q_EXPORT TQSizePolicy +{ +private: + enum SizePolicy_Internal { HSize = 6, HMask = 0x3f, VMask = HMask << HSize, + MayGrow = 1, ExpMask = 2, MayShrink = 4 }; +public: + enum SizeType { Fixed = 0, + Minimum = MayGrow, + Maximum = MayShrink, + Preferred = MayGrow | MayShrink, + MinimumExpanding = MayGrow | ExpMask, + Expanding = MayGrow | MayShrink | ExpMask, + Ignored = ExpMask /* magic value */ }; + + enum ExpandData { NoDirection = 0, + Horizontally = 1, + Vertically = 2, +#ifndef QT_NO_COMPAT + Horizontal = Horizontally, + Vertical = Vertically, +#endif + BothDirections = Horizontally | Vertically }; + + TQSizePolicy() : data( 0 ) { } + + TQSizePolicy( SizeType hor, SizeType ver, bool hfw = FALSE ) + : data( hor | (ver<> HSize ); } + + bool mayShrinkHorizontally() const { return horData() & MayShrink || horData() == Ignored; } + bool mayShrinkVertically() const { return verData() & MayShrink || verData() == Ignored; } + bool mayGrowHorizontally() const { return horData() & MayGrow || horData() == Ignored; } + bool mayGrowVertically() const { return verData() & MayGrow || verData() == Ignored; } + + ExpandData expanding() const + { + return (ExpandData)( (int)(verData() & ExpMask ? Vertically : 0) | + (int)(horData() & ExpMask ? Horizontally : 0) ); + } + + void setHorData( SizeType d ) { data = (Q_UINT32)(data & ~HMask) | d; } + void setVerData( SizeType d ) { data = (Q_UINT32)(data & ~(HMask << HSize)) | + (d << HSize); } + + void setHeightForWidth( bool b ) { data = b ? (Q_UINT32)( data | ( 1 << 2*HSize ) ) + : (Q_UINT32)( data & ~( 1 << 2*HSize ) ); } + bool hasHeightForWidth() const { return data & ( 1 << 2*HSize ); } + + bool operator==( const TQSizePolicy& s ) const { return data == s.data; } + bool operator!=( const TQSizePolicy& s ) const { return data != s.data; } + + + uint horStretch() const { return data >> 24; } + uint verStretch() const { return (data >> 16) & 0xff; } + void setHorStretch( uchar sf ) { data = (data&0x00ffffff) | (uint(sf)<<24); } + void setVerStretch( uchar sf ) { data = (data&0xff00ffff) | (uint(sf)<<16); } + inline void transpose(); + +private: + TQSizePolicy( int i ) : data( (Q_UINT32)i ) { } + + Q_UINT32 data; +}; + +inline TQSizePolicy::TQSizePolicy( SizeType hor, SizeType ver, uchar hors, uchar vers, bool hfw ) + : data( hor | (ver< +#endif + + +/*! + \class TQSocketNotifier qsocketnotifier.h + \brief The TQSocketNotifier class provides support for socket callbacks. + + \ingroup io + + This class makes it possible to write asynchronous socket-based + code in TQt. Using synchronous socket operations blocks the + program, which is clearly not acceptable for an event-driven GUI + program. + + Once you have opened a non-blocking socket (whether for TCP, UDP, + a UNIX-domain socket, or any other protocol family your operating + system supports), you can create a socket notifier to monitor the + socket. Then you connect the activated() signal to the slot you + want to be called when a socket event occurs. + + Note for Windows users: the socket passed to TQSocketNotifier will + become non-blocking, even if it was created as a blocking socket. + + There are three types of socket notifiers (read, write and + exception); you must specify one of these in the constructor. + + The type specifies when the activated() signal is to be emitted: + \list 1 + \i TQSocketNotifier::Read - There is data to be read (socket read event). + \i TQSocketNotifier::Write - Data can be written (socket write event). + \i TQSocketNofifier::Exception - An exception has occurred (socket + exception event). We recommend against using this. + \endlist + + For example, if you need to monitor both reads and writes for the + same socket you must create two socket notifiers. + + For read notifiers it makes little sense to connect the + activated() signal to more than one slot because the data can be + read from the socket only once. + + Also observe that if you do not read all the available data when + the read notifier fires, it fires again and again. + + For write notifiers, immediately disable the notifier after the + activated() signal has been received and you have sent the data to + be written on the socket. When you have more data to be written, + enable it again to get a new activated() signal. The exception is + if the socket data writing operation (send() or equivalent) fails + with a "would block" error, which means that some buffer is full + and you must wait before sending more data. In that case you do + not need to disable and re-enable the write notifier; it will fire + again as soon as the system allows more data to be sent. + + The behavior of a write notifier that is left in enabled state + after having emitting the first activated() signal (and no "would + block" error has occurred) is undefined. Depending on the + operating system, it may fire on every pass of the event loop or + not at all. + + If you need a time-out for your sockets you can use either \link + TQObject::startTimer() timer events\endlink or the TQTimer class. + + Socket action is detected in the \link TQApplication::exec() main + event loop\endlink of TQt. The X11 version of TQt has a single UNIX + select() call that incorporates all socket notifiers and the X + socket. + + Note that on XFree86 for OS/2, select() works only in the thread + in which main() is running; you should therefore use that thread + for GUI operations. + + \sa TQSocket, TQServerSocket, TQSocketDevice, TQFile::handle() +*/ + +/*! + \enum TQSocketNotifier::Type + + \value Read + \value Write + \value Exception +*/ + + +/*! + Constructs a socket notifier called \a name, with the parent, \a + parent. It watches \a socket for \a type events, and enables it. + + It is generally advisable to explicitly enable or disable the + socket notifier, especially for write notifiers. + + \sa setEnabled(), isEnabled() +*/ + +TQSocketNotifier::TQSocketNotifier( int socket, Type type, TQObject *parent, + const char *name ) + : TQObject( parent, name ) +{ +#if defined(QT_CHECK_RANGE) + if ( socket < 0 ) + qWarning( "TQSocketNotifier: Invalid socket specified" ); +# if defined(Q_OS_UNIX) + if ( socket >= FD_SETSIZE ) + qWarning( "TQSocketNotifier: Socket descriptor too large for select()" ); +# endif +#endif + sockfd = socket; + sntype = type; + snenabled = TRUE; + TQApplication::eventLoop()->registerSocketNotifier( this ); +} + +/*! + Destroys the socket notifier. +*/ + +TQSocketNotifier::~TQSocketNotifier() +{ + setEnabled( FALSE ); +} + + +/*! + \fn void TQSocketNotifier::activated( int socket ) + + This signal is emitted under certain conditions specified by the + notifier type(): + \list 1 + \i TQSocketNotifier::Read - There is data to be read (socket read event). + \i TQSocketNotifier::Write - Data can be written (socket write event). + \i TQSocketNofifier::Exception - An exception has occurred (socket + exception event). + \endlist + + The \a socket argument is the \link socket() socket\endlink identifier. + + \sa type(), socket() +*/ + + +/*! + \fn int TQSocketNotifier::socket() const + + Returns the socket identifier specified to the constructor. + + \sa type() +*/ + +/*! + \fn Type TQSocketNotifier::type() const + + Returns the socket event type specified to the constructor: \c + TQSocketNotifier::Read, \c TQSocketNotifier::Write, or \c + TQSocketNotifier::Exception. + + \sa socket() +*/ + + +/*! + \fn bool TQSocketNotifier::isEnabled() const + + Returns TRUE if the notifier is enabled; otherwise returns FALSE. + + \sa setEnabled() +*/ + +/*! + Enables the notifier if \a enable is TRUE or disables it if \a + enable is FALSE. + + The notifier is enabled by default. + + If the notifier is enabled, it emits the activated() signal + whenever a socket event corresponding to its \link type() + type\endlink occurs. If it is disabled, it ignores socket events + (the same effect as not creating the socket notifier). + + Write notifiers should normally be disabled immediately after the + activated() signal has been emitted; see discussion of write + notifiers in the \link #details class description\endlink above. + + \sa isEnabled(), activated() +*/ + +void TQSocketNotifier::setEnabled( bool enable ) +{ + if ( sockfd < 0 ) + return; + if ( snenabled == enable ) // no change + return; + snenabled = enable; + + TQEventLoop *eventloop = TQApplication::eventLoop(); + if ( ! eventloop ) // perhaps application is shutting down + return; + + if ( snenabled ) + eventloop->registerSocketNotifier( this ); + else + eventloop->unregisterSocketNotifier( this ); +} + + +/*!\reimp +*/ +bool TQSocketNotifier::event( TQEvent *e ) +{ + // Emits the activated() signal when a \c TQEvent::SockAct is + // received. + TQObject::event( e ); // will activate filters + if ( e->type() == TQEvent::SockAct ) { + emit activated( sockfd ); + return TRUE; + } + return FALSE; +} diff --git a/src/kernel/qsocketnotifier.h b/src/kernel/qsocketnotifier.h new file mode 100644 index 000000000..3565232b9 --- /dev/null +++ b/src/kernel/qsocketnotifier.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Definition of TQSocketNotifier class +** +** Created : 951114 +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#ifndef TQSOCKETNOTIFIER_H +#define TQSOCKETNOTIFIER_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + + +class Q_EXPORT TQSocketNotifier : public TQObject +{ + Q_OBJECT +public: + enum Type { Read, Write, Exception }; + + TQSocketNotifier( int socket, Type, TQObject *parent=0, const char *name=0 ); + ~TQSocketNotifier(); + + int socket() const; + Type type() const; + + bool isEnabled() const; + virtual void setEnabled( bool ); + +signals: + void activated( int socket ); + +protected: + bool event( TQEvent * ); + +private: + int sockfd; + Type sntype; + bool snenabled; + +private: // Disabled copy constructor and operator= +#if defined(Q_DISABLE_COPY) + TQSocketNotifier( const TQSocketNotifier & ); + TQSocketNotifier &operator=( const TQSocketNotifier & ); +#endif +}; + + +inline int TQSocketNotifier::socket() const +{ return sockfd; } + +inline TQSocketNotifier::Type TQSocketNotifier::type() const +{ return sntype; } + +inline bool TQSocketNotifier::isEnabled() const +{ return snenabled; } + + +#endif // TQSOCKETNOTIFIER_H diff --git a/src/kernel/qsound.cpp b/src/kernel/qsound.cpp new file mode 100644 index 000000000..14b685ea3 --- /dev/null +++ b/src/kernel/qsound.cpp @@ -0,0 +1,316 @@ +/**************************************************************************** +** +** Implementation of TQSound class and TQAuServer internal class +** +** Created : 000117 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#include "qsound.h" + +#ifndef QT_NO_SOUND + +#include "qptrlist.h" + +static TQPtrList *servers=0; + +TQAuServer::TQAuServer(TQObject* parent, const char* name) : + TQObject(parent,name) +{ + if ( !servers ) { + servers = new TQPtrList; + // ### add cleanup + } + servers->prepend(this); +} + +TQAuServer::~TQAuServer() +{ + servers->remove(this); + if ( servers->count() == 0 ) { + delete servers; + servers = 0; + } +} + +void TQAuServer::play(const TQString& filename) +{ + TQSound s(filename); + play(&s); +} + +extern TQAuServer* qt_new_audio_server(); + +static TQAuServer& server() +{ + if (!servers) qt_new_audio_server(); + return *servers->first(); +} + +class TQSoundData { +public: + TQSoundData(const TQString& fname) : + filename(fname), bucket(0), looprem(0), looptotal(1) + { + } + + ~TQSoundData() + { + delete bucket; + } + + TQString filename; + TQAuBucket* bucket; + int looprem; + int looptotal; +}; + +/*! + \class TQSound qsound.h + \brief The TQSound class provides access to the platform audio facilities. + + \ingroup multimedia + \mainclass + + TQt provides the most commonly retquired audio operation in GUI + applications: asynchronously playing a sound file. This is most + easily accomplished with a single call: + \code + TQSound::play("mysounds/bells.wav"); + \endcode + + A second API is provided in which a TQSound object is created from + a sound file and is played later: + \code + TQSound bells("mysounds/bells.wav"); + + bells.play(); + \endcode + + Sounds played using the second model may use more memory but play + more immediately than sounds played using the first model, + depending on the underlying platform audio facilities. + + On Microsoft Windows the underlying multimedia system is used; + only WAVE format sound files are supported. + + On X11 the \link ftp://ftp.x.org/contrib/audio/nas/ Network Audio + System\endlink is used if available, otherwise all operations work + silently. NAS supports WAVE and AU files. + + On Macintosh, ironically, we use QT (\link + http://tquicktime.apple.com QuickTime\endlink) for sound, this + means all QuickTime formats are supported by TQt/Mac. + + On TQt/Embedded, a built-in mixing sound server is used, which + accesses \c /dev/dsp directly. Only the WAVE format is supported. + + The availability of sound can be tested with + TQSound::isAvailable(). +*/ + +/*! + \fn static bool TQSound::available() + + Returns TRUE if sound support is available; otherwise returns FALSE. +*/ + +/*! + Plays the sound in a file called \a filename. +*/ +void TQSound::play(const TQString& filename) +{ + server().play(filename); +} + +/*! + Constructs a TQSound that can tquickly play the sound in a file + named \a filename. + + This may use more memory than the static \c play function. + + The \a parent and \a name arguments (default 0) are passed on to + the TQObject constructor. +*/ +TQSound::TQSound(const TQString& filename, TQObject* parent, const char* name) : + TQObject(parent,name), + d(new TQSoundData(filename)) +{ + server().init(this); +} + +/*! + Destroys the sound object. If the sound is not finished playing stop() is called on it. + + \sa stop() isFinished() +*/ +TQSound::~TQSound() +{ + if ( !isFinished() ) + stop(); + delete d; +} + +/*! + Returns TRUE if the sound has finished playing; otherwise returns FALSE. + + \warning On Windows this function always returns TRUE for unlooped sounds. +*/ +bool TQSound::isFinished() const +{ + return d->looprem == 0; +} + +/*! + \overload + + Starts the sound playing. The function returns immediately. + Depending on the platform audio facilities, other sounds may stop + or may be mixed with the new sound. + + The sound can be played again at any time, possibly mixing or + replacing previous plays of the sound. +*/ +void TQSound::play() +{ + d->looprem = d->looptotal; + server().play(this); +} + +/*! + Returns the number of times the sound will play. +*/ +int TQSound::loops() const +{ + return d->looptotal; +} + +/*! + Returns the number of times the sound will loop. This value + decreases each time the sound loops. +*/ +int TQSound::loopsRemaining() const +{ + return d->looprem; +} + +/*! + Sets the sound to repeat \a l times when it is played. Passing the + value -1 will cause the sound to loop indefinitely. + + \sa loops() +*/ +void TQSound::setLoops(int l) +{ + d->looptotal = l; +} + +/*! + Returns the filename associated with the sound. +*/ +TQString TQSound::fileName() const +{ + return d->filename; +} + +/*! + Stops the sound playing. + + On Windows the current loop will finish if a sound is played + in a loop. + + \sa play() +*/ +void TQSound::stop() +{ + server().stop(this); +} + + +/*! + Returns TRUE if sound facilities exist on the platform; otherwise + returns FALSE. An application may choose either to notify the user + if sound is crucial to the application or to operate silently + without bothering the user. + + If no sound is available, all TQSound operations work silently and + tquickly. +*/ +bool TQSound::isAvailable() +{ + return server().okay(); +} + +/*! + Sets the internal bucket record of sound \a s to \a b, deleting + any previous setting. +*/ +void TQAuServer::setBucket(TQSound* s, TQAuBucket* b) +{ + delete s->d->bucket; + s->d->bucket = b; +} + +/*! + Returns the internal bucket record of sound \a s. +*/ +TQAuBucket* TQAuServer::bucket(TQSound* s) +{ + return s->d->bucket; +} + +/*! + Decrements the TQSound::loopRemaining() value for sound \a s, + returning the result. +*/ +int TQAuServer::decLoop(TQSound* s) +{ + if ( s->d->looprem > 0 ) + --s->d->looprem; + return s->d->looprem; +} + +/*! + Initializes the sound. The default implementation does nothing. +*/ +void TQAuServer::init(TQSound*) +{ +} + +TQAuBucket::~TQAuBucket() +{ +} + +#endif // QT_NO_SOUND diff --git a/src/kernel/qsound.h b/src/kernel/qsound.h new file mode 100644 index 000000000..30805d324 --- /dev/null +++ b/src/kernel/qsound.h @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** Definition of TQSound class and TQAuServer internal class +** +** Created : 000117 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ +#ifndef TQSOUND_H +#define TQSOUND_H + +#ifndef QT_H +#include "qobject.h" +#endif // QT_H + +#ifndef QT_NO_SOUND + +class TQSoundData; + +class Q_EXPORT TQSound : public TQObject { + Q_OBJECT +public: + static bool isAvailable(); + static void play(const TQString& filename); + + TQSound(const TQString& filename, TQObject* parent=0, const char* name=0); + ~TQSound(); + + /* Coming soon... + ? + TQSound(int hertz, Type type=Mono); + int play(const ushort* data, int samples); + bool full(); + signal void notFull(); + ? + */ + +#ifndef QT_NO_COMPAT + static bool available() { return isAvailable(); } +#endif + + int loops() const; + int loopsRemaining() const; + void setLoops(int); + TQString fileName() const; + + bool isFinished() const; + +public slots: + void play(); + void stop(); + +private: + TQSoundData* d; + friend class TQAuServer; +}; + + +/* + TQAuServer is an INTERNAL class. If you wish to provide support for + additional audio servers, you can make a subclass of TQAuServer to do + so, HOWEVER, your class may need to be re-engineered to some degree + with each new TQt release, including minor releases. + + TQAuBucket is whatever you want. +*/ + +class TQAuBucket { +public: + virtual ~TQAuBucket(); +}; + +class TQAuServer : public TQObject { + Q_OBJECT + +public: + TQAuServer(TQObject* parent, const char* name); + ~TQAuServer(); + + virtual void init(TQSound*); + virtual void play(const TQString& filename); + virtual void play(TQSound*)=0; + virtual void stop(TQSound*)=0; + virtual bool okay()=0; + +protected: + void setBucket(TQSound*, TQAuBucket*); + TQAuBucket* bucket(TQSound*); + int decLoop(TQSound*); +}; + +#endif // QT_NO_SOUND + +#endif diff --git a/src/kernel/qsound_x11.cpp b/src/kernel/qsound_x11.cpp new file mode 100644 index 000000000..a4013d938 --- /dev/null +++ b/src/kernel/qsound_x11.cpp @@ -0,0 +1,283 @@ +/**************************************************************************** +** +** Implementation of TQSound class and TQAuServer internal class +** +** Created : 000117 +** +** Copyright (C) 1999-2008 Trolltech ASA. All rights reserved. +** +** This file is part of the kernel module of the TQt GUI Toolkit. +** +** This file may be used under the terms of the GNU General +** Public License versions 2.0 or 3.0 as published by the Free +** Software Foundation and appearing in the files LICENSE.GPL2 +** and LICENSE.GPL3 included in the packaging of this file. +** Alternatively you may (at your option) use any later version +** of the GNU General Public License if such license has been +** publicly approved by Trolltech ASA (or its successors, if any) +** and the KDE Free TQt Foundation. +** +** Please review the following information to ensure GNU General +** Public Licensing retquirements will be met: +** http://trolltech.com/products/qt/licenses/licensing/opensource/. +** If you are unsure which license is appropriate for your use, please +** review the following information: +** http://trolltech.com/products/qt/licenses/licensing/licensingoverview +** or contact the sales department at sales@trolltech.com. +** +** This file may be used under the terms of the Q Public License as +** defined by Trolltech ASA and appearing in the file LICENSE.TQPL +** included in the packaging of this file. Licensees holding valid TQt +** Commercial licenses 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 WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted +** herein. +** +**********************************************************************/ + +#define QT_CLEAN_NAMESPACE + +#include "qsound.h" + +#ifndef QT_NO_SOUND + +#include "qptrdict.h" +#include "qsocketnotifier.h" +#include "qapplication.h" + + +#ifdef QT_NAS_SUPPORT + +#include