1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74:
75: public class QtComponentPeer extends NativeWrapper implements ComponentPeer
76: {
77:
78:
81: protected static final int POPUP_TRIGGER = 3;
82:
83:
86: protected QtToolkit toolkit;
87:
88:
91: Component owner;
92:
93:
96: private long eventMask;
97:
98:
101: private boolean hasMotionListeners;
102:
103:
106: protected QtImage backBuffer;
107:
108: protected long qtApp;
109:
110: private boolean settingUp;
111:
112: private boolean ignoreResize = false;
113:
114: QtComponentPeer( QtToolkit kit, Component owner )
115: {
116: this.owner = owner;
117: this.toolkit = kit;
118: qtApp = QtToolkit.guiThread.QApplicationPointer;
119: nativeObject = 0;
120: synchronized(this)
121: {
122: callInit();
123: try
124: {
125: wait();
126: }
127: catch(InterruptedException e)
128: {
129: }
130: }
131: setup();
132: hasMotionListeners = false;
133: }
134:
135: protected native void callInit();
136:
137:
141: protected void init()
142: {
143: }
144:
145: protected void setup()
146: {
147: settingUp = true;
148: if (owner != null)
149: {
150: if (owner instanceof javax.swing.JComponent)
151: setBackground(owner.getBackground());
152: else
153: owner.setBackground(getNativeBackground());
154:
155: if (owner.getForeground() != null)
156: setForeground(owner.getForeground());
157: else
158: setForeground( Color.black );
159:
160: if (owner.getCursor() != null)
161: if (owner.getCursor().getType() != Cursor.DEFAULT_CURSOR)
162: setCursor(owner.getCursor());
163:
164: if (owner.getFont() != null)
165: setFont(owner.getFont());
166:
167: setEnabled( owner.isEnabled() );
168:
169: backBuffer = null;
170: updateBounds();
171:
172: setVisible( owner.isVisible() );
173: QtToolkit.repaintThread.queueComponent(this);
174: }
175: settingUp = false;
176: }
177:
178: native void QtUpdate();
179: native void QtUpdateArea( int x, int y, int w, int h );
180: private synchronized native void disposeNative();
181: private native void setGround( int r, int g, int b, boolean isForeground );
182: private native void setBoundsNative( int x, int y, int width, int height );
183: private native void setCursor( int ctype );
184: private native Color getNativeBackground();
185: private native void setFontNative( QtFontPeer fp );
186: private native int whichScreen();
187: private native void reparentNative( QtContainerPeer parent );
188: private native void getLocationOnScreenNative( Point p );
189:
190: private boolean drawableComponent()
191: {
192: return ((this instanceof QtContainerPeer &&
193: !(this instanceof QtScrollPanePeer)) ||
194: (this instanceof QtCanvasPeer));
195: }
196:
197: void updateBounds()
198: {
199: Rectangle r = owner.getBounds();
200: setBounds( r.x, r.y, r.width, r.height );
201: }
202:
203: synchronized void updateBackBuffer(int width, int height)
204: {
205: if(width <= 0 || height <= 0)
206: return;
207:
208: if( !drawableComponent() && backBuffer == null)
209: return;
210:
211: if( backBuffer != null )
212: {
213: if( width < backBuffer.width && height < backBuffer.height )
214: return;
215: backBuffer.dispose();
216: }
217: backBuffer = new QtImage(width, height);
218: }
219:
220:
221:
222:
223:
226: protected void closeEvent()
227: {
228: if (owner instanceof Window)
229: {
230: WindowEvent e = new WindowEvent((Window)owner,
231: WindowEvent.WINDOW_CLOSING);
232: QtToolkit.eventQueue.postEvent(e);
233: }
234: }
235:
236: protected void enterEvent(int modifiers, int x, int y, int dummy)
237: {
238: MouseEvent e = new MouseEvent(owner,
239: MouseEvent.MOUSE_ENTERED,
240: System.currentTimeMillis(),
241: (modifiers & 0x2FF), x, y, 0, false);
242: QtToolkit.eventQueue.postEvent(e);
243: }
244:
245: protected void focusInEvent()
246: {
247: FocusEvent e = new FocusEvent(owner, FocusEvent.FOCUS_GAINED);
248: QtToolkit.eventQueue.postEvent(e);
249: }
250:
251: protected void focusOutEvent()
252: {
253: FocusEvent e = new FocusEvent(owner, FocusEvent.FOCUS_LOST);
254: QtToolkit.eventQueue.postEvent(e);
255: }
256:
257: protected void keyPressEvent(int modifiers, int code, int unicode, int dummy)
258: {
259: KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
260: KeyEvent e = new KeyEvent(owner,
261: KeyEvent.KEY_PRESSED,
262: System.currentTimeMillis(),
263: modifiers, code, (char)(unicode & 0xFFFF),
264: KeyEvent.KEY_LOCATION_UNKNOWN);
265: if (!manager.dispatchEvent (e))
266: QtToolkit.eventQueue.postEvent(e);
267: }
268:
269: protected void keyReleaseEvent(int modifiers, int code, int unicode, int dummy)
270: {
271: KeyEvent e = new KeyEvent(owner,
272: KeyEvent.KEY_RELEASED,
273: System.currentTimeMillis(),
274: modifiers, code, (char)(unicode & 0xFFFF),
275: KeyEvent.KEY_LOCATION_UNKNOWN);
276: QtToolkit.eventQueue.postEvent(e);
277: }
278:
279: protected void leaveEvent(int modifiers, int x, int y, int dummy)
280: {
281: MouseEvent e = new MouseEvent(owner,
282: MouseEvent.MOUSE_EXITED,
283: System.currentTimeMillis(),
284: (modifiers & 0x2FF), x, y, 0, false);
285: QtToolkit.eventQueue.postEvent(e);
286: }
287:
288:
289: protected void mouseDoubleClickEvent( int modifiers, int x, int y, int clickCount)
290: {
291: if( (eventMask & AWTEvent.MOUSE_EVENT_MASK) == 0 )
292: return;
293: int button = 0;
294: if((modifiers & InputEvent.BUTTON1_DOWN_MASK) ==
295: InputEvent.BUTTON1_DOWN_MASK) button = 1;
296: if((modifiers & InputEvent.BUTTON2_DOWN_MASK) ==
297: InputEvent.BUTTON2_DOWN_MASK) button = 2;
298: if((modifiers & InputEvent.BUTTON3_DOWN_MASK) ==
299: InputEvent.BUTTON3_DOWN_MASK) button = 3;
300: MouseEvent e = new MouseEvent(owner,
301: MouseEvent.MOUSE_CLICKED,
302: System.currentTimeMillis(),
303: (modifiers & 0x2FF), x, y, clickCount,
304: false, button);
305: QtToolkit.eventQueue.postEvent(e);
306: }
307:
308: protected void mouseMoveEvent( int modifiers, int x, int y, int clickCount)
309: {
310: if( (eventMask & AWTEvent.MOUSE_EVENT_MASK) == 0 )
311: return;
312:
313: int button = 0;
314: if((modifiers & InputEvent.BUTTON1_DOWN_MASK) ==
315: InputEvent.BUTTON1_DOWN_MASK) button = 1;
316: if((modifiers & InputEvent.BUTTON2_DOWN_MASK) ==
317: InputEvent.BUTTON2_DOWN_MASK) button = 2;
318: if((modifiers & InputEvent.BUTTON3_DOWN_MASK) ==
319: InputEvent.BUTTON3_DOWN_MASK) button = 3;
320:
321: int type = (button != 0) ?
322: MouseEvent.MOUSE_DRAGGED :MouseEvent.MOUSE_MOVED;
323:
324: MouseEvent e = new MouseEvent(owner,
325: type,
326: System.currentTimeMillis(),
327: (modifiers & 0x2FF), x, y, clickCount,
328: false, button);
329: QtToolkit.eventQueue.postEvent(e);
330: }
331:
332: protected void mousePressEvent( int modifiers, int x, int y, int clickCount)
333: {
334: if( (eventMask & AWTEvent.MOUSE_EVENT_MASK) == 0 )
335: return;
336: int button = 0;
337: if((modifiers & InputEvent.BUTTON1_DOWN_MASK) ==
338: InputEvent.BUTTON1_DOWN_MASK) button = 1;
339: if((modifiers & InputEvent.BUTTON2_DOWN_MASK) ==
340: InputEvent.BUTTON2_DOWN_MASK) button = 2;
341: if((modifiers & InputEvent.BUTTON3_DOWN_MASK) ==
342: InputEvent.BUTTON3_DOWN_MASK) button = 3;
343: MouseEvent e = new MouseEvent(owner,
344: MouseEvent.MOUSE_PRESSED,
345: System.currentTimeMillis(),
346: (modifiers & 0x2FF), x, y, clickCount,
347: ( button == POPUP_TRIGGER ),
348: button);
349: QtToolkit.eventQueue.postEvent(e);
350: }
351:
352: protected void mouseReleaseEvent( int modifiers, int x, int y, int clickCount)
353: {
354: if( (eventMask & AWTEvent.MOUSE_EVENT_MASK) == 0 )
355: return;
356: int button = 0;
357: if((modifiers & InputEvent.BUTTON1_DOWN_MASK) ==
358: InputEvent.BUTTON1_DOWN_MASK) button = 1;
359: if((modifiers & InputEvent.BUTTON2_DOWN_MASK) ==
360: InputEvent.BUTTON2_DOWN_MASK) button = 2;
361: if((modifiers & InputEvent.BUTTON3_DOWN_MASK) ==
362: InputEvent.BUTTON3_DOWN_MASK) button = 3;
363:
364: MouseEvent e = new MouseEvent(owner,
365: MouseEvent.MOUSE_RELEASED,
366: System.currentTimeMillis(),
367: (modifiers & 0x2FF), x, y, clickCount,
368: false, button);
369: QtToolkit.eventQueue.postEvent(e);
370: }
371:
372: protected void moveEvent(int x, int y, int oldx, int oldy)
373: {
374: if( !ignoreResize )
375: {
376:
377:
378: ignoreResize = true;
379: owner.setLocation( x, y );
380: ignoreResize = false;
381: }
382: }
383:
384: protected void resizeEvent(int oldWidth, int oldHeight,
385: int width, int height)
386: {
387: if(!(owner instanceof Window))
388: return;
389: updateBackBuffer(width, height);
390: ignoreResize = true;
391: owner.setSize(width, height);
392: ignoreResize = false;
393: ComponentEvent e = new ComponentEvent(owner,
394: ComponentEvent.COMPONENT_RESIZED);
395: QtToolkit.eventQueue.postEvent(e);
396: QtToolkit.repaintThread.queueComponent(this);
397: }
398:
399: protected void showEvent()
400: {
401: if (owner instanceof Window)
402: {
403: WindowEvent e = new WindowEvent((Window)owner,
404: WindowEvent.WINDOW_OPENED);
405: QtToolkit.eventQueue.postEvent(e);
406: }
407: else
408: {
409: ComponentEvent e = new ComponentEvent(owner,
410: ComponentEvent.COMPONENT_SHOWN);
411: QtToolkit.eventQueue.postEvent(e);
412: }
413: }
414:
415: protected void hideEvent()
416: {
417: ComponentEvent e = new ComponentEvent(owner,
418: ComponentEvent.COMPONENT_HIDDEN);
419: QtToolkit.eventQueue.postEvent(e);
420: }
421:
422:
423:
424:
425: public void setEventMask(long x)
426: {
427: eventMask = x;
428: }
429:
430:
431: public boolean canDetermineObscurity()
432: {
433: return true;
434: }
435:
436: public int checkImage(Image img,
437: int w,
438: int h,
439: ImageObserver o)
440: {
441: return toolkit.checkImage(img, w, h, o);
442: }
443:
444: public void createBuffers(int numBuffers, BufferCapabilities caps)
445: throws AWTException
446: {
447:
448: }
449:
450: public Image createImage(ImageProducer producer)
451: {
452: return toolkit.createImage(producer);
453: }
454:
455: public Image createImage(int width, int height)
456: {
457: return new QtImage(width, height);
458: }
459:
460: public void coalescePaintEvent(PaintEvent e)
461: {
462:
463: }
464:
465: public VolatileImage createVolatileImage(int w, int h)
466: {
467: return new QtVolatileImage( w, h );
468: }
469:
470: public void destroyBuffers()
471: {
472:
473: }
474:
475: public void disable()
476: {
477: setEnabled(false);
478: }
479:
480: public void dispose()
481: {
482: disposeNative();
483: if( backBuffer != null )
484: backBuffer.dispose();
485: }
486:
487: public void enable()
488: {
489: setEnabled(true);
490: }
491:
492: public void finalize()
493: {
494: dispose();
495: }
496:
497: public void flip(BufferCapabilities.FlipContents contents)
498: {
499: }
500:
501: public Image getBackBuffer()
502: {
503: return backBuffer;
504: }
505:
506: public ColorModel getColorModel()
507: {
508: return toolkit.getColorModel();
509: }
510:
511: public FontMetrics getFontMetrics(Font font)
512: {
513: return new QtFontMetrics( font, getGraphics() );
514: }
515:
516: public Graphics getGraphics()
517: {
518: if( backBuffer == null )
519: {
520: Rectangle r = owner.getBounds();
521: backBuffer = new QtImage( r.width, r.height );
522: }
523: return backBuffer.getDirectGraphics( this );
524: }
525:
526: public GraphicsConfiguration getGraphicsConfiguration()
527: {
528: int id = whichScreen();
529: GraphicsDevice[] devs = QtToolkit.graphicsEnv.getScreenDevices();
530: return devs[id].getDefaultConfiguration();
531: }
532:
533: public Point getLocationOnScreen()
534: {
535: Point p = new Point();
536: synchronized( p )
537: {
538: getLocationOnScreenNative( p );
539: try
540: {
541: p.wait();
542: }
543: catch(InterruptedException e)
544: {
545: }
546: }
547: return p;
548: }
549:
550: private native void getSizeNative(Dimension d, boolean preferred);
551:
552: private Dimension getSize(boolean preferred)
553: {
554: Dimension d = new Dimension();
555: synchronized( d )
556: {
557: getSizeNative(d, preferred);
558: try
559: {
560: d.wait();
561: }
562: catch(InterruptedException e)
563: {
564: }
565: }
566: return d;
567: }
568:
569: public Dimension getMinimumSize()
570: {
571: return getSize( false );
572: }
573:
574: public Dimension getPreferredSize()
575: {
576: return getSize( true );
577: }
578:
579: public Toolkit getToolkit()
580: {
581: return toolkit;
582: }
583:
584: public native boolean handlesWheelScrolling();
585:
586: public void hide()
587: {
588: setVisible(false);
589: }
590:
591: public native boolean isFocusable();
592:
593: public boolean isFocusTraversable()
594: {
595:
596: return false;
597: }
598:
599: public native boolean isObscured();
600:
601: public Dimension minimumSize()
602: {
603: return getMinimumSize();
604: }
605:
606: public Dimension preferredSize()
607: {
608: return getPreferredSize();
609: }
610:
611: public native void requestFocus();
612:
613: public boolean requestFocus (Component source, boolean bool1,
614: boolean bool2, long x)
615: {
616:
617: return true;
618: }
619:
620: public void reshape(int x,
621: int y,
622: int width,
623: int height)
624: {
625: setBounds( x, y, width, height );
626: }
627:
628: public void setBackground(Color c)
629: {
630: if(c == null && !settingUp)
631: return;
632: setGround(c.getRed(), c.getGreen(), c.getBlue(), false);
633: }
634:
635: public void setBounds(int x, int y, int width, int height)
636: {
637: if( ignoreResize )
638: return;
639: updateBackBuffer(width, height);
640: QtToolkit.repaintThread.queueComponent(this);
641: setBoundsNative(x, y, width, height);
642: }
643:
644: public void setCursor(Cursor cursor)
645: {
646: if (cursor != null)
647: setCursor(cursor.getType());
648: }
649:
650: public native void setEnabled(boolean b);
651:
652: public void setFont(Font f)
653: {
654: if( f == null || f.getPeer() == null)
655: throw new IllegalArgumentException("Null font.");
656: setFontNative( (QtFontPeer)f.getPeer() );
657: }
658:
659: public void setForeground(Color c)
660: {
661: if(c == null && !settingUp)
662: return;
663: setGround(c.getRed(), c.getGreen(), c.getBlue(), true);
664: }
665:
666: public native void setVisible(boolean b);
667:
668: public void show()
669: {
670: setVisible(true);
671: }
672:
673: public void handleEvent (AWTEvent e)
674: {
675: int eventID = e.getID();
676: Rectangle r;
677:
678: switch (eventID)
679: {
680: case ComponentEvent.COMPONENT_SHOWN:
681: QtToolkit.repaintThread.queueComponent(this);
682: break;
683: case PaintEvent.PAINT:
684: case PaintEvent.UPDATE:
685: r = ((PaintEvent)e).getUpdateRect();
686: QtToolkit.repaintThread.queueComponent(this, r.x, r.y,
687: r.width, r.height);
688: break;
689: case KeyEvent.KEY_PRESSED:
690: break;
691: case KeyEvent.KEY_RELEASED:
692: break;
693: }
694: }
695:
696:
700: public void paint(Graphics g)
701: {
702: Rectangle r = g.getClipBounds();
703:
704: if (backBuffer != null)
705: backBuffer.drawPixelsScaledFlipped ((QtGraphics) g,
706: 0, 0, 0,
707: false, false,
708: r.x, r.y, r.width, r.height,
709: r.x, r.y, r.width, r.height,
710: false );
711: }
712:
713: public void paintBackBuffer() throws InterruptedException
714: {
715: if( backBuffer != null )
716: {
717: backBuffer.clear();
718: Graphics2D bbg = (Graphics2D)backBuffer.getGraphics();
719: owner.paint(bbg);
720: bbg.dispose();
721: }
722: }
723:
724: public void paintBackBuffer(int x, int y, int w, int h)
725: throws InterruptedException
726: {
727: if( backBuffer != null )
728: {
729: Graphics2D bbg = (Graphics2D)backBuffer.getGraphics();
730: bbg.setBackground( getNativeBackground() );
731: bbg.clearRect(x, y, w, h);
732: bbg.setClip(x, y, w, h);
733: owner.paint(bbg);
734: bbg.dispose();
735: }
736: }
737:
738: public boolean prepareImage(Image img,
739: int w,
740: int h,
741: ImageObserver o)
742: {
743: return toolkit.prepareImage(img, w, h, o);
744: }
745:
746: public void print(Graphics g)
747: {
748:
749: }
750:
751:
754: public void repaint(long tm,
755: int x,
756: int y,
757: int w,
758: int h)
759: {
760: if( tm <= 0 )
761: {
762: QtToolkit.repaintThread.queueComponent(this, x, y, w, h);
763: return;
764: }
765: Timer t = new Timer();
766: t.schedule(new RepaintTimerTask(this, x, y, w, h), tm);
767: }
768:
769:
772: public void updateCursorImmediately()
773: {
774: if (owner.getCursor() != null)
775: setCursor(owner.getCursor().getType());
776: }
777:
778:
781: private class RepaintTimerTask extends TimerTask
782: {
783: private int x, y, w, h;
784: private QtComponentPeer peer;
785: RepaintTimerTask(QtComponentPeer peer, int x, int y, int w, int h)
786: {
787: this.x=x;
788: this.y=y;
789: this.w=w;
790: this.h=h;
791: this.peer=peer;
792: }
793: public void run()
794: {
795: QtToolkit.repaintThread.queueComponent(peer, x, y, w, h);
796: }
797: }
798:
799: public native Rectangle getBounds();
800:
801: public void reparent(ContainerPeer parent)
802: {
803: if(!(parent instanceof QtContainerPeer))
804: throw new IllegalArgumentException("Illegal peer.");
805: reparentNative((QtContainerPeer)parent);
806: }
807:
808: public void setBounds(int x, int y, int width, int height, int z)
809: {
810:
811:
812: }
813:
814: public boolean isReparentSupported()
815: {
816: return true;
817: }
818:
819:
820: public void layout()
821: {
822:
823: }
824:
825: public boolean requestFocus(Component lightweightChild, boolean temporary,
826: boolean focusedWindowChangeAllowed,
827: long time, sun.awt.CausedFocusEvent.Cause cause)
828: {
829:
830:
831: return true;
832: }
833:
834: }