~3v1n0/nux/x11-conffile-on-unity-only

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
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */

#include "Nux.h"
#include "RadioButton.h"
#include "RadioButtonGroup.h"

namespace nux
{
  NUX_IMPLEMENT_OBJECT_TYPE(RadioButtonGroup);

  RadioButtonGroup::RadioButtonGroup(NUX_FILE_LINE_DECL)
  : InitiallyUnownedObject(NUX_FILE_LINE_PARAM)  // RadioButtonGroup is created as un-owned
  , active_button_index_(0)
  {
  }

  RadioButtonGroup::~RadioButtonGroup()
  {
    std::vector<ObjectWeakPtr<RadioButton> >::iterator it;

    for (it = radio_button_array_.begin(); it != radio_button_array_.end(); it++)
    {
      if ((*it).IsValid())
      {
        (*it)->SetRadioButtonGroup(NULL);
        (*it)->radio_group_index_ = -1;
      }
    }
  }


  void RadioButtonGroup::ConnectButton(RadioButton* radio)
  {
    if (radio == NULL)
    {
      return;
    }

    if (!radio->Type().IsDerivedFromType(RadioButton::StaticObjectType))
    {
      return;
    }

    std::vector<ObjectWeakPtr<RadioButton> >::iterator it;

    for (it = radio_button_array_.begin(); it != radio_button_array_.end(); it++)
    {
      if ((*it).IsValid() && ((*it).GetPointer() == radio))
      {
        // already in
        return;
      }
    }

    size_t index = (unsigned int) radio_button_array_.size();

    if (index == 0)
    {
      // Inserting the first radio button
      if (radio->GetRadioButtonGroup().IsValid())
      {
        // Disconnect from the other selector
        radio->GetRadioButtonGroup()->DisconnectButton(radio);
      }

      radio->SetRadioButtonGroup(this);
      radio->radio_group_index_ = 0;
      radio->SetStatePrivate(true);
      radio_button_array_.push_back(ObjectWeakPtr<RadioButton>(radio));
      active_button_index_ = 0;
    }
    else
    {
      if (radio->GetRadioButtonGroup().IsValid())
      {
        // Disconnect from the other selector
        radio->GetRadioButtonGroup()->DisconnectButton(radio);
      }

      radio->SetRadioButtonGroup(this);
      radio->radio_group_index_ = index;
      // When a radio button is added to a group that is not empty, its check state is set to false.
      radio->SetStatePrivate(false);
      radio_button_array_.push_back(ObjectWeakPtr<RadioButton>(radio));
    }
  }

  void RadioButtonGroup::DisconnectButton(RadioButton* radio)
  {
    bool found = false;
    size_t array_size = (unsigned int) radio_button_array_.size();
    std::vector<ObjectWeakPtr<RadioButton> >::iterator it;
    it = radio_button_array_.begin();
    size_t i;

    for (i = 0; i < array_size; i++, it++)
    {
      if (radio_button_array_[i] == radio)
      {
        radio->radio_group_index_ = -1;
        radio->SetStatePrivate(false);
        radio_button_array_.erase(it);
        found = true;
        break;
      }
    }

    if (found && (i - 1 > 0) && (!radio_button_array_.empty()))
    {
      // The previous button becomes active
      radio_button_array_[i]->SetStatePrivate(true);
    }
  }

  int RadioButtonGroup::GetNumButtons()
  {
    int count = 0;
    std::vector<ObjectWeakPtr<RadioButton> >::iterator it;
    for (it = radio_button_array_.begin(); it != radio_button_array_.end(); ++it)
    {
      if ((*it).IsValid())
      {
        ++count;
      }
    }
    return count;
  }

  void RadioButtonGroup::NotifyClick(RadioButton* radio)
  {
    std::vector<ObjectWeakPtr<RadioButton> >::iterator it;

    for (it = radio_button_array_.begin(); it != radio_button_array_.end(); ++it)
    {
      if ((*it).GetPointer() != radio)
      {
        if ((*it).IsValid())
        {
          (*it)->SetStatePrivate(false, true);
        }
      }
      else
      {
        if ((*it).IsValid())
        {
          (*it)->SetStatePrivate(true,  true);
        }
      }
    }
  }

  void RadioButtonGroup::SetActiveButton(RadioButton* radio, bool emit_signal)
  {
    std::vector<ObjectWeakPtr<RadioButton> >::iterator it = find(radio_button_array_.begin(), radio_button_array_.end(), radio);

    if (it == radio_button_array_.end())
      return;

    if ((*it).IsValid() == false)
    {
      // The button has been destroyed.
      // Remove it from the array
      radio_button_array_.erase(it);
      return;
    }

    for (it = radio_button_array_.begin(); it != radio_button_array_.end(); ++it)
    {
      if ((*it).IsValid() && ((*it).GetPointer() != radio))
      {
        (*it)->SetStatePrivate(false, emit_signal);
      }
    }

    radio->SetStatePrivate(true, emit_signal);
  }
}