1 #include <../../nrnconf.h>
34 #include <IV-look/choice.h>
35 #include <IV-look/dialogs.h>
36 #include <IV-look/fbrowser.h>
37 #include <IV-look/kit.h>
38 #include <InterViews/action.h>
39 #include <InterViews/event.h>
40 #include <InterViews/font.h>
41 #include <InterViews/hit.h>
42 #include <InterViews/input.h>
43 #include <InterViews/layout.h>
44 #include <InterViews/scrbox.h>
45 #include <InterViews/style.h>
46 #include <InterViews/target.h>
47 #include <InterViews/session.h>
48 #include <InterViews/display.h>
49 #include <OS/string.h>
66 class SymChooserImpl {
69 friend class SymBrowserAccept;
70 SymChooserImpl(
int nbrowser);
84 std::string selected_;
85 CopyString last_selected_;
103 double* selected_var();
104 int selected_vector_count();
106 const char* pattern_attribute,
107 const char* default_pattern,
108 const char* caption_attribute,
109 const char* default_caption,
113 void accept_browser();
114 void accept_browser_index(
int);
115 void cancel_browser();
118 bool chdir(
int,
int);
121 class SymBrowserAccept:
public Action {
123 SymBrowserAccept(SymChooserImpl*,
int);
124 virtual ~SymBrowserAccept();
125 virtual void execute();
128 SymChooserImpl* sci_;
132 SymBrowserAccept::SymBrowserAccept(SymChooserImpl* sci,
int browser_index) {
134 browser_index_ = browser_index;
137 SymBrowserAccept::~SymBrowserAccept() {}
139 void SymBrowserAccept::execute() {
140 sci_->accept_browser_index(browser_index_);
149 const char* caption =
"Choose a Variable Name or";
153 Style* style =
new Style(Session::instance()->style());
154 style->attribute(
"caption", caption);
185 Display* d = Session::instance()->default_display();
188 b = sc->post_at_aligned(x, y, 0.0, 0.0);
190 b = sc->post_at_aligned(d->width() / 2, d->height() / 2, .5, .5);
216 declareActionCallback(SymChooserImpl)
217 implementActionCallback(SymChooserImpl)
219 declareFieldEditorCallback(SymChooserImpl)
220 implementFieldEditorCallback(SymChooserImpl)
221 #define SHOW_SECTION 1
229 impl_ =
new SymChooserImpl(nbrowser);
230 SymChooserImpl& fc = *impl_;
251 return impl_->selected_;
255 return impl_->selected_var();
259 return impl_->selected_vector_count();
263 SymChooserImpl& fc = *
impl_;
265 if (!fc.chdir(fc.dir_->path())) {
272 Dialog::dismiss(accept);
273 SymChooserImpl& fc = *
impl_;
274 if (fc.action_ !=
NULL) {
275 fc.action_->execute(
this, accept);
280 SymChooserImpl::SymChooserImpl(
int nbrowser) {
281 nbrowser_ = nbrowser;
285 for (
int i = 0;
i < nbrowser_; ++
i) {
290 SymChooserImpl::~SymChooserImpl() {
302 style_->alias(
"FileChooser");
303 style_->alias(
"Dialog");
304 update_ =
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::build);
305 style_->add_trigger_any(update_);
309 void SymChooserImpl::scfree() {
310 for (
int i = nbrowser_ - 1;
i >= 0; --
i) {
313 delete[] filter_map_;
315 style_->remove_trigger_any(update_);
319 void SymChooserImpl::build() {
321 const LayoutKit& layout = *LayoutKit::instance();
326 s->find_attribute(
"caption", caption);
327 String subcaption(
"Enter Symbol name:");
328 s->find_attribute(
"subcaption", subcaption);
329 String open(
"Accept");
330 s->find_attribute(
"open", open);
331 String close(
"Cancel");
332 s->find_attribute(
"cancel", close);
334 s->find_attribute(
"rows", rows);
335 const Font* f = kit.font();
338 Coord height = rows * (bbox.ascent() + bbox.descent()) + 1.0;
340 if (!
s->find_attribute(
"width", width)) {
341 width = 16 * f->width(
'm') + 3.0;
345 Action* accept =
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::accept_browser);
346 Action* cancel =
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::cancel_browser);
347 editor_ = DialogKit::instance()->field_editor(
348 "",
s,
new FieldEditorCallback(SymChooserImpl)(
this, &SymChooserImpl::editor_accept,
NULL));
350 for (
i = 0;
i < nbrowser_; ++
i) {
353 fchooser_->remove_all_input_handlers();
354 fchooser_->append_input_handler(editor_);
355 for (
i = 0;
i < nbrowser_; ++
i) {
356 fchooser_->append_input_handler(fbrowser_[
i]);
358 fchooser_->next_focus();
360 Glyph* g = layout.vbox();
361 if (caption.length() > 0) {
362 g->append(layout.r_margin(kit.fancy_label(caption), 5.0,
fil, 0.0));
364 if (subcaption.length() > 0) {
365 g->append(layout.r_margin(kit.fancy_label(subcaption), 5.0,
fil, 0.0));
367 g->append(layout.vglue(5.0, 0.0, 2.0));
369 g->append(layout.vglue(5.0, 0.0, 2.0));
370 g->append(makeshowmenu());
371 g->append(layout.vglue(15.0, 0.0, 12.0));
374 for (
i = 0;
i < nbrowser_; ++
i) {
375 b = layout.hbox(layout.vcenter(kit.inset_frame(layout.margin(
376 layout.natural_span(fbrowser_[
i], width, height), 1.0)),
379 kit.vscroll_bar(fbrowser_[
i]->adjustable()));
383 g->append(layout.vspace(15.0));
384 if (
s->value_is_on(
"filter")) {
386 SymChooserImpl)(
this, &SymChooserImpl::filter_accept,
NULL);
387 filter_ = add_filter(
s,
"filterPattern",
"",
"filterCaption",
"Filter:", g, action);
388 if (
s->value_is_on(
"directoryFilter")) {
389 directory_filter_ = add_filter(
s,
390 "directoryFilterPattern",
392 "directoryFilterCaption",
397 directory_filter_ =
NULL;
401 directory_filter_ =
NULL;
403 g->append(layout.hbox(layout.hglue(10.0),
404 layout.vcenter(kit.default_button(open, accept)),
405 layout.hglue(10.0, 0.0, 5.0),
406 layout.vcenter(kit.push_button(close, cancel)),
407 layout.hglue(10.0)));
409 fchooser_->body(layout.vcenter(kit.outset_frame(layout.margin(g, 5.0)), 1.0));
414 Menu* SymChooserImpl::makeshowmenu() {
416 Menu* mb =
k.menubar();
419 Menu* mp =
k.pulldown();
424 mi->action(
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::show_all));
426 mi->state()->set(TelltaleState::is_chosen,
true);
429 mi->action(
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::show_var));
433 mi->action(
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::show_objref));
437 mi->action(
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::show_objid));
441 mi->action(
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::show_sec));
444 mi->state()->set(TelltaleState::is_chosen,
true);
447 mi->action(
new ActionCallback(SymChooserImpl)(
this, &SymChooserImpl::show_pysec));
460 void SymChooserImpl::show_all() {
463 void SymChooserImpl::show_var() {
466 void SymChooserImpl::show_objref() {
469 void SymChooserImpl::show_objid() {
472 void SymChooserImpl::show_sec() {
475 void SymChooserImpl::show_pysec() {
479 void SymChooserImpl::clear(
int bindex) {
480 for (
int bi = bindex; bi < nbrowser_; ++bi) {
486 b.remove_selectable(0);
496 void SymChooserImpl::load(
int bindex) {
498 Browser& b = *fbrowser_[bindex];
502 const LayoutKit& layout = *LayoutKit::instance();
503 int dircount = d.
count();
504 delete[] filter_map_;
505 int*
index =
new int[dircount];
508 for (
int i = 0;
i < dircount;
i++) {
509 const String& f = d.
name(
i).c_str();
511 if ((is_dir && filtered(f, directory_filter_)) || (!is_dir && filtered(f, filter_))) {
515 name = layout.hbox(
name, kit.label(
"_"));
517 name = layout.hbox(
name, kit.label(
"."));
523 b.append_selectable(
t);
524 b.append(
new ChoiceItem(
t, label, kit.bright_inset_frame(label)));
531 fbrowser_[bindex]->refresh();
532 editor_->field(d.
path().c_str());
537 const char* pattern_attribute,
538 const char* default_pattern,
539 const char* caption_attribute,
540 const char* default_caption,
543 String pattern(default_pattern);
544 s->find_attribute(pattern_attribute, pattern);
545 String caption(default_caption);
546 s->find_attribute(caption_attribute, caption);
547 FieldEditor* e = DialogKit::instance()->field_editor(pattern,
s, action);
548 fchooser_->append_input_handler(e);
550 LayoutKit& layout = *LayoutKit::instance();
551 body->append(layout.hbox(layout.vcenter(kit.fancy_label(caption), 0.5),
553 layout.vcenter(e, 0.5)));
554 body->append(layout.vspace(10.0));
562 const String*
s = e->text();
563 if (
s ==
NULL ||
s->length() == 0) {
569 void SymChooserImpl::accept_browser_index(
int bindex) {
570 int i = int(fbrowser_[bindex]->selected());
576 const String& path = dir->
path().c_str();
577 const String&
name = dir->
name(
i).c_str();
579 int length = path.length() +
name.length();
580 auto const tmp_len = length + 2;
581 char* tmp =
new char[tmp_len];
583 tmp, tmp_len,
"%.*s%.*s", path.length(), path.string(),
name.length(),
name.string());
585 last_selected_ = tmp;
587 selected_ = editor_->text()->string();
589 if (chdir(bindex,
i)) {
590 fchooser_->focus(editor_);
596 browser_index_ = bindex;
602 double* SymChooserImpl::selected_var() {
603 if (last_index_ != -1 && selected_ == last_selected_.string()) {
611 int SymChooserImpl::selected_vector_count() {
612 if (last_index_ != -1 && selected_ == last_selected_.string()) {
620 void SymChooserImpl::accept_browser() {
621 int bi = browser_index_;
622 int i = int(fbrowser_[bi]->selected());
624 editor_accept(editor_);
628 const String& path = dir_[bi]->path().c_str();
629 const String&
name = dir_[bi]->name(
i).c_str();
630 int length = path.length() +
name.length();
631 char* tmp =
new char[length + 1];
633 tmp, length + 1,
"%.*s%.*s", path.length(), path.string(),
name.length(),
name.string());
636 selected_ = editor_->text()->string();
637 if (dir_[bi]->is_directory(
i)) {
639 fchooser_->focus(editor_);
644 fchooser_->dismiss(
true);
649 void SymChooserImpl::cancel_browser() {
651 fchooser_->dismiss(
false);
654 void SymChooserImpl::editor_accept(
FieldEditor* e) {
655 if (
int i = dir_[browser_index_]->
index(e->text()->string());
i >= 0) {
656 if (!chdir(browser_index_,
i)) {
657 selected_ = dir_[browser_index_]->name(
i);
658 fchooser_->dismiss(
true);
662 selected_ = e->text()->string();
663 fchooser_->dismiss(
true);
672 bool SymChooserImpl::chdir(
int bindex,
int index) {
673 if (dir_[bindex]->is_directory(
index)) {
676 if (dir_[bindex]->obj(
index)) {
684 dir_[bindex]->
object(),
685 dir_[bindex]->symbol(
index),
686 dir_[bindex]->array_index(
index));
689 if (bi > nbrowser_ - 1) {
#define FieldEditorAction
static MenuItem * radio_menu_item(TelltaleGroup *, const char *)
virtual void unref() const
virtual void execute(SymChooser *, bool accept)
virtual ~SymChooserAction()
virtual void dismiss(bool)
virtual double * selected_var()
virtual const std::string & selected() const
virtual int selected_vector_count()
SymChooser(SymDirectory *, WidgetKit *, Style *, SymChooserAction *=NULL, int nbrowser=3)
virtual int count() const
virtual double * variable(int index)
virtual int whole_vector(int index)
static bool match(const std::string &name, const std::string &pattern)
Symbol * symbol(int index) const
SymDirectory * newsymdir(int index)
virtual const std::string & name(int index) const
virtual const std::string & path() const
virtual bool is_directory(int index) const
void class2oc(const char *, ctor_f *cons, dtor_f *destruct, Member_func *, Member_ret_obj_func *, Member_ret_str_func *)
void hoc_assign_str(char **cpp, const char *buf)
Symbol * hoc_lookup(const char *)
char ** hoc_pgargstr(int narg)
#define TRY_GUI_REDIRECT_ACTUAL_DOUBLE(name, obj)
#define TRY_GUI_REDIRECT_NO_RETURN(name, obj)
#define TRY_GUI_REDIRECT_OBJ(name, obj)
int const size_t const size_t n
static PyObject * is_pysec(NPySecObj *self)
static double text(void *v)
static Member_func members[]
static double srun(void *v)
static void * scons(Object *)
static void sdestruct(void *v)
bool nrn_spec_dialog_pos(Coord &x, Coord &y)
true if Style 'dialog_spec_position: on' and fills x,y with dialog_left_position and dialog_bottom_po...