flu_file_chooser.cpp

来自「ncbi源码」· C++ 代码 · 共 2,471 行 · 第 1/5 页

CPP
2,471
字号
    fileListBtn = new Flu_Button( 511, 33, 25, 25 );    fileListBtn->type( FL_RADIO_BUTTON );    fileListBtn->value(1);    fileListBtn->callback( reloadCB, this );    fileListBtn->image( file_list_img );    fileListBtn->tooltip( "List mode" );    fileListWideBtn = new Flu_Button( 540, 33, 25, 25 );    fileListWideBtn->type( FL_RADIO_BUTTON );    fileListWideBtn->callback( reloadCB, this );    fileListWideBtn->image( file_listwide_img );    fileListWideBtn->tooltip( "Wide List mode" );    fileDetailsBtn = new Flu_Button( 569, 33, 25, 25 );    fileDetailsBtn->type( FL_RADIO_BUTTON );    fileDetailsBtn->image( fileDetails );    fileDetailsBtn->callback( reloadCB, this );    fileDetailsBtn->tooltip( "Detail mode" );    g->end();  }  hiddenFiles = new Fl_Check_Button( 110, 33, 130, 25, "Show Hidden Files" );  hiddenFiles->callback( reloadCB, this );#ifdef WIN32  hiddenFiles->hide();#endif  g->resizable( hiddenFiles );  g->end();  dummy->resizable( filesystems );  dummy->end();  ////////////////////////////////////////////////////////////////  previewTile = new PreviewTile( 110, 60, w()-110-5, h()-80-40-5, this );  fileGroup = new Fl_Group( 110, 60, w()-120-5, h()-80-40-5 );  {    fileGroup->box( FL_DOWN_FRAME );    filelist = new FileList( fileGroup->x()+2, fileGroup->y()+2, fileGroup->w()-4, fileGroup->h()-4, this );    filelist->box( FL_FLAT_BOX );    filelist->color( FL_WHITE );    filelist->type( FL_HORIZONTAL );    filelist->spacing( 4, 1 );    filelist->scrollbar.linesize( DEFAULT_ENTRY_WIDTH+4 );    filelist->end();    fileDetailsGroup = new Fl_Group( fileGroup->x()+2, fileGroup->y()+2, fileGroup->w()-4, fileGroup->h()-4 );    filecolumns = new FileColumns( fileGroup->x()+2, fileGroup->y()+2, fileGroup->w()-4, 20, this );    //filecolumns->hide();    filescroll = new Fl_Scroll( fileGroup->x()+2, fileGroup->y()+22, fileGroup->w()-4, fileGroup->h()-20-4 );    filescroll->color( FL_WHITE );    filescroll->scrollbar.linesize( 20 );    filescroll->box( FL_FLAT_BOX );    filescroll->type( Fl_Scroll::VERTICAL );    {      filedetails = new FileDetails( fileGroup->x()+2, fileGroup->y()+22, fileGroup->w()-4, fileGroup->h()-20-4, this );      filedetails->end();    }    filescroll->end();    //filescroll->hide();    fileDetailsGroup->end();    fileDetailsGroup->resizable( filescroll );    fileGroup->resizable( filelist );  }  fileGroup->end();  previewGroup = new PreviewGroup( fileGroup->x()+fileGroup->w(), fileGroup->y(), previewTile->w()-fileGroup->w(), fileGroup->h(), this );  previewGroup->end();  {    Fl_Box *b = new Fl_Box( previewTile->x()+250, previewTile->y(), previewTile->w()-350, previewTile->h() );    previewTile->add_resizable( *b );  }  previewTile->end();  previewTile->position( previewGroup->x(), previewGroup->y(), previewTile->x()+previewTile->w(), previewGroup->y() );  previewTile->last = previewTile->x()+previewTile->w()-200;  resizable( previewTile );  filePattern = new Flu_Combo_List( 70, h()-30, w()-70-85-10, 25, "File types:" );  filePattern->editable( false );  filePattern->callback( reloadCB, this );  filePattern->pop_height( 200 );  ok.callback( _okCB, this );  cancel.callback( _cancelCB, this );  {    g = new Fl_Group( 0, h()-60, w(), 30 );    g->end();    g->add( filename );    g->add( ok );    g->resizable( filename );    g = new Fl_Group( 0, h()-30, w(), 30 );    g->end();    g->add( filePattern );    g->add( cancel );    g->resizable( filePattern );  }  end();  FL_NORMAL_SIZE = oldNormalSize;  char buf[1024];  // determine the user's home area for loading/storing favorites and transient config info#ifdef WIN32  {    LPTSTR lpszSystemInfo;          // pointer to system information string     DWORD cchBuff = 256;            // size of user name    TCHAR tchBuffer[UNLEN + 1];     // buffer for expanded string    lpszSystemInfo = tchBuffer;    // Get the user name.    GetUserName(lpszSystemInfo, &cchBuff);    userHome = "C:/Documents and Settings/";    userHome += lpszSystemInfo;    userHome += "/";  }#else  fl_filename_expand( buf, 1024, "~/" );  userHome = buf;#endif  configFilename = userHome + ".Flu_File_Chooser.favorites";  // try to load the favorites  {    FILE *f = fopen( configFilename.c_str(), "r" );    //printf( "load: %s %X\n", buf, f );    if( f )      {	buf[0] = '\0';	while( !feof(f) )	  {	    fgets( buf, 1024, f );	    char *newline = strrchr( buf, '\n' );	    if( newline )	      *newline = '\0';	    if( strlen( buf ) > 0 )	      {		// eliminate duplicates		bool duplicate = false;		for( int i = 1; i <= favoritesList->size(); i++ )		  {		    if( streq( buf, favoritesList->text(i) ) )		      {			duplicate = true;			break;		      }		  }		if( !duplicate )		  favoritesList->add( buf );	      }	  }	fclose( f );      }  }  if( !imgTxtPreview )    {      imgTxtPreview = new ImgTxtPreview();      add_preview_handler( imgTxtPreview );    }  pattern( pat );  default_file_icon( &default_file );  cd( NULL ); // prime with the current directory  clear_history();  cd( pathname );}Flu_File_Chooser :: ~Flu_File_Chooser(){  Fl::remove_timeout( Entry::_editCB );  filelist->clear();  filedetails->clear();  clear_history();}void Flu_File_Chooser :: cancelCB(){   filename.value("");  filename.position( filename.size(), filename.size() );  unselect_all();  hide();}void Flu_File_Chooser :: pattern( const char *p ){  // just like in Fl_File_Chooser, we accept tab, |, and ; delimited strings like this:  // "Description (patterns)" or just "patterns" where patterns is   // of the form *.xxx or *.{xxx,yyy,zzz}}  rawPattern = p;  // clear out the old  filePattern->list.clear();  filePattern->input.value( "" );  patterns.clear();  if( p == 0 )    p = "*";  else if( p[0] == '\0' )    p = "*";  FluSimpleString pat = p, pattern;  bool addedAll = false;  const char *next = strtok( (char*)pat.c_str(), "\t|;" );  const char *start;  while( next )    {      //printf( "next pattern = %s\n", next );      if( next[0] == '\0' )	break;      // eat whitespace      while( isspace( *next ) )	next++;      // degenerate check      if( strcmp( next, "*" ) == 0 )	{	  addedAll = true;	  filePattern->list.add( "All Files (*)" );	  patterns.add( "*" );	  next = strtok( NULL, "\t|;" );	  continue;	}      // extract the patterns from the substring      if( next[0] != '*' ) // starts with description	{	  // the pattern starts after the first '('	  start = strchr( next, '(' );	  if( !start ) // error: couldn't find the '('	    {	      next = strtok( NULL, "\t|;" );	      continue;	    }	  start++; // skip the '('	}      else	start = next;      if( start[0] != '*' )	{	  next = strtok( NULL, "\t|;" );	  continue;	}      start++; // skip the '*'      if( start[0] != '.' )	{	  next = strtok( NULL, "\t|;" );	  continue;	}      start++; // skip the '.'      if( start[0] == '{' )	{	  // the pattern is between '{' and '}'	  pattern = start+1;	}      else	pattern = start;      // remove the last '}'      int brace = pattern.find( '}' );      if( brace != -1 )	pattern[brace] = '\0';      // remove the last ')'      int paren = pattern.find( ')' );      if( paren != -1 )	pattern[paren] = '\0';      //printf( "patterns: %s\n", pattern.c_str() );      if( pattern.size() )	{	  // add the whole string to the list	  filePattern->list.add( next );	  patterns.add( pattern );	}      // advance to the pattern token      next = strtok( NULL, "\t|;" );   }  // add all files  if( !addedAll )    {      filePattern->list.add( "All Files (*)" );      patterns.add( "*" );    }  // choose the first added item  filePattern->value( filePattern->list.text(1) );}int Flu_File_Chooser :: handle( int event ){  if( Fl_Double_Window::handle( event ) )    return 1;  else if( event == FL_KEYDOWN && Fl::event_key(FL_Escape) )    {      cancel.do_callback();      return 1;    }  else if( event == FL_KEYDOWN && Fl::event_key('a') && Fl::event_state(FL_CTRL) )    {      select_all();      return 1;    }  else    return 0;}void Flu_File_Chooser :: newFolderCB(){  // start with the name "New Folder". while the name exists, keep appending a number (1..2..etc)  FluSimpleString newName = "New Folder", path = currentDir + newName;  int count = 1;  int i;  for(;;)    {      bool found = false;      // see if any entry already has that name      Fl_Group *g = getEntryGroup();      for( i = 0; i < g->children(); i++ )	{	  if( ((Entry*)g->child(i))->filename == newName )	    {	      found = true;	      break;	    }	}      // since an entry already exists, change the name and try again      if( found )	{	  char buf[16];	  sprintf( buf, "%d", count++ );	  newName = "New Folder" + FluSimpleString(buf);	  path = currentDir + newName;	}      else	break;    }  // try to create the folder#if ( defined WIN32 || defined MINGW ) && !defined CYGWIN  if( mkdir( path.c_str() ) != 0 )#else  if( mkdir( path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) != 0 )#endif    {      fl_alert( "Could not create directory '%s'. You may not have permission to perform this operation.",		newName.c_str() );      return;    }    // create a new entry with the name of the new folder. add to either the list or the details  Entry *entry = new Entry( newName.c_str(), ENTRY_DIR, fileDetailsBtn->value(), this );  if( !fileDetailsBtn->value() )    filelist->add( *entry );  else    filedetails->add( *entry );  // switch that entry to input mode and scroll the browser to it  entry->editCB();  /*  entry->editMode = 2;  entry->value( entry->filename.c_str() );  entry->take_focus();  entry->position( 0, entry->filename.size() );    entry->redraw();  */  if( !fileDetailsBtn->value() )    filelist->scroll_to( entry );  else    filedetails->scroll_to( entry );}void Flu_File_Chooser :: recursiveScan( const char *dir, StringVector *files ){  dirent **e;  char *name;  FluSimpleString fullpath;  int num = fl_filename_list( dir, &e );  for( int i = 0; i < num; i++ )    {      name = e[i]->d_name;      // if 'name' ends in '/' or '\', remove it      if( name[strlen(name)-1] == '/' || name[strlen(name)-1] == '\\' )	name[strlen(name)-1] = '\0';      // ignore the "." and ".." names      if( strcmp( name, "." ) == 0 || strcmp( name, ".." ) == 0 )	continue;      // file or directory?      fullpath = dir;      fullpath += "/";      fullpath += name;      if( fl_filename_isdir( fullpath.c_str() ) != 0 )	recursiveScan( fullpath.c_str(), files );      files->add( fullpath );    }  files->add( dir );}void Flu_File_Chooser :: trashCB( bool recycle ){  // linux doesn't have a recycle bin#ifndef WIN32  recycle = false;#endif  bool inFavorites = ( currentDir == FAVORITES_UNIQUE_STRING );  if( inFavorites )    recycle = false;  // see how many files are selected  FluSimpleString name;  int selected = 0;  int i;  const char *first = "";  Fl_Group *g = getEntryGroup();  for( i = 0; i < g->children(); i++ )    {      if( ((Entry*)g->child(i))->selected )	{	  if( selected == 0 )	    first = ((Entry*)g->child(i))->filename.c_str();	  selected++;	}    }   if( selected )     {       if( selected == 1 )	 {	   if( recycle )	     {	       if( !fl_ask( "Really send '%s' to the Recycle Bin?", first ) )		 return;	     }	   else	     {	       if( !fl_ask( "Really delete '%s'?", first ) )		 return;	     }	 }       else	 {	   if( recycle )	     {	       if( !fl_ask( "Really send these %d files to the Recycle Bin?", selected ) )		 return;	     }	   else	     {	       if( !fl_ask( "Really delete these %d files?", selected ) )		 return;	     }	 }       if( inFavorites )	 {	   for( i = 0; i < g->children(); )	     {	       Entry *e = ((Entry*)g->child(i));	       if( e->selected )		 {		   favoritesList->remove(i+1);		   g->remove( *e );		   delete e;		 }	       else 		 i++;	     }	   // save the favorites	   FILE *f = fopen( configFilename.c_str(), "w" );	   if( f )	     {	       for( i = 1; i <= favoritesList->size(); i++ )		 fprintf( f, "%s\n", favoritesList->text(i) );	       fclose( f );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?