print.c
14.3 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA, 02111-1307, USA
*
* Este programa está nomeado como print.c e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
* licinio@bb.com.br (Licínio Luis Branco)
* kraucer@bb.com.br (Kraucer Fernandes Mazuco)
*
*/
#include "globals.h"
#include "v3270/v3270.h"
#include <lib3270/selection.h>
/*--[ Structs ]--------------------------------------------------------------------------------------*/
typedef struct _print_info
{
GdkColor color[V3270_COLOR_COUNT];
int show_selection : 1;
int all : 1;
H3270 * session;
gchar * font;
guint fontsize;
cairo_font_weight_t fontweight;
int baddr;
int rows;
int cols;
int pages;
cairo_font_extents_t extents;
double left;
double width;
double height;
cairo_scaled_font_t * font_scaled;
} PRINT_INFO;
/*--[ Implement ]------------------------------------------------------------------------------------*/
static void setup_font(GtkPrintContext * context, PRINT_INFO *info)
{
cairo_t *cr = gtk_print_context_get_cairo_context(context);
cairo_select_font_face(cr, info->font, CAIRO_FONT_SLANT_NORMAL, info->fontweight);
info->font_scaled = cairo_get_scaled_font(cr);
cairo_scaled_font_reference(info->font_scaled);
cairo_scaled_font_extents(info->font_scaled,&info->extents);
info->width = ((double) info->cols) * info->extents.max_x_advance;
info->height = ((double) info->rows) * (info->extents.height + info->extents.descent);
// Center image
info->left = (gtk_print_context_get_width(context)-info->width)/2;
if(info->left < 2)
info->left = 2;
}
static void begin_print(GtkPrintOperation *prt, GtkPrintContext *context, PRINT_INFO *info)
{
setup_font(context,info);
gtk_print_operation_set_n_pages(prt,1);
}
static void draw_page(GtkPrintOperation *prt, GtkPrintContext *context, gint pg, PRINT_INFO *info)
{
int row;
int col;
cairo_t * cr = gtk_print_context_get_cairo_context(context);
int baddr = info->baddr;
GdkRectangle rect;
cairo_set_scaled_font(cr,info->font_scaled);
memset(&rect,0,sizeof(rect));
rect.y = 2;
rect.height = (info->extents.height + info->extents.descent);
rect.width = info->extents.max_x_advance;
gdk_cairo_set_source_color(cr,info->color+V3270_COLOR_BACKGROUND);
cairo_rectangle(cr, info->left-2, 0, (rect.width*info->cols)+4, (rect.height*info->rows)+4);
cairo_fill(cr);
cairo_stroke(cr);
rect.width++;
rect.height++;
for(row = 0; row < info->rows; row++)
{
rect.x = info->left;
for(col = 0; col < info->cols; col++)
{
unsigned char c;
unsigned short attr;
if(!lib3270_get_element(info->session,baddr++,&c,&attr) && (info->all || (attr & LIB3270_ATTR_SELECTED)))
{
if(!info->show_selection)
attr &= ~LIB3270_ATTR_SELECTED;
v3270_draw_element(cr,c,attr,info->session,info->extents.height,&rect,info->color);
}
rect.x += (rect.width-1);
}
rect.y += (rect.height-1);
}
}
#ifdef WIN32
#define save_string(h,k,v) save_settings(k,v,h)
static void save_settings(const gchar *key, const gchar *value, HKEY hKey)
{
RegSetValueEx(hKey,key,0,REG_SZ,(const BYTE *) value,strlen(value)+1);
}
static void save_double(HKEY hKey, const gchar *key, gdouble value)
{
// Reference: http://git.gnome.org/browse/glib/tree/glib/gkeyfile.c
gchar result[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (result, sizeof (result), value);
save_settings(key,result,hKey);
}
/*
* From: http://git.gnome.org/browse/gtk+/tree/gtk/gtkpagesetup.c
* something like this should really be in gobject!
*
* I Agree!! (Perry Werneck)
*
*/
static gchar * enum_to_string(GType type, guint enum_value)
{
GEnumClass *enum_class;
GEnumValue *value;
gchar *retval = NULL;
enum_class = g_type_class_ref (type);
value = g_enum_get_value(enum_class, enum_value);
if (value)
retval = g_strdup (value->value_nick);
g_type_class_unref (enum_class);
return retval;
}
#endif // WIN32
static void done(GtkPrintOperation *prt, GtkPrintOperationResult result, PRINT_INFO *info)
{
#ifdef WIN32
HKEY registry;
if(get_registry_handle("print",®istry,KEY_SET_VALUE))
{
HKEY hKey;
DWORD disp;
if(RegCreateKeyEx(registry,"settings",0,NULL,REG_OPTION_NON_VOLATILE,KEY_SET_VALUE,NULL,&hKey,&disp) == ERROR_SUCCESS)
{
gtk_print_settings_foreach( gtk_print_operation_get_print_settings(prt),
(GtkPrintSettingsFunc) save_settings,
hKey );
RegCloseKey(hKey);
}
if(RegCreateKeyEx(registry,"pagesetup",0,NULL,REG_OPTION_NON_VOLATILE,KEY_SET_VALUE,NULL,&hKey,&disp) == ERROR_SUCCESS)
{
HKEY hPaperSize;
GtkPageSetup * setup = gtk_print_operation_get_default_page_setup(prt);
gchar * orientation = enum_to_string(GTK_TYPE_PAGE_ORIENTATION,gtk_page_setup_get_orientation(setup));
// From http://git.gnome.org/browse/gtk+/tree/gtk/gtkpagesetup.c
save_double(hKey, "MarginTop", gtk_page_setup_get_top_margin(setup, GTK_UNIT_MM));
save_double(hKey, "MarginBottom", gtk_page_setup_get_bottom_margin(setup, GTK_UNIT_MM));
save_double(hKey, "MarginLeft", gtk_page_setup_get_left_margin(setup, GTK_UNIT_MM));
save_double(hKey, "MarginRight", gtk_page_setup_get_right_margin(setup, GTK_UNIT_MM));
save_string(hKey, "Orientation", orientation);
g_free (orientation);
if(RegCreateKeyEx(hKey,"papersize",0,NULL,REG_OPTION_NON_VOLATILE,KEY_SET_VALUE,NULL,&hPaperSize,&disp) == ERROR_SUCCESS)
{
GtkPaperSize *paper_size = gtk_page_setup_get_paper_size (setup);
if(paper_size)
{
#warning Implement it
}
RegCloseKey(hPaperSize);
}
RegCloseKey(hKey);
}
RegCloseKey(registry);
}
#else
GKeyFile * conf = get_application_keyfile();
gtk_print_settings_to_key_file(gtk_print_operation_get_print_settings(prt),conf,"print_settings");
gtk_page_setup_to_key_file(gtk_print_operation_get_default_page_setup(prt),conf,"page_setup");
#endif
if(info->font_scaled)
cairo_scaled_font_destroy(info->font_scaled);
if(info->font)
g_free(info->font);
g_free(info);
}
#if GTK_CHECK_VERSION(3,2,0)
static gboolean filter_monospaced(const PangoFontFamily *family,const PangoFontFace *face,gpointer data)
{
return pango_font_family_is_monospace((PangoFontFamily *) family);
}
#endif // GTK(3,2,0)
static void font_set(GtkFontButton *widget, PRINT_INFO *info)
{
const gchar * name = gtk_font_button_get_font_name(widget);
PangoFontDescription * descr = pango_font_description_from_string(name);
if(!descr)
return;
if(info->font)
g_free(info->font);
info->font = g_strdup(pango_font_description_get_family(descr));
info->fontsize = pango_font_description_get_size(descr);
info->fontweight = CAIRO_FONT_WEIGHT_NORMAL;
if(pango_font_description_get_weight(descr) == PANGO_WEIGHT_BOLD)
info->fontweight = CAIRO_FONT_WEIGHT_BOLD;
pango_font_description_free(descr);
set_string_to_config("print","font",name);
trace("Font set to \"%s\" with size %d",info->font,info->fontsize);
}
static void toggle_show_selection(GtkToggleButton *togglebutton,PRINT_INFO *info)
{
gboolean active = gtk_toggle_button_get_active(togglebutton);
info->show_selection = active ? 1 : 0;
set_boolean_to_config("print","selection",active);
}
static GObject * create_custom_widget(GtkPrintOperation *prt, PRINT_INFO *info)
{
static const gchar * label[] = { N_( "Font:" ), N_( "Color scheme:" ) };
GtkWidget * container = gtk_table_new(3,2,FALSE);
GtkWidget * widget;
int f;
gchar * ptr;
for(f=0;f<G_N_ELEMENTS(label);f++)
{
widget = gtk_label_new(gettext(label[f]));
gtk_misc_set_alignment(GTK_MISC(widget),0,0.5);
gtk_table_attach(GTK_TABLE(container),widget,0,1,f,f+1,GTK_FILL,GTK_FILL,0,0);
}
// Font selection button
widget = gtk_font_button_new();
#if GTK_CHECK_VERSION(3,2,0)
gtk_font_chooser_set_filter_func((GtkFontChooser *) widget,filter_monospaced,NULL,NULL);
#endif // GTK(3,2,0)
gtk_table_attach(GTK_TABLE(container),widget,1,2,0,1,GTK_EXPAND|GTK_FILL,GTK_FILL,5,0);
info->font = get_string_from_config("print","font","Courier 10");
gtk_font_button_set_font_name((GtkFontButton *) widget,info->font);
font_set((GtkFontButton *) widget,info);
g_signal_connect(G_OBJECT(widget),"font-set",G_CALLBACK(font_set),info);
// Color scheme dropdown
ptr = get_string_from_config("print","colors","");
if(*ptr)
v3270_set_color_table(info->color,ptr);
else
v3270_set_mono_color_table(info->color,"black","white");
g_free(ptr);
widget = color_scheme_new(info->color);
g_object_set_data(G_OBJECT(container),"combo",widget);
gtk_table_attach(GTK_TABLE(container),widget,1,2,1,2,GTK_EXPAND|GTK_FILL,GTK_FILL,5,0);
// Selection checkbox
widget = gtk_check_button_new_with_label(_("Print selection box"));
if(info->all)
{
info->show_selection = get_boolean_from_config("print","selection",FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget),info->show_selection);
g_signal_connect(G_OBJECT(widget),"toggled",G_CALLBACK(toggle_show_selection),info);
}
else
{
gtk_widget_set_sensitive(widget,FALSE);
}
gtk_table_attach(GTK_TABLE(container),widget,1,2,2,3,GTK_EXPAND|GTK_FILL,GTK_FILL,5,0);
// Show and return
gtk_widget_show_all(container);
return G_OBJECT(container);
}
static void custom_widget_apply(GtkPrintOperation *prt, GtkWidget *widget, PRINT_INFO *info)
{
GtkWidget * combo = g_object_get_data(G_OBJECT(widget),"combo");
GdkColor * clr = g_object_get_data(G_OBJECT(combo),"selected");
if(clr)
{
int f;
GString *str = g_string_new("");
for(f=0;f<V3270_COLOR_COUNT;f++)
{
info->color[f] = clr[f];
if(f)
g_string_append_c(str,',');
g_string_append_printf(str,"%s",gdk_color_to_string(clr+f));
}
set_string_to_config("print","colors","%s",str->str);
g_string_free(str,TRUE);
}
g_object_unref(combo);
}
static GtkPrintOperation * begin_print_operation(GtkAction *action, GtkWidget *widget, PRINT_INFO **info)
{
GtkPrintOperation * print = gtk_print_operation_new();
GtkPrintSettings * settings = gtk_print_settings_new();
GtkPageSetup * setup = gtk_page_setup_new();
const gchar * attr;
*info = g_new0(PRINT_INFO,1);
(*info)->session = v3270_get_session(widget);
(*info)->fontweight = CAIRO_FONT_WEIGHT_NORMAL;
// Basic setup
gtk_print_operation_set_allow_async(print,TRUE);
attr = (const gchar *) g_object_get_data(G_OBJECT(action),"jobname");
if(attr)
{
gtk_print_operation_set_job_name(print,attr);
}
else
{
gchar *ptr = g_strconcat(PACKAGE_NAME,".",gtk_action_get_name(action),NULL);
gtk_print_operation_set_job_name(print,ptr);
g_free(ptr);
}
gtk_print_operation_set_custom_tab_label(print,_( "Options" ));
gtk_print_operation_set_show_progress(print,TRUE);
// Common signals
g_signal_connect(print,"done",G_CALLBACK(done),*info);
g_signal_connect(print,"create-custom-widget",G_CALLBACK(create_custom_widget), *info);
g_signal_connect(print,"custom-widget-apply",G_CALLBACK(custom_widget_apply), *info);
// Load page and print settings
{
#ifdef WIN32
#warning Implementar
#else
GKeyFile * conf = get_application_keyfile();
GError * err = NULL;
if(!gtk_print_settings_load_key_file(settings,conf,"print_settings",&err))
{
g_warning("Error getting print settings: %s",err->message);
g_error_free(err);
err = NULL;
}
if(!gtk_page_setup_load_key_file(setup,conf,"page_setup",&err))
{
g_warning("Error getting page setup: %s",err->message);
g_error_free(err);
err = NULL;
}
#endif
}
// Finish settings
gtk_print_operation_set_print_settings(print,settings);
gtk_print_operation_set_default_page_setup(print,setup);
return print;
}
void print_all_action(GtkAction *action, GtkWidget *widget)
{
PRINT_INFO * info = NULL;
GtkPrintOperation * print = begin_print_operation(action,widget,&info);
trace("Action %s activated on widget %p print=%p",gtk_action_get_name(action),widget,print);
lib3270_get_screen_size(info->session,&info->rows,&info->cols);
info->all = 1;
g_signal_connect(print,"begin_print",G_CALLBACK(begin_print),info);
g_signal_connect(print,"draw_page",G_CALLBACK(draw_page),info);
// Run Print dialog
gtk_print_operation_run(print,GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,GTK_WINDOW(gtk_widget_get_toplevel(widget)),NULL);
g_object_unref(print);
}
void print_selected_action(GtkAction *action, GtkWidget *widget)
{
PRINT_INFO * info = NULL;
int start, end, rows;
GtkPrintOperation * print = begin_print_operation(action,widget,&info);;
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
if(lib3270_get_selected_addr(info->session,&start,&end))
{
g_warning("Can't get selected addresses for action %s",gtk_action_get_name(action));
g_object_unref(print);
return;
}
info->baddr = start;
lib3270_get_screen_size(info->session,&rows,&info->cols);
info->rows = ((end / info->cols) - (start / info->cols))+1;
trace("First row: %d End row: %d Num rows: %d",(start / info->cols),(end / info->cols),info->rows);
info->all = 0;
g_signal_connect(print,"begin_print",G_CALLBACK(begin_print),info);
g_signal_connect(print,"draw_page",G_CALLBACK(draw_page),info);
// Run Print dialog
gtk_print_operation_run(print,GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,GTK_WINDOW(gtk_widget_get_toplevel(widget)),NULL);
g_object_unref(print);
}
void print_copy_action(GtkAction *action, GtkWidget *widget)
{
trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
}