|
1 package org.eclipse.swt.internal.qt; |
|
2 |
|
3 import org.eclipse.swt.SWT; |
|
4 import org.eclipse.swt.SWTException; |
|
5 import org.eclipse.swt.widgets.Display; |
|
6 import org.eclipse.swt.widgets.Internal_PackageSupport; |
|
7 import org.eclipse.swt.widgets.Widget; |
|
8 |
|
9 /** |
|
10 * Base CSS Engine for eSWT. This engine recognizes only Qt |
|
11 * <a href="http://qt.nokia.com/doc/4.6/stylesheet.html">stylesheet</a> syntax. |
|
12 * |
|
13 * CSS id selectors are supported by setting an id to the widget as follows. |
|
14 * <pre> |
|
15 * Button myButton = new Button(composite, SWT.PUSH); |
|
16 * myButton.setData(WidgetConstant.CSS_ID,"myButtonId"); |
|
17 * </pre> |
|
18 * |
|
19 * <p><b>NOTE:</b> This class is intended for internal use only.</p> |
|
20 */ |
|
21 public final class BaseCSSEngine { |
|
22 private Display parent; |
|
23 |
|
24 /** |
|
25 * Creates an engine instance. |
|
26 * |
|
27 * @exception IllegalArgumentException |
|
28 * <ul> |
|
29 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
|
30 * <li>ERROR_INVALID_ARGUMENT - if the parent is disposed</li> |
|
31 * </ul> |
|
32 * @exception SWTException |
|
33 * <ul> |
|
34 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the |
|
35 * thread that created the parent</li> |
|
36 * </ul> |
|
37 */ |
|
38 public BaseCSSEngine(Display display){ |
|
39 if (display == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); |
|
40 if (display.isDisposed ()) SWT.error (SWT.ERROR_INVALID_ARGUMENT); |
|
41 if (display.getThread() != Thread.currentThread()) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS); |
|
42 this.parent = display; |
|
43 } |
|
44 |
|
45 /** |
|
46 * Applies the given string containing valid CSS to all the widgets created for the Display. |
|
47 * CSS used in this method can not have any relative urls (for example for imports and images). |
|
48 * Relative urls will not be resolved. |
|
49 * |
|
50 *<p><b>NOTE:</b> This is intended for internal use.</p> |
|
51 * |
|
52 * @param style |
|
53 * @exception IllegalArgumentException |
|
54 * <ul> |
|
55 * <li>ERROR_NULL_ARGUMENT - if style is null</li> |
|
56 * <li>ERROR_INVALID_ARGUMENT - if style is not a valid style string</li> |
|
57 * </ul> |
|
58 * @exception SWTException <ul> |
|
59 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
|
60 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
|
61 * </ul> |
|
62 */ |
|
63 public void applyCSS( String style ){ |
|
64 checkEngine(); |
|
65 if( style == null ) SWT.error (SWT.ERROR_NULL_ARGUMENT); |
|
66 if( style.startsWith("file:///") ) SWT.error (SWT.ERROR_INVALID_ARGUMENT); |
|
67 OS.QApplication_setStyleSheet(style); |
|
68 } |
|
69 |
|
70 /** |
|
71 * Applies the given string containing valid CSS to the widget. |
|
72 * CSS used in this method can not have any relative urls (for example for imports and images). |
|
73 * Relative urls will not be resolved. |
|
74 * |
|
75 * <p><b>NOTE:</b> This is intended for internal use.</p> |
|
76 * |
|
77 * @param style |
|
78 * @exception IllegalArgumentException |
|
79 * <ul> |
|
80 * <li>ERROR_NULL_ARGUMENT - if style is null</li> |
|
81 * <li>ERROR_INVALID_ARGUMENT - if style is not a valid style string</li> |
|
82 * </ul> |
|
83 * @exception SWTException <ul> |
|
84 * <li>ERROR_WIDGET_DISPOSED - if the receiver or the widget has been disposed</li> |
|
85 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
|
86 * </ul> |
|
87 */ |
|
88 public void applyCSS(Widget widget, String style ){ |
|
89 checkEngine(); |
|
90 if( style == null ) SWT.error (SWT.ERROR_NULL_ARGUMENT); |
|
91 if( widget == null ) SWT.error (SWT.ERROR_NULL_ARGUMENT); |
|
92 if( widget.isDisposed() ) SWT.error( SWT.ERROR_WIDGET_DISPOSED); |
|
93 if( style.startsWith("file:///") ) SWT.error (SWT.ERROR_INVALID_ARGUMENT); |
|
94 OS.QWidget_setStyleSheet(Internal_PackageSupport.handle(widget), style); |
|
95 } |
|
96 /** |
|
97 * Loads a file containing valid CSS and applies to all the widgets for Display. |
|
98 * This function bypasses java security checks for accessing the file. |
|
99 * |
|
100 * <p><b>NOTE:</b> This is intended for internal use.</p> |
|
101 * |
|
102 * @param style |
|
103 * @exception IllegalArgumentException <ul> |
|
104 * <li>ERROR_NULL_ARGUMENT - if the file name is null</li> |
|
105 * </ul> |
|
106 * @exception SWTException <ul> |
|
107 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
|
108 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
|
109 * </ul> |
|
110 */ |
|
111 public void loadCSS( String filename ){ |
|
112 checkEngine(); |
|
113 if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
114 OS.QApplication_setStyleSheet("file:///"+filename); |
|
115 } |
|
116 |
|
117 /** |
|
118 * Loads a file containing valid CSS and applies to the widget. |
|
119 * This function bypasses Java security checks for accessing the file. |
|
120 * |
|
121 * <p><b>NOTE:</b> This is intended for internal use.</p> |
|
122 * |
|
123 * @param style |
|
124 * @exception IllegalArgumentException |
|
125 * <ul> |
|
126 * <li>ERROR_NULL_ARGUMENT - if the file name is null</li> |
|
127 * </ul> |
|
128 * @exception SWTException <ul> |
|
129 * <li>ERROR_WIDGET_DISPOSED - if the receiver or the widget has been disposed</li> |
|
130 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
|
131 * </ul> |
|
132 */ |
|
133 public void loadCSS( Widget widget, String filename ){ |
|
134 if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
135 if( widget == null ) SWT.error (SWT.ERROR_NULL_ARGUMENT); |
|
136 if( widget.isDisposed() ) SWT.error( SWT.ERROR_WIDGET_DISPOSED); |
|
137 OS.QWidget_setStyleSheet(Internal_PackageSupport.handle(widget), "file:///"+filename); |
|
138 } |
|
139 |
|
140 /** |
|
141 * Dispose the engine. |
|
142 * @exception SWTException <ul> |
|
143 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
|
144 * </ul> |
|
145 * |
|
146 */ |
|
147 public void dispose(){ |
|
148 if(parent != null) { |
|
149 if (parent.getThread() != Thread.currentThread()) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS); |
|
150 parent = null; |
|
151 } |
|
152 } |
|
153 |
|
154 private void checkEngine(){ |
|
155 if ( parent == null || parent.isDisposed() ) SWT.error( SWT.ERROR_WIDGET_DISPOSED); |
|
156 if ( parent.getThread() != Thread.currentThread() ) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS); |
|
157 } |
|
158 } |