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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
|
/* GtkPageSetupUnixDialog
* Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
* Copyright © 2006, 2007, 2008 Christian Persch
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <locale.h>
#include <glib/gi18n-lib.h>
#include "gtkprivate.h"
#include "gtkbutton.h"
#include "gtkscrolledwindow.h"
#include "gtkcheckbutton.h"
#include "gtklabel.h"
#include "gtkgrid.h"
#include "gtkpagesetupunixdialog.h"
#include "gtkcustompaperunixdialog.h"
#include "gtkprintbackendprivate.h"
#include "gtkpapersize.h"
#include "gtkprintutils.h"
#include "deprecated/gtkdialogprivate.h"
/**
* GtkPageSetupUnixDialog:
*
* `GtkPageSetupUnixDialog` implements a page setup dialog for platforms
* which don’t provide a native page setup dialog, like Unix.
*
* ![An example GtkPageSetupUnixDialog](pagesetupdialog.png)
*
* It can be used very much like any other GTK dialog, at the
* cost of the portability offered by the high-level printing
* API in [class@Gtk.PrintOperation].
*/
typedef struct _GtkPageSetupUnixDialogClass GtkPageSetupUnixDialogClass;
struct _GtkPageSetupUnixDialog
{
GtkDialog parent_instance;
GListModel *printer_list;
GListStore *page_setup_list;
GListStore *custom_paper_list;
GListStore *manage_papers_list;
GListStore *paper_size_list;
GList *print_backends;
GtkWidget *printer_combo;
GtkWidget *paper_size_combo;
GtkWidget *paper_size_label;
GtkWidget *portrait_radio;
GtkWidget *reverse_portrait_radio;
GtkWidget *landscape_radio;
GtkWidget *reverse_landscape_radio;
gulong request_details_tag;
GtkPrinter *request_details_printer;
GtkPrintSettings *print_settings;
gboolean internal_change;
/* Save last setup so we can re-set it after selecting manage custom sizes */
GtkPageSetup *last_setup;
};
struct _GtkPageSetupUnixDialogClass
{
GtkDialogClass parent_class;
};
/* Keep these in line with GtkListStores defined in gtkpagesetupunixprintdialog.ui */
enum {
PRINTER_LIST_COL_NAME,
PRINTER_LIST_COL_PRINTER,
PRINTER_LIST_N_COLS
};
enum {
PAGE_SETUP_LIST_COL_PAGE_SETUP,
PAGE_SETUP_LIST_COL_IS_SEPARATOR,
PAGE_SETUP_LIST_N_COLS
};
G_DEFINE_TYPE (GtkPageSetupUnixDialog, gtk_page_setup_unix_dialog, GTK_TYPE_DIALOG)
static void gtk_page_setup_unix_dialog_finalize (GObject *object);
static void fill_paper_sizes_from_printer (GtkPageSetupUnixDialog *dialog,
GtkPrinter *printer);
static void printer_changed_callback (GtkDropDown *combo_box,
GParamSpec *pspec,
GtkPageSetupUnixDialog *dialog);
static void paper_size_changed (GtkDropDown *combo_box,
GParamSpec *pspec,
GtkPageSetupUnixDialog *dialog);
static void load_print_backends (GtkPageSetupUnixDialog *dialog);
static const char common_paper_sizes[][16] = {
"na_letter",
"na_legal",
"iso_a4",
"iso_a5",
"roc_16k",
"iso_b5",
"jis_b5",
"na_number-10",
"iso_dl",
"jpn_chou3",
"na_ledger",
"iso_a3"
};
static void
gtk_page_setup_unix_dialog_class_init (GtkPageSetupUnixDialogClass *class)
{
GObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = G_OBJECT_CLASS (class);
widget_class = GTK_WIDGET_CLASS (class);
object_class->finalize = gtk_page_setup_unix_dialog_finalize;
/* Bind class to template
*/
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gtk/libgtk/ui/gtkpagesetupunixdialog.ui");
gtk_widget_class_bind_template_child (widget_class, GtkPageSetupUnixDialog, printer_combo);
gtk_widget_class_bind_template_child (widget_class, GtkPageSetupUnixDialog, paper_size_combo);
gtk_widget_class_bind_template_child (widget_class, GtkPageSetupUnixDialog, paper_size_label);
gtk_widget_class_bind_template_child (widget_class, GtkPageSetupUnixDialog, portrait_radio);
gtk_widget_class_bind_template_child (widget_class, GtkPageSetupUnixDialog, reverse_portrait_radio);
gtk_widget_class_bind_template_child (widget_class, GtkPageSetupUnixDialog, landscape_radio);
gtk_widget_class_bind_template_child (widget_class, GtkPageSetupUnixDialog, reverse_landscape_radio);
gtk_widget_class_bind_template_callback (widget_class, printer_changed_callback);
gtk_widget_class_bind_template_callback (widget_class, paper_size_changed);
}
static void
setup_paper_size_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkWidget *label;
label = gtk_label_new ("");
gtk_widget_set_halign (label, GTK_ALIGN_START);
gtk_list_item_set_child (item, label);
}
static void
bind_paper_size_list_item (GtkSignalListItemFactory *factory,
GtkListItem *item,
GtkPageSetupUnixDialog *self)
{
GtkPageSetup *page_setup;
GtkWidget *label;
guint pos;
GListModel *papers;
GListModel *model;
gpointer first;
page_setup = gtk_list_item_get_item (item);
label = gtk_list_item_get_child (item);
pos = gtk_list_item_get_position (item);
papers = gtk_drop_down_get_model (GTK_DROP_DOWN (self->paper_size_combo));
model = gtk_flatten_list_model_get_model_for_item (GTK_FLATTEN_LIST_MODEL (papers), pos);
if (model != G_LIST_MODEL (self->manage_papers_list))
{
GtkPaperSize *paper_size = gtk_page_setup_get_paper_size (page_setup);
gtk_label_set_text (GTK_LABEL (label), gtk_paper_size_get_display_name (paper_size));
}
else
gtk_label_set_text (GTK_LABEL (label), _("Manage Custom Sizes…"));
first = g_list_model_get_item (model, 0);
g_object_unref (first);
if (pos != 0 &&
page_setup == GTK_PAGE_SETUP (first))
gtk_widget_add_css_class (gtk_widget_get_parent (label), "separator");
else
gtk_widget_remove_css_class (gtk_widget_get_parent (label), "separator");
}
static void
bind_paper_size_item (GtkSignalListItemFactory *factory,
GtkListItem *item,
GtkPageSetupUnixDialog *self)
{
GtkWidget *label;
bind_paper_size_list_item (factory, item, self);
label = gtk_list_item_get_child (item);
gtk_widget_remove_css_class (label, "separator-before");
}
static gboolean
match_func (gpointer item, gpointer user_data)
{
return !gtk_printer_is_virtual (GTK_PRINTER (item));
}
static void
setup_printer_item (GtkSignalListItemFactory *factory,
GtkListItem *item)
{
GtkWidget *label;
label = gtk_label_new ("");
gtk_widget_set_halign (label, GTK_ALIGN_START);
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_list_item_set_child (item, label);
}
static void
bind_printer_item (GtkSignalListItemFactory *factory,
GtkListItem *item,
GtkPageSetupUnixDialog *self)
{
GtkPrinter *printer;
GtkWidget *label;
const char *location;
const char *name;
char *str;
printer = gtk_list_item_get_item (item);
label = gtk_list_item_get_child (item);
name = gtk_printer_get_name (printer);
location = gtk_printer_get_location (printer);
str = g_strdup_printf ("<b>%s</b>\n%s", name, location ? location : "");
gtk_label_set_markup (GTK_LABEL (label), str);
g_free (str);
}
static void
gtk_page_setup_unix_dialog_init (GtkPageSetupUnixDialog *dialog)
{
GtkListItemFactory *factory;
GListStore *store;
GListModel *paper_size_list;
GtkPrinter *printer;
GListStore *printer_list;
GListStore *printer_list_list;
GListModel *full_list;
GtkFilter *filter;
GtkPageSetup *page_setup;
dialog->internal_change = TRUE;
dialog->print_backends = NULL;
gtk_widget_init_template (GTK_WIDGET (dialog));
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (dialog));
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_Apply"), GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
G_GNUC_END_IGNORE_DEPRECATIONS
dialog->page_setup_list = g_list_store_new (GTK_TYPE_PAGE_SETUP);
dialog->custom_paper_list = g_list_store_new (GTK_TYPE_PAGE_SETUP);
dialog->manage_papers_list = g_list_store_new (GTK_TYPE_PAGE_SETUP);
page_setup = gtk_page_setup_new ();
g_list_store_append (dialog->manage_papers_list, page_setup);
g_object_unref (page_setup);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_paper_size_item), dialog);
g_signal_connect (factory, "bind", G_CALLBACK (bind_paper_size_item), dialog);
gtk_drop_down_set_factory (GTK_DROP_DOWN (dialog->paper_size_combo), factory);
g_object_unref (factory);
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_paper_size_item), dialog);
g_signal_connect (factory, "bind", G_CALLBACK (bind_paper_size_list_item), dialog);
gtk_drop_down_set_list_factory (GTK_DROP_DOWN (dialog->paper_size_combo), factory);
g_object_unref (factory);
store = g_list_store_new (G_TYPE_LIST_MODEL);
g_list_store_append (store, dialog->page_setup_list);
g_list_store_append (store, dialog->custom_paper_list);
g_list_store_append (store, dialog->manage_papers_list);
paper_size_list = G_LIST_MODEL (gtk_flatten_list_model_new (G_LIST_MODEL (store)));
gtk_drop_down_set_model (GTK_DROP_DOWN (dialog->paper_size_combo), paper_size_list);
g_object_unref (paper_size_list);
/* Do this in code, we want the translatable strings without the markup */
printer_list_list = g_list_store_new (G_TYPE_LIST_MODEL);
printer_list = g_list_store_new (GTK_TYPE_PRINTER);
printer = gtk_printer_new (_("Any Printer"), NULL, FALSE);
gtk_printer_set_location (printer, _("For portable documents"));
g_list_store_append (printer_list, printer);
g_object_unref (printer);
g_list_store_append (printer_list_list, printer_list);
g_object_unref (printer_list);
full_list = G_LIST_MODEL (gtk_flatten_list_model_new (G_LIST_MODEL (printer_list_list)));
filter = GTK_FILTER (gtk_custom_filter_new (match_func, NULL, NULL));
dialog->printer_list = G_LIST_MODEL (gtk_filter_list_model_new (full_list, filter));
factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (setup_printer_item), dialog);
g_signal_connect (factory, "bind", G_CALLBACK (bind_printer_item), dialog);
gtk_drop_down_set_factory (GTK_DROP_DOWN (dialog->printer_combo), factory);
g_object_unref (factory);
gtk_drop_down_set_model (GTK_DROP_DOWN (dialog->printer_combo), dialog->printer_list);
printer_changed_callback (GTK_DROP_DOWN (dialog->printer_combo), NULL, dialog);
/* Load data */
gtk_print_load_custom_papers (dialog->custom_paper_list);
load_print_backends (dialog);
dialog->internal_change = FALSE;
}
static void
gtk_page_setup_unix_dialog_finalize (GObject *object)
{
GtkPageSetupUnixDialog *dialog = GTK_PAGE_SETUP_UNIX_DIALOG (object);
GList *node;
if (dialog->request_details_tag)
{
g_signal_handler_disconnect (dialog->request_details_printer,
dialog->request_details_tag);
g_object_unref (dialog->request_details_printer);
dialog->request_details_printer = NULL;
dialog->request_details_tag = 0;
}
g_clear_object (&dialog->printer_list);
g_clear_object (&dialog->page_setup_list);
g_clear_object (&dialog->custom_paper_list);
g_clear_object (&dialog->manage_papers_list);
if (dialog->print_settings)
{
g_object_unref (dialog->print_settings);
dialog->print_settings = NULL;
}
for (node = dialog->print_backends; node != NULL; node = node->next)
gtk_print_backend_destroy (GTK_PRINT_BACKEND (node->data));
g_list_free_full (dialog->print_backends, g_object_unref);
dialog->print_backends = NULL;
G_OBJECT_CLASS (gtk_page_setup_unix_dialog_parent_class)->finalize (object);
}
static void
load_print_backends (GtkPageSetupUnixDialog *dialog)
{
GListModel *full_list;
GListStore *printer_list_list;
GList *node;
full_list = gtk_filter_list_model_get_model (GTK_FILTER_LIST_MODEL (dialog->printer_list));
printer_list_list = G_LIST_STORE (gtk_flatten_list_model_get_model (GTK_FLATTEN_LIST_MODEL (full_list)));
if (g_module_supported ())
dialog->print_backends = gtk_print_backend_load_modules ();
for (node = dialog->print_backends; node != NULL; node = node->next)
{
GtkPrintBackend *backend = node->data;
g_list_store_append (printer_list_list, gtk_print_backend_get_printers (backend));
}
}
static GtkPageSetup *
get_current_page_setup (GtkPageSetupUnixDialog *dialog)
{
guint selected;
GListModel *model;
selected = gtk_drop_down_get_selected (GTK_DROP_DOWN (dialog->paper_size_combo));
model = gtk_drop_down_get_model (GTK_DROP_DOWN (dialog->paper_size_combo));
if (selected != GTK_INVALID_LIST_POSITION)
return g_list_model_get_item (model, selected);
return gtk_page_setup_new ();
}
static gboolean
page_setup_is_equal (GtkPageSetup *a,
GtkPageSetup *b)
{
return
gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
gtk_page_setup_get_paper_size (b)) &&
gtk_page_setup_get_top_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_top_margin (b, GTK_UNIT_MM) &&
gtk_page_setup_get_bottom_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_bottom_margin (b, GTK_UNIT_MM) &&
gtk_page_setup_get_left_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_left_margin (b, GTK_UNIT_MM) &&
gtk_page_setup_get_right_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_right_margin (b, GTK_UNIT_MM);
}
static gboolean
page_setup_is_same_size (GtkPageSetup *a,
GtkPageSetup *b)
{
return gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
gtk_page_setup_get_paper_size (b));
}
static gboolean
set_paper_size (GtkPageSetupUnixDialog *dialog,
GtkPageSetup *page_setup,
gboolean size_only,
gboolean add_item)
{
GListModel *model;
GtkPageSetup *list_page_setup;
guint i;
if (page_setup == NULL)
return FALSE;
model = gtk_drop_down_get_model (GTK_DROP_DOWN (dialog->paper_size_combo));
for (i = 0; i < g_list_model_get_n_items (model); i++)
{
list_page_setup = g_list_model_get_item (model, i);
if (list_page_setup == NULL)
continue;
if ((size_only && page_setup_is_same_size (page_setup, list_page_setup)) ||
(!size_only && page_setup_is_equal (page_setup, list_page_setup)))
{
gtk_drop_down_set_selected (GTK_DROP_DOWN (dialog->paper_size_combo), i);
g_object_unref (list_page_setup);
return TRUE;
}
g_object_unref (list_page_setup);
}
if (add_item)
{
i = g_list_model_get_n_items (model);
g_list_store_append (dialog->page_setup_list, page_setup);
gtk_drop_down_set_selected (GTK_DROP_DOWN (dialog->paper_size_combo), i);
return TRUE;
}
return FALSE;
}
static void
fill_paper_sizes_from_printer (GtkPageSetupUnixDialog *dialog,
GtkPrinter *printer)
{
GList *list, *l;
GtkPageSetup *current_page_setup, *page_setup;
GtkPaperSize *paper_size;
int i;
g_list_store_remove_all (dialog->page_setup_list);
if (printer == NULL)
{
for (i = 0; i < G_N_ELEMENTS (common_paper_sizes); i++)
{
page_setup = gtk_page_setup_new ();
paper_size = gtk_paper_size_new (common_paper_sizes[i]);
gtk_page_setup_set_paper_size_and_default_margins (page_setup, paper_size);
gtk_paper_size_free (paper_size);
g_list_store_append (dialog->page_setup_list, page_setup);
g_object_unref (page_setup);
}
}
else
{
list = gtk_printer_list_papers (printer);
/* TODO: We should really sort this list so interesting size
are at the top */
for (l = list; l != NULL; l = l->next)
{
page_setup = l->data;
g_list_store_append (dialog->page_setup_list, page_setup);
g_object_unref (page_setup);
}
g_list_free (list);
}
current_page_setup = NULL;
/* When selecting a different printer, select its default paper size */
if (printer != NULL)
current_page_setup = gtk_printer_get_default_page_size (printer);
if (current_page_setup == NULL)
current_page_setup = get_current_page_setup (dialog);
if (!set_paper_size (dialog, current_page_setup, FALSE, FALSE))
set_paper_size (dialog, current_page_setup, TRUE, TRUE);
if (current_page_setup)
g_object_unref (current_page_setup);
}
static void
printer_changed_finished_callback (GtkPrinter *printer,
gboolean success,
GtkPageSetupUnixDialog *dialog)
{
g_signal_handler_disconnect (dialog->request_details_printer,
dialog->request_details_tag);
g_object_unref (dialog->request_details_printer);
dialog->request_details_tag = 0;
dialog->request_details_printer = NULL;
if (success)
fill_paper_sizes_from_printer (dialog, printer);
}
static void
printer_changed_callback (GtkDropDown *combo_box,
GParamSpec *pspec,
GtkPageSetupUnixDialog *dialog)
{
GtkPrinter *printer;
guint selected;
if (dialog->request_details_tag)
{
g_signal_handler_disconnect (dialog->request_details_printer,
dialog->request_details_tag);
g_object_unref (dialog->request_details_printer);
dialog->request_details_printer = NULL;
dialog->request_details_tag = 0;
}
selected = gtk_drop_down_get_selected (GTK_DROP_DOWN (dialog->printer_combo));
if (selected != GTK_INVALID_LIST_POSITION)
{
GListModel *model;
model = gtk_drop_down_get_model (GTK_DROP_DOWN (dialog->printer_combo));
printer = g_list_model_get_item (model, selected);
if (strcmp (gtk_printer_get_name (printer), _("Any Printer")) == 0)
g_clear_object (&printer);
if (printer == NULL ||
gtk_printer_has_details (printer))
fill_paper_sizes_from_printer (dialog, printer);
else
{
dialog->request_details_printer = g_object_ref (printer);
dialog->request_details_tag =
g_signal_connect (printer, "details-acquired",
G_CALLBACK (printer_changed_finished_callback), dialog);
gtk_printer_request_details (printer);
}
if (printer)
g_object_unref (printer);
if (dialog->print_settings)
{
const char *name = NULL;
if (printer)
name = gtk_printer_get_name (printer);
gtk_print_settings_set (dialog->print_settings,
"format-for-printer", name);
}
}
}
/* We do this munging because we don't want to show zero digits
after the decimal point, and not to many such digits if they
are nonzero. I wish printf let you specify max precision for %f... */
static char *
double_to_string (double d,
GtkUnit unit)
{
char *val, *p;
struct lconv *locale_data;
const char *decimal_point;
int decimal_point_len;
locale_data = localeconv ();
decimal_point = locale_data->decimal_point;
decimal_point_len = strlen (decimal_point);
/* Max two decimal digits for inch, max one for mm */
if (unit == GTK_UNIT_INCH)
val = g_strdup_printf ("%.2f", d);
else
val = g_strdup_printf ("%.1f", d);
if (strstr (val, decimal_point))
{
p = val + strlen (val) - 1;
while (*p == '0')
p--;
if (p - val + 1 >= decimal_point_len &&
strncmp (p - (decimal_point_len - 1), decimal_point, decimal_point_len) == 0)
p -= decimal_point_len;
p[1] = '\0';
}
return val;
}
static void
custom_paper_dialog_response_cb (GtkDialog *custom_paper_dialog,
int response_id,
gpointer user_data)
{
GtkPageSetupUnixDialog *dialog = GTK_PAGE_SETUP_UNIX_DIALOG (user_data);
GtkPageSetup *last_page_setup;
dialog->internal_change = TRUE;
gtk_print_load_custom_papers (dialog->custom_paper_list);
printer_changed_callback (GTK_DROP_DOWN (dialog->printer_combo), NULL, dialog);
dialog->internal_change = FALSE;
if (dialog->last_setup)
last_page_setup = g_object_ref (dialog->last_setup);
else
last_page_setup = gtk_page_setup_new (); /* "good" default */
set_paper_size (dialog, last_page_setup, FALSE, TRUE);
g_object_unref (last_page_setup);
gtk_window_destroy (GTK_WINDOW (custom_paper_dialog));
}
static void
paper_size_changed (GtkDropDown *combo_box,
GParamSpec *pspec,
GtkPageSetupUnixDialog *dialog)
{
GtkPageSetup *page_setup, *last_page_setup;
guint selected;
GtkUnit unit;
char *str, *w, *h;
char *top, *bottom, *left, *right;
GtkLabel *label;
const char *unit_str;
if (dialog->internal_change)
return;
label = GTK_LABEL (dialog->paper_size_label);
selected = gtk_drop_down_get_selected (GTK_DROP_DOWN (combo_box));
if (selected != GTK_INVALID_LIST_POSITION)
{
GListModel *papers, *model;
papers = gtk_drop_down_get_model (GTK_DROP_DOWN (dialog->paper_size_combo));
page_setup = g_list_model_get_item (papers, selected);
model = gtk_flatten_list_model_get_model_for_item (GTK_FLATTEN_LIST_MODEL (papers), selected);
if (model == G_LIST_MODEL (dialog->manage_papers_list))
{
GtkWidget *custom_paper_dialog;
/* Change from "manage" menu item to last value */
if (dialog->last_setup)
last_page_setup = g_object_ref (dialog->last_setup);
else
last_page_setup = gtk_page_setup_new (); /* "good" default */
set_paper_size (dialog, last_page_setup, FALSE, TRUE);
g_object_unref (last_page_setup);
/* And show the custom paper dialog */
custom_paper_dialog = _gtk_custom_paper_unix_dialog_new (GTK_WINDOW (dialog), NULL);
g_signal_connect (custom_paper_dialog, "response", G_CALLBACK (custom_paper_dialog_response_cb), dialog);
gtk_window_present (GTK_WINDOW (custom_paper_dialog));
g_object_unref (page_setup);
return;
}
if (dialog->last_setup)
g_object_unref (dialog->last_setup);
dialog->last_setup = g_object_ref (page_setup);
unit = _gtk_print_get_default_user_units ();
if (unit == GTK_UNIT_MM)
unit_str = _("mm");
else
unit_str = _("inch");
w = double_to_string (gtk_page_setup_get_paper_width (page_setup, unit),
unit);
h = double_to_string (gtk_page_setup_get_paper_height (page_setup, unit),
unit);
str = g_strdup_printf ("%s × %s %s", w, h, unit_str);
g_free (w);
g_free (h);
gtk_label_set_text (label, str);
g_free (str);
top = double_to_string (gtk_page_setup_get_top_margin (page_setup, unit), unit);
bottom = double_to_string (gtk_page_setup_get_bottom_margin (page_setup, unit), unit);
left = double_to_string (gtk_page_setup_get_left_margin (page_setup, unit), unit);
right = double_to_string (gtk_page_setup_get_right_margin (page_setup, unit), unit);
str = g_strdup_printf (_("Margins:\n"
" Left: %s %s\n"
" Right: %s %s\n"
" Top: %s %s\n"
" Bottom: %s %s"
),
left, unit_str,
right, unit_str,
top, unit_str,
bottom, unit_str);
g_free (top);
g_free (bottom);
g_free (left);
g_free (right);
gtk_widget_set_tooltip_text (dialog->paper_size_label, str);
g_free (str);
g_object_unref (page_setup);
}
else
{
gtk_label_set_text (label, "");
gtk_widget_set_tooltip_text (dialog->paper_size_label, NULL);
if (dialog->last_setup)
g_object_unref (dialog->last_setup);
dialog->last_setup = NULL;
}
}
/**
* gtk_page_setup_unix_dialog_new:
* @title: (nullable): the title of the dialog
* @parent: (nullable): transient parent of the dialog
*
* Creates a new page setup dialog.
*
* Returns: the new `GtkPageSetupUnixDialog`
*/
GtkWidget *
gtk_page_setup_unix_dialog_new (const char *title,
GtkWindow *parent)
{
GtkWidget *result;
if (title == NULL)
title = _("Page Setup");
result = g_object_new (GTK_TYPE_PAGE_SETUP_UNIX_DIALOG,
"title", title,
NULL);
if (parent)
gtk_window_set_transient_for (GTK_WINDOW (result), parent);
return result;
}
static GtkPageOrientation
get_orientation (GtkPageSetupUnixDialog *dialog)
{
if (gtk_check_button_get_active (GTK_CHECK_BUTTON (dialog->portrait_radio)))
return GTK_PAGE_ORIENTATION_PORTRAIT;
if (gtk_check_button_get_active (GTK_CHECK_BUTTON (dialog->landscape_radio)))
return GTK_PAGE_ORIENTATION_LANDSCAPE;
if (gtk_check_button_get_active (GTK_CHECK_BUTTON (dialog->reverse_landscape_radio)))
return GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE;
return GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT;
}
static void
set_orientation (GtkPageSetupUnixDialog *dialog,
GtkPageOrientation orientation)
{
switch (orientation)
{
case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
gtk_check_button_set_active (GTK_CHECK_BUTTON (dialog->reverse_portrait_radio), TRUE);
break;
case GTK_PAGE_ORIENTATION_PORTRAIT:
gtk_check_button_set_active (GTK_CHECK_BUTTON (dialog->portrait_radio), TRUE);
break;
case GTK_PAGE_ORIENTATION_LANDSCAPE:
gtk_check_button_set_active (GTK_CHECK_BUTTON (dialog->landscape_radio), TRUE);
break;
case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
gtk_check_button_set_active (GTK_CHECK_BUTTON (dialog->reverse_landscape_radio), TRUE);
break;
default:
break;
}
}
/**
* gtk_page_setup_unix_dialog_set_page_setup:
* @dialog: a `GtkPageSetupUnixDialog`
* @page_setup: a `GtkPageSetup`
*
* Sets the `GtkPageSetup` from which the page setup
* dialog takes its values.
*/
void
gtk_page_setup_unix_dialog_set_page_setup (GtkPageSetupUnixDialog *dialog,
GtkPageSetup *page_setup)
{
if (page_setup)
{
set_paper_size (dialog, page_setup, FALSE, TRUE);
set_orientation (dialog, gtk_page_setup_get_orientation (page_setup));
}
}
/**
* gtk_page_setup_unix_dialog_get_page_setup:
* @dialog: a `GtkPageSetupUnixDialog`
*
* Gets the currently selected page setup from the dialog.
*
* Returns: (transfer none): the current page setup
*/
GtkPageSetup *
gtk_page_setup_unix_dialog_get_page_setup (GtkPageSetupUnixDialog *dialog)
{
GtkPageSetup *page_setup;
page_setup = get_current_page_setup (dialog);
gtk_page_setup_set_orientation (page_setup, get_orientation (dialog));
return page_setup;
}
static gboolean
set_active_printer (GtkPageSetupUnixDialog *dialog,
const char *printer_name)
{
guint i, n;
GtkPrinter *printer;
if (!printer_name)
return FALSE;
for (i = 0, n = g_list_model_get_n_items (dialog->printer_list); i < n; i++)
{
printer = g_list_model_get_item (dialog->printer_list, i);
if (strcmp (gtk_printer_get_name (printer), printer_name) == 0)
{
gtk_drop_down_set_selected (GTK_DROP_DOWN (dialog->printer_combo), i);
g_object_unref (printer);
return TRUE;
}
g_object_unref (printer);
}
return FALSE;
}
/**
* gtk_page_setup_unix_dialog_set_print_settings:
* @dialog: a `GtkPageSetupUnixDialog`
* @print_settings: (nullable): a `GtkPrintSettings`
*
* Sets the `GtkPrintSettings` from which the page setup dialog
* takes its values.
*/
void
gtk_page_setup_unix_dialog_set_print_settings (GtkPageSetupUnixDialog *dialog,
GtkPrintSettings *print_settings)
{
const char *format_for_printer;
if (dialog->print_settings == print_settings) return;
if (dialog->print_settings)
g_object_unref (dialog->print_settings);
dialog->print_settings = print_settings;
if (print_settings)
{
g_object_ref (print_settings);
format_for_printer = gtk_print_settings_get (print_settings, "format-for-printer");
/* Set printer if in list, otherwise set when
* that printer is added
*/
set_active_printer (dialog, format_for_printer);
}
}
/**
* gtk_page_setup_unix_dialog_get_print_settings:
* @dialog: a `GtkPageSetupUnixDialog`
*
* Gets the current print settings from the dialog.
*
* Returns: (transfer none) (nullable): the current print settings
**/
GtkPrintSettings *
gtk_page_setup_unix_dialog_get_print_settings (GtkPageSetupUnixDialog *dialog)
{
return dialog->print_settings;
}
|