XToolkit.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* Copyright (C) 2000, 2002, 2003, 2005 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.*;
  8. import java.awt.dnd.*;
  9. import java.awt.dnd.peer.*;
  10. import java.awt.font.*;
  11. import java.awt.im.*;
  12. import java.awt.peer.*;
  13. import java.awt.image.ImageProducer;
  14. import java.awt.image.ImageObserver;
  15. import java.net.*;
  16. import java.awt.datatransfer.Clipboard;
  17. import java.io.InputStream;
  18. import java.text.AttributedString;
  19. import java.util.Map;
  20. import java.util.Properties;
  21. import gnu.gcj.xlib.Display;
  22. import gnu.gcj.xlib.Screen;
  23. import gnu.gcj.xlib.Visual;
  24. import gnu.java.awt.ClasspathToolkit;
  25. import gnu.java.awt.EmbeddedWindow;
  26. import gnu.java.awt.peer.ClasspathFontPeer;
  27. import gnu.java.awt.peer.EmbeddedWindowPeer;
  28. public class XToolkit extends ClasspathToolkit
  29. {
  30. static XToolkit INSTANCE;
  31. Display display;
  32. EventQueue queue;
  33. XEventLoop eventLoop;
  34. XGraphicsConfiguration defaultConfig;
  35. public XToolkit()
  36. {
  37. INSTANCE = this;
  38. display = new Display();
  39. synchronized (display)
  40. {
  41. queue = new XEventQueue(display);
  42. eventLoop = new XEventLoop(display, queue);
  43. }
  44. }
  45. public void flushIfIdle()
  46. {
  47. eventLoop.flushIfIdle();
  48. }
  49. protected ButtonPeer createButton(Button frontend)
  50. {
  51. // FIXME: Stubbed out, needs Swing:
  52. /*
  53. XCanvasPeer realPeer = new XCanvasPeer(frontend);
  54. SButtonPeer sbPeer = new SButtonPeer(frontend, realPeer);
  55. return sbPeer;
  56. */
  57. return null;
  58. }
  59. protected TextFieldPeer createTextField(TextField frontend)
  60. {
  61. return null; // FIXME
  62. }
  63. protected LabelPeer createLabel(Label frontend)
  64. {
  65. return null; // FIXME
  66. }
  67. protected ListPeer createList(List frontend)
  68. {
  69. return null; // FIXME
  70. }
  71. protected CheckboxPeer createCheckbox(Checkbox frontend)
  72. {
  73. return null; // FIXME
  74. }
  75. protected ScrollbarPeer createScrollbar(Scrollbar frontend)
  76. {
  77. return null; // FIXME
  78. }
  79. protected ScrollPanePeer createScrollPane(ScrollPane frontend)
  80. {
  81. return null; // FIXME
  82. }
  83. protected TextAreaPeer createTextArea(TextArea frontend)
  84. {
  85. return null; // FIXME
  86. }
  87. protected ChoicePeer createChoice(Choice frontend)
  88. {
  89. return null; // FIXME
  90. }
  91. protected FramePeer createFrame(Frame frontend) {
  92. return new XFramePeer(frontend);
  93. }
  94. protected CanvasPeer createCanvas(Canvas frontend) {
  95. XCanvasPeer peer = new XCanvasPeer(frontend);
  96. return peer;
  97. }
  98. protected PanelPeer createPanel(Panel frontend) {
  99. return new XPanelPeer(frontend);
  100. }
  101. protected WindowPeer createWindow(Window frontend)
  102. {
  103. return null; // FIXME
  104. }
  105. protected DialogPeer createDialog(Dialog frontend)
  106. {
  107. return null; // FIXME
  108. }
  109. protected MenuBarPeer createMenuBar(MenuBar frontend)
  110. {
  111. return null; // FIXME
  112. }
  113. protected MenuPeer createMenu(Menu frontend)
  114. {
  115. return null; // FIXME
  116. }
  117. protected PopupMenuPeer createPopupMenu(PopupMenu frontend)
  118. {
  119. return null; // FIXME
  120. }
  121. protected MenuItemPeer createMenuItem(MenuItem frontend)
  122. {
  123. return null; // FIXME
  124. }
  125. protected FileDialogPeer createFileDialog(FileDialog frontend)
  126. {
  127. return null; // FIXME
  128. }
  129. protected CheckboxMenuItemPeer
  130. createCheckboxMenuItem(CheckboxMenuItem frontend)
  131. {
  132. return null; // FIXME
  133. }
  134. protected java.awt.peer.FontPeer getFontPeer(String name, int style)
  135. {
  136. return new XFontPeer (name,style);
  137. }
  138. public Dimension getScreenSize()
  139. {
  140. throw new UnsupportedOperationException("not implemented yet");
  141. }
  142. public int getScreenResolution()
  143. {
  144. throw new UnsupportedOperationException("not implemented yet");
  145. }
  146. public java.awt.image.ColorModel getColorModel()
  147. {
  148. return getDefaultXGraphicsConfiguration().getColorModel();
  149. }
  150. public String[] getFontList()
  151. {
  152. throw new UnsupportedOperationException("not implemented yet");
  153. }
  154. public FontMetrics getFontMetrics(Font font)
  155. {
  156. return getDefaultXGraphicsConfiguration().getXFontMetrics(font);
  157. }
  158. public void sync()
  159. {
  160. flushIfIdle ();
  161. // FIXME: should instead wait for eventLoop to go idle
  162. // (perhaps send a dummy event there and block till it makes
  163. // it through the queue)
  164. }
  165. public Image getImage(String filename)
  166. {
  167. return createImage(filename);
  168. }
  169. public Image getImage(URL url)
  170. {
  171. throw new UnsupportedOperationException("not implemented yet");
  172. }
  173. public Image createImage(String filename)
  174. {
  175. // FIXME: Stubbed out. We need a proper image I/O API.
  176. /*
  177. BufferedImage jpeg;
  178. FileInputStream fis = openFile(filename);
  179. if (fis == null)
  180. return null;
  181. BasicRasterImageConsumer consumer = new BasicRasterImageConsumer();
  182. JPEGImageDecoder jid = new JPEGImageDecoder(fis);
  183. jid.startProduction(consumer);
  184. jpeg = consumer.getImage();
  185. int w = jpeg.getWidth();
  186. int h = jpeg.getHeight();
  187. BufferedImage img =
  188. getDefaultXGraphicsConfiguration().createCompatibleImage(w, h);
  189. Renderers renderers = Renderers.getInstance();
  190. RasterOp renderer = renderers.createRenderer(jpeg.getColorModel(),
  191. jpeg.getSampleModel(),
  192. img.getColorModel(),
  193. img.getSampleModel());
  194. if (renderer == null)
  195. {
  196. throw new UnsupportedOperationException("couldn't find renderer");
  197. }
  198. renderer.filter(jpeg.getRaster(), img.getRaster());
  199. return img;
  200. */
  201. return null;
  202. }
  203. public Image createImage(URL url)
  204. {
  205. throw new UnsupportedOperationException("not implemented yet");
  206. }
  207. public boolean prepareImage(Image image,
  208. int width,
  209. int height,
  210. ImageObserver observer)
  211. {
  212. throw new UnsupportedOperationException("not implemented yet");
  213. }
  214. public int checkImage(Image image,
  215. int width,
  216. int height,
  217. ImageObserver observer)
  218. {
  219. throw new UnsupportedOperationException("not implemented yet");
  220. }
  221. public Image createImage(ImageProducer producer)
  222. {
  223. throw new UnsupportedOperationException("not implemented yet");
  224. }
  225. public Image createImage(byte[] imagedata,
  226. int imageoffset,
  227. int imagelength)
  228. {
  229. throw new UnsupportedOperationException("not implemented yet");
  230. }
  231. /*
  232. public PrintJob getPrintJob(Frame frame,
  233. String jobtitle,
  234. Properties props);
  235. */
  236. public void beep()
  237. {
  238. throw new UnsupportedOperationException("not implemented yet");
  239. }
  240. public Clipboard getSystemClipboard()
  241. {
  242. return null; // FIXME
  243. }
  244. protected EventQueue getSystemEventQueueImpl()
  245. {
  246. return queue;
  247. }
  248. public PrintJob getPrintJob (Frame frame, String title, Properties props)
  249. {
  250. return null; // FIXME
  251. }
  252. XGraphicsConfiguration getDefaultXGraphicsConfiguration()
  253. {
  254. if (defaultConfig == null)
  255. {
  256. Screen screen = display.getDefaultScreen();
  257. Visual visual = screen.getRootVisual();
  258. defaultConfig = new XGraphicsConfiguration(visual);
  259. // ASSERT:
  260. if (!defaultConfig.getVisual().getScreen().equals(screen))
  261. {
  262. String msg = "screen of graphics configuration is not " +
  263. "default screen";
  264. throw new Error(msg);
  265. }
  266. }
  267. return defaultConfig;
  268. }
  269. public DragSourceContextPeer
  270. createDragSourceContextPeer(DragGestureEvent dge)
  271. throws InvalidDnDOperationException
  272. {
  273. throw new UnsupportedOperationException("not implemented");
  274. }
  275. public DragGestureRecognizer
  276. createDragGestureRecognizer(Class abstractRecognizerClass,
  277. DragSource ds, Component c,
  278. int srcActions, DragGestureListener dgl)
  279. {
  280. throw new UnsupportedOperationException("not implemented");
  281. }
  282. public Map mapInputMethodHighlight(InputMethodHighlight highlight)
  283. {
  284. throw new UnsupportedOperationException("not implemented");
  285. }
  286. /** Returns a shared instance of the local, platform-specific
  287. * graphics environment.
  288. *
  289. * <p>This method is specific to GNU Classpath. It gets called by
  290. * the Classpath implementation of {@link
  291. * GraphicsEnvironment.getLocalGraphcisEnvironment()}.
  292. */
  293. public GraphicsEnvironment getLocalGraphicsEnvironment ()
  294. {
  295. return new XGraphicsEnvironment (this);
  296. }
  297. /** Acquires an appropriate {@link ClasspathFontPeer}, for use in
  298. * classpath's implementation of {@link java.awt.Font}.
  299. *
  300. * @param name The logical name of the font. This may be either a face
  301. * name or a logical font name, or may even be null. A default
  302. * implementation of name decoding is provided in
  303. * {@link ClasspathFontPeer}, but may be overridden in other toolkits.
  304. *
  305. * @param attrs Any extra {@link java.awt.font.TextAttribute} attributes
  306. * this font peer should have, such as size, weight, family name, or
  307. * transformation.
  308. */
  309. public ClasspathFontPeer getClasspathFontPeer (String name, Map attrs)
  310. {
  311. int style = Font.PLAIN;
  312. float size = 12;
  313. if (attrs.containsKey (TextAttribute.WEIGHT))
  314. {
  315. Float weight = (Float) attrs.get (TextAttribute.WEIGHT);
  316. if (weight.floatValue () >= TextAttribute.WEIGHT_BOLD.floatValue ())
  317. style += Font.BOLD;
  318. }
  319. if (attrs.containsKey (TextAttribute.POSTURE))
  320. {
  321. Float posture = (Float) attrs.get (TextAttribute.POSTURE);
  322. if (posture.floatValue () >= TextAttribute.POSTURE_OBLIQUE.floatValue ())
  323. style += Font.ITALIC;
  324. }
  325. if (attrs.containsKey (TextAttribute.SIZE))
  326. {
  327. Float fsize = (Float) attrs.get (TextAttribute.SIZE);
  328. size = fsize.floatValue ();
  329. }
  330. return new XFontPeer (name,style,size);
  331. }
  332. /** Creates a font, reading the glyph definitions from a stream.
  333. *
  334. * <p>This method provides the platform-specific implementation for
  335. * the static factory method {@link Font#createFont(int,
  336. * java.io.InputStream)}.
  337. *
  338. * @param format the format of the font data, such as {@link
  339. * Font#TRUETYPE_FONT}. An implementation may ignore this argument
  340. * if it is able to automatically recognize the font format from the
  341. * provided data.
  342. *
  343. * @param stream an input stream from where the font data is read
  344. * in. The stream will be advanced to the position after the font
  345. * data, but not closed.
  346. *
  347. * @throws IllegalArgumentException if <code>format</code> is
  348. * not supported.
  349. *
  350. * @throws FontFormatException if <code>stream</code> does not
  351. * contain data in the expected format, or if required tables are
  352. * missing from a font.
  353. *
  354. * @throws IOException if a problem occurs while reading in the
  355. * contents of <code>stream</code>.
  356. */
  357. public Font createFont (int format, InputStream stream)
  358. {
  359. throw new java.lang.UnsupportedOperationException ();
  360. }
  361. public RobotPeer createRobot (GraphicsDevice screen) throws AWTException
  362. {
  363. throw new java.lang.UnsupportedOperationException ();
  364. }
  365. public EmbeddedWindowPeer createEmbeddedWindow (EmbeddedWindow w)
  366. {
  367. throw new java.lang.UnsupportedOperationException ();
  368. }
  369. public boolean nativeQueueEmpty()
  370. {
  371. // Tell EventQueue the native queue is empty, because XEventLoop
  372. // separately ensures that native events are posted to AWT.
  373. return true;
  374. }
  375. public void wakeNativeQueue()
  376. {
  377. // Not implemented, because the native queue is always awake.
  378. // (i.e. it's polled in a thread separate from the AWT dispatch thread)
  379. }
  380. /** Checks the native event queue for events. If blocking, waits until an
  381. * event is available before returning, unless interrupted by
  382. * wakeNativeQueue. If non-blocking, returns immediately even if no
  383. * event is available.
  384. *
  385. * @param locked The calling EventQueue
  386. * @param block If true, waits for a native event before returning
  387. */
  388. public void iterateNativeQueue(java.awt.EventQueue locked, boolean block)
  389. {
  390. // There is nothing to do here except block, because XEventLoop
  391. // iterates the queue in a dedicated thread.
  392. if (block)
  393. {
  394. try
  395. {
  396. queue.wait ();
  397. }
  398. catch (InterruptedException ie)
  399. {
  400. // InterruptedException intentionally ignored
  401. }
  402. }
  403. }
  404. public void setAlwaysOnTop(boolean b)
  405. {
  406. // TODO: Implement properly.
  407. }
  408. public boolean isModalExclusionTypeSupported
  409. (Dialog.ModalExclusionType modalExclusionType)
  410. {
  411. // TODO: Implement properly.
  412. return false;
  413. }
  414. public boolean isModalityTypeSupported(Dialog.ModalityType modalityType)
  415. {
  416. // TODO: Implement properly.
  417. return false;
  418. }
  419. }