~ubuntu-branches/debian/squeeze/stella/squeeze

« back to all changes in this revision

Viewing changes to src/gui/CheckListWidget.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Mario Iseli
  • Date: 2006-04-08 18:38:25 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060408183825-vu1jk57rk929derx
* New Maintainer (Closes: #361345)
* New upstream release (Closes: #349725)
* Build-Depend now on libslang2-dev (Closes: #325577)
* Complete rebuild of debian/, upgraded to policy-standards
  3.6.2 and compat-level 5.
* Removed stellarc since stella only reads ~/.stellarc and even
  works without a first config.
* New debian/watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//============================================================================
 
2
//
 
3
//   SSSS    tt          lll  lll       
 
4
//  SS  SS   tt           ll   ll        
 
5
//  SS     tttttt  eeee   ll   ll   aaaa 
 
6
//   SSSS    tt   ee  ee  ll   ll      aa
 
7
//      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator"
 
8
//  SS  SS   tt   ee      ll   ll  aa  aa
 
9
//   SSSS     ttt  eeeee llll llll  aaaaa
 
10
//
 
11
// Copyright (c) 1995-2005 by Bradford W. Mott and the Stella team
 
12
//
 
13
// See the file "license" for information on usage and redistribution of
 
14
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
 
15
//
 
16
// $Id: CheckListWidget.cxx,v 1.10 2006/02/22 17:38:04 stephena Exp $
 
17
//
 
18
//   Based on code from ScummVM - Scumm Interpreter
 
19
//   Copyright (C) 2002-2004 The ScummVM project
 
20
//============================================================================
 
21
 
 
22
#include "CheckListWidget.hxx"
 
23
#include "Widget.hxx"
 
24
 
 
25
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
26
CheckListWidget::CheckListWidget(GuiObject* boss, const GUI::Font& font,
 
27
                                 int x, int y, int w, int h)
 
28
  : ListWidget(boss, font, x, y, w, h)
 
29
{
 
30
  int ypos = _y + 2;
 
31
 
 
32
  // rowheight is determined by largest item on a line,
 
33
  // possibly meaning that number of rows will change
 
34
  _fontHeight = MAX(_fontHeight, CheckboxWidget::boxSize());
 
35
  _rows = h / _fontHeight;
 
36
 
 
37
  // Create a CheckboxWidget for each row in the list
 
38
  CheckboxWidget* t;
 
39
  for(int i = 0; i < _rows; ++i)
 
40
  {
 
41
    t = new CheckboxWidget(boss, font, _x + 2, ypos, "", kCheckActionCmd);
 
42
    t->setTarget(this);
 
43
    t->setID(i);
 
44
    t->holdFocus(false);
 
45
    ypos += _fontHeight;
 
46
 
 
47
    _checkList.push_back(t);
 
48
  }
 
49
}
 
50
 
 
51
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
52
CheckListWidget::~CheckListWidget()
 
53
{
 
54
}
 
55
 
 
56
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
57
void CheckListWidget::setStyle(CheckStyle style)
 
58
{
 
59
  for(unsigned int i = 0; i < _checkList.size(); ++i)
 
60
  {
 
61
    if(style == kXFill)
 
62
    {
 
63
      _checkList[i]->drawBox(true);
 
64
      _checkList[i]->setFill(false);
 
65
      _checkList[i]->setColor(kTextColor);
 
66
    }
 
67
    else if(style == kSolidFill)
 
68
    {
 
69
      _checkList[i]->drawBox(false);
 
70
      _checkList[i]->setFill(true);
 
71
      _checkList[i]->setColor(kTextColorEm);
 
72
    }
 
73
  }
 
74
}
 
75
 
 
76
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
77
void CheckListWidget::setList(const StringList& list, const BoolArray& state)
 
78
{
 
79
  _list = list;
 
80
  _stateList = state;
 
81
 
 
82
  assert(_list.size() == _stateList.size());
 
83
 
 
84
  // Enable all checkboxes
 
85
  for(int i = 0; i < _rows; ++i)
 
86
    _checkList[i]->setFlags(WIDGET_ENABLED);
 
87
 
 
88
  // Then turn off any extras
 
89
  if((int)_stateList.size() < _rows)
 
90
    for(int i = _stateList.size(); i < _rows; ++i)
 
91
      _checkList[i]->clearFlags(WIDGET_ENABLED);
 
92
 
 
93
  ListWidget::recalc();
 
94
}
 
95
 
 
96
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
97
void CheckListWidget::setLine(int line, const string& str, const bool& state)
 
98
{
 
99
  if(line >= (int)_list.size())
 
100
    return;
 
101
 
 
102
  _list[line]      = str;
 
103
  _stateList[line] = state;
 
104
}
 
105
 
 
106
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
107
void CheckListWidget::drawWidget(bool hilite)
 
108
{
 
109
//cerr << "CheckListWidget::drawWidget\n";
 
110
  FrameBuffer& fb = _boss->instance()->frameBuffer();
 
111
  int i, pos, len = _list.size();
 
112
  string buffer;
 
113
  int deltax;
 
114
 
 
115
  // Draw a thin frame around the list and to separate columns
 
116
  fb.hLine(_x, _y, _x + _w - 1, kColor);
 
117
  fb.hLine(_x, _y + _h - 1, _x + _w - 1, kShadowColor);
 
118
  fb.vLine(_x, _y, _y + _h - 1, kColor);
 
119
 
 
120
  fb.vLine(_x + CheckboxWidget::boxSize() + 5, _y, _y + _h - 1, kColor);
 
121
 
 
122
  // Draw the list items
 
123
  for (i = 0, pos = _currentPos; i < _rows && pos < len; i++, pos++)
 
124
  {
 
125
    // Draw checkboxes for correct lines (takes scrolling into account)
 
126
    _checkList[i]->setState(_stateList[pos]);
 
127
    _checkList[i]->setDirty();
 
128
    _checkList[i]->draw();
 
129
 
 
130
    const int y = _y + 2 + _fontHeight * i + 2;
 
131
 
 
132
    GUI::Rect r(getEditRect());
 
133
 
 
134
    // Draw the selected item inverted, on a highlighted background.
 
135
    if (_selectedItem == pos)
 
136
    {
 
137
      if (_hasFocus && !_editMode)
 
138
        fb.fillRect(_x + r.left - 3, _y + 1 + _fontHeight * i,
 
139
                    _w - r.left, _fontHeight,
 
140
                    kTextColorHi);
 
141
      else
 
142
        fb.frameRect(_x + r.left - 3, _y + 1 + _fontHeight * i,
 
143
                     _w - r.left, _fontHeight,
 
144
                     kTextColorHi);
 
145
    }
 
146
 
 
147
    if (_selectedItem == pos && _editMode)
 
148
    {
 
149
      buffer = _editString;
 
150
      adjustOffset();
 
151
      deltax = -_editScrollOffset;
 
152
 
 
153
      fb.drawString(_font, buffer, _x + r.left, y, r.width(), kTextColor,
 
154
                    kTextAlignLeft, deltax, false);
 
155
    }
 
156
    else
 
157
    {
 
158
      buffer = _list[pos];
 
159
      deltax = 0;
 
160
      fb.drawString(_font, buffer, _x + r.left, y, r.width(), kTextColor);
 
161
    }
 
162
  }
 
163
 
 
164
  // Only draw the caret while editing, and if it's in the current viewport
 
165
  if(_editMode && (_selectedItem >= _scrollBar->_currentPos) &&
 
166
    (_selectedItem < _scrollBar->_currentPos + _rows))
 
167
    drawCaret();
 
168
}
 
169
 
 
170
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
171
GUI::Rect CheckListWidget::getEditRect() const
 
172
{
 
173
  GUI::Rect r(2, 1, _w, _fontHeight);
 
174
  const int yoffset = (_selectedItem - _currentPos) * _fontHeight,
 
175
            xoffset = CheckboxWidget::boxSize() + 10;
 
176
  r.top    += yoffset;
 
177
  r.bottom += yoffset;
 
178
  r.left  += xoffset;
 
179
  r.right -= xoffset - 15;
 
180
        
 
181
  return r;
 
182
}
 
183
 
 
184
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
185
bool CheckListWidget::getState(int line)
 
186
{
 
187
  if(line >= 0 && line < (int)_stateList.size())
 
188
    return _stateList[line];
 
189
  else
 
190
    return false;
 
191
}
 
192
 
 
193
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
194
void CheckListWidget::handleCommand(CommandSender* sender, int cmd,
 
195
                                    int data, int id)
 
196
{
 
197
  switch(cmd)
 
198
  {
 
199
    case kCheckActionCmd:
 
200
    {
 
201
      // Figure out which line has been checked
 
202
      int line = _currentPos + id;
 
203
      _stateList[line] = bool(data);
 
204
 
 
205
      // Let the boss know about it
 
206
      sendCommand(kListItemChecked, line, _id);
 
207
      break;
 
208
    }
 
209
 
 
210
    default:
 
211
      ListWidget::handleCommand(sender, cmd, data, id);
 
212
  }
 
213
}