1 /*****************************************************/
2 /* This java file is a part of the */
4 /* - Plouf's Java IRC Client - */
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
8 /* All contacts : theplouf@yahoo.com */
10 /* PJIRC is free software; you can redistribute */
11 /* it and/or modify it under the terms of the GNU */
12 /* General Public License as published by the */
13 /* Free Software Foundation; version 2 or later of */
16 /* PJIRC is distributed in the hope that it will */
17 /* be useful, but WITHOUT ANY WARRANTY; without */
18 /* even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
20 /* General Public License for more details. */
22 /* You should have received a copy of the GNU */
23 /* General Public License along with PJIRC; if */
24 /* not, write to the Free Software Foundation, */
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
26 /* MA 02111-1307 USA */
28 /*****************************************************/
32 import irc.StyleContext;
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Graphics;
37 import java.awt.Image;
38 import java.awt.Panel;
39 import java.awt.event.InputEvent;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseListener;
46 public class AWTStyleSelector extends Panel implements MouseListener {
47 private PixxConfiguration _pixxConfiguration;
48 private StyleContext _ct;
50 private int _backColor;
51 private boolean _bold;
52 private boolean _underline;
55 * Create a new AWTStyleSelector.
58 * global irc configuration.
60 public AWTStyleSelector(PixxConfiguration config) {
62 _pixxConfiguration = config;
67 addMouseListener(this);
68 _ct = _pixxConfiguration.getIRCConfiguration().getDefaultStyleContext();
72 * Release this object.
74 public void release() {
75 removeMouseListener(this);
76 _pixxConfiguration = null;
80 * Set the style context.
85 public void setStyleContext(StyleContext ct) {
90 public void paint(Graphics g) {
94 private void drawSelect(Graphics g, int i, int j) {
95 int x = (int) (i * getItemWidth());
96 int y = (int) (j * getItemHeight());
97 int w = (int) getItemWidth();
98 int h = (int) getItemHeight();
99 g.setColor(Color.black);
100 g.drawRect(x + 1, y + 1, w - 2, h - 2);
101 g.setColor(Color.white);
102 g.drawRect(x, y, w - 2, h - 2);
105 private void drawColor(Graphics g, int i, int j, Color c) {
106 int x = (int) (i * getItemWidth());
107 int y = (int) (j * getItemHeight());
108 int w = (int) getItemWidth();
109 int h = (int) getItemHeight();
111 g.fillRect(x, y, w, h);
114 private void drawBold(Graphics g, int i, int j) {
115 int x = (int) (i * getItemWidth());
116 int y = (int) (j * getItemHeight());
117 int w = (int) getItemWidth();
118 int h = (int) getItemHeight();
119 int tw = g.getFontMetrics().stringWidth("a");
120 g.drawString("a", x + (w - tw) / 2, y + h - (h - g.getFont().getSize()) / 2 - 2);
121 g.drawString("a", x + (w - tw) / 2 + 1, y + h - (h - g.getFont().getSize()) / 2 - 2);
124 private void drawUnderline(Graphics g, int i, int j) {
125 int x = (int) (i * getItemWidth());
126 int y = (int) (j * getItemHeight());
127 int w = (int) getItemWidth();
128 int h = (int) getItemHeight();
129 int tw = g.getFontMetrics().stringWidth("a");
130 g.drawString("a", x + (w - tw) / 2, y + h - (h - g.getFont().getSize()) / 2 - 2);
132 g.drawLine(x + w / 2 - tw / 2, y + h - 1, x + w / 2 + tw / 2, y + h - 1);
136 public void update(Graphics g) {
137 int w = getSize().width;
138 int h = getSize().height;
143 buffer = createImage(w, h);
144 gra = buffer.getGraphics();
145 } catch (Throwable e) {
149 Color[] cols = _pixxConfiguration.getIRCConfiguration().getStyleColors(_ct);
151 for (int y = 0; y < 2; y++) {
152 for (int x = 1; x < 9; x++) {
153 drawColor(gra, x, y, cols[c]);
155 drawSelect(gra, x, y);
160 gra.setColor(cols[_backColor]);
161 gra.fillRect(0, 0, (int) getItemWidth(), h);
163 gra.setColor(cols[_color]);
165 drawUnderline(gra, 0, 1);
168 drawSelect(gra, 0, 0);
170 drawSelect(gra, 0, 1);
172 g.drawImage(buffer, 0, 0, this);
176 public Dimension getPreferredSize() {
177 return new Dimension(9 * 10, 10);
180 private double getItemWidth() {
181 return getSize().width / 9.0;
184 private double getItemHeight() {
185 return getSize().height / 2.0;
189 * Set the front color.
192 * the new front color.
194 public void setFrontColor(int c) {
200 * Set the back color.
203 * the new back color.
205 public void setBackColor(int c) {
211 * Set the new bold status.
214 * the new bold status.
216 public void setBold(boolean bold) {
222 * Set the new underline status.
225 * the new underline statu.
227 public void setUnderline(boolean underline) {
228 _underline = underline;
233 * Get prefix to be used for text styling.
235 * @return text style prefix.
237 public String getPrefix() {
239 if (_color != 1 || _backColor != 0) {
247 if (_backColor != 0) {
248 c2 = "" + _backColor;
253 if (c2.length() > 0 && c1.length() == 0)
255 pre = pre + (char) 3 + c1 + c2;
265 public void mouseClicked(MouseEvent e) {
269 public void mouseEntered(MouseEvent e) {
273 public void mouseExited(MouseEvent e) {
277 public void mousePressed(MouseEvent e) {
278 int x = (int) (e.getX() / getItemWidth());
279 int y = (int) (e.getY() / getItemHeight());
284 _underline = !_underline;
287 if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
290 _backColor = x + y * 8;
296 public void mouseReleased(MouseEvent e) {