File: dialog_ctor.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 (262 lines) | stat: -rw-r--r-- 7,691 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// dialog_ctor.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 "localdefs.h"
#include <InterViews/border.h>
#include <InterViews/box.h>
#include <InterViews/button.h>
#include <InterViews/frame.h>
#include <InterViews/glue.h>
#include <InterViews/message.h>
#include <InterViews/painter.h>
#include "application.h"
#include "buttonarray.h"
#include "dialog_ctor.h"
#include "eventforwarder.h"
#include "queryvalue.h"
#include "request.h"
#include "dialogbox.h"
#include "fileselector.h"
#include "textinput.h"
#include "valueslider.h"

// ResponseButton is a private subclass of PushButton whose label is
// highlighted when the particular instance is the default response to a 
// query, i.e., the one that will be activated if the user presses <return>

class ResponseButton : public PushButton {
public:
	ResponseButton(const char* text, ButtonState* s, int v,
			boolean isDefault=false)
		: PushButton(text, s, v), is_default(isDefault) {}
	ResponseButton(const char* text, ButtonState* s, void* v,
			boolean isDefault=false)
		: PushButton(text, s, v), is_default(isDefault) {}
	virtual ~ResponseButton() {}
	redefined void Refresh();
protected:
	boolean is_default;	// is this button the default for <return>
};

void
ResponseButton::Refresh() {
	int oldStyle = output->GetStyle();
	// button that is pressed when <return> is hit has bold font
	if(is_default) output->SetStyle(Boldface);
	PushButton::Refresh();
	if(is_default) output->SetStyle(oldStyle);
}

//********

DialogConstructor* DialogConstructor::ctorInstance = nil;

DialogConstructor*
DialogConstructor::getInstance() {
	if (ctorInstance == nil)
		ctorInstance = new DialogConstructor;
	return ctorInstance;
}

void
DialogConstructor::destroyInstance() {
	delete ctorInstance;
	ctorInstance = nil;
}

DialogBox *
DialogConstructor::createDialog(Interactor* under, Request &request) {
	typedef void (DialogBox::*(DialogForwardFunction))(Event&);
	class DialogEventForwarder : public EventForwarder {
	public:
		DialogEventForwarder(DialogBox* db, DialogForwardFunction dff)
			: myDialog(db), myFunction(dff) {}
		redefined void handle(Event &e) {
			(myDialog->*myFunction)(e);
		}
	private:
		DialogBox* myDialog;
		DialogForwardFunction myFunction;
	};
	typedef DialogBox*
		(*DialogFactoryFunction)(ButtonState*, Interactor*, Response);
	static DialogFactoryFunction dialogFactory[] = {
		&Alert::create,
		&Confirmer::create,
		&ChoiceDialog::create,
		&InputDialog::create
	};
	// all confirm, deny buttons are tied to this state
	ButtonState* state = new ButtonState(NullResponse);
	Response response = request.buttonList->defaultResponse();
	DialogBox* dialog= (*dialogFactory[request.type()])(state, under, response);
	VBox* interior = new VBox;
	if(request.hasLabels()) {
		interior->Insert(messageBox(request.labelList));
	}
	if(request.hasBrowser()) {
		interior->Insert(new HBorder);
		Scene* selector = fileSelector(request.browserQuery, state);
		interior->Insert(selector);
		FileSelector* fs = (FileSelector *) selector;
		fs->setForwarder(
			new DialogEventForwarder(dialog, &DialogBox::eventLoop)
		);
		((InputDialog *) dialog)->addFileSelector(fs);
	}
	if(request.hasValues()) {
		interior->Insert(new HBorder);
		interior->Insert(textInputBox((InputDialog *) dialog,
			request.valueList, state));
	}
	if(request.hasChoices()) {
		interior->Insert(new HBorder);
		interior->Insert(choiceButtonBox(request.choiceList, state));
	}
	interior->Insert(new HBorder);
	// all dialogs have a button box
	interior->Insert(new VGlue(5, 0));	// spacer above buttons
	interior->Insert(buttonBox(request.buttonList, state));
	interior->Insert(new VGlue(2, 0));	// spacer below buttons
	dialog->Insert(interior);
	dialog->setBell(request.useBell());
	Resource::unref(state);
	return dialog;
}

Scene *
DialogConstructor::messageBox(QueryList *list) {
	VBox* mesgBox = new VBox;
	mesgBox->Align(Center);
	for(list->start(); list->more(); list->next()) {
		mesgBox->Insert(
			new MarginFrame(
				new Message("Title", list->item()->label()),
				5, 0, hfil,
				0, 0, vfil
			)
		);
	}
	return mesgBox;
}

Scene *
DialogConstructor::fileSelector(QueryFile *qfile, ButtonState* response) {
	return new FileSelector(
		response, qfile->path(), qfile->suffixes(), 15, 32
	);
}

Scene *
DialogConstructor::textInputBox(InputDialog *ipd, ValueList *list,
		ButtonState *bs) {
	VBox* textBox = new VBox;
	const char* inputSubtitle = list->label();
	// insert text entry subtitle before inserting text entry items
	if(inputSubtitle != nil && strlen(inputSubtitle) > 0) {
		textBox->Insert(
			new MarginFrame(
				new Message("Subtitle", inputSubtitle),
				10, 0, 0,		// horiz border 10, fixed
				0, 0, vfil		// vert  border 0, stretchable
			)
		);
	}
	TextInput* lasttxt = nil;
	for(list->start(); list->more(); list->next()) {
		register QueryValue *qvalue = list->item();
		TextInput* txt = nil;
		ValueSlider* slider = nil;
		if(qvalue->type() == QueryValue::RangedValue) {
			textBox->Insert(
				new MarginFrame(
					slider = new ValueSlider(bs, qvalue),
					10, 0, 0, 0, 0, vfil
				)
			);
			txt = slider->getTextInput();
		}
		else {	
			const char* samp = "MMMMMMMMMMMMMM";
			txt = new TextInput(bs, samp, qvalue);
			textBox->Insert(
				new MarginFrame(
					new HBox(
						new Message("TextEntryLabel", qvalue->label(), Center),
						new HGlue(5, 0),
						new Frame(txt, 1)
					),
					10, 0, 0,			// horiz border 10, fixed
					0, 0, vfil			// vert  border 0, stretchable
				)
			);
		}
		// add current TextInput to the linked list of TextInputs
		if(lasttxt != nil)
			lasttxt->appendTo(txt);
		else
			ipd->addTextInput(txt);
		lasttxt = txt;
	}
	return textBox;
}

Scene *
DialogConstructor::choiceButtonBox(ChoiceList *list, ButtonState* state) {
	VBox* outerBox = new VBox;
	boolean notFirst = false;
	for(list->start(); list->more(); list->next()) {
		if(notFirst) outerBox->Insert(new HBorder);
		outerBox->Insert(new ButtonArray(state, list->item()));
		notFirst = true;
	}
	outerBox->Insert(new VGlue);
	return outerBox;
}

// create set of buttons initialized with appropriate responses (Yes, No, etc.)

Scene *
DialogConstructor::buttonBox(ButtonList *list, ButtonState *s) {
	HBox* btnBox = new HBox;
	for(list->start(); list->more(); list->next()) {
		btnBox->Insert(new HGlue);
		Response resp = list->item()->response();
		Interactor *button = new ResponseButton(
			list->item()->label(),
			s,
			int(resp),
			resp == list->defaultResponse()	// true if is default
		);
		btnBox->Insert(button);
	}
	btnBox->Insert(new HGlue);
	return btnBox;
}