I've found why my example was behaving strange when trying print on both "Printer" and "PSFile", inside function "start_job" the "Fl_Device *current" is set and all following print commands goes to the last device set.

As well I was setting one of the box labels via a temporary stl::string returned by an stl::stringstream:

before: sales_invoice->invoicePageBox->label(ss_page.str().c_str());

now: sales_invoice->invoicePageBox->copy_label(ss_page.str().c_str());

In the main loop for the printing I'm calling Fl::wait() to just in case the print job is length, allowing user interaction with the rest of the program.

But then I found a possible problem, if there is only one "Fl_Device" active at any time the rest of the user interface can't be used because:

- Fl_Device can be switched to display/Screen and all other print commands goes there. - Fl_Device isn't switched and any new update to display/screen goes to printer.

This solution isn't thread safe !!!!!!!!

Obs.: The postscript generation file doesn't handle UTF-8 chars.
# data file for the Fltk User Interface Designer (fluid)
version 1.0300 
header_name {.h} 
code_name {.cxx}
decl {\#include <FL/Fl_Double_Window.H>} {public global
} 

decl {\#include <FL/Fl_Input.H>} {public global
} 

decl {\#include <FL/Fl_Help_View.H>} {public global
} 

decl {\#include <FL/Fl_Table.H>
\#include <FL/Fl_Printer.H>} {public global
} 

decl {\#include <FL/Fl_Printer.H>} {public global
} 

decl {\#include <string>} {public global
} 

decl {\#include <sstream>} {public global
} 

decl {template<typename T>
void show_create(T*& aptr) {
  if (!aptr) aptr = new T();
  aptr->show();
}} {
  comment {This template checks if the pointer is already assigned,
if not it creates a new object.} public global
} 

Function {validate_product_id(Fl_Input *o, void *v)} {return_type void
} {
  code {//printf("Hello, World!\\n");} {}
} 

class DAD_Base_Window {
  comment {This is the base class for all application windows and is used to 
hold common code/functionality.} open : {public Fl_Double_Window}
} {
  decl {DAD_Base_Window *callee;} {protected local
  }
  Function {DAD_Base_Window(int X, int Y, int W, int H, const char *L) : 
Fl_Double_Window(X, Y, W, H, L)} {open
  } {
    code {callee = NULL;} {}
  }
  Function {DAD_Base_Window(int W, int H, const char *L):Fl_Double_Window(0, 0, 
W, H, L)} {open
  } {
    code {//printf("Hello, World!\\n");} {}
  }
  Function {make_window_when_null(DAD_Base_Window &aref)} {open return_type 
{static DAD_Base_Window*}
  } {
    code {//if (!aref) aref = 
return &aref;} {}
  }
} 

widget_class DAD_Invoice_Print {
  label {Invoice Print} open
  xywh {82 59 658 692} type Double color 7 resizable
  class Fl_Double_Window visible
} {
  Fl_Scroll pi_scroll {open
    xywh {4 2 650 688} color 7
  } {
    Fl_Group pageA4 {open
      xywh {20 10 628 685} color 7
    } {
      Fl_Box logo {
        label FLTK
        image {../help/logotipo-santiagopidal-s.bmp} xywh {28 20 200 41} 
labeltype SHADOW_LABEL labelfont 3 labelsize 47 align 148
      }
      Fl_Box companyBox {
        label {FLTK (pronounced "fulltick") is a cross-platform C++ GUI toolkit 
for UNIX®/Linux® (X11), Microsoft® Windows®, and MacOS® X. FLTK provides 
modern GUI functionality without the bloat and supports 3D graphics via 
OpenGL® and its built-in GLUT emulation.}
        xywh {28 66 308 80} labelfont 2 labelsize 12 align 144
      }
      Fl_Group invoiceNumber {open
        xywh {347 20 286 40} box BORDER_FRAME color 31
      } {
        Fl_Box box_header_number_date {
          xywh {348 21 284 18} box FLAT_BOX color 47 labeltype NO_LABEL
        }
        Fl_Box hline_header_number_date {
          xywh {347 39 286 1} box BORDER_FRAME color 31 labeltype NO_LABEL 
labelsize 18
        }
        Fl_Box vline_header_number_date {
          xywh {492 20 1 40} box BORDER_FRAME color 39 labeltype NO_LABEL
        }
        Fl_Box header_date_box {
          label DATE
          xywh {492 20 139 20} labelfont 1 labelsize 12
        }
      }
      Fl_Box box_to {
        xywh {347 65 286 85} box BORDER_FRAME color 31 labelfont 1 labelsize 16 
align 21
      }
      Fl_Box box_header {
        xywh {28 160 606 20} box BORDER_FRAME color 31
      }
      Fl_Box box_header_fill {
        xywh {29 161 604 18} box FLAT_BOX color 47 labeltype NO_LABEL labelsize 
18
      }
      Fl_Box header_code_box {
        label CODE
        xywh {29 161 54 18} color 47 labelfont 1 labelsize 10
      }
      Fl_Box header_description_box {
        label DESCRIPTION
        xywh {82 161 338 18} color 47 labelfont 1 labelsize 10
      }
      Fl_Box header_quantity_box {
        label {QTY.}
        xywh {421 161 41 18} color 47 labelfont 1 labelsize 10
      }
      Fl_Box header_price_box {
        label PRICE
        xywh {461 161 64 18} color 47 labelfont 1 labelsize 10
      }
      Fl_Box header_discount_box {
        label {DISC.}
        xywh {525 161 40 18} color 47 labelfont 1 labelsize 10
      }
      Fl_Box header_subtotal_box {
        label SUBTOTAL
        xywh {567 161 66 18} color 47 labelfont 1 labelsize 10
      }
      Fl_Box box_totals {
        xywh {28 615 606 30} box BORDER_FRAME color 31
      }
      Fl_Box notes_box {
        label {FLTK is provided under the terms of the GNU Library Public 
License, Version 2 with exceptions that allow for static linking.}
        xywh {28 648 606 37} labelfont 2 labelsize 12 align 149
      }
      Fl_Box box_header_totals {
        xywh {28 596 606 20} box BORDER_FRAME color 31
      }
      Fl_Box box_header_totals_fill {
        xywh {29 597 604 18} box FLAT_BOX color 47
      }
      Fl_Box vline_totals_1 {
        xywh {92 596 1 49} box BORDER_FRAME color 31 labeltype NO_LABEL
      }
      Fl_Box {} {
        xywh {217 596 1 49} box BORDER_FRAME color 31 labeltype NO_LABEL
      }
      Fl_Box {} {
        xywh {349 596 1 49} box BORDER_FRAME color 31 labeltype NO_LABEL
      }
      Fl_Box {} {
        xywh {488 596 1 49} box BORDER_FRAME color 31 labeltype NO_LABEL
      }
      Fl_Box header_totals_page_box {
        label PAGE
        xywh {29 597 63 18} color 47 labelfont 1 labelsize 12
      }
      Fl_Box header_totals_subtotal_box {
        label SUBTOTAL
        xywh {93 597 124 18} color 47 labelfont 1 labelsize 12
      }
      Fl_Box header_totals_vat_percent_box {
        label {V.A.T. %}
        xywh {218 597 131 18} color 47 labelfont 1 labelsize 12
      }
      Fl_Box header_totals_vat_amount_box {
        label {TOTAL V.A.T.}
        xywh {351 597 137 18} color 47 labelfont 1 labelsize 12
      }
      Fl_Box header_totals_total_box {
        label TOTAL
        xywh {489 597 144 18} color 47 labelfont 1 labelsize 12
      }
      Fl_Box invoice_lines_box {
        image {/home/mingo/dadbiz-1.8/dadbiz/help/alfarero2.bmp} xywh {28 179 
606 406} box BORDER_FRAME color 31
      }
      Fl_Box vline_lines_1 {
        xywh {82 160 1 425} box BORDER_FRAME color 31
      }
      Fl_Box {} {
        xywh {420 160 1 425} box BORDER_FRAME color 31
      }
      Fl_Box {} {
        xywh {461 160 1 425} box BORDER_FRAME color 31
      }
      Fl_Box {} {
        xywh {524 160 1 425} box BORDER_FRAME color 31
      }
      Fl_Box {} {
        xywh {566 160 1 425} box BORDER_FRAME color 31
      }
      Fl_Group printDataGroup {open
        xywh {20 10 628 637}
      } {
        Fl_Box invoiceTypeBox {
          label {Invoice Nº}
          xywh {350 20 139 20} labelfont 1 labelsize 12
        }
        Fl_Box numberBox {
          label {2010/4687}
          xywh {350 40 139 20} labelfont 5 labelsize 15 align 16
        }
        Fl_Box dateBox {
          label {01/03/2010}
          xywh {496 40 133 20} labelfont 5 labelsize 15 align 16
        }
        Fl_Box clientBox {
          label {Domingo Alvarez Duarte
50.566.789-L
c/ Felicidad, 34 4ºB
29999 - Málaga
Málaga - Spain}
          xywh {349 66 277 82} labelsize 12 align 84
        }
        Fl_Group dataLinesGroup {open
          xywh {28 179 605 407} align 16
        } {
          Fl_Box codeBox {
            label {123
345
567
675
567
}
            xywh {29 180 52 404} labelsize 11 align 25
          }
          Fl_Box descriptionBox {
            label {Ceramica
Barro
Card
France
Debit
AZULEJOS BLANCOS 15X15/45 PRODESCO
ESMALTE  DE ALTA TEMPERATURA ANARANJADO 0-6002}
            xywh {86 180 332 404} labelsize 11 align 85
          }
          Fl_Box quantityBox {
            label {123
345
567
675
567
}
            xywh {422 180 38 404} labelsize 11 align 25
          }
          Fl_Box priceBox {
            label {12.456,88
345
567
675
567
}
            xywh {463 180 61 404} labelsize 11 align 25
          }
          Fl_Box discountBox {
            label {123
345
567
675
567
}
            xywh {526 180 39 404} labelsize 11 align 25
          }
          Fl_Box lineTotalBox {
            label {23.456,90
345
567
675
567
}
            xywh {568 180 63 404} labelsize 11 align 25
          }
        }
        Fl_Group {} {open
          xywh {28 595 609 52}
        } {
          Fl_Box invoicePageBox {
            label {1/1}
            xywh {28 618 65 25} labelfont 5 labelsize 15
          }
          Fl_Box fisrtTotalBox {
            label {12.345,78}
            xywh {95 618 120 25} labelfont 5 labelsize 15
          }
          Fl_Box vatPercentageBox {
            label 16
            xywh {223 618 125 25} labelfont 5 labelsize 15
          }
          Fl_Box vatAmountBox {
            label {12.345,78}
            xywh {350 618 138 25} labelfont 5 labelsize 15
          }
          Fl_Box totalBox {
            label {12.345,78}
            xywh {488 618 144 25} labelfont 5 labelsize 15
          }
        }
      }
    }
  }
} 

widget_class DAD_Main_Window {
  label {DADBIZ Sales & Inventory} open
  xywh {8 28 336 286} type Double hide
  class DAD_Base_Window
} {
  decl {DAD_Invoice_Print *sales_invoice;} {public local
  }
  Function {~DAD_Main_Window()} {open return_type virtual
  } {
    code {delete sales_invoice;} {}
  }
  code {sales_invoice = NULL;} {}
  Fl_Tabs tabs {open
    xywh {10 11 315 266}
  } {
    Fl_Group tabSales {
      label Sales open
      xywh {10 43 315 234} color 127 labelsize 20
    } {
      Fl_Button btnPreview {
        label {Invoice Preview}
        callback {show_create(sales_invoice);
generate_invoice();}
        xywh {30 61 275 55} box PLASTIC_UP_BOX labeltype SHADOW_LABEL labelfont 
3 labelsize 32 align 16
      }
      Fl_Counter pageCount {
        label Pages
        xywh {60 161 220 30} labeltype SHADOW_LABEL labelfont 3 labelsize 31 
align 1 minimum 1 maximum 20 step 1 value 1 textsize 32
      }
      Fl_Button btnPrint {
        label {Print Invoice}
        callback {print_invoice()}
        xywh {30 201 275 55} box PLASTIC_UP_BOX labeltype SHADOW_LABEL 
labelfont 3 labelsize 32
      }
    }
    Fl_Group tabBuys {
      label Buys open
      xywh {10 43 315 234} color 174 labelsize 20 hide
    } {}
    Fl_Group tabConfig {
      label Config open
      xywh {10 44 315 232} labelsize 20 hide
    } {}
  }
  Function {generate_invoice()} {open return_type void
  } {
    code {std::ostringstream  ss_code, ss_description, ss_quantity, ss_price, 
ss_discount, ss_line_total;
double dbl_subtotal, dbl_vat, dbl_vat_total, dbl_total;

dbl_subtotal = dbl_vat_total = dbl_total = 0.0;
dbl_vat = 16.0;

for(int i=1; i < 28; i++){
        int icode = i * 1000;
        double line_price = i * 1.53;
        double dbl_line_total = (line_price * i) ;

        dbl_subtotal += dbl_line_total;

        ss_code << icode << "\\n";
        ss_description << "Implementantion of function " << icode << "\\n";
        ss_quantity << i << "\\n";
        ss_price << line_price << "\\n";
        //ss_discount << 5 << "\\n";
        ss_line_total << dbl_line_total << "\\n";
}

sales_invoice->codeBox->copy_label(ss_code.str().c_str());
sales_invoice->descriptionBox->copy_label(ss_description.str().c_str());
sales_invoice->quantityBox->copy_label(ss_quantity.str().c_str());
sales_invoice->priceBox->copy_label(ss_price.str().c_str());
sales_invoice->discountBox->copy_label(ss_discount.str().c_str());
sales_invoice->lineTotalBox->copy_label(ss_line_total.str().c_str());} {}
  }
  Function {print_invoice()} {open return_type void
  } {
    code {int pages = pageCount->value();
show_create(sales_invoice);
generate_invoice();

Fl::wait();

std::ostringstream ss_page;

//FILE *file_ps_out = fopen("output.ps", "w");
Fl_PSfile_Device ps_device;
Fl_Printer printer;
//p.page(Fl_Printer::A4);
//p.place(g, 70, 70, p->page_width() - 140, p->page_height() - 140,  
FL_ALIGN_CENTER);
int w, h;
float scale_tmp, scale;
printer.start_job(1);
ps_device.start_job(1);

for(int i=1; i <= pages; i++) {
    //calling Fl::wait() can switch context/device
    Fl::wait();
    ss_page.str("");
    ss_page << i << "/" << pages;
    sales_invoice->invoicePageBox->copy_label(ss_page.str().c_str());

    //postscript generation
    ps_device.set_current();
    if( ps_device.start_page() ) break;
    w = h = 0;
    ps_device.printable_rect(&w,&h);
    // scale the printer device so that the window fits on the page
    scale = 1;
    if (sales_invoice->pageA4->w() > w || sales_invoice->pageA4->h() > h) {
    scale = (float)w/sales_invoice->pageA4->w();
    scale_tmp = (float)h/sales_invoice->pageA4->h();
    if ( scale_tmp < scale) scale = scale_tmp;
    ps_device.scale(scale, scale);
    }
        ps_device.print_widget(sales_invoice->pageA4);
        ps_device.end_page();


    //Printer
    printer.set_current();
    if( printer.start_page() ) break;
    w = h = 0;
    printer.printable_rect(&w,&h);
    // scale the printer device so that the window fits on the page
    scale = 1;
    if (sales_invoice->pageA4->w() > w || sales_invoice->pageA4->h() > h) {
    scale = (float)w/sales_invoice->pageA4->w();
    scale_tmp = (float)h/sales_invoice->pageA4->h();
    if ( scale_tmp < scale) scale = scale_tmp;
    printer.scale(scale, scale);
    }
        printer.print_widget(sales_invoice->pageA4);
        printer.end_page();

}
ps_device.end_job();
printer.end_job();
//fclose(file_ps_out);} {selected
    }
  }
} 

Function {} {open
} {
  code {DAD_Main_Window* w = new DAD_Main_Window();
w->show(argc, argv);} {}
} 

widget_class DAD_Address_Book_Print {
  label {Address Book Print} open
  xywh {8 28 658 692} type Double color 7 resizable
  class Fl_Double_Window visible
} {
  Fl_Scroll pi_scroll {open
    xywh {4 2 650 688} color 7
  } {
    Fl_Group pageA4 {open
      xywh {15 5 628 685} color 7
    } {
      Fl_Box hline_header_top {
        xywh {20 70 620 2} box BORDER_FRAME color 31 labeltype NO_LABEL 
labelsize 18
      }
      Fl_Box hline_header_bottom {
        xywh {20 646 620 2} box BORDER_FRAME color 31 labeltype NO_LABEL 
labelsize 18
      }
      Fl_Box {} {
        label {Address Book}
        xywh {130 19 335 21} labelsize 18
      }
      Fl_Browser {} {
        xywh {20 75 620 567} labelsize 18 textsize 18
      }
    }
  }
} 
_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to