[Thread Prev][Thread Next]   >Date Index >Thread Index

[wmx] Hierachical command menu

Lasse Rasinen - Wed Jan 20 20:59:26 1999

I was bored, so I hacked this patch together in a couple of
hours. Basically, it makes the command menu have submenus which are
directories under .wmx or whatever. 

As a feature directory names beginning with . are stripped away, to get
rid of . and .., and if one wants to hide stuff in there (I have .wmx/.bin
for funny scripts, like Slashdot => .bin/lynx and Freshmeat => .bin/lynx
and you can guess what .bin/lynx does ;) 

Anyway, this works bit awkwardly. First of all, the cursor should warp to
where the new menu appears, but I haven't found a way to do this
conviniently. Perhaps Boolean Menu::m_submenu and then warping if this is
true. Also, putting the directories as hidden entries and files as normal
entries might look more natural. I might try those features later this
evening if I'm not too tired.

Have fun with this one. It shouldn't even crash wmx ;)

*** Menu.C.old	Wed Jan 20 19:10:00 1999
--- Menu.C	Wed Jan 20 21:02:54 1999
***************
*** 564,602 ****
  }
  
  
! CommandMenu::CommandMenu(WindowManager *manager, XEvent *e)
      : Menu(manager, e)
  {
-     m_commandDir = NULL;
      const char *home = getenv("HOME");
      const char *wmxdir = getenv("WMXDIR");
  
!     if (wmxdir == NULL)
!     {
! 	if (home == NULL) return;
!     m_commandDir =
! 	(char *)malloc(strlen(home) + strlen(CONFIG_COMMAND_MENU) + 2);
!     sprintf(m_commandDir, "%s/%s", home, CONFIG_COMMAND_MENU);
!     }
!     else
      {
! 	if(wmxdir[0] == '/')
  	{
  	    m_commandDir =
! 	      (char *)malloc(strlen(wmxdir) + 1);
! 	    strcpy(m_commandDir, wmxdir);
  	}
  	else
  	{
! 	    m_commandDir =
! 	      (char *)malloc(strlen(home) + strlen(wmxdir) + 2);
! 	    sprintf(m_commandDir, "%s/%s", home, wmxdir);
  	}
      }
  
      int i = getSelection();
  	
!     if (i >= 0 && i < m_nItems) {
  	char *commandFile =
  	    (char *)malloc(strlen(m_commandDir) + strlen(m_items[i]) + 2);
  
--- 564,611 ----
  }
  
  
! CommandMenu::CommandMenu(WindowManager *manager, XEvent *e,
! 			 char* otherdir = NULL)
      : Menu(manager, e)
  {
      const char *home = getenv("HOME");
      const char *wmxdir = getenv("WMXDIR");
  
!     m_commandDir = NULL;
!     
!     if (otherdir == NULL)
      {
! 	if (wmxdir == NULL)
  	{
+ 	    if (home == NULL) return;
  	    m_commandDir =
! 		(char *)malloc(strlen(home) + strlen(CONFIG_COMMAND_MENU) + 2);
! 	    sprintf(m_commandDir, "%s/%s", home, CONFIG_COMMAND_MENU);
  	}
  	else
  	{
! 	    if(wmxdir[0] == '/')
! 	    {
! 		m_commandDir =
! 		    (char *)malloc(strlen(wmxdir) + 1);
! 		strcpy(m_commandDir, wmxdir);
! 	    }
! 	    else
! 	    {
! 		m_commandDir =
! 		    (char *)malloc(strlen(home) + strlen(wmxdir) + 2);
! 		sprintf(m_commandDir, "%s/%s", home, wmxdir);
! 	    }
  	}
      }
+     else
+     {
+ 	m_commandDir = otherdir;
+     }
  
      int i = getSelection();
  	
!     if (i >= 0 && i < m_nHidden) {
  	char *commandFile =
  	    (char *)malloc(strlen(m_commandDir) + strlen(m_items[i]) + 2);
  
***************
*** 604,609 ****
--- 613,631 ----
  	m_windowManager->spawn(m_items[i], commandFile);
  	free(commandFile);
      }
+ 
+     if (i >= m_nHidden && i < m_nItems)
+     {
+ 	char *new_directory;
+ 	int dirlen = strlen (m_commandDir);
+ 	
+ 	new_directory = (char *)malloc (dirlen + strlen(m_items[i]) + 2);
+ 	strcpy (new_directory, m_commandDir);
+ 	new_directory[dirlen] = '/';
+ 	strcpy (new_directory + dirlen + 1, m_items[i]);
+ 
+ 	CommandMenu menu(manager, e, new_directory);
+     }
  }
  
  CommandMenu::~CommandMenu()
***************
*** 677,688 ****
  	items[count++] = NewString(ent->d_name);
      }
  
      free(dirpath);
      closedir(dir);
  
-     qsort(items, count, sizeof(char *), sortstrs);
- 
-     *niR = *nhR = count;
      return items;
  }
  
--- 699,730 ----
  	items[count++] = NewString(ent->d_name);
      }
  
+     qsort(items, count, sizeof(char *), sortstrs);
+     *nhR = count;
+ 
+     rewinddir(dir);
+ 
+     while ((ent = readdir(dir)) != NULL) {
+ 
+ 	struct stat st;
+ 	sprintf(dirpath + dirlen, "/%s", ent->d_name);
+ 		
+ 	if (stat(dirpath, &st) == -1) continue;
+ 	if (!S_ISDIR(st.st_mode) || !(st.st_mode & 0444)) continue;
+ 	if (ent->d_name[0] == '.') continue;
+ 	
+ 	items = (!items ? (char **)malloc(sizeof(char *)) :
+ 		 (char **)realloc(items, (count + 1) * sizeof(char *)));
+ 
+ 	items[count++] = NewString(ent->d_name);
+     }
+ 
+     *niR = count;
+     qsort(items + *nhR, *niR - *nhR, sizeof(char *), sortstrs);
+     
      free(dirpath);
      closedir(dir);
  
      return items;
  }
  
*** Menu.h.old	Wed Jan 20 19:10:05 1999
--- Menu.h	Wed Jan 20 19:57:03 1999
***************
*** 65,71 ****
  class CommandMenu : public Menu
  {
  public:
!     CommandMenu(WindowManager *, XEvent *e);
      virtual ~CommandMenu();
  
  private:
--- 65,71 ----
  class CommandMenu : public Menu
  {
  public:
!     CommandMenu(WindowManager *, XEvent *e, char *m_commandDir = NULL);
      virtual ~CommandMenu();
  
  private:


Next: