eiconman based on fltk1. Most of the stuff is

revised from scratch.
This commit is contained in:
Sanel Zukan
2007-06-18 14:18:35 +00:00
parent 95a3034827
commit 202b4ffb29
13 changed files with 563 additions and 1136 deletions

View File

@ -12,63 +12,50 @@
#include "DesktopIcon.h"
#include "eiconman.h"
#include "Utils.h"
#include <FL/Fl.h>
#include <FL/fl_draw.h>
#include <FL/Fl_Shared_Image.h>
#include <FL/x.h>
#include <edelib/Debug.h>
#include <edelib/IconTheme.h>
#include <edelib/Nls.h>
#include <edelib/Item.h>
#include <edelib/Ask.h>
#include <fltk/SharedImage.h>
#include <fltk/Color.h>
#include <fltk/Divider.h>
#include <fltk/draw.h>
#include <fltk/events.h>
#include <fltk/damage.h>
#include <X11/extensions/shape.h>
#include <fltk/x11.h> // Pixmap
// fuck !
#define Window XWindow
#include <X11/extensions/shape.h>
#undef Window
// default icon size
// minimal icon size
#define ICONSIZE 48
// spaces around box in case of large/small icons
#define OFFSET_W 16
#define OFFSET_H 16
void icon_delete_cb(fltk::Widget*, void* di) {
DesktopIcon* dicon = (DesktopIcon*)di;
edelib::ask(_("Delete '%s' ?"), dicon->label());
}
// label offset from icon y()+h(), so selection box can be drawn nicely
#define LABEL_OFFSET 2
DesktopIcon::DesktopIcon(GlobalIconSettings* gisett, IconSettings* isett) :
fltk::Widget(isett->x, isett->y, ICONSIZE, ICONSIZE), settings(NULL)
{
EASSERT(gisett != NULL); EASSERT(isett != NULL);
DesktopIcon::DesktopIcon(GlobalIconSettings* gs, IconSettings* is, int bg) :
Fl_Button(is->x, is->y, ICONSIZE, ICONSIZE) {
EASSERT(gs != NULL);
lwidth = lheight = 0;
focus = false;
micon = false;
image_w = image_h = ICONSIZE;
micon = NULL;
/*
* GlobalIconSettings is shared from desktop so we only
* reference it. On other hand IconSettings is not shared
* and we must construct a copy from given parameter
*/
globals = gisett;
globals = gs;
settings = new IconSettings;
settings->name = isett->name;
settings->cmd = isett->cmd;
settings->icon = isett->icon;
settings->type = isett->type;
settings->key_name= isett->key_name;
settings->name = is->name;
settings->cmd = is->cmd;
settings->icon = is->icon;
settings->type = is->type;
settings->key_name= is->key_name;
// x,y are not needed since x(), y() are filled with it
@ -77,83 +64,52 @@ DesktopIcon::DesktopIcon(GlobalIconSettings* gisett, IconSettings* isett) :
if(!settings->icon.empty()) {
const char* nn = settings->icon.c_str();
edelib::IconSizes isize = edelib::ICON_SIZE_MEDIUM;
edelib::String ipath = edelib::IconTheme::get(nn, isize);
edelib::String ipath = edelib::IconTheme::get(nn, edelib::ICON_SIZE_MEDIUM);
Fl_Image* img = Fl_Shared_Image::get(ipath.c_str());
if(img) {
int img_w = img->w();
int img_h = img->h();
/*
* Resize box according to size from IconSizes.
*
* Here is avoided image()->measure() since looks like
* that function messes up icon's alpha blending if is widget
* resizes (but not icon). This is a bug in fltk.
*/
if(isize >= ICONSIZE) {
image_w = isize;
image_h = isize;
// resize box if icon is larger
if(img_w > ICONSIZE || img_h > ICONSIZE)
size(img_w + OFFSET_W, img_h + OFFSET_H);
resize(image_w + OFFSET_W, image_h + OFFSET_H);
}
nn = ipath.c_str();
image(fltk::SharedImage::get(nn));
image(img);
} else
EDEBUG(ESTRLOC ": Unable to load %s\n", ipath.c_str());
}
/*
EDEBUG(ESTRLOC ": Got label: %s\n", label());
EDEBUG(ESTRLOC ": Got image: %s\n", settings->icon.c_str());
EDEBUG(ESTRLOC ": Got x/y : %i %i\n", x(), y());
*/
//Use desktop color as color for icon background
color(bg);
align(fltk::ALIGN_WRAP);
align(FL_ALIGN_WRAP);
update_label_size();
pmenu = new fltk::PopupMenu(0, 0, 100, 100);
pmenu->begin();
if(settings->type == ICON_TRASH) {
edelib::Item* it = new edelib::Item(_("&Open"));
it->offset_x(12, 12);
it = new edelib::Item(_("&Properties"));
it->offset_x(12, 12);
new fltk::Divider();
it = new edelib::Item(_("&Empty"));
it->offset_x(12, 12);
} else {
edelib::Item* it = new edelib::Item(_("&Open"));
it->offset_x(12, 12);
it = new edelib::Item(_("&Rename"));
it->offset_x(12, 12);
it = new edelib::Item(_("&Delete"));
it->offset_x(12, 12);
it->callback(icon_delete_cb, this);
new fltk::Divider();
it = new edelib::Item(_("&Properties"));
it->offset_x(12, 12);
}
pmenu->end();
pmenu->type(fltk::PopupMenu::POPUP3);
}
DesktopIcon::~DesktopIcon()
{
if(settings) {
EDEBUG(ESTRLOC ": IconSettings clearing from ~DesktopIcon()\n");
delete settings;
}
DesktopIcon::~DesktopIcon() {
EDEBUG("DesktopIcon::~DesktopIcon()\n");
if(settings)
delete settings;
if(micon)
delete micon;
if(pmenu)
delete pmenu;
}
void DesktopIcon::update_label_size(void) {
lwidth = globals->label_maxwidth;
lheight= 0;
// FIXME: really needed ???
fltk::setfont(labelfont(), labelsize());
/*
* make sure current font size/type is set (internaly to fltk)
* so fl_measure() can correctly calculate label width and height
*/
fl_font(labelfont(), labelsize());
//fltk::measure(label(), lwidth, lheight, fltk::ALIGN_WRAP);
fltk::measure(label(), lwidth, lheight, flags());
fl_measure(label(), lwidth, lheight, align());
lwidth += 8;
lheight += 4;
@ -172,7 +128,7 @@ void DesktopIcon::drag(int x, int y, bool apply) {
if(apply) {
position(micon->x(), micon->y());
delete micon;
micon = 0;
micon = NULL;
}
}
@ -180,7 +136,7 @@ void DesktopIcon::drag(int x, int y, bool apply) {
int DesktopIcon::drag_icon_x(void) {
if(!micon)
return x();
else
else
return micon->x();
}
@ -192,130 +148,94 @@ int DesktopIcon::drag_icon_y(void) {
return micon->y();
}
void DesktopIcon::draw(void) {
if(image() && (damage() & fltk::DAMAGE_ALL)) {
fltk::Image* ii = (fltk::Image*)image();
int ix = (w()/2) - (ii->w()/2);
/*
* Make sure to pickup image's w() and h() since if fltk is compiled
* with Xrender it will be scaled to rectangle's w()/h().
*
* On other hand, if fltk is _not_ compiled with Xrender,
* scaling will not be done.
* Yuck !
*/
fltk::Rectangle ir(ix, 5, ii->w(), ii->h());
ii->draw(ir);
void DesktopIcon::fast_redraw(void) {
EASSERT(parent() != NULL && "Impossible !");
// LABEL_OFFSET + 2 include selection box line height too
parent()->damage(FL_DAMAGE_ALL, x(), y(), w(), h() + lheight + LABEL_OFFSET + 2);
}
void DesktopIcon::draw(void) {
//draw_box(FL_UP_BOX, FL_BLACK);
if(image() && (damage() & FL_DAMAGE_ALL)) {
Fl_Image* im = image();
// center image in the box
int ix = (w()/2) - (im->w()/2);
int iy = (h()/2) - (im->h()/2);
ix += x();
iy += y();
im->draw(ix, iy);
}
if(globals->label_draw && (damage() & (fltk::DAMAGE_ALL | fltk::DAMAGE_CHILD_LABEL))) {
int X = w()-(w()/2)-(lwidth/2);
int Y = h()+2;
if(globals->label_draw && (damage() & (FL_DAMAGE_ALL))) {
int X = x() + w()-(w()/2)-(lwidth/2);
int Y = y() + h() + LABEL_OFFSET;
Fl_Color old = fl_color();
if(!globals->label_transparent) {
fltk::setcolor(globals->label_background);
fltk::fillrect(X, Y, lwidth, lheight);
fl_color(globals->label_background);
fl_rectf(X, Y, lwidth, lheight);
}
Rectangle r(X, Y, lwidth, lheight);
fltk::setcolor(globals->label_foreground);
drawtext(label(), r, flags());
fl_color(globals->label_foreground);
fl_draw(label(), X, Y, lwidth, lheight, align(), 0, 0);
if(is_focused()) {
/*
* draw focused box on our way so later
* this can be used to draw customised boxes
*/
fltk::line_style(fltk::DOT);
fl_line_style(FL_DOT);
fltk::push_matrix();
fl_color(globals->label_foreground);
//fltk::setcolor(fltk::WHITE);
fltk::setcolor(globals->label_foreground);
fltk::addvertex(X,Y);
fltk::addvertex(X+lwidth,Y);
fltk::addvertex(X+lwidth,Y+lheight);
fltk::addvertex(X,Y+lheight);
fltk::addvertex(X,Y);
fltk::strokepath();
fltk::pop_matrix();
fl_push_matrix();
fl_begin_loop();
fl_vertex(X,Y);
fl_vertex(X+lwidth,Y);
fl_vertex(X+lwidth,Y+lheight);
fl_vertex(X,Y+lheight);
fl_vertex(X,Y);
fl_end_loop();
fl_pop_matrix();
// revert to default line style
fltk::line_style(0);
fl_line_style(0);
}
set_damage(damage() & ~fltk::DAMAGE_CHILD_LABEL);
// revert to old color whatever that be
fl_color(old);
}
}
int DesktopIcon::handle(int event) {
switch(event) {
case fltk::FOCUS:
case fltk::UNFOCUS:
return 1;
case fltk::ENTER:
case fltk::LEAVE:
return 1;
/*
* We have to handle fltk::MOVE too, if
* want to get only once fltk::ENTER when
* entered or fltk::LEAVE when leaved.
*/
case fltk::MOVE:
return 1;
case fltk::PUSH:
if(fltk::event_button() == 3)
pmenu->popup();
return 1;
case fltk::RELEASE:
if(fltk::event_clicks() > 0)
EDEBUG(ESTRLOC ": EXECUTE: %s\n", settings->cmd.c_str());
return 1;
case fltk::DND_ENTER:
EDEBUG("Icon DND_ENTER\n");
return 1;
case fltk::DND_DRAG:
EDEBUG("Icon DND_DRAG\n");
return 1;
case fltk::DND_LEAVE:
EDEBUG("Icon DND_LEAVE\n");
return 1;
case fltk::DND_RELEASE:
EDEBUG("Icon DND_RELEASE\n");
return 1;
case fltk::PASTE:
EDEBUG("Icon PASTE\n");
return 1;
}
return fltk::Widget::handle(event);
return Fl_Button::handle(event);
}
MovableIcon::MovableIcon(DesktopIcon* ic) : fltk::Window(ic->x(), ic->y(), ic->w(), ic->h()), icon(ic) {
MovableIcon::MovableIcon(DesktopIcon* ic) : Fl_Window(ic->x(), ic->y(), ic->w(), ic->h()), icon(ic) {
EASSERT(icon != NULL);
set_override();
create();
color(ic->color());
fltk::Image* img = icon->icon_image();
if(img)
image(img);
begin();
icon_box = new Fl_Box(0, 0, w(), h());
icon_box->image(ic->icon_image());
end();
}
MovableIcon::~MovableIcon() {
}
void MovableIcon::show(void) {
if(!shown())
Fl_X::make_xid(this);
#if 0
if(img) {
#ifdef SHAPE
Pixmap mask = create_pixmap_mask(img->width(), img->height());
XShapeCombineMask(fltk::xdisplay, fltk::xid(this), ShapeBounding, 0, 0, mask, ShapeSet);
#else
image(img);
#endif
}
Pixmap mask = create_mask((Fl_RGB_Image*)icon->icon_image());
XShapeCombineMask(fl_display, fl_xid(this), ShapeBounding, 0, 0, mask, ShapeSet);
#endif
}
MovableIcon::~MovableIcon()
{
}