class Print

Constants

FONT
FONT_SIZE
FONT_SMALL
FONT_SMALL_SIZE

Public Class Methods

new(parent_window, text) click to toggle source
Calls superclass method
# File gtk2/sample/misc/print.rb, line 42
def initialize (parent_window, text)
    super()
    @parent_window = parent_window
    @text = text

    # with this option disabled, the origin is the the upper left corner
    # *taking into consideration margins* !
    self.use_full_page = false
    self.unit = Gtk::PaperSize::UNIT_POINTS

    # set default paper size
    page_setup = Gtk::PageSetup.new
    paper_size = Gtk::PaperSize.new(Gtk::PaperSize.default)
    page_setup.paper_size_and_default_margins = paper_size
    self.default_page_setup = page_setup

    # show a progress bar
    self.show_progress = true        

    # You must choose between "paginate" and "begin-print".
    # "begin-print" is emitted only once and "paginate" is emitted
    # until the block returns true.
    # In both cases, you must set the number of pages at the end with
    # Gtk::PrintOperation#n_pages=
    signal_connect("begin-print") do |pop, context|
        puts "calls begin-print"
        cr = context.cairo_context
        paragraphs = @text.split("\n")
        layouts = []
        paragraphs.each do |para|
            layouts << create_layout(cr, para)
        end

        # distribute paragraph layouts among pages
        @page_layouts = []

        curr_height = 0
        n_pages = 0
        layouts.each do |layout|
            height = layout.height_in_points
            if curr_height + height > real_page_height
                n_pages += 1
                curr_height = 0
            end
            @page_layouts[n_pages] ||= []
            @page_layouts[n_pages] << layout
            curr_height += height
        end

        pop.n_pages = n_pages + 1
    end

    signal_connect("draw-page") do |pop, context, page_num|
        puts "calls draw-page %d" % page_num

        cr = context.cairo_context

        x, y = [0,0] # this means we start at the upper left margin  

        if @page_layouts[page_num]
            @page_layouts[page_num].each do |layout|
                cr.move_to(x,y)
                cr.show_pango_layout(layout)            
                y += layout.height_in_points
            end
            total_pages = @page_layouts.length
        else
            total_pages = 1
        end
        
        # page_num starts at 0
        draw_footer(cr, page_num + 1, total_pages)
    end
end

Public Instance Methods

run_preview() click to toggle source
# File gtk2/sample/misc/print.rb, line 131
def run_preview
    res = run(ACTION_PREVIEW, @parent_window)
end
run_print_dialog() click to toggle source
# File gtk2/sample/misc/print.rb, line 117
def run_print_dialog
    res = run(ACTION_PRINT_DIALOG, @parent_window)
    case res
        when RESULT_ERROR
            puts "error"
        when RESULT_CANCEL
            puts "cancelled"
        when RESULT_APPLY
            puts "applied"
        when RESULT_IN_PROGRESS
            puts "in progress"
    end
end