2
0

pedidos2.nut 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C) 2013 by Domingo Alvarez Duarte <[email protected]>
  3. *
  4. * Licensed under GPLv3, see http://www.gnu.org/licenses/gpl.html.
  5. */
  6. function _tr(str) {return str;}
  7. class Fl_Multiline_Output extends Fl_Output {
  8. constructor(px, py, pw, ph, pl=null){
  9. base.constructor(px, py, pw, ph, pl);
  10. type(4);
  11. }
  12. }
  13. enum Fl_Data_Table_Events {e_none, e_event, e_select, e_insert, e_update, e_delete};
  14. class Fl_Data_Table extends Flv_Data_Table {
  15. _cols_info = null;
  16. _data = null;
  17. _call_this = null;
  18. _forPrint = null;
  19. constructor(px, py, pw, ph, pl=null){
  20. base.constructor(px, py, pw, ph, pl);
  21. _cols_info = [];
  22. _data = [];
  23. _forPrint = false;
  24. has_scrollbar(FLVS_BOTH);
  25. }
  26. function for_print(bval){_forPrint = bval; }
  27. function resize(ax, ay, aw, ah){
  28. base.resize(ax, ay, aw, ah);
  29. calc_cols();
  30. }
  31. function clear_data_rows(){
  32. col(0);
  33. cols(0);
  34. row(0);
  35. rows(0);
  36. _data.clear();
  37. }
  38. function recalc_data(){
  39. rows(_data.size());
  40. redraw();
  41. }
  42. function set_data(data_array){
  43. _data = data_array;
  44. recalc_data();
  45. }
  46. function set_new_data(data_array){
  47. clear_data_rows();
  48. _data = data_array;
  49. local mycols = _data[0];
  50. _data.remove(0);
  51. set_cols(mycols);
  52. recalc_data();
  53. }
  54. function get_data_value(arow, acol){ return _data[arow][acol];}
  55. function get_value(arow, acol){
  56. if(acol < 0)
  57. {
  58. if(arow < 0) return "#";
  59. return format("%d", arrow+1);
  60. }
  61. if(arow < 0) return _cols_info[acol].header;
  62. local ci = _cols_info[acol];
  63. local value = _data[arow][acol];
  64. switch(ci.format)
  65. {
  66. case 'M': return math.number_format(value.size() ? value.tofloat() : 0, 2);
  67. break;
  68. case 'P': return math.number_format(value.size() ? value.tofloat() : 0, 3);
  69. break;
  70. case 'N': return math.number_format(value.size() ? value.tofloat() : 0, -6);
  71. break;
  72. case 'Z': //Empty when zero
  73. case 'D': //Zero monetary //ZM':
  74. {
  75. if(value == "0" || value == "") return "";
  76. else
  77. {
  78. if(ci.format == 'Z') return math.number_format(value.tofloat(), -6);
  79. else return math.number_format(value.tofloat(), 2);
  80. }
  81. }
  82. break;
  83. case 'B': return (value == "1" ? "@-3+" : "");
  84. break;
  85. case 'S': return value.tostring();
  86. break;
  87. case '@': return value.replace("@", "@@"); //escape any @ {
  88. break;
  89. }
  90. return value;
  91. }
  92. function get_style(style, Row, Col){
  93. base.get_style(style, Row, Col);
  94. if(Row >= 0 && Col >= 0)
  95. {
  96. local rs = row_style().get(Row);
  97. local cs = col_style().get(Col);
  98. local have_bg = 0;
  99. if(cs.background_defined()) have_bg = cs.background();
  100. else if(rs.background_defined()) have_bg = rs.background();
  101. if((Row % 2) == 0)
  102. {
  103. if(have_bg)
  104. //s:background(fltk.fl_color_average(207, have_bg, 0.5))
  105. style.background(have_bg);
  106. else
  107. style.background(207);
  108. }
  109. else
  110. {
  111. if(have_bg)
  112. //s:background(have_bg)
  113. style.background(fl_color_average(FL_WHITE, have_bg, 0.5));
  114. else
  115. style.background(FL_WHITE);
  116. }
  117. }
  118. else if(_forPrint)
  119. {
  120. style.frame(FL_NO_BOX);
  121. }
  122. }
  123. function handle(event){
  124. switch(event){
  125. case FL_RELEASE:{
  126. local done = false;
  127. if(Fl.event_clicks() > 0){
  128. row_selected(Fl_Data_Table_Events.e_update);
  129. done = true;
  130. }
  131. if(done){
  132. Fl.event_clicks(0);
  133. return 1;
  134. }
  135. }
  136. break;
  137. case FL_KEYBOARD:
  138. local key = Fl.event_key();
  139. switch(key){
  140. case FL_KP_Plus:
  141. if(Fl.event_ctrl()) break;
  142. case FL_Insert:
  143. row_selected(Fl_Data_Table_Events.e_insert);
  144. break;
  145. //case FL_KP_Minus:
  146. // if(Fl::event_ctrl()) break;
  147. case FL_Delete:
  148. row_selected(Fl_Data_Table_Events.e_delete);
  149. break;
  150. case FL_KP_Enter:
  151. case FL_Enter:
  152. case FL_Key_Space:
  153. if(!Fl.event_ctrl()){
  154. row_selected(Fl_Data_Table_Events.e_update);
  155. }
  156. break;
  157. }
  158. break;
  159. }
  160. local rc = base.handle(event);
  161. return rc;
  162. }
  163. function row_selected(ev){ if(_call_this) _call_this.row_selected(this, ev);}
  164. function set_cols(mycols, size_absolute=false){
  165. _cols_info.clear();
  166. for(local i=0, max_cols=mycols.size(); i < max_cols; ++i){
  167. local col_info = {};
  168. parse_field_header(mycols[i], col_info);
  169. _cols_info.push(col_info);
  170. }
  171. cols(_cols_info.size());
  172. calc_cols(size_absolute);
  173. }
  174. function parse_field_header(str, col_info){
  175. local ci = str.split('|');
  176. local ci_size = ci.size();
  177. local curr_ci = 0;
  178. col_info.colname <- (ci_size > curr_ci++) ? ci[curr_ci-1] : "?";
  179. col_info.header <- (ci_size > curr_ci++) ? ci[curr_ci-1] : "?";
  180. col_info.width <- (ci_size > curr_ci++) ? ci[curr_ci-1].tofloat() : 10;
  181. col_info.align <- (ci_size > curr_ci++) ? ci[curr_ci-1][0] : 'L';
  182. col_info.format <- (ci_size > curr_ci++) ? ci[curr_ci-1][0] : '\0';
  183. col_info.color <- (ci_size > curr_ci++) ? ci[curr_ci-1].tointeger() : 0;
  184. col_info.bgcolor <- (ci_size > curr_ci++) ? ci[curr_ci-1].tointeger() : 0;
  185. if(ci_size == 1) {
  186. col_info.colname = str;
  187. col_info.header = str;
  188. }
  189. }
  190. function calc_cols(size_absolute=false){
  191. calc_cols_width(size_absolute);
  192. for(local k=0, len = cols(); k < len; ++k)
  193. {
  194. local v = _cols_info[k];
  195. //v.header = _tr(v.header.c_str());
  196. local col_align;
  197. switch(v.align)
  198. {
  199. case 'C':
  200. col_align = FL_ALIGN_CENTER;
  201. break;
  202. case 'R':
  203. col_align = FL_ALIGN_RIGHT;
  204. break;
  205. default:
  206. col_align = FL_ALIGN_LEFT;
  207. }
  208. local cs = col_style().get(k);
  209. cs.align(col_align);
  210. if(v.color) cs.foreground(v.color);
  211. if(v.bgcolor) cs.background(v.bgcolor);
  212. }
  213. }
  214. function calc_cols_width(size_absolute=false){
  215. if(size_absolute){
  216. for(local k=0, len = cols(); k < len; ++k)
  217. {
  218. local cs = col_style().get(k);
  219. cs.width(_cols_info[k].width);
  220. }
  221. return;
  222. }
  223. local grid_width, total_widths, char_width;
  224. grid_width = w();
  225. if(has_scrollbar()) grid_width -= scrollbar_width();
  226. //char_width = self:labelsize() --fltk.fl_width('w')
  227. //it seems that if not set the text font on fluid
  228. //we get segfault here
  229. fl_font(textfont(), textsize());
  230. local gs = global_style();
  231. gs.height(fl_height()+fl_descent());
  232. char_width = fl_width("w");
  233. draw_offset(char_width/3);
  234. //print(grid_width, char_width)
  235. total_widths = 0;
  236. for(local k=0, len = cols(); k < len; ++k)
  237. {
  238. local v = _cols_info[k].width;
  239. if(v > 0) total_widths += ((v * char_width) + 1);
  240. }
  241. for(local k=0, len = cols(); k < len; ++k)
  242. {
  243. local v = _cols_info[k];
  244. local col_width = v.width;
  245. if( col_width < 0) col_width = grid_width - total_widths;
  246. else col_width *= char_width;
  247. local cs = col_style().get(k);
  248. cs.width(col_width);
  249. }
  250. }
  251. function get_selection(ar_out, withIds=false){
  252. }
  253. function clear_selection(){
  254. }
  255. function get_row_id(arow=null){
  256. if(arow == null) arow = row();
  257. return _data[arow][0].tointeger();
  258. }
  259. }
  260. class Base_Window extends Fl_Window {
  261. _db_map = null;
  262. constructor(px, py, pw, ph, pl){
  263. base.constructor(px, py, pw, ph, pl);
  264. _db_map = {};
  265. }
  266. function add_input_field_to_map(tbl, fldname, fld){
  267. local tbl_map = table_get(_db_map, tbl, false);
  268. if(!tbl_map){
  269. tbl_map = {};
  270. _db_map[tbl] <- tbl_map;
  271. }
  272. tbl_map[fldname] <- fld;
  273. }
  274. }
  275. class Fl_Multiline_Input extends Fl_Input {
  276. constructor(px, py, pw, ph, pl=null){
  277. base.constructor(px, py, pw, ph, pl);
  278. type(4);
  279. }
  280. }
  281. local function _do_delayed_focus (widget){
  282. widget->take_focus();
  283. }
  284. function delayed_focus (widget){
  285. Fl.add_timeout(0.05, _do_delayed_focus, widget);
  286. }
  287. dofile("pedidos2-gui.nut");
  288. class Pedidos2 extends PedidosWindow {
  289. // Declaration of class members
  290. menu_bar_deactivated_menus = null;
  291. pedidos_edit_record = null;
  292. constructor(){
  293. base.constructor();
  294. }
  295. function row_selected(sender, ev){
  296. }
  297. function handle(event){
  298. if (event == FL_KEYBOARD && Fl.event_command() == 0){
  299. local key = Fl.event_key();
  300. //print(key)
  301. switch(key){
  302. case FL_Menu: menu_bar_navigate(); break;
  303. case FL_F1: mostrar_ventana_ayuda(); break;
  304. case FL_F2: mostrar_ventana_pedido(); break;
  305. case FL_F3: mostrar_ventana_pedidos(); break;
  306. case FL_F4: mostrar_ventana_clientes(); break;
  307. case FL_F5: mostrar_ventana_articulos(); break;
  308. case FL_F6: mostrar_ventana_totales(); break;
  309. case FL_F9: mostrar_ventana_opciones(); break;
  310. }
  311. }
  312. return base.handle(event);
  313. }
  314. function reset_menus_desactivados(){
  315. if (menu_bar_deactivated_menus){
  316. local mb = menu_bar;
  317. foreach(k,v in menu_bar_deactivated_menus) mb.menu_at(v).deactivate();
  318. }
  319. }
  320. function reset_menus_desactivados_activando(list){
  321. if (menu_bar){
  322. local mb = menu_bar;
  323. reset_menus_desactivados();
  324. foreach(k,v in list) mb.menu_at(v).activate();
  325. }
  326. }
  327. function menu_bar_navigate(){
  328. local mb = menu_bar;
  329. local v = mb.menu().pulldown(mb.x(), mb.y(), mb.w(), mb.h(), mb.menu_at(0), mb, null, 1);
  330. mb.picked(v);
  331. }
  332. function mostrar_ventana_ayuda(){
  333. }
  334. function mostrar_ventana_pedido(){
  335. tabs.value(tab_pedido);
  336. if (pedidos_edit_record && pedidos_edit_record.id)
  337. delayed_focus(pedido_lineas_codigo);
  338. else
  339. delayed_focus(pedidos_cliente_codigo_r);
  340. reset_menus_desactivados_activando([
  341. menu_pedido_guardar,
  342. menu_pedido_borrar,
  343. menu_pedido_imprimir,
  344. ]);
  345. }
  346. function mostrar_ventana_pedidos(){
  347. tabs.value(tab_pedidos_lista);
  348. }
  349. function mostrar_ventana_clientes(){
  350. tabs.value(tab_clientes_lista);
  351. }
  352. function mostrar_ventana_articulos(){
  353. tabs.value(tab_articulos_lista);
  354. }
  355. function mostrar_ventana_totales(){
  356. tabs.value(tab_totales);
  357. }
  358. function mostrar_ventana_opciones(){
  359. tabs.value(tab_opciones);
  360. }
  361. }
  362. Fl.scheme("gtk+");
  363. Fl.visual(FL_RGB);
  364. //allow arrow keys navigation
  365. Fl.option(Fl.OPTION_ARROW_FOCUS, true);
  366. local win = new Pedidos2();
  367. //local win = new SalesTaxRatesEditWindow();
  368. win->resizable(win);
  369. win->show_main();
  370. Fl.run();