FLTK logo

[master] 935074b - macOS: add "Toggle printing of titlebar" to application menu.

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] 935074b - macOS: add "Toggle printing of titlebar" to application menu. "ManoloFLTK" Feb 02, 2022  
 
commit 935074ba0b5acc99f0bf82e73be1f002eab07157
Author:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
AuthorDate: Wed Feb 2 11:05:39 2022 +0100
Commit:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
CommitDate: Wed Feb 2 11:05:53 2022 +0100

    macOS: add "Toggle printing of titlebar" to application menu.

 FL/mac.H                     | 12 +++++++----
 src/Fl_MacOS_Sys_Menu_Bar.mm |  8 +++++---
 src/Fl_cocoa.mm              | 48 ++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 57 insertions(+), 11 deletions(-)

diff --git FL/mac.H FL/mac.H
index 588afcf..48aea68 100644
--- FL/mac.H
+++ FL/mac.H
@@ -140,10 +140,14 @@ public:
   /** Localizable text for the "About xxx" application menu item */
   static const char *about;
   /** Localizable text for the "Print Front Window" application menu item.
-   This menu item won't be displayed if Fl_Mac_App_Menu::print
+   This menu item and next one won't be displayed if Fl_Mac_App_Menu::print
    is set to an empty string.
    */
-  static const char *print;
+  static const char *print_with_titlebar;
+  /** Localizable text for the "Print Front Window" application menu item. */
+  static const char *print_no_titlebar;
+  /** Localizable text for the "Toggle print titlebar" application menu item. */
+  static const char *toggle_print_titlebar;
   /** Localizable text for the "Services" application menu item */
   static const char *services;
   /** Localizable text for the "Hide xxx" application menu item */
@@ -155,8 +159,8 @@ public:
   /** Localizable text for the "Quit xxx" application menu item */
   static const char *quit;
   /** Adds custom menu item(s) to the application menu of the system menu bar.
-   They are positioned after the "Print Front Window" item, or at its place
-   if it was removed with <tt>Fl_Mac_App_Menu::print = ""</tt>.
+   They are positioned after the "Print Front Window / Toggle printing of titlebar" items, or at their place
+   if they were removed with <tt>Fl_Mac_App_Menu::print = ""</tt>.
    \param m zero-ending array of Fl_Menu_Item 's.
    */
   static void custom_application_menu_items(const Fl_Menu_Item *m);
diff --git src/Fl_MacOS_Sys_Menu_Bar.mm src/Fl_MacOS_Sys_Menu_Bar.mm
index 3320d0f..ad24ec5 100644
--- src/Fl_MacOS_Sys_Menu_Bar.mm
+++ src/Fl_MacOS_Sys_Menu_Bar.mm
@@ -74,7 +74,9 @@ Fl_MacOS_Sys_Menu_Bar_Driver* Fl_MacOS_Sys_Menu_Bar_Driver::driver() {
 
 // Apple App Menu
 const char *Fl_Mac_App_Menu::about = "About %@";
-const char *Fl_Mac_App_Menu::print = "Print Front Window";
+const char *Fl_Mac_App_Menu::print_with_titlebar = "Print Front Window & Titlebar";
+const char *Fl_Mac_App_Menu::print_no_titlebar = "Print Front Window";
+const char *Fl_Mac_App_Menu::toggle_print_titlebar = "Toggle printing of titlebar";
 const char *Fl_Mac_App_Menu::services = "Services";
 const char *Fl_Mac_App_Menu::hide = "Hide %@";
 const char *Fl_Mac_App_Menu::hide_others = "Hide Others";
@@ -504,8 +506,8 @@ void Fl_Mac_App_Menu::custom_application_menu_items(const Fl_Menu_Item *m)
   NSInteger to_index;
   if ([[menu itemAtIndex:2] action] != @selector(printPanel)) { // the 'Print' item was removed
     [menu insertItem:[NSMenuItem separatorItem] atIndex:1];
-    to_index = 2;
-  } else to_index = 3; // after the "Print Front Window" item
+    to_index = 3;
+  } else to_index = 4; // after the "Print Front Window/Toggle" items
   NSInteger count = [menu numberOfItems];
   createSubMenu(menu, m, NULL, @selector(customCallback)); // add new items at end of application menu
   NSInteger count2 = [menu numberOfItems];
diff --git src/Fl_cocoa.mm src/Fl_cocoa.mm
index d9f6e0f..4b18507 100644
--- src/Fl_cocoa.mm
+++ src/Fl_cocoa.mm
@@ -3902,6 +3902,28 @@ int Fl_Cocoa_Window_Driver::set_cursor(const Fl_RGB_Image *image, int hotx, int
   return 1;
 }
 
+@interface PrintWithTitlebarItem : NSMenuItem {
+}
+- (void) toggleCallback;
+@end
+
+@implementation PrintWithTitlebarItem
+- (void) toggleCallback {
+  NSMenuItem *item = [self representedObject];
+  const char *title;
+  if ([self state] == NSOnState) {
+    [self setState:NSOffState];
+    title = Fl_Mac_App_Menu::print_no_titlebar;
+  } else {
+    [self setState:NSOnState];
+    title = Fl_Mac_App_Menu::print_with_titlebar;
+  }
+  [item setTitle:NSLocalizedString([NSString stringWithUTF8String:title], nil)];
+}
+@end
+
+static PrintWithTitlebarItem *print_with_titlebar_item = NULL;
+
 @interface FLaboutItemTarget : NSObject
 {
 }
@@ -3942,8 +3964,13 @@ int Fl_Cocoa_Window_Driver::set_cursor(const Fl_RGB_Image *image, int hotx, int
   // scale the printer device so that the window fits on the page
   float scale = 1;
   printer.printable_rect(&w, &h);
-  ww = win->decorated_w();
-  wh = win->decorated_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;
@@ -3959,7 +3986,10 @@ int Fl_Cocoa_Window_Driver::set_cursor(const Fl_RGB_Image *image, int hotx, int
 #else
   printer.origin(w/2, h/2);
 #endif
-  printer.print_window(win, -ww/2, -wh/2);
+  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();
@@ -3992,7 +4022,7 @@ static void createAppleMenu(void)
   [menuItem setTarget:about];
   [appleMenu addItem:[NSMenuItem separatorItem]];
   // Print front window
-  title = NSLocalizedString([NSString stringWithUTF8String:Fl_Mac_App_Menu::print], nil);
+  title = NSLocalizedString([NSString stringWithUTF8String:Fl_Mac_App_Menu::print_with_titlebar], nil);
   if ([title length] > 0) {
     menuItem = [appleMenu
                 addItemWithTitle:title
@@ -4000,6 +4030,16 @@ static void createAppleMenu(void)
                 keyEquivalent:@""];
     [menuItem setTarget:about];
     [menuItem setEnabled:YES];
+  // Toggle "Print Window with titlebar" / "Print Window"
+    title = NSLocalizedString([NSString stringWithUTF8String:Fl_Mac_App_Menu::toggle_print_titlebar], nil);
+    print_with_titlebar_item = [[PrintWithTitlebarItem alloc] initWithTitle:title
+                                                  action:@selector(toggleCallback)
+                                           keyEquivalent:@""];
+    [appleMenu addItem:print_with_titlebar_item];
+    [print_with_titlebar_item setTarget:print_with_titlebar_item];
+    [print_with_titlebar_item setRepresentedObject:menuItem];
+    [print_with_titlebar_item setState:NSOnState];
+    [print_with_titlebar_item setEnabled:YES];
     [appleMenu addItem:[NSMenuItem separatorItem]];
     }
   if (fl_mac_os_version >= 100400) { // services+hide+quit already in menu in OS 10.3
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'.