|
1 /******************************************************************************* |
|
2 * Copyright (c) 2000, 2005 IBM Corporation and others. |
|
3 * All rights reserved. This program and the accompanying materials |
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
|
5 * which accompanies this distribution, and is available at |
|
6 * http://www.eclipse.org/legal/epl-v10.html |
|
7 * |
|
8 * Contributors: |
|
9 * IBM Corporation - initial API and implementation |
|
10 * Lynne Kues (IBM Corp) - modified to reflect eSWT API subset |
|
11 * Nokia Corporation - S60 implementation |
|
12 *******************************************************************************/ |
|
13 package org.eclipse.swt.widgets; |
|
14 |
|
15 |
|
16 import org.eclipse.swt.SWT; |
|
17 import org.eclipse.swt.events.SelectionEvent; |
|
18 import org.eclipse.swt.events.SelectionListener; |
|
19 import org.eclipse.swt.widgets.Shell; |
|
20 import org.eclipse.swt.widgets.Combo; |
|
21 import org.eclipse.swt.widgets.Button; |
|
22 import org.eclipse.swt.widgets.Label; |
|
23 import org.eclipse.swt.graphics.Font; |
|
24 import org.eclipse.swt.graphics.FontData; |
|
25 import org.eclipse.swt.graphics.Color; |
|
26 import org.eclipse.swt.graphics.Image; |
|
27 import org.eclipse.swt.graphics.GC; |
|
28 import org.eclipse.swt.graphics.RGB; |
|
29 import org.eclipse.swt.graphics.Rectangle; |
|
30 import org.eclipse.swt.layout.FillLayout; |
|
31 import org.eclipse.swt.layout.GridData; |
|
32 import org.eclipse.swt.layout.GridLayout; |
|
33 import org.eclipse.ercp.swt.expanded.internal.OS; |
|
34 |
|
35 |
|
36 /** |
|
37 * Instances of this class allow the user to select a font |
|
38 * from all available fonts in the system. |
|
39 * <dl> |
|
40 * <dt><b>Styles:</b></dt> |
|
41 * <dd>(none)</dd> |
|
42 * <dt><b>Events:</b></dt> |
|
43 * <dd>(none)</dd> |
|
44 * </dl> |
|
45 * <p> |
|
46 * IMPORTANT: This class is intended to be subclassed <em>only</em> |
|
47 * within the SWT implementation. |
|
48 * </p> |
|
49 */ |
|
50 public class FontDialog extends Dialog |
|
51 { |
|
52 // Constants |
|
53 private final int[] HEIGHTS = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 24, 36, 48, 72}; |
|
54 private final String BOLD = "B"; |
|
55 private final String ITALIC = "I"; |
|
56 |
|
57 // Components |
|
58 private Shell shell; |
|
59 private Combo faceCombo; |
|
60 private Combo heightCombo; |
|
61 private Label previewLabel; |
|
62 private Button weightButton; |
|
63 private Button styleButton; |
|
64 private Button colorButton; |
|
65 private Composite row4; |
|
66 Button lsk; |
|
67 Button rsk; |
|
68 |
|
69 // Component data |
|
70 private String title = ""; |
|
71 private String stringOk = ""; |
|
72 private String stringCancel = ""; |
|
73 private Font previewFont; |
|
74 private Color previewColor; |
|
75 private Image colorButtonImg; |
|
76 private Font weightButtonFont; |
|
77 private Font styleButtonFont; |
|
78 private FontData defFontData; |
|
79 |
|
80 // Result data |
|
81 private FontData resFontData = null; |
|
82 private RGB resRgb = null; |
|
83 private boolean resRgbChanged = false; |
|
84 |
|
85 // Flags |
|
86 private boolean isOpen = false; |
|
87 |
|
88 // Platform fonts |
|
89 private String[] platformFonts; |
|
90 |
|
91 // Listeners |
|
92 private Listener settingsListener; |
|
93 private Listener skListener; |
|
94 |
|
95 /** |
|
96 * Constructs a new instance of this class given its parent |
|
97 * and a style value describing its behavior and appearance. |
|
98 * <p> |
|
99 * The style value is either one of the style constants defined in |
|
100 * class <code>SWT</code> which is applicable to instances of this |
|
101 * class, or must be built by <em>bitwise OR</em>'ing together |
|
102 * (that is, using the <code>int</code> "|" operator) two or more |
|
103 * of those <code>SWT</code> style constants. The class description |
|
104 * lists the style constants that are applicable to the class. |
|
105 * Style bits are also inherited from superclasses. |
|
106 * </p> |
|
107 * |
|
108 * @param parent a shell which will be the parent of the new instance |
|
109 * @param style the style of dialog to construct |
|
110 * |
|
111 * @exception IllegalArgumentException <ul> |
|
112 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
|
113 * </ul> |
|
114 * @exception SWTException <ul> |
|
115 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
|
116 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
|
117 * </ul> |
|
118 */ |
|
119 public FontDialog(Shell parent) |
|
120 { |
|
121 this(parent, SWT.NONE); |
|
122 } |
|
123 |
|
124 /** |
|
125 * Constructs a new instance of this class given only its parent. |
|
126 * |
|
127 * @param parent a shell which will be the parent of the new instance |
|
128 * |
|
129 * @exception IllegalArgumentException <ul> |
|
130 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
|
131 * </ul> |
|
132 * @exception SWTException <ul> |
|
133 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
|
134 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
|
135 * </ul> |
|
136 */ |
|
137 public FontDialog(Shell parent, int style) |
|
138 { |
|
139 super(parent, style); |
|
140 checkSubclass(); |
|
141 stringOk = OS.FontDialog_GetCommandOkTitle(); |
|
142 stringCancel = OS.FontDialog_GetCommandCancelTitle(); |
|
143 platformFonts = OS.FontDialog_GetFontNames(true); |
|
144 defFontData = parent.getDisplay().getSystemFont().getFontData()[0]; |
|
145 } |
|
146 |
|
147 /** |
|
148 * Returns a FontData set describing the font that was |
|
149 * selected in the dialog, or null if none is available. |
|
150 * |
|
151 * @return the FontData for the selected font, or null |
|
152 * @since 2.1.1 |
|
153 */ |
|
154 public FontData[] getFontList() |
|
155 { |
|
156 if (resFontData == null) |
|
157 { |
|
158 return null; |
|
159 } |
|
160 else |
|
161 { |
|
162 return new FontData[] {resFontData}; |
|
163 } |
|
164 } |
|
165 |
|
166 /** |
|
167 * Returns the currently selected color in the receiver. |
|
168 * |
|
169 * @return the RGB value for the selected color, may be null |
|
170 * |
|
171 * @see PaletteData#getRGBs |
|
172 * |
|
173 * @since 2.1 |
|
174 */ |
|
175 public RGB getRGB() |
|
176 { |
|
177 return resRgb; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Makes the dialog visible and brings it to the front |
|
182 * of the display. |
|
183 * |
|
184 * @return a FontData object describing the font that was selected, |
|
185 * or null if the dialog was cancelled or an error occurred |
|
186 * |
|
187 * @exception SWTException <ul> |
|
188 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li> |
|
189 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li> |
|
190 * </ul> |
|
191 */ |
|
192 public FontData open() |
|
193 { |
|
194 // Check thread access |
|
195 if (!parent.getDisplay().isValidThread()) |
|
196 { |
|
197 error(SWT.ERROR_THREAD_INVALID_ACCESS); |
|
198 } |
|
199 |
|
200 // Dialog shell |
|
201 int flags = SWT.BORDER | SWT.APPLICATION_MODAL | SWT.ON_TOP; |
|
202 if (title != null && title.length() > 0) |
|
203 flags |= SWT.TITLE; |
|
204 shell = new Shell(getParent(), flags); |
|
205 shell.setText(title); |
|
206 |
|
207 if (org.eclipse.swt.internal.symbian.OS.windowServer |
|
208 < org.eclipse.swt.internal.symbian.OS.WS_SYMBIAN_S60_92) |
|
209 { |
|
210 // On 5.0 the modal Shell needs to be made visible before Combo is created. |
|
211 shell.open(); |
|
212 } |
|
213 |
|
214 // Listen to skin and resolution changes |
|
215 settingsListener = new Listener() |
|
216 { |
|
217 public void handleEvent(Event ev) |
|
218 { |
|
219 if (resRgb == null || !resRgbChanged) |
|
220 { |
|
221 resRgb = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND).getRGB(); |
|
222 updatePreviewColor(); |
|
223 } |
|
224 Rectangle rect = shell.internal_getDefaultBounds(); // default dialog bounds |
|
225 Rectangle screenRect = parent.getDisplay().getBounds(); |
|
226 rect.height += row4.computeSize(shell.getClientArea().width, SWT.DEFAULT).y; |
|
227 if (screenRect.height > screenRect.width) |
|
228 { |
|
229 // Bottom position for portrait orientation |
|
230 rect.y = screenRect.height - rect.height; |
|
231 } |
|
232 else |
|
233 { |
|
234 // Vertical middle position for landscape orientation |
|
235 rect.y = (screenRect.height - rect.height) / 2; |
|
236 } |
|
237 |
|
238 shell.setBounds(rect); |
|
239 updatePreviewFont(); |
|
240 } |
|
241 }; |
|
242 shell.getDisplay().addListener(SWT.Settings, settingsListener); |
|
243 |
|
244 // Listen to softkey presses and deliver them to the buttons |
|
245 skListener = new Listener(){ |
|
246 public void handleEvent(Event event) { |
|
247 switch(event.keyCode) { |
|
248 case -6: // LSK |
|
249 isOpen = false; |
|
250 break; |
|
251 case -7: // RSK |
|
252 resFontData = null; |
|
253 resRgb = null; |
|
254 isOpen = false; |
|
255 break; |
|
256 default: |
|
257 break; |
|
258 } |
|
259 }}; |
|
260 parent.getDisplay().addFilter(SWT.KeyDown, skListener); |
|
261 |
|
262 // Create default results if not already set |
|
263 if (resFontData == null) |
|
264 { |
|
265 resFontData = defFontData; |
|
266 } |
|
267 if (resRgb == null || !resRgbChanged) |
|
268 { |
|
269 resRgb = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND).getRGB(); |
|
270 } |
|
271 |
|
272 GridLayout layout0 = new GridLayout(); |
|
273 layout0.marginTop = 0; |
|
274 layout0.marginBottom = 0; |
|
275 layout0.marginLeft = 0; |
|
276 layout0.marginRight = 0; |
|
277 layout0.marginWidth = 0; |
|
278 layout0.marginHeight = 0; |
|
279 layout0.horizontalSpacing = 1; |
|
280 layout0.verticalSpacing = 1; |
|
281 shell.setLayout(layout0); |
|
282 |
|
283 // Row 1 |
|
284 createFaceControl(shell); |
|
285 |
|
286 // Row 2 |
|
287 Composite row2 = new Composite(shell, 0); |
|
288 row2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
|
289 GridLayout layout2 = new GridLayout(5, true); |
|
290 layout2.marginTop = 0; |
|
291 layout2.marginBottom = 0; |
|
292 layout2.marginLeft = 0; |
|
293 layout2.marginRight = 0; |
|
294 layout2.marginWidth = 0; |
|
295 layout2.marginHeight = 0; |
|
296 layout2.horizontalSpacing = 1; |
|
297 layout2.verticalSpacing = 1; |
|
298 row2.setLayout(layout2); |
|
299 createHeightControl(row2); |
|
300 createWeightControl(row2); |
|
301 createStyleControl(row2); |
|
302 createColorControl(row2); |
|
303 |
|
304 // Row 3 |
|
305 createPreviewControl(shell); |
|
306 |
|
307 // Row 4 |
|
308 row4 = new Composite(shell, 0); |
|
309 row4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
|
310 row4.setLayout(new FillLayout(SWT.HORIZONTAL)); |
|
311 |
|
312 createButtons(row4); |
|
313 |
|
314 Rectangle rect = shell.getBounds(); // default dialog bounds |
|
315 Rectangle screenRect = parent.getDisplay().getBounds(); |
|
316 rect.height += row4.computeSize(shell.getClientArea().width, SWT.DEFAULT).y; |
|
317 if (screenRect.height > screenRect.width) |
|
318 { |
|
319 // Bottom position for portrait orientation |
|
320 rect.y = screenRect.height - rect.height; |
|
321 } |
|
322 else |
|
323 { |
|
324 // Vertical middle position for landscape orientation |
|
325 rect.y = (screenRect.height - rect.height) / 2; |
|
326 } |
|
327 |
|
328 shell.setBounds(rect); |
|
329 |
|
330 if (org.eclipse.swt.internal.symbian.OS.windowServer |
|
331 >= org.eclipse.swt.internal.symbian.OS.WS_SYMBIAN_S60_92) |
|
332 { |
|
333 shell.open(); |
|
334 } |
|
335 |
|
336 // Block till submited |
|
337 isOpen = true; |
|
338 Display display = shell.getDisplay(); |
|
339 while (isOpen) |
|
340 { |
|
341 if (!display.readAndDispatch()) |
|
342 { |
|
343 display.sleep(); |
|
344 } |
|
345 } |
|
346 destroyControls(); |
|
347 |
|
348 return resFontData; |
|
349 } |
|
350 |
|
351 /** |
|
352 * Sets a set of FontData objects describing the font to |
|
353 * be selected by default in the dialog, or null to let |
|
354 * the platform choose one. |
|
355 * |
|
356 * @param fontData the set of FontData objects to use initially, or null |
|
357 * @since 2.1.1 |
|
358 */ |
|
359 public void setFontList(FontData [] fontData) |
|
360 { |
|
361 if (fontData != null && fontData.length > 0) |
|
362 { |
|
363 resFontData = fontData[0]; |
|
364 } |
|
365 else |
|
366 { |
|
367 resFontData = null; |
|
368 } |
|
369 } |
|
370 |
|
371 /** |
|
372 * Sets the receiver's selected color to be the argument. |
|
373 * |
|
374 * @param rgb the new RGB value for the selected color, may be |
|
375 * null to let the platform to select a default when |
|
376 * open() is called |
|
377 * |
|
378 * @see PaletteData#getRGBs |
|
379 * |
|
380 * @since 2.1 |
|
381 */ |
|
382 public void setRGB(RGB rgb) |
|
383 { |
|
384 resRgb = rgb; |
|
385 } |
|
386 |
|
387 /** |
|
388 * Sets the receiver's text, which is the string that the |
|
389 * window manager will typically display as the receiver's |
|
390 * <em>title</em>, to the argument, which must not be null. |
|
391 * |
|
392 * @param string the new text |
|
393 * |
|
394 * @exception IllegalArgumentException <ul> |
|
395 * <li>ERROR_NULL_ARGUMENT - if the text is null</li> |
|
396 * </ul> |
|
397 * @exception SWTException <ul> |
|
398 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
|
399 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
|
400 * </ul> |
|
401 */ |
|
402 public void setText(String string) |
|
403 { |
|
404 super.setText(string); |
|
405 title = string; |
|
406 } |
|
407 |
|
408 private void createButtons(final Composite parent) { |
|
409 lsk = new Button(parent, 0); |
|
410 shell.setDefaultButton(lsk); |
|
411 lsk.setText(stringOk); |
|
412 lsk.addSelectionListener( |
|
413 new SelectionListener() |
|
414 { |
|
415 public void widgetDefaultSelected(SelectionEvent ev) |
|
416 { |
|
417 } |
|
418 public void widgetSelected(SelectionEvent ev) |
|
419 { |
|
420 isOpen = false; |
|
421 } |
|
422 }); |
|
423 |
|
424 rsk = new Button(parent, 0); |
|
425 rsk.setText(stringCancel); |
|
426 rsk.addSelectionListener( |
|
427 new SelectionListener() |
|
428 { |
|
429 public void widgetDefaultSelected(SelectionEvent ev) |
|
430 { |
|
431 } |
|
432 public void widgetSelected(SelectionEvent ev) |
|
433 { |
|
434 resFontData = null; |
|
435 resRgb = null; |
|
436 isOpen = false; |
|
437 } |
|
438 }); |
|
439 } |
|
440 |
|
441 private void createColorControl(final Composite parent) { |
|
442 // Control |
|
443 colorButton = new Button(parent, SWT.PUSH); |
|
444 colorButton.addSelectionListener( |
|
445 new SelectionListener() |
|
446 { |
|
447 public void widgetDefaultSelected(SelectionEvent ev) |
|
448 { |
|
449 } |
|
450 public void widgetSelected(SelectionEvent ev) |
|
451 { |
|
452 ColorDialog dlg = new ColorDialog(parent.getShell(), SWT.NONE); |
|
453 RGB rgb = dlg.open(); |
|
454 fontColorChanged(rgb); |
|
455 } |
|
456 }); |
|
457 |
|
458 // Layout |
|
459 colorButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); |
|
460 } |
|
461 |
|
462 private void createFaceControl(final Shell parent) { |
|
463 // Control |
|
464 faceCombo = new Combo(parent, SWT.READ_ONLY); |
|
465 faceCombo.addSelectionListener( |
|
466 new SelectionListener() |
|
467 { |
|
468 public void widgetDefaultSelected(SelectionEvent ev) |
|
469 { |
|
470 } |
|
471 public void widgetSelected(SelectionEvent ev) |
|
472 { |
|
473 fontFaceChanged(); |
|
474 } |
|
475 }); |
|
476 |
|
477 // Items |
|
478 faceCombo.setItems(platformFonts); |
|
479 |
|
480 // Defaults |
|
481 int index = faceCombo.indexOf(resFontData.getName()); |
|
482 if (index >= 0) |
|
483 { |
|
484 faceCombo.select(index); |
|
485 } |
|
486 |
|
487 // Layout |
|
488 faceCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
|
489 } |
|
490 |
|
491 private void createHeightControl(final Composite parent) { |
|
492 // Control |
|
493 heightCombo = new Combo(parent, SWT.READ_ONLY); |
|
494 heightCombo.addSelectionListener( |
|
495 new SelectionListener() |
|
496 { |
|
497 public void widgetDefaultSelected(SelectionEvent ev) |
|
498 { |
|
499 } |
|
500 public void widgetSelected(SelectionEvent ev) |
|
501 { |
|
502 fontHeightChanged(); |
|
503 } |
|
504 }); |
|
505 |
|
506 // Items |
|
507 String[] items = new String[HEIGHTS.length]; |
|
508 for (int i = 0; i < HEIGHTS.length; i++) |
|
509 { |
|
510 items[i] = Integer.toString(HEIGHTS[i]); |
|
511 } |
|
512 heightCombo.setItems(items); |
|
513 |
|
514 // Defaults |
|
515 int pos = -1; |
|
516 for (int i = 0; i < HEIGHTS.length; i++) |
|
517 { |
|
518 if (resFontData.getHeight() <= HEIGHTS[i]) |
|
519 { |
|
520 pos = i; |
|
521 break; |
|
522 } |
|
523 } |
|
524 if (pos >= 0) |
|
525 { |
|
526 heightCombo.select(pos); |
|
527 } |
|
528 |
|
529 // Layout |
|
530 heightCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); |
|
531 } |
|
532 |
|
533 private void createStyleControl(final Composite parent) { |
|
534 // Control |
|
535 styleButton = new Button(parent, SWT.TOGGLE); |
|
536 styleButtonFont = new Font(parent.getDisplay(), |
|
537 new FontData(defFontData.getName(), defFontData.getHeight(), SWT.ITALIC)); |
|
538 styleButton.setFont(styleButtonFont); |
|
539 styleButton.setText(this.ITALIC); |
|
540 styleButton.addSelectionListener( |
|
541 new SelectionListener() |
|
542 { |
|
543 public void widgetDefaultSelected(SelectionEvent ev) |
|
544 { |
|
545 } |
|
546 public void widgetSelected(SelectionEvent ev) |
|
547 { |
|
548 fontStyleChanged(); |
|
549 } |
|
550 }); |
|
551 |
|
552 // Defaults |
|
553 if ((resFontData.getStyle() & SWT.ITALIC) != 0) |
|
554 { |
|
555 styleButton.setSelection(true); |
|
556 } |
|
557 |
|
558 // Layout |
|
559 styleButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); |
|
560 } |
|
561 |
|
562 private void createWeightControl(final Composite parent) { |
|
563 // Control |
|
564 weightButton = new Button(parent, SWT.TOGGLE); |
|
565 weightButtonFont = new Font(parent.getDisplay(), |
|
566 new FontData(defFontData.getName(), defFontData.getHeight(), SWT.BOLD)); |
|
567 weightButton.setFont(weightButtonFont); |
|
568 weightButton.setText(this.BOLD); |
|
569 weightButton.addSelectionListener( |
|
570 new SelectionListener() |
|
571 { |
|
572 public void widgetDefaultSelected(SelectionEvent ev) |
|
573 { |
|
574 } |
|
575 public void widgetSelected(SelectionEvent ev) |
|
576 { |
|
577 fontWeightChanged(); |
|
578 } |
|
579 }); |
|
580 |
|
581 // Defaults |
|
582 if ((resFontData.getStyle() & SWT.BOLD) != 0) |
|
583 { |
|
584 weightButton.setSelection(true); |
|
585 } |
|
586 |
|
587 // Layout |
|
588 weightButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); |
|
589 } |
|
590 |
|
591 private void createPreviewControl(final Shell parent) { |
|
592 previewLabel = new Label(parent, SWT.CENTER | SWT.BORDER); |
|
593 previewLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
|
594 updatePreviewText(); |
|
595 updatePreviewFont(); |
|
596 updatePreviewColor(); |
|
597 } |
|
598 |
|
599 private void destroyControls() |
|
600 { |
|
601 shell.getDisplay().removeListener(SWT.Settings, settingsListener); |
|
602 shell.getDisplay().removeListener(SWT.KeyDown, skListener); |
|
603 |
|
604 shell.dispose(); |
|
605 shell = null; |
|
606 |
|
607 faceCombo = null; |
|
608 heightCombo = null; |
|
609 previewLabel = null; |
|
610 weightButton = null; |
|
611 styleButton = null; |
|
612 colorButton = null; |
|
613 row4 = null; |
|
614 lsk = null; |
|
615 rsk = null; |
|
616 |
|
617 if (previewFont != null) |
|
618 { |
|
619 previewFont.dispose(); |
|
620 previewFont = null; |
|
621 } |
|
622 if (previewColor != null) |
|
623 { |
|
624 previewColor.dispose(); |
|
625 previewColor = null; |
|
626 } |
|
627 if (colorButtonImg != null) |
|
628 { |
|
629 colorButtonImg.dispose(); |
|
630 colorButtonImg = null; |
|
631 } |
|
632 if (weightButtonFont != null) |
|
633 { |
|
634 weightButtonFont.dispose(); |
|
635 weightButtonFont = null; |
|
636 } |
|
637 if (styleButtonFont != null) |
|
638 { |
|
639 styleButtonFont.dispose(); |
|
640 styleButtonFont = null; |
|
641 } |
|
642 } |
|
643 |
|
644 private void fontColorChanged(RGB newRGB) |
|
645 { |
|
646 if (newRGB == null) |
|
647 { |
|
648 return; |
|
649 } |
|
650 resRgb = newRGB; |
|
651 resRgbChanged = true; |
|
652 updatePreviewColor(); |
|
653 } |
|
654 |
|
655 private void fontFaceChanged() |
|
656 { |
|
657 int index = faceCombo.getSelectionIndex(); |
|
658 if (index >= 0) |
|
659 { |
|
660 String fontFace = faceCombo.getItem(index); |
|
661 resFontData.setName(fontFace); |
|
662 updatePreviewText(); |
|
663 updatePreviewFont(); |
|
664 } |
|
665 } |
|
666 |
|
667 private void fontHeightChanged() |
|
668 { |
|
669 int index = heightCombo.getSelectionIndex(); |
|
670 if (index >= 0 && index < HEIGHTS.length) |
|
671 { |
|
672 resFontData.setHeight(HEIGHTS[index]); |
|
673 updatePreviewFont(); |
|
674 } |
|
675 } |
|
676 |
|
677 private void fontStyleChanged() |
|
678 { |
|
679 if (styleButton.getSelection()) |
|
680 { |
|
681 resFontData.setStyle(resFontData.getStyle() | SWT.ITALIC); |
|
682 } |
|
683 else |
|
684 { |
|
685 resFontData.setStyle(resFontData.getStyle() & (~SWT.ITALIC)); |
|
686 } |
|
687 updatePreviewFont(); |
|
688 } |
|
689 |
|
690 private void fontWeightChanged() |
|
691 { |
|
692 if (weightButton.getSelection()) |
|
693 { |
|
694 resFontData.setStyle(resFontData.getStyle() | SWT.BOLD); |
|
695 } |
|
696 else |
|
697 { |
|
698 resFontData.setStyle(resFontData.getStyle() & (~SWT.BOLD)); |
|
699 } |
|
700 updatePreviewFont(); |
|
701 } |
|
702 |
|
703 private void updatePreviewColor() |
|
704 { |
|
705 // Create new color based on result rgb |
|
706 Color color = new Color(shell.getDisplay(), resRgb); |
|
707 |
|
708 // Set new color to preview label |
|
709 if (previewLabel != null) |
|
710 { |
|
711 previewLabel.setForeground(color); |
|
712 } |
|
713 |
|
714 // Set new color to color button image |
|
715 Image img = null; |
|
716 int h = weightButton.getSize().y / 2; |
|
717 if (h <= 0) |
|
718 h = weightButton.computeSize(-1, -1).y / 2; |
|
719 img = new Image(shell.getDisplay(), h, h); |
|
720 GC gc = new GC(img); |
|
721 gc.setBackground(color); |
|
722 gc.fillRectangle(0, 0, h, h); |
|
723 gc.dispose(); |
|
724 colorButton.setImage(img); |
|
725 |
|
726 // Replace old preview color |
|
727 if (previewColor != null) |
|
728 { |
|
729 previewColor.dispose(); |
|
730 } |
|
731 previewColor = color; |
|
732 |
|
733 // Replace old color button image |
|
734 if (colorButtonImg != null) |
|
735 { |
|
736 colorButtonImg.dispose(); |
|
737 } |
|
738 colorButtonImg = img; |
|
739 } |
|
740 |
|
741 private void updatePreviewFont() |
|
742 { |
|
743 Font font = new Font(shell.getDisplay(), resFontData); |
|
744 if (previewLabel != null) |
|
745 { |
|
746 previewLabel.setFont(font); |
|
747 } |
|
748 if (previewFont != null) |
|
749 { |
|
750 previewFont.dispose(); |
|
751 } |
|
752 previewFont = font; |
|
753 } |
|
754 |
|
755 private void updatePreviewText() |
|
756 { |
|
757 if (previewLabel != null) |
|
758 { |
|
759 previewLabel.setText(resFontData.getName()); |
|
760 } |
|
761 } |
|
762 |
|
763 } |