Page 1 of 1

GTK-Print-Dialog example (for use until PB-Version is fixed)

Posted: Mon Dec 03, 2007 8:20 pm
by walker
Due to a question in the bugs section - an example to use the GTK+ Print Dialog
It's a (quite quick) hacked example for using it. And the best.. it has a working preview( 8) )
At present only text can be printed.. from top to bottom.. without any layout... Further enhancements can be done... if requested....

Code: Select all

; ---------------------------------------------------------------------------------------
; using the GTK print dialog
; 2007 walker 
; not fully functional yet but shows how to use this dialog
; BUT with a working preview ;-)
; ---------------------------------------------------------------------------------------
;---------------- Preparations ---------------------------------------------------
;import needed functions
ImportC "/usr/lib/libgtk-x11-2.0.so"
  gtk_print_operation_new()
  gtk_print_operation_run(*op, action, *parent, *error);the print dialog
  gtk_print_settings_new()
  gtk_print_operation_set_print_settings(*print,*settings)
  gtk_print_context_create_pango_layout(*context)
  pango_layout_set_text(layout,txt.p-utf8,length)
  pango_font_description_from_string(font.p-utf8)
  pango_font_description_free(desc)
  pango_layout_set_font_description(layout,desc)
  cairo_move_to(cr,x,y)
  pango_cairo_show_layout(cr,layout)
  gtk_print_context_get_cairo_context(context)
  gtk_print_operation_set_n_pages(*op,n_pages)
  gtk_print_operation_set_unit(*op,unit)
  gtk_print_operation_get_status_string (*op); a (translated) string 
  gtk_print_operation_get_status (*op);the numer enumerated below
  gtk_print_operation_is_finished (*op);returns true or false
EndImport

;print actions
Enumeration 
  #GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
  #GTK_PRINT_OPERATION_ACTION_PRINT
  #GTK_PRINT_OPERATION_ACTION_PREVIEW
    #GTK_PRINT_OPERATION_ACTION_EXPORT
EndEnumeration
;return values from printdialog
Enumeration 
  #GTK_PRINT_OPERATION_RESULT_ERROR
  #GTK_PRINT_OPERATION_RESULT_APPLY
  #GTK_PRINT_OPERATION_RESULT_CANCEL
  #GTK_PRINT_OPERATION_RESULT_IN_PROGRESS
EndEnumeration
;status of printing
Enumeration 
  #GTK_PRINT_STATUS_INITIAL
  #GTK_PRINT_STATUS_PREPARING
  #GTK_PRINT_STATUS_GENERATING_DATA
  #GTK_PRINT_STATUS_SENDING_DATA
  #GTK_PRINT_STATUS_PENDING
  #GTK_PRINT_STATUS_PENDING_ISSUE
  #GTK_PRINT_STATUS_PRINTING
  #GTK_PRINT_STATUS_FINISHED
  #GTK_PRINT_STATUS_FINISHED_ABORTED
EndEnumeration  
;layout units
Enumeration 
  #GTK_UNIT_PIXEL
  #GTK_UNIT_POINTS
  #GTK_UNIT_INCH
  #GTK_UNIT_MM
EndEnumeration

ProcedureCDLL begin_print(*op,context, user_data)
;this procedure is called before rendering the pages...

;set number of pages.. ESSENTIAL!!!! never forget this! (or you'll wait forever)
gtk_print_operation_set_n_pages(*op,1);

EndProcedure

ProcedureCDLL  draw_page(*op,context,page_nr,user_data)
;this Procedure is called for each page to print.... so a fixed text like this example isn't a good idea
gtk_print_operation_set_unit (*op, #GTK_UNIT_MM);
cr= gtk_print_context_get_cairo_context(context)
layout = gtk_print_context_create_pango_layout (context);
pango_layout_set_text (layout, "Hello World!", -1);
desc = pango_font_description_from_string ("sans 28");
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);
pango_cairo_show_layout (cr, layout)
g_object_unref_(layout)

EndProcedure

ProcedureCDLL  print_preview(*op,*preview, *context, *parent, user_data)
;for your own preview routine...

EndProcedure

ProcedureCDLL end_print(*op, *context, user_data) 
;if printig is finished, you can add code here for some cleanups etc...

EndProcedure                                                        

;-------------------- DEMO -------------------------
OpenWindow(0,0,0,400,400,"printtest",#PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ButtonGadget(0,10,50,100,30,"Print")
CloseGadgetList()

Repeat  
event=WaitWindowEvent()

    If event=#PB_Event_Gadget
        If EventGadget()=0
            ;print button
            ;create a new printoperation
            print=gtk_print_operation_new()
            ;connect teh actions to a procedure
            g_signal_connect_(print, "begin-print", @begin_print(), user_data);
            g_signal_connect_(print, "draw-page", @draw_page(), user_data);
            ;If you activate the following line, the built in preview is DISABLED
            ; g_signal_connect_(print, "preview", @print_preview(), d);
            g_signal_connect_(print, "end-print", @end_print(), user_data);
            ;finally calling the GTK+ printrequester
            res=gtk_print_operation_run(print,#GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,WindowID(0),*error)
            ;If res returns  #GTK_PRINT_OPERATION_RESULT_CANCEL the user had clicked on cancel - else one of the callbacks is called
        EndIf   
    EndIf   

Until event=#PB_Event_CloseWindow

Re: GTK-Print-Dialog example (for use until PB-Version is fixed)

Posted: Fri Feb 12, 2010 2:55 pm
by lnxque

Re: GTK-Print-Dialog example (for use until PB-Version is fi

Posted: Fri Sep 11, 2015 5:08 pm
by collectordave
New to this so please forgive me if this is the wrong place to post.

Just started to use PB for windows and OSX and found that print and preview all seem to be Platform specific so I have started to write my own print and preview modules attempting to use only native PB commands from version 5.4 Beta to create a cross platform printing solution.

I have had some success and can now print Text, Lines, boxes and images anywhere on a page in portrait or landscape each page can have a different orientation etc. This so far works on Windows and MAC OSX.

I have not used Linux before but I do have some UNIX experience from 1982 but not much so no use.

Can anyone help by testing my code on Linux and/or provide me with some help in getting a list of printers available and the paper sizes supported by a selected printer?

Cheers