- Finish dnd support. It currently doesn't work with other file managers

(sporadically it works with Konqueror) because fltk always sets text/plain
mimetype.
- Fix cut/copy/paste to use system clipboard instead of internal arrays.
Again, it would work with other fm's with proper mimetype.
- Many tweaks to EDE_Browser to make it render properly inside a Fl_Tile.
- Add treeview support to Fl_Icon_Browser and implement directory tree in
efiler.
- Add location bar.
- Beggining of multi-view support.
This commit is contained in:
Vedran Ljubovic 2007-08-01 18:04:35 +00:00
parent 68364591e2
commit a99c55b6f1
8 changed files with 1205 additions and 462 deletions

View File

@ -214,21 +214,28 @@ static void scroll_cb(Fl_Widget* w, void*) {
EDE_Browser::EDE_Browser(int X,int Y,int W,int H,const char *L) : Fl_Icon_Browser(X,Y,W,H),
totalwidth_(0), column_header_(0), sort_column(0), sort_type(NO_SORT), sort_direction(false) {
heading = new Heading(X,Y,W,buttonheight);
heading->end();
heading->hide();
heading->parent(this); // for callback
hscrollbar = new Fl_Scrollbar(X, Y+H-Fl::scrollbar_size(), W-Fl::scrollbar_size(), Fl::scrollbar_size());
hscrollbar->type(FL_HORIZONTAL);
hscrollbar->hide();
hscrollbar->parent(this); // for callback
hscrollbar->callback(scroll_cb);
Fl_Group* thegroup = new Fl_Group(X,Y,W,H);
thegroup->begin();
heading = new Heading(0,0,W,buttonheight);
heading->box(FL_FLAT_BOX); // draw heading background
heading->align(FL_ALIGN_CLIP);
heading->end();
heading->hide();
heading->parent(this); // for callback
hscrollbar = new Fl_Scrollbar(1, H-Fl::scrollbar_size()-2, W-Fl::scrollbar_size()-3, Fl::scrollbar_size()); // take account for edges
hscrollbar->type(FL_HORIZONTAL);
hscrollbar->hide();
hscrollbar->parent(this); // for callback
hscrollbar->callback(scroll_cb);
thegroup->end();
thegroup->add(this);
has_scrollbar(VERTICAL);
resizable(0);
thegroup->resizable(this);
thegroup->align(FL_ALIGN_CLIP);
// EDE_Browser is always a multiple-selection browser
type(FL_MULTI_BROWSER);
@ -341,6 +348,7 @@ const int* EDE_Browser::column_widths() const {
// Subclassed handle() for keyboard searching
int EDE_Browser::handle(int e) {
if (e==FL_FOCUS) { fprintf(stderr, "EB::focus\n"); }
if (e==FL_KEYBOARD && Fl::event_state()==0) {
// when user presses a key, jump to row starting with that character
int k=Fl::event_key();
@ -374,6 +382,12 @@ int EDE_Browser::handle(int e) {
do_callback();
// }
}
if (k == FL_Tab) {
fprintf (stderr, "TAB\n");
// Fl_Icon_Browser::handle(FL_UNFOCUS); return 1;
}
}
return Fl_Icon_Browser::handle(e);
}
@ -388,13 +402,20 @@ void EDE_Browser::resize(int X, int Y, int W, int H) {
H += fsbs;
} else {
// show scrollbar
hscrollbar->resize(X, Y+H-fsbs, W-fsbs, fsbs);
hscrollbar->value(hscrollbar->value(), W, 0, totalwidth_);
if (!hscrollbar->visible()) {
hscrollbar->resize(X+1, Y+H-fsbs-2, W-fsbs-3, fsbs);
hscrollbar->show();
H -= fsbs;
} else {
hscrollbar->resize(X+1, Y+H-2, W-fsbs-3, fsbs);
hscrollbar->redraw();
}
}
heading->resize(X, Y-buttonheight, W, buttonheight);
// else
// heading->position(X, Y);
Fl_Icon_Browser::resize(X,Y,W,H);
}
@ -440,9 +461,15 @@ int EDE_Browser::Heading::handle(int event) {
// Event coordinates
int evx = Fl::event_x();
int evy = Fl::event_y();
if (event==FL_FOCUS) return 0; // this is a focusless widget!
// if (event==FL_FOCUS && Fl::event_key()==FL_Tab) {
// parent()->take_focus(); // heading shouldn't take focus
// return 1;
// }
switch (event) {
case FL_MOVE:
case FL_ENTER:
case FL_PUSH: {

View File

@ -25,6 +25,7 @@
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Scrollbar.H>
#include <FL/Fl_Image.H>
#include <FL/fl_draw.H>
#include "Fl_Icon_Browser.h"
@ -67,6 +68,12 @@ private:
public:
Heading(int x, int y, int w, int h, const char *label = 0) : Fl_Group(x,y,w,h,label) {}
int handle(int e);
void draw() {
if (!visible()) return;
if (x() && y() && w() && h() && fl_not_clipped(x(),y(),w(),h())) fl_push_clip(x(),y(),w(),h());
Fl_Group::draw();
if (x() && y() && w() && h() && fl_not_clipped(x(),y(),w(),h())) fl_pop_clip();
}
} *heading;
Fl_Scrollbar *hscrollbar;
@ -171,17 +178,21 @@ public:
void resize(int X, int Y, int W, int H);
// Overload hposition(), so that heading scrolls together with browser
void hposition(int x) {
void hposition(int X) {
if (heading->visible()) {
heading->resize(0-x,heading->y(),totalwidth_,buttonheight);
static int oldX=0;
// Move each button by X pixels
for(int i=0; i<heading->children(); i++) {
Fl_Widget* c = heading->child(i);
c->position(c->x()-X+oldX, c->y());
}
fl_push_clip(x(),y(),w(),h());
heading->redraw();
fl_pop_clip();
oldX=X;
}
Fl_Icon_Browser::hposition(x);
Fl_Icon_Browser::hposition(X);
}
// This is *really* needed :(
int get_focus() { return lineno(selection()); }
void set_focus(int row) { select(row,selected(row)); }
};
#endif

View File

@ -39,15 +39,15 @@ struct FileItem {
};
// Type for rename_callback
// I don't know how to do this without creating a new type :(
typedef void (rename_callback_type)(const char*);
typedef void (paste_callback_type)(const char*,const char*);
typedef void (paste_callback_type)(const char*);
class FileDetailsView : public EDE_Browser {
class FileDetailsView_ : public EDE_Browser {
private:
// EDE_Browser* browser; - yada
// internal list of items
@ -66,13 +66,17 @@ private:
}
rename_callback_type* rename_callback_;
paste_callback_type* paste_callback_;
Fl_Callback* context_callback_;
// Subclass Fl_Input so we can handle keyboard
class EditBox : public Fl_Input {
friend class FileDetailsView;
public:
EditBox(int x, int y, int w, int h, const char* label = 0) : Fl_Input(x,y,w,h,label) {}
int handle (int e) {
FileDetailsView* view = (FileDetailsView*)parent();
FileDetailsView_* view = (FileDetailsView_*)parent();
if (e==FL_KEYBOARD && visible()) {
int k = Fl::event_key();
if (Fl::event_key()==FL_Enter && visible()) {
@ -94,9 +98,6 @@ private:
}* editbox_;
int editbox_row;
rename_callback_type* rename_callback_;
paste_callback_type* dnd_callback_;
// show editbox at specified row and make the row "invisible" (bgcolor same as fgcolor)
void show_editbox(int row) {
if (!rename_callback_) return;
@ -111,14 +112,16 @@ private:
char* tmp = strchr(filename, column_char());
if (tmp) *tmp='\0';
editbox_->value(filename);
bucket.add(filename);
// make the row "invisible"
editbox_row=row;
char* ntext = (char*)malloc(sizeof(char)*strlen(text(row))+5); // add 4 places for format chars
strncpy(ntext+5, text(row), strlen(text(row)));
ntext[0]='@'; ntext[1]='C'; ntext[2]='2'; ntext[3]='5'; ntext[4]='5';
char* ntext = (char*)malloc(sizeof(char)*(strlen(text(row))+8)); // add 7 places for format chars
strncpy(ntext+7, text(row), strlen(text(row)));
strncpy(ntext, "@C255@.", 7);
ntext[strlen(text(row))+7]='\0';
text(row,ntext);
free(ntext);
bucket.add(ntext);
// calculate location for editbox
@ -143,10 +146,11 @@ private:
editbox_->hide();
// Make the edited row visible again
char* ntext = (char*)malloc(sizeof(char)*strlen(text(editbox_row))-5); // add 4 places for format chars
strncpy(ntext, text(editbox_row)+5, strlen(text(editbox_row))-5);
char* ntext = (char*)malloc(sizeof(char)*(strlen(text(editbox_row))-6)); // remove 7 places for format chars
strncpy(ntext, text(editbox_row)+7, strlen(text(editbox_row))-7);
ntext[strlen(text(editbox_row))-7]='\0';
text(editbox_row,ntext);
free(ntext);
bucket.add(ntext);
}
// Do the rename
@ -189,7 +193,7 @@ private:
} bucket;
public:
FileDetailsView(int X, int Y, int W, int H, char*label=0) : EDE_Browser(X,Y,W,H,label) {
FileDetailsView_(int X, int Y, int W, int H, char*label=0) : EDE_Browser(X,Y,W,H,label) {
// browser = new EDE_Browser(X,Y,W,H,label);
// browser->end();
// end();
@ -214,7 +218,8 @@ public:
editbox_->hide();
rename_callback_ = 0;
dnd_callback_ = 0;
paste_callback_ = 0;
context_callback_ = 0;
}
// ~FileDetailsView() { delete browser; }
@ -247,11 +252,10 @@ fprintf (stderr, "value: %s\n", value.c_str());
//EDE_Browser::remove(row);
//insert(row, item);
// this was reimplemented because a) it's unoptimized, b) adds stuff at the end,
// this was reimplemented because a) it's unoptimized, b) adds everything at the end,
// c) causes browser to lose focus, making it impossible to click on something while
// directory is loading
edelib::String value;
value = item->name+"\t"+item->description+"\t"+item->size+"\t"+item->date+"\t"+item->permissions;
char* realpath = strdup(item->realpath.c_str());
@ -266,14 +270,23 @@ fprintf (stderr, "value: %s\n", value.c_str());
set_icon(row, Fl_Shared_Image::get(icon.c_str()));
}
// This is needed because update() uses path to find item in list
void update_path(const char* oldpath,const char* newpath) {
int row=findrow(oldpath);
if (row==0) return;
char* c = strdup(newpath);
data(row,c);
bucket.add(c);
}
// Change color of row to gray
void gray(int row) {
if (text(row)[0] == '@' && text(row)[1] == 'C') return; // already greyed
char *ntext = (char*)malloc(sizeof(char)*strlen(text(row))+5); // add 4 places for format chars
strncpy(ntext+4, text(row), strlen(text(row)));
ntext[0]='@'; ntext[1]='C'; ntext[2]='2'; ntext[3]='5'; // @C25 - nice shade of gray
ntext[strlen(text(row))+4]='\0'; // in case text(row) was broken
char *ntext = (char*)malloc(sizeof(char)*(strlen(text(row))+7)); // add 6 places for format chars
strncpy(ntext+6, text(row), strlen(text(row)));
strncpy(ntext, "@C25@.", 6);
ntext[strlen(text(row))+6]='\0';
text(row,ntext);
bucket.add(ntext);
@ -287,28 +300,56 @@ fprintf (stderr, "value: %s\n", value.c_str());
void ungray(int row) {
if (text(row)[0] != '@' || text(row)[1] != 'C') return; // not greyed
char *ntext = (char*)malloc(sizeof(char)*strlen(text(row))-4); // 4 places for format chars
strncpy(ntext, text(row)+4, strlen(text(row))-4);
char *ntext = (char*)malloc(sizeof(char)*(strlen(text(row))-5)); // remove 6 places for format chars
strncpy(ntext, text(row)+6, strlen(text(row))-6);
ntext[strlen(text(row))-6]='\0';
text(row,ntext);
bucket.add(ntext);
// don't work
// doesn't work
//Fl_Image* im = get_icon(row);
//im->uncache(); // doesn't work
//redraw(); // OPTIMIZE
}
// Overloaded handle for file renaming and dnd support
int handle(int e) {
// Rename support
if (e==FL_KEYBOARD) {
// Right click
//fprintf(stderr, "Event: %d\n", e);
if (e==FL_RELEASE && Fl::event_button()==3) {
void* item = item_first();
int itemy=y()-position();
int i;
for (i=1; i<=size(); i++) {
itemy+=item_height(item);
if (itemy>Fl::event_y()) break;
}
set_focus(i);
Fl::event_is_click(0); // prevent doubleclicking with right button
if (context_callback_) context_callback_(this, data(i));
return 1;
}
// if (e==FL_RELEASE && Fl::event_button()==3) { return 1; }
/* ------------------------------
Rename support
--------------------------------- */
// fprintf (stderr, "Event: %d\n", e);
/* if (e==FL_KEYBOARD) {
if (Fl::event_key()==FL_F+2) {
if (editbox_->visible())
hide_editbox();
else
show_editbox(get_focus());
}
}
}*/
if (e==FL_PUSH && editbox_->visible() && !Fl::event_inside(editbox_))
hide_editbox(); // hide editbox when user clicks outside of it
if (e==FL_MOUSEWHEEL && editbox_->visible())
@ -322,6 +363,7 @@ fprintf (stderr, "value: %s\n", value.c_str());
if (Fl::event_x()<x() || Fl::event_x()>x()+l[0])
return Fl_Icon_Browser::handle(e); // we're only interested in first column
// Is clicked item also focused?
void* item = item_first();
int focusy=y()-position();
for (int i=1; i<get_focus(); i++) {
@ -330,74 +372,109 @@ fprintf (stderr, "value: %s\n", value.c_str());
item=item_next(item);
}
if (Fl::event_y()<focusy || Fl::event_y()>focusy+item_height(item))
return Fl_Icon_Browser::handle(e); // Click outside selected item
return Fl_Icon_Browser::handle(e); // It isn't
if (selected(get_focus())!=1)
return Fl_Icon_Browser::handle(e); // allow to select item if it's just focused
return Fl_Icon_Browser::handle(e); // If it isn't selected, then this action is select
renaming=true;
renaming=true; // On next event, we will check if it's doubleclick
}
if (e==FL_RELEASE && renaming && Fl::event_clicks()==0) {
if (renaming && (e==FL_DRAG || e==FL_RELEASE || e==FL_MOVE) && Fl::event_is_click()==0) {
// Fl::event_is_click() will be >0 on doubleclick
show_editbox(get_focus());
renaming=false;
return 1; // don't pass mouse event, otherwise item will become selected again
return 1; // don't pass mouse event, otherwise item will become reselected which is a bit ugly
}
// Drag&drop support
static int paste_event_y;
/* ------------------------------
Drag&drop support
--------------------------------- */
/*--- This is to get dnd events from non-fltk apps ---
static bool dragging=false;
if (e==FL_PUSH) dragging=false;
if (e==FL_DND_ENTER) dragging=true;
if (e==FL_RELEASE && dragging) {
paste_event_y=Fl::event_y();
Fl::paste(*this,0);
dragging=false;
// If paste_callback_ isn't set, that means we don't support dnd
if (paste_callback_==0) return EDE_Browser::handle(e);
// Let the window manager know that we accept dnd
if (e==FL_DND_ENTER||e==FL_DND_DRAG) return 1;
// Scroll the view by dragging to border (only works on heading... :( )
if (e==FL_DND_LEAVE) {
if (Fl::event_y()<y())
position(position()-1);
if (Fl::event_y()>y()+h())
position(position()+1);
return 1;
}
/*--- End ugly hack ---*/
// Don't unselect on FL_PUSH cause that could be dragging
// Don't unselect items on FL_PUSH cause that could be dragging
if (e==FL_PUSH && Fl::event_clicks()!=1) return 1;
static int dragx,dragy;
if (e==FL_DRAG) {
edelib::String selected_items;
for (int i=1; i<=size(); i++)
if (selected(i)==1) {
if (selected_items != "") selected_items += ",";
if (selected_items != "") selected_items += "\n";
selected_items += (char*)data(i);
}
Fl::copy(selected_items.c_str(),selected_items.length(),0);
Fl::dnd();
dragx = Fl::event_x(); dragy = Fl::event_y();
return 1; // don't do the multiple selection thing from Fl_Browser
}
static bool dndrelease=false;
if (e==FL_DND_RELEASE) {
paste_event_y=Fl::event_y();
Fl::paste(*this,0);
if (Fl::event_y()<y() || Fl::event_y()>y()+h()) return 1;
// ^^ this can be a source of crashes in Fl::dnd()
fprintf(stderr, "FL_DND_RELEASE\n");
// Sometimes drag is accidental
if (abs(Fl::event_x()-dragx)>10 || abs(Fl::event_y()-dragy)>10) {
dndrelease=true;
Fl::paste(*this,0);
}
}
if (e==FL_PASTE) {
fprintf(stderr, "FL_PASTE\n");
if (!Fl::event_text() || !Fl::event_length()) return 1;
fprintf(stderr, "1 '%s' (%d)\n",Fl::event_text(),Fl::event_length());
// Paste comes from menu/keyboard
if (!dndrelease) {
paste_callback_(0);
return 1;
}
dndrelease=false;
// Where is the user dropping?
// If it's not the first column, we assume the current directory
const int* l = column_widths();
if (Fl::event_x()<x() || Fl::event_x()>x()+l[0]) {
paste_callback_(0);
return 1;
}
// Find item where stuff is dropped
void* item = item_first();
int itemy=y()-position();
int i;
for (i=1; i<=size(); i++) {
itemy+=item_height(item);
if (itemy>paste_event_y) break;
if (itemy>Fl::event_y()) break;
}
dnd_callback_(Fl::event_text(),(const char*)data(i));
}
// if (e==FL_DND_ENTER) { take_focus(); Fl::focus(this); Fl::belowmouse(this); Fl_Icon_Browser::handle(FL_FOCUS); }
// if (e==FL_DND_LEAVE) { take_focus(); Fl::focus(this); Fl::belowmouse(this); Fl_Icon_Browser::handle(FL_FOCUS); }
//fprintf (stderr, "Event: %d\n", e);
return Fl_Icon_Browser::handle(e);
paste_callback_((const char*)data(i));
return 1; // Fl_Browser doesn't know about paste so don't bother it
}
return EDE_Browser::handle(e);
}
// Setup callback that will be used when renaming and dnd
void rename_callback(rename_callback_type* cb) { rename_callback_ = cb; }
void dnd_callback(paste_callback_type* cb) { dnd_callback_ = cb; }
void paste_callback(paste_callback_type* cb) { paste_callback_ = cb; }
void context_callback(Fl_Callback* cb) { context_callback_ = cb; }
// Avoid memory leak
void clear() {
@ -408,6 +485,70 @@ fprintf(stderr, "Call FileView::clear()\n");
};
class FileView : public Fl_Group {
public:
FileView(int X, int Y, int W, int H, char*label=0) : Fl_Group(X,Y,W,H,label) {}
void insert(int row, FileItem *item);
void add(FileItem *item);
void remove(FileItem *item);
void update(FileItem *item);
void update_path(const char* oldpath,const char* newpath);
void gray(int row);
void ungray(int row);
void rename_callback(rename_callback_type* cb);
void paste_callback(paste_callback_type* cb);
};
class FileDetailsView : public FileView {
private:
FileDetailsView_ *browser;
public:
FileDetailsView(int X, int Y, int W, int H, char*label=0) : FileView(X,Y,W,H,label) {
browser = new FileDetailsView_(X,Y,W,H,label);
browser->end();
end();
}
// ~FileDetailsView() { delete browser; }
void insert(int row, FileItem *item) { browser->insert(row,item); }
void add(FileItem *item) { browser->add(item); }
void remove(FileItem *item) { browser->remove(item); }
void update(FileItem *item) { browser->update(item); }
void update_path(const char* oldpath,const char* newpath) { browser->update_path(oldpath,newpath); }
void gray(int row) { browser->gray(row); }
void ungray(int row) { browser->ungray(row); }
void rename_callback(rename_callback_type* cb) { browser->rename_callback(cb); }
void paste_callback(paste_callback_type* cb) { browser->paste_callback(cb); }
void context_callback(Fl_Callback* cb) { browser->context_callback(cb); }
// Browser methods
const char* path(int i) { return (const char*)browser->data(i); }
int size() { return browser->size(); }
int selected(int i) { return browser->selected(i); }
void select(int i, int k) { browser->select(i,k); browser->middleline(i); }
int get_focus() { return browser->get_focus(); }
void set_focus(int i) { browser->set_focus(i); }
void remove(int i) { browser->remove(i); }
void clear() { browser->clear(); }
void callback(Fl_Callback*cb) { browser->callback(cb); }
// These methods are used by do_rename
const char* text(int i) { return browser->text(i); }
void text(int i, const char* c) { return browser->text(i,c); }
uchar column_char() { return browser->column_char(); }
};
#endif
/* $Id */

View File

@ -42,7 +42,7 @@
// up.
// Also added the ability to "hide" a line. This set's it's height to
// zero, so the Fl_Icon_Browser_ cannot pick it.
// zero, so the Fl_Browser_ cannot pick it.
#define SELECTED 1
#define NOTDISPLAYED 2
@ -54,6 +54,7 @@ struct FL_BLINE { // data is in a linked list of these
Fl_Image* icon;
short length; // sizeof(txt)-1, may be longer than string
char flags; // selected, displayed
int level; // indentation level for tree
char txt[1]; // start of allocated array
};
@ -174,6 +175,7 @@ void Fl_Icon_Browser::insert(int line, const char* newtext, void* d) {
strcpy(t->txt, newtext);
t->data = d;
t->icon = 0;
t->level = 0;
insert(line, t);
}
@ -194,6 +196,7 @@ void Fl_Icon_Browser::text(int line, const char* newtext) {
n->length = (short)l;
n->flags = t->flags;
n->icon = t->icon;
n->level = t->level;
n->prev = t->prev;
if (n->prev) n->prev->next = n; else first = n;
n->next = t->next;
@ -337,6 +340,151 @@ void Fl_Icon_Browser::item_draw(void* v, int X, int Y, int W, int H) const {
if (e) {*e = 0; w1 = *i++;}
}
// Tree drawing code
// TODO: try to avoid using item_height...
#define TREE_GLYPH_WIDTH 10
// [+] and [-] box size
#define TREE_BOX_SIZE 6
if (first && blind->level>0) {
if (blind->flags & SELECTED)
fl_color(fl_contrast(textcolor(), selection_color()));
else
fl_color(textcolor());
fl_line_style(FL_DOT);
int ih = item_height(blind);
int hspace = (TREE_GLYPH_WIDTH-TREE_BOX_SIZE)/2;
int vspace = (ih-TREE_BOX_SIZE)/2;
// Draw vertical lines for parents
for(int i=1; i<blind->level; i++) {
// is there anything below to connect?
FL_BLINE* k = blind->next;
while (k && k->level > i)
k = k->next;
if (k && k->level==i)
fl_line(X+TREE_GLYPH_WIDTH/2, Y, X+TREE_GLYPH_WIDTH/2, Y+ih);
X += TREE_GLYPH_WIDTH; W -= TREE_GLYPH_WIDTH; w1 -= TREE_GLYPH_WIDTH;
}
if (blind->next && blind->next->level > blind->level) {
// draw dotted lines before and after
if (blind->prev)
fl_line(X+TREE_GLYPH_WIDTH/2, Y, X+TREE_GLYPH_WIDTH/2, Y+vspace);
// is there another item with same level?
FL_BLINE* p = blind->next;
bool collapsed=true;
while (p && p->level > blind->level) {
if (!(p->flags&NOTDISPLAYED)) collapsed=false;
p = p->next;
}
if (p && p->level == blind->level)
fl_line(X+TREE_GLYPH_WIDTH/2, Y+ih-vspace, X+TREE_GLYPH_WIDTH/2, Y+ih);
// draw [+] glyph
fl_color(fl_lighter(fl_color()));
fl_line_style(0);
fl_line(X+hspace, Y+vspace, X+TREE_GLYPH_WIDTH-hspace, Y+vspace);
fl_line(X+TREE_GLYPH_WIDTH-hspace, Y+vspace, X+TREE_GLYPH_WIDTH-hspace, Y+ih-vspace-(ih%2));
fl_line(X+TREE_GLYPH_WIDTH-hspace, Y+ih-vspace-(ih%2), X+hspace, Y+ih-vspace-(ih%2));
fl_line(X+hspace, Y+ih-vspace-(ih%2), X+hspace, Y+vspace);
fl_line(X+hspace+2, Y+ih/2, X+TREE_GLYPH_WIDTH-hspace-2, Y+ih/2); // minus
if (collapsed)
fl_line(X+TREE_GLYPH_WIDTH/2, Y+vspace+2, X+TREE_GLYPH_WIDTH/2, Y+ih-vspace-2-(ih%2)); // plus
fl_color(fl_darker(fl_color()));
}
else if (blind->next && blind->next->level == blind->level) {
// draw |- glyph
fl_line(X+TREE_GLYPH_WIDTH/2, Y, X+TREE_GLYPH_WIDTH/2, Y+ih);
fl_line(X+TREE_GLYPH_WIDTH/2+2, Y+ih/2, X+TREE_GLYPH_WIDTH-1, Y+ih/2);
}
else {
// draw L glyph
fl_line(X+TREE_GLYPH_WIDTH/2, Y, X+TREE_GLYPH_WIDTH/2, Y+ih/2);
fl_line(X+TREE_GLYPH_WIDTH/2+2, Y+ih/2, X+TREE_GLYPH_WIDTH-1, Y+ih/2);
}
X += TREE_GLYPH_WIDTH; W -= TREE_GLYPH_WIDTH; w1 -= TREE_GLYPH_WIDTH;
fl_line_style(0);
}
// NOTE: This version is drawing continuous lines, but the problem is,
// if parent item is invisible (e.g. scrolled out) it's line isn't drawn :(
/* #define TREE_GLYPH_WIDTH 10
#define TREE_BOX_SIZE 6 // box size
if (first && blind->level>0) {
if (blind->flags & SELECTED)
fl_color(fl_contrast(textcolor(), selection_color()));
else
fl_color(textcolor());
fl_line_style(FL_DOT);
int ih = item_height(blind);
int hspace = (TREE_GLYPH_WIDTH-TREE_BOX_SIZE)/2;
int vspace = (ih-TREE_BOX_SIZE)/2;
for(int i=1; i<blind->level; i++) {
// leave space for the vertical line
X += TREE_GLYPH_WIDTH; W -= TREE_GLYPH_WIDTH; w1 -= TREE_GLYPH_WIDTH;
}
if (blind->next && blind->next->level > blind->level) {
// draw dotted lines before and after
if (blind->prev)
fl_line(X+TREE_GLYPH_WIDTH/2, Y, X+TREE_GLYPH_WIDTH/2, Y+vspace);
int lineend = Y+ih;
FL_BLINE* p = blind->next;
bool collapsed=true;
while (p && p->level > blind->level) {
lineend += item_height(p);
if (!(p->flags&NOTDISPLAYED)) collapsed=false;
p = p->next;
}
if (p && p->level == blind->level)
fl_line(X+TREE_GLYPH_WIDTH/2, Y+ih-vspace, X+TREE_GLYPH_WIDTH/2, lineend);
// draw [+] glyph
fl_color(fl_lighter(fl_color()));
fl_line_style(0);
fl_line(X+hspace, Y+vspace, X+TREE_GLYPH_WIDTH-hspace, Y+vspace);
fl_line(X+TREE_GLYPH_WIDTH-hspace, Y+vspace, X+TREE_GLYPH_WIDTH-hspace, Y+ih-vspace-(ih%2));
fl_line(X+TREE_GLYPH_WIDTH-hspace, Y+ih-vspace-(ih%2), X+hspace, Y+ih-vspace-(ih%2));
fl_line(X+hspace, Y+ih-vspace-(ih%2), X+hspace, Y+vspace);
fl_line(X+hspace+2, Y+ih/2, X+TREE_GLYPH_WIDTH-hspace-2, Y+ih/2); // minus
if (collapsed)
fl_line(X+TREE_GLYPH_WIDTH/2, Y+vspace+2, X+TREE_GLYPH_WIDTH/2, Y+ih-vspace-2-(ih%2)); // plus
fl_color(fl_darker(fl_color()));
fl_line_style(FL_DOT);
X += TREE_GLYPH_WIDTH; W -= TREE_GLYPH_WIDTH; w1 -= TREE_GLYPH_WIDTH;
}
else if (blind->next && blind->next->level == blind->level) {
// draw |- glyph
int lineend = Y+ih;
FL_BLINE* p = blind->next;
FL_BLINE* q = blind;
while (p && p->level == blind->level) {
lineend += item_height(p);
q = p;
p = p->next;
}
lineend -= item_height(q);
fl_line(X+TREE_GLYPH_WIDTH/2, Y, X+TREE_GLYPH_WIDTH/2, lineend);
fl_line(X+TREE_GLYPH_WIDTH/2+2, Y+ih/2, X+TREE_GLYPH_WIDTH-1, Y+ih/2);
X += TREE_GLYPH_WIDTH; W -= TREE_GLYPH_WIDTH; w1 -= TREE_GLYPH_WIDTH;
}
else {
// draw L glyph
fl_line(X+TREE_GLYPH_WIDTH/2, Y, X+TREE_GLYPH_WIDTH/2, Y+ih/2);
fl_line(X+TREE_GLYPH_WIDTH/2+2, Y+ih/2, X+TREE_GLYPH_WIDTH-1, Y+ih/2);
X += TREE_GLYPH_WIDTH; W -= TREE_GLYPH_WIDTH; w1 -= TREE_GLYPH_WIDTH;
}
fl_line_style(0);
}*/
// Icon drawing code
if (first) {
first=false;
@ -592,7 +740,133 @@ void Fl_Icon_Browser::remove_icon(int line) {
}
}
Fl_Image* Fl_Icon_Browser::get_icon(int line) { return find_line(line)->icon; }
Fl_Image* Fl_Icon_Browser::get_icon(int line) {
if (line < 1 || line > lines) return 0;
return find_line(line)->icon;
}
// Tree support
void Fl_Icon_Browser::indent(int line, int level) {
if (line < 1 || line > lines) return;
if (level<0) return;
find_line(line)->level = level;
}
int Fl_Icon_Browser::indent(int line) {
if (line < 1 || line > lines) return 0;
return find_line(line)->level;
}
void Fl_Icon_Browser::collapse(void* l) {
fprintf(stderr, "collapse(l)\n");
FL_BLINE* ll = (FL_BLINE*)l;
FL_BLINE* p = ll->next;
while (p && p->level > ll->level) {
if (!(p->flags & NOTDISPLAYED)) {
full_height_ -= item_height(p);
p->flags |= NOTDISPLAYED;
}
p = p->next;
}
}
void Fl_Icon_Browser::expand(void* l) {
fprintf(stderr, "expand(l)\n");
FL_BLINE* ll = (FL_BLINE*)l;
FL_BLINE* p = ll->prev;
// if invisible, expand parent
if (ll->flags & NOTDISPLAYED) {
while (p && p->level != ll->level-1)
p = p->prev;
if (p) expand(p); // if not found, do nothing
}
p = ll->next;
while (p && p->level > ll->level) {
// expand only the level below!
if ((p->flags & NOTDISPLAYED) && (p->level==ll->level+1)) {
p->flags &= ~NOTDISPLAYED;
full_height_ += item_height(p);
}
p = p->next;
}
}
void Fl_Icon_Browser::collapse(int line) {
if (line < 1 || line > lines) return;
collapse(find_line(line));
redraw_lines();
}
void Fl_Icon_Browser::expand(int line) {
if (line < 1 || line > lines) return;
expand(find_line(line));
redraw();
}
void Fl_Icon_Browser::collapse_all() {
FL_BLINE* l = first;
if (!l) return;
do {
collapse(l);
} while (l=l->next);
redraw_lines();
}
void Fl_Icon_Browser::expand_all() {
FL_BLINE* l = first;
if (!l) return;
do {
collapse(l);
} while (l=l->next);
redraw();
}
void Fl_Icon_Browser::toggle_collapse(int line) {
if (line < 1 || line > lines) return;
toggle_collapse(find_line(line));
redraw();
}
void Fl_Icon_Browser::toggle_collapse(void* lp) {
FL_BLINE* l = (FL_BLINE*)lp;
if (!l->next || l->next->level <= l->level) return;
bool invisibles=false;
FL_BLINE* p = l->next;
do {
// expand only one level below!
if ((p->flags & NOTDISPLAYED) && (p->level==l->level+1))
invisibles=true;
} while (p=p->next);
if (invisibles)
expand(l);
else
collapse(l);
}
int Fl_Icon_Browser::handle(int e) {
if (e==FL_PUSH) {
FL_BLINE* l = first;
int itemy=y()-position();
while (l) {
itemy+=item_height(l);
if (itemy>Fl::event_y()) break;
l = l->next;
}
if (l && (l->level > 0) && (Fl::event_x()>x()+(l->level-1)*TREE_GLYPH_WIDTH) && (Fl::event_x()<x()+l->level*TREE_GLYPH_WIDTH)) {
toggle_collapse(l);
redraw();
Fl::event_is_click(0); // prevent the next click from becoming callback
return 1;
}
}
return Fl_Browser_::handle(e);
}
//

View File

@ -135,6 +135,22 @@ public:
void remove_icon(int line);
Fl_Image* get_icon(int line);
// focus management
int get_focus() { return lineno(selection()); }
void set_focus(int row) { select(row,selected(row)); }
// tree support
void indent(int line, int level);
int indent(int line);
void collapse(void*l);
void expand(void*l);
void toggle_collapse(void*l);
void collapse(int line);
void expand(int line);
void toggle_collapse(int line);
void collapse_all();
void expand_all();
int handle(int);
};
#endif

View File

@ -14,9 +14,8 @@
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/time.h> // timer
#include <sys/resource.h> // for core
#include <sys/vfs.h> // used for statfs()
#include <sys/time.h> // timer in cb_open
#include <sys/resource.h> // for core dumps
#include <Fl/Fl.H>
@ -25,6 +24,8 @@
#include <Fl/Fl_File_Chooser.H> // for fl_dir_chooser, used in "Open location"
#include <Fl/filename.H>
#include <Fl/fl_ask.H>
#include <Fl/fl_ask.H>
#include <Fl/Fl_File_Input.H> // location bar
#include <edelib/Nls.h>
#include <edelib/MimeType.h>
@ -33,23 +34,32 @@
#include <edelib/Run.h>
#include "EDE_FileView.h" // our file view widget
#include "EDE_DirTree.h" // directory tree
#include "Util.h" // ex-edelib
#include "fileops.h" // file operations
#include "filesystem.h" // filesystem support
#define DEFAULT_ICON "misc-vedran"
Fl_Window* win;
FileDetailsView* view;
Fl_Menu_Bar* main_menu;
Fl_Box* statusbar;
DirTree* dirtree;
Fl_Tile* tile;
Fl_Group* location_bar;
Fl_File_Input* location_input;
Fl_Menu_Button* context_menu;
char current_dir[FL_PATH_MAX];
bool showhidden;
bool semaphore;
bool dirsfirst;
bool ignorecase;
bool showtree;
bool showlocation;
int tree_width;
// constants
@ -57,78 +67,11 @@ bool ignorecase;
const int default_window_width = 600;
const int default_window_height = 400;
const int menubar_height = 30;
const int location_bar_height = 40;
const int statusbar_height = 24;
const int statusbar_width = 400;
/*-----------------------------------------------------------------
Filesystem functions
-------------------------------------------------------------------*/
// Read filesystems - adapted from fltk file chooser
char filesystems[50][PATH_MAX]; // must be global
int get_filesystems() {
FILE *mtab; // /etc/mtab or /etc/mnttab file
// Results are cached in a static array
static int fs_number=0;
// On first access read filesystems
if (fs_number == 0) {
mtab = fopen("/etc/mnttab", "r"); // Fairly standard
if (mtab == NULL)
mtab = fopen("/etc/mtab", "r"); // More standard
if (mtab == NULL)
mtab = fopen("/etc/fstab", "r"); // Otherwise fallback to full list
if (mtab == NULL)
mtab = fopen("/etc/vfstab", "r"); // Alternate full list file
char line[PATH_MAX]; // Input line
char device[PATH_MAX], mountpoint[PATH_MAX], fs[PATH_MAX];
while (mtab!= NULL && fgets(line, sizeof(line), mtab) != NULL) {
if (line[0] == '#' || line[0] == '\n')
continue;
if (sscanf(line, "%s%s%s", device, mountpoint, fs) != 3)
continue;
strcpy(filesystems[fs_number],mountpoint);
fs_number++;
}
fclose (mtab);
if (fs_number == 0) return 0; // error reading mtab/fstab
}
return fs_number;
}
// Get mount point of filesystem for given file
const char* find_fs_for(const char* file) {
int fs_number = get_filesystems();
if (fs_number==0) return 0; // error reading mtab/fstab
// Find filesystem for file (largest mount point match)
char *max;
int maxlen = 0;
for (int i=0; i<fs_number; i++) {
int mylen = strlen(filesystems[i]);
if ((strncmp(file,filesystems[i],mylen)==0) && (mylen>maxlen)) {
maxlen=mylen;
max = (char*)filesystems[i];
}
}
if (maxlen == 0) return 0; // doesn't match any fs? there should always be root
return max;
}
// Tests if two files are on the same filesystem
bool is_on_same_fs(const char* file1, const char* file2) {
const char* fs1 = find_fs_for(file1);
// See if file2 matches the same filesystem
return (strncmp(file1,fs1,strlen(fs1))==0);
}
int default_tree_width=150;
/*-----------------------------------------------------------------
@ -212,7 +155,8 @@ void loaddir(const char *path) {
strncpy(old_dir,current_dir,strlen(current_dir)); // Restore olddir in case of error
// Set current_dir
if (fl_filename_isdir(path)) {
// fl_filename_isdir() thinks that / isn't a dir :(
if (strcmp(path,"/")==0 || fl_filename_isdir(path)) {
if (path[0] == '~') // Expand tilde
snprintf(current_dir,PATH_MAX,"%s/%s",getenv("HOME"),path+1);
else
@ -233,14 +177,13 @@ void loaddir(const char *path) {
*tmp='\0';
}
fprintf (stderr, "loaddir(%s) = (%s)\n",path,current_dir);
// Update directory tree
// dirtree->set_current(current_dir);
dirtree->set_current(current_dir);
location_input->value(current_dir);
// variables used later
int size=0;
dirent **files;
dirent **files;
// List all files in directory
if (ignorecase)
@ -256,12 +199,13 @@ fprintf (stderr, "loaddir(%s) = (%s)\n",path,current_dir);
}
// set window label
// unlike fltk2, labels can be pointers to static char
win->label(tsprintf(_("%s - File manager"), current_dir));
statusbar->label(tsprintf(_("Scanning directory %s..."), current_dir));
// copy_label() is a method that calls strdup() and later free()
win->copy_label(tsprintf(_("%s - File manager"), current_dir));
statusbar->copy_label(tsprintf(_("Scanning directory %s..."), current_dir));
view->clear();
fprintf(stderr, "Size: %d\n", size);
FileItem **item_list = new FileItem*[size];
int fsize=0;
@ -297,6 +241,7 @@ fprintf (stderr, "loaddir(%s) = (%s)\n",path,current_dir);
item->description = "Directory";
// item->name += "/";
item->size = "";
item->realpath += "/";
} else {
item->icon = "unknown";
item->description = "Unknown";
@ -352,15 +297,11 @@ fprintf (stderr, "ICON: %s !!!!!\n", icon.c_str());
semaphore=false;
// Get partition size and free space
// TODO Attempt to cache the results in a meaningful way
static struct statfs statfs_buffer;
if (statfs(current_dir, &statfs_buffer)==0) {
double totalsize = double(statfs_buffer.f_bsize) * statfs_buffer.f_blocks;
double freesize = double(statfs_buffer.f_bsize) * statfs_buffer.f_bavail; // This is what df returns
// f_bfree is size available to root
double percent = double(statfs_buffer.f_blocks-statfs_buffer.f_bavail)/statfs_buffer.f_blocks*100;
char *tmp = strdup(nice_size(totalsize)); // nice_size() operates on a static char buffer, we can't use two calls at the same time
statusbar->label(tsprintf(_("Filesystem %s, Size %s, Free %s (%4.1f%% used)"), find_fs_for(current_dir), tmp, nice_size(freesize), percent));
double totalsize, freesize;
if (fs_usage(current_dir,totalsize,freesize)) {
double percent = (totalsize-freesize)/totalsize*100;
char *tmp = strdup(nice_size(totalsize)); // nice_size() operates on a static char buffer, we can't use two calls in the same command
statusbar->copy_label(tsprintf(_("Filesystem %s, Size %s, Free %s (%4.1f%% used)"), find_fs_for(current_dir), tmp, nice_size(freesize), percent));
free(tmp);
} else
statusbar->label(_("Error reading filesystem information!"));
@ -375,7 +316,7 @@ fprintf (stderr, "ICON: %s !!!!!\n", icon.c_str());
// Open callback
void open_cb(Fl_Widget*w, void*data) {
fprintf (stderr,"cb\n");
if (Fl::event_clicks() || Fl::event_key() == FL_Enter || w==main_menu) {
if (Fl::event_clicks() || Fl::event_key() == FL_Enter || w==main_menu || w==context_menu) {
fprintf (stderr,"enter\n");
//if (Fl::event_clicks()) fprintf(stderr, "clicks\n");
//if (Fl::event_key()==FL_Enter) fprintf(stderr, "ekey\n");
@ -384,9 +325,9 @@ fprintf (stderr,"enter\n");
gettimeofday(&newtm,0);
if (newtm.tv_sec - tm.tv_sec < 1 || (newtm.tv_sec-tm.tv_sec==1 && newtm.tv_usec<tm.tv_usec)) return; // no calling within 1 second
tm=newtm;
if (view->value()==0) return; // This can happen while efiler is loading
if (view->get_focus()==0) return; // This can happen while efiler is loading
char* path = (char*)view->data(view->value());
char* path = (char*)view->path(view->get_focus());
fprintf(stderr, "Path: %s (ev %d)\n",path,Fl::event());
if (stat(path,&stat_buffer)) return; // error
@ -406,19 +347,13 @@ fprintf (stderr,"enter\n");
rlim->rlim_cur = RLIM_INFINITY;
setrlimit (RLIMIT_CORE, rlim);
const char *o2 = tsprintf(opener,path);
const char *o2 = tsprintf(opener,path); // opener should contain %s
fprintf (stderr, "run_program: %s\n", o2);
// construct filename for the message
char* filename = strdup(view->text(view->value()));
if (char*k = strchr(filename, view->column_char())) *k='\0';
if (opener) {
int k=edelib::run_program(o2,false); fprintf(stderr, "retval: %d\n", k);
} else
statusbar->label(tsprintf(_("No program to open %s!"), filename));
free(filename);
statusbar->copy_label(tsprintf(_("No program to open %s!"), fl_filename_name(view->path(view->get_focus()))));
rlim->rlim_cur = old_rlimit;
setrlimit (RLIMIT_CORE, rlim);
@ -438,18 +373,58 @@ void quit_cb(Fl_Widget*, void*) {exit(0);}
void cut_cb(Fl_Widget*, void*) { do_cut_copy(false); }
void copy_cb(Fl_Widget*, void*) { do_cut_copy(true); }
void paste_cb(Fl_Widget*, void*) { do_paste(); }
void paste_cb(Fl_Widget*, void*) { Fl::paste(*view,1); } // view->handle() will call do_paste()
void delete_cb(Fl_Widget*, void*) { do_delete(); }
void showhidden_cb(Fl_Widget*, void*) { showhidden=!showhidden; loaddir(current_dir); }
void showtree_cb(Fl_Widget*, void*) {
showtree = !showtree;
if (!showtree) {
tree_width = dirtree->w();
tile->position(default_tree_width, 1, 1, 1); // NOTE!
fprintf(stderr, "Hide tree: %d\n", tree_width);
// showtree->clear();
} else {
int currentw = dirtree->w();
tile->position(currentw, 1, tree_width, 1);
fprintf(stderr, "Show tree: %d %d\n", currentw, tree_width);
// showtree->set();
}
}
void refresh_cb(Fl_Widget*, void*) {
loaddir(current_dir);
// TODO: reload directory tree as well-
}
void case_cb(Fl_Widget*, void*) { ignorecase=!ignorecase; loaddir(current_dir); }
void locationbar_cb(Fl_Widget*, void*) {
showlocation = !showlocation;
if (showlocation) {
location_bar->show();
location_bar->resize(0, menubar_height, win->w(), location_bar_height);
tile->resize(0, menubar_height+location_bar_height, win->w(), win->h()-menubar_height-location_bar_height-statusbar_height);
win->redraw();
} else {
location_bar->hide();
// location_bar->resize(0, menubar_height, win->w(), 0);
tile->resize(0, menubar_height, win->w(), win->h()-menubar_height-statusbar_height);
win->redraw();
}
}
void showhidden_cb(Fl_Widget*, void*) {
showhidden=!showhidden;
dirtree->show_hidden(showhidden);
dirtree->reload();
loaddir(current_dir);
}
void case_cb(Fl_Widget*, void*) {
ignorecase=!ignorecase;
dirtree->ignore_case(ignorecase);
dirtree->reload();
loaddir(current_dir);
}
void dirsfirst_cb(Fl_Widget*, void*) { dirsfirst=!dirsfirst; loaddir(current_dir); }
@ -458,12 +433,40 @@ void ow_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); } // make a list
void pref_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
void iconsview_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
void listview_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
void showtree_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
void about_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
void aboutede_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
// Directory tree callback
void tree_cb(Fl_Widget*, void*) {
if (Fl::event_clicks() || Fl::event_key() == FL_Enter) {
int selected = dirtree->get_focus();
loaddir((char*)dirtree->data(selected));
}
}
// User entered a location
void location_input_cb(Fl_Widget*, void*) { loaddir(location_input->value()); }
Fl_Menu_Item context_menu_definition[] = {
{_("&Open"), 0, open_cb},
{_("Open &with..."), 0, ow_cb, 0,FL_MENU_DIVIDER},
{_("&Cut"), 0, cut_cb},
{_("Co&py"), 0, copy_cb},
{_("Pa&ste"), 0, paste_cb},
{_("&Delete"), 0, delete_cb, 0,FL_MENU_DIVIDER},
{_("P&references..."), 0, pref_cb},
{0}
};
// Right click - show context menu
void context_cb(Fl_Widget*, void*) {
// context_menu->position(Fl::event_x_root(),Fl::event_y_root());
context_menu->popup();
context_menu->value(-1);
}
/*-----------------------------------------------------------------
GUI design
@ -474,7 +477,7 @@ Fl_Menu_Item main_menu_definition[] = {
{_("&File"), 0, 0, 0, FL_SUBMENU},
{_("&Open"), FL_CTRL+'o', open_cb},
{_("Open &with..."), 0, ow_cb, 0,FL_MENU_DIVIDER},
{_("Open &location"), 0, location_cb, 0,FL_MENU_DIVIDER},
// {_("Open &location"), 0, location_cb, 0,FL_MENU_DIVIDER},
{_("&New window"), FL_CTRL+'n', new_cb, 0,FL_MENU_DIVIDER},
{_("&Quit"), FL_CTRL+'q', quit_cb},
{0},
@ -491,8 +494,9 @@ Fl_Menu_Item main_menu_definition[] = {
{_("&View"), 0, 0, 0, FL_SUBMENU},
{_("&Icons"), FL_F+8, iconsview_cb, 0, FL_MENU_RADIO}, // coming soon
{_("&Detailed list"), FL_F+8, listview_cb, 0, FL_MENU_RADIO|FL_MENU_VALUE|FL_MENU_DIVIDER},
{_("&Show hidden"), 0, showhidden_cb, 0, FL_MENU_TOGGLE},
{_("Directory &tree"), FL_F+9, showtree_cb, 0, FL_MENU_TOGGLE|FL_MENU_DIVIDER}, // coming soon
{_("Directory &tree"), FL_F+9, showtree_cb, 0, FL_MENU_TOGGLE|FL_MENU_VALUE},
{_("&Location bar"), FL_F+10, locationbar_cb, 0, FL_MENU_TOGGLE|FL_MENU_VALUE},
{_("&Show hidden"), 0, showhidden_cb, 0, FL_MENU_TOGGLE|FL_MENU_DIVIDER},
{_("&Refresh"), FL_F+5, refresh_cb},
{_("S&ort"), 0, 0, 0, FL_SUBMENU},
{_("&Case sensitive"), 0, case_cb, 0, FL_MENU_TOGGLE},
@ -524,11 +528,30 @@ edelib::IconTheme::init("crystalsvg");
main_menu->menu(main_menu_definition);
main_menu->textsize(12); // hack for label size
view = new FileDetailsView(0, menubar_height, default_window_width, default_window_height-menubar_height-statusbar_height, 0);
view->callback(open_cb);
// callback for renaming
view->rename_callback(do_rename);
view->dnd_callback(dnd_cb);
location_bar = new Fl_Group(0, menubar_height, default_window_width, location_bar_height);
location_bar->begin();
location_input = new Fl_File_Input(70, menubar_height+2, default_window_width-200, location_bar_height-5, _("Location:"));
location_input->labelsize(12); // hack for label size
location_input->textsize(12); // hack for label size
location_input->align(FL_ALIGN_LEFT);
location_input->callback(location_input_cb);
location_bar->end();
location_bar->box(FL_UP_BOX); // hack for label size
location_bar->resizable(location_input);
tile = new Fl_Tile(0, menubar_height+location_bar_height, default_window_width, default_window_height-menubar_height-location_bar_height-statusbar_height);
tile->begin();
dirtree = new DirTree(0, menubar_height+location_bar_height, default_tree_width, default_window_height-menubar_height-location_bar_height-statusbar_height);
dirtree->textsize(12); // hack for label size
dirtree->callback(tree_cb);
view = new FileDetailsView(150, menubar_height+location_bar_height, default_window_width-default_tree_width, default_window_height-menubar_height-location_bar_height-statusbar_height);
view->callback(open_cb);
// callback for renaming
view->rename_callback(do_rename);
view->paste_callback(do_paste);
view->context_callback(context_cb);
tile->end();
Fl_Group *sbgroup = new Fl_Group(0, default_window_height-statusbar_height, default_window_width, statusbar_height);
statusbar = new Fl_Box(2, default_window_height-statusbar_height+2, statusbar_width, statusbar_height-4);
@ -541,14 +564,27 @@ edelib::IconTheme::init("crystalsvg");
sbgroup->end();
sbgroup->resizable(filler);
context_menu = new Fl_Menu_Button (0,0,0,0);
context_menu->type(Fl_Menu_Button::POPUP3);
context_menu->menu(context_menu_definition);
context_menu->textsize(12); // hack for label size
context_menu->box(FL_NO_BOX);
win->end();
win->resizable(view);
win->resizable(tile);
// win->resizable(view);
// win->icon(Icon::get("folder",Icon::TINY));
win->show(argc,argv);
view->take_focus();
// Yet another hack for label size....
fl_message_font(FL_HELVETICA, 12);
// TODO remember previous configuration
showhidden=false; dirsfirst=true; ignorecase=true; semaphore=false;
showhidden=false; dirsfirst=true; ignorecase=true; semaphore=false; showtree=true; showlocation=true;
tree_width = default_tree_width;
dirtree->init();
if (argc==1) // No params
loaddir ("");
else

View File

@ -1,3 +1,14 @@
/*
* $Id$
*
* EFiler - EDE File Manager
* Part of Equinox Desktop Environment (EDE).
* Copyright (c) 2000-2006 EDE Authors.
*
* This program is licenced under terms of the
* GNU General Public Licence version 2 or newer.
* See COPYING for details.
*/
// This file implements copy / move / delete operation with files
// NOT TO BE CONFUSED WITH edelib::File.h !!!
@ -11,11 +22,15 @@
#include <Fl/Fl_Window.H>
#include <Fl/Fl_Progress.H>
#include <Fl/Fl_Button.H>
#include <Fl/Fl_Menu_Button.H>
#include <Fl/filename.H>
#include <Fl/fl_ask.H>
#include <edelib/File.h>
#include <edelib/Nls.h>
#include <edelib/String.h>
#include <edelib/StrUtil.h>
#include <edelib/MimeType.h>
#include "EDE_FileView.h"
@ -26,61 +41,74 @@ Fl_Progress* cut_copy_progress;
bool stop_now;
bool overwrite_all, skip_all;
char **cut_copy_buffer = 0;
bool operation_is_copy = false;
enum {
CUT,
COPY,
ASK
} operation = ASK;
// Some helper function
// -- is last character a / (doesn't call stat)?
bool my_isdir(const char* path) {
return (path[strlen(path)-1]=='/');
}
// Execute cut or copy operation when List View is active
// -- wrapper around fl_filename_name() that also works with directories
const char* my_filename_name(const char* path) {
if (!my_isdir(path)) { fprintf (stderr, "Blah\n"); return fl_filename_name(path); }
static char buffer[FL_PATH_MAX];
strncpy(buffer, path, FL_PATH_MAX);
buffer[strlen(path)-1]='\0';
return fl_filename_name(buffer);
}
// Execute cut or copy operation
void do_cut_copy(bool m_copy) {
// Count selected icons, for malloc
int num = view->size();
int nselected = 0;
for (int i=1; i<=num; i++)
if (view->selected(i)) {
nselected++;
// Can not cut/copy the up directory button (..)
const char* tmp = view->text(i);
if (tmp[0]=='.' && tmp[1]=='.' && (tmp[2]=='\0' || tmp[2]==view->column_char())) return;
}
// Clear cut/copy buffer and optionally ungray the previously cutted icons
if (cut_copy_buffer) {
for (int i=0; cut_copy_buffer[i]; i++)
free(cut_copy_buffer[i]);
free(cut_copy_buffer);
if (!operation_is_copy) {
for (int i=1; i<=num; i++)
view->ungray(i);
}
}
if (m_copy) operation = COPY; else operation = CUT;
// Allocate buffer
cut_copy_buffer = (char**)malloc(sizeof(char*) * (nselected+2));
int bufsize=10000;
char* buf = (char*)malloc(sizeof(char)*bufsize);
buf[0]='\0';
// Add selected files to buffer and optionally grey icons (for cut effect)
int buf=0;
int nselected=0;
for (int i=1; i<=num; i++) {
view->ungray(i);
if (view->selected(i)==1) {
cut_copy_buffer[buf] = strdup((char*)view->data(i));
if (!m_copy) view->gray(i);
buf++;
char* t = (char*)view->path(i);
if (strncmp(t+strlen(t)-3, "/..", 3)==0) {
// Can't cut/copy the Up button ("..")
free(buf);
return;
}
char* p = (char*)view->path(i);
while (strlen(buf)+strlen(p) >= bufsize) {
bufsize+=10000;
buf = (char*)realloc(buf,sizeof(char)*bufsize);
}
if (buf[0] != '\0') strncat(buf, "\n", 1);
strncat(buf,p,strlen(p));
if (operation == CUT) view->gray(i);
nselected++;
}
}
if (buf==0) { //nothing selected, use the focused item
if (nselected==0) { //nothing selected, use the focused item
int i=view->get_focus();
cut_copy_buffer[buf] = strdup((char*)view->data(i));
if (!m_copy) view->gray(i);
buf++;
char* p = (char*)view->path(i);
// an individual path should never be longer > 10000 chars!
strncat(buf,p,strlen(p));
nselected=1;
}
cut_copy_buffer[buf] = 0;
operation_is_copy = m_copy;
Fl::copy(buf,strlen(buf),1); // use clipboard
free(buf);
// Deselect all
// Deselect all and restore focus
int focused = view->get_focus();
for (int i=1; i<=num; i++)
if (view->selected(i)) view->select(i,0);
@ -88,17 +116,16 @@ void do_cut_copy(bool m_copy) {
// Update statusbar
if (m_copy)
statusbar->label(tsprintf(_("Selected %d items for copying"), nselected));
statusbar->copy_label(tsprintf(_("Selected %d items for copying"), nselected));
else
statusbar->label(tsprintf(_("Selected %d items for moving"), nselected));
statusbar->copy_label(tsprintf(_("Selected %d items for moving"), nselected));
}
// Helper functions for paste:
// Copy single file. Returns true if operation should continue
// Note that at this point directories should be expanded into subdirectories etc.
// so when "copying" a directory we actually mean creating a new directory with same info
// so when "copying" a directory we actually mean creating a new directory
bool my_copy(const char* src, const char* dest) {
FILE *fold, *fnew;
int c;
@ -109,7 +136,7 @@ bool my_copy(const char* src, const char* dest) {
if (edelib::file_exists(dest)) {
// if both src and dest are directories, do nothing
if (fl_filename_isdir(src) && fl_filename_isdir(dest))
if (my_isdir(src) && my_isdir(dest))
return true;
int c = -1;
@ -125,28 +152,35 @@ bool my_copy(const char* src, const char* dest) {
// At this point either c==0 (overwrite) or overwrite_all == true
// copy directory over file
if (fl_filename_isdir(src))
if (my_isdir(src))
unlink(dest);
// copy file over directory
// TODO: we will just skip this case, but ideally there should be
// another warning
if (fl_filename_isdir(dest))
// TODO: we will just skip this case because it's "impossible",
// but maybe there should be another warning
if (my_isdir(dest))
return true;
}
if (fl_filename_isdir(src)) {
if (mkdir (dest, umask(0))==0)
if (my_isdir(src)) {
if (my_isdir(dest))
return true; // directory already exists, just continue copying
// try to preserve permissions
struct stat buf;
stat(src,&buf);
if (mkdir (dest, buf.st_mode)==0)
return true; // success
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot create directory %s"),dest), _("&Stop"), _("&Continue"), 0);
if (q == 0) return true; else return false;
int q = fl_choice(tsprintf(_("Cannot create directory %s (%d)"),dest,strerror(errno)), _("&Stop"), _("&Continue"), 0);
if (q == 0) return false; else return true;
}
/* Sorry, edelib funcs just don't cut it....
if ( !edelib::file_readable(src) ) {
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot read file %s"),src), _("&Stop"), _("&Continue"), 0);
if (q == 0) return true; else return false;
if (q == 0) return false; else return true;
}
// edelib::file_writeable() returns false if dest doesn't exist
@ -155,19 +189,57 @@ bool my_copy(const char* src, const char* dest) {
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot create file %s"),dest), _("&Stop"), _("&Continue"), 0);
if (q == 0) return true; else return false;
}*/
}
// we will try to preserve permissions etc. cause that's usually what people want
if (!edelib::file_copy(src,dest,true))
fl_alert(tsprintf(_("Error copying %s to %s"),src,dest));
fl_alert(tsprintf(_("Error copying %s to %s"),src,dest)); */
if ( ( fold = fopen( src, "rb" ) ) == NULL ) {
int q = fl_choice(tsprintf(_("Cannot read file\n\t%s\n%s"), src, strerror(errno)), _("&Stop"), _("&Continue"), 0);
if (q == 0) return false; else return true;
}
if ( ( fnew = fopen( dest, "wb" ) ) == NULL )
{
fclose ( fold );
int q = fl_choice(tsprintf(_("Cannot create file\n\t%s\n%s"), dest, strerror(errno)), _("&Stop"), _("&Continue"), 0);
if (q == 0) return false; else return true;
}
int count=0;
while (!feof(fold)) {
c = fgetc(fold);
fputc(c, fnew);
// Update interface
if (count++ > 1000) {
count=0;
Fl::check();
}
if (stop_now) break; // this will leave a half-sized file...
}
if (ferror(fold)) {
fclose(fold); // don't flush error buffer before time
fclose(fnew);
int q = fl_choice(tsprintf(_("Error while reading file\n\t%s\n%s"), src, strerror(errno)), _("&Stop"), _("&Continue"), 0);
if (q == 0) return false; else return true;
}
if (ferror(fnew)) {
fclose(fold); // don't flush error buffer before time
fclose(fnew);
int q = fl_choice(tsprintf(_("Error while writing file\n\t%s\n%s"), dest, strerror(errno)), _("&Stop"), _("&Continue"), 0);
if (q == 0) return false; else return true;
}
fclose(fold);
fclose(fnew);
// fclose(fold);
// fclose(fnew);
return true;
}
// Recursive function that creates a list of all files to be copied, such that
// Recursive function that creates a list of all files to be copied. All
// directories are opened and all files and subdirectories etc. are added
// separately to the list. The total number of elements will be stored in
// listsize. Returns false if user decided to interrupt copying.
@ -177,11 +249,10 @@ bool expand_dirs(const char* src, char** &list, int &list_size, int &list_capaci
list_capacity += 1000;
list = (char**)realloc(list, sizeof(char**)*list_capacity);
}
// We add both files and directories to list
list[list_size++] = strdup(src);
if (fl_filename_isdir(src)) {
if (my_isdir(src)) { // fl_filename_list makes sure that directories are appended with /
char new_src[PATH_MAX];
dirent **files;
// FIXME: use same sort as used in view
@ -209,201 +280,77 @@ void stop_copying_cb(Fl_Widget*,void* v) {
caption->parent()->redraw();
}
// Execute paste operation - this will copy or move files based on chosen
// operation
void do_paste() {
if (!cut_copy_buffer || !cut_copy_buffer[0]) return;
overwrite_all=false; skip_all=false;
// Moving files on same filesystem is trivial, just like rename
// We don't even need a progress bar
if (!operation_is_copy && is_on_same_fs(current_dir, cut_copy_buffer[0])) {
for (int i=0; cut_copy_buffer[i]; i++) {
char *newname;
asprintf(&newname, "%s%s", current_dir, fl_filename_name(cut_copy_buffer[i]));
if (edelib::file_exists(newname)) {
int c = -1;
if (!overwrite_all && !skip_all) {
// here was choice_alert
c = fl_choice(tsprintf(_("File already exists: %s. What to do?"), newname), _("&Overwrite"), _("Over&write all"), _("&Skip"), _("Skip &all")); // * means default
}
if (c==1) overwrite_all=true;
if (c==3) skip_all=true;
if (c==2 || skip_all) {
free(cut_copy_buffer[i]);
continue; // go to next entry
}
// At this point c==0 (Overwrite) or overwrite_all == true
unlink(newname);
}
rename(cut_copy_buffer[i],newname);
free(cut_copy_buffer[i]);
free(newname);
}
free(cut_copy_buffer);
cut_copy_buffer=0;
loaddir(current_dir); // Update display
return;
//
// Real file moving / copying using recursive algorithm
//
} else {
stop_now = false;
// Create srcdir string
char *srcdir = strdup(cut_copy_buffer[0]);
char *p = strrchr(srcdir,'/');
if (*(p+1) == '\0') { // slash is last - find one before
*p = '\0';
p = strrchr(srcdir,'/');
}
*(p+1) = '\0';
if (strcmp(srcdir,current_dir)==0) {
fl_alert(_("You cannot copy a file onto itself!"));
free(srcdir);
return;
}
// Draw progress dialog
Fl_Window* progress_window = new Fl_Window(350,150);
if (operation_is_copy)
progress_window->label(_("Copying files"));
else
progress_window->label(_("Moving files"));
progress_window->set_modal();
progress_window->begin();
Fl_Box* caption = new Fl_Box(20,20,310,25, _("Counting files in directories"));
caption->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
cut_copy_progress = new Fl_Progress(20,60,310,20);
Fl_Button* stop_button = new Fl_Button(145,100,60,40, _("&Stop"));
stop_button->callback(stop_copying_cb,caption);
progress_window->end();
progress_window->show();
// How many items do we copy?
int copy_items=0;
for (; cut_copy_buffer[copy_items]; copy_items++);
// Set ProgressBar range accordingly
cut_copy_progress->minimum(0);
cut_copy_progress->maximum(copy_items);
cut_copy_progress->value(0);
// Count files in directories
int list_size = 0, list_capacity = 1000;
char** files_list = (char**)malloc(sizeof(char**)*list_capacity);
for (int i=0; i<copy_items; i++) {
// We must ensure that cut_copy_buffer is deallocated
// even if user clicked on Stop
if (!stop_now) expand_dirs(cut_copy_buffer[i], files_list, list_size, list_capacity);
free(cut_copy_buffer[i]);
cut_copy_progress->value(i+1);
Fl::check(); // check to see if user pressed Stop
}
free (cut_copy_buffer);
cut_copy_buffer=0;
// Now copying those files
if (!stop_now) {
char label[150];
char dest[PATH_MAX];
cut_copy_progress->minimum(0);
cut_copy_progress->maximum(list_size);
cut_copy_progress->value(0);
for (int i=0; i<list_size; i++) {
// Prepare dest filename
char *srcfile = files_list[i] + strlen(srcdir);
snprintf (dest, PATH_MAX, "%s%s", current_dir, srcfile);
if (operation_is_copy)
snprintf(label, 150, _("Copying %d of %d files to %s"), i, list_size, current_dir);
else
snprintf(label, 150, _("Moving %d of %d files to %s"), i, list_size, current_dir);
caption->label(label);
caption->redraw();
if (stop_now || !my_copy(files_list[i], dest))
break;
// Delete file after moving
if (!operation_is_copy) unlink(files_list[i]);
cut_copy_progress->value(cut_copy_progress->value()+1);
Fl::check(); // check to see if user pressed Stop
}
}
progress_window->hide();
// Reload current dir
loaddir(current_dir);
// select the just pasted files and cleanup memory
for (int i=0; i<list_size; i++) {
char* tmp = strrchr(files_list[i],'/')+1;
// if (!tmp) { fprintf (stderr, "not found\n"); continue; }
// tmp++;
for (int j=1; j<=view->size(); j++)
if (strncmp(tmp, view->text(j), strlen(tmp))==0)
view->select(j,1);
free(files_list[i]);
}
free(files_list);
free(srcdir);
}
}
// Delete currently selected file(s) or directory(es)
// Delete currently selected file(s) and directory(es)
void do_delete() {
// Count all selected item, expanding directories as neccessary
// TODO: Add a progress bar???
// Delete is, in my experience, *very* fast (don't know why Konqueror takes so long)
// Count all selected items, expanding directories as neccessary
int list_size = 0, list_capacity = 1000;
char** files_list = (char**)malloc(sizeof(char**)*list_capacity);
for (int i=1; i<=view->size(); i++)
if (view->selected(i)==1)
expand_dirs((char*)view->data(i), files_list, list_size, list_capacity);
expand_dirs(view->path(i), files_list, list_size, list_capacity);
if (list_size==0) { //nothing selected, use the focused item
int i=view->get_focus();
expand_dirs((char*)view->data(i), files_list, list_size, list_capacity);
expand_dirs(view->path(i), files_list, list_size, list_capacity);
}
// Issue a warning
// here was choice_alert
int c;
if (list_size==1) {
char* k=strrchr(files_list[0],'/');
c = fl_choice(tsprintf(_("Are you sure that you want to delete file %s ?"), k+1), _("Do&n't delete"), _("&Delete"), 0);
if (list_size==1 && my_isdir(files_list[0])) {
c = fl_choice(tsprintf(_("Are you sure that you want to delete directory\n\t%s\nincluding everything in it?"), files_list[0]), _("Do&n't delete"), _("&Delete"), 0);
} else if (list_size==1) {
c = fl_choice(tsprintf(_("Are you sure that you want to delete file %s ?"), my_filename_name(files_list[0])), _("Do&n't delete"), _("&Delete"), 0);
} else
c = fl_choice(tsprintf(_("Are you sure that you want to delete %d files?"), list_size), _("Do&n't delete"), _("&Delete"), 0);
if (c!=1) return;
c = fl_choice(tsprintf(_("Are you sure that you want to delete %d files and directories?"), list_size), _("Do&n't delete"), _("&Delete"), 0);
// delete
for (int i=0; i<list_size; i++)
edelib::file_remove(files_list[i]);
// loaddir(current_dir); - optimized
for (int i=1; i<=view->size(); i++)
if (view->selected(i)==1)
view->remove(i);
if (c==1) {
// first remove files...
for (int i=0; i<list_size; i++)
if (!my_isdir(files_list[i]))
if (!edelib::file_remove(files_list[i]))
fl_alert(tsprintf(_("Couldn't delete file %s !\n%s"), fl_filename_name(files_list[i]), strerror(errno)));
// ...then directories
// since expand_dirs() returns first dirs then files, we should go in oposite direction
for (int i=list_size-1; i>=0; i--)
if (my_isdir(files_list[i]))
// if (!edelib::file_remove(files_list[i]))
// ^^ this apparently can't remove directories
if (remove(files_list[i])!=0)
fl_alert(tsprintf(_("Couldn't delete directory\n\t%s !\n%s"), files_list[i], strerror(errno)));
// refresh directory listing - optimized
for (int i=1; i<=view->size(); i++)
for (int j=0; j<list_size; j++)
if (strcmp(files_list[j],view->path(i))==0)
view->remove(i);
}
// Cleanup memory
for (int i=0; i<list_size; i++) free(files_list[i]);
free(files_list);
}
// Rename the file with focus to given name
// Rename the file that has focus to given name 'c'
void do_rename(const char* c) {
edelib::String oldname(current_dir);
edelib::String newname(current_dir);
int focus = view->get_focus();
edelib::String vline(view->text(focus)); // get filename
edelib::String vline(fl_filename_name(view->path(focus))); // get filename
oldname += vline.substr(0,vline.find(view->column_char(),0));
newname += c;
if (edelib::file_exists(newname.c_str()))
fl_alert(tsprintf(_("Filename already in use: %s"), newname.c_str()));
// For some reason edelib::file_rename() always fails
// For some reason, edelib::file_rename() always fails and returns false
// else if (!edelib::file_rename(oldname.c_str(),newname.c_str()))
// fl_alert(tsprintf(_("Rename %s to %s failed!"), oldname.c_str(), newname.c_str()));
else {
@ -415,55 +362,337 @@ void do_rename(const char* c) {
vline = newname.substr(j+1) + vline.substr(vline.find(view->column_char(),0));
view->text(focus,vline.c_str());
view->update_path(oldname.c_str(),newname.c_str());
}
}
// Drag & drop callback - mostly copied from do_cut_copy()
void dnd_cb(const char* from,const char* to) {
fprintf (stderr, "PASTE from '%s', to '%s'\n",from,to);
return;
char *t = (char*)to;
if (!fl_filename_isdir(to))
t=current_dir;
// Execute paste - this will copy or move files based on chosen operation
/*
// Clear cut/copy buffer and optionally ungray the previously cutted icons
if (cut_copy_buffer) {
for (int i=0; cut_copy_buffer[i]; i++)
free(cut_copy_buffer[i]);
free(cut_copy_buffer);
if (!operation_is_copy) {
for (int i=1; i<=num; i++)
view->ungray(i);
// FIXME: if user cuts some files, then does dnd from another window,
// do_paste will assume operation=CUT and won't ask the user!
void do_paste(const char* t) {
FileItem **item_list=0;
int item_list_size=0;
char *to = (char*)t;
if (!t || !fl_filename_isdir(t) || (strncmp(t+strlen(t)-3,"/..",3)==0))
to = current_dir;
fprintf (stderr, "PASTE from '%s', to '%s', type=%d\n",(char*)Fl::event_text(),to,operation);
if (!strchr(Fl::event_text(), '/'))
return; // User is pasting something that isn't files
// Tokenize event text into an array of strings ("from")
char* tmp = (char*)Fl::event_text();
int count=0;
do count++; while (tmp && (tmp = strchr(tmp+1,'\n')));
char** from = (char**)malloc(sizeof(char*) * count);
tmp = (char*)Fl::event_text();
char* tmp2;
int k=0;
do {
tmp2 = strchr(tmp,'\n');
int len=tmp2-tmp;
if (!tmp2) len=strlen(tmp);
from[k] = (char*)malloc(sizeof(char) * (len+2));
strncpy(from[k],tmp,len);
from[k][len]='\0';
// Paste from file managers with a vfs
if (strncmp(from[k],"file://",7)==0)
for (int i=0; i<=len-7; i++)
from[k][i]=from[k][i+7];
// All directories must end in /
if (fl_filename_isdir(from[k]) && (from[k][len-1] != '/')) {
from[k][len++]='/';
from[k][len]='\0';
}
fprintf (stderr, "from[%d]='%s'\n", k, from[k]);
k++;
tmp=tmp2+1;
} while (tmp2);
// Some sanity checks
for (int i=0; i<count; i++) {
// If this window is below others, try to get focus
win->take_focus();
if (strcmp(to,from[i])==0) {
//fl_alert(tsprintf(_("Can't copy directory\n\t%s\ninto itself."), to));
// This happens accidentally, so statusbar is less annoying
statusbar->copy_label(tsprintf(_("Can't copy directory %s into itself."), my_filename_name(to)));
goto FINISH;
}
if ((strncmp(to,from[i],strlen(to))==0)) {
char *k = strchr(from[i]+strlen(to), '/');
if (!k || *(k+1)=='\0') {
//fl_alert(tsprintf(_("File %s is already in directory\n\t%s"), from[i]+strlen(to), to));
statusbar->copy_label(tsprintf(_("File %s is already in directory %s"), from[i]+strlen(to), to));
goto FINISH;
}
}
}
// Allocate buffer
cut_copy_buffer = (char**)malloc(sizeof(char*) * (nselected+2));
// Add selected files to buffer
int buf=0;
for (int i=1; i<=num; i++)
if (view->selected(i)==1)
cut_copy_buffer[buf++] = strdup((char*)view->data(i));
// We don't know yet if this is cut or copy, so no need to gray anything
// Resolve operation
if (operation == ASK) {
// TODO make a popup menu just like in Konqueror
/* fprintf(stderr, "x=%d y=%d\n", Fl::event_x_root(),Fl::event_y_root());
Fl_Menu_Button* mb = new Fl_Menu_Button (Fl::event_x_root(),Fl::event_y_root(),0,0);
mb->box(FL_NO_BOX);
mb->type(Fl_Menu_Button::POPUP123);
mb->textsize(12); // hack for label size
mb->add(_("&Copy"),0,cb_dnd_copy);
mb->add(_("_&Move"),0,cb_dnd_cut);
mb->add(_("C&ancel"),0,0);
mb->popup();
goto FINISH;*/
//
int c;
if (count==1 && my_isdir(from[0]))
c = fl_choice(tsprintf(_("Copy or move directory\n\t%s\nto directory\n\t%s ?"), from[0], to), _("C&ancel"), _("&Copy"), _("&Move"));
else if (count==1)
c = fl_choice(tsprintf(_("Copy or move file %s to directory\n\t%s ?"), fl_filename_name(from[0]), to), _("C&ancel"), _("&Copy"), _("&Move"));
else
c = fl_choice(tsprintf(_("Copy or move file these %d files to directory\n\t%s ?"), count, to), _("C&ancel"), _("&Copy"), _("&Move"));
// Clear cut/copy buffer and optionally ungray the previously cutted icons
if (cut_copy_buffer) {
for (int i=0; cut_copy_buffer[i]; i++)
free(cut_copy_buffer[i]);
free(cut_copy_buffer);
if (!operation_is_copy) {
for (int i=1; i<=num; i++)
view->ungray(i);
if (c==0) goto FINISH;
if (c==1) operation=COPY; else operation=CUT;
}
overwrite_all=false; skip_all=false;
// Moving files on same filesystem is trivial, just like rename
// We don't even need a progress bar
// OTOH maybe the two branches should be merged, lots of common code?
if (operation == CUT && is_on_same_fs(to, from[0])) {
// This list is used for updating mimetypes
if (to==current_dir) item_list = new FileItem*[count];
for (int i=0; i<count; i++) {
char *dest;
asprintf(&dest, "%s%s", to, my_filename_name(from[i]));
if (edelib::file_exists(dest)) {
int c = -1;
if (!overwrite_all && !skip_all) {
// here was choice_alert
c = fl_choice(tsprintf(_("File with name\n\t%s\nalready exists. What to do?"), dest), _("&Overwrite"), _("Over&write all"), _("&Skip"), _("Skip &all"));
}
if (c==1) overwrite_all=true;
if (c==3) skip_all=true;
if (c==2 || skip_all) continue; // go to next entry
// At this point c==0 (Overwrite) or overwrite_all == true
unlink(dest);
} else if (to==current_dir) {
// Update interface - add file to list
FileItem *item = new FileItem;
item->name = fl_filename_name(from[i]);
item->realpath = dest;
if (fl_filename_isdir(dest)) {
item->icon = "folder";
item->description = "Directory";
// item->name += "/";
} else {
item->icon = "unknown";
item->description = "Unknown";
}
item_list[item_list_size++] = item;
view->add(item); // don't bother with sorting, that would be too complex
}
rename(from[i],dest);
free(dest);
}
//
// Real file moving / copying using recursive algorithm
//
} else {
stop_now = false;
// Create srcdir string
char *srcdir = strdup(from[0]);
char *p = strrchr(srcdir,'/');
if (*(p+1) == '\0') { // slash is last character - find one before
*p = '\0';
p = strrchr(srcdir,'/');
}
*(p+1) = '\0';
if (strcmp(srcdir,to)==0) {
// This should never happen cause we already checked it...
fl_alert(_("You cannot copy a file onto itself!"));
free(srcdir);
goto FINISH;
}
// Draw progress dialog
Fl_Window* progress_window = new Fl_Window(350,150);
if (operation == COPY)
progress_window->label(_("Copying files"));
else
progress_window->label(_("Moving files"));
progress_window->set_modal();
progress_window->begin();
Fl_Box* caption = new Fl_Box(20,20,310,25, _("Counting files in directories"));
caption->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
cut_copy_progress = new Fl_Progress(20,60,310,20);
Fl_Button* stop_button = new Fl_Button(145,100,60,40, _("&Stop"));
stop_button->callback(stop_copying_cb,caption);
progress_window->end();
progress_window->show();
caption->labelsize(12); // hack for label size
stop_button->labelsize(12);
// Set ProgressBar range
cut_copy_progress->minimum(0);
cut_copy_progress->maximum(count);
cut_copy_progress->value(0);
// Count files in directories
int list_size = 0, list_capacity = 1000;
char** files_list = (char**)malloc(sizeof(char**)*list_capacity);
for (int i=0; i<count; i++) {
if (!stop_now) expand_dirs(from[i], files_list, list_size, list_capacity);
cut_copy_progress->value(i+1);
Fl::check(); // check to see if user pressed Stop
}
if (stop_now) { // user pressed stop while counting, cleanup and exit
for (int i=0; i<list_size; i++) free(files_list[i]);
free(files_list);
goto FINISH;
}
// Now copying those files
char dest[PATH_MAX];
cut_copy_progress->minimum(0);
cut_copy_progress->maximum(list_size);
cut_copy_progress->value(0);
// This list is used for updating mimetypes
if (to==current_dir) item_list = new FileItem*[list_size];
for (int i=0; i<list_size; i++) {
// Prepare dest filename
char *srcfile = files_list[i] + strlen(srcdir);
snprintf (dest, PATH_MAX, "%s%s", to, srcfile);
if (operation == COPY)
caption->copy_label(tsprintf(_("Copying %d of %d files to %s"), i, list_size, to));
else
caption->copy_label(tsprintf(_("Moving %d of %d files to %s"), i, list_size, to));
caption->redraw();
// Check if it already exists
if (edelib::file_exists(dest)) {
int c = -1;
if (!overwrite_all && !skip_all) {
// here was choice_alert
c = fl_choice(tsprintf(_("File with name\n\t%s\nalready exists. What to do?"), dest), _("&Overwrite"), _("Over&write all"), _("&Skip"), _("Skip &all"));
}
if (c==1) overwrite_all=true;
if (c==3) skip_all=true;
if (c==2 || skip_all) continue; // go to next entry
// At this point c==0 (Overwrite) or overwrite_all == true
unlink(dest);
} else if (to==current_dir) {
// Update interface - add file to list
FileItem *item = new FileItem;
item->name = my_filename_name(dest);
item->realpath = dest;
if (my_isdir(dest)) {
item->icon = "folder";
item->description = "Directory";
// item->name += "/";
} else {
item->icon = "unknown";
item->description = "Unknown";
}
item_list[item_list_size++] = item;
view->add(item); // don't bother with sorting, that would be too complex
}
if (stop_now || !my_copy(files_list[i], dest))
break;
// Delete file after moving
if (operation == CUT) unlink(files_list[i]);
cut_copy_progress->value(cut_copy_progress->value()+1);
Fl::check(); // check to see if user pressed Stop
}
progress_window->hide();
// Cleanup memory
for (int i=0; i<list_size; i++) free(files_list[i]);
free(files_list);
free(srcdir);
if (stop_now) { // User pressed Stop but we don't know when, so we'll just reload
loaddir(current_dir);
goto FINISH;
}
}
int c = fl_choice(tsprintf(_("Do you want to copy or move file %s to directory %s?"), k+1), _("Do&n't delete"), _("&Delete"), 0);*/
// Update interface in a smart way
// Remove cutted files
if (operation == CUT)
for (int i=0; i<count; i++)
for (int j=1; j<=view->size(); j++)
if (strcmp(from[i],view->path(j))==0)
view->remove(j);
// Update mimetypes for pasted files
for (int i=0; i<item_list_size; i++) {
struct stat buf;
if (stat(from[i],&buf)) { delete item_list[i]; continue; }
FileItem *item = item_list[i];
item->date = nice_time(buf.st_mtime);
item->permissions = ""; // todo
if (item->description != "Directory") {
item->size = nice_size(buf.st_size);
edelib::MimeType mt;
mt.set(from[i]);
edelib::String desc,icon;
desc = mt.comment();
// First letter of desc should be upper case:
if (desc.length()>0 && desc[0]>='a' && desc[0]<='z') desc[0] = desc[0]-'a'+'A';
icon = mt.icon_name();
if (desc!="" || icon!="") {
if (desc != "") item->description = desc;
if (icon != "") item->icon = icon;
}
Fl::check();
}
view->update(item);
delete item_list[i];
}
delete[] item_list;
// Select the just pasted files (they're at the bottom)
if (item_list_size>0) {
view->redraw();
for (int i=view->size(),j=0; j<count; i--,j++)
view->select(i,1);
}
// Cleanup memory and exit
FINISH:
for (int i=0; i<count; i++) free(from[i]);
free(from);
// Set operation for future dnd; do_cut_copy() will change this if
// classic cut or copy is used
operation=ASK;
}

View File

@ -1,3 +1,14 @@
/*
* $Id$
*
* EFiler - EDE File Manager
* Part of Equinox Desktop Environment (EDE).
* Copyright (c) 2000-2006 EDE Authors.
*
* This program is licenced under terms of the
* GNU General Public Licence version 2 or newer.
* See COPYING for details.
*/
// This file implements copy / move / delete operation with files
// NOT TO BE CONFUSED WITH edelib::File.h !!!
@ -12,19 +23,17 @@ void do_cut_copy(bool m_copy);
// Execute paste operation - this will copy or move files based on chosen
// operation
void do_paste();
void do_paste(const char*to = 0);
// Delete currently selected file(s) or directory(es)
void do_delete();
// Rename the file with focus to given name
// Rename the file that has focus to given name
void do_rename(const char*);
// Callback for drag&drop operations
void dnd_cb(const char*from,const char*to);
extern FileDetailsView* view;
extern Fl_Window* win;
extern Fl_Box* statusbar;
extern char current_dir[];
extern void loaddir(const char*);