JNR
uiRadio.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // uiRadio.cpp
32 // Dependent: -
33 //
34 // UI Radio buttons group
35 //
36 #include "stdafx.h"
37 #include "GUI.h"
38 
39 uiRadio::uiRadio(void)
40 {
41  m_nCnt = 0;
42  m_nActive = 0;
43 }
44 
45 uiRadio::~uiRadio(void)
46 {
47 }
48 
49 void uiRadio::create(laPoint3 pos, laPoint3 size, unsigned align)
50 {
51  uiContainer::create(pos, size);
52  toggleBorders(M_FALSE);
53  toggleMoveable(M_FALSE);
54 
55  //Add buttons
56  for(unsigned i=0; i<CUI__MAX_RADIOBTN_CNT; i++)
57  {
58  //set pos
59  if(align==CUI__ALIGNH)
60  m_arButton[i].create(laPoint3(i*70,0), laPoint3(size.x(), 40) );
61  else
62  m_arButton[i].create(laPoint3(0,i*50), laPoint3(size.x(), 40) );
63 
64  m_arButton[i].setID(i); // set ID
65  m_arButton[i].setReceiver(this); // redirect messages to this window
66  m_arButton[i].hide(); // hide initially
67  m_arButton[i].togglePersistent(M_TRUE); // make it persistent (lika a checkbox)
68  this->insert(m_arButton+i); // insert into this window
69  }
70 }
71 
72 void uiRadio::setCount(unsigned cnt)
73 {
74  //Set count
75  if(cnt>=CUI__MAX_RADIOBTN_CNT)
76  throw laError("uiRadio::SetCount(): Exceeding the limit of CUI__MAX_RADIOBTN_CNT=%d", cnt);
77  m_nCnt=cnt;
78 
79  //Enable requested buttons
80  for(unsigned i=0; i<m_nCnt; i++) m_arButton[i].show();
81 
82  //Hide the rest
83  for(unsigned i=m_nCnt; i<CUI__MAX_RADIOBTN_CNT; i++) m_arButton[i].hide();
84 }
85 
87 {
89 }
90 
91 void uiRadio::onCommand(unsigned long uID, unsigned long nCmd)
92 {
93  m_nActive = uID;
94 
95  //Make sure only one button is active
96  for(unsigned i=0; i<m_nCnt; i++)
97  {
98  if(i!=m_nActive) m_arButton[i].unpress();
99  }
100 }
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiButton.cpp:55
void kill()
Discard window and all children.
Definition: uiRadio.cpp:86
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiContainer.cpp:97
void setReceiver(uiWindow *pWnd)
Set receiver window (fires onCommand when the button is clicked)
Definition: uiButton.h:95
void toggleBorders(M_BOOL bEnable)
Toggle border visbility.
Definition: uiContainer.h:87
virtual void onCommand(unsigned long uID, unsigned long nCmd)
Command event handler.
Definition: uiRadio.cpp:91
void toggleMoveable(M_BOOL bEnable, unsigned nButton=0)
Toggle moveable flag.
Definition: uiContainer.h:95
Error handling class.
Definition: laError.h:46
virtual void kill()
Discard window and all children.
Definition: uiWindow.cpp:125