summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/core/controls/CFlatButton.java
blob: 156cf5e660a442b0bf7bad8382516638c22a1026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.core.controls;

import org.eclipse.wb.draw2d.IColorConstants;
import org.eclipse.wb.internal.core.utils.ui.DrawUtils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * Class representing flat push button as it looks in Mac OSX.
 * 
 * It doesn't draw text, not need for now. ;-)
 * 
 * @author mitin_aa
 */
public final class CFlatButton extends Canvas {
  // colors
  private static final Color COLOR_FACE = DrawUtils.getShiftedColor(IColorConstants.button, 12);
  private static final Color COLOR_FACE_SELECTED = IColorConstants.buttonDarker;
  private static final Color COLOR_BORDER_GRADIENT1 = DrawUtils.getShiftedColor(
      IColorConstants.button,
      -12);
  private static final Color COLOR_BORDER_GRADIENT1_SELECTED = DrawUtils.getShiftedColor(
      IColorConstants.buttonDarker,
      64);
  private static final Color COLOR_BORDER_GRADIENT2 = DrawUtils.getShiftedColor(COLOR_FACE, -8);
  private static final Color COLOR_BORDER_GRADIENT2_SELECTED = DrawUtils.getShiftedColor(
      COLOR_FACE_SELECTED,
      -8);
  // fields
  private Image m_image;
  private boolean m_down;
  private boolean m_selected;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  public CFlatButton(Composite parent, int style) {
    super(parent, style);
    addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        boolean isSelected = m_down | m_selected;
        Color faceColor = isSelected ? COLOR_FACE_SELECTED : COLOR_FACE;
        Color borderGradientColor1 =
            isSelected ? COLOR_BORDER_GRADIENT1_SELECTED : COLOR_BORDER_GRADIENT1;
        Color borderGradientColor2 =
            isSelected ? COLOR_BORDER_GRADIENT2_SELECTED : COLOR_BORDER_GRADIENT2;
        GC gc = e.gc;
        Rectangle ca = getClientArea();
        // draw client area
        // dark border
        gc.setForeground(IColorConstants.buttonDarker);
        gc.drawRectangle(ca.x, ca.y, ca.width - 1, ca.height - 1);
        cropClientArea(ca);
        // gradient border
        gc.setForeground(borderGradientColor1);
        gc.setBackground(borderGradientColor2);
        gc.fillGradientRectangle(ca.x, ca.y, ca.width, ca.height, true);
        cropClientArea(ca);
        // fill background
        gc.setBackground(faceColor);
        gc.fillRectangle(ca);
        // draw face upper-half gradient 
        Rectangle ca1 = getClientArea();
        cropClientArea(ca1);
        gc.setForeground(faceColor);
        gc.setBackground(borderGradientColor1);
        gc.fillGradientRectangle(ca1.x, ca1.y, ca1.width, ca1.height / 4, true);
        // draw face down-half gradient 
        ca1.x += 1;
        ca1.width -= 2;
        gc.setForeground(borderGradientColor1);
        gc.setBackground(faceColor);
        gc.fillGradientRectangle(ca1.x, ca1.y + ca1.height / 4 - 1, ca1.width, ca1.height / 2, true);
        // draw image
        Image image = getImage();
        if (image != null) {
          Rectangle imageBounds = image.getBounds();
          // center it in client area
          int x = ca.x + (ca.width - imageBounds.width) / 2;
          int y = ca.y + (ca.height - imageBounds.height) / 2;
          gc.drawImage(image, x, y);
        }
      }
    });
    addListener(SWT.MouseDown, new Listener() {
      public void handleEvent(Event e) {
        m_down = true;
        redraw();
      }
    });
    addListener(SWT.MouseUp, new Listener() {
      public void handleEvent(Event e) {
        m_down = false;
        redraw();
        update();
        if (getClientArea().contains(e.x, e.y)) {
          fireSelectionEvent(e.time, e.stateMask);
        }
      }
    });
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Utils
  //
  ////////////////////////////////////////////////////////////////////////////
  private void fireSelectionEvent(int time, int stateMask) {
    Event event = new Event();
    event.time = time;
    event.stateMask = stateMask;
    notifyListeners(SWT.Selection, event);
  }

  private void cropClientArea(Rectangle ca) {
    ca.x += 1;
    ca.y += 1;
    ca.width -= 2;
    ca.height -= 2;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  public final Image getImage() {
    return m_image;
  }

  public void setImage(Image image) {
    m_image = image;
  }

  public void setSelected(boolean selected) {
    m_selected = selected;
  }
}