campo-sirio/ba/ba0.cpp
guy bb4279849b Richiesta ditte anche nella ricerca veloce
git-svn-id: svn://10.65.10.50/trunk@444 c028cbd2-c16b-5b4b-a496-9718f37d4682
1994-10-24 15:00:02 +00:00

387 lines
7.7 KiB
C++
Executable File

#include <applicat.h>
#include <execp.h>
#include <mask.h>
#include <progind.h>
#include <utility.h>
#include <urldefid.h>
#if XVT_OS == XVT_OS_WIN
extern "C"
{
#include <cpb.h>
}
#include <dos.h>
#else
#include <dirent.h>
#endif
#include "ba0.h"
///////////////////////////////////////////////////////////
// Picture Mask
///////////////////////////////////////////////////////////
class TPicture_mask : public TMask
{
static short _id;
static PICTURE _picture;
protected:
virtual void handler(WINDOW win, EVENT* ep);
void set_picture(short id);
public:
TPicture_mask(const char* name, int dx, int dy, short picture_id);
void reset();
};
short TPicture_mask::_id = 0;
PICTURE TPicture_mask::_picture = 0L;
void TPicture_mask::set_picture(short id)
{
if (id != _id)
{
_id = id;
#if XVT_OS == XVT_OS_WIN
if (_picture) picture_free(_picture);
_picture = cpb_picture_load(id);
#endif
}
}
void TPicture_mask::reset()
{
#if XVT_OS == XVT_OS_WIN
if (_picture)
{
picture_free(_picture);
_picture = 0L;
_id = 0;
}
#endif
}
TPicture_mask::TPicture_mask(const char* name, int dx, int dy, short pic)
: TMask(name, 1, dx, dy)
{
set_picture(pic);
}
void TPicture_mask::handler(WINDOW win, EVENT* ep)
{
TMask::handler(win, ep);
if (ep->type == E_UPDATE)
{
#if XVT_OS == XVT_OS_WIN
if (_picture)
cpb_win_picture_draw_at(win, _picture, 1, CHARY<<1);
else
#endif
{
const int max = 16;
for (int i = 0; i < max; i++)
{
TTemp_window w(win);
w.rect(i*2, i, max-i*2, max-i);
}
}
}
}
///////////////////////////////////////////////////////////
// Menu application
///////////////////////////////////////////////////////////
class TMenu_application : public TApplication
{
const char* _name;
enum { MAXLEVEL = 1024 };
int _first[MAXLEVEL];
TArray _menu; // TAG|DESCRIPTION|ACTION
int _level, _max;
bool _ditta_asked;
static int _last_button;
static bool _find_button;
protected:
void test_temp() const;
void load_menu();
int do_level();
int find_menu(const char* s) const;
virtual bool create();
virtual bool menu(MENU_TAG m);
static bool menu_item_handler(TMask_field&f, KEY k);
static bool menu_find_handler(TMask_field&f, KEY k);
public:
TMenu_application(const char* name) : _name(name), _ditta_asked(FALSE) {}
};
int TMenu_application::_last_button = 0;
bool TMenu_application::_find_button = FALSE;
inline TMenu_application& app()
{ return (TMenu_application&)main_app(); }
void TMenu_application::load_menu()
{
TScanner s(_name);
_max = -1;
while (s.line().not_empty())
{
TToken_string* ts = new TToken_string(s.token());
int l = ts->get_int();
if (l < _max)
{
error_box("Item of level %d while %d was expected)", l, _max);
l = _max;
}
if (l > _max)
{
if (l >= MAXLEVEL)
{
error_box("Too many menu levels: %d", l);
l = _max;
}
_first[_max = l] = _menu.items();
}
_menu.add(ts);
}
_first[++_max] = _menu.items();
}
int TMenu_application::find_menu(const char* s) const
{
TString80 str(s); str.upper();
int found = -1;
for (int i = 0; i < _menu.items(); i++)
{
TToken_string& l = (TToken_string&)_menu[i];
const int m = l.get_int(0);
if (m != _level)
{
TString80 v(l.get()); v.upper();
if (v.find(str) >= 0)
{
found = i;
if (isalpha(l.get_char())) break;
}
}
}
return found;
}
bool TMenu_application::menu_item_handler(TMask_field&f, KEY k)
{
if (k == K_SPACE)
{
_last_button = f.dlg();
_find_button = FALSE;
f.mask().stop_run(K_AUTO_ENTER);
}
return TRUE;
}
bool TMenu_application::menu_find_handler(TMask_field&f, KEY k)
{
if (k == K_TAB && f.focusdirty())
{
const TString& v = f.get();
if (v.not_empty())
{
_last_button = app().find_menu(v);
if (_last_button >= 0)
{
_find_button = TRUE;
f.mask().stop_run(K_AUTO_ENTER);
}
else
{
beep();
return FALSE;
}
}
}
return TRUE;
}
int TMenu_application::do_level()
{
const int first = _first[_level];
const int last = _first[_level+1];
TToken_string& row = (TToken_string&)_menu[first];
const TString80 head(row.get(1));
const int width = 72;
const int height = 18;
const int bwidth = 20;
const int x = width-bwidth-12;
const short pic = BA0_PICTURE+(short)row.get_int();
TPicture_mask menu(head, width, height, pic);
int y = 1;
#if XVT_OS == XVT_OS_WIN
TString16 t(format("#%d", BMP_STOPREC));
#else
TString16 t;
#endif
for (int i = first+1; i < last; i++, y++)
{
TToken_string& row = (TToken_string&)_menu[i];
TString80 item(row.get(1));
if (isdigit(*row.get())) item << "...";
menu.add_static(-1, 0, item, x+4, y);
menu.add_button(100+y, 0, t, x, y, 1, 1);
menu.set_handler(100+y, menu_item_handler);
}
menu.add_static(-1, 0, "Cerca", 1,-3);
menu.add_string(99, 0, "", -12, -3, 50, "", bwidth+1);
menu.set_handler(99, menu_find_handler);
t = first ? "Menu precedente" : "Fine";
menu.add_button(first ? DLG_CANCEL : DLG_QUIT, 0, t, -22, -1, bwidth, 2);
if (first)
menu.add_button(DLG_QUIT, 0, "Fine", -12, -1, bwidth, 2);
if (_find_button && _last_button > first)
menu.first_focus(100+_last_button-first);
_last_button = _find_button = 0;
const int k = menu.run();
int m = 0;
switch (k)
{
case K_ESC:
m = -1; break;
case K_QUIT:
menu.reset();
m = -2; break;
default:
if (_find_button)
m = -1;
else
m = first+_last_button-100;
break;
}
return m;
}
void TMenu_application::test_temp() const
{
TFilename name; name.tempdir();
name << "/" << "ci*";
#if XVT_OS == XVT_OS_WIN
struct _find_t f;
if (_dos_findfirst(name, _A_NORMAL, &f) == 0 &&
yesno_box("Si desidera cancellare i file temporanei?"))
{
TIndwin pi(40, "Cancellazione file temporanei residui", FALSE, FALSE);
TFilename dir; dir.tempdir();
do
{
name = dir;
name << "/" << f.name;
remove(name);
} while (_dos_findnext(&f) == 0);
}
#endif
}
bool TMenu_application::create()
{
test_temp();
load_menu();
dispatch_e_menu(BAR_ITEM(1));
return TRUE;
}
bool TMenu_application::menu(MENU_TAG)
{
int refarray[256];
memset(refarray, 0, sizeof(refarray));
int i = 0;
_level = 0;
while (i >= 0)
{
const int m = do_level();
if (m >= 0)
{
TToken_string& row = (TToken_string&)_menu[m];
const TFilename option(row.get(2));
if (option.not_empty())
{
const int l = atoi(option);
if (l > 0)
{
const char* flags = row.get();
if (flags && strchr(flags, 'F') != NULL)
{
set_firm();
_ditta_asked = TRUE;
}
refarray[i++] = _level;
if (l < _max) _level = l;
}
else
{
TExternal_app a(option);
const TString& module = option.left(2);
if (module == "ba") _ditta_asked = FALSE; else
if (module == "cg" && !_ditta_asked)
{
set_firm();
_ditta_asked = TRUE;
}
a.run();
}
}
}
else
{
if (m < -1) break;
if (_find_button)
{
TToken_string& row = (TToken_string&)_menu[_last_button];
_level = row.get_int(0);
}
else
{
_level = (i > 0) ? refarray[--i] : 0;
if (_level == 0) i = 0;
}
}
}
return FALSE;
}
int main(int argc, char** argv)
{
const char* menu = (argc < 2) ? "prassi.mnu" : argv[1];
TMenu_application ma(menu);
ma.run(argc, argv, "Menu Principale");
return TRUE;
}