campo-sirio/ba/ba0.cpp
alex 3849f63cf0 Patch level : 02.00.303
Files correlati     :
Ricompilazione Demo : [ ]
Commento            :
Ripotata la versione Partners 2.0 patch 302 sul main trunk


git-svn-id: svn://10.65.10.50/trunk@10387 c028cbd2-c16b-5b4b-a496-9718f37d4682
2002-07-03 14:48:48 +00:00

2594 lines
59 KiB
C++
Executable File
Raw Blame History

#include <xinclude.h>
#include <applicat.h>
#include <automask.h>
#include <colors.h>
#include <config.h>
#include <controls.h>
#include <currency.h>
#include <dongle.h>
#include <execp.h>
#include <image.h>
#include <isam.h>
#include <msksheet.h>
#include <os_dep.h>
#include <prefix.h>
#include <progind.h>
#include <stack.h>
#include <tree.h>
#include <utility.h>
#include <urldefid.h>
#include <agasys.h>
#include <nditte.h>
#include "ba0.h"
#include "ba0100a.h"
#include "ba0400a.h"
#define OPTIONS_MENU M_FONT
#define PREFERRED_MENU M_STYLE
#ifdef XVAGA
#define MEN_FILE "bamenu.men"
#else
#define MEN_FILE "baprassi.men"
#endif
///////////////////////////////////////////////////////////
// TPriority_image
///////////////////////////////////////////////////////////
class TTimed_image : public TImage
{
clock_t _last_time;
public:
clock_t touch() { return _last_time = clock(); }
clock_t last_time() const { return _last_time; }
TTimed_image(const char* name) : TImage(name) { touch(); }
virtual ~TTimed_image() { }
};
///////////////////////////////////////////////////////////
// Picture Mask
///////////////////////////////////////////////////////////
class TPicture_mask : public TMask
{
TImage* _image;
TArray _euro;
int _euro_frame;
long _euro_timer;
static TString _last_string;
protected: // TMask
virtual void update();
virtual void handler(WINDOW win, EVENT * ep);
virtual bool on_key(KEY k);
virtual void on_firm_change();
public:
virtual bool stop_run(KEY key);
void set_last_search_string(const char* str) { _last_string = str; }
void set_image(TImage* image);
void reload_images();
TPicture_mask(const char* name, int dx, int dy, TImage* image, int x = -1, int y = -1);
virtual ~TPicture_mask();
};
TString TPicture_mask::_last_string;
void TPicture_mask::set_image(TImage* image)
{
if (image && image->ok())
{
if (image != _image)
{
_image = image;
_image->set_palette(win());
force_update();
}
}
else
_image = NULL;
}
bool TPicture_mask::stop_run(KEY key)
{
if (key == K_FORCE_CLOSE)
key = K_QUIT;
return TWindow::stop_run(key);
}
void TPicture_mask::update()
{
if (_image && _image->ok())
{
RCT cli;
field(DLG_USER).get_rect(cli);
int vy = cli.top;
const TMask_field& fld = field(101);
fld.get_rect(cli);
int vx = cli.left;
if (fld.class_id() == CLASS_TREE_FIELD)
{
vx = -cli.right;
::xvt_vobj_get_client_rect(win(), &cli);
vx += cli.right - 2*CHARX;
vy = cli.bottom - 2*CHARX;
}
const double ratiox = double(vx) / _image->width();
const double ratioy = double(vy) / _image->height();
const double ratio = min(ratiox, ratioy);
const int maxx = int(ratio * _image->width());
const int maxy = int(ratio * _image->height());
int x = 1;
int y = 1;
if (fld.class_id() == CLASS_TREE_FIELD)
{
x = cli.right - maxx;
y = (cli.bottom - maxy) / 2;
}
RCT dst;
::xvt_rect_set(&dst, x, y, x+maxx-1, y+maxy-1);
if (::xvt_dwin_is_update_needed(win(), &dst))
_image->draw(win(), dst);
}
}
bool TPicture_mask::on_key(KEY k)
{
switch (k)
{
case K_F3:
case K_F8:
set(DLG_USER, _last_string, TRUE);
return TRUE;
case K_ENTER:
case K_UP:
case K_DOWN:
case K_LEFT:
case K_RIGHT:
if (focus_field().is_kind_of(CLASS_TREE_FIELD))
{
TTree_field& tf = (TTree_field&)focus_field();
return tf.win().on_key(k);
}
break;
default:
break;
}
return TMask::on_key(k);
}
void TPicture_mask::handler(WINDOW win, EVENT* ep)
{
switch (ep->type)
{
case E_TIMER:
if (ep->v.timer.id == _euro_timer && _euro.items())
{
const TImage& euro = (TImage&)_euro[_euro_frame];
RCT rct; xvt_vobj_get_client_rect(win, &rct);
int x = rct.right - euro.width() -1;
int y = rct.bottom - euro.height() -1;
xvt_dwin_set_clip(win, &rct);
euro.draw(win, x, y);
_euro_frame++;
if (_euro_frame >= _euro.items())
_euro_frame = 0;
}
break;
default:
break;
}
TMask::handler(win, ep);
}
void TPicture_mask::reload_images()
{
_euro.destroy();
const TString& codval = prefix().firm().codice_valuta();
if (::is_euro_value(codval))
{
for (int i = 0; i < 17; i++)
{
TImage* e = new TImage(30001+i);
e->convert_transparent_color(MASK_BACK_COLOR);
_euro.add(e);
}
}
else
{
TFilename n = codval;
n << "0??.bmp";
TString_array a;
const int frames = list_files(n, a);
if (frames > 0)
{
for (int i = 0; i < frames; i++)
{
TImage* e = new TImage(a.row(i));
e->convert_transparent_color(MASK_BACK_COLOR);
_euro.add(e);
}
}
}
_euro_frame = 0;
}
void TPicture_mask::on_firm_change()
{
reload_images();
force_update();
}
TPicture_mask::TPicture_mask(const char* name, int dx, int dy,
TImage* image, int x, int y)
: TMask(name, 1, dx, dy, x, y)
{
set_image(image);
reload_images();
_euro_timer = xvt_timer_create(win(), 50);
}
TPicture_mask::~TPicture_mask()
{
xvt_timer_destroy(_euro_timer);
}
///////////////////////////////////////////////////////////
// Color Mask
///////////////////////////////////////////////////////////
class TColor_mask : public TAutomask
{
TAssoc_array _color;
protected: // TMask
virtual void update();
virtual bool stop_run(KEY key) { return TWindow::stop_run(key); }
virtual bool on_field_event(TOperable_field& f, TField_event e, long jolly);
// @cmember Converte le coordinate logiche (caratteri) in coordinate fisiche (pixel)
virtual PNT log2dev(long x, long y) const;
protected:
static bool color_handler(TMask_field& f, KEY k);
static bool azzera_handler(TMask_field& f, KEY k);
COLOR get_color_entry(const char* c) const;
void set_color_entry(const char* name, COLOR col);
const char* cid2name(short cid) const;
COLOR cid2color(short cid) const;
public:
void save_colors();
virtual KEY run();
TColor_mask();
virtual ~TColor_mask() { }
};
bool TColor_mask::on_field_event(TOperable_field& f, TField_event e, long jolly)
{
const short id = f.dlg();
bool ok = TRUE;
switch (id)
{
case DLG_USER:
if (e == fe_button)
{
for (int i = fields()-1; i >= 0; i--)
{
TMask_field& mf = fld(i);
const short bid = mf.dlg();
if (bid >= 101 && bid <= 112 && mf.is_kind_of(CLASS_BUTTON_FIELD))
{
const char* name = cid2name(bid);
const COLOR color = cid2color(bid);
set_color_entry(name, color);
}
}
update();
}
break;
case 114:
if (e == fe_button)
{
xvt_dm_post_font_sel(TASK_WIN, xvt_default_font(), NULL, 0);
force_update();
}
break;
default:
if (id >= 101 && id <= 112 && e == fe_button)
{
TColor_mask& m = (TColor_mask&)f.mask();
const char* name = m.cid2name(f.dlg());
COLOR col = m.get_color_entry(name);
col = choose_color(col, m.win());
ok = col != COLOR_INVALID;
if (ok)
{
m.set_color_entry(name, col);
XVT_PALETTE wp = xvt_vobj_get_palet(m.win());
if (wp != NULL)
{
XVT_PALETTE up = xvt_palet_create(XVT_PALETTE_USER, 0);
if (up != NULL)
{
const int MAXPAL = 256;
COLOR color[MAXPAL];
const int n = xvt_palet_get_colors(wp, color, MAXPAL);
if (n < MAXPAL) color[n] = col;
xvt_palet_set_tolerance(up, xvt_palet_get_tolerance(wp));
xvt_palet_add_colors(up, color, n+1);
xvt_vobj_set_palet(m.win(), up);
xvt_palet_destroy(wp);
}
}
m.stop_run(K_CTRL + 'R');
}
}
break;
}
return ok;
}
TColor_mask::TColor_mask()
: TAutomask("ba0200a")
{
TConfig color(CONFIG_USER, "Colors");
_color = color.list_variables();
set(113, color.get_bool("Campi3D") ? "X" : "");
}
PNT TColor_mask::log2dev(long x, long y) const
{
PNT p = { int(y * XI_FU_MULTIPLE), int(x * XI_FU_MULTIPLE) };
#ifdef XI_R4
XI_OBJ* itf = xi_get_itf((XinWindow)win());
xi_fu_to_pu(itf, (XinPoint*)&p, 1);
#else
XI_OBJ* itf = xi_get_itf(win());
xi_fu_to_pu(itf, &p, 1);
#endif
p.v = int(y * ROWY);
p.h += XI_FU_MULTIPLE / 2;
return p;
}
KEY TColor_mask::run()
{
KEY k = K_CTRL + 'R';
while (k == K_CTRL + 'R')
k = TMask::run();
return k;
}
void TColor_mask::update()
{
COLOR p, b;
const int x = 1;
const int y = 7;
const int w = 32;
const int h = 9;
set_pen(COLOR_BLACK);
set_brush(b = get_color_entry("MaskBack"));
frame(x+0, y+0, x+w, y+h, 0);
set_pen(p = get_color_entry("MaskLight"));
line(x+1, y, x+w-2, y);
line(x+1, y, x+1, y+h-1);
set_pen(p = get_color_entry("MaskDark"));
line(x+1, y+h-1, x+w-2, y+h-1);
line(x+w-2, y+h-1, x+w-2, y);
set_opaque_text(FALSE);
set_pen(p = get_color_entry("Normal"));
set_brush(b = get_color_entry("NormalBack"));
frame(x+3, y+1, x+w-3, y+2, 0);
set_color(p, b);
stringat(x+4, y+1, "Campo normale");
set_pen(p = get_color_entry("Focus"));
set_brush(b = get_color_entry("FocusBack"));
frame(x+3, y+3, x+w-3, y+4, 0);
set_color(p, b);
stringat(x+4, y+3, "Campo attivo");
set_pen(p = get_color_entry("Disabled"));
set_brush(b = get_color_entry("DisabledBack"));
frame(x+3, y+5, x+w-3, y+6, 0);
set_color(p, b);
stringat(x+4, y+5, "Campo disabilitato");
set_pen(p = get_color_entry("ButtonLight"));
set_brush(b = get_color_entry("ButtonBack"));
frame(x+3, y+7, x+w-3, y+8, 0);
set_color(get_color_entry("Normal"), b);
stringat(x+4, y+7, "Bottone normale");
}
void TColor_mask::save_colors()
{
TConfig colors(CONFIG_USER, "Colors");
FOR_EACH_ASSOC_STRING(_color, obj, key, str)
colors.set(key, str);
colors.set("Campi3D", get_bool(113) ? "X" : "");
}
COLOR TColor_mask::get_color_entry(const char* name) const
{
COLOR c = COLOR_INVALID;
const TObject* s = ((TColor_mask*)this)->_color.objptr(name);
if (s)
{
TToken_string colore(*(TString*)s, ',');
const byte r = (byte)colore.get_int();
const byte g = (byte)colore.get_int();
const byte b = (byte)colore.get_int();
c = RGB2COLOR(r, g, b);
}
return c;
}
void TColor_mask::set_color_entry(const char* name, COLOR col)
{
TString* s = (TString*)_color.objptr(name);
if (s == NULL)
{
s = new TString(15);
_color.add(name, s);
}
s->format("%d,%d,%d", XVT_COLOR_GET_RED(col), XVT_COLOR_GET_GREEN(col), XVT_COLOR_GET_BLUE(col));
}
const char* TColor_mask::cid2name(short cid) const
{
const char* name[] = { "MaskBack", "MaskLight", "MaskDark",
"Normal", "NormalBack",
"Focus", "FocusBack",
"Disabled", "DisabledBack",
"ButtonBack", "ButtonLight", "ButtonDark"
};
const int i = cid - 101;
CHECK(i >= 0 && i < 12, "Invalid color id");
return name[i];
}
COLOR TColor_mask::cid2color(short cid) const
{
COLOR color[] = { COLOR_LTGRAY, COLOR_WHITE, COLOR_GRAY,
COLOR_BLACK, COLOR_WHITE,
COLOR_BLACK, COLOR_YELLOW,
COLOR_DKGRAY, COLOR_LTGRAY,
COLOR_LTGRAY, COLOR_WHITE, COLOR_GRAY};
const int i = cid - 101;
CHECK(i >= 0 && i < 12, "Invalid color id");
return color[i];
}
///////////////////////////////////////////////////////////
// Menu mamnagement
///////////////////////////////////////////////////////////
static int get_next_string(const char* s, int from, TString& str, char& brace)
{
if (from < 0)
return -1;
char closing = '\0';
int start = 0;
for (int i = from; s[i]; i++)
{
if (s[i] == closing)
{
char* fine = (char*)(s + i);
const char old = *fine;
*fine = '\0';
str = s + start;
*fine = old;
return i+1;
}
if (!closing)
{
switch(s[i])
{
case '\'':
case '"' : closing = s[i]; break;
case '<' : closing = '>' ; break;
case '[' : closing = ']' ; break;
default : break;
}
if (closing)
{
start = i+1;
brace = s[i];
}
}
}
return -1;
}
class TSubmenu;
class TMenu;
class TMenuitem : public TObject
{
TSubmenu* _submenu;
TString _caption, _action;
char _type;
COLOR _color;
int _exist : 2;
bool _enabled : 2;
bool _firm : 2;
bool _password : 2;
bool _reloadmenu : 2;
protected:
bool perform_submenu() const;
bool perform_program() const;
public:
virtual bool ok() { return _caption.not_empty(); }
const TString& caption() const { return _caption; }
const TString& action() const { return _action; }
bool enabled() const;
bool disabled() const { return !enabled(); }
COLOR color() const { return enabled() ? _color : DISABLED_COLOR; }
bool is_submenu() const { return _type == '[' && _action.not_empty(); }
bool is_program() const { return _type != '[' && _action.not_empty(); }
bool perform() const;
TSubmenu& submenu() const { return *_submenu; }
TMenu& menu() const;
void create(const char* t);
TMenuitem(TSubmenu* sm);
virtual ~TMenuitem() { }
};
class TSubmenu : public TObject
{
TMenu* _menu;
TString _name;
TString _caption;
TFilename _picture;
TArray _items;
bool _enabled : 2;
bool _firm : 2;
public:
void read(TScanner& scanner);
TMenu& menu() const { return *_menu; }
int find_string(const char* str) const;
TMenuitem& item(int i) { return (TMenuitem&)_items[i]; }
const TMenuitem& item(int i) const { return (const TMenuitem&)_items[i]; }
const TMenuitem& operator[](int i) const { return item(i); }
const TString& name() const { return _name; }
const TString& caption() const { return _caption; }
const TString& picture() const { return _picture; }
int items() const { return _items.items(); }
bool query_firm() const { return _firm; }
bool enabled() const { return _enabled; }
bool disabled() const { return !enabled(); }
bool perform(int i);
TSubmenu(TMenu* menu, const char* name);
virtual ~TSubmenu() { }
};
class TMenu : public TAssoc_array
{
TSubmenu* _current;
int _item;
TStack _stack;
TFilename _default_bmp;
TString _default_menu;
TAssoc_array _images;
TAssoc_array _modules;
TToken_string _dangerous;
protected:
bool can_be_transparent(const TImage& i) const;
public: // TObject
virtual bool ok() const { return _current != NULL; }
public:
bool read(const char* name); // First call
bool read(const char* name, TString& root);
TSubmenu& current() const { return *_current; }
TSubmenu* find(const char* name) const { return (TSubmenu*)objptr(name); }
bool jumpto(TSubmenu *next);
bool jumpto_root();
TSubmenu& pop();
bool at_top() const { return _stack.count() == 0; }
void select(int i) { _item = i; }
int selected() const { return _item; }
TMenuitem& curr_item() { return _current->item(_item); }
bool perform();
TSubmenu* find_string(const char* str);
TImage& image(const char* name);
void reload_images();
bool has_module(const char* mod);
bool is_dangerous(const char* mod);
void set_dangerous_modules(const TToken_string& mod)
{ _dangerous = mod; }
TMenu() : _current(NULL), _item(0) { }
TMenu(const char* name) { read(name); }
virtual ~TMenu() { }
};
TMenuitem::TMenuitem(TSubmenu* sm)
: _submenu(sm),
_exist(-1), _firm(FALSE), _password(FALSE), _reloadmenu(FALSE),
_color(NORMAL_COLOR)
{ }
TMenu& TMenuitem::menu() const
{ return _submenu->menu(); }
void TMenuitem::create(const char* t)
{
TString16 flags;
char brace;
int start = 0;
start = get_next_string(t, start, _caption, brace);
start = get_next_string(t, start, _action, _type);
start = get_next_string(t, start, flags, brace);
for (int i = flags.len()-1; i >= 0; i--)
{
switch(toupper(flags[i]))
{
case 'D': _exist = FALSE; break;
case 'F': _firm = TRUE; break;
case 'P': _password = TRUE; break;
case 'R': _reloadmenu = TRUE; break;
default : break;
}
}
if (_type == '<')
{
if (_action.find('.') < 0)
_action << ".men";
if (fexist(_action))
menu().read(_action, _action);
else
_action.cut(0);
_type = '[';
}
if (_action.empty())
{
_exist = FALSE;
_enabled = FALSE;
}
// Controlla lo stato di aggiornamento
if (_enabled && is_program())
_enabled = !menu().is_dangerous(_action);
}
bool TMenuitem::enabled() const
{
bool yes = FALSE;
if (_exist)
{
if (is_submenu())
{
TSubmenu* mnu = menu().find(_action);
yes = mnu && mnu->enabled();
}
else
{
if (_exist < 0)
{
if (menu().is_dangerous(_action))
{
yes = FALSE;
}
else
{
const int endname = _action.find(' ');
TFilename name(endname > 0 ? _action.left(endname) : _action);
const char* ext[] = { "exe", "pif", "com", "bat", NULL };
for (int e = 0; ext[e]; e++)
{
name.ext(ext[e]);
if (name.exist())
break;
}
yes = ext[e] != NULL;
}
((TMenuitem*)this)->_exist = yes;
}
if (_exist)
{
TExternal_app app(_action);
yes = app.can_run();
}
if (!yes)
((TMenuitem*)this)->_enabled = FALSE;
}
}
return yes;
}
bool TMenuitem::perform_submenu() const
{
TSubmenu* mnu = menu().find(_action);
bool ok = mnu != NULL && mnu->enabled();
if (ok)
menu().jumpto(mnu);
return ok;
}
bool TMenuitem::perform_program() const
{
bool ok = TRUE;
if (_password)
{
TMask mask("ba0100a");
mask.disable(F_USER);
mask.set(F_USER, "SERVIZIO");
ok = FALSE;
if (mask.run() == K_ENTER)
{
const TDate oggi(TODAY);
TString16 pwd; pwd << dongle().administrator() << (oggi.month() + oggi.day());
ok = pwd == mask.get(F_PASSWORD);
}
if (!ok) error_box("Password di servizio errata!\nAccesso negato.");
}
if (_firm && main_app().get_firm() == 0)
#ifdef _DEMO_
ok = main_app().set_firm(1);
#else
ok = main_app().set_firm();
#endif
if (ok)
{
TCurrency::force_cache_update(); // Chiude cache valute
prefix().set(NULL); // Chiude prefix
TExternal_app a(_action);
a.run(FALSE,3);
const bool maintenance_app = _action.compare("ba1 -0", 6, TRUE) == 0;
if (maintenance_app)
{
char line1[16],line2[16];
while (fexist("conv.his"))
{
FILE* fp = fopen("conv.his","r");
fgets(line1,15,fp);
fclose(fp);
// Ora aspetta...
time_t old_time ;
time( &old_time) ;
while ( time( (time_t *) 0 ) <= old_time ) do_events();
TExternal_app auto_conv("ba1 -0 -C");
auto_conv.run();
fp = fopen("conv.his","r");
if (fp != NULL)
{
fgets(line2,15,fp);
fclose(fp);
}
else strcpy(line2,"");
if (strcmp(line1,line2) == 0)
if (!yesno_box("La conversione non sembra procedere. Continuare?"))
break;
}
}
prefix().set("DEF"); // Aggiorna prefix
}
return ok;
}
bool TMenuitem::perform() const
{
bool ok = enabled();
if (ok)
{
if (is_submenu())
ok = perform_submenu();
else
ok = perform_program();
}
return ok;
}
TSubmenu::TSubmenu(TMenu* menu, const char* name)
: _menu(menu), _name(name), _enabled(TRUE), _firm(FALSE), _items(12)
{
}
void TSubmenu::read(TScanner& scanner)
{
while (scanner.ok())
{
TString& line = scanner.line();
if (line.empty())
break;
if (line[0] == '[')
{
scanner.push();
break;
}
char brace;
if (line.compare("Caption", 7, TRUE) == 0)
get_next_string(line, 8, _caption, brace); else
if (line.compare("Module", 6, TRUE) == 0)
{
const int equal = line.find('=');
if (equal > 0)
{
bool disable = TRUE;
TToken_string mod(line.mid(equal+1, -1), ',');
FOR_EACH_TOKEN(mod, cod)
{
const int code = atoi(cod);
if (code == 0 || main_app().has_module(code))
{
disable = FALSE;
break;
}
}
if (disable)
_enabled = FALSE;
}
} else
if (line.compare("Picture", 7, TRUE) == 0)
get_next_string(line, 8, _picture, brace); else
if (line.compare("Flags", 5, TRUE) == 0)
{
TString16 flags;
get_next_string(line, 6, flags, brace);
if (flags.find('D') >= 0)
_enabled = FALSE;
if (flags.find('F') >= 0)
_firm = TRUE;
} else
if (line.compare("Item", 4, TRUE) == 0)
{
TMenuitem* item = new TMenuitem(this);
_items.add(item);
item->create(line);
}
}
}
int TSubmenu::find_string(const char* str) const
{
bool found = FALSE;
TString caption;
caption = _caption; caption.upper();
if (caption.find(str) >= 0)
found = TRUE;
for (int i = 0; i < items(); i++)
{
const TMenuitem& mi = item(i);
caption = item(i).caption();
caption.upper();
const bool match = caption.find(str) >= 0;
found = match && mi.is_program() && mi.enabled();
if (found)
return i;
}
return -1;
}
bool TSubmenu::perform(int i)
{
bool ok = i >= 0 && i < items();
if (ok)
ok = item(i).perform();
return ok;
}
bool TMenu::read(const char* name, TString& root)
{
TString str(255);
bool first = TRUE;
TScanner scanner(name);
while (scanner.ok())
{
const TString& line = first ? scanner.line() : scanner.pop();
if (line.empty())
break;
char brace = '[';
get_next_string(line, 0, str, brace);
if (first)
{
root = str;
first = FALSE;
}
if (objptr(str) == NULL)
{
TSubmenu* mnu = new TSubmenu(this, str);
mnu->read(scanner);
add(str, mnu);
}
else
break; // Menu gia' caricato!
}
return first == FALSE;
}
bool TMenu::read(const char* name)
{
TString root;
bool ok = read(name, root);
if (ok && _current == NULL)
{
_default_menu = root;
_current = find(root);
_item = 0;
}
return ok;
}
bool TMenu::jumpto(TSubmenu* next)
{
if (next && next->disabled())
next = NULL;
if (next)
{
if (next->query_firm())
{
#ifdef _DEMO_
if (!main_app().set_firm(1))
next = NULL;
#else
if (!main_app().set_firm())
next = NULL;
#endif
}
if (next)
{
if (_stack.count() >= 32)
_stack.destroy_base();
_stack.push(_current->name());
_current = next;
_item = 0;
}
}
return next != NULL;
}
bool TMenu::jumpto_root()
{
TSubmenu* sm = find(_default_menu);
return jumpto(sm);
}
TSubmenu& TMenu::pop()
{
TSubmenu* sm = _current;
if (!at_top())
{
TString& name = (TString&)_stack.pop();
sm = (TSubmenu*)objptr(name);
}
if (sm)
{
_current = sm;
_item = 0;
}
return *sm;
}
TSubmenu* TMenu::find_string(const char* str)
{
TString upstr(str);
upstr.upper();
restart();
for (TSubmenu* sm = (TSubmenu*)get(); sm; sm = (TSubmenu*)get())
{
if (sm != _current && sm->enabled())
{
int item = sm->find_string(upstr);
if (item >= 0)
{
jumpto(sm);
_item = item;
break;
}
}
}
return sm;
}
bool TMenu::perform()
{
bool ok = _current != NULL;
if (ok)
ok = _current->perform(_item);
return ok;
}
bool TMenu::can_be_transparent(const TImage& i) const
{
const int w = i.width()-1;
const int h = i.height()-1;
const COLOR col = i.get_pixel(0,0);
if (i.get_pixel(w,0) != col)
return FALSE;
if (i.get_pixel(w,h) != col)
return FALSE;
if (i.get_pixel(0,h) != col)
return FALSE;
return TRUE;
}
TImage& TMenu::image(const char* name)
{
TTimed_image* image = (TTimed_image*)_images.objptr(name);
if (image == NULL)
{
if (fexist(name))
{
if (_images.items() == 0)
_default_bmp = name; // Store default bitmap name
image = new TTimed_image(name);
if (can_be_transparent(*image))
image->convert_transparent_color(MASK_BACK_COLOR);
_images.add(name, image);
}
else
{
image = (TTimed_image*)_images.objptr(_default_bmp);
if (image == NULL)
fatal_box("Impossibile trovare l'immagine %s", (const char*)_default_bmp);
}
if (_images.items() > 3)
{
TString worst_bmp;
clock_t worst_time = image->touch(); // Impedisco di cancellare la prossima
_images.restart();
for (THash_object* o = _images.get_hashobj(); o; o = _images.get_hashobj())
{
if (o->key() != _default_bmp)
{
TTimed_image& i = (TTimed_image&)o->obj();
if (i.last_time() < worst_time)
{
worst_time = i.last_time();
worst_bmp = o->key();
}
}
}
_images.remove(worst_bmp);
}
}
image->touch();
return *image;
}
void TMenu::reload_images()
{
_images.restart();
for (THash_object* h = _images.get_hashobj(); h; h = _images.get_hashobj())
{
const TString& name = h->key();
TImage& i = (TImage&)h->obj();
i.load(name);
i.convert_transparent_color(MASK_BACK_COLOR);
}
}
bool TMenu::has_module(const char* mod)
{
TString16 key;
if (_modules.items() == 0)
{
TScanner scanner(AUT_FILE);
TString16 val;
for (int aut = 0; scanner.line() != ""; aut++)
{
key.strncpy(scanner.token(), 2);
key.lower();
val.format("%d", aut);
_modules.add(key, val);
}
}
key.strncpy(mod, 2);
key.lower();
int module = 0;
TString* cod = (TString*)_modules.objptr(key);
if (cod) module = atoi(*cod);
return main_app().has_module(module);
}
bool TMenu::is_dangerous(const char* mod)
{
TString code(mod);
code.cut(2);
return _dangerous.get_pos(code) >= 0;
}
///////////////////////////////////////////////////////////
// Menu application
///////////////////////////////////////////////////////////
class TMenu_application : public TApplication
{
const char* _name;
TMenu _menu;
bool _tree_view;
TString_array _preferred;
TPicture_mask* _mask;
static int _last_button;
static bool _find_button;
protected: // TApplication
virtual bool create();
virtual bool destroy();
virtual bool menu(MENU_TAG m);
virtual long handler(WINDOW win, EVENT* ep);
virtual bool test_assistance_year() const;
protected:
bool main_loop();
void test_temp();
void load_menu();
int do_level();
int do_tree();
bool check_user();
static bool menu_item_handler(TMask_field& f, KEY k);
static bool menu_find_handler(TMask_field& f, KEY k);
static bool tree_handler(TMask_field& f, KEY k);
static bool tree_find_handler(TMask_field& f, KEY k);
static bool tree_shrink_handler(TMask_field& f, KEY k);
void select_tree_current();
bool choose_colors();
bool choose_editors();
bool choose_study();
void load_preferences();
void save_preferences();
void update_preferred();
void add_to_preferred();
void manage_preferred();
bool test_programs();
bool test_network();
public:
void reload_images();
TMenu_application(const char* name) : _name(name), _mask(NULL) { }
virtual ~TMenu_application() { }
};
int TMenu_application::_last_button = 0;
bool TMenu_application::_find_button = FALSE;
inline TMenu_application& app()
{ return (TMenu_application&)main_app(); }
bool TMenu_application::test_assistance_year() const
{
return TRUE;
}
bool TMenu_application::menu_item_handler(TMask_field&f, KEY k)
{
if (k == K_SPACE)
{
app()._menu.select(f.dlg()-101);
f.set_focusdirty(FALSE);
return 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())
{
TPicture_mask& m = (TPicture_mask&)f.mask();
m.set_last_search_string(v);
if (app()._menu.find_string(v))
{
f.set_focusdirty(FALSE);
return f.mask().stop_run(K_F9);
}
else
{
beep();
return FALSE;
}
}
}
return TRUE;
}
int TMenu_application::do_level()
{
const TSubmenu& curr = _menu.current();
const int bwidth = 20;
TImage& image = _menu.image(curr.picture());
TPicture_mask mask(curr.caption(), -4, -2, &image);
CHECK(_mask == NULL, "Two masks are better than one?");
_mask = &mask;
const int x = mask.columns() / 2;;
int y = 1;
TString caption;
for (int i = 0; i < curr.items(); i++, y++)
{
const TMenuitem& item = curr[i];
caption = item.caption();
if (item.is_submenu() && caption.right(3) != "...")
caption << "...";
mask.add_static(-1, 0, caption, x+4, y);
const short id = 100+y;
mask.add_button(id, 0, "", x, y, 1, 1, "", BMP_STOPREC);
mask.set_handler(id, menu_item_handler);
if (item.disabled())
mask.disable(id);
}
TEdit_field& ef = mask.add_string(DLG_USER, 0, "Cerca ", 1, -3, 50, "", 50);
ef.set_handler(menu_find_handler);
const bool top = _menu.at_top();
TButton_field& bf = mask.add_button(DLG_QUIT, 0, "Fine", -12, -1, bwidth, 2);
if (!top)
mask.add_button(DLG_CANCEL, 0, "Menu precedente", -22, -1, bwidth, 2);
mask.first_focus(101+_menu.selected());
const int k = mask.run();
_mask = NULL;
int m = 0;
switch (k)
{
case K_ESC:
_menu.pop();
m = -1;
break;
case K_QUIT:
mask.reset();
m = -2;
break;
case K_F9:
case K_CTRL+'R':
m = 0;
break;
default:
m = _menu.selected() + 1; // Sempre > 0
break;
}
return m;
}
void TMenu_application::test_temp()
{
TFilename dir; dir.tempdir(); // Directory temporanea
dir.add("*");
TString_array files;
const int count = list_files(dir, files);
if (count > 0 && yesno_box("Cancellare %d file temporane%c in %s?",
count, (count > 1) ? 'i' : 'o', dir.path()))
{
TProgind bar(count, "Cancellazione file temporanei", TRUE, TRUE);
for (int i = count-1; i >= 0; i--)
{
if (bar.iscancelled()) break;
bar.addstatus(1);
const char* e = files.row(i);
::remove(e);
}
}
}
#ifdef DBG
HIDDEN bool pwd_handler(TMask_field& fld, KEY key)
{
if (key == K_F8)
{
TMask& m = fld.mask();
TString16 pwd;
TString16 usr = dongle().administrator(&pwd);
TLocalisamfile users(LF_USER);
users.put("USERNAME", usr);
users.read();
pwd = decode(users.get("PASSWORD"));
m.set(F_USER, usr);
m.set(F_PASSWORD, pwd);
m.stop_run(K_ENTER);
}
return TRUE;
}
#endif
void TMenu_application::reload_images()
{
_menu.reload_images();
if (_mask)
_mask->reload_images();
}
bool TMenu_application::check_user()
{
TString utente(user());
#ifdef _DEMO_
user() = utente = dongle().administrator();
const bool ok = TRUE;
#else
TMask m("ba0100a");
#ifdef DBG
m.set_handler(F_USER, pwd_handler);
m.set_handler(F_PASSWORD, pwd_handler);
#endif
TLocalisamfile users(LF_USER);
// Se i dati sono gia' convertiti aggiunge filtro sui gruppi
if (users.curr().exist("ISGROUP"))
{
TEdit_field& e = m.efield(F_USER);
e.browse()->set_filter("ISGROUP!=\"X\"");
}
TString pwd;
bool ok = FALSE;
for (int i = 0 ; i < 3 && !ok; i++)
{
if (utente.not_empty() && utente != dongle().administrator())
{
m.set(F_USER, utente);
m.first_focus(F_PASSWORD);
}
if (m.run() == K_ESC)
break;
utente = m.get(F_USER);
const TString& pass = m.get(F_PASSWORD);
users.zero();
users.put("USERNAME", utente);
pwd = "";
int err = users.read(_isequal, _lock);
if (err == NOERR)
{
pwd = decode(users.get("PASSWORD"));
}
else
{
if (utente == dongle().administrator(&pwd))
{
users.zero();
users.put("USERNAME", utente);
users.put("USERDESC", utente);
users.put("PASSWORD", encode(pwd));
users.write();
err = users.read(_isequal);
}
else
pwd = "";
}
ok = utente.not_empty() && pwd.not_empty() && pwd == pass;
if (ok)
{
ok = !users.get_bool("CONNECTED");
if (!ok)
{
ok = yesno_box("L'utente %s risulta essere gia' collegato\n"
"Si desidera continuare ugualmente?", (const char*)utente);
}
if (ok)
{
users.put("USERNAME", dongle().administrator());
users.read(_isequal);
if (users.get("AUTSTR") == "CONVERTING")
{
TString msg = "E' in corso una conversione archivi:\n";
if (utente == dongle().administrator())
{
msg << "Si desidera continuare ugualmente?";
ok = yesno_box(msg);
}
else
{
msg << "Accesso negato.";
ok = error_box(msg);
}
}
}
if (ok)
{
user() = utente;
dongle().logout();
ok = get_serial_number() >= 0;
if (!ok)
error_box("Probabilmente e' stato superato il numero massimo di utenti");
}
}
else
{
error_box("Utente e/o password errata:\nfare attenzione alle maiuscole");
m.set(F_PASSWORD,"");
}
if (err == NOERR)
{
if (ok)
{
users.put("USERNAME", utente);
users.read(_isequal, _lock);
users.put("CONNECTED", "X");
users.rewrite();
}
}
}
#endif // _DEMO_
if (ok)
{
if (utente != dongle().administrator())
{
TConfig prawin(CONFIG_INSTALL, "Main");
prawin.set("User", utente);
}
enable_menu_item(OPTIONS_MENU);
enable_menu_item(PREFERRED_MENU);
customize_colors(); // Aggiorna set di colori
reload_images(); // Ritrasparentizza immagini
xvt_dwin_invalidate_rect(TASK_WIN, NULL); // Ridisegna sfondo
load_preferences();
}
return ok;
}
HIDDEN int compare_version(const char* v1, int p1, const char* v2, int p2)
{
TString16 ver1(v1), ver2(v2);
ver1.trim();
if (ver1.len() == 4)
ver1.insert((v1[0] == '9') ? "19" : "20", 0);
ver2.trim();
if (ver2.len() == 4)
ver2.insert((v2[0] == '9') ? "19" : "20", 0);
int res = ver1.compare(ver2, -1, TRUE);
if (res == 0)
res = p1 - p2;
return res;
}
static int get_module_version(TConfig& cfg, void* jolly)
{
const TString& p = cfg.get_paragraph();
if (p.len() == 2)
{
TAssoc_array& map = *(TAssoc_array*)jolly;
TToken_string* tok = new TToken_string(15);
*tok = cfg.get("Versione");
tok->add(cfg.get("Patch"));
map.add(p, tok);
}
return FALSE;
}
bool TMenu_application::test_network()
{
return ::os_test_network_version();
}
bool TMenu_application::test_programs()
{
TToken_string dangerous;
bool test = FALSE;
bool more = FALSE;
{
TConfig prawin(CONFIG_INSTALL, "Main");
test = prawin.get_bool("TestPrograms");
}
TExternal_app app("ba1 -6");
while (test)
{
TConfig install("install.ini", "Main");
TFilename remote_name = install.get("DiskPath");
remote_name.add("install.ini");
if (remote_name.exist())
{
TProgind pi(3, "Controllo aggiornamento programmi", FALSE, TRUE);
TConfig remote_install(remote_name, "Main");
TAssoc_array my_modules, his_modules;
pi.addstatus(1);
install.for_each_paragraph(get_module_version, &my_modules);
pi.addstatus(1);
remote_install.for_each_paragraph(get_module_version, &his_modules);
pi.addstatus(1);
TScanner scan(AUT_FILE);
for (int module = 0; scan.line().not_empty(); module++)
{
if (dongle().active(module))
{
const TString16 code = scan.token().left(2);
TToken_string* mytok = (TToken_string*)my_modules.objptr(code);
TToken_string* histok = (TToken_string*)his_modules.objptr(code);
const TString16 v1 = mytok ? mytok->get(0) : "";
const int p1 = mytok ? mytok->get_int() : 0;
const TString16 v2 = histok ? histok->get(0) : "";
const int p2 = histok ? histok->get_int() : 0;
if (!v1.blank() && compare_version(v1, p1, v2, p2) < 0)
{
dangerous.add(code);
const TString& name = scan.token().mid(3);
if (!more)
warning_box("ATTENZIONE: Il modulo %s\ndeve essere aggiornato prima di poterlo utilizzare.", (const char*)name);
}
}
}
}
if (more)
break;
if (dangerous.empty() ||
(!more && !yesno_box("Si desidera aggiornare i moduli adesso?")))
break;
app.run(FALSE, TRUE, TRUE, TRUE);
more = TRUE; // ricontrolla
}
_menu.set_dangerous_modules(dangerous);
return TRUE;
}
bool TMenu_application::create()
{
TApplication::create();
if (!test_network())
return FALSE;
#ifdef _DEMO_
{
TMask w("ATTENZIONE", 1, 68, 12);
w.add_static(DLG_NULL, 0 ,"@bATTENZIONE" , 30 , 1);
w.add_static(DLG_NULL, 0 ,"Questo programma <20> in versione dimostrativa." , 1 , 3);
w.add_static(DLG_NULL, 0 ,"Non si possono memorizzare date con mese successivo a Marzo." , 1 , 5);
w.add_static(DLG_NULL, 0 ,"Il programma funziona circa per due ore ogni giorno." , 1 , 7);
w.add_static(DLG_NULL, 0 ,"Il numero di registrazioni <20> stato limitato ad un migliaio." , 1 , 9);
w.add_button(DLG_OK, 0, "", -11, -1, 10, 2);
w.run();
}
#endif
if (!check_user())
return FALSE;
if (!TApplication::test_assistance_year())
{
TExternal_app app("ba1 -4");
app.run();
}
set_perms();
test_temp();
TWait_cursor hourglass;
if (!test_programs())
return FALSE;
if (!_menu.ok())
{
TWait_cursor hourglass;
TFilename menu = (argc() < 2) ? MEN_FILE : argv(1);
menu.ext("men");
_menu.read(menu);
dispatch_e_menu(MENU_ITEM(1));
}
return TRUE;
}
bool TMenu_application::destroy()
{
TLocalisamfile users(LF_USER);
users.put("USERNAME", user());
int err = users.read(_isequal, _lock);
if (err == NOERR)
{
users.zero("CONNECTED");
users.rewrite();
}
// ba0 / 1 substitute: serve per installare anche i file menu ed installatore che erano in esecuzione
TFilename ba0exfile("ba0.ex_"),ba1exfile("ba1.ex_");
if (ba0exfile.exist()||ba1exfile.exist())
{
TExternal_app ba0epilogue("ba0close.exe");
ba0epilogue.run(TRUE,TRUE,TRUE,FALSE); // run asynchronous...
}
return TRUE;
}
bool TMenu_application::main_loop()
{
bool run = TRUE;
while (run)
{
const int m = _tree_view ? do_tree() : do_level();
if (m > 0)
_menu.perform();
else
run = m >= -1;
}
return FALSE;
}
bool TMenu_application::choose_colors()
{
disable_menu_item(OPTIONS_MENU);
TColor_mask * cm = new TColor_mask();
if (cm->run() == K_ENTER)
{
cm->save_colors(); // Aggiorna config
customize_colors(); // Aggiorna set di colori
reload_images(); // Aggiorna bitmaps del menu
// Ridisegna sfondo
TTemp_window tw(TASK_WIN);
tw.force_update();
// Provoca chiusura forzata del menu
if (_mask != NULL)
_mask->stop_run(K_CTRL + 'R');
}
enable_menu_item(OPTIONS_MENU);
delete cm;
return TRUE;
}
HIDDEN bool browse_file_handler(TMask_field& f, KEY k)
{
bool ok = TRUE;
if (k == K_F9)
{
FILE_SPEC fs; memset(&fs, 0, sizeof(FILE_SPEC));
xvt_fsys_convert_str_to_dir(".", &fs.dir);
strcpy(fs.type, "EXE");
strcpy(fs.name, f.get());
strcpy(fs.creator, "MENU");
DIRECTORY dir;
xvt_fsys_get_dir(&dir);
const int err = xvt_dm_post_file_open(&fs, "Selezione programma");
xvt_fsys_set_dir(&dir);
if (err == FL_OK)
{
TFilename n;
xvt_fsys_convert_dir_to_str(&fs.dir, n.get_buffer(), n.size());
n.add(fs.name);
f.set(n);
}
}
if (k == K_TAB && f.focusdirty())
{
TFilename infile(f.get());
if (infile.not_empty())
{
TFilename outfile;
if (*infile.ext() == '\0')
infile.ext("exe");
if (!infile.search_in_path(outfile))
ok = error_box("Il programma %s non esiste!", (const char*)infile);
}
}
return ok;
}
HIDDEN bool link_notify(TSheet_field& s, int r, KEY k)
{
bool ok = TRUE;
if (k == K_INS)
{
ok = s.items() < 29; // Accetta 29 righe al massimo
}
return ok;
}
bool TMenu_application::choose_editors()
{
disable_menu_item(OPTIONS_MENU);
TConfig link(CONFIG_USER, "Link");
TMask* msk = new TMask("ba0300a");
TMask& m = *msk;
TSheet_field& sheet = m.sfield(201);
sheet.set_notify(link_notify);
TMask& sm = sheet.sheet_mask();
sm.set_handler(102, browse_file_handler);
TAssoc_array& var = (TAssoc_array&)link.list_variables();
FOR_EACH_ASSOC_STRING(var, obj, key, str)
{
TToken_string& row = sheet.row(-1);
if (strlen(key) <= 3)
{
row = key;
row.lower();
row.add(str);
}
}
sheet.rows_array().sort();
if (m.run() == K_ENTER)
{
link.remove_all();
FOR_EACH_SHEET_ROW_BACK(sheet, r, row)
{
TString16 ext = row->get(0);
if (!ext.blank())
{
ext.lower();
TFilename prg = row->get();
link.set(ext, prg);
}
}
}
delete msk;
enable_menu_item(OPTIONS_MENU);
return TRUE;
}
HIDDEN bool study_handler(TMask_field& f, KEY k)
{
bool ok = TRUE;
if (f.to_check(k))
{
TFilename path(f.get());
path.add("com");
path.add("dir.gen");
if (!path.exist())
ok = f.error_box("La directory %s non e' uno studio valido!",
(const char*)f.get());
}
return ok;
}
bool TMenu_application::choose_study()
{
TMask m("Scelta studio", 1, 60, 5);
m.add_button(DLG_OK, 0, "", -12, -1, 10, 2);
m.add_button(DLG_CANCEL, 0, "", -22, -1, 10, 2);
m.add_string(DLG_USER, 0, "Studio ", 1, 1, 50);
m.set_handler(DLG_USER, study_handler);
m.set(DLG_USER, prefix().get_studio());
bool ok = m.run() == K_ENTER;
if (ok)
{
destroy();
prefix().set_studio(m.get(DLG_USER));
ok = create();
if (ok)
{
set_firm();
_mask->stop_run(K_F9); // Ricarica maschera
}
else
stop_run(); // Termina applicazione
}
return ok;
}
long TMenu_application::handler(WINDOW win, EVENT* ep)
{
long ret = TApplication::handler(win, ep);
if (ep->type == E_FONT)
{
if (_mask != NULL)
_mask->stop_run(K_CTRL + 'R');
}
return ret;
}
///////////////////////////////////////////////////////////
// TMenu_tree
///////////////////////////////////////////////////////////
class TMenu_tree : public TBidirectional_tree
{
TMenu* _menu;
const TSubmenu* _submenu;
int _menuitem;
TString _root_id, _curr_id;
protected: // TTree
virtual void node2id(const TObject* node, TString& id) const;
public: // TTree
virtual bool goto_root();
virtual bool goto_firstson();
virtual bool goto_rbrother();
virtual bool goto_node(const TString &id);
virtual bool has_son() const;
virtual bool has_rbrother() const;
virtual TObject* curr_node() const;
virtual bool has_root() const;
virtual bool has_father() const;
virtual bool has_lbrother() const;
virtual bool goto_father();
virtual bool goto_lbrother();
virtual void curr_id(TString& id) const { id = _curr_id; }
virtual bool get_description(TString& desc) const;
virtual TImage* image(bool selected) const;
public:
const TSubmenu& curr_submenu() const;
const TMenuitem& curr_item() const;
bool find_string(const TString& str);
bool find_leaf(const TString& str);
TMenu_tree(TMenu& menu);
virtual ~TMenu_tree() { }
};
const TSubmenu& TMenu_tree::curr_submenu() const
{
CHECKS(_submenu, "NULL submenu ", (const char*)_curr_id);
return *_submenu;
}
const TMenuitem& TMenu_tree::curr_item() const
{
const TSubmenu& sm = curr_submenu();
if (_menuitem < 0 || _menuitem >= sm.items())
{
NFCHECK("Invalid submenu item %d in %s", _menuitem, (const char*)sm.name());
return sm.item(0);
}
return sm.item(_menuitem);
}
struct TFind_string_data
{
TString _str;
TString _ignore;
};
HIDDEN bool find_string_callback(TTree& tree, void* jolly, word flags)
{
if (flags == SCAN_PRE_ORDER)
{
TMenu_tree& mt = (TMenu_tree&)tree;
TFind_string_data& data = *(TFind_string_data*)jolly;
const TSubmenu& sm = mt.curr_submenu();
if (sm.name() == data._ignore)
return FALSE;
TString desc; mt.get_description(desc);
desc.upper();
if (desc.find(data._str) >= 0)
return TRUE;
}
return FALSE;
}
struct TFind_node_data
{
TString _id;
long _count;
};
HIDDEN bool find_node_callback(TTree& tree, void* jolly, word flags)
{
if (flags == SCAN_PRE_ORDER)
{
TFind_node_data& data = *(TFind_node_data*)jolly;
data._count++;
TString id; tree.curr_id(id);
if (id == data._id)
return TRUE;
}
return FALSE;
}
HIDDEN bool find_leaf_callback(TTree& tree, void* jolly, word flags)
{
if (flags == SCAN_PRE_ORDER)
{
const TString& leaf = *(TString*)jolly;
TString id; tree.curr_id(id);
const int slash = id.rfind('/');
if (slash > 0)
id = id.mid(slash+1);
if (id == leaf)
return TRUE;
}
return FALSE;
}
bool TMenu_tree::find_string(const TString& str)
{
static TFind_string_data data;
data._str = str; data._str.upper();
goto_root();
bool ok = scan_depth_first(find_string_callback, &data, SCAN_PRE_ORDER);
if (ok)
data._ignore = curr_submenu().name();
else
data._ignore.cut(0);
return ok;
}
bool TMenu_tree::find_leaf(const TString& str)
{
goto_root();
bool ok = scan_depth_first(find_leaf_callback, (void *)&str, SCAN_PRE_ORDER);
return ok;
}
void TMenu_tree::node2id(const TObject* node, TString& id) const
{
TString* str = (TString*)node;
id = *str;
}
bool TMenu_tree::goto_root()
{
TSubmenu* sm = _menu->find(_root_id);
if (sm)
{
_curr_id = _root_id;
_curr_id << ".0";
_submenu = sm;
_menuitem = 0;
}
return sm != NULL;
}
bool TMenu_tree::goto_firstson()
{
const TMenuitem& mi = curr_item();
if (mi.is_submenu())
{
const TSubmenu* sm = _menu->find(mi.action());
if (sm && sm->items() > 0)
{
_curr_id << '/' << mi.action() << ".0";
_submenu = sm;
_menuitem = 0;
return TRUE;
}
}
return FALSE;
}
bool TMenu_tree::goto_rbrother()
{
if (_menuitem < _submenu->items()-1)
{
const int dot = _curr_id.rfind('.');
_curr_id.cut(dot+1);
_curr_id << (++_menuitem);
return TRUE;
}
return FALSE;
}
bool TMenu_tree::goto_node(const TString &id)
{
const int dot = id.rfind('.');
CHECKS(dot > 0, "Invalid tree node ", (const char*)id);
_menuitem = atoi(id.mid(dot+1));
_curr_id = id.left(dot);
const int slash = _curr_id.rfind('/');
_curr_id = _curr_id.mid(slash+1);
_submenu = _menu->find(_curr_id);
_curr_id = id;
return _submenu != NULL;
}
bool TMenu_tree::has_son() const
{
const TMenuitem& mi = curr_item();
return mi.is_submenu();
}
bool TMenu_tree::has_rbrother() const
{
return _menuitem < _submenu->items()-1;
}
bool TMenu_tree::has_root() const
{
return _root_id.not_empty();
}
bool TMenu_tree::has_father() const
{
return _curr_id.find('/') > 0;
}
bool TMenu_tree::has_lbrother() const
{
return _menuitem > 0;
}
bool TMenu_tree::goto_father()
{
const int slash = _curr_id.rfind('/');
if (slash > 0)
{
const TString id = _curr_id.left(slash);
return goto_node(id);
}
return FALSE;
}
bool TMenu_tree::goto_lbrother()
{
if (_menuitem > 0)
{
const int dot = _curr_id.rfind('.');
_curr_id.cut(dot+1);
_curr_id << (--_menuitem);
return TRUE;
}
return FALSE;
}
TObject* TMenu_tree::curr_node() const
{
return &((TMenu_tree*)this)->_curr_id;
}
bool TMenu_tree::get_description(TString& desc) const
{
const TMenuitem& mi = curr_item();
desc = mi.caption();
return desc.not_empty();
}
TImage* TMenu_tree::image(bool selected) const
{
const TMenuitem& mi = curr_item();
if (mi.disabled())
return get_res_image(BMP_STOP);
return TTree::image(selected);
}
TMenu_tree::TMenu_tree(TMenu& menu)
: _menu(&menu), _curr_id(128, '/')
{
_root_id = _menu->current().name();
goto_root();
}
///////////////////////////////////////////////////////////
// Tree view implementation
///////////////////////////////////////////////////////////
bool TMenu_application::tree_handler(TMask_field& f, KEY k)
{
if (k == K_CTRL + K_SPACE)
{
TTree_field& tf = (TTree_field&)f;
TMenu_tree& mt = *(TMenu_tree*)tf.tree();
const TMenuitem& mi = mt.curr_item();
mi.perform();
if (mi.is_submenu() && mt.expanded())
{
TMenu& menu = mt.curr_submenu().menu();
TImage& image = menu.image(menu.current().picture());
TPicture_mask& pm = (TPicture_mask&)f.mask();
pm.set_image(&image);
}
}
return TRUE;
}
void TMenu_application::select_tree_current()
{
TTree_field& tf = (TTree_field&)_mask->field(101);
TMenu_tree& mt = *(TMenu_tree*)tf.tree();
tf.select_current();
TString id; mt.curr_id(id);
do { mt.expand(); } while (mt.goto_father());
TFind_node_data data;
data._id = id;
data._count = 0;
mt.goto_root();
mt.scan_depth_first(find_node_callback, &data,
SCAN_PRE_ORDER | SCAN_IGNORING_UNEXPANDED);
TField_window& win = tf.win();
const TPoint& range = win.range();
if (data._count > range.y)
win.set_scroll_max(win.columns(), data._count+win.rows());
const int dot = id.rfind('.');
const int pos = atoi(id.mid(dot+1));
win.update_thumb(-1, data._count-pos-2);
win.force_update();
tf.set_focus();
}
bool TMenu_application::tree_find_handler(TMask_field&f, KEY k)
{
if (k == K_TAB && f.focusdirty() && !f.empty())
{
const TString& v = f.get();
TPicture_mask& m = (TPicture_mask&)f.mask();
m.set_last_search_string(v);
TTree_field& tf = (TTree_field&)m.field(101);
TMenu_tree& mt = *(TMenu_tree*)tf.tree();
if (mt.find_string(v))
app().select_tree_current();
else
beep();
}
return TRUE;
}
bool TMenu_application::tree_shrink_handler(TMask_field&f, KEY k)
{
if (k == K_SPACE)
{
TTree_field& tf = (TTree_field&)f.mask().field(101);
TMenu_tree& mt = *(TMenu_tree*)tf.tree();
mt.shrink_all();
mt.goto_root();
tf.select_current();
tf.win().update_thumb(0,0);
tf.win().force_update();
}
return TRUE;
}
void TMenu_application::update_preferred()
{
MENU_ITEM* mm = xvt_menu_get_tree(TASK_WIN);
for (MENU_ITEM* mi = mm; mi != NULL && mi->tag != 0 && mi->tag != PREFERRED_MENU; mi++);
if (mi == NULL || mi->tag <= 0 || mi->child == NULL)
{
NFCHECK("Can't find Preferiti Menu");
return;
}
int i;
MENU_ITEM* pm = (MENU_ITEM*)xvt_mem_zalloc((_preferred.items()+4)*sizeof(MENU_ITEM));
memcpy(pm, mi->child, 3*sizeof(MENU_ITEM));
for (i = 0; i < 2; i++)
{
const char* src = mi->child[i].text;
pm[i].text = xvt_str_duplicate(src);
}
xvt_res_free_menu_tree(mi->child);
mi->child = pm;
for (i = -1; i < _preferred.items(); i++)
{
MENU_ITEM& m = pm[i+3];
if (i >= 0)
{
m.tag = MENU_ITEM(50+i);
m.enabled = TRUE;
const char* src = _preferred.row(i).get(0);
m.text = xvt_str_duplicate(src);
}
else
{
m.tag = -1;
m.separator = TRUE;
}
}
xvt_menu_set_tree(TASK_WIN, mm);
xvt_menu_update(TASK_WIN);
xvt_res_free_menu_tree(mm);
}
void TMenu_application::load_preferences()
{
TConfig cfg(CONFIG_USER, "ba0");
_tree_view = cfg.get_bool("TreeView");
_preferred.destroy();
TToken_string row;
for (int i = 0; ; i++)
{
row = cfg.get("Preferred", NULL, i);
if (row.empty_items())
break;
_preferred.add(row);
}
if (i > 0)
update_preferred();
}
void TMenu_application::save_preferences()
{
TConfig cfg(CONFIG_USER, "ba0");
for (int i = 0; i < _preferred.items(); i++)
cfg.set("Preferred", _preferred.row(i), NULL, TRUE, i);
cfg.set("Preferred", "", NULL, TRUE, i);
cfg.set("TreeView", _tree_view);
}
void TMenu_application::add_to_preferred()
{
const int maxpref = 16;
if (_mask == NULL) // Succede durante il login!
return;
if (_preferred.items() < maxpref)
{
TToken_string tok;
if (_tree_view)
{
TTree_field& tf = _mask->tfield(101);
tf.goto_selected();
tf.tree()->get_description(tok);
TString id; tf.tree()->curr_id(id);
tok.add(id);
}
else
{
const TMask_field& butt = _mask->focus_field();
const int index = butt.dlg() - 101;
if (index >= 0 && index < 16)
{
_menu.select(index);
tok = _menu.curr_item().caption();
tok.add(_menu.current().name());
tok << '.' << index;
}
}
if (!tok.empty())
{
_preferred.add(tok);
update_preferred();
save_preferences();
}
}
else
error_box("Non e' possibile memorizzare piu' di %d preferenze", maxpref);
}
class TPreferred_mask : public TAutomask
{
protected:
virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly);
public:
TPreferred_mask() : TAutomask("ba0400a") { }
virtual ~TPreferred_mask() { }
};
bool TPreferred_mask::on_field_event(TOperable_field& o, TField_event e, long jolly)
{
switch (o.dlg())
{
case F_PREF_SHEET:
switch (e)
{
case se_query_add:
return FALSE;
case se_enter:
enable(F_PREF_UP, jolly > 0);
enable(F_PREF_DN, jolly < ((TSheet_field&)o).items()-1);
break;
default:
break;
}
break;
case F_PREF_UP:
if (e == fe_button)
{
TSheet_field& sf = sfield(F_PREF_SHEET);
const int r = sf.selected();
if (r > 0)
{
sf.rows_array().swap(r, r-1);
sf.select(r-1);
sf.force_update();
}
}
break;
case F_PREF_DN:
if (e == fe_button)
{
TSheet_field& sf = sfield(F_PREF_SHEET);
const int r = sf.selected();
if (r < sf.items()-1)
{
sf.rows_array().swap(r, r+1);
sf.select(r+1);
sf.force_update();
}
}
break;
case DLG_DELREC:
if (e == fe_button && jolly == 0) // Main delete button pressed
{
TSheet_field& sf = sfield(F_PREF_SHEET);
const int r = sf.selected();
if (r >= 0)
sf.destroy(r);
return FALSE;
}
break;
default:
break;
}
return TRUE;
}
void TMenu_application::manage_preferred()
{
TPreferred_mask* m = new TPreferred_mask;
m->set(F_PREF_TREE, _tree_view ? "X" : "");
TSheet_field& sf = m->sfield(F_PREF_SHEET);
sf.rows_array() = _preferred;
sf.force_update();
if (m->run() == K_ENTER)
{
_preferred = sf.rows_array();
const bool old_tv = _tree_view;
_tree_view = m->get_bool(F_PREF_TREE);
update_preferred();
save_preferences();
if (_tree_view != old_tv)
_mask->stop_run(K_CTRL + 'R'); // Provoca chiusura forzata del menu
}
delete m;
}
int TMenu_application::do_tree()
{
_menu.jumpto_root();
const TSubmenu& curr = _menu.current();
TImage& image = _menu.image(curr.picture());
TPicture_mask mask(curr.caption(), 0, 0, &image, 0, 0);
CHECK(_mask == NULL, "Two masks are better than one?");
_mask = &mask;
const int twidth = 54;
const int bwidth = (mask.columns() - twidth - 8);
TMenu_tree tree(_menu);
TTree_field& tree_fld = mask.add_tree(101, 0, 0, 0, twidth, -1);
tree_fld.set_tree(&tree);
tree_fld.set_handler(tree_handler);
RCT rct; tree_fld.get_rect(rct);
TMask_field& sf = mask.add_static(DLG_NULL, 0, "Cerca", -2, 0);
TEdit_field& ef = mask.add_string(DLG_USER, 0, "", -2, 1, 50, "", bwidth);
ef.set_handler(tree_find_handler);
TButton_field& mf = mask.add_button(102, 0, "Menu principale", -1, 2, bwidth, 2);
mf.set_handler(tree_shrink_handler);
TButton_field& bf = mask.add_button(DLG_QUIT, 0, "Fine", -56, -1, bwidth/2, 2);
mask.first_focus(101);
KEY key = mask.run();
_mask = NULL;
return key == K_QUIT ? -2 : 0;
}
bool TMenu_application::menu(MENU_TAG mt)
{
bool ok = TRUE;
switch (mt)
{
case MENU_ITEM(1): ok = main_loop(); break;
case MENU_ITEM(2): choose_colors(); break;
case MENU_ITEM(3): choose_editors(); break;
case MENU_ITEM(4): choose_study(); break;
case MENU_ITEM(5): add_to_preferred(); break;
case MENU_ITEM(6): manage_preferred(); break;
default:
if (mt >= MENU_ITEM(50) && mt < MENU_ITEM(50+_preferred.items()))
{
bool ok = FALSE;
const int index = mt - MENU_ITEM(50);
TToken_string node(_preferred.row(index).get(1), '/');
if (_tree_view)
{
TMenu_tree& met = *(TMenu_tree*)_mask->tfield(101).tree();
ok = node.items() == 1 ? met.find_leaf(node) : met.goto_node(node);
if (ok)
select_tree_current();
}
else
{
TToken_string mi(node.get(-2), '.');
const char* sub = mi.get(0);
TSubmenu* sm = _menu.find(sub);
if (sm && _menu.jumpto(sm))
{
ok = TRUE;
_menu.select(mi.get_int());
_mask->stop_run(K_CTRL + 'R');
}
}
if (!ok) beep();
}
break;
}
return ok;
}
///////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
TApplication::check_parameters(argc, argv);
if (user().blank())
{
char name[16];
if (aga_get_user_name(name, 16))
user() = name;
}
if (user().blank())
{
TConfig prawin(CONFIG_INSTALL, "Main");
user() = prawin.get("User");
}
TFilename menu = (argc < 2) ? MEN_FILE : argv[1];
if (menu.exist())
{
TMenu_application *ma = new TMenu_application(menu);
ma->run(argc, argv, "Menu Principale");
delete ma;
}
else
error_box("Non esiste il menu %s", (const char*)menu);
return 0;
}