DO NOT REPLY TO THIS MESSAGE. INSTEAD, POST ANY RESPONSES TO THE LINK BELOW.
[STR New]
Link: http://www.fltk.org/str.php?L1758
Version: 1.3-feature
Link: http://www.fltk.org/str.php?L1758
Version: 1.3-feature
Index: src/Fl_Menu_.cxx
===================================================================
--- src/Fl_Menu_.cxx (revision 7469)
+++ src/Fl_Menu_.cxx (working copy)
@@ -128,17 +128,33 @@
\see Fl_Menu_::find_item(Fl_Callback*), item_pathname()
*/
const Fl_Menu_Item * Fl_Menu_::find_item(const char *name) {
+ int i = find_index(name);
+ return( (i==-1) ? 0 : (const Fl_Menu_Item*)(menu_+i));
+}
+
+int Fl_Menu_::find_index(Fl_Menu_Item *item) const {
+ Fl_Menu_Item *max = menu_+size();
+ if (item<menu_ || item>max) return(-1);
+ return(item-menu_);
+}
+
+int Fl_Menu_::find_index(Fl_Callback *cb) const {
+ for ( int t=0; t < size(); t++ )
+ if (menu_[t].callback_==cb)
+ return(t);
+ return(-1);
+}
+
+int Fl_Menu_::find_index(const char *name) const {
char menupath[1024] = ""; // File/Export
-
for ( int t=0; t < size(); t++ ) {
Fl_Menu_Item *m = menu_ + t;
-
if (m->flags&FL_SUBMENU) {
// IT'S A SUBMENU
// we do not support searches through FL_SUBMENU_POINTER links
if (menupath[0]) strlcat(menupath, "/", sizeof(menupath));
strlcat(menupath, m->label(), sizeof(menupath));
- if (!strcmp(menupath, name)) return m;
+ if (!strcmp(menupath, name)) return(t);
} else {
if (!m->label()) {
// END OF SUBMENU? Pop back one level.
@@ -147,17 +163,15 @@
else menupath[0] = '\0';
continue;
}
-
// IT'S A MENU ITEM
char itempath[1024]; // eg. Edit/Copy
strcpy(itempath, menupath);
if (itempath[0]) strlcat(itempath, "/", sizeof(itempath));
strlcat(itempath, m->label(), sizeof(itempath));
- if (!strcmp(itempath, name)) return m;
+ if (!strcmp(itempath, name)) return(t);
}
}
-
- return (const Fl_Menu_Item *)0;
+ return(-1);
}
/**
@@ -330,6 +344,16 @@
}
}
+int Fl_Menu_::clear_submenu(int index) {
+ if ( ! (menu_[index].flags & FL_SUBMENU) ) return(-1);
+ ++index; // advance to first item
+ while ( index < size() ) {
+ if ( menu_[index].text == 0 ) break; // end of this submenu? done
+ remove(index); // remove items/submenus
+ }
+ return(0);
+}
+
//
// End of "$Id$".
//
Index: src/Fl_Menu_add.cxx
===================================================================
--- src/Fl_Menu_add.cxx (revision 7469)
+++ src/Fl_Menu_add.cxx (working copy)
@@ -53,11 +53,12 @@
// Insert a single Fl_Menu_Item into an array of size at offset n,
// if this is local_array it will be reallocated if needed.
-static Fl_Menu_Item* insert(
- Fl_Menu_Item* array, int size,
- int n,
- const char *text,
- int flags
+static Fl_Menu_Item* array_insert(
+ Fl_Menu_Item* array, // array to modify
+ int size, // size of array
+ int n, // index of new insert position
+ const char *text, // text of new item (copy is made)
+ int flags // flags for new item
) {
if (array == local_array && size >= local_array_alloc) {
local_array_alloc = 2*size;
@@ -107,6 +108,17 @@
void *data,
int myflags
) {
+ return(insert(-1,mytext,sc,cb,data,myflags)); // -1: append
+}
+
+int Fl_Menu_Item::insert(
+ int index,
+ const char *mytext,
+ int sc,
+ Fl_Callback *cb,
+ void *data,
+ int myflags
+) {
Fl_Menu_Item *array = this;
Fl_Menu_Item *m = this;
const char *p;
@@ -133,17 +145,18 @@
item = buf;
if (*p != '/') break; /* not a menu title */
- mytext = p+1; /* point at item title */
+ index = -1; /* any submenu specified overrides insert position */
+ mytext = p+1; /* point at item title */
/* find a matching menu title: */
for (; m->text; m = m->next())
if (m->flags&FL_SUBMENU && !compare(item, m->text)) break;
if (!m->text) { /* create a new menu */
- int n = m-array;
- array = insert(array, msize, n, item, FL_SUBMENU|flags1);
+ int n = (index==-1) ? m-array : index;
+ array = array_insert(array, msize, n, item, FL_SUBMENU|flags1);
msize++;
- array = insert(array, msize, n+1, 0, 0);
+ array = array_insert(array, msize, n+1, 0, 0);
msize++;
m = array+n;
}
@@ -156,11 +169,11 @@
if (!(m->flags&FL_SUBMENU) && !compare(m->text,item)) break;
if (!m->text) { /* add a new menu item */
- int n = m-array;
- array = insert(array, msize, n, item, myflags|flags1);
+ int n = (index==-1) ? m-array : index;
+ array = array_insert(array, msize, n, item, myflags|flags1);
msize++;
if (myflags & FL_SUBMENU) { // add submenu delimiter
- array = insert(array, msize, n+1, 0, 0);
+ array = array_insert(array, msize, n+1, 0, 0);
msize++;
}
m = array+n;
@@ -266,6 +279,17 @@
*/
int Fl_Menu_::add(const char *label,int shortcut,Fl_Callback *callback,void
*userdata,int flags) {
+ return(insert(-1,label,shortcut,callback,userdata,flags)); // -1: append
+}
+
+int Fl_Menu_::insert(
+ int index,
+ const char *label,
+ int shortcut,
+ Fl_Callback *callback,
+ void *userdata,
+ int flags
+) {
// make this widget own the local array:
if (this != fl_menu_array_owner) {
if (fl_menu_array_owner) {
@@ -299,7 +323,7 @@
}
fl_menu_array_owner = this;
}
- int r = menu_->add(label,shortcut,callback,userdata,flags);
+ int r = menu_->insert(index,label,shortcut,callback,userdata,flags);
// if it rellocated array we must fix the pointer:
int value_offset = value_-menu_;
menu_ = local_array; // in case it reallocated it
Index: FL/Fl_Menu_Item.H
===================================================================
--- FL/Fl_Menu_Item.H (revision 7469)
+++ FL/Fl_Menu_Item.H (working copy)
@@ -386,6 +386,7 @@
/** back compatibility only \deprecated. */
void uncheck() {flags &= ~FL_MENU_VALUE;}
+ int insert(int,const char*,int,Fl_Callback*,void* =0, int =0);
int add(const char*, int shortcut, Fl_Callback*, void* =0, int = 0);
/** See int add(const char*, int shortcut, Fl_Callback*, void*, int) */
Index: FL/Fl_Menu_.H
===================================================================
--- FL/Fl_Menu_.H (revision 7469)
+++ FL/Fl_Menu_.H (working copy)
@@ -66,6 +66,9 @@
const Fl_Menu_Item* picked(const Fl_Menu_Item*);
const Fl_Menu_Item* find_item(const char *name);
const Fl_Menu_Item* find_item(Fl_Callback*);
+ int find_index(const char *name) const;
+ int find_index(Fl_Menu_Item *item) const;
+ int find_index(Fl_Callback *cb) const;
const Fl_Menu_Item* test_shortcut() {return picked(menu()->test_shortcut());}
void global();
@@ -77,6 +80,7 @@
const Fl_Menu_Item *menu() const {return menu_;}
void menu(const Fl_Menu_Item *m);
void copy(const Fl_Menu_Item *m, void* user_data = 0);
+ int insert(int index, const char*, int shortcut, Fl_Callback*, void* = 0,
int = 0);
int add(const char*, int shortcut, Fl_Callback*, void* = 0, int = 0);
/** See int Fl_Menu_::add(const char* label, int shortcut, Fl_Callback*,
void *user_data=0, int flags=0)*/
int add(const char* a, const char* b, Fl_Callback* c, void* d = 0, int e =
0) {
@@ -85,6 +89,7 @@
int size() const ;
void size(int W, int H) { Fl_Widget::size(W, H); }
void clear();
+ int clear_submenu(int index);
void replace(int,const char *);
void remove(int);
/** Changes the shortcut of item i to n. */
_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev