File: widget_list.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (426 lines) | stat: -rw-r--r-- 10,219 bytes parent folder | download | duplicates (4)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Widget list : store all widgets displayed on one screen
 * It is a fake widget.
 *****************************************************************************/
#include <iostream>
#include <SDL_keyboard.h>
#include "graphic/video.h"
#include "gui/widget_list.h"
#include "gui/widget.h"
#include "interface/mouse.h"

WidgetList::WidgetList()
  : selected_widget(NULL)
{
}

WidgetList::WidgetList(const Point2i &size)
  : Widget(size)
  , selected_widget(NULL)
{
}

WidgetList::WidgetList(Profile * profile, const xmlNode * widgetListNode)
  : Widget(profile, widgetListNode)
  , selected_widget(NULL)
{
}

WidgetList::~WidgetList()
{
  // Do not use Clear/Empty methods, they might be implemented
  // for other purposes
  for (std::list<Widget*>::iterator w=widget_list.begin();
       w != widget_list.end();
       w++)
    delete *w;

  widget_list.clear();
}

void WidgetList::Clear()
{
  for (std::list<Widget*>::iterator w=widget_list.begin();
       w != widget_list.end();
       w++)
    delete *w;

  Empty();
}

void WidgetList::DelFirstWidget()
{
  delete widget_list.front();
  widget_list.pop_front();
}

void WidgetList::AddWidget(Widget* w)
{
  ASSERT(w!=NULL);
  widget_list.push_back(w);
  w->SetContainer(this);
}

void WidgetList::RemoveWidget(Widget* w)
{
  ASSERT(w!=NULL);
  widget_list.remove(w);
  w->SetContainer(NULL);
  delete w;
}

void WidgetList::SetHighlighted(bool focus)
{
  Widget::SetHighlighted(focus);
  for (wit w = widget_list.begin(); w != widget_list.end(); w++)
    (*w)->SetHighlighted(focus);
}

void WidgetList::SetFocusOn(Widget* widget, bool force_mouse_position)
{
  if (widget == selected_widget)
    return;

  // Previous selection ?
  if (selected_widget != NULL) {
    selected_widget->SetFocus(false);
  }

  selected_widget = widget;

  if (selected_widget) {
    selected_widget->SetFocus(true);

    if (force_mouse_position &&
        !selected_widget->Contains(Mouse::GetInstance()->GetPosition())) {

      Mouse::GetInstance()->SetPosition(selected_widget->GetPosition() +
                                        selected_widget->GetSize()/2);
    }
  }
}

Widget* WidgetList::GetFirstWidget() const
{
  Widget *first = NULL;

  MSG_DEBUG("widgetlist", "%p::GetFirstWidget()", this);

  for (cwit it = widget_list.begin(); it != widget_list.end(); it++) {
    if ((*it)->IsWidgetBrowser()) {
      MSG_DBG_RTTI("widgetlist", "%s:%p is a widget browser!\n",
                   typeid(*it).name(), (*it));

      first = (*it)->GetFirstWidget();
      if (first != NULL)
        return first;
    } else {
      MSG_DBG_RTTI("widgetlist", "%s:%p is NOT a widget browser!\n",
                   typeid(*it).name(), (*it));

      return (*it);
    }
  }

  return NULL;
}

Widget* WidgetList::GetLastWidget() const
{
  Widget *last = NULL;

  for (crwit it = widget_list.rbegin(); it != widget_list.rend(); it++) {
    if ((*it)->IsWidgetBrowser()) {
      last = (*it)->GetLastWidget();
      if (last)
        return last;
    } else {
      return (*it);
    }
  }

  return NULL;
}

Widget* WidgetList::GetNextWidget(const Widget *w, bool loop) const
{
  Widget *r = NULL;

  ASSERT(!w || !w->IsWidgetBrowser());

  MSG_DBG_RTTI("widgetlist", "%p::GetNextWidget(%s:%p)",
               this, typeid(w).name(), w);

  if (widget_list.size() == 0) {
    return NULL;
  }

  if (w == NULL) {
    r = GetFirstWidget();
    MSG_DBG_RTTI("widgetlist", "%p::GetNextWidget(%s:%p) ==> %s%p",
                 this, typeid(w).name(), w, typeid(r).name(), r);
    return r;
  }

  std::list<Widget*>::const_iterator it;
  for (it = widget_list.begin(); it != widget_list.end(); it++) {

    MSG_DBG_RTTI("widgetlist", "iterate on %s:%p", typeid(*it).name(), (*it));

    if (w == (*it)) {
      MSG_DBG_RTTI("widgetlist", "we have found %s:%p", typeid(*it).name(), (*it));

      it++;
      if (it != widget_list.end())
        r = (*it);
      else if (loop)
        r = GetFirstWidget();
      else
        r = (Widget*)w;
      break;
    }

    if ((*it)->IsWidgetBrowser()) {
      MSG_DBG_RTTI("widgetlist", "%s:%p is a widget browser!\n",
                   typeid(*it).name(), (*it));

      r = (*it)->GetNextWidget(w, false);

      if (r && r == w && it != widget_list.end()) {
        MSG_DBG_RTTI("widgetlist", "r == w %s:%p", typeid(r).name(), (r));
        it++;
        if (it != widget_list.end()) {
          r = (*it);
          MSG_DBG_RTTI("widgetlist", "r ==>  %s:%p", typeid(r).name(), (r));
          if (r->IsWidgetBrowser()) {
            r = r->GetFirstWidget();
          }
        } else if (loop) {
          r = GetFirstWidget();
        }
      }
      if (r)
        break;
    } else {
      MSG_DBG_RTTI("widgetlist", "%s:%p is NOT a widget browser!\n",
                   typeid(*it).name(), (*it));
    }
  }

  ASSERT(!r || !r->IsWidgetBrowser());

  MSG_DBG_RTTI("widgetlist", "%p::GetNextWidget(%s:%p) ==> %s%p",
               this, typeid(w).name(), w, typeid(r).name(), r);

  return r;
}

void WidgetList::SetFocusOnNextWidget()
{
  // No widget => exit
  if (widget_list.size() == 0) {
    selected_widget = NULL;
    return;
  }

  MSG_DBG_RTTI("widgetlist", "before %s:%p",
               typeid(selected_widget).name(), selected_widget);

  Widget* w = GetNextWidget(selected_widget, true);
  SetFocusOn(w, true);
}

Widget* WidgetList::GetPreviousWidget(const Widget *w, bool loop) const
{
  Widget *r = NULL;

  if (widget_list.size() == 0) {
    return NULL;
  }

  if (w == NULL) {
    r = GetLastWidget();
    return r;
  }

  for (std::list<Widget*>::const_reverse_iterator it = widget_list.rbegin();
       it != widget_list.rend();
       it++) {
    if (w == (*it)) {
      it++;
      if (it != widget_list.rend())
        r = (*it);
      else if (loop)
        r = (*widget_list.rbegin());
      else
        r = NULL;
      break;
    }
  }

  return r;
}

void WidgetList::SetFocusOnPreviousWidget()
{
  // No widget => exit
  if (widget_list.size() == 0) {
    selected_widget = NULL;
    return;
  }

  Widget* w = GetPreviousWidget(selected_widget, true);
  SetFocusOn(w, true);
}

bool WidgetList::SendKey(SDL_keysym key)
{
  if (selected_widget != NULL)
    return selected_widget->SendKey(key);

  return false;
}

bool WidgetList::Update(const Point2i& mousePosition,
                        const Point2i& lastMousePosition)
{
  Rectanglei clip;
  Rectanglei wlr = GetClip(clip);
  if (!wlr.GetSizeX() || !wlr.GetSizeY())
      return false;

  // Redraw the background
  bool updated = false;
  if (need_redrawing)
    RedrawBackground(wlr);

  for (std::list<Widget*>::const_iterator w=widget_list.begin();
      w != widget_list.end();
      w++)
  {
    Rectanglei r((*w)->GetPosition(), (*w)->GetSize());
    r.Clip(wlr);

    if (r.GetSizeX() && r.GetSizeY()) {
      SwapWindowClip(r);
      updated |= (*w)->Update(mousePosition, lastMousePosition);
      SwapWindowClip(r);
    }
  }

  if (updated)
    RedrawForeground();

  // Restore initial clip rectangle
  UnsetClip(clip);
  need_redrawing = false;
  return updated;
}

void WidgetList::Draw(const Point2i &mousePosition)
{
  Rectanglei clip;
  Rectanglei wlr = GetClip(clip);
  if (!wlr.GetSizeX() || !wlr.GetSizeY())
      return;

  for (std::list<Widget*>::const_iterator w=widget_list.begin();
      w != widget_list.end();
      w++)
  {
    Rectanglei r((*w)->GetPosition(), (*w)->GetSize());
    r.Clip(wlr);

    if (r.GetSizeX() && r.GetSizeY()) {
      Rectanglei wr = r;
      SwapWindowClip(r);
      (*w)->RedrawBackground(wr);
      (*w)->Draw(mousePosition);
      (*w)->RedrawForeground();
      SwapWindowClip(r);
    }
  }

  // Restore initial clip rectangle
  UnsetClip(clip);
}

Widget* WidgetList::ClickUp(const Point2i &mousePosition, uint button)
{
  for (wit w=widget_list.begin(); w != widget_list.end(); w++) {
    if ((*w)->Contains(mousePosition)) {
      Widget* child = (*w)->ClickUp(mousePosition,button);

      if (child)
        SetFocusOn(child);

      return child;
    }
  }

  return NULL;
}

Widget* WidgetList::Click(const Point2i &mousePosition, uint button)
{
  for (wit w=widget_list.begin(); w != widget_list.end(); w++)
    if ((*w)->Contains(mousePosition))
      (*w)->Click(mousePosition,button);

  return NULL;
}

void WidgetList::NeedRedrawing()
{
  need_redrawing = true;

  for (wit w=widget_list.begin(); w != widget_list.end(); w++)
    (*w)->NeedRedrawing();
}

void WidgetList::Pack()
{
  for (wit w=widget_list.begin(); w != widget_list.end(); w++)
    (*w)->Pack();
}

void WidgetList::SetSelfBackgroundColor(const Color &background_color)
{
  Widget::SetBackgroundColor(background_color);
}

void WidgetList::SetSelfHighlightBgColor(const Color &highlight_bg_color)
{
  Widget::SetHighlightBgColor(highlight_bg_color);
}

void WidgetList::SetBackgroundColor(const Color &background_color)
{
  Widget::SetBackgroundColor(background_color);
  for (wit w=widget_list.begin(); w != widget_list.end(); w++)
    (*w)->SetBackgroundColor(background_color);
}

void WidgetList::SetHighlightBgColor(const Color &highlight_bg_color)
{
  Widget::SetHighlightBgColor(highlight_bg_color);
  for (wit w=widget_list.begin(); w != widget_list.end(); w++)
    (*w)->SetHighlightBgColor(highlight_bg_color);
}