3 * kkconsui dialogbox class
4 * $Id: dialogbox.cc,v 1.14 2003/10/19 23:25:05 konst Exp $
6 * Copyright (C) 1999-2001 by Konstantin Klyagin <k@thekonst.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 #include "dialogbox.h"
27 dialogbox
*dialogbox::it
;
29 dialogbox::dialogbox(): first(true), menu(0), tree(0), window(0), bar(0), browser(0),
30 otherkeys(0), idle(0) {
31 freemenu
= freetree
= freewindow
= freebar
= freebrowser
= false;
34 dialogbox::~dialogbox() {
35 if(menu
&& freemenu
) delete menu
;
36 if(tree
&& freetree
) delete tree
;
37 if(bar
&& freebar
) delete bar
;
38 if(window
&& freewindow
) delete window
;
39 if(browser
&& freebrowser
) delete browser
;
42 void dialogbox::setwindow(textwindow
*neww
, bool fw
) {
47 void dialogbox::setbar(horizontalbar
*newb
, bool fb
) {
52 void dialogbox::setmenu(verticalmenu
*newm
, bool fm
) {
57 void dialogbox::settree(treeview
*newt
, bool ft
) {
62 void dialogbox::setbrowser(textbrowser
*newbr
, bool fbr
) {
67 verticalmenu
*dialogbox::getmenu() {
71 treeview
*dialogbox::gettree() {
75 textwindow
*dialogbox::getwindow() {
79 horizontalbar
*dialogbox::getbar() {
83 textbrowser
*dialogbox::getbrowser() {
87 bool dialogbox::open(int &menuitem
, int &baritem
, void **ref
) {
89 list
<keybarassociation
>::iterator i
;
95 menuitem
= menu
->open();
96 if(ref
) *ref
= menu
->getref(menuitem
-1);
97 ret
= menuitem
|| (menu
->getlastkey() != KEY_ESC
);
100 r
= tree
->open(&menuitem
);
102 ret
= menuitem
|| (tree
->menu
.getlastkey() != KEY_ESC
) || r
;
104 menuitem
= browser
->open();
111 for(fin
= false; !fin
; ) {
112 proceed
= idle
? keypressed() : true;
116 switch(k
= getkey()) {
126 fin
= true; ret
= false;
129 i
= find(it
->kba
.begin(), it
->kba
.end(), k
);
131 if(i
!= it
->kba
.end()) {
133 it
->bar
->item
= i
->baritem
;
139 if((k
= otherkeys(*this, k
)) != -1) {
145 if(idle
) idle(*this);
159 bool dialogbox::open(int &menuitem
) {
161 return open(menuitem
, bi
);
164 bool dialogbox::open() {
166 return open(menuitem
, bi
);
169 void dialogbox::redraw() {
171 if(window
->isopen()) window
->redraw(); else window
->open();
174 if(window
->isbordered()) {
175 window
->separatey(window
->y2
-window
->y1
-2);
178 bar
->setcoords(window
->x2
- (window
->isbordered() ? 1 : 2), window
->y2
-1);
183 int x1
= window
->x1
+ (window
->isbordered() ? 1 : 0);
186 menu
->setcoords(x1
, window
->y1
+1, window
->x2
, bar
? window
->y2
-2 : window
->y2
);
187 menu
->idle
= &menuidle
;
188 menu
->otherkeys
= &menukeys
;
190 tree
->setcoords(x1
, window
->y1
+1, window
->x2
, bar
? window
->y2
-2 : window
->y2
);
192 tree
->menu
.idle
= &menuidle
;
193 tree
->menu
.otherkeys
= &menukeys
;
195 int x2
= window
->x2
- (window
->isbordered() ? 1 : 0);
197 browser
->setcoords(window
->x1
+1, window
->y1
+1, x2
,
198 bar
? window
->y2
-2 : window
->y2
);
201 browser
->idle
= &browseridle
;
202 browser
->otherkeys
= &browserkeys
;
209 void dialogbox::close() {
210 if(window
) window
->close();
213 void dialogbox::clearkeys() {
217 void dialogbox::addkey(int key
, int baritem
) {
218 kba
.push_back(keybarassociation(key
, baritem
));
221 void dialogbox::addautokeys() {
223 vector
<string
>::const_iterator i
;
224 string::const_iterator c
;
227 for(i
= bar
->items
.begin(); i
!= bar
->items
.end(); i
++) {
228 for(c
= i
->begin(); (c
!= i
->end()) && !isupper(*c
); c
++);
229 key
= (c
== i
->end()) ? *i
->begin() : *c
;
230 addkey(tolower(key
), i
-bar
->items
.begin());
231 addkey(toupper(key
), i
-bar
->items
.begin());
236 void dialogbox::menuidle(verticalmenu
&caller
) {
243 select(1, &fds
, 0, 0, 0);
247 void dialogbox::browseridle(textbrowser
&caller
) {
254 select(1, &fds
, 0, 0, 0);
258 int dialogbox::menukeys(verticalmenu
&caller
, int k
) {
259 list
<keybarassociation
>::iterator i
;
274 if(it
->tree
->collapsable
) {
275 int nid
= it
->tree
->getid(it
->tree
->menu
.getpos());
277 if(it
->tree
->isnode(nid
)) {
278 if(it
->tree
->isnodeopen(nid
)) it
->tree
->closenode(nid
);
279 else it
->tree
->opennode(nid
);
281 it
->tree
->menu
.redraw();
286 i
= find(it
->kba
.begin(), it
->kba
.end(), k
);
288 if(i
!= it
->kba
.end()) {
290 it
->bar
->item
= i
->baritem
;
295 return it
->menu
->getpos()+1;
296 } else if(it
->tree
) {
297 return it
->tree
->menu
.getpos()+1;
302 ip
= it
->otherkeys(*it
, k
);
313 int dialogbox::browserkeys(textbrowser
&caller
, int k
) {
314 list
<keybarassociation
>::iterator i
;
330 i
= find(it
->kba
.begin(), it
->kba
.end(), k
);
332 if(i
!= it
->kba
.end()) {
334 it
->bar
->item
= i
->baritem
;
342 ip
= it
->otherkeys(*it
, k
);
353 // ----------------------------------------------------------------------------
355 dialogbox::keybarassociation::keybarassociation(int nkey
, int nbarit
) {
360 bool dialogbox::keybarassociation::operator == (const int akey
) {
364 bool dialogbox::keybarassociation::operator != (const int akey
) {
365 return !(*this == akey
);