📄 qtbackend.cpp
字号:
/* * @(#)QtBackEnd.cpp 1.15 06/10/25 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */#include <stdlib.h>#include <assert.h>#include <unistd.h>#include <time.h>#include "java_awt_QtGraphicsEnvironment.h"#include "java_awt_QtDefaultGraphicsConfiguration.h"#include "java_awt_Cursor.h"#include "java_awt_AlphaComposite.h"#include "qt.h"#define __QT_BACKEND_CPP__#include "QtApplication.h"#include "QtBackEnd.h"#include "QtScreenFactory.h"/* temporarily for now...FIXME */extern void dispatchMouseButtonEvent(MouseButtonEvent *qte);extern void dispatchMouseMoveEvent(MouseMoveEvent *qte);extern void dispatchKeyEvent(KeyboardEvent *qte);static long getKeyCode(int qtKey);/* the contents of the env variables is "<x>,<y>-<width>x>height>" */#define PBP_SCREEN_BOUNDS_ENV "PBP_SCREEN_BOUNDS"#ifdef QWS#define PBP_FRAME_WFLAGS (Qt::WStyle_Customize|Qt::WStyle_NoBorder)#else /* QWS */#define PBP_FRAME_WFLAGS (Qt::WRepaintNoErase|Qt::WStyle_Customize|Qt::WStyle_NoBorder)#endif /* QWS */#ifdef QT_AWT_STATIC_POOLQtImageDesc QtImageDescPool[NUM_IMAGE_DESCRIPTORS];QtGraphDesc QtGraphDescPool[NUM_GRAPH_DESCRIPTORS];#elseQtImageDesc *QtImageDescPool = NULL;QtGraphDesc *QtGraphDescPool = NULL;#endifint NumGraphDescriptors = NUM_GRAPH_DESCRIPTORS;int NumImageDescriptors = NUM_IMAGE_DESCRIPTORS;QtScreen::QtScreen() { this->m_bounds_restricted = FALSE;#ifndef QT_AWT_STATIC_POOL if ( QtImageDescPool == NULL ) { QtImageDescPool = (QtImageDesc *) calloc(sizeof(QtImageDesc), NumImageDescriptors); assert(QtImageDescPool != NULL); } if ( QtGraphDescPool == NULL ) { QtGraphDescPool = (QtGraphDesc *) calloc(sizeof(QtGraphDesc), NumGraphDescriptors); assert(QtGraphDescPool != NULL); }#endif }boolQtScreen::init(){ if ( qtApp != NULL ) return TRUE; // already initialized /* * We need to create an array of size 3 because the QApplication assigns a * NULL pointer to the argv[argc] position after it processes and removes * all the Qt command line options from the argv vector. This is a Qt bug * to write past the end of the argv vector. Qt also keeps a refernece * to argv, * so it shouldn't go away. */ int argc = 1; char ** argv = this->getArgs(&argc); /* * Create a QApplication */ new QtApplication(argc, argv); assert(qtApp != NULL); QRect screen_bounds = QApplication::desktop()->frameGeometry();#ifdef QWS // for zaurus, get the bounds of the screen dynamically instead of // relying on awt.h values this->m_x = screen_bounds.x() ; this->m_y = screen_bounds.y() ; this->m_width = screen_bounds.width() ; this->m_height = screen_bounds.height() ;#else this->m_x = 0 ; this->m_y = 0 ; this->m_width = SCREEN_X_RESOLUTION ; this->m_height = SCREEN_Y_RESOLUTION ;#endif /* Set the basis frame size based on the environmental variable PBP_SCREEN_BOUNDS. Syntax should follow 0,0-640x480 for (0,0,640,480), for example */ char *userBounds = NULL; if((userBounds=getenv(PBP_SCREEN_BOUNDS_ENV))!=NULL) { int tmpx, tmpy; int tmpw, tmph; if(sscanf(userBounds, "%d,%d-%dx%d", &tmpx,&tmpy,&tmpw,&tmph) == 4) { QRect ubounds(tmpx, tmpy, tmpw, tmph) ; ubounds = screen_bounds.intersect(ubounds) ; if ( ubounds.isValid() && !ubounds.isNull() ) { this->m_x = ubounds.x() ; this->m_y = ubounds.y() ; this->m_width = ubounds.width() ; this->m_height = ubounds.height() ; this->m_bounds_restricted = TRUE; } } } /* * Now that we have computed the screen bounds, give the subclass a * chance to redefine it */ this->computeBounds() ; /* * Create the Qt widget that backs the Window */ this->createQtWindow(); assert(this->m_window != NULL); AWT_QT_LOCK; qtApp->setMainWidget(this->m_window);#ifdef QTOPIA qtApp->setStylusOperation(this->m_window, QPEApplication::RightOnHold);#endif /* QTOPIA */ AWT_QT_UNLOCK; QtGraphDescPool[0].used = 1; QtGraphDescPool[0].qid = 0; AWT_QT_LOCK; QtGraphDescPool[0].qp = new QPen(); QtGraphDescPool[0].qb = new QBrush(); AWT_QT_UNLOCK; QtGraphDescPool[0].blendmode = java_awt_AlphaComposite_SRC_OVER; return TRUE ;}void QtScreen::showWindow() { #ifdef QWS bool isQWS = TRUE ; #else bool isQWS = FALSE ; #endif //QWS /* * if we are running under QWS and the bounds are not restricted the * show the window as Maximized (which takes care of not going past * the taskbar on the QPE manager if any) * else we simply set the bounds and show the window */ AWT_QT_LOCK; if ( isQWS && (!this->m_bounds_restricted) ) { this->m_window->showMaximized(); } else { this->m_window->move(this->m_x, this->m_y); this->m_window->setFixedSize(this->m_width, this->m_height); this->m_window->show(); } AWT_QT_UNLOCK; this->m_x = this->m_window->x(); this->m_y = this->m_window->y(); this->m_width = this->m_window->width(); this->m_height = this->m_window->height(); /* * indicate to the subclass that the window is shown */ this->windowShown(); QtImageDescPool[0].width = this->m_width; QtImageDescPool[0].height = this->m_height; QtImageDescPool[0].count = 1; QtImageDescPool[0].mask = this->m_window->mask(); QtImageDescPool[0].qpd = this->m_window;}char **QtScreen::getArgs(int *argc) { int count; // Use the "-sync" flag to force Xlib calls to be synchronous, which // would aid in debugging Xlib async errors. static char *argv[] = {"cvm", "-qws", NULL}; count = 1;#ifdef QWS#ifndef QTOPIA // If environment varible QWS_CLIENT is not defined, // assume we are running as the server (in the qt-embedded sense). if (getenv("QWS_CLIENT") == NULL) { count++; }#endif /* QTOPIA */#endif /* QWS */ *argc = count; return (char **)&argv;}voidQtScreen::computeBounds() { // nothing special to do here}voidQtScreen::windowShown() { // nothing special to do here}voidQtScreen::createQtWindow() { AWT_QT_LOCK; this->m_window = new QtWindow(PBP_FRAME_WFLAGS); QBitmap bitmap(this->m_width, this->m_height); AWT_QT_UNLOCK; this->m_window->setMask(bitmap);}void QtScreen::setMouseCursor(int cursor){ int QtCursorType; switch((jint)cursor) {#if (QT_VERSION >= 0x030000) case java_awt_Cursor_CROSSHAIR_CURSOR: QtCursorType = Qt::CrossCursor; break; case java_awt_Cursor_TEXT_CURSOR: QtCursorType = Qt::IbeamCursor; break; case java_awt_Cursor_WAIT_CURSOR: QtCursorType = Qt::WaitCursor; break; case java_awt_Cursor_SW_RESIZE_CURSOR: QtCursorType = Qt::SizeBDiagCursor; break; case java_awt_Cursor_SE_RESIZE_CURSOR: QtCursorType = Qt::SizeFDiagCursor; break; case java_awt_Cursor_NW_RESIZE_CURSOR: QtCursorType = Qt::SizeFDiagCursor; break; case java_awt_Cursor_NE_RESIZE_CURSOR: QtCursorType = Qt::SizeBDiagCursor; break; case java_awt_Cursor_N_RESIZE_CURSOR: QtCursorType = Qt::SizeVerCursor; break; case java_awt_Cursor_S_RESIZE_CURSOR: QtCursorType = Qt::SizeVerCursor; break; case java_awt_Cursor_W_RESIZE_CURSOR: QtCursorType = Qt::SizeHorCursor; break; case java_awt_Cursor_E_RESIZE_CURSOR: QtCursorType = Qt::SizeHorCursor; break; case java_awt_Cursor_HAND_CURSOR: QtCursorType = Qt::PointingHandCursor; break; case java_awt_Cursor_MOVE_CURSOR: QtCursorType = Qt::CrossCursor; break; default: QtCursorType = Qt::ArrowCursor; break;#else case java_awt_Cursor_CROSSHAIR_CURSOR: QtCursorType = CrossCursor; break; case java_awt_Cursor_TEXT_CURSOR: QtCursorType = IbeamCursor; break; case java_awt_Cursor_WAIT_CURSOR: QtCursorType = WaitCursor; break; case java_awt_Cursor_SW_RESIZE_CURSOR: QtCursorType = SizeBDiagCursor; break; case java_awt_Cursor_SE_RESIZE_CURSOR: QtCursorType = SizeFDiagCursor; break; case java_awt_Cursor_NW_RESIZE_CURSOR: QtCursorType = SizeFDiagCursor; break; case java_awt_Cursor_NE_RESIZE_CURSOR: QtCursorType = SizeBDiagCursor; break; case java_awt_Cursor_N_RESIZE_CURSOR: QtCursorType = SizeVerCursor; break; case java_awt_Cursor_S_RESIZE_CURSOR: QtCursorType = SizeVerCursor; break; case java_awt_Cursor_W_RESIZE_CURSOR: QtCursorType = SizeHorCursor; break; case java_awt_Cursor_E_RESIZE_CURSOR: QtCursorType = SizeHorCursor; break; case java_awt_Cursor_HAND_CURSOR: QtCursorType = PointingHandCursor; break; case java_awt_Cursor_MOVE_CURSOR: QtCursorType = CrossCursor; break; default: QtCursorType = ArrowCursor; break;#endif /* QT_VERSION */ } AWT_QT_LOCK; QCursor qc = this->m_window->cursor(); qc.setShape(QtCursorType); this->m_window->setCursor(qc); AWT_QT_UNLOCK;}void QtScreen::beep(){}int QtScreen::x(){ return this->m_x;}int QtScreen::y(){ return this->m_y;}int QtScreen::width(){ return this->m_width;}int QtScreen::height(){ return this->m_height;}void QtScreen::close(){}QtWindow *QtScreen::window() { return this->m_window;}int QtScreen::dotsPerInch() { int width ,widthMM, calcDpi; AWT_QT_LOCK; { QWidget *d = QApplication::desktop(); width = d->width(); QPaintDeviceMetrics pdm( d ); widthMM = pdm.widthMM(); } AWT_QT_UNLOCK; /* 1 inch = 2.54 cm */ calcDpi = (int) (width * 25.4) / widthMM; return calcDpi;}QtWindow::QtWindow(int flags, const char *name, QWidget *parent) : QWidget( parent, name, (WFlags)flags){ // install event filter...? // If mouse tracking is true, then mouseMoveEvent is called // whenever the mouse moved, even if button isn't held down. AWT_QT_LOCK; setMouseTracking(true); AWT_QT_UNLOCK; transMask = NULL;}void QtWindow::mousePressEvent(QMouseEvent *event){ MouseButtonEvent ev; ev.type = EVENT_MOUSE_BUTTON_PRESSED; switch (event->button()) { case Qt::LeftButton: ev.button = MOUSE_BUTTON_LEFT; break; case Qt::MidButton: ev.button = MOUSE_BUTTON_MIDDLE; break; case Qt::RightButton: ev.button = MOUSE_BUTTON_RIGHT; break; default: ev.button = MOUSE_BUTTON_LEFT; } dispatchMouseButtonEvent(&ev); }void QtWindow::mouseReleaseEvent(QMouseEvent *event){ MouseButtonEvent ev; ev.type = EVENT_MOUSE_BUTTON_RELEASED; switch (event->button()) { case Qt::LeftButton: ev.button = MOUSE_BUTTON_LEFT; break; case Qt::MidButton: ev.button = MOUSE_BUTTON_MIDDLE; break; case Qt::RightButton: ev.button = MOUSE_BUTTON_RIGHT; break; default: ev.button = MOUSE_BUTTON_LEFT; } dispatchMouseButtonEvent(&ev);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -