~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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * 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 "HLayout.h"
#include "AbstractCheckedButton.h"
#include "StaticText.h"

namespace nux
{
  NUX_IMPLEMENT_OBJECT_TYPE(AbstractCheckedButton);
  
  AbstractCheckedButton::AbstractCheckedButton(const std::string &str, bool state, NUX_FILE_LINE_DECL)
    : AbstractButton(NUX_FILE_LINE_PARAM)
  {
    label_ = str;
    active_ = state;
    hlayout_ = 0;

    static_text_  = new StaticText(label_, NUX_TRACKER_LOCATION);
    static_text_->SetTextColor(label_color_);
    hlayout_      = new HLayout(NUX_TRACKER_LOCATION);
    check_area_   = new BasicView(NUX_TRACKER_LOCATION);

    check_area_->SetInputEventSensitivity(false);
    static_text_->SetInputEventSensitivity(false);

    // Set Geometry
    check_area_->SetMinMaxSize(14, 14);

    hlayout_->SetSpaceBetweenChildren(4);
    hlayout_->SetContentDistribution(MAJOR_POSITION_LEFT);
    hlayout_->AddView(check_area_, 0, MINOR_POSITION_CENTER, MINOR_SIZE_MATCHCONTENT);
    hlayout_->AddView(static_text_, 1, MINOR_POSITION_CENTER, MINOR_SIZE_MATCHCONTENT);

//     // This is convenient to make the layout and the AbstractCheckedButton fit the check area and the caption area.
//     // Since the check area is bigger than 4x4, it will force the layout and the AbstractCheckedButton to grow.
//     // This is useful if the AbstractCheckedButton is put in a vertical layout and it has a stretch factor of 0. Then the width of the AbstractCheckedButton
//     // will be adjusted to fit the minimum width of the check area and the caption area.
//     {
//       hlayout_->SetMinimumSize(1, 1);
//       SetMinimumSize(14, 14);
//       ApplyMinWidth();
//       ApplyMinHeight();
//     }

    hlayout_->SetScaleFactor(0);
    SetLayout(hlayout_);

  }

  AbstractCheckedButton::~AbstractCheckedButton()
  {
  }

  void AbstractCheckedButton::SetLabel(const std::string &checkbox_label)
  {
    label_ = checkbox_label;
    static_text_->SetText(label_);
    QueueDraw();
  }

  std::string AbstractCheckedButton::GetLabel() const
  {
    return label_;
  }

  void AbstractCheckedButton::Draw(GraphicsEngine &graphics_engine, bool /* force_draw */)
  {
    Geometry base = GetGeometry();
    graphics_engine.PushClippingRectangle(base);

    GetPainter().PaintBackground(graphics_engine, base);

    InteractState is;
    is.is_on = active_;

    if (visual_state_ == VISUAL_STATE_PRESSED)
    {
      is.is_focus = true;
    }
    else if (visual_state_ == VISUAL_STATE_PRELIGHT)
    {
      is.is_prelight = true;
    }
    else
    {
      is.is_focus = false;
      is.is_prelight = false;
    }

    GetPainter().PushPaintLayerStack();
    {
      GetPainter().PaintCheckBox(graphics_engine, check_area_->GetGeometry(), is, Color(0xff000000));
      static_text_->ProcessDraw(graphics_engine, true);
    }
    GetPainter().PopPaintLayerStack();

    graphics_engine.PopClippingRectangle();
  }

  long AbstractCheckedButton::ComputeContentSize()
  {
    if (view_layout_)
    {
      PreLayoutManagement();

      int old_width = GetBaseWidth();
      int old_height = GetBaseHeight();

      // Let the text view be as large as possible.
      static_text_->SetMaximumWidth(AREA_MAX_WIDTH);

      // Constrain the vertical expansion of the color selector.
      view_layout_->SetBaseHeight(1);
      long ret = view_layout_->ComputeContentSize();

      PostLayoutManagement(ret);

      {
        // Check if the text view goes out of the AbstractCheckedButton area.
        Geometry base = GetGeometry();
        Geometry text_geo = static_text_->GetGeometry();
 
        // Intersect the AbstractCheckedButton and the text view
        Geometry intersection = base.Intersect(text_geo);
        if (intersection != text_geo)
        {
          // The text view goes outside of the AbstractCheckedButton area. We have to clip it
          static_text_->SetMaximumWidth(intersection.width);

          // Assign a size of 1 to the layout and call ComputeContentSize.
          // Inside the StaticText::ComputeContentSize there is code that takes care of size negociation.
          view_layout_->SetBaseWidth(1);
          ret = view_layout_->ComputeContentSize();

          // Assign the layout geometry to the AbstractCheckedButton view.
          PostLayoutManagement(ret);
        }
      }

      int new_width = GetBaseWidth();
      int new_height = GetBaseHeight();

      long size_compliance = 0;

      // The layout has been resized to tightly pack its content
      if (new_width > old_width)
      {
        size_compliance |= eLargerWidth; // need scrollbar
      }
      else if (new_width < old_width)
      {
        size_compliance |= eSmallerWidth;
      }
      else
      {
        size_compliance |= eCompliantWidth;
      }

      // The layout has been resized to tightly pack its content
      if (new_height > old_height)
      {
        size_compliance |= eLargerHeight; // need scrollbar
      }
      else if (new_height < old_height)
      {
        size_compliance |= eSmallerHeight;
      }
      else
      {
        size_compliance |= eCompliantHeight;
      }

      return size_compliance;
    }
    else
    {
      PreLayoutManagement();
      int ret = PostLayoutManagement(eCompliantHeight | eCompliantWidth);
      return ret;
    }

    return 0;
  }

  void AbstractCheckedButton::SetLabelFontSize(int point)
  {
    AbstractButton::SetLabelFontSize(point);
    if (static_text_ == NULL)
      return;
    
    static_text_->SetTextPointSize(point);
    
    ComputeContentSize();
    QueueDraw();
  }
}