///
/// Add a menu item to the current menu.  When the user selects it, the
/// given functor will be called
void CommandManager::AddItem(wxString name, wxString label_in,
                             CommandFunctor *callback, int checkmark)
{
   wxString label = label_in;
   if( ItemShouldBeHidden( label ) )
      return;

   int ID = NewIdentifier(name, label, CurrentMenu(), callback, false, 0, 0);

   // Replace the accel key with the one from the preferences
   label = label.BeforeFirst(wxT('\t'));

   // This is a very weird hack.  Under GTK, menu labels are totally
   // linked to accelerators the first time you create a menu item
   // with that label and can't be changed.  This causes all sorts of
   // problems.  As a workaround, we create each menu item with a
   // made-up name (just an ID number string) but with the accelerator
   // we want, then immediately change the label to the correct string.
   // -DMM
   mHiddenID++;
   wxString dummy, newLabel;
   dummy.Printf(wxT("%s%08d"), label.c_str(), mHiddenID);
   newLabel = label;

   bool shortcut = false;

   if (mCommandIDHash[ID]->key.Length() > 0)
      shortcut = true;
   
   // Mac OS X fixes
  #ifdef __WXMAC__
   if (newLabel.Length() > 0 && newLabel[0] == wxT('&'))
      newLabel = newLabel.Right(newLabel.Length()-1);

   if (shortcut == true &&
       (mCommandIDHash[ID]->key.Length() < 5 ||
        mCommandIDHash[ID]->key.Left(5) != wxT("Ctrl+")))
      shortcut = false;
  #endif
   
   if (shortcut) {
      dummy = dummy + wxT("\t") + mCommandIDHash[ID]->key;
   }

   if (checkmark >= 0) {
      CurrentMenu()->AppendCheckItem(ID, dummy);
      CurrentMenu()->Check(ID, checkmark !=0);
   }
   else {
      CurrentMenu()->Append(ID, dummy);
   }
   CurrentMenu()->SetLabel(ID, newLabel);
   mbSeparatorAllowed = true;
}
///
/// Add a list of menu items to the current menu.  When the user selects any
/// one of these, the given functor will be called
/// with its position in the list as the index number.
/// When you call Enable on this command name, it will enable or disable
/// all of the items at once.
void CommandManager::AddItemList(wxString name, wxArrayString labels,
                                 CommandFunctor *callback,
                                 bool plugins /*= false*/)
{
   unsigned int i;

   #ifndef __WXGTK__
   plugins = false;
   #endif

   if( mHidingLevel  > 0 )
      return;

   if (CurrentMenu()->GetMenuItemCount() + labels.GetCount() < MAX_MENU_LEN)
      plugins = false;

   if (!plugins) {
      for(i=0; i<labels.GetCount(); i++) {
         int ID = NewIdentifier(name, labels[i], CurrentMenu(), callback,
                                true, i, labels.GetCount());
         CurrentMenu()->Append(ID, labels[i]);
      }
      mbSeparatorAllowed = true;
      return;
   }

   wxString label;
   unsigned int effLen = labels.GetCount();
   int listnum = 1;
   int tmpmax = MAX_SUBMENU_LEN  < effLen? MAX_SUBMENU_LEN: effLen;

   //The first submenu starts at 1.
   BeginSubMenu(wxString::Format(_("Plugins 1 to %i"), tmpmax));

   for(i=0; i<effLen; i++) {
      int ID = NewIdentifier(name, labels[i], CurrentMenu(), callback,
                             true, i, effLen);

      CurrentMenu()->Append(ID, labels[i]);
     
      if(((i+1) % MAX_SUBMENU_LEN) == 0 && i != (effLen - 1)) {
         EndSubMenu();
         listnum++;
         
         //This label the plugins by number in the submenu title (1 to 15, 15 to 30, etc.)
         tmpmax = i + MAX_SUBMENU_LEN  < effLen? 1 + i + MAX_SUBMENU_LEN: effLen;
         BeginSubMenu(wxString::Format(_("Plugins %i to %i"),i+2,tmpmax));
      }
   }
   EndSubMenu();
}
void CommandManager::AddItem(const wxChar *name,
                             const wxChar *label_in,
                             CommandFunctor *callback,
                             const wxChar *accel,
                             unsigned int flags,
                             unsigned int mask,
                             int checkmark)
{
   wxString label(label_in);
   label += wxT("\t");
   label += accel ? accel : wxEmptyString;

   if (ItemShouldBeHidden(label)) {
      delete callback;
      return;
   }

   int ID = NewIdentifier(name, label, CurrentMenu(), callback, false, 0, 0);

   if (flags != NoFlagsSpecifed || mask != NoFlagsSpecifed) {
      SetCommandFlags(name, flags, mask);
   }

   // Replace the accel key with the one from the preferences
   label = label.BeforeFirst(wxT('\t'));

   // This is a very weird hack.  Under GTK, menu labels are totally
   // linked to accelerators the first time you create a menu item
   // with that label and can't be changed.  This causes all sorts of
   // problems.  As a workaround, we create each menu item with a
   // made-up name (just an ID number string) but with the accelerator
   // we want, then immediately change the label to the correct string.
   // -DMM
   wxString newLabel;
   newLabel.Printf(wxT("%s%08d"), label.c_str(), ++mHiddenID);

   if (checkmark >= 0) {
      CurrentMenu()->AppendCheckItem(ID, newLabel);
      CurrentMenu()->Check(ID, checkmark != 0);
   }
   else {
      CurrentMenu()->Append(ID, newLabel);
   }

   CurrentMenu()->SetLabel(ID, label);
   mbSeparatorAllowed = true;
}
void CommandManager::AddSeparator()
{
   if( mHidingLevel > 0 )
      return;
   if( mbSeparatorAllowed )
      CurrentMenu()->AppendSeparator();
   mbSeparatorAllowed = false; // boolean to prevent too many separators.
}
///
/// This function is called after the final item of a SUBmenu is added.
/// Submenu items are added just like regular menu items; they just happen
/// after BeginSubMenu() is called but before EndSubMenu() is called.
void CommandManager::EndSubMenu()
{
   size_t submenu_count = mSubMenuList.GetCount()-1;

   //Save the submenu's information
   SubMenuListEntry *tmpSubMenu = mSubMenuList[submenu_count];

   //Pop off the new submenu so CurrentMenu returns the parent of the submenu
   mSubMenuList.RemoveAt(submenu_count);

   //Add the submenu to the current menu
   CurrentMenu()->Append(0, tmpSubMenu->name, tmpSubMenu->menu, tmpSubMenu->name);

   delete tmpSubMenu;
}
///
/// This function is called after the final item of a SUBmenu is added.
/// Submenu items are added just like regular menu items; they just happen
/// after BeginSubMenu() is called but before EndSubMenu() is called.
void CommandManager::EndSubMenu()
{
   if( mHidingLevel > 0 ) 
   {
      mHidingLevel--;
      return;
   }

   size_t submenu_count = mSubMenuList.GetCount()-1;

   //Save the submenu's information
   SubMenuListEntry *tmpSubMenu = mSubMenuList[submenu_count];

   //Pop off the new submenu so CurrentMenu returns the parent of the submenu
   mSubMenuList.RemoveAt(submenu_count);

   //Add the submenu to the current menu
   CurrentMenu()->Append(0, tmpSubMenu->name, tmpSubMenu->menu, tmpSubMenu->name);
   mbSeparatorAllowed = true;

   delete tmpSubMenu;
}
Exemple #7
0
///
/// Add a list of menu items to the current menu.  When the user selects any
/// one of these, the given functor will be called
/// with its position in the list as the index number.
/// When you call Enable on this command name, it will enable or disable
/// all of the items at once.
void CommandManager::AddItemList(wxString name, wxArrayString labels,
                                 CommandFunctor *callback,
                                 bool plugins /*= false*/)
{
   unsigned int i;

   #ifndef __WXGTK__
   plugins = false;
   #endif

   if( mHidingLevel  > 0 )
      return;

   unsigned int effLen = labels.GetCount();
   unsigned int nVisibleEffects=0;

   wxString label;
   int tmpmax;

   // Count the visible effects.
   for(i=0; i<effLen; i++) {
      // ItemShouldBeHidden removes the ! so do it to a temporary.
      label = labels[i];
      if (!ItemShouldBeHidden(label)) {
         nVisibleEffects++;
      }
   }

   if (CurrentMenu()->GetMenuItemCount() + nVisibleEffects < MAX_MENU_LEN)
      plugins = false;

   // j counts the visible menu items, i counts the actual menu items.
   // These numbers are the same unless we are using a simplified interface
   // by hiding effects with a ! before them when translated.
   int j=0;
   for(i=0; i<effLen; i++) {
      if (!ItemShouldBeHidden(labels[i])) {

         // ---- Start of code for Plugin sub-menus.  Only relevant on wxGTK.
         // If plugins, and at start of a sublist....
         if( plugins && ((j % MAX_SUBMENU_LEN) == 0 )) {
            // End previous sub-menu, if there was one.
            if( j>0 )
               EndSubMenu();

            // Start new sub-menu
            // tmpmax is number of last plugin for this sub-menu
            tmpmax = wxMin(j + MAX_SUBMENU_LEN, (int)nVisibleEffects);
            // Submenu titles are 1 to 15, 15 to 30, etc.
            BeginSubMenu(wxString::Format(_("Plug-ins %i to %i"),j+1,tmpmax));
         }
         // ---- End of code for Plugin sub-menus.

         j++;
         int ID = NewIdentifier(name, labels[i], CurrentMenu(), callback,
                                true, i, effLen);
         CurrentMenu()->Append(ID, labels[i]);
         mbSeparatorAllowed = true;
      }
   }
   if( plugins && (nVisibleEffects>0 ))
      EndSubMenu();
}
void CommandManager::AddSeparator()
{
   CurrentMenu()->AppendSeparator();
}