File: wb_printing_linux.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (341 lines) | stat: -rw-r--r-- 12,085 bytes parent folder | download
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

#include "grtui/gui_plugin_base.h"
#include <gtkmm.h>
#include "wb_printing.h"
#include "mdc_canvas_view_printing.h"
#include "wbcanvas/model_diagram_impl.h"
#include "grts/structs.workbench.h"
#include "gtk_helpers.h"
#include <stdio.h>
#include <gtk/gtk.h>
#include "base/string_utilities.h"

using base::strfmt;

namespace linux_printing
{
  
  static void update_gtk_page_setup_from_grt(Glib::RefPtr<Gtk::PageSetup> &setup,
                                             const app_PageSettingsRef &settings,
                                             bool skip_margins)
  {
    app_PaperTypeRef current_type = settings->paperType();
    Gtk::PaperSize paper_size(current_type->name().c_str()
                             , current_type->caption().c_str()
                             , current_type->width()
                             , current_type->height()
                             , Gtk::UNIT_MM
                              );
    setup->set_bottom_margin((skip_margins?0:1)*settings->marginBottom(), Gtk::UNIT_MM);
    setup->set_left_margin((skip_margins?0:1)*settings->marginLeft(), Gtk::UNIT_MM);
    setup->set_right_margin((skip_margins?0:1)*settings->marginRight(), Gtk::UNIT_MM);
    setup->set_top_margin((skip_margins?0:1)*settings->marginTop(), Gtk::UNIT_MM);
    
    if (settings->paperType().is_valid())
      setup->set_paper_size(paper_size);

    if (*settings->orientation() == "landscape")
      setup->set_orientation(Gtk::PAGE_ORIENTATION_LANDSCAPE);
    else
      setup->set_orientation(Gtk::PAGE_ORIENTATION_PORTRAIT);
  }

//==============================================================================
class WBPageSetup
{
public:
  WBPageSetup(const app_PageSettingsRef& ps);

  void propagate_print_settings_to_grt_tree();
  virtual void run_setup();

  Glib::RefPtr<Gtk::PageSetup>      _page_setup;
  Glib::RefPtr<Gtk::PrintSettings>  _print_settings;

private:
  static app_PageSettingsRef                      _app_page_settings;
};

app_PageSettingsRef               WBPageSetup::_app_page_settings;

//------------------------------------------------------------------------------
WBPageSetup::WBPageSetup(const app_PageSettingsRef& ps)
{
  _app_page_settings = ps;
  if (!_page_setup)
    _page_setup     = Gtk::PageSetup::create();

  if (!_print_settings)
    _print_settings = Gtk::PrintSettings::create();
}


//------------------------------------------------------------------------------
void WBPageSetup::run_setup()
{
  if (_app_page_settings.is_valid())
  {
    update_gtk_page_setup_from_grt(_page_setup, _app_page_settings, false);
  }

  Glib::RefPtr<Gtk::PageSetup> new_page_setup = Gtk::run_page_setup_dialog(*get_mainwindow(), _page_setup, _print_settings);
  _page_setup = new_page_setup;

  propagate_print_settings_to_grt_tree();
}

//------------------------------------------------------------------------------
void WBPageSetup::propagate_print_settings_to_grt_tree()
{
  std::string page_orientation_as_str;
  
  // Set orientation
  Gtk::PageOrientation page_orient = _page_setup->get_orientation();
  if ( page_orient == Gtk::PAGE_ORIENTATION_PORTRAIT )
    page_orientation_as_str = "portrait";
  else if ( page_orient == Gtk::PAGE_ORIENTATION_LANDSCAPE )
    page_orientation_as_str = "landscape";
  else
  {
    g_message("Unsupported page orientation. Setting page orientation to portrait");
    page_orientation_as_str = "portrait";
  }  
  _app_page_settings->orientation(page_orientation_as_str);
  
  // Set paper type
  Gtk::PaperSize   gtk_paper_size      = _page_setup->get_paper_size();
  app_PaperTypeRef paper_type          = _app_page_settings->paperType();

  const std::string paper_name = gtk_paper_size_get_name(gtk_paper_size.gobj());

  grt::ListRef<app_PaperType> paper_types(grt::ListRef<app_PaperType>::cast_from(_app_page_settings.get_grt()->get("/wb/options/paperTypes")));
  
  app_PaperTypeRef ptype(app_PaperTypeRef::cast_from(grt::find_named_object_in_list(paper_types, paper_name)));

  _app_page_settings->marginBottom(gtk_paper_size.get_default_bottom_margin(Gtk::UNIT_MM));
  _app_page_settings->marginLeft(gtk_paper_size.get_default_left_margin(Gtk::UNIT_MM));
  _app_page_settings->marginRight(gtk_paper_size.get_default_right_margin(Gtk::UNIT_MM));
  _app_page_settings->marginTop(gtk_paper_size.get_default_top_margin(Gtk::UNIT_MM));

  if (ptype.is_valid())
    _app_page_settings->paperType(ptype);
  else
    g_warning("Unknown paper size selected in GTK Page Setup dialog: %s", paper_name.c_str());
}

//==============================================================================
class WBPrintOperation : public Gtk::PrintOperation
{
public:
  WBPrintOperation(const model_DiagramRef& diagram);
  static Glib::RefPtr<WBPrintOperation> create(const model_DiagramRef& diagram);
  virtual ~WBPrintOperation();
    
protected:
  virtual void on_begin_print(const Glib::RefPtr<Gtk::PrintContext>& ctx);    
  virtual void on_draw_page(const Glib::RefPtr<Gtk::PrintContext>& ctx, int page_nr);    
  virtual void on_done(Gtk::PrintOperationResult result);
  
private:
  model_DiagramRef                  _diagram;
  mdc::CanvasViewExtras             *_printer;
  int _xpages;
  int _ypages;

  Glib::RefPtr<Gtk::PageSetup>      _page_setup;
  Glib::RefPtr<Gtk::PrintSettings>  _print_settings;
};

//------------------------------------------------------------------------------
WBPrintOperation::WBPrintOperation(const model_DiagramRef& diagram)
    : _diagram(diagram)
    , _printer(0)
    , _xpages(0)
    , _ypages(0)
{
  _page_setup     = Gtk::PageSetup::create();
  _print_settings = Gtk::PrintSettings::create();
}

//------------------------------------------------------------------------------
Glib::RefPtr<WBPrintOperation> WBPrintOperation::create(const model_DiagramRef& diagram)
{
  return Glib::RefPtr<WBPrintOperation>(new WBPrintOperation(diagram));
}

//------------------------------------------------------------------------------
WBPrintOperation::~WBPrintOperation()
{
  delete _printer;
}

//------------------------------------------------------------------------------
void WBPrintOperation::on_begin_print(const Glib::RefPtr<Gtk::PrintContext>& ctx)
{
  app_PageSettingsRef pageSettings(workbench_DocumentRef::cast_from(_diagram.get_grt()->get("/wb/doc"))->pageSettings());
  app_PaperTypeRef paperType(pageSettings->paperType());

  update_gtk_page_setup_from_grt(_page_setup, pageSettings, true);

  Gtk::PaperSize gpaperSize(_page_setup->get_paper_size());

  set_default_page_setup(_page_setup);
  set_print_settings(_print_settings);
  set_track_print_status();

  // page settings in cairo coordinates
  float paperWidth= (*paperType->width() * *pageSettings->scale());
  float paperHeight= (*paperType->height() * *pageSettings->scale());
  float marginLeft= (*pageSettings->marginLeft() * *pageSettings->scale());
  float marginRight= (*pageSettings->marginRight() * *pageSettings->scale());
  float marginTop= (*pageSettings->marginTop() * *pageSettings->scale());
  float marginBottom= (*pageSettings->marginBottom() * *pageSettings->scale());

  if (pageSettings->orientation() == "landscape")
  {
    std::swap(paperWidth, paperHeight);
    std::swap(marginLeft, marginTop);
    std::swap(marginRight, marginBottom);
  }

  // size of the printable area in cairo coords
  base::Size pageSize;
  pageSize.width= paperWidth - marginLeft - marginRight;
  pageSize.height= paperHeight - marginTop - marginBottom;
  
  _printer= new mdc::CanvasViewExtras(_diagram->get_data()->get_canvas_view());
  
  // margins are already added by the system
  _printer->set_page_margins(marginTop, marginLeft, marginBottom, marginRight);
  _printer->set_paper_size(paperWidth, paperHeight);
  //    _printer->set_orientation(pageSettings->orientation()=="landscape"?mdc::Landscape:mdc::Portrait);
  //
  
  _printer->set_print_border(true);

  set_n_pages(wbprint::getPageCount(_diagram));
  wbprint::getPageLayout(_diagram, _xpages, _ypages);
}

//------------------------------------------------------------------------------
void WBPrintOperation::on_draw_page(const Glib::RefPtr<Gtk::PrintContext>& ctx, int page_nr)
{
  Cairo::RefPtr<Cairo::Context> context = ctx->get_cairo_context();
  mdc::CairoCtx cairoctx(context->cobj());

  // scaling to transform from cairo coordinates to Cocoa coordinates
  double pwidth, pheight;
  _printer->get_paper_size(pwidth, pheight);
  float xscale= ctx->get_width() / pwidth;
  float yscale= ctx->get_height() / pheight;

  // scale has to be set here, ctx->get_width() returns some different value in on_begin_print()
  _printer->set_scale(xscale, yscale);

  _printer->render_page(&cairoctx, page_nr % _xpages, page_nr / _xpages);
 
}

//------------------------------------------------------------------------------
void WBPrintOperation::on_done(Gtk::PrintOperationResult result)
{
  delete _printer;
  _printer= 0;
  PrintOperation::on_done(result);
}

//==============================================================================
class WBPrintingLinux : public GUIPluginBase
{
  public:
    WBPrintingLinux(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args);
    virtual void execute();
    virtual void show_plugin();
    
  private:
    void on_print_done(Gtk::PrintOperationResult result, Glib::RefPtr<WBPrintOperation> &op);
    model_DiagramRef    _diagram; //!< TODO: use currently selected diagram!
};

//------------------------------------------------------------------------------
WBPrintingLinux::WBPrintingLinux(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args)
                : GUIPluginBase(m)
                , _diagram(model_DiagramRef::cast_from(args.get(0)))
{}

//------------------------------------------------------------------------------
void WBPrintingLinux::execute()
{}

//------------------------------------------------------------------------------
void WBPrintingLinux::show_plugin()
{
  try
  {
    Glib::RefPtr<WBPrintOperation> printer = WBPrintOperation::create(_diagram);

    printer->signal_done().connect(sigc::bind(sigc::mem_fun(this, &WBPrintingLinux::on_print_done), printer));
    /*Gtk::PrintOperationResult result = */printer->run(Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG, *(get_mainwindow()));
  }
  catch (const Gtk::PrintError &e)
  {
    g_message("Error while printing %s", e.what().c_str());
  }
}

//------------------------------------------------------------------------------
void WBPrintingLinux::on_print_done(Gtk::PrintOperationResult result, Glib::RefPtr<WBPrintOperation> &op)
{
  if (result == Gtk::PRINT_OPERATION_RESULT_ERROR)
  {
    Gtk::MessageDialog err_dlg(*get_mainwindow(), "Error printing document", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
    err_dlg.run();
  }
  else if (result == Gtk::PRINT_OPERATION_RESULT_APPLY)
  {
    //_print_settings = op->get_print_settings();
  }
}

}// end of namespace linux_printing

//------------------------------------------------------------------------------
extern "C" 
{
  GUIPluginBase *createPrintDialog(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args)
  {
    //return new linux_printing::WBPrintingLinux(m, grtm, args);
    linux_printing::WBPrintingLinux lp(m, grtm, args);
    lp.show_plugin();
    return 0;
  }
};

//------------------------------------------------------------------------------
extern "C" 
{
  GUIPluginBase *createPrintPreviewDialog(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args)
  {
    g_message("print preview");
//    linux_printing::WBPrintingLinux lp(m, grtm, args);
//    lp.show_plugin();
    return 0;
  }
};

//------------------------------------------------------------------------------
extern "C" 
{
  GUIPluginBase *createPrintSetupDialog(grt::Module *m, bec::GRTManager *grtm, const grt::BaseListRef &args)
  { 
    workbench_DocumentRef doc(workbench_DocumentRef::cast_from(grtm->get_grt()->get("/wb/doc")));
    if (doc.is_valid())
    {
      linux_printing::WBPageSetup ps(doc->pageSettings());

      ps.run_setup();
    }
    return 0;
  }
};