mirror of
https://github.com/edeproject/ede.git
synced 2023-08-10 21:13:03 +03:00
- Beggining of dnd support
- Click on selected item to rename - Fix problems reported by Valgrind
This commit is contained in:
@ -40,86 +40,10 @@ struct FileItem {
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
/*// Event handler for EditBox
|
||||
int EditBox::handle(int event) {
|
||||
if (!this->visible()) return parent()->handle(event);
|
||||
bool above=false;
|
||||
//fprintf(stderr,"Editbox event: %d (%s)\n",event,event_name(event));
|
||||
|
||||
// Change filename
|
||||
if (event==KEY && (event_key()==ReturnKey || event_key()==KeypadEnter)) {
|
||||
// split old filename to path and file
|
||||
char path[PATH_MAX], file[PATH_MAX];
|
||||
strcpy(path, (char*)editing_->user_data());
|
||||
if (path[strlen(path)-1] == '/') path[strlen(path)-1]='\0';
|
||||
char *p = strrchr(path,'/');
|
||||
if (p==0 || *p=='\0') {
|
||||
strcpy(file,path);
|
||||
path[0]='\0';
|
||||
} else { // usual case
|
||||
p++;
|
||||
strcpy(file,p);
|
||||
*p='\0';
|
||||
}
|
||||
|
||||
if (strlen(file)!=strlen(text()) || strcmp(file,text())!=0) {
|
||||
// Create new filename
|
||||
strncat(path, text(), PATH_MAX-strlen(path));
|
||||
char oldname[PATH_MAX];
|
||||
strcpy(oldname, (char*)editing_->user_data());
|
||||
if (rename(oldname, path) == -1) {
|
||||
alert(tsprintf(_("Could not rename file! Error was:\n\t%s"), strerror(errno)));
|
||||
} else {
|
||||
// Update browser
|
||||
free(editing_->user_data());
|
||||
editing_->user_data(strdup(path));
|
||||
const char* l = editing_->label();
|
||||
editing_->label(tasprintf("%s%s",text(),strchr(l, '\t')));
|
||||
}
|
||||
}
|
||||
|
||||
above=true;
|
||||
}
|
||||
|
||||
// Hide editbox
|
||||
if (above || ( event==KEY && event_key()==EscapeKey ) ) {
|
||||
this->hide();
|
||||
return 1;
|
||||
}
|
||||
Input::handle(event);
|
||||
}
|
||||
|
||||
|
||||
// We override hide method to ensure certain things done
|
||||
void EditBox::hide() {
|
||||
Input::hide();
|
||||
// Remove box so it doesn't get in the way
|
||||
this->x(0);
|
||||
this->y(0);
|
||||
this->w(0);
|
||||
this->h(0);
|
||||
// Return the browser item into "visible" state
|
||||
if (editing_) {
|
||||
editing_->textcolor(textcolor());
|
||||
editing_->redraw();
|
||||
parent()->take_focus();
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Type for rename_callback
|
||||
// I don't know how to do this without creating a new type :(
|
||||
typedef void (my_callback)(const char*);
|
||||
typedef void (rename_callback_type)(const char*);
|
||||
typedef void (paste_callback_type)(const char*,const char*);
|
||||
|
||||
|
||||
|
||||
@ -164,17 +88,20 @@ private:
|
||||
|
||||
return 1; // don't send keys to view
|
||||
}
|
||||
if (e==FL_MOUSEWHEEL && visible()) view->hide_editbox();
|
||||
return Fl_Input::handle(e);
|
||||
}
|
||||
}* editbox_;
|
||||
int editbox_row;
|
||||
|
||||
my_callback* rename_callback_;
|
||||
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;
|
||||
if (row<1 || row>size()) return; // nothing selected
|
||||
if (strcmp(text(row), "..")==0) return; // can't rename "go up" button
|
||||
if (text(row)[0]=='.' && text(row)[1]=='.' && text(row)[2]==column_char()) return; // can't rename "go up" button
|
||||
|
||||
// unselect the row with focus - it's prettier that way
|
||||
select(row,0);
|
||||
@ -229,7 +156,37 @@ private:
|
||||
//hide_editbox();
|
||||
}
|
||||
|
||||
|
||||
// Bucket class is used to prevent memleaks
|
||||
// It stores pointers to allocated memory that will be cleaned up later
|
||||
// Just remember to call empty() when needed - everything else is automatic :)
|
||||
class Bucket {
|
||||
void** items;
|
||||
int size, capacity;
|
||||
public:
|
||||
Bucket() : size(0), capacity(1000), items((void**)malloc(sizeof(void*)*1000)) {
|
||||
for (int i=0; i<capacity; i++) items[i]=0;
|
||||
}
|
||||
~Bucket() { empty(); free(items); }
|
||||
void add(void* p) {
|
||||
if (size>=capacity) {
|
||||
capacity+=1000;
|
||||
items = (void**)realloc(items,sizeof(void*)*capacity);
|
||||
for (int i=capacity-1000; i<capacity; i++)
|
||||
items[i]=0;
|
||||
}
|
||||
items[size++]=p;
|
||||
}
|
||||
void empty() {
|
||||
for (int i=0; i<size; i++) {
|
||||
if (items[i]) free(items[i]);
|
||||
items[i]=0;
|
||||
}
|
||||
size=0;
|
||||
}
|
||||
void debug() {
|
||||
fprintf(stderr, "Bucket size %d, capacity %d\n", size, capacity);
|
||||
}
|
||||
} bucket;
|
||||
|
||||
public:
|
||||
FileDetailsView(int X, int Y, int W, int H, char*label=0) : EDE_Browser(X,Y,W,H,label) {
|
||||
@ -255,6 +212,9 @@ public:
|
||||
editbox_->parent(this);
|
||||
editbox_->textsize(12); // FIXME: hack for font size
|
||||
editbox_->hide();
|
||||
|
||||
rename_callback_ = 0;
|
||||
dnd_callback_ = 0;
|
||||
}
|
||||
// ~FileDetailsView() { delete browser; }
|
||||
|
||||
@ -262,7 +222,9 @@ public:
|
||||
// Construct browser line
|
||||
edelib::String value;
|
||||
value = item->name+"\t"+item->description+"\t"+item->size+"\t"+item->date+"\t"+item->permissions;
|
||||
EDE_Browser::insert(row, value.c_str(), strdup(item->realpath.c_str())); // put realpath into data
|
||||
char* realpath = strdup(item->realpath.c_str());
|
||||
EDE_Browser::insert(row, value.c_str(), realpath); // put realpath into data
|
||||
bucket.add(realpath);
|
||||
fprintf (stderr, "value: %s\n", value.c_str());
|
||||
|
||||
// Get icon
|
||||
@ -278,24 +240,42 @@ fprintf (stderr, "value: %s\n", value.c_str());
|
||||
int row = findrow(item->realpath);
|
||||
if (row) EDE_Browser::remove(row);
|
||||
}
|
||||
void remove(int row) { EDE_Browser::remove(row); } // why???
|
||||
void update(FileItem *item) {
|
||||
int row=findrow(item->realpath);
|
||||
if (row==0) return;
|
||||
EDE_Browser::remove(row);
|
||||
insert(row, item);
|
||||
// FIXME: this will lose focus, making it impossible to click on something while
|
||||
|
||||
//EDE_Browser::remove(row);
|
||||
//insert(row, item);
|
||||
// this was reimplemented because a) it's unoptimized, b) adds stuff 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());
|
||||
text(row, value.c_str());
|
||||
data(row, realpath);
|
||||
bucket.add(realpath);
|
||||
fprintf (stderr, "value: %s\n", value.c_str());
|
||||
|
||||
// Get icon
|
||||
edelib::String icon = edelib::IconTheme::get(item->icon.c_str(),edelib::ICON_SIZE_TINY);
|
||||
if (icon=="") icon = edelib::IconTheme::get("misc",edelib::ICON_SIZE_TINY,edelib::ICON_CONTEXT_MIMETYPE);
|
||||
set_icon(row, Fl_Shared_Image::get(icon.c_str()));
|
||||
}
|
||||
|
||||
// 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))+4); // add 4 places for format chars
|
||||
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';
|
||||
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
|
||||
text(row,ntext);
|
||||
free(ntext);
|
||||
bucket.add(ntext);
|
||||
|
||||
// grey icon - but how to ungray?
|
||||
Fl_Image* im = get_icon(row)->copy();
|
||||
@ -310,7 +290,7 @@ fprintf (stderr, "value: %s\n", value.c_str());
|
||||
char *ntext = (char*)malloc(sizeof(char)*strlen(text(row))-4); // 4 places for format chars
|
||||
strncpy(ntext, text(row)+4, strlen(text(row))-4);
|
||||
text(row,ntext);
|
||||
free(ntext);
|
||||
bucket.add(ntext);
|
||||
|
||||
// don't work
|
||||
//Fl_Image* im = get_icon(row);
|
||||
@ -319,8 +299,8 @@ fprintf (stderr, "value: %s\n", value.c_str());
|
||||
//redraw(); // OPTIMIZE
|
||||
}
|
||||
|
||||
// Renaming support
|
||||
int handle(int e) {
|
||||
// Rename support
|
||||
if (e==FL_KEYBOARD) {
|
||||
if (Fl::event_key()==FL_F+2) {
|
||||
if (editbox_->visible())
|
||||
@ -330,12 +310,101 @@ fprintf (stderr, "value: %s\n", value.c_str());
|
||||
}
|
||||
}
|
||||
if (e==FL_PUSH && editbox_->visible() && !Fl::event_inside(editbox_))
|
||||
hide_editbox(); // hide editbox when click outside it
|
||||
hide_editbox(); // hide editbox when user clicks outside of it
|
||||
if (e==FL_MOUSEWHEEL && editbox_->visible())
|
||||
hide_editbox(); // hide editbox when scrolling mouse
|
||||
|
||||
// Click once on item that is already selected AND focused to rename it
|
||||
static bool renaming=false;
|
||||
if (e==FL_PUSH) renaming=false;
|
||||
if (e==FL_PUSH && !editbox_->visible() && Fl::event_clicks()==0 && Fl::event_button()==1) {
|
||||
const int* l = column_widths();
|
||||
if (Fl::event_x()<x() || Fl::event_x()>x()+l[0])
|
||||
return Fl_Icon_Browser::handle(e); // we're only interested in first column
|
||||
|
||||
void* item = item_first();
|
||||
int focusy=y()-position();
|
||||
for (int i=1; i<get_focus(); i++) {
|
||||
focusy+=item_height(item);
|
||||
if (focusy>Fl::event_y()) break;
|
||||
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
|
||||
if (selected(get_focus())!=1)
|
||||
return Fl_Icon_Browser::handle(e); // allow to select item if it's just focused
|
||||
|
||||
renaming=true;
|
||||
}
|
||||
if (e==FL_RELEASE && renaming && Fl::event_clicks()==0) {
|
||||
show_editbox(get_focus());
|
||||
renaming=false;
|
||||
return 1; // don't pass mouse event, otherwise item will become selected again
|
||||
}
|
||||
|
||||
// Drag&drop support
|
||||
static int paste_event_y;
|
||||
|
||||
/*--- 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;
|
||||
}
|
||||
/*--- End ugly hack ---*/
|
||||
|
||||
// Don't unselect on FL_PUSH cause that could be dragging
|
||||
if (e==FL_PUSH && Fl::event_clicks()!=1) return 1;
|
||||
|
||||
if (e==FL_DRAG) {
|
||||
edelib::String selected_items;
|
||||
for (int i=1; i<=size(); i++)
|
||||
if (selected(i)==1) {
|
||||
if (selected_items != "") selected_items += ",";
|
||||
selected_items += (char*)data(i);
|
||||
}
|
||||
Fl::copy(selected_items.c_str(),selected_items.length(),0);
|
||||
Fl::dnd();
|
||||
return 1; // don't do the multiple selection thing from Fl_Browser
|
||||
}
|
||||
|
||||
if (e==FL_DND_RELEASE) {
|
||||
paste_event_y=Fl::event_y();
|
||||
Fl::paste(*this,0);
|
||||
}
|
||||
if (e==FL_PASTE) {
|
||||
if (!Fl::event_text() || !Fl::event_length()) return 1;
|
||||
|
||||
// Where is the user dropping?
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
// Setup callback that will be used when renaming
|
||||
void rename_callback(my_callback* cb) { rename_callback_ = cb; }
|
||||
// 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; }
|
||||
|
||||
// Avoid memory leak
|
||||
void clear() {
|
||||
fprintf(stderr, "Call FileView::clear()\n");
|
||||
bucket.empty();
|
||||
EDE_Browser::clear();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user