summaryrefslogtreecommitdiff
path: root/plugingui/widget.cc
blob: 4f8d226e2f34d96524971bca21f0b7f8eba9b835 (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
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            widget.cc
 *
 *  Sun Oct  9 13:01:44 CEST 2011
 *  Copyright 2011 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of CamSync.
 *
 *  CamSync 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.
 *
 *  CamSync 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 CamSync; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "widget.h"

#include "painter.h"
#include "window.h"

#include <stdio.h>

namespace GUI {

Widget::Widget(Widget *parent)
  : pixbuf(1, 1)
{
  _width = _height = 10;

  this->parent = parent;
  if(parent) {
    parent->addChild(this);
    _window = parent->window();
  }
  _width = _height = 0;
  _visible = true;
}

Widget::~Widget()
{
  if(parent) parent->removeChild(this);
}

void Widget::show()
{
  setVisible(true);
}

void Widget::hide()
{
  setVisible(false);
}

void Widget::setVisible(bool v)
{
  _visible = v;
  repaintEvent(NULL);
}

bool Widget::visible()
{
  return _visible;
}

void Widget::addChild(Widget *widget)
{
  children.push_back(widget);
}

void Widget::removeChild(Widget *widget)
{
  std::vector<Widget *>::iterator i = children.begin();
  while(i != children.end()) {
    if(*i == widget) {
      children.erase(i);
      return;
    }
    i++;
  }
}

void Widget::resize(int width, int height)
{
  if(width < 1 || height < 1) return;
  _width = width;
  _height = height;
  pixbuf.realloc(width, height);
}

void Widget::move(size_t x, size_t y)
{
  _x = x;
  _y = y;
}

size_t Widget::x() { return _x; }
size_t Widget::y() { return _y; }
size_t Widget::width() { return _width; }
size_t Widget::height() { return _height; }

size_t Widget::windowX()
{
  size_t window_x = x();
  if(parent) window_x += parent->windowX();
  return window_x;
}

size_t Widget::windowY()
{
  size_t window_y = y();
  if(parent) window_y += parent->windowY();
  return window_y;
}

Widget *Widget::find(size_t x, size_t y)
{
  std::vector<Widget*>::reverse_iterator i = children.rbegin();
  while(i != children.rend()) {
    Widget *w = *i;
    if(w->visible()) {
      if(w->x() <= x && (w->x() + w->width()) >= x &&
         w->y() <= y && w->y() + w->height() >= y)
        return w->find(x - w->x(), y - w->y());
    }
    i++;
  }

  if(x > width() || x < 0 || y > height() || y < 0) return NULL;
  return this;
}

Window *Widget::window()
{
  return _window;
}

void Widget::repaint_r(RepaintEvent *e)
{
  Painter p(this); // make sure pixbuf refcount is incremented.

  repaintEvent(e);

  std::vector<Widget*>::iterator i = children.begin();
  while(i != children.end()) {
    Widget *w = *i;
    w->repaint_r(e);
    i++;
  }
}

std::vector<PixelBufferAlpha *> Widget::getPixelBuffers()
{
  std::vector<PixelBufferAlpha *> pbs;

  pixbuf.x = windowX();
  pixbuf.y = windowY();

  pbs.push_back(&pixbuf);

  std::vector<Widget*>::iterator i = children.begin();
  while(i != children.end()) {
    Widget *w = *i;
    if(w->visible()) {
      std::vector<PixelBufferAlpha *> pbs0 = w->getPixelBuffers();
      pbs.insert(pbs.end(), pbs0.begin(), pbs0.end());
    }
    i++;
  }

  return pbs;
}

bool Widget::hasKeyboardFocus()
{
  return window()->keyboardFocus() == this;
}

} // GUI::