aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/printing/e-contact-print-envelope.c
blob: c702728e4e50811d4341b1f933112cd3a11bfaa9 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-contact-print-envelope.c
 * Copyright (C) 2000  Helix Code, Inc.
 * Author: Chris Lahey <clahey@helixcode.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include "e-contact-print-envelope.h"
#include <libgnomeprint/gnome-print.h>
#include <libgnomeprint/gnome-print-dialog.h>
#include <libgnomeprint/gnome-print-master.h>
#include <libgnomeprint/gnome-print-master-preview.h>
#include <addressbook/backend/ebook/e-card.h>
#include <addressbook/backend/ebook/e-card-simple.h>


#define ENVELOPE_HEIGHT (72.0 * 4.0)
#define ENVELOPE_WIDTH (72.0 * 10.0)

typedef struct {
    int start;
    int length;
} EcpeLine;

static void
startset(void *pointer, EcpeLine **iterator)
{
    (*iterator)--;
    (*iterator)->start = GPOINTER_TO_INT(pointer);
}

static void
lengthset(void *pointer, EcpeLine **iterator)
{
    (*iterator)--;
    (*iterator)->length = GPOINTER_TO_INT(pointer);
}

static EcpeLine *
ecpe_break(char *address)
{
    int i;
    int length = 0;
    int laststart = 0;
    GList *startlist = NULL;
    GList *lengthlist = NULL;
    EcpeLine *ret_val;
    EcpeLine *iterator;

    for (i = 0; address[i]; i++) {
        if (address[i] == '\n') {
            startlist = g_list_prepend (startlist, GINT_TO_POINTER(laststart));
            lengthlist = g_list_prepend (lengthlist, GINT_TO_POINTER(i - laststart));
            length ++;
            laststart = i + 1;
        }
    }
    startlist = g_list_prepend (startlist, GINT_TO_POINTER(laststart));
    lengthlist = g_list_prepend (lengthlist, GINT_TO_POINTER(i - laststart));
    length ++;

    ret_val = g_new(EcpeLine, length + 1);

    iterator = ret_val + length;
    g_list_foreach(startlist, (GFunc) startset, &iterator);
    g_list_free(startlist);

    iterator = ret_val + length;
    g_list_foreach(lengthlist, (GFunc) lengthset, &iterator);
    g_list_free(lengthlist);

    ret_val[length].start = -1;
    ret_val[length].length = -1;

    return ret_val;
}

static void
ecpe_linelist_dimensions(GnomeFont *font, char *address, EcpeLine *linelist, double *widthp, double *heightp)
{
    double width = 0;
    int i;
    if (widthp) {
        for (i = 0; linelist[i].length != -1; i++) {
            width = MAX(width, gnome_font_get_width_string_n (font, address + linelist[i].start, linelist[i].length));
        }
        *widthp = width;
    } else {
        for (i = 0; linelist[i].length != -1; i++)
            /* Intentionally empty */;
    }
    if (heightp) {
        *heightp = gnome_font_get_size(font) * i;
    }
}

static void
ecpe_linelist_print(GnomePrintContext *pc, GnomeFont *font, char *address, EcpeLine *linelist, double x, double y)
{
    int i;
    for (i = 0; linelist[i].length != -1; i++) {
        gnome_print_moveto(pc, x, y + gnome_font_get_ascender(font));
        gnome_print_show_sized (pc, address + linelist[i].start, linelist[i].length);
        y -= gnome_font_get_size(font);
    }
}

static gint
e_contact_print_envelope_close(GnomeDialog *dialog, gpointer data)
{
    return FALSE;
}

static void
ecpe_print(GnomePrintContext *pc, ECard *ecard)
{
    ECardSimple *card = e_card_simple_new(ecard);
    char *address;
    EcpeLine *linelist;
    double x;
    double y;
    GnomeFont *font;
    

    gnome_print_rotate(pc, 90);
    gnome_print_translate(pc, 72.0 * 11.0 - ENVELOPE_WIDTH, -72.0 * 8.5 + (72.0 * 8.5 - ENVELOPE_HEIGHT) / 2);

    address = e_card_simple_get(card, E_CARD_SIMPLE_FIELD_ADDRESS_BUSINESS);
    linelist = ecpe_break(address);
    font = gnome_font_new("Helvetica", 12);
    ecpe_linelist_dimensions(font, address, linelist, &x, &y);
    x = (ENVELOPE_WIDTH - x) / 2;
    y = (ENVELOPE_HEIGHT - y) / 2;
    ecpe_linelist_print(pc, font, address, linelist, x, y);
    gtk_object_unref(GTK_OBJECT(font));
    g_free(linelist);

    g_free(address);

    gnome_print_showpage(pc);
    gnome_print_context_close(pc);

    gtk_object_unref(GTK_OBJECT(card));
}

static void
e_contact_print_envelope_button(GnomeDialog *dialog, gint button, gpointer data)
{
    GnomePrintMaster *master;
    GnomePrintContext *pc;
    ECard *card = NULL;
    GtkWidget *preview;

    card = gtk_object_get_data(GTK_OBJECT(dialog), "card");

    switch( button ) {
    case GNOME_PRINT_PRINT:
        master = gnome_print_master_new_from_dialog( GNOME_PRINT_DIALOG(dialog) );
        pc = gnome_print_master_get_context( master );

        ecpe_print(pc, card);
        
        gnome_print_master_print(master);
        gnome_dialog_close(dialog);
        break;
    case GNOME_PRINT_PREVIEW:
        master = gnome_print_master_new_from_dialog( GNOME_PRINT_DIALOG(dialog) );
        pc = gnome_print_master_get_context( master );

        ecpe_print(pc, card);
        
        preview = GTK_WIDGET(gnome_print_master_preview_new(master, "Print Preview"));
        gtk_widget_show_all(preview);
        break;
    case GNOME_PRINT_CANCEL:
        gtk_object_unref(GTK_OBJECT(card));
        gnome_dialog_close(dialog);
        break;
    }
}

GtkWidget *
e_contact_print_envelope_dialog_new(ECard *card)
{
    GtkWidget *dialog;
    
    dialog = gnome_print_dialog_new("Print envelope", GNOME_PRINT_DIALOG_COPIES);

    card = e_card_duplicate(card);
    gtk_object_set_data(GTK_OBJECT(dialog), "card", card);
    gtk_signal_connect(GTK_OBJECT(dialog),
               "clicked", GTK_SIGNAL_FUNC(e_contact_print_envelope_button), NULL);
    gtk_signal_connect(GTK_OBJECT(dialog),
               "close", GTK_SIGNAL_FUNC(e_contact_print_envelope_close), NULL);
    return dialog;
}