1 /*******************************************************************************
2  * Copyright (c) 2018 Red Hat and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Red Hat - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.tests.gtk.snippets;
15 
16 
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.KeyAdapter;
19 import org.eclipse.swt.events.KeyEvent;
20 import org.eclipse.swt.events.MouseAdapter;
21 import org.eclipse.swt.events.MouseEvent;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.TabFolder;
26 import org.eclipse.swt.widgets.TabItem;
27 import org.eclipse.swt.widgets.Table;
28 import org.eclipse.swt.widgets.TableItem;
29 import org.eclipse.swt.widgets.Text;
30 
31 // clicking on a row item and typing appends characters.
32 // Gtk2: Works as expected.
33 // Gtk3: Spam of errors, black second window.
34 //(SWT:7324): Gdk-WARNING **: gdk_window_new(): parent is destroyed
35 //(SWT:7324): Gdk-CRITICAL **: gdk_window_set_user_data: assertion 'GDK_IS_WINDOW (window)' failed
36 //(SWT:7324): Gdk-CRITICAL **: gdk_window_get_scale_factor: assertion 'GDK_IS_WINDOW (window)' failed
37 //(SWT:7324): GLib-GObject-CRITICAL **: g_object_ref: assertion 'G_IS_OBJECT (object)' failed
38 
39 public class Bug510803_TabFolder_editable_table_brokenGtk3 {
main(String[] args)40 	public static void main(String[] args) {
41 		Shell shell = shellSetup();
42 
43 		final TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
44 		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
45 		tabItem.setText("Hello tab");
46 
47 		Table table = new Table(tabFolder, SWT.NONE);
48 		TableItem tableItem = new TableItem(table, SWT.None);
49 		tableItem.setText("Item 1");
50 		TableItem tableItem2 = new TableItem(table, SWT.None);
51 		tableItem2.setText("Item 2");
52 
53 		// Notes:
54 		// - Seems to occur with any control, not just Text. (Tested with Button also)
55 		final Text cellEditorText = new Text(table, SWT.SINGLE); // Note,
56 		cellEditorText.setText("Hello world");
57 
58 //		 Listeners that make typing into Table edit controls. Useful to test
59 //		 functionality, but errors occur without the listeners also.
60 		table.addMouseListener(new MouseAdapter() {
61 		@Override
62 		public void mouseUp(MouseEvent e) {
63 		cellEditorText.setFocus();
64 		}
65 		});
66 
67 		cellEditorText.addKeyListener(new KeyAdapter() {
68 		@Override
69 		public void keyPressed(KeyEvent e) {
70 		TableItem selectedItem = table.getSelection()[0];
71 		selectedItem.setText(selectedItem.getText() + e.character);
72 		}
73 		});
74 
75 		// Location of setControl() method call has an impact.
76 		// If it's before 'Text' creation, no errors are thrown into the
77 		// console.
78 		tabItem.setControl(table);
79 
80 		mainEventLoop(shell);
81 	}
82 
shellSetup()83 	private static Shell shellSetup() {
84 		final Display display = new Display();
85 		Shell shell = new Shell(display);
86 		shell.setLayout(new FillLayout());
87 		return shell;
88 	}
89 
mainEventLoop(Shell shell)90 	private static void mainEventLoop(Shell shell) {
91 		Display display = shell.getDisplay();
92 		shell.open();
93 		shell.setSize(200, 300);
94 		while (!shell.isDisposed()) {
95 			if (!display.readAndDispatch())
96 				display.sleep();
97 		}
98 		display.dispose();
99 	}
100 }
101