From: woersi@LODSUN1.LOD.UNI-KARLSRUHE.DE (Woersinger Diplomand von ASK)
Subject: simple use of group of radiobuttons
Organization: Internet-USENET Gateway at Stanford University
Date: Thu, 8 Oct 1992 15:41:37 GMT

hi,

playing around with radiobuttons, I wrote a class RadioGroup
to simplyfy the use of them.
For more information, see radiogroup.h
main.c is a small example how to use it.

The code works on SunOS 4.1.1 + AT&T C++2.1.03 + IV3.1beta2. Maybe
it will work on other machine too.

If you extend/improve/correct it, please let me know.


Rolf-Martin Woersinger                    Akademische Software Kooperation (ASK)
woersi@lodsun1.lod.uni-karlsruhe.de       Karlsruhe, Germany      +49-721-695639


#---------------------------------- cut here ----------------------------------
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Rolf-Martin Woersinger 357939 <woersi@askhp> on Thu Oct  8 16:35:59 1992
#
# This archive contains:
#	radiogroup.h	radiogroup.c	main.c		Imakefile	
#

LANG=""; export LANG
PATH=/bin:/usr/bin:$PATH; export PATH

echo x - radiogroup.h
cat >radiogroup.h <<'@EOF'
//
//
//
//	radiogroup.h
//
//
//	create and handle a group of radiobuttons
//
//
//      author: Rolf-Martin Woersinger (woersi@lodsun1.lod.uni-karlsruhe.de)
//	first edit: 2.10.92
//	last edit:  8.10.92
//	IV-version: 3.1beta2

#ifndef RADIOGROUP_H
#define RADIOGROUP_H

#include <InterViews/action.h>
#include <InterViews/telltale.h>

#include <OS/list.h>


enum Direction { horizontal=0, vertical };

class Button;
class Glyph;
class TelltaleGroup;


struct RadioDef {
     const char* text;		// displayed text
     Action* action;		// action to call when radiobutton is pressed
};    


declarePtrList(ButtonList, Button)	// macro, see <OS/list.h>


class RadioGroup {

public:

				// create group of radiobuttons
				// rd_list: list of struct RadioDef,
				//	containing text to dislay
				//      and optional action to call when pressed
				// drct: horizonal or vertical order
				// space: space between radiobuttons
    RadioGroup (RadioDef* rd_list, Direction drct, float space);

   ~RadioGroup ();

    Glyph* get_glyph();		// get glyph of group of radiobuttons

    long selected();		// get index of chosen radiobutton

				// set i.th radiobuttons flag true or false
				// flag must be TelltaleState::is_enabled or ...
				//   see <Interviews/telltale.h>
				//   and Refman fig. 9.3 
    void set(long i, TelltaleFlags flag, boolean bool);

				// get i.th radiobuttons value of flag
				// flag must be ... see set()
    boolean test(long i, TelltaleFlags flag);

private:
    TelltaleGroup* telltalegroup_;
    Glyph* glyph_;
    ButtonList radiobuttonlist_;
};

#endif
@EOF

chmod 640 radiogroup.h

echo x - radiogroup.c
cat >radiogroup.c <<'@EOF'
//
//
//
//	radiogroup.c
//
//
//	create and handle a group of radiobuttons
//
//
//
//      author: Rolf-Martin Woersinger (woersi@lodsun1.lod.uni-karlsruhe.de)
//	first edit:	10/02/92
//	last edit:	10/08/92
//	IV-version: 	3.1beta2


#include <IV-look/kit.h>
#include <InterViews/layout.h>
#include <InterViews/resource.h>

#include "radiogroup.h"


implementPtrList(ButtonList, Button);	// macro, see <OS/list.h>


RadioGroup::RadioGroup(RadioDef* rd_list, Direction drct, float space) {

    WidgetKit& kit = *WidgetKit::instance();
    const LayoutKit& layout = *LayoutKit::instance();

    glyph_ = (drct == horizontal ? layout.hbox() : layout.vbox() );
    telltalegroup_ = new TelltaleGroup;
    Glyph* space_gl = 
	( drct==horizontal ? layout.hspace(space) : layout.vspace(space) );

    int i=0;
    for (RadioDef* cur=rd_list; cur->text!=nil; cur++,i++) {
	if (cur!=rd_list) glyph_->append(space_gl);
				// append radiobutton in list
	radiobuttonlist_.append(
	    kit.radio_button(telltalegroup_, cur->text, cur->action)
	);
	glyph_->append(radiobuttonlist_.item(i));

    }
}


RadioGroup::~RadioGroup() {

    for (int i=0; i < radiobuttonlist_.count(); i++) {
	delete radiobuttonlist_.item(i);
    }
    radiobuttonlist_.remove_all();

    delete telltalegroup_;
}


Glyph* RadioGroup::get_glyph() {
    return glyph_;
}


long RadioGroup::selected() {

    int i=0;			// ListItr(list_type) supports iteration 
				// in list_type with more(), next(), cur()
				// see <OS/list.h> and RefMan section A.3 List

    for (ListItr(ButtonList) l(radiobuttonlist_);l.more();l.next(),i++) {
	Button* rbut = l.cur();
	if (rbut->state()->test(TelltaleState::is_chosen)) return i;
    }
    return -1;
}

void RadioGroup::set(long index, TelltaleFlags flag, boolean bool) {
    radiobuttonlist_.item(index)->state()->set(flag, bool);
}

boolean RadioGroup::test(long index, TelltaleFlags flag) {
    return radiobuttonlist_.item(index)->state()->test(flag);
}

@EOF

chmod 640 radiogroup.c

echo x - main.c
cat >main.c <<'@EOF'
#include <IV-look/kit.h>
#include <InterViews/action.h>
#include <InterViews/background.h>
#include <InterViews/layout.h>
#include <InterViews/session.h>
#include <InterViews/style.h>
#include <InterViews/window.h>
#include <stdio.h>


#include "radiogroup.h"

struct CmdInfo;
RadioDef rg_def[] = { 
    { "AND" }, { "OR" }, { "AND NOT" }, { "OR NOT" }, { nil }
};

class App {
public:
    App();
    ~App();
    void set(), selected(), test(), and();
    RadioGroup* radiogroup_;
};

declareActionCallback(App)
implementActionCallback(App)

App::App() {}

App::~App() { delete radiogroup_; }

// setting value of radiobutton flag
void App::set() { radiogroup_->set(0,TelltaleState::is_chosen, true); }

void App::selected() {
     printf ("selected radiobutton is no.%d\n", radiogroup_->selected());
}

// testing value of radiobutton flag
void App::test() {
     printf ("radiobutton OR is%s chosen\n", 
		(radiogroup_->test(1,TelltaleState::is_chosen) ? "" : " not") );
}

// actioncallback for radiobutton AND
void App::and() { printf("radiobutton AND was pressed\n"); }

int main(int argc, char** argv) {
    Session* session = new Session("Himom", argc, argv);
    Style* style = session->style();
    WidgetKit& kit = *WidgetKit::instance();
    const LayoutKit& layout = *LayoutKit::instance();
    App* a = new App;

    // set action
    rg_def[0].action = new ActionCallback(App)(a, &App::and);

    a->radiogroup_= new RadioGroup (rg_def, horizontal, 10);

    return session->run_window(
	new ApplicationWindow(
            new Background(
		kit.outset_frame( 
		    layout.vbox(
			layout.margin( 
			    layout.vbox(
				a->radiogroup_->get_glyph(), // get glyph of radiogroup
				layout.vspace(20),
			        layout.hbox(
			            layout.hglue(0.0, fil, 0.0),
				    kit.push_button("set AND",
				        new ActionCallback(App)(a,&App::set)
				    ),
			            layout.hglue(0.0, fil, 0.0),
			            kit.push_button("test OR",
				        new ActionCallback(App)(a,&App::test)
				    ),
			            layout.hglue(0.0, fil, 0.0),
			            kit.push_button("select",
				        new ActionCallback(App)(a,&App::selected)
				    ),
			        )
			    ),
			    20.0
			)

		    )
		),
            kit.background()
	    )
	)
    );
}
@EOF

chmod 640 main.c

echo x - Imakefile
cat >Imakefile <<'@EOF'
#ifdef InObjectCodeDir

Use_libInterViews()
ComplexProgramTarget(test)

MakeObjectFromSrc(main)
MakeObjectFromSrc(radiogroup)

#else

MakeInObjectCodeDir()

#endif
@EOF

chmod 640 Imakefile

exit 0
Newsgroups: comp.windows.interviews