------------------ namespace printer ------------------ constant version = "4.9.4" include GtkEngine.e include std/datetime.e -------------------------------------------------------------------------------- -- This version handles most common printing needs, but it will not yet respect -- 'marked up' a.k.a. 'rich' text, i.e. text with colors and styles such as -- set by test59. It just prints them as plain text. -- However, it DOES print text marked up with GTK's HTML subset, so you can use -- , , , length(text) then set(progress,"text","Printing complete") return 0 end if if show_progress then set(progress,"text",sprintf("Printing page %d",pg)) set(progress,"fraction",pg/n_pages) end if ifdef DELAY then sleep(0.25) end ifdef object details = { page_title,sub_title,file_name,short_name, pg,n_pages,n_copies, today,user,real_name,font,filesize,timestamp,export_file } object page if atom(header) then header = "[1] page [5] of [6]\n\n" end if if pg = 1 or atom(subheader) then page = text:format(header,details) & flatten(text[pg]) & text:format(footer,details) else page = text:format(subheader,details) & flatten(text[pg]) & text:format(footer,details) end if set(pl,"markup",page,length(page)) set(pl,"update layout",cr) set(pl,"show layout",cr) ifdef PRINT then printf(1,"Page %d\n",pg) end ifdef return 1 end function ------------------------------------------------------------------------ function process_text(object txt) ------------------------------------------------------------------------ txt = split(txt,'\n') integer comment object a,b object test for i = 1 to length(txt) do -- replace chars which will confuse markup txt[i] = join(split(txt[i],'&'),"&") txt[i] = join(split(txt[i],"&amp;"),"&") if sourcecode then txt[i] = join(split(txt[i],'<'),"<") txt[i] = join(split(txt[i],'>'),">") end if if use_color then comment = match("--",txt[i]) if comment then comment -=1 txt[i] = txt[i][1..comment] & "" & txt[i][comment+1..$] & "" end if end if if use_line_numbers then txt[i] = text:format(line_number_format,{i,txt[i]}) else txt[i] &= '\n' end if end for txt = breakup(txt,lines_per_page) if n_pages = 0 then -- no selection n_pages = length(txt) end if return txt end function ------------------------------------------------------------------------ export function end_print() ------------------------------------------------------------------------ status_string = "Printing complete" ifdef PRINT then display(status_string) end ifdef return 1 end function --------------------------------------------------------------- export function setup_printer() --------------------------------------------------------------- atom _size = create(GtkPaperSize,paper_name) atom err = allocate(16) err = 0 object results = 0 atom fn7 = define_func("gtk_print_operation_run",{P,I,P,P},I) atom fn8 = define_func("gtk_print_run_page_setup_dialog",{P,P,P},P) set(settings,"paper size",_size,units) set(settings,"n copies",n_copies) set(settings,"collate",collate) set(settings,"duplex",duplex) set(settings,"reverse",order) set(settings,"scale",scale) set(settings,"quality",quality) set(settings,"number up",number_up) set(settings,"number up layout",number_up_layout) if string(name) then set(settings,"printer",name) end if atom setup = create(GtkPageSetup) set(setup,"paper size",_size) set(setup,"orientation",orientation) set(setup,"left margin",left_margin,units) set(setup,"right margin",right_margin,units) set(setup,"top margin",top_margin,units) set(setup,"bottom margin",bottom_margin,units) atom printop = create(GtkPrintOperation) set(printop,"print settings",settings) set(printop,"default page setup",setup) set(printop,"show progress",show_progress) set(printop,"track print status",track_status) set(printop,"embed page setup",embed_page_setup) set(printop,"support selection",support_selection) set(printop,"has selection",has_selection) set(printop,"use full page",use_full_page) if action = GTK_PRINT_OPERATION_ACTION_EXPORT then export_file = canonical_path(export_file) set(printop,"export filename",export_file) end if if string(jobname) then set(printop,"job name",jobname) end if if custom_tab_hook != 0 then set(printop,"custom tab label",custom_tab_label) connect(printop,"create-custom-widget",custom_tab_func,printop) connect(printop,"custom-widget-apply",custom_tab_hook) end if connect(printop,"status-changed",signal_status_changed) connect(printop,"begin-print",signal_begin_print) connect(printop,"draw-page",signal_draw_page) connect(printop,"end-print",signal_end_print) connect(printop,"request-page-setup",signal_request_page_setup) connect(printop,"done",signal_done) connect(printop,"ready",signal_ready) connect(printop,"got-page-size",signal_got_page_size) c_func(fn7,{printop,action,parent,err}) -- start the print process; if string(settings_file) then get(settings,"to file",settings_file) end if if string(setup_file) then get(setup,"to file",setup_file) end if object jobname = get(printop,"job name") if confirm then if action = GTK_PRINT_OPERATION_ACTION_EXPORT then if Question(0,"PDF Written", sprintf("%s\nFolder: %s", {filename(export_file),pathname(export_file)}), sprintf("%s\nStatus: %d\n%s\nClick Yes to view",{jobname,status_code,status_string}) ,,,"cups") then show_uri(export_file) end if else Info(0,"Print Job",jobname, sprintf("Status: %d %s",{status_code,status_string}) ,,"cups") end if end if page_title = 0 n_pages = 0 n_copies = 1 action = GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG return 1 end function header = "[1]\n\n" ------------------------- -- © 2015 by Irv Mullins -------------------------