FLTK logo

[master] d9451a8 - Add platform-independent Fl_Screen_Driver::print_or_copy_window().

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] d9451a8 - Add platform-independent Fl_Screen_Driver::print_or_copy_window(). "ManoloFLTK" Feb 02, 2022  
 
commit d9451a86696a2aa58d46cb18180c9344a5e0c179
Author:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
AuthorDate: Wed Feb 2 14:55:38 2022 +0100
Commit:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
CommitDate: Wed Feb 2 14:55:48 2022 +0100

    Add platform-independent Fl_Screen_Driver::print_or_copy_window().
    
    This member function is available for all platforms to print or copy the front window
    with or without its titlebar.

 src/Fl_Screen_Driver.H |   2 +
 src/Fl_cocoa.mm        |  43 +----------------
 src/print_button.cxx   | 128 +++++++++++++++++++++++++++----------------------
 3 files changed, 75 insertions(+), 98 deletions(-)

diff --git src/Fl_Screen_Driver.H src/Fl_Screen_Driver.H
index 1f6c80f..fae5b0f 100644
--- src/Fl_Screen_Driver.H
+++ src/Fl_Screen_Driver.H
@@ -201,6 +201,8 @@ public:
   virtual APP_SCALING_CAPABILITY rescalable() { return NO_APP_SCALING; }
   // supports Fl_Window::default_icons()
   virtual void default_icons(const Fl_RGB_Image *icons[], int count);
+  // this one is implemented in print_button.cxx
+  static int print_or_copy_window(Fl_Window*, bool, int);
 };
 
 #endif // !FL_SCREEN_DRIVER_H
diff --git src/Fl_cocoa.mm src/Fl_cocoa.mm
index 340fe2e..d4a155a 100644
--- src/Fl_cocoa.mm
+++ src/Fl_cocoa.mm
@@ -3949,50 +3949,11 @@ static PrintWithTitlebarItem *print_with_titlebar_item = NULL;
                              nil];
     [NSApp orderFrontStandardAboutPanelWithOptions:options];
 }
-//#include <FL/Fl_PostScript.H>
 - (void)printPanel
 {
-  Fl_Printer printer;
-  //Fl_PostScript_File_Device printer;
-  int w, h, ww, wh;
-  Fl_Window *win = Fl::first_window();
-  if(!win) return;
-  if (win->parent()) win = win->top_window();
-  if( printer.begin_job(1) ) return;
-  if( printer.begin_page() ) return;
+  bool grab_decoration = ([print_with_titlebar_item state] == NSOnState);
   fl_lock_function();
-  // scale the printer device so that the window fits on the page
-  float scale = 1;
-  printer.printable_rect(&w, &h);
-  if ([print_with_titlebar_item state] == NSOnState) {
-    ww = win->decorated_w();
-    wh = win->decorated_h();
-  } else {
-    ww = win->w();
-    wh = win->h();
-  }
-  if (ww>w || wh>h) {
-    scale = (float)w/win->w();
-    if ((float)h/wh < scale) scale = (float)h/wh;
-    printer.scale(scale);
-    printer.printable_rect(&w, &h);
-  }
-//#define ROTATE 1
-#ifdef ROTATE
-  printer.scale(scale * 0.8, scale * 0.8);
-  printer.printable_rect(&w, &h);
-  printer.origin(w/2, h/2 );
-  printer.rotate(20.);
-#else
-  printer.origin(w/2, h/2);
-#endif
-  if ([print_with_titlebar_item state] == NSOnState)
-    printer.draw_decorated_window(win, -ww/2, -wh/2);
-  else
-    printer.draw(win, -ww/2, -wh/2);
-  //printer.print_window_part(win,0,0,win->w(),win->h(), -ww/2, -wh/2);
-  printer.end_page();
-  printer.end_job();
+  Fl_Screen_Driver::print_or_copy_window(Fl::first_window(), grab_decoration, 1);
   fl_unlock_function();
 }
 - (void)terminate:(id)sender
diff --git src/print_button.cxx src/print_button.cxx
index 35c7d10..40836ad 100644
--- src/print_button.cxx
+++ src/print_button.cxx
@@ -38,14 +38,17 @@
 // Currently the shortcut can't be configured and is always ALT+SHIFT+'s'.
 // Todo: make the shortcut configurable.
 
+#include <FL/Fl_Window.H>
+#include "Fl_Screen_Driver.H"
+#include <FL/Fl_Printer.H>
+#include <FL/Fl_PostScript.H>
+#include <FL/Fl_Copy_Surface.H>
+
+//#define USE_PRINT_BUTTON 1
 #ifdef USE_PRINT_BUTTON
 
-#include <FL/Fl_Printer.H>
 #include <FL/Fl_Button.H>
 #include <FL/Fl_Check_Button.H>
-#include <FL/Fl_Copy_Surface.H>
-#include <FL/Fl_Image_Surface.H>
-#include <FL/Fl_RGB_Image.H>
 #include <FL/fl_draw.H>
 #include <FL/fl_ask.H>
 
@@ -66,7 +69,7 @@ static Fl_Check_Button *deco_button = 0;  // window decoration button
 //  2: copy window to clipboard
 // else: see 2.
 
-static void output_cb(Fl_Widget *o, void *data) {
+static void output_cb(Fl_Widget * /*unused*/, void *data) {
 
   print_window->hide();
   Fl_Window *win = Fl::first_window();
@@ -75,58 +78,7 @@ static void output_cb(Fl_Widget *o, void *data) {
   // print window again (which ends the program)
 
   if (!win) return;
-
-  int mode = fl_int(data);
-  int deco = deco_button->value();
-  int ww = deco ? win->decorated_w() : win->w();
-  int wh = deco ? win->decorated_h() : win->h();
-
-  if (mode == 1) { // print window
-
-    Fl_Printer printer;
-    int w, h;
-    if (printer.begin_job(1)) { // fail or cancel
-      print_window->show();
-      return;
-    }
-    if (printer.begin_page()) { // fail or cancel
-      print_window->show();
-      return;
-    }
-    printer.printable_rect(&w, &h);
-    // scale the printer device so that the window fits on the page
-    float scale = 1;
-    if (ww > w || wh > h) {
-      scale = (float)w / ww;
-      if ((float)h / wh < scale)
-        scale = (float)h / wh;
-      printer.scale(scale, scale);
-      printer.printable_rect(&w, &h);
-    }
-#ifdef ROTATE
-    printer.scale(scale * 0.8, scale * 0.8);
-    printer.printable_rect(&w, &h);
-    printer.origin(w / 2, h / 2);
-    printer.rotate(ROTATE);
-    printer.print_widget(win, -win->w() / 2, -win->h() / 2);
-#else
-    printer.origin(w / 2, h / 2);
-    printer.print_window(win, -ww / 2, -wh / 2);
-#endif
-    printer.end_page();
-    printer.end_job();
-
-  } else { // copy window to clipboard
-
-    Fl_Copy_Surface *surf = new Fl_Copy_Surface(ww, wh);
-    if (deco)
-      surf->draw_decorated_window(win); // draw the window content
-    else
-      surf->draw(win); // draw the window content
-    delete surf;       // put the window on the clipboard
-
-  } // print | copy
-
+  Fl_Screen_Driver::print_or_copy_window(win, deco_button->value(), fl_int(data));
   print_window->show();
 }
 
@@ -190,3 +142,65 @@ int fl_create_print_window() {
 }
 
 #endif // USE_PRINT_BUTTON
+
+/**
+ To print or copy to clipboard a window.
+ \param win The window to process
+ \param grab_decoration true means the window titlebar is processed too
+ \param mode 1 means print, other means copy
+ */
+int Fl_Screen_Driver::print_or_copy_window(Fl_Window *win, bool grab_decoration, int mode)
+{
+  if (!win) return 0;
+
+  int ww = grab_decoration ? win->decorated_w() : win->w();
+  int wh = grab_decoration ? win->decorated_h() : win->h();
+
+  if (mode == 1) { // print window
+
+    // exchange the 2 constructors below to test class Fl_PostScript_File_Device
+    Fl_Printer printer;
+    //Fl_PostScript_File_Device printer;
+    int w, h;
+    if (printer.begin_job(1)) { // fail or cancel
+      return 1;
+    }
+    if (printer.begin_page()) { // fail or cancel
+      return 1;
+    }
+    printer.printable_rect(&w, &h);
+    // scale the printer device so that the window fits on the page
+    float scale = 1;
+    if (ww > w || wh > h) {
+      scale = (float)w / ww;
+      if ((float)h / wh < scale)
+        scale = (float)h / wh;
+      printer.scale(scale, scale);
+      printer.printable_rect(&w, &h);
+    }
+#ifdef ROTATE
+    printer.scale(scale * 0.8, scale * 0.8);
+    printer.printable_rect(&w, &h);
+    printer.origin(w / 2, h / 2);
+    printer.rotate(ROTATE);
+    printer.print_widget(win, -win->w() / 2, -win->h() / 2);
+#else
+    printer.origin(w / 2, h / 2);
+    if (grab_decoration) printer.draw_decorated_window(win, -ww / 2, -wh / 2);
+    else printer.draw(win, -ww / 2, -wh / 2);
+#endif
+    printer.end_page();
+    printer.end_job();
+
+  } else { // copy window to clipboard
+
+    Fl_Copy_Surface *surf = new Fl_Copy_Surface(ww, wh);
+    if (grab_decoration)
+      surf->draw_decorated_window(win); // draw the window content
+    else
+      surf->draw(win); // draw the window content
+    delete surf;       // put the window on the clipboard
+
+  } // print | copy
+  return 0;
+}
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.