summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorManolo Gouy <Manolo>2017-12-02 16:20:30 +0000
committerManolo Gouy <Manolo>2017-12-02 16:20:30 +0000
commit34dd1b15fbbc44e70bfdb21498ce7898bf8658b3 (patch)
treefb65dd41847ff4205236066de124a1c9cb27e33c /src
parenta8fe10cf88e757f59057fdaf51637d26ab3019aa (diff)
Converted class Fl_Sys_Menu_Bar to the driver model.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12575 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt3
-rw-r--r--src/Fl_MacOS_Sys_Menu_Bar.mm (renamed from src/Fl_Sys_Menu_Bar.mm)210
-rw-r--r--src/Fl_Sys_Menu_Bar.cxx189
-rw-r--r--src/Makefile3
4 files changed, 280 insertions, 125 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ca6dc74be..4216518b8 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -63,6 +63,7 @@ set (CPPFILES
Fl_Single_Window.cxx
Fl_Slider.cxx
Fl_Spinner.cxx
+ Fl_Sys_Menu_Bar.cxx
Fl_System_Driver.cxx
Fl_Table.cxx
Fl_Table_Row.cxx
@@ -405,7 +406,7 @@ if (APPLE AND (NOT OPTION_APPLE_X11) AND (NOT OPTION_APPLE_SDL))
Fl_cocoa.mm
drivers/Cocoa/Fl_Cocoa_Printer_Driver.mm
Fl_Native_File_Chooser_MAC.mm
- Fl_Sys_Menu_Bar.mm
+ Fl_MacOS_Sys_Menu_Bar.mm
)
else()
set (MMFILES
diff --git a/src/Fl_Sys_Menu_Bar.mm b/src/Fl_MacOS_Sys_Menu_Bar.mm
index 22d7c983a..bf9af8145 100644
--- a/src/Fl_Sys_Menu_Bar.mm
+++ b/src/Fl_MacOS_Sys_Menu_Bar.mm
@@ -3,7 +3,7 @@
//
// MacOS system menu bar widget for the Fast Light Tool Kit (FLTK).
//
-// Copyright 1998-2013 by Bill Spitzak and others.
+// Copyright 1998-2017 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
@@ -31,12 +31,35 @@
*
* Many other calls of the parent class don't work.
*/
-
-#if defined(__APPLE__) || defined(FL_DOXYGEN) // PORTME: Fl_Screen_Driver - platform system menu bar
-#include <FL/Fl_Menu_Item.H>
-#include <FL/Fl_Sys_Menu_Bar.H>
+#include <FL/Fl_Sys_Menu_Bar_Driver.H>
#include <FL/x.H>
-#include <FL/Fl.H>
+
+
+#if defined(__APPLE__)
+
+class Fl_MacOS_Sys_Menu_Bar_Driver : public Fl_Sys_Menu_Bar_Driver {
+public:
+ Fl_MacOS_Sys_Menu_Bar_Driver();
+ virtual ~Fl_MacOS_Sys_Menu_Bar_Driver();
+ virtual void update();
+ virtual void draw() {}
+ virtual void about(Fl_Callback *cb, void *data);
+ virtual int add(const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags);
+ virtual int add(const char* str);
+ virtual int insert(int index, const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags);
+ virtual void menu(const Fl_Menu_Item *m);
+ virtual void shortcut (int i, int s);
+ virtual void setonly (Fl_Menu_Item *item);
+ virtual void clear();
+ virtual int clear_submenu(int index);
+ virtual void remove(int index);
+ virtual void replace(int index, const char *name);
+ virtual void mode(int i, int fl);
+};
+
+// this runs once if this source file is linked in, and initializes the
+// static variable Fl_Sys_Menu_Bar_Driver::driver_ with an object of class Fl_MacOS_Sys_Menu_Bar_Driver
+static int unused = (Fl_Sys_Menu_Bar_Driver::driver_ = new Fl_MacOS_Sys_Menu_Bar_Driver(), 0);
#import <Cocoa/Cocoa.h>
@@ -47,7 +70,6 @@
typedef const Fl_Menu_Item *pFl_Menu_Item;
-Fl_Sys_Menu_Bar *fl_sys_menu_bar = 0;
static Fl_Menu_Bar *custom_menu;
static char *remove_ampersand(const char *s);
@@ -201,22 +223,19 @@ const char *Fl_Mac_App_Menu::quit = "Quit %@";
@end
-void fl_mac_set_about( Fl_Callback *cb, void *user_data, int shortcut)
+void Fl_MacOS_Sys_Menu_Bar_Driver::about( Fl_Callback *cb, void *user_data)
{
fl_open_display();
Fl_Menu_Item aboutItem;
memset(&aboutItem, 0, sizeof(Fl_Menu_Item));
aboutItem.callback(cb);
aboutItem.user_data(user_data);
- aboutItem.shortcut(shortcut);
NSMenu *appleMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu];
CFStringRef cfname = CFStringCreateCopy(NULL, (CFStringRef)[[appleMenu itemAtIndex:0] title]);
[appleMenu removeItemAtIndex:0];
FLMenuItem *item = [[[FLMenuItem alloc] initWithTitle:(NSString*)cfname
action:@selector(directCallback)
keyEquivalent:@""] autorelease];
- if (aboutItem.shortcut())
- [item setFltkShortcut:aboutItem.shortcut()];
NSData *pointer = [NSData dataWithBytes:&aboutItem length:sizeof(Fl_Menu_Item)];
[item setRepresentedObject:pointer];
[appleMenu insertItem:item atIndex:0];
@@ -347,159 +366,104 @@ static void convertToMenuBar(const Fl_Menu_Item *mm)
if (mm) createSubMenu(fl_system_menu, mm, NULL, @selector(doCallback));
}
-
-/**
- * @brief create a system menu bar using the given list of menu structs
- *
- * \author Matthias Melcher
- *
- * @param m list of Fl_Menu_Item
- */
-void Fl_Sys_Menu_Bar::menu(const Fl_Menu_Item *m)
+void Fl_MacOS_Sys_Menu_Bar_Driver::update()
{
- fl_open_display();
- Fl_Menu_Bar::menu( m );
- convertToMenuBar(m);
+ convertToMenuBar(bar->Fl_Menu_::menu());
}
-/**
- * @brief Add a new menu item to the system menu bar.
- *
- * Add to the system menu bar a new menu item, with a title string, shortcut int,
- * callback, argument to the callback, and flags.
- *
- * @param label - new menu item's label
- * @param shortcut - new menu item's integer shortcut (can be 0 for none, or e.g. FL_ALT+'x')
- * @param cb - callback to be invoked when item selected (can be 0 for none, in which case the menubar's callback() can be used instead)
- * @param user_data - argument to the callback
- * @param flags - item's flags, e.g. ::FL_MENU_TOGGLE, etc.
- *
- * \returns the index into the menu() array, where the entry was added
- *
- * @see Fl_Menu_::add(const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
- */
-int Fl_Sys_Menu_Bar::add(const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
+static int process_sys_menu_shortcuts(int event)
{
- fl_open_display();
- int index = Fl_Menu_::add(label, shortcut, cb, user_data, flags);
- update();
- return index;
+ if (event != FL_SHORTCUT || !fl_sys_menu_bar || Fl::modal()) return 0;
+ // is the last event the shortcut of an item of the fl_sys_menu_bar menu ?
+ const Fl_Menu_Item *item = fl_sys_menu_bar->menu()->test_shortcut();
+ if (!item) return 0;
+ if (item->visible()) // have the system menu process the shortcut, highlighting the corresponding menu
+ [[NSApp mainMenu] performKeyEquivalent:[NSApp currentEvent]];
+ else // have FLTK process the shortcut associated to an invisible Fl_Menu_Item
+ fl_sys_menu_bar->picked(item);
+ return 1;
}
-/**
- * Forms-compatible procedure to add items to the system menu bar
- *
- * \returns the index into the menu() array, where the entry was added
- * @see Fl_Menu_::add(const char* str)
- */
-int Fl_Sys_Menu_Bar::add(const char* str)
+Fl_MacOS_Sys_Menu_Bar_Driver::Fl_MacOS_Sys_Menu_Bar_Driver() : Fl_Sys_Menu_Bar_Driver()
{
- fl_open_display();
- int index = Fl_Menu_::add(str);
- update();
- return index;
+ Fl::add_handler(process_sys_menu_shortcuts);
}
-/**
- * @brief insert in the system menu bar a new menu item
- *
- * Insert in the system menu bar a new menu item, with a title string, shortcut int,
- * callback, argument to the callback, and flags.
- *
- * \returns the index into the menu() array, where the entry was inserted
- * @see Fl_Menu_::insert(int index, const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
- */
-int Fl_Sys_Menu_Bar::insert(int index, const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
+Fl_MacOS_Sys_Menu_Bar_Driver::~Fl_MacOS_Sys_Menu_Bar_Driver()
+{
+ Fl::remove_handler(process_sys_menu_shortcuts);
+}
+
+void Fl_MacOS_Sys_Menu_Bar_Driver::menu(const Fl_Menu_Item *m)
{
fl_open_display();
- int menu_index = Fl_Menu_::insert(index, label, shortcut, cb, user_data, flags);
- update();
- return menu_index;
+ bar->Fl_Menu_Bar::menu( m );
+ convertToMenuBar(m);
}
-void Fl_Sys_Menu_Bar::clear()
+void Fl_MacOS_Sys_Menu_Bar_Driver::clear()
{
- Fl_Menu_::clear();
+ bar->Fl_Menu_::clear();
convertToMenuBar(NULL);
}
-int Fl_Sys_Menu_Bar::clear_submenu(int index)
+int Fl_MacOS_Sys_Menu_Bar_Driver::clear_submenu(int index)
{
- int retval = Fl_Menu_::clear_submenu(index);
+ int retval = bar->Fl_Menu_::clear_submenu(index);
if (retval != -1) update();
return retval;
}
-/**
- * @brief remove an item from the system menu bar
- *
- * @param index the index of the item to remove
- */
-void Fl_Sys_Menu_Bar::remove(int index)
+void Fl_MacOS_Sys_Menu_Bar_Driver::remove(int index)
{
- Fl_Menu_::remove(index);
+ bar->Fl_Menu_::remove(index);
update();
}
-
-/**
- * @brief rename an item from the system menu bar
- *
- * @param index the index of the item to rename
- * @param name the new item name as a UTF8 string
- */
-void Fl_Sys_Menu_Bar::replace(int index, const char *name)
+void Fl_MacOS_Sys_Menu_Bar_Driver::replace(int index, const char *name)
{
- Fl_Menu_::replace(index, name); // index
+ bar->Fl_Menu_::replace(index, name);
update();
}
-void Fl_Sys_Menu_Bar::update()
-{
- convertToMenuBar(Fl_Menu_::menu());
+void Fl_MacOS_Sys_Menu_Bar_Driver::mode(int i, int fl) {
+ bar->Fl_Menu_::mode(i, fl);
+ update();
}
-/*
- * Draw the menu bar.
- * Nothing here because the OS does this for us.
- */
-void Fl_Sys_Menu_Bar::draw() {
+void Fl_MacOS_Sys_Menu_Bar_Driver::shortcut (int i, int s) {
+ bar->Fl_Menu_Bar::shortcut(i, s);
+ update();
}
-static int process_sys_menu_shortcuts(int event)
-{
- if (event != FL_SHORTCUT || !fl_sys_menu_bar || Fl::modal()) return 0;
- // is the last event the shortcut of an item of the fl_sys_menu_bar menu ?
- const Fl_Menu_Item *item = fl_sys_menu_bar->menu()->test_shortcut();
- if (!item) return 0;
- if (item->visible()) // have the system menu process the shortcut, highlighting the corresponding menu
- [[NSApp mainMenu] performKeyEquivalent:[NSApp currentEvent]];
- else // have FLTK process the shortcut associated to an invisible Fl_Menu_Item
- fl_sys_menu_bar->picked(item);
- return 1;
+void Fl_MacOS_Sys_Menu_Bar_Driver::setonly (Fl_Menu_Item *item) {
+ bar->Fl_Menu_::setonly(item);
+ update();
}
+int Fl_MacOS_Sys_Menu_Bar_Driver::add(const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
+{
+ fl_open_display();
+ int index = bar->Fl_Menu_::add(label, shortcut, cb, user_data, flags);
+ update();
+ return index;
+}
-/**
- The constructor.
- On Mac OS X, all arguments are unused. On other platforms they are used as by Fl_Menu_Bar::Fl_Menu_Bar().
- */
-Fl_Sys_Menu_Bar::Fl_Sys_Menu_Bar(int x,int y,int w,int h,const char *l)
-: Fl_Menu_Bar(x,y,w,h,l)
+int Fl_MacOS_Sys_Menu_Bar_Driver::add(const char* str)
{
- deactivate(); // don't let the old area take events
- if (fl_sys_menu_bar) delete fl_sys_menu_bar;
- fl_sys_menu_bar = this;
- Fl::add_handler(process_sys_menu_shortcuts);
+ fl_open_display();
+ int index = bar->Fl_Menu_::add(str);
+ update();
+ return index;
}
-/** The destructor */
-Fl_Sys_Menu_Bar::~Fl_Sys_Menu_Bar()
+int Fl_MacOS_Sys_Menu_Bar_Driver::insert(int index, const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
{
- fl_sys_menu_bar = 0;
- clear();
- Fl::remove_handler(process_sys_menu_shortcuts);
+ fl_open_display();
+ int menu_index = bar->Fl_Menu_::insert(index, label, shortcut, cb, user_data, flags);
+ update();
+ return menu_index;
}
/** \class Fl_Mac_App_Menu
@@ -542,7 +506,7 @@ void Fl_Mac_App_Menu::custom_application_menu_items(const Fl_Menu_Item *m)
[item release];
}
}
-#endif /* __APPLE__ */ // PORTME: Fl_Screen_Driver - platform system menu bar
+#endif /* __APPLE__ */
//
// End of "$Id$".
diff --git a/src/Fl_Sys_Menu_Bar.cxx b/src/Fl_Sys_Menu_Bar.cxx
new file mode 100644
index 000000000..08c7186a7
--- /dev/null
+++ b/src/Fl_Sys_Menu_Bar.cxx
@@ -0,0 +1,189 @@
+//
+// "$Id$"
+//
+// system menu bar widget for the Fast Light Tool Kit (FLTK).
+//
+// Copyright 1998-2017 by Bill Spitzak and others.
+//
+// This library is free software. Distribution and use rights are outlined in
+// the file "COPYING" which should have been included with this file. If this
+// file is missing or damaged, see the license at:
+//
+// http://www.fltk.org/COPYING.php
+//
+// Please report all bugs and problems on the following page:
+//
+// http://www.fltk.org/str.php
+//
+
+
+#include <FL/Fl_Sys_Menu_Bar_Driver.H>
+#include <FL/x.H>
+
+
+Fl_Sys_Menu_Bar *fl_sys_menu_bar = 0;
+
+// initialize this static variable if it was not initialized previously
+Fl_Sys_Menu_Bar_Driver *Fl_Sys_Menu_Bar_Driver::driver_ =
+ ( driver_ ? driver_ : new Fl_Sys_Menu_Bar_Driver() );
+
+Fl_Sys_Menu_Bar_Driver *Fl_Sys_Menu_Bar::driver() {return Fl_Sys_Menu_Bar_Driver::driver_;}
+
+
+/**
+ The constructor.
+ On Mac OS X, all arguments are unused. On other platforms they are used as by Fl_Menu_Bar::Fl_Menu_Bar().
+ */
+Fl_Sys_Menu_Bar::Fl_Sys_Menu_Bar(int x,int y,int w,int h,const char *l)
+: Fl_Menu_Bar(x,y,w,h,l)
+{
+ if (fl_sys_menu_bar) delete fl_sys_menu_bar;
+ fl_sys_menu_bar = this;
+ Fl_Sys_Menu_Bar_Driver::driver_->bar = this;
+}
+
+/** The destructor */
+Fl_Sys_Menu_Bar::~Fl_Sys_Menu_Bar()
+{
+ fl_sys_menu_bar = 0;
+ clear();
+}
+
+Fl_Sys_Menu_Bar_Driver::Fl_Sys_Menu_Bar_Driver() {bar = NULL;}
+
+Fl_Sys_Menu_Bar_Driver::~Fl_Sys_Menu_Bar_Driver() {}
+
+void Fl_Sys_Menu_Bar::update() {
+ driver()->update();
+}
+
+/**
+ * @brief create a system menu bar using the given list of menu structs
+ *
+ * \author Matthias Melcher
+ *
+ * @param m Zero-ending list of Fl_Menu_Item's
+ */
+void Fl_Sys_Menu_Bar::menu(const Fl_Menu_Item *m)
+{
+ driver()->menu(m);
+}
+
+/** Changes the shortcut of item i to n.
+ */
+void Fl_Sys_Menu_Bar::shortcut (int i, int s) {
+ driver()->shortcut(i, s);
+}
+
+/** Turns the radio item "on" for the menu item and turns "off" adjacent radio items of the same group.*/
+void Fl_Sys_Menu_Bar::setonly (Fl_Menu_Item *item) {
+ driver()->setonly(item);
+}
+
+/** Sets the flags of item i
+ \see Fl_Menu_::mode(int i, int fl) */
+void Fl_Sys_Menu_Bar::mode (int i, int fl) {
+ driver()->mode(i, fl);
+}
+
+/**
+ * @brief Add a new menu item to the system menu bar.
+ *
+ * Add to the system menu bar a new menu item, with a title string, shortcut int,
+ * callback, argument to the callback, and flags.
+ *
+ * @param label - new menu item's label
+ * @param shortcut - new menu item's integer shortcut (can be 0 for none, or e.g. FL_ALT+'x')
+ * @param cb - callback to be invoked when item selected (can be 0 for none, in which case the menubar's callback() can be used instead)
+ * @param user_data - argument to the callback
+ * @param flags - item's flags, e.g. ::FL_MENU_TOGGLE, etc.
+ *
+ * \returns the index into the menu() array, where the entry was added
+ *
+ * @see Fl_Menu_::add(const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
+ */
+int Fl_Sys_Menu_Bar::add(const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
+{
+ return driver()->add(label, shortcut, cb, user_data, flags);
+}
+
+/**
+ * Forms-compatible procedure to add items to the system menu bar
+ *
+ * \returns the index into the menu() array, where the entry was added
+ * @see Fl_Menu_::add(const char* str)
+ */
+int Fl_Sys_Menu_Bar::add(const char* str)
+{
+ return driver()->add(str);
+}
+
+/**
+ * @brief insert in the system menu bar a new menu item
+ *
+ * Insert in the system menu bar a new menu item, with a title string, shortcut int,
+ * callback, argument to the callback, and flags.
+ *
+ * \returns the index into the menu() array, where the entry was inserted
+ * @see Fl_Menu_::insert(int index, const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
+ */
+int Fl_Sys_Menu_Bar::insert(int index, const char* label, int shortcut, Fl_Callback *cb, void *user_data, int flags)
+{
+ return driver()->insert(index, label, shortcut, cb, user_data, flags);
+}
+
+/** Set the Fl_Menu_Item array pointer to null, indicating a zero-length menu.
+ \see Fl_Menu_::clear()
+ */
+void Fl_Sys_Menu_Bar::clear()
+{
+ driver()->clear();
+}
+
+/** Clears the specified submenu pointed to by index of all menu items.
+ \see Fl_Menu_::clear_submenu(int index)
+ */
+int Fl_Sys_Menu_Bar::clear_submenu(int index)
+{
+ return driver()->clear_submenu(index);
+}
+
+/**
+ * @brief remove an item from the system menu bar
+ *
+ * @param index the index of the item to remove
+ */
+void Fl_Sys_Menu_Bar::remove(int index)
+{
+ driver()->remove(index);
+}
+
+/**
+ * @brief rename an item from the system menu bar
+ *
+ * @param index the index of the item to rename
+ * @param name the new item name as a UTF8 string
+ */
+void Fl_Sys_Menu_Bar::replace(int index, const char *name)
+{
+ driver()->replace(index, name);
+}
+
+/**
+ * Attaches a callback to the "About myprog" item of the system application menu.
+ * This cross-platform function is effective only under the MacOS platform.
+ * \param cb a callback that will be called by "About myprog" menu item
+ * with NULL 1st argument.
+ * \param user_data a pointer transmitted as 2nd argument to the callback.
+ */
+void Fl_Sys_Menu_Bar::about(Fl_Callback *cb, void *data) {
+ if (fl_sys_menu_bar) fl_sys_menu_bar->driver()->about(cb, data);
+}
+
+void Fl_Sys_Menu_Bar::draw() {
+ driver()->draw();
+}
+
+//
+// End of "$Id$".
+//
diff --git a/src/Makefile b/src/Makefile
index b943f813c..9364320a7 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -82,6 +82,7 @@ CPPFILES = \
Fl_Single_Window.cxx \
Fl_Slider.cxx \
Fl_Spinner.cxx \
+ Fl_Sys_Menu_Bar.cxx \
Fl_System_Driver.cxx \
Fl_Table.cxx \
Fl_Table_Row.cxx \
@@ -167,7 +168,7 @@ OBJCPPFILES = \
Fl_cocoa.mm \
drivers/Cocoa/Fl_Cocoa_Printer_Driver.mm \
Fl_Native_File_Chooser_MAC.mm \
- Fl_Sys_Menu_Bar.mm
+ Fl_MacOS_Sys_Menu_Bar.mm
FLCPPFILES = \
forms_compatibility.cxx \