#include #include #include /////////////////////////////////////////////////////////// // TReport_application /////////////////////////////////////////////////////////// bool TReport_application::create() { bool ok = user_create(); if (ok) return TSkeleton_application::create(); return false; } bool TReport_application::destroy() { user_destroy(); return TSkeleton_application::destroy(); } // @cmember Esegue la stampa void TReport_application::execute_print(TReport_book & book, TAutomask & mask, TReport & rep, export_type & type) { rep.set_export_sections(type); book.add(rep, type); } TTrec * TReport_application::get_dbase_recdesc(TReport & rep, const TAutomask & mask) { TTrec * desc = new TTrec; TReport_section * head = rep.find_section('H', 0); int header_row = -1; TToken_string key; int nfields = 0; int hrow = 0; if (head != nullptr) { int secfields = head->items(); for (int i = secfields - 1; hrow == 0 && i >= 0; i--) { const TReport_field & fld = head->field(i); const TReport_rct& r = fld.get_rect(); if ((r.x < 1500) && (fld.type() == 'T')) hrow = r.y; } TString fieldname; for (int i = 0; i < secfields; i++) { const TReport_field & fld = head->field(i); const TReport_rct& r = fld.get_rect(); if ((r.y >= hrow - 50) && (r.y <= hrow + 50) && (fld.type() == 'T')) { fieldname = fld.formatted_text(); fieldname.cut(9); fieldname.strip_spaces(); fieldname.strip(",."); fieldname.upper(); if (nfields == 0) key = fieldname; const int nf = desc->fields(); TString f; TToken_string def; bool found = false; for (int n = nf - 1; !found && n >= 0; n--) { def = desc->fielddef(n); f = def.get(0); if (f.starts_with(fieldname)) { fieldname << atoi(f.mid(fieldname.len())) + 1; found = true; } } def = fieldname; def.add(_alfafld); def.add(80); desc->set_fields(nfields); desc->update_fielddef(nfields++, def); } } desc->set_keys(1); desc->update_keydef(0, key); } return desc; } // @cmember Ciclo principale void TReport_application::main_loop() { TAutomask & m = get_mask(); KEY k; bool has_rs = main_app().has_module(RSAUT, CHK_DONGLE); int narg = 2; const long old_firm = get_firm(); bool restore_firm = false; TString arg; char arg_type = get_arg(narg, arg); TFilename name; if (arg_type == 'B') { TFilename fname = arg; char sep = '\t'; if (fname.exist()) { m.load(fname, true); TReport & rep = get_report(m); TReport_book b(title()); export_type type = _export_printer; while ((arg_type = get_arg(narg, arg)) != '\0') { switch (arg_type) { case 'F' : if (atol(arg) != old_firm) { restore_firm = true; set_firm(atol(arg)); } break; case 'O': name = arg; break; case 'S': sep = arg[0]; break; case 'T': k = 20000 + UPCASE(arg[0]); break; case ' ': default: if (atol(arg) != 0) { if (atol(arg) != old_firm) { restore_firm = true; set_firm(atol(arg)); } } else if (arg.len() == 1) k = 20000 + UPCASE(arg[0]); else name = arg; break; } } bool ask_filename = name.blank(); if (name.blank()) name = get_output_filename(m, rep); else { if (!dexist(name.path())) if (!make_dir(name.path())) { error_box(FR("Non posso creare %s uso il direttorio temporaneo"), (const char *)name.path()); const TString fname = name.name(); name.cut(0); name.tempdir(); name.add(fname); } } _user_key = ' '; switch (k) { case K_PRINT: type = _export_printer; break; case K_EXPORT: type = _export_generic; break; case K_EXCEL: type = _export_excel; break; case K_PDF: type = _export_pdf; break; case K_TEXT: type = _export_text; break; case K_DBF: type = _export_dbase; break; case K_VISUALIZE: type = _export_visualize; break; default: type = _export_user; _user_key = k; break; } execute_print(b, m, rep, type); switch (k) { case K_ENTER: case K_PRINT: print(b, m, rep); break; case K_EXPORT: esporta(b, m, rep); break; case K_EXCEL: b.export_excel(name, false, false, false, sep); break; case K_PDF: b.export_pdf(name); break; case K_TEXT: b.export_text(name); break; case K_DBF: b.export_dbase(name, get_dbase_recdesc(rep, m), false, false, ask_filename); break; case K_VISUALIZE: preview(b, m, rep); break; default: break; } } if (restore_firm) set_firm(old_firm); } else { if (m.exist(DLG_EXPORT)) m.enable(DLG_EXPORT, has_rs); if (m.exist(DLG_EXPORT_EXCEL)) m.enable(DLG_EXPORT_EXCEL, has_rs); if (m.exist(DLG_EXPORT_PDF)) m.enable(DLG_EXPORT_PDF, has_rs); if (m.exist(DLG_EXPORT_DBF)) m.enable(DLG_EXPORT_DBF, has_rs); if (m.exist(DLG_EXPORT_TEXT)) m.enable(DLG_EXPORT_TEXT, has_rs); if (m.exist(DLG_OUTPUTD)) { m.show(DLG_OUTPUTD, has_rs); m.show(DLG_OUTPUTF, has_rs); } while ((k = m.run()) != K_QUIT) { TReport & rep = get_report(m); TReport_book b(title()); TFilename name = get_output_filename(m, rep); bool ask_filename = name.blank(); export_type type = _export_printer; _user_key = ' '; switch (k) { case K_ENTER: case K_PRINT: type = _export_printer; break; case K_EXCEL: type = _export_excel; break; case K_EXPORT: type = _export_generic; break; case K_PDF: type = _export_pdf; break; case K_TEXT: type = _export_text; break; case K_DBF: type = _export_dbase; break; case K_VISUALIZE: type = _export_visualize; break; default: type = _export_user; _user_key = k; break; } execute_print(b, m, rep, type); switch (type) { case _export_printer: b.print_or_preview(); break; case _export_generic: esporta(b, m, rep); break; case _export_excel: export_excel(name, b, m, rep); break; case _export_pdf: export_pdf(name, b, m, rep); break; case _export_text: export_text(name, b, m, rep); break; case _export_dbase: export_dbase(name, b, m, rep); break; case _export_visualize: b.preview(); break; default: break; } } } } const char * TReport_application::get_output_filename(const TAutomask & mask, const TReport & rep) { TFilename output = get_mask().get(outputdir_id()); TString fname = get_mask().get(outputfile_id()); if (output.blank()) output.tempdir(); if (!dexist(output)) if (!make_dir(output)) { error_box(FR("Non posso creare %s uso il direttorio temporaneo"), (const char *)output); output.cut(0); output.tempdir(); } if (fname.blank()) { TString t(20); fname = output_name(mask, rep); _strtime_s(t.get_buffer(), t.size()); t.strip(":"); fname << '_' << today.date2ansi() << '_' << t; } output.add(fname); return get_tmp_string() = output; }