From: dave@kanga.lcs.mit.edu (David Sternlicht)
Newsgroups: comp.windows.interviews
Subject: styles and triggers
Date: 15 Dec 92 22:09:53 GMT
Organization: mit X consortium

I wanted to see how style triggers work so I wrote this small program
that "changes" a buttons font when it is pressed.
			
				dave sternlicht
				mit X consortium
				dave@expo.lcs.mit.edu

#!/bin/sh
# To extract, remove the header and type "sh filename"
#
# This archive contains the following files:
#
#         ./Imakefile
#         ./main.c
#
if `test ! -s ./Imakefile`
then
echo "writing ./Imakefile"
sed 's/^X//' > ./Imakefile << '\End\Of\File\'
X#ifdef InObjectCodeDir
X
XUse_libInterViews()
XComplexProgramTarget(test)
X
XMakeObjectFromSrc(main)
X
X#else
X
XMakeInObjectCodeDir()
X
X#endif
\End\Of\File\
else
  echo "will not over write ./Imakefile"
fi
if `test ! -s ./main.c`
then
echo "writing ./main.c"
sed 's/^X//' > ./main.c << '\End\Of\File\'
X//
X//  This demo explores the use an InterViews style trigger callback.
X//                         dave sternlicht - mit X consortium.
X//
X
X#include <IV-look/kit.h>
X#include <InterViews/layout.h>
X#include <InterViews/monoglyph.h>
X#include <InterViews/patch.h>
X#include <InterViews/session.h>
X#include <InterViews/style.h>
X#include <InterViews/window.h>
X
Xclass App {
Xpublic:
X    App();
X    ~App();
X    int run(int, char**);
X
Xprivate:
X    MonoGlyph* build(char*); 
X    void set_attr();
X    void trigger();
X
X    WidgetKit* kit_;
X    LayoutKit* layout_;
X    Button* btn_;
X    Patch* patch_;
X    Style* style_;
X    Glyph* vspace_;
X    Glyph* hspace_;
X    int font_cnt;
X    const int num_fonts;
X};    
X
XdeclareActionCallback(App)
XimplementActionCallback(App)
X
XApp::App() : font_cnt(0), num_fonts(5){}
X
XApp::~App(){}
X
Xint App::run(int argc, char** argv) {
X    Session* session = new Session("Resdemo", argc, argv);
X    kit_ = WidgetKit::instance();
X    style_ = kit_->style();
X    style_->add_trigger_any(new ActionCallback(App)(this, &App::trigger));
X    layout_ = LayoutKit::instance();
X    vspace_ = layout_->vglue(18.0, fil, 16.0);
X    hspace_ = layout_->hglue(36.0, fil, 34.0);
X    patch_ = new Patch(build("Push Me"));
X    return session->run_window(
X        new ApplicationWindow(patch_)
X    );
X};
X
Xstatic char *app_fonts[] = {
X    "fixed", "8x13bold", "9x15", "10x20", "lucidasans-italic-10"
X};
X
X//
X// Create a push button with a callback that set an attribute of the style.
X// Note that the use of glue allows the buttons size to change with the
X// the font size.
X//
X
XMonoGlyph* App::build(char* str) {
X    return kit_->inset_frame(
X        layout_->vbox(
X            vspace_,
X            layout_->hbox(
X                hspace_,
X                btn_ = kit_->push_button(
X	            str, new ActionCallback(App)(this, &App::set_attr)
X        	),
X                hspace_
X            ),
X            vspace_,
X        )
X    );
X}
X
X//
X// Set the styles attribute.  Note that this invokes the trigger.
X// We use a patch to force update of the button's allocation and characters.
X//
Xvoid App::set_attr() {
X    style_->attribute("Resdemo*font", app_fonts[font_cnt % num_fonts]);
X
X    patch_->reallocate();
X    patch_->redraw();
X
X    font_cnt++;
X}
X
X//
X// Do the style change.
X//
Xvoid App::trigger() {
X    patch_->body(build("Push Me Again"));
X}
X
X//
X// Minimalistic main...
X//
Xint main(int argc, char** argv) {
X    App* a = new App;
X    return a->run(argc, argv);
X}
\End\Of\File\
else
  echo "will not over write ./main.c"
fi
echo "Finished archive 1 of 1"
exit