GTK-Print-Dialog example (for use until PB-Version is fixed)
Posted: Mon Dec 03, 2007 8:20 pm
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(
)
At present only text can be printed.. from top to bottom.. without any layout... Further enhancements can be done... if requested....
It's a (quite quick) hacked example for using it. And the best.. it has a working preview(

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