Article View: comp.windows.interviews
Article #438StringChooser + IV3.1beta2 problem
From: Woersinger Diplomand von ASK
Date: Mon, 14 Sep 1992 16:05
Date: Mon, 14 Sep 1992 16:05
577 lines
18075 bytes
18075 bytes
hi,
some weeks ago, there was an article form Igor Metz, containing sources
for a Dialog, which can display some strings and the user can select one
(similar to FileChooser).
The class was called StringChooser. (see below)
I use this class in my application and it's running well with 3.1beta.
BUT running with 3.1beta2, no strings are displayed.
Is there anyone who has the same problem?
Thanx
Rolf-Martin Woersinger Akademische Software Kooperation (ASK)
woersi@lodsun1.lod.uni-karlsruhe.de Karlsruhe, Germany +49-721-695639
This version is to run with 3.1alpha or 3.1beta.
To run with 3.1beta2, creator of FileBrowser in StringChooserImpl::build()
needs also ActionCallBack for cancel-button:
strbrowser_ = new FileBrowser(kit_, accept, cancel);
______
#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 07/20/1992 09:29 UTC by metz@donald
# Source directory /var/tmp/metz
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 2736 -rw-r----- versuch.C
# 3588 -rw-r----- strchooser.h
# 7500 -rw-r----- strchooser.C
#
# ============= versuch.C ==============
if test -f 'versuch.C' -a X"$1" != X"-c"; then
echo 'x - skipping versuch.C (File already exists)'
else
echo 'x - extracting versuch.C (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'versuch.C' &&
#include <IV-look/kit.h>
#include <InterViews/display.h>
#include <InterViews/background.h>
#include <InterViews/event.h>
#include <InterViews/color.h>
#include <InterViews/input.h>
#include <InterViews/session.h>
#include <InterViews/style.h>
#include <InterViews/window.h>
#include <OS/string.h>
X
#include <iostream.h>
X
#include "strchooser.h"
X
class Versuch: public InputHandler {
public:
X Versuch(Glyph *g, Style* s);
X virtual ~Versuch();
X
X void setWindow(Window *w) { window_ = w; }
X virtual void press(const Event &e);
protected:
X Window *window_;
};
X
Versuch::Versuch(Glyph *g, Style* s)
: InputHandler(g, s) {}
X
Versuch::~Versuch() {}
X
void Versuch::press(const Event &e) {
X if (e.pointer_button() == Event::right) {
X StringChooser *dialog = new StringChooser(WidgetKit::instance(),
X Session::instance()->style());
X Resource::ref(dialog);
X
X dialog->add(String("Andreas Spichiger"));
X dialog->add(String("Andreas Ueltschi"));
X dialog->add(String("Bruno Grossniklaus"));
X dialog->add(String("Gerhard Jaeger"));
X dialog->add(String("Hansjuerg Mey"));
X dialog->add(String("Hanspeter Bieri"));
X dialog->add(String("Hanspeter Blau"));
X dialog->add(String("Heike Horn"));
X dialog->add(String("Hermann Hueni"));
X dialog->add(String("Horst Bunke"));
X dialog->add(String("Igor Metz"));
X dialog->add(String("Jens Grabowski"));
X dialog->add(String("Jiri Dvorak"));
X dialog->add(String("Karsten Decker"));
X dialog->add(String("Markus Marzetta"));
X dialog->add(String("Rene Rehmann,T502,031658964,031559316"));
X dialog->add(String("Robert Owen"));
X dialog->add(String("Stefan Leue"));
X dialog->add(String("Urs Meier"));
X dialog->add(String("Xiao-Yi Jiang"));
X
X dialog->post_for(window_);
X const String* sel = dialog->selected();
X if (sel != nil) {
X cout << "Selected = " << sel->string() << endl;
X } else {
X cout << "Nothing selected" << endl;
X }
X Resource::unref(dialog);
X }
}
X
int main(int argc, char* argv[]) {
X
X Session* session = new Session("simple", argc, argv);
X Style* style = session->style() ;
X WidgetKit* kit = WidgetKit::instance() ;
X Display* d = session->default_display();
X
X Versuch *v = new Versuch(new Background(kit->label("Hello World"),
X kit->background()),
X style);
X Window *w = new ApplicationWindow(v);
X v->setWindow(w);
X w->place(d->width() / 2, d->height() / 2);
X w->align(0.5, 0.5);
X d->ring_bell(0);
X session->run_window(w);
X cout << "Finito" << endl;
X delete w;
X delete session;
X return 0;
}
X
// CC -c -I/usr/local/IV3.0/include -I../incl ../src/versuch.C
// CC -o versuch versuch.o strchooser.o strbrowser.o -L/usr/local/IV3.0/lib/SUN4 -lIV -lX11 -lm
SHAR_EOF
chmod 0640 versuch.C ||
echo 'restore of versuch.C failed'
Wc_c="`wc -c < 'versuch.C'`"
test 2736 -eq "$Wc_c" ||
echo 'versuch.C: original size 2736, current size' "$Wc_c"
fi
# ============= strchooser.h ==============
if test -f 'strchooser.h' -a X"$1" != X"-c"; then
echo 'x - skipping strchooser.h (File already exists)'
else
echo 'x - extracting strchooser.h (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'strchooser.h' &&
// This is -*- C++ -*- Code
X
/*
X * Copyright (c) 1987, 1988, 1989, 1990, 1991 Stanford University
X * Copyright (c) 1991 Silicon Graphics, Inc.
X * Copyright (c) 1992 University of Berne
X *
X * Permission to use, copy, modify, distribute, and sell this software and
X * its documentation for any purpose is hereby granted without fee, provided
X * that (i) the above copyright notices and this permission notice appear in
X * all copies of the software and related documentation, and (ii) the names of
X * Stanford and Silicon Graphics may not be used in any advertising or
X * publicity relating to the software without the specific, prior written
X * permission of Stanford and Silicon Graphics.
X *
X * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
X * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
X * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
X *
X * IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
X * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
X * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
X * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
X * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
X * OF THIS SOFTWARE.
X */
X
/*
X * $Header$
X * $Log$
X */
X
/*
X * StringChooser -- select a String
X */
X
#ifndef _strchooser_h
#define _strchooser_h
X
#include <InterViews/dialog.h>
#include <InterViews/resource.h>
X
#include <InterViews/_enter.h>
X
class StringChooser;
class StringChooserImpl;
class String;
class WidgetKit;
X
class StringChooserAction : public Resource {
protected:
X StringChooserAction();
X virtual ~StringChooserAction();
public:
X virtual void execute(StringChooser*, boolean accept);
};
X
#if defined(__STDC__) || defined(__ANSI_CPP__)
#define StringChooserCallback(T) T##_StringChooserCallback
#define StringChooserMemberFunction(T) T##_StringChooserMemberFunction
#else
#define StringChooserCallback(T) T/**/_StringChooserCallback
#define StringChooserMemberFunction(T) T/**/_StringChooserMemberFunction
#endif
X
#define declareStringChooserCallback(T) \
typedef void (T::*StringChooserMemberFunction(T))(StringChooser*, boolean); \
class StringChooserCallback(T) : public StringChooserAction { \
public: \
X StringChooserCallback(T)(T*, StringChooserMemberFunction(T)); \
X virtual ~StringChooserCallback(T)(); \
\
X virtual void execute(StringChooser*, boolean accept); \
private: \
X T* obj_; \
X StringChooserMemberFunction(T) func_; \
};
X
#define implementStringChooserCallback(T) \
StringChooserCallback(T)::StringChooserCallback(T)( \
X T* obj, StringChooserMemberFunction(T) func \
) { \
X obj_ = obj; \
X func_ = func; \
} \
\
StringChooserCallback(T)::~StringChooserCallback(T)() { } \
\
void StringChooserCallback(T)::execute(StringChooser* f, boolean accept) { \
X (obj_->*func_)(f, accept); \
}
X
// Properties defined for this class
//
// Property Default value
// -----------------------------------------------
// StringChooser*caption "Select an item:"
// StringChooser*subcaption ""
// StringChooser*ok "OK"
// StringChooser*cancel "Cancel"
// StringChooser*rows 10
// StringChooser*width is computed
X
X
class StringChooser : public Dialog {
public:
X StringChooser(WidgetKit*, Style*, StringChooserAction* = nil);
X virtual ~StringChooser();
X
X virtual void add(const String& s);
X
X virtual const String* selected() const;
X virtual void dismiss(boolean);
private:
X StringChooserImpl* impl_;
};
X
#include <InterViews/_leave.h>
X
#endif
SHAR_EOF
chmod 0640 strchooser.h ||
echo 'restore of strchooser.h failed'
Wc_c="`wc -c < 'strchooser.h'`"
test 3588 -eq "$Wc_c" ||
echo 'strchooser.h: original size 3588, current size' "$Wc_c"
fi
# ============= strchooser.C ==============
if test -f 'strchooser.C' -a X"$1" != X"-c"; then
echo 'x - skipping strchooser.C (File already exists)'
else
echo 'x - extracting strchooser.C (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'strchooser.C' &&
// This is -*- C++ -*-
X
/*
X * Copyright (c) 1987, 1988, 1989, 1990, 1991 Stanford University
X * Copyright (c) 1991 Silicon Graphics, Inc.
X * Copyright (c) 1992 University of Berne
X *
X * Permission to use, copy, modify, distribute, and sell this software and
X * its documentation for any purpose is hereby granted without fee, provided
X * that (i) the above copyright notices and this permission notice appear in
X * all copies of the software and related documentation, and (ii) the names of
X * Stanford and Silicon Graphics may not be used in any advertising or
X * publicity relating to the software without the specific, prior written
X * permission of Stanford and Silicon Graphics.
X *
X * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
X * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
X * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
X *
X * IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
X * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
X * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
X * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
X * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
X * OF THIS SOFTWARE.
X */
X
/*
X * $Id$
X * $Log$
X *
X */
X
/*
X * StringChooser -- select a String
X */
X
#include <IV-look/fbrowser.h>
#include "strchooser.h"
X
#include <IV-look/choice.h>
#include <IV-look/dialogs.h>
#include <IV-look/kit.h>
#include <InterViews/action.h>
#include <InterViews/event.h>
#include <InterViews/font.h>
#include <InterViews/hit.h>
#include <InterViews/input.h>
#include <InterViews/layout.h>
#include <InterViews/scrbox.h>
#include <InterViews/style.h>
#include <InterViews/target.h>
#include <OS/string.h>
#include <OS/list.h>
X
#include <assert.h>
X
declarePtrList(ListOfString,CopyString);
implementPtrList(ListOfString,CopyString);
X
class StringChooserImpl {
private:
X friend class StringChooser;
X
X WidgetKit* kit_;
X StringChooser* strchooser_;
X FileBrowser* strbrowser_;
X StringChooserAction* action_;
X const String* selected_;
X Style* style_;
X Action* update_;
X ListOfString strings_;
X
X StringChooserImpl();
X ~StringChooserImpl();
X
X void init(StringChooser*, Style*, StringChooserAction*);
X void add(const String& s);
X void build();
X void clear();
X void accept_browser();
X void cancel_browser();
};
X
declareActionCallback(StringChooserImpl)
implementActionCallback(StringChooserImpl)
X
StringChooser::StringChooser(
X WidgetKit* kit, Style* s, StringChooserAction* a
) : Dialog(nil, s) {
X impl_ = new StringChooserImpl;
X StringChooserImpl& sc = *impl_;
X sc.kit_ = kit;
X sc.init(this, s, a);
}
X
StringChooser::~StringChooser() {
X delete impl_;
}
X
void StringChooser::add(const String& s) {
X impl_->add(s);
}
X
const String* StringChooser::selected() const {
X return impl_->selected_;
}
X
void StringChooser::dismiss(boolean accept) {
X Dialog::dismiss(accept);
X StringChooserImpl& sc = *impl_;
X if (sc.action_ != nil) {
X sc.action_->execute(this, accept);
X }
}
X
/** class StringChooserImpl **/
StringChooserImpl::StringChooserImpl()
: kit_(0), strchooser_(0), strbrowser_(0), action_(0), selected_(0),
X style_(0), update_(0) { }
X
StringChooserImpl::~StringChooserImpl() {
X Resource::unref(strbrowser_);
X Resource::unref(action_);
X style_->remove_trigger_any(update_);
X Resource::unref(update_);
X Resource::unref(style_);
X
X for (int i=0; i < strings_.count(); i++) {
X delete strings_.item(i);
X }
X strings_.remove_all();
}
X
void StringChooserImpl::init(
X StringChooser* chooser, Style* s, StringChooserAction* a
) {
X strchooser_ = chooser;
X Resource::ref(a);
X action_ = a;
X style_ = new Style(s);
X Resource::ref(style_);
X style_->alias("StringChooser");
X style_->alias("Dialog");
X update_ = new ActionCallback(StringChooserImpl)(
X this, &StringChooserImpl::build
X );
X Resource::ref(update_);
X style_->add_trigger_any(update_);
X build();
}
X
void StringChooserImpl::add(const String& s) {
X const LayoutKit& layout = *LayoutKit::instance();
X
X Glyph* name = kit_->label(s);
X Glyph* label = new Target(layout.h_margin(name,
X 3.0, 0.0, 0.0, 15.0, fil, 0.0),
X TargetPrimitiveHit);
X TelltaleState* t = new TelltaleState(TelltaleState::is_enabled);
X strbrowser_->append_selectable(t);
X strbrowser_->append(new ChoiceItem(t, label,
X kit_->bright_inset_frame(label)));
X strbrowser_->refresh();
X CopyString *str = new CopyString(s);
X strings_.append(str);
X // NB. the destructor will delete the strings allocated here
}
X
void StringChooserImpl::build() {
X WidgetKit& kit = *kit_;
X const LayoutKit& layout = *LayoutKit::instance();
X Style* s = style_;
X kit.push_style();
X kit.style(s);
X String caption("");
X s->find_attribute("caption", caption);
X String subcaption("Select an item:");
X s->find_attribute("subcaption", subcaption);
X String ok("OK");
X s->find_attribute("ok", ok);
X String close("Cancel");
X s->find_attribute("cancel", close);
X long rows = 10;
X s->find_attribute("rows", rows);
X const Font* f = kit.font();
X FontBoundingBox bbox;
X f->font_bbox(bbox);
X Coord height = rows * (bbox.ascent() + bbox.descent()) + 1.0;
X Coord width;
X if (!s->find_attribute("width", width)) {
X width = 16 * f->width('m') + 3.0;
X }
X
X Action* accept = new ActionCallback(StringChooserImpl)(
X this, &StringChooserImpl::accept_browser
X );
X Action* cancel = new ActionCallback(StringChooserImpl)(
X this, &StringChooserImpl::cancel_browser
X );
X strbrowser_ = new FileBrowser(kit_, accept);
X
X Resource::ref(strbrowser_);
X strchooser_->remove_all_input_handlers();
X strchooser_->append_input_handler(strbrowser_);
X strchooser_->next_focus();
X
X Glyph* g = layout.vbox();
X if (caption.length() > 0) {
X g->append(layout.r_margin(kit.fancy_label(caption), 5.0, fil, 0.0));
X }
X if (subcaption.length() > 0) {
X g->append(layout.r_margin(kit.fancy_label(subcaption), 5.0, fil, 0.0));
X }
X g->append(layout.vglue(15.0, 0.0, 12.0));
X g->append(
X layout.hbox(
X layout.vcenter(
X kit.inset_frame(
X layout.margin(
X layout.natural_span(strbrowser_, width, height), 1.0
X )
X ),
X 1.0
X ),
X layout.hspace(4.0),
X kit.vscroll_bar(strbrowser_->adjustable())
X )
X );
X g->append(layout.vspace(15.0));
X g->append(layout.hbox(layout.hglue(10.0),
X layout.vcenter(kit.push_button(ok, accept)),
X layout.hglue(10.0, 0.0, 5.0),
X layout.vcenter(kit.push_button(close, cancel)),
X layout.hglue(10.0)
X )
X );
X
X strchooser_->body(
X layout.vcenter(kit.outset_frame(layout.margin(g, 5.0)), 1.0)
X );
X kit.pop_style();
}
X
void StringChooserImpl::clear() {
X Browser& b = *strbrowser_;
X b.select(-1);
X GlyphIndex n = b.count();
X for (GlyphIndex i = 0; i < n; i++) {
X b.remove_selectable(0);
X b.remove(0);
X }
}
X
void StringChooserImpl::accept_browser() {
X int i = int(strbrowser_->selected());
X if (i == -1) {
X return;
X }
X assert(0<= i && i < strings_.count());
X selected_ = strings_.item(i);
X strchooser_->dismiss(true);
}
X
void StringChooserImpl::cancel_browser() {
X selected_ = nil;
X strchooser_->dismiss(false);
}
X
X
/** class StringChooserAction **/
X
StringChooserAction::StringChooserAction() { }
StringChooserAction::~StringChooserAction() { }
void StringChooserAction::execute(StringChooser*, boolean) { }
SHAR_EOF
chmod 0640 strchooser.C ||
echo 'restore of strchooser.C failed'
Wc_c="`wc -c < 'strchooser.C'`"
test 7500 -eq "$Wc_c" ||
echo 'strchooser.C: original size 7500, current size' "$Wc_c"
fi
exit 0
---
Igor Metz <metz@iam.unibe.ch>
Institut fuer Informatik und angew. Mathematik, Universitaet Bern, Switzerland.
Phone: (0041) 31 65 49 90 Fax: (0041) 31 65 39 65
Technical Details
Message-ID:
<9209141605.AA21013@lodsun1.lod.uni-karlsruhe.de>Path:
arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!LODSUN1.LOD.UNI-KARLSRUHE.DE!woersiShow Full Headers
Complete Article Headers
Newsgroups: comp.windows.interviews Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!LODSUN1.LOD.UNI-KARLSRUHE.DE!woersi From: woersi@LODSUN1.LOD.UNI-KARLSRUHE.DE (Woersinger Diplomand von ASK) Subject: StringChooser + IV3.1beta2 problem Message-ID: <9209141605.AA21013@lodsun1.lod.uni-karlsruhe.de> Sender: news@shelby.stanford.edu (USENET News System) Organization: Internet-USENET Gateway at Stanford University Date: Mon, 14 Sep 1992 16:05:40 GMT Lines: 580