1 /* QtRepaintThread.java -- Repaint thread implementation 2 Copyright (C) 2005 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 package gnu.java.awt.peer.qt; 39 40 /** 41 * This class does repainting of Component back-buffers. It is undesirable to 42 * do this directly from the paint callback in QtComponentPeer, because that 43 * is executed from the main thread. Thus, if a call is made at the same time 44 * which requires execution by the main thread, and this is sharing a lock with 45 * paint(), then a deadlock will occur, which must be avoided. In general, 46 * the main Qt thread should avoid calling into java code as far as possible. 47 * 48 */ 49 public class QtRepaintThread extends Thread 50 { 51 static class RepaintComponent 52 { 53 public QtComponentPeer curr; 54 public RepaintComponent next; 55 public boolean paintAll; 56 public int x, y, w, h; 57 RepaintComponent(QtComponentPeer p)58 public RepaintComponent(QtComponentPeer p) 59 { 60 curr = p; 61 next = null; 62 paintAll = true; 63 } 64 RepaintComponent(QtComponentPeer p, int x, int y, int w, int h)65 public RepaintComponent(QtComponentPeer p, int x, int y, int w, int h) 66 { 67 this(p); 68 paintAll = false; 69 this.x = x; 70 this.y = y; 71 this.w = w; 72 this.h = h; 73 } 74 } 75 76 RepaintComponent component; 77 boolean busy; 78 QtRepaintThread()79 public QtRepaintThread() 80 { 81 component = null; 82 } 83 run()84 public void run() 85 { 86 while( true ) 87 { 88 try 89 { 90 busy = false; 91 // Wait for a repaint 92 sleep(100); 93 busy = true; 94 } 95 catch (InterruptedException ie) 96 { 97 while( component != null ) 98 { 99 try 100 { 101 if( component.paintAll ) 102 { 103 // update the back-buffer. 104 component.curr.paintBackBuffer(); 105 component.curr.QtUpdate(); // trigger a native repaint event 106 } 107 else 108 { 109 component.curr.paintBackBuffer(component.x, component.y, 110 component.w, component.h); 111 component.curr.QtUpdateArea(component.x, component.y, 112 component.w, component.h); 113 } 114 } 115 catch (InterruptedException e) 116 { 117 } 118 component = component.next; 119 } 120 } 121 } 122 } 123 124 /** 125 * Enqueue a component for repainting. 126 */ queueComponent(QtComponentPeer p)127 public synchronized void queueComponent(QtComponentPeer p) 128 { 129 if( component == null ) 130 component = new RepaintComponent(p); 131 else 132 { 133 RepaintComponent r = component; 134 while( r.next != null ) r = r.next; 135 r.next = new RepaintComponent(p); 136 } 137 interrupt(); 138 } 139 140 /** 141 * Enqueue a component for repainting. 142 */ queueComponent(QtComponentPeer p, int x, int y, int w, int h)143 public synchronized void queueComponent(QtComponentPeer p, int x, int y, 144 int w, int h) 145 { 146 if( component == null ) 147 component = new RepaintComponent(p, x, y, w, h); 148 else 149 { 150 RepaintComponent r = component; 151 while( r.next != null ) r = r.next; 152 r.next = new RepaintComponent(p, x, y, w, h); 153 } 154 interrupt(); 155 } 156 } 157