File: example_10.py

package info (click to toggle)
gnome-python-desktop 2.32.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,548 kB
  • sloc: sh: 10,214; xml: 8,851; ansic: 3,428; python: 1,457; makefile: 664
file content (240 lines) | stat: -rwxr-xr-x 6,307 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#! /usr/bin/env python

# *  example_10.c: sample gnome-print code
# *
# *  This program is free software; you can redistribute it and/or
# *  modify it under the terms of the GNU Library General Public License
# *  as published by the Free Software Foundation; either version 2 of
# *  the License, or (at your option) any later version.
# *
# *  This program is distributed in the hope that it will be useful,
# *  but WITHOUT ANY WARRANTY; without even the implied warranty of
# *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# *  GNU Library General Public License for more details.
# *
# *  You should have received a copy of the GNU Library General Public
# *  License along with this program; if not, write to the Free Software
# *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# *
# *  Authors:
# *    Chema Celorio <chema@ximian.com>
#    Python conversion:
#      Gustavo J. A. M. Carneiro <gustavo@users.sf.net>
# *
# *  Copyright (C) 2002 Ximian Inc. and authors
# *
# */

#/*
# * See README
# */

import pygtk; pygtk.require("2.0")
import gnomeprint, gnomeprint.ui
import gobject, gtk
import gtk.glade as glade
import sys

class MyDoc(object): __slots__ = ('view', 'config', 'name')

class MyApp(object):
    __slots__ = ('status_bar', 'active_doc', 'doc1', 'doc2', 'doc3')


app = MyApp()

def my_status_bar_pop(id):
    app.status_bar.remove(0, id)
    return False


def my_status_bar_print(message):
    num = app.status_bar.push(0, message)
    gobject.timeout_add(3000, my_status_bar_pop, num)


def my_print_image_from_pixbuf(gpc, pixbuf):
    raw_image = pixbuf.get_pixels()
    has_alpha = pixbuf.get_has_alpha()
    rowstride = pixbuf.get_rowstride()
    height    = pixbuf.get_height()
    width     = pixbuf.get_width()
	
    if has_alpha:
	gpc.rgbaimage(raw_image, width, height, rowstride)
    else:
	gpc.rgbimage(raw_image, width, height, rowstride)


def my_print_image_from_disk(gpc):
    pixbuf = gtk.gdk.pixbuf_new_from_file("sample-image.png")

    gpc.gsave()
    gpc.scale(144, 144)
    my_print_image_from_pixbuf(gpc, pixbuf)
    
    gpc.grestore()


def my_draw(gpc, page):
    font = gnomeprint.font_find_closest("Sans Regular", 18)

    gpc.beginpage(str(page))

    gpc.moveto(1, 1)
    gpc.lineto(200, 200)
    gpc.stroke()

    gpc.setfont(font)
    gpc.moveto(200, 72)
    gpc.show("Page: %d" % (page + 1))

    my_print_image_from_disk(gpc)
    gpc.showpage()


def my_print(job, preview):
    gpc = job.get_context()
    for i in xrange(4):
	my_draw(gpc, i)

    job.close()

    if not preview:
	my_status_bar_print("Printing ...")
	job.print_()
    else:
	my_status_bar_print("Print previewing ...")
	gnomeprint.ui.JobPreview(job, "Title goes here").show()


def my_print_cb(widget):
    # Create the objects
    job    = gnomeprint.Job(app.active_doc.config)
    dialog = gnomeprint.ui.Dialog(job, "Sample print dialog",
				  gnomeprint.ui.DIALOG_RANGE | gnomeprint.ui.DIALOG_COPIES)
    dialog.construct_range_page(gnomeprint.ui.RANGE_ALL|gnomeprint.ui.RANGE_SELECTION,
				1, 2, "A", "Lines")

    response = dialog.run()

    if response == gnomeprint.ui.DIALOG_RESPONSE_PRINT:
	my_print(job, False)

    elif response == gnomeprint.ui.DIALOG_RESPONSE_PREVIEW:
	my_print(job, True)


def my_print_preview_cb(widget):
    job = gnomeprint.Job(app.active_doc.config)
    my_print(job, True)


def my_print_setup_cb(widget):

    notebook = gtk.Notebook()

    # GnomePrintPaperSelector */
    paper_selector = gnomeprint.ui.PaperSelector(app.active_doc.config, 0)
    paper_selector.set_size_request(200, 400)
    label = gobject.new(gtk.Label, label="P_aper", use_underline=True)
    notebook.append_page(paper_selector, label)

    # GnomePrintPaperSelector */
    paper_selector = gnomeprint.ui.PaperSelector(app.active_doc.config, 0)
    paper_selector.set_size_request(200, 400)
    label = gobject.new(gtk.Label, label="_foo", use_underline=True)
    notebook.append_page(paper_selector, label)


    # Dialog
    dialog = gtk.Dialog("Printer Setup", None, 0,
			buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
				 gtk.STOCK_OK,     gtk.RESPONSE_OK))
    dialog.vbox.pack_start(notebook, True, True, 0)
    dialog.show_all()
	
    response = dialog.run()
    dialog.destroy()


def my_font_dialog_cb(widget):
    font = None
    dialog = gnomeprint.ui.FontDialog("Sample Font dialog")
    fontsel = dialog.get_fontsel()
    if font is not None:
	fontsel.set_font(font)

    dialog.show_all()
    dialog.run()

    font = fontsel.get_font()

    dialog.destroy()


# This function is called when a document is activated. We set the
# active document here and update the GnomePrintWidgets with the
# active document config
def my_document_activate_cb(view, doc):
    if app.active_doc is doc:
	return
    app.active_doc = doc
    my_status_bar_print("The active document is now \"%s\"" % doc.name)


# Creates a new document
# 
# Return Value: a pointer to the new document, aborts on error
def my_new_doc(doc_name, gui):
	doc = MyDoc()
	doc.view = gui.get_widget(doc_name)
	doc.name = doc_name
	doc.config = gnomeprint.config_default()
	doc.view.connect("grab_focus", my_document_activate_cb, doc)

	return doc


def my_app_load():
    gui = glade.XML("example_10.glade")

    # Connect menu items to callbacks
    item = gui.get_widget("menu_quit_item")
    item.connect("activate", lambda arg1: gtk.main_quit())

    item = gui.get_widget("window1")
    item.connect("delete_event", lambda arg1, arg2: gtk.main_quit())

    item = gui.get_widget("menu_print_item")
    item.connect("activate", my_print_cb)
    
    item = gui.get_widget("menu_print_preview_item")
    item.connect("activate", my_print_preview_cb)

    item = gui.get_widget("menu_print_setup_item")
    item.connect("activate", my_print_setup_cb)

#     item = gui.get_widget("menu_tree_item")
#     item.connect("activate", my_tree_cb)

    item = gui.get_widget("menu_font_item")
    item.connect("activate", my_font_dialog_cb)

    app.status_bar = gui.get_widget("statusbar")
    app.doc1 = my_new_doc("doc1", gui)
    if 0:
	app.doc2 = my_new_doc("doc2", gui)
	app.doc3 = my_new_doc("doc3", gui)
    app.active_doc = None
    app.doc1.view.grab_focus()
    
    return True

# --- main ---

if  not my_app_load():
    sys.exit(-1)

gtk.main()