~om26er/ubuntu/oneiric/nux/sru-819721

« back to all changes in this revision

Viewing changes to Nux/Button.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-11-18 19:17:32 UTC
  • Revision ID: james.westby@ubuntu.com-20101118191732-rn35790vekj6o4my
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
12
 * License for more details.
 
13
 *
 
14
 * You should have received a copy of both the GNU Lesser General Public
 
15
 * License version 3 along with this program.  If not, see
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "Nux.h"
 
24
#include "Button.h"
 
25
#include "HLayout.h"
 
26
 
 
27
namespace nux
 
28
{
 
29
 
 
30
  Button::Button (const TCHAR *Caption, NUX_FILE_LINE_DECL)
 
31
    :   AbstractButton (Caption, NUX_FILE_LINE_PARAM)
 
32
  {
 
33
    m_hlayout   = 0;
 
34
    m_Checkable = false;
 
35
    m_State     = false;
 
36
 
 
37
    InitializeLayout();
 
38
    InitializeWidgets();
 
39
    SetCheckable (m_Checkable, m_State, false);
 
40
    SetCaption (Caption);
 
41
  }
 
42
 
 
43
  Button::~Button()
 
44
  {
 
45
    DestroyLayout();
 
46
  }
 
47
 
 
48
  void Button::InitializeWidgets()
 
49
  {
 
50
    // Set Signals
 
51
    OnMouseClick.connect (sigc::mem_fun (this, &Button::RecvClick) );
 
52
    OnMouseDown.connect (sigc::mem_fun (this, &Button::RecvMouseDown) );
 
53
    OnMouseDoubleClick.connect (sigc::mem_fun (this, &Button::RecvMouseDown) );
 
54
    OnMouseUp.connect (sigc::mem_fun (this, &Button::RecvMouseUp) );
 
55
    OnMouseMove.connect (sigc::mem_fun (this, &Button::RecvMouseMove) );
 
56
    OnMouseEnter.connect (sigc::mem_fun (this, &Button::RecvMouseEnter) );
 
57
    OnMouseLeave.connect (sigc::mem_fun (this, &Button::RecvMouseLeave) );
 
58
 
 
59
    // Set Geometry
 
60
    SetMinimumSize (DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
 
61
 
 
62
    m_hlayout->SetVerticalExternalMargin (0);
 
63
    m_hlayout->SetHorizontalExternalMargin (0);
 
64
    SetCompositionLayout (m_hlayout);
 
65
    SetTextColor (Color::Black);
 
66
  }
 
67
 
 
68
  void Button::InitializeLayout()
 
69
  {
 
70
    m_hlayout = new HLayout();
 
71
  }
 
72
 
 
73
  void Button::DestroyLayout()
 
74
  {
 
75
  }
 
76
 
 
77
  long Button::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
 
78
  {
 
79
    long ret = TraverseInfo;
 
80
    ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo);
 
81
    return ret;
 
82
  }
 
83
 
 
84
  void Button::Draw (GraphicsEngine &GfxContext, bool force_draw)
 
85
  {
 
86
    Geometry base = GetGeometry();
 
87
 
 
88
    InteractState is;
 
89
    is.is_on = m_State;
 
90
    is.is_focus = HasMouseFocus();
 
91
 
 
92
    is.is_prelight = IsMouseInside();
 
93
 
 
94
    if (m_Checkable)
 
95
    {
 
96
      if (m_State)
 
97
      {
 
98
        GetPainter().PushDrawSliceScaledTextureLayer (GfxContext, base, eBUTTON_FOCUS, Color::White, eAllCorners);
 
99
        GetPainter().PopBackground();
 
100
      }
 
101
      else
 
102
      {
 
103
        GetPainter().PushDrawSliceScaledTextureLayer (GfxContext, base, eBUTTON_NORMAL, Color::White, eAllCorners);
 
104
        GetPainter().PopBackground();
 
105
      }
 
106
    }
 
107
    else
 
108
    {
 
109
      if (is.is_focus)
 
110
      {
 
111
        GetPainter().PushDrawSliceScaledTextureLayer (GfxContext, base, eBUTTON_FOCUS, Color::White, eAllCorners);
 
112
        GetPainter().PopBackground();
 
113
      }
 
114
      else if (is.is_prelight)
 
115
      {
 
116
        GetPainter().PushDrawSliceScaledTextureLayer (GfxContext, base, eBUTTON_PRELIGHT, Color::White, eAllCorners);
 
117
        GetPainter().PopBackground();
 
118
      }
 
119
      else
 
120
      {
 
121
        GetPainter().PushDrawSliceScaledTextureLayer (GfxContext, base, eBUTTON_NORMAL, Color::White, eAllCorners);
 
122
        GetPainter().PopBackground();
 
123
      }
 
124
    }
 
125
 
 
126
    GetPainter().PaintTextLineStatic (GfxContext, GetFont(), base, GetBaseString().GetTCharPtr(), GetTextColor(), true, eAlignTextCenter);
 
127
  }
 
128
 
 
129
  void Button::DrawContent (GraphicsEngine &GfxContext, bool force_draw)
 
130
  {
 
131
 
 
132
  }
 
133
 
 
134
  void Button::PostDraw (GraphicsEngine &GfxContext, bool force_draw)
 
135
  {
 
136
 
 
137
  }
 
138
 
 
139
  void Button::SetCaption (const TCHAR *Caption)
 
140
  {
 
141
    if (Caption == 0 || (StringLength (Caption) == 0) )
 
142
    {
 
143
      SetBaseString (TEXT ("") );
 
144
    }
 
145
    else
 
146
      SetBaseString (Caption);
 
147
  }
 
148
 
 
149
  const NString &Button::GetCaption() const
 
150
  {
 
151
    return GetBaseString();
 
152
  }
 
153
 
 
154
  void Button::SetState (bool b)
 
155
  {
 
156
    m_State = b;
 
157
    NeedRedraw();
 
158
  }
 
159
 
 
160
  void Button::SetState (bool State, bool EmitSignal)
 
161
  {
 
162
    m_State = State;
 
163
 
 
164
    if (EmitSignal)
 
165
    {
 
166
      sigClick.emit();
 
167
 
 
168
      if (m_Checkable)
 
169
      {
 
170
        sigToggled.emit();
 
171
        sigStateChanged.emit (m_State);
 
172
      }
 
173
    }
 
174
 
 
175
    NeedRedraw();
 
176
  }
 
177
 
 
178
  bool Button::GetState() const
 
179
  {
 
180
    return m_State;
 
181
  }
 
182
 
 
183
  void Button::SetCheckable (bool b)
 
184
  {
 
185
    m_Checkable = b;
 
186
    NeedRedraw();
 
187
  }
 
188
 
 
189
  void Button::SetCheckable (bool b, bool CurrentState, bool EmitSignal)
 
190
  {
 
191
    m_Checkable = b;
 
192
    m_State = CurrentState;
 
193
 
 
194
    if (EmitSignal)
 
195
    {
 
196
      sigClick.emit();
 
197
    }
 
198
 
 
199
    NeedRedraw();
 
200
  }
 
201
 
 
202
  void Button::RecvClick (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
203
  {
 
204
    m_State = !m_State;
 
205
    sigClick.emit();
 
206
    NeedRedraw();
 
207
  }
 
208
 
 
209
  void Button::RecvMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
210
  {
 
211
    NeedRedraw();
 
212
  }
 
213
 
 
214
  void Button::RecvMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
215
  {
 
216
    NeedRedraw();
 
217
  }
 
218
 
 
219
  void Button::RecvMouseMove (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
 
220
  {
 
221
 
 
222
  }
 
223
 
 
224
  void Button::RecvMouseEnter (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
225
  {
 
226
    NeedRedraw();
 
227
  }
 
228
 
 
229
  void Button::RecvMouseLeave (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
230
  {
 
231
    NeedRedraw();
 
232
  }
 
233
 
 
234
 
 
235
 
 
236
 
 
237
}