XCanvasPeer.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /* Copyright (C) 2000, 2002, 2003 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. package gnu.awt.xlib;
  7. import java.awt.Dimension;
  8. import java.awt.BufferCapabilities;
  9. import java.awt.Component;
  10. import java.awt.EventQueue;
  11. import java.awt.Rectangle;
  12. import java.awt.Color;
  13. import java.awt.Container;
  14. import java.awt.Image;
  15. import java.awt.GraphicsConfiguration;
  16. import java.awt.Font;
  17. import java.awt.FontMetrics;
  18. import java.awt.Graphics;
  19. import java.awt.Point;
  20. import java.awt.Toolkit;
  21. import java.awt.AWTEvent;
  22. import java.awt.Cursor;
  23. import java.awt.Shape;
  24. import java.awt.peer.*;
  25. import java.awt.image.*;
  26. import java.awt.event.MouseListener;
  27. import java.awt.event.PaintEvent;
  28. import java.util.EventListener;
  29. import gnu.gcj.xlib.WMSizeHints;
  30. import gnu.gcj.xlib.Window;
  31. import gnu.gcj.xlib.WindowAttributes;
  32. import gnu.gcj.xlib.Display;
  33. import gnu.gcj.xlib.Visual;
  34. import gnu.gcj.xlib.Screen;
  35. import gnu.gcj.xlib.XImage;
  36. import gnu.awt.j2d.*;
  37. import sun.awt.CausedFocusEvent;
  38. public class XCanvasPeer implements CanvasPeer
  39. {
  40. static final Dimension MIN_SIZE = new Dimension(1, 1);
  41. public // temporary
  42. Window window;
  43. Window parent;
  44. Component component;
  45. XGraphicsConfiguration config;
  46. private WindowAttributes attributes = new WindowAttributes();
  47. private long eventMask;
  48. public XCanvasPeer(Component component)
  49. {
  50. this.component = component;
  51. // Set up graphics configuration (ie. screen + visual):
  52. config = (XGraphicsConfiguration)
  53. component.getGraphicsConfiguration();
  54. if (config == null)
  55. {
  56. // This will usually only happen for toplevel windows
  57. config = getXToolkit().getDefaultXGraphicsConfiguration();
  58. }
  59. Rectangle bounds = component.getBounds();
  60. parent = locateParentWindow(bounds);
  61. // Windows in X must atleast be of size 1x1
  62. boolean boundsChanged = false;
  63. if (bounds.width < 1)
  64. {
  65. boundsChanged = true;
  66. bounds.width = 1;
  67. }
  68. if (bounds.height < 1)
  69. {
  70. boundsChanged = true;
  71. bounds.height = 1;
  72. }
  73. /* don't worry about this calling back to us, since the real
  74. component object has not yet received a reference to this peer
  75. object. */
  76. component.setBounds(bounds);
  77. /* Set background color */
  78. Color bg = component.getBackground();
  79. if (bg != null)
  80. {
  81. int[] components =
  82. {
  83. bg.getRed(),
  84. bg.getGreen(),
  85. bg.getBlue(),
  86. 0xff
  87. };
  88. ColorModel cm = config.getColorModel();
  89. long pixel = cm.getDataElement(components, 0);
  90. attributes.setBackground(pixel);
  91. }
  92. /* Set exposure mask so that we get exposure events
  93. that can be translated into paint() calls. */
  94. long eventMask = WindowAttributes.MASK_EXPOSURE;
  95. /* It would be nice to set up all other required events here, but
  96. it is not possible to do so before after all the children of
  97. this component has been realized. The reason is that it is not
  98. determined whether a component is lightweight before after the
  99. addNotify() method has been called. Thus, it is not possible
  100. for parent component to determine what events it needs to
  101. furnish for lightweight children. Instead, we currently rely
  102. on the component calling our setEventMask() method after the
  103. correct event mask has been determined. */
  104. attributes.setEventMask(eventMask);
  105. // TODO: set more window attributes?
  106. /* don't allow event queue to process events from the newly
  107. created window before this peer has been registered as client
  108. data. */
  109. synchronized (getXToolkit().eventLoop)
  110. {
  111. window = new gnu.gcj.xlib.Window(parent, bounds, attributes);
  112. window.setClientData(this); /* make it possible to find back
  113. to this peer object. Used by
  114. XEventQueue. */
  115. }
  116. initWindowProperties();
  117. if (component.isVisible())
  118. EventQueue.invokeLater(new DoMap(window));
  119. }
  120. /**
  121. * Override this in subclasses to implement other ways of obtaining
  122. * parent windows. Toplevel windows will typically have a different
  123. * implementation.
  124. */
  125. gnu.gcj.xlib.Window locateParentWindow(Rectangle bounds)
  126. {
  127. Container parent = component.getParent();
  128. while (parent.isLightweight())
  129. {
  130. bounds.x += parent.getX();
  131. bounds.y += parent.getY();
  132. parent = parent.getParent();
  133. // a null pointer here is a genuine error
  134. }
  135. XCanvasPeer parentPeer = (XCanvasPeer) parent.getPeer();
  136. if (parentPeer == null)
  137. throw new NullPointerException("Parent has no peer. This should " +
  138. "not be possible, since the " +
  139. "calls leading here should come " +
  140. "from parent, after it has " +
  141. "set the parent peer.");
  142. return parentPeer.window;
  143. }
  144. /**
  145. * Template method to allow subclasses to apply properties to X11
  146. * window right after creation.
  147. */
  148. void initWindowProperties()
  149. {
  150. }
  151. XToolkit getXToolkit()
  152. {
  153. return XToolkit.INSTANCE;
  154. }
  155. protected void ensureFlush()
  156. {
  157. getXToolkit().flushIfIdle();
  158. }
  159. public Component getComponent()
  160. {
  161. return component;
  162. }
  163. long getBasicEventMask()
  164. {
  165. return WindowAttributes.MASK_EXPOSURE;
  166. }
  167. // -------- java.awt.peer.ComponentPeer implementation
  168. public int checkImage(Image img, int width, int height, ImageObserver o)
  169. {
  170. throw new UnsupportedOperationException("FIXME, not implemented");
  171. }
  172. public Image createImage(ImageProducer prod)
  173. {
  174. return new XOffScreenImage (config, window, prod, config.getColorModel());
  175. }
  176. public Image createImage(int width, int height)
  177. {
  178. return new XOffScreenImage (config, window, width, height, config.getColorModel());
  179. }
  180. public void dispose()
  181. {
  182. throw new UnsupportedOperationException("FIXME, not implemented");
  183. }
  184. public GraphicsConfiguration getGraphicsConfiguration()
  185. {
  186. return config;
  187. }
  188. public FontMetrics getFontMetrics(Font f)
  189. {
  190. throw new UnsupportedOperationException("FIXME, not implemented");
  191. }
  192. public ColorModel getColorModel ()
  193. {
  194. return null;
  195. }
  196. public Graphics getGraphics()
  197. {
  198. DirectRasterGraphics gfxDevice = new XGraphics(window, config);
  199. IntegerGraphicsState igState = new IntegerGraphicsState(gfxDevice);
  200. Graphics2DImpl gfx2d = new Graphics2DImpl(config);
  201. gfx2d.setState(igState);
  202. gfx2d.setColor(component.getBackground());
  203. return gfx2d;
  204. }
  205. private Rectangle locationBounds;
  206. public Point getLocationOnScreen()
  207. {
  208. locationBounds = window.getBounds (locationBounds);
  209. return new Point (locationBounds.x,locationBounds.y);
  210. }
  211. public Dimension getMinimumSize ()
  212. {
  213. return MIN_SIZE;
  214. }
  215. public Dimension minimumSize ()
  216. {
  217. return getMinimumSize ();
  218. }
  219. public Dimension getPreferredSize ()
  220. {
  221. return component.getSize();
  222. }
  223. public Dimension preferredSize ()
  224. {
  225. return getPreferredSize();
  226. }
  227. public Toolkit getToolkit()
  228. {
  229. return getXToolkit();
  230. }
  231. public void handleEvent(AWTEvent event)
  232. {
  233. int id = event.getID ();
  234. switch (id)
  235. {
  236. case PaintEvent.PAINT:
  237. case PaintEvent.UPDATE:
  238. {
  239. try
  240. {
  241. Graphics g = getGraphics ();
  242. g.setClip (((PaintEvent)event).getUpdateRect ());
  243. if (id == PaintEvent.PAINT)
  244. component.paint (g);
  245. else
  246. component.update (g);
  247. g.dispose ();
  248. }
  249. catch (InternalError e)
  250. {
  251. System.err.println (e);
  252. }
  253. }
  254. break;
  255. }
  256. }
  257. public boolean isFocusTraversable()
  258. {
  259. throw new UnsupportedOperationException("FIXME, not implemented");
  260. }
  261. public void paint(Graphics gfx)
  262. {
  263. // do nothing by default
  264. }
  265. public boolean prepareImage(Image img, int width, int height,
  266. ImageObserver o)
  267. {
  268. throw new UnsupportedOperationException("FIXME, not implemented");
  269. }
  270. public void print(Graphics graphics)
  271. {
  272. paint(graphics);
  273. }
  274. public void repaint(long tm, int x, int y, int w, int h)
  275. {
  276. /* TODO?
  277. X allows intelligent X servers to do smart
  278. refreshing. Perhaps involve X in repainting of components,
  279. rather that keeping it all within the local event queue. */
  280. PaintEvent updateEvent = new PaintEvent(component,
  281. PaintEvent.UPDATE,
  282. new Rectangle(x, y, w, h));
  283. getXToolkit().queue.postEvent(updateEvent);
  284. }
  285. public void requestFocus()
  286. {
  287. throw new UnsupportedOperationException("FIXME, not implemented");
  288. }
  289. public void setBackground(Color color)
  290. {
  291. if (color != null)
  292. {
  293. int[] components =
  294. {
  295. color.getRed (),
  296. color.getGreen (),
  297. color.getBlue (),
  298. 0xff
  299. };
  300. ColorModel cm = config.getColorModel ();
  301. long pixel = cm.getDataElement (components, 0);
  302. attributes.setBackground (pixel);
  303. window.setAttributes (attributes);
  304. }
  305. }
  306. public void setBounds(int x, int y, int width, int height)
  307. {
  308. width = Math.max(width, 1);
  309. height = Math.max(height, 1);
  310. window.setBounds(x, y, width, height);
  311. ensureFlush();
  312. }
  313. public void reshape (int x, int y, int width, int height)
  314. {
  315. setBounds (x, y, width, height);
  316. }
  317. public void setCursor(Cursor cursor)
  318. {
  319. throw new UnsupportedOperationException("FIXME, not implemented");
  320. }
  321. public void setEnabled(boolean enabled)
  322. {
  323. throw new UnsupportedOperationException("FIXME, not implemented");
  324. }
  325. public void enable ()
  326. {
  327. setEnabled (true);
  328. }
  329. public void disable ()
  330. {
  331. setEnabled (false);
  332. }
  333. public void setEventMask(long eventMask)
  334. {
  335. if (this.eventMask != eventMask)
  336. {
  337. this.eventMask = eventMask;
  338. long xEventMask = getBasicEventMask ();
  339. if ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)
  340. {
  341. xEventMask |=
  342. WindowAttributes.MASK_BUTTON_PRESS |
  343. WindowAttributes.MASK_BUTTON_RELEASE;
  344. }
  345. attributes.setEventMask (xEventMask);
  346. window.setAttributes (attributes);
  347. ensureFlush ();
  348. }
  349. }
  350. public void setFont(Font font)
  351. {
  352. /* default canvas peer does not keep track of font, since it won't
  353. paint anything. */
  354. }
  355. public void setForeground(Color color)
  356. {
  357. /* default canvas peer does not keep track of foreground, since it won't
  358. paint anything. */
  359. }
  360. public void setVisible(boolean visible)
  361. {
  362. if (visible)
  363. {
  364. window.map();
  365. ensureFlush();
  366. }
  367. else
  368. {
  369. window.unmap();
  370. ensureFlush();
  371. }
  372. }
  373. public void show ()
  374. {
  375. setVisible (true);
  376. }
  377. public void hide ()
  378. {
  379. setVisible (false);
  380. }
  381. public boolean isFocusable ()
  382. {
  383. return false;
  384. }
  385. public boolean requestFocus (Component source, boolean b1,
  386. boolean b2, long x)
  387. {
  388. return false;
  389. }
  390. public boolean requestFocus (Component source, boolean b1,
  391. boolean b2, long x,
  392. CausedFocusEvent.Cause cause)
  393. {
  394. return false;
  395. }
  396. public boolean isObscured ()
  397. {
  398. return false;
  399. }
  400. public boolean canDetermineObscurity ()
  401. {
  402. return false;
  403. }
  404. public void coalescePaintEvent (PaintEvent e)
  405. {
  406. }
  407. public void updateCursorImmediately ()
  408. {
  409. }
  410. public VolatileImage createVolatileImage (int width, int height)
  411. {
  412. return null;
  413. }
  414. public boolean handlesWheelScrolling ()
  415. {
  416. return false;
  417. }
  418. public void createBuffers (int x, BufferCapabilities capabilities)
  419. throws java.awt.AWTException
  420. {
  421. }
  422. public Image getBackBuffer ()
  423. {
  424. return null;
  425. }
  426. public void flip (BufferCapabilities.FlipContents contents)
  427. {
  428. }
  429. public void destroyBuffers ()
  430. {
  431. }
  432. static class DoMap implements Runnable
  433. {
  434. Window window;
  435. public DoMap(Window w)
  436. {
  437. this.window = w;
  438. }
  439. public void run()
  440. {
  441. window.map();
  442. }
  443. }
  444. /**
  445. * @since 1.5
  446. */
  447. public boolean isRestackSupported ()
  448. {
  449. return false;
  450. }
  451. /**
  452. * @since 1.5
  453. */
  454. public void cancelPendingPaint (int x, int y, int width, int height)
  455. {
  456. }
  457. /**
  458. * @since 1.5
  459. */
  460. public void restack ()
  461. {
  462. }
  463. /**
  464. * @since 1.5
  465. */
  466. public Rectangle getBounds ()
  467. {
  468. return null;
  469. }
  470. /**
  471. * @since 1.5
  472. */
  473. public void reparent (ContainerPeer parent)
  474. {
  475. }
  476. /**
  477. * @since 1.5
  478. */
  479. public void setBounds (int x, int y, int width, int height, int z)
  480. {
  481. }
  482. /**
  483. * @since 1.5
  484. */
  485. public boolean isReparentSupported ()
  486. {
  487. return false;
  488. }
  489. /**
  490. * @since 1.5
  491. */
  492. public void layout ()
  493. {
  494. }
  495. }