~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to Nux/HScrollBar.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 "Layout.h"
 
25
#include "HLayout.h"
 
26
#include "VLayout.h"
 
27
#include "HScrollBar.h"
 
28
 
 
29
namespace nux
 
30
{
 
31
 
 
32
  const int HSCROLLBAR_WIDTH = 10;
 
33
  const int HSCROLLBAR_HEIGHT = 10;
 
34
 
 
35
  HScrollBar::HScrollBar (NUX_FILE_LINE_DECL)
 
36
    :   ScrollBar (NUX_FILE_LINE_PARAM)
 
37
  {
 
38
    m_contentWidth      = 0;
 
39
    m_contentHeight     = 0;
 
40
    m_containerWidth    = 0;
 
41
    m_containerHeight   = 0;
 
42
    m_TrackWidth        = 0;
 
43
    m_TrackHeight       = 0;
 
44
    m_SlideBarOffsetX   = 0;
 
45
    m_SlideBarOffsetY   = 0;
 
46
    m_contentOffsetX    = 0;
 
47
    m_contentOffsetY    = 0;
 
48
    b_MouseUpTimer      = false;
 
49
    b_MouseDownTimer    = false;
 
50
    m_color_factor      = 1.0f;
 
51
    m_LeftTimerHandler  = 0;
 
52
    m_RightTimerHandler = 0;
 
53
 
 
54
    hlayout = new HLayout (TEXT (""), NUX_TRACKER_LOCATION);
 
55
    m_LeftThumb = new CoreArea (NUX_TRACKER_LOCATION);
 
56
    m_Track = new CoreArea (NUX_TRACKER_LOCATION);
 
57
    m_RightThumb = new CoreArea (NUX_TRACKER_LOCATION);
 
58
    m_SlideBar = new CoreArea (NUX_TRACKER_LOCATION);
 
59
 
 
60
    // Set Original State
 
61
    SetMinimumSize (BASEOBJECT_MINWIDTH, HSCROLLBAR_HEIGHT);
 
62
    SetMaximumSize (BASEOBJECT_MAXWIDTH, HSCROLLBAR_HEIGHT);
 
63
    // Set Signals
 
64
    m_RightThumb->OnMouseDown.connect ( sigc::mem_fun (this, &HScrollBar::RecvStartScrollRight) );
 
65
    m_RightThumb->OnMouseUp.connect ( sigc::mem_fun (this, &HScrollBar::RecvEndScrollRight) );
 
66
    m_LeftThumb->OnMouseDown.connect ( sigc::mem_fun (this, &HScrollBar::RecvStartScrollLeft) );
 
67
    m_LeftThumb->OnMouseUp.connect ( sigc::mem_fun (this, &HScrollBar::RecvEndScrollLeft) );
 
68
 
 
69
    m_SlideBar->OnMouseDown.connect ( sigc::mem_fun (this, &HScrollBar::OnSliderMouseDown) );
 
70
    m_SlideBar->OnMouseUp.connect ( sigc::mem_fun (this, &HScrollBar::OnSliderMouseUp) );
 
71
    m_SlideBar->OnMouseDrag.connect ( sigc::mem_fun (this, &HScrollBar::OnSliderMouseDrag) );
 
72
 
 
73
    m_Track->OnMouseDown.connect ( sigc::mem_fun (this, &HScrollBar::RecvTrackMouseDown) );
 
74
    m_Track->OnMouseUp.connect ( sigc::mem_fun (this, &HScrollBar::RecvTrackMouseUp) );
 
75
    m_Track->OnMouseDrag.connect ( sigc::mem_fun (this, &HScrollBar::RecvTrackMouseDrag) );
 
76
 
 
77
    // Set Geometry
 
78
    m_RightThumb->SetMinimumSize (HSCROLLBAR_WIDTH, HSCROLLBAR_HEIGHT);
 
79
    m_RightThumb->SetMaximumSize (HSCROLLBAR_WIDTH, HSCROLLBAR_HEIGHT);
 
80
    m_RightThumb->SetGeometry (Geometry (0, 0, HSCROLLBAR_WIDTH, HSCROLLBAR_HEIGHT) );
 
81
    m_LeftThumb->SetMinimumSize (HSCROLLBAR_WIDTH, HSCROLLBAR_HEIGHT);
 
82
    m_LeftThumb->SetMaximumSize (HSCROLLBAR_WIDTH, HSCROLLBAR_HEIGHT);
 
83
    m_LeftThumb->SetGeometry (Geometry (0, 0, HSCROLLBAR_WIDTH, HSCROLLBAR_HEIGHT) );
 
84
 
 
85
 
 
86
    hlayout->AddView (m_LeftThumb, 0, eCenter, eFix);
 
87
    hlayout->AddView (m_Track, 1, eCenter, eFull);
 
88
    hlayout->AddView (m_RightThumb, 0, eCenter, eFix);
 
89
 
 
90
    callback = new TimerFunctor;
 
91
    callback->OnTimerExpired.connect (sigc::mem_fun (this, &HScrollBar::HScrollBarHandler) );
 
92
    left_callback = new TimerFunctor;
 
93
    left_callback->OnTimerExpired.connect (sigc::mem_fun (this, &HScrollBar::ScrollLeft) );
 
94
    right_callback = new TimerFunctor;
 
95
    right_callback->OnTimerExpired.connect (sigc::mem_fun (this, &HScrollBar::ScrollRight) );
 
96
    trackleft_callback = new TimerFunctor;
 
97
    trackleft_callback->OnTimerExpired.connect (sigc::mem_fun (this, &HScrollBar::TrackLeft) );
 
98
    trackright_callback = new TimerFunctor;
 
99
    trackright_callback->OnTimerExpired.connect (sigc::mem_fun (this, &HScrollBar::TrackRight) );
 
100
 
 
101
    SetCompositionLayout (hlayout);
 
102
  }
 
103
 
 
104
  HScrollBar::~HScrollBar()
 
105
  {
 
106
    delete callback;
 
107
    delete left_callback;
 
108
    delete trackleft_callback;
 
109
    delete right_callback;
 
110
  }
 
111
 
 
112
  void HScrollBar::HScrollBarHandler (void *v)
 
113
  {
 
114
    HScrollBar *scrollbar = static_cast<HScrollBar *> (v);
 
115
 
 
116
    if (scrollbar->b_MouseUpTimer && scrollbar->m_color_factor < 1)
 
117
    {
 
118
      scrollbar->m_color_factor += 0.1f;
 
119
 
 
120
      if (scrollbar->m_color_factor >= 1)
 
121
      {
 
122
        scrollbar->m_color_factor = 1;
 
123
        scrollbar->b_MouseUpTimer = false;
 
124
      }
 
125
      else
 
126
      {
 
127
        scrollbar->NeedRedraw();
 
128
        GetTimer().AddTimerHandler (10, callback, scrollbar);
 
129
      }
 
130
    }
 
131
 
 
132
    if (scrollbar->b_MouseDownTimer && scrollbar->m_color_factor > 0)
 
133
    {
 
134
      scrollbar->m_color_factor -= 0.09f;
 
135
 
 
136
      if (scrollbar->m_color_factor <= 0)
 
137
      {
 
138
        scrollbar->m_color_factor = 0;
 
139
        scrollbar->b_MouseUpTimer = false;
 
140
      }
 
141
      else
 
142
      {
 
143
        scrollbar->NeedRedraw();
 
144
        GetTimer().AddTimerHandler (10, callback, scrollbar);
 
145
      }
 
146
    }
 
147
  }
 
148
 
 
149
  void HScrollBar::ScrollRight (void *v)
 
150
  {
 
151
    OnScrollRight.emit (m_ScrollUnit, 1);
 
152
 
 
153
    if (AtMaximum() )
 
154
      RecvEndScrollRight (0, 0, 0, 0);
 
155
    else
 
156
      m_RightTimerHandler = GetTimer().AddTimerHandler (10, right_callback, this);
 
157
 
 
158
    NeedRedraw();
 
159
  }
 
160
 
 
161
  void HScrollBar::ScrollLeft (void *v)
 
162
  {
 
163
    OnScrollLeft.emit (m_ScrollUnit, 1);
 
164
 
 
165
    if (AtMaximum() )
 
166
      RecvEndScrollLeft (0, 0, 0, 0);
 
167
    else
 
168
      m_LeftTimerHandler = GetTimer().AddTimerHandler (10, left_callback, this);
 
169
 
 
170
    NeedRedraw();
 
171
  }
 
172
 
 
173
  void HScrollBar::TrackLeft (void *v)
 
174
  {
 
175
    if (m_TrackMouseCoord.x < m_SlideBar->GetBaseX() - m_Track->GetBaseX() )
 
176
    {
 
177
      OnScrollLeft.emit (m_containerWidth, 1);
 
178
      m_TrackLeftTimerHandler  = GetTimer().AddTimerHandler (10, trackleft_callback, this);
 
179
      NeedRedraw();
 
180
    }
 
181
  }
 
182
 
 
183
  void HScrollBar::TrackRight (void *v)
 
184
  {
 
185
    if (m_TrackMouseCoord.x > m_SlideBar->GetBaseX() + m_SlideBar->GetBaseWidth() - m_Track->GetBaseX() )
 
186
    {
 
187
      OnScrollRight.emit (m_containerWidth, 1);
 
188
      m_TrackRightTimerHandler  = GetTimer().AddTimerHandler (10, trackright_callback, this);
 
189
      NeedRedraw();
 
190
    }
 
191
  }
 
192
 
 
193
  void HScrollBar::RecvStartScrollLeft (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
194
  {
 
195
    if (!AtMinimum() )
 
196
      ScrollLeft (this);
 
197
  }
 
198
 
 
199
  void HScrollBar::RecvStartScrollRight (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
200
  {
 
201
    if (!AtMaximum() )
 
202
      ScrollRight (this);
 
203
  }
 
204
 
 
205
  void HScrollBar::RecvEndScrollRight (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
206
  {
 
207
    if (m_RightTimerHandler.IsValid() )
 
208
      GetTimer().RemoveTimerHandler (m_RightTimerHandler);
 
209
 
 
210
    m_RightTimerHandler = 0;
 
211
  }
 
212
 
 
213
  void HScrollBar::RecvEndScrollLeft (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
214
  {
 
215
    if (m_LeftTimerHandler.IsValid() )
 
216
      GetTimer().RemoveTimerHandler (m_LeftTimerHandler);
 
217
 
 
218
    m_LeftTimerHandler = 0;
 
219
  }
 
220
 
 
221
  void HScrollBar::RecvTrackMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
222
  {
 
223
    m_TrackMouseCoord = Point (x, y);
 
224
 
 
225
    int X = m_SlideBar->GetBaseX() - m_Track->GetBaseX();
 
226
 
 
227
    if (x < X)
 
228
    {
 
229
      // move the slide bar up
 
230
      TrackLeft (this);
 
231
    }
 
232
    else
 
233
    {
 
234
      TrackRight (this);
 
235
      // move the slide bar down
 
236
    }
 
237
  }
 
238
 
 
239
  void HScrollBar::RecvTrackMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
240
  {
 
241
    if (m_TrackLeftTimerHandler.IsValid() )
 
242
      GetTimer().RemoveTimerHandler (m_TrackLeftTimerHandler);
 
243
 
 
244
    if (m_TrackRightTimerHandler.IsValid() )
 
245
      GetTimer().RemoveTimerHandler (m_TrackRightTimerHandler);
 
246
 
 
247
    m_TrackLeftTimerHandler = 0;
 
248
    m_TrackRightTimerHandler = 0;
 
249
  }
 
250
 
 
251
  void HScrollBar::RecvTrackMouseDrag (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
 
252
  {
 
253
 
 
254
  }
 
255
 
 
256
  long HScrollBar::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
 
257
  {
 
258
    long ret = TraverseInfo;
 
259
    ret = m_RightThumb->OnEvent (ievent, ret, ProcessEventInfo);
 
260
    ret = m_LeftThumb->OnEvent (ievent, ret, ProcessEventInfo);
 
261
    ret = m_SlideBar->OnEvent (ievent, ret, ProcessEventInfo);
 
262
    ret = m_Track->OnEvent (ievent, ret, ProcessEventInfo);
 
263
    ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo);
 
264
 
 
265
    return ret;
 
266
  }
 
267
 
 
268
  void HScrollBar::Draw (GraphicsEngine &GfxContext, bool force_draw)
 
269
  {
 
270
    Geometry base = GetGeometry();
 
271
    GetPainter().PaintBackground (GfxContext, base);
 
272
    base.OffsetPosition (HSCROLLBAR_WIDTH, 0);
 
273
    base.OffsetSize (-2 * HSCROLLBAR_WIDTH, 0);
 
274
    GetPainter().PaintShape (GfxContext, base,
 
275
                         Color (COLOR_SCROLLBAR_TRACK), eHSCROLLBAR, false);
 
276
 
 
277
    GetPainter().PaintShape (GfxContext, m_LeftThumb->GetGeometry(), Color (0xFFFFFFFF), eSCROLLBAR_TRIANGLE_LEFT);
 
278
    GetPainter().PaintShape (GfxContext, m_RightThumb->GetGeometry(), Color (0xFFFFFFFF), eSCROLLBAR_TRIANGLE_RIGHT);
 
279
 
 
280
    GetPainter().PaintShape (GfxContext, m_SlideBar->GetGeometry(),
 
281
                         Color (0.2156 * m_color_factor, 0.2156 * m_color_factor, 0.2156 * m_color_factor, 1.0f),
 
282
                         eHSCROLLBAR, true);
 
283
  };
 
284
 
 
285
  void HScrollBar::SetContainerSize (int x, int y, int w, int h)
 
286
  {
 
287
    // x and y are not needed
 
288
    m_containerWidth = w;
 
289
    m_containerHeight = h;
 
290
    ComputeScrolling();
 
291
  }
 
292
 
 
293
  void HScrollBar::SetContentSize (int x, int y, int w, int h)
 
294
  {
 
295
    // x and y are not needed
 
296
    m_contentWidth = w;
 
297
    m_contentHeight = h;
 
298
    ComputeScrolling();
 
299
  }
 
300
 
 
301
  void HScrollBar::SetContentOffset (float dx, float dy)
 
302
  {
 
303
    m_contentOffsetX = dx;
 
304
    m_contentOffsetY = dy;
 
305
    ComputeScrolling();
 
306
  }
 
307
 
 
308
  void HScrollBar::ComputeScrolling()
 
309
  {
 
310
    float percentage = 0;
 
311
 
 
312
    if (m_contentWidth == 0)
 
313
    {
 
314
      percentage = 100.0f;
 
315
    }
 
316
    else
 
317
    {
 
318
      percentage = 100.0f * (float) m_containerWidth / (float) m_contentWidth;
 
319
    }
 
320
 
 
321
    if (percentage > 100.0f)
 
322
    {
 
323
      percentage = 100.0f;
 
324
    }
 
325
 
 
326
    if (percentage < 0.0f)
 
327
    {
 
328
      percentage = 0.0f;
 
329
    }
 
330
 
 
331
    m_TrackWidth = m_Track->GetBaseWidth();
 
332
 
 
333
    int slider_height = m_LeftThumb->GetBaseHeight();
 
334
    int slider_width = m_TrackWidth * percentage / 100.0f;
 
335
 
 
336
    if (slider_width < 15)
 
337
    {
 
338
      slider_width = 15;
 
339
    }
 
340
 
 
341
    m_SlideBar->SetBaseWidth (slider_width);
 
342
    m_SlideBar->SetBaseHeight (slider_height);
 
343
    m_SlideBar->SetBaseY (m_LeftThumb->GetBaseY() );
 
344
 
 
345
 
 
346
    float pct;
 
347
 
 
348
    if (m_contentWidth - m_containerWidth > 0)
 
349
      pct = - (float) m_contentOffsetX / (float) (m_contentWidth - m_containerWidth);
 
350
    else
 
351
      pct = 0;
 
352
 
 
353
    int x = m_Track->GetBaseX() + pct * (m_TrackWidth - slider_width);
 
354
    m_SlideBar->SetBaseX (x);
 
355
  }
 
356
 
 
357
 
 
358
/////////////////
 
359
//  RECEIVERS  //
 
360
/////////////////
 
361
  void HScrollBar::SetValue (float value)
 
362
  {
 
363
    //m_ValueString.setCaption(value);
 
364
  }
 
365
 
 
366
  void HScrollBar::SetParameterName (const char *parameter_name)
 
367
  {
 
368
    //m_ParameterName.setCaption(parameter_name);
 
369
  }
 
370
 
 
371
////////////////
 
372
//  EMITTERS  //
 
373
////////////////
 
374
 
 
375
  void HScrollBar::OnSliderMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
376
  {
 
377
    m_SliderDragPositionX = x;
 
378
    m_SliderDragPositionY = y;
 
379
    //sigVScrollBarSliderMouseDown.emit();
 
380
    b_MouseDownTimer = true;
 
381
    b_MouseUpTimer = false;
 
382
    GetTimer().AddTimerHandler (10, callback, this);
 
383
  }
 
384
 
 
385
  void HScrollBar::OnSliderMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags)
 
386
  {
 
387
    b_MouseDownTimer = false;
 
388
    b_MouseUpTimer = true;
 
389
    GetTimer().AddTimerHandler (10, callback, this);
 
390
  }
 
391
 
 
392
  void HScrollBar::OnSliderMouseDrag (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
 
393
  {
 
394
    if (m_Track->GetBaseWidth() - m_SlideBar->GetBaseWidth() > 0)
 
395
    {
 
396
      stepX = (float) (m_contentWidth - m_containerWidth) / (float) (m_Track->GetBaseWidth() - m_SlideBar->GetBaseWidth() );
 
397
    }
 
398
    else
 
399
    {
 
400
      return;
 
401
    }
 
402
 
 
403
    if ( (dx > 0) && (x > m_SliderDragPositionX) )
 
404
    {
 
405
      OnScrollRight.emit (stepX, x - m_SliderDragPositionX);
 
406
    }
 
407
 
 
408
    if ( (dx < 0) && (x < m_SliderDragPositionX) )
 
409
    {
 
410
      OnScrollLeft.emit (stepX, m_SliderDragPositionX - x);
 
411
    }
 
412
  }
 
413
 
 
414
  bool HScrollBar::AtMaximum()
 
415
  {
 
416
    if (m_SlideBar->GetBaseX() + m_SlideBar->GetBaseWidth() == m_Track->GetBaseX() + m_Track->GetBaseWidth() )
 
417
      return TRUE;
 
418
 
 
419
    return FALSE;
 
420
  }
 
421
 
 
422
  bool HScrollBar::AtMinimum()
 
423
  {
 
424
    if (m_SlideBar->GetBaseX() == m_Track->GetBaseX() )
 
425
      return TRUE;
 
426
 
 
427
    return FALSE;
 
428
  }
 
429
 
 
430
  int HScrollBar::GetBaseWidth()
 
431
  {
 
432
    return Area::GetBaseWidth();
 
433
  }
 
434
 
 
435
  int HScrollBar::GetBaseHeight()
 
436
  {
 
437
    return HSCROLLBAR_HEIGHT;
 
438
  }
 
439
 
 
440
  long HScrollBar::PostLayoutManagement (long LayoutResult)
 
441
  {
 
442
    long ret = ScrollBar::PostLayoutManagement (LayoutResult);
 
443
    ComputeScrolling();
 
444
    return ret;
 
445
  }
 
446
 
 
447
}