File: buttonarray.C

package info (click to toggle)
mixviews 1.20-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 5,960
  • sloc: cpp: 32,879; ansic: 2,110; makefile: 445; sh: 17
file content (156 lines) | stat: -rw-r--r-- 4,764 bytes parent folder | download | duplicates (3)
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
// buttonarray.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and that
 *  both that copyright notice and this permission notice appear in
 *  supporting documentation. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <InterViews/box.h>
#include <InterViews/button.h>
#include <InterViews/glue.h>
#include <InterViews/message.h>

#include "buttonarray.h"
#include "query.h"

// macro for deleting any type of list as defined by me

#define delete_list(LinkType, list) {LinkType *l = list; while(l) { LinkType *nxt = l->next; delete l; l = nxt; }}

ButtonArray::ButtonStateLink::~ButtonStateLink() { Resource::unref(state); }
		
ButtonArray::ButtonStateLink *
ButtonArray::ButtonStateLink::tail() {
	ButtonStateLink *b = this;
	while(b && b->next) b = b->next;
	return b;
}

void
ButtonArray::ButtonStateLink::append(ButtonArray::ButtonStateLink *b) {
	tail()->next = b;
}

int
ButtonArray::ButtonStateLink::finalValue() {	// logical OR of its states
	ButtonStateLink *b = this;
	int value = 0;
	while(b != nil) {
		int currentVal;
		b->state->GetValue(currentVal);
		value |= currentVal;
		b = b->next;
	}
	return value;
}

////////

ButtonArray::ButtonArray(ButtonState* state, QueryChoice* choices)
		: subject(nil), stateList(nil), queryChoice(choices) {
	init(state, choices);
}

ButtonArray::~ButtonArray() {
	if(subject != nil)
		subject->Detach(this);
	delete_list(ButtonStateLink, stateList);
}

void
ButtonArray::init(ButtonState* state, QueryChoice* choices) {
	subject = state;
	if(subject != nil)
		subject->Attach(this);
	boolean exclusive = choices->areExclusive();
	Scene* arrayBox = new VBox;	// enclosing box for buttons
	Scene* rowBox = new HBox;		// box for each row of buttons
	rowBox->Insert(new HGlue);			// left edge glue
	const char *label = nil;
	// exclusive buttons all use the same state
	ButtonState *exclstate = exclusive ? 
		new ButtonState(choices->state()) : nil;
	int numberOfButtons = choices->numberOfValues();
	int endOfRow = (numberOfButtons > 4) ?
		roundUp(numberOfButtons / 2.0) : numberOfButtons;
	endOfRow -= 1;		// to match with 0 to n-1
	choices->start();	// initialize the iterators
	for(int buttonNumber = 0; buttonNumber < numberOfButtons; buttonNumber++) {
		label = choices->nextLabel();
		int value;
		// retrieve value & whether or not it is currently set
		boolean isOn = choices->nextValue(&value);
		Interactor *button = nil;
		// radio buttons are used for exclusive choices and
		// check-boxes used for multiple choices
		if(exclusive) {
			button = new RadioButton(label, exclstate, value);
		}
		else {
			exclstate = new ButtonState(isOn ? value : 0);
			button = new CheckBox(label, exclstate, value, 0);
		}
		// append each state if non-exclusive
		if(stateList == nil)
			stateList = new ButtonStateLink(exclstate);
		else if(!exclusive)
			stateList->append(new ButtonStateLink(exclstate));
		rowBox->Insert(button);
		rowBox->Insert(new HGlue);
		if(buttonNumber == endOfRow) {
			arrayBox->Insert(rowBox);	// insert current row
			int finalEnd = numberOfButtons - 1;
			if(buttonNumber < finalEnd) {
				rowBox = new HBox;	// start new row if not at end
				rowBox->Insert(new HGlue); // left edge glue
				endOfRow = finalEnd;
			}
		}
	}
	// each set of choice buttons has a text label above it
	Insert(
		new VBox(
			new HBox(
				new HGlue(10),
				new Message("ChoiceButtonTitle", choices->label()),
				new HGlue(10)
			),
			new VGlue,
			arrayBox
		)
	);
}

// set the "master" state of the ButtonArray to the OR'd combination of the
// individual buttons' states (returned by ButtonStateList::finalValue())

void
ButtonArray::Update() {
	int stateValue;
	subject->GetValue(stateValue);
	if(stateValue == Yes)	// if user confirmed the dialog
		queryChoice->setState(stateList->finalValue());
	Super::Update();
}