ede/ede-conf/ede-conf.cpp

202 lines
4.9 KiB
C++
Raw Normal View History

2008-08-01 22:30:04 +04:00
/*
* $Id$
*
2008-09-22 17:31:20 +04:00
* ede-conf, a control panel for EDE
2008-08-01 22:30:04 +04:00
* Part of Equinox Desktop Environment (EDE).
* Copyright (c) 2008 EDE Authors.
*
* This program is licensed under terms of the
* GNU General Public License version 2 or newer.
* See COPYING for details.
*/
2008-09-22 17:31:20 +04:00
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/fl_draw.H>
2007-05-22 18:51:00 +04:00
#include <edelib/Config.h>
#include <edelib/StrUtil.h>
#include <edelib/Debug.h>
2008-08-01 22:30:04 +04:00
#include <edelib/ExpandableGroup.h>
#include <edelib/String.h>
#include <edelib/IconTheme.h>
2007-05-22 18:51:00 +04:00
#include <edelib/Nls.h>
2008-08-01 22:30:04 +04:00
#include <edelib/Window.h>
#include <edelib/MessageBox.h>
#include <edelib/Run.h>
#include <edelib/File.h>
2008-08-01 22:30:04 +04:00
typedef edelib::list<edelib::String> StrList;
typedef edelib::list<edelib::String>::iterator StrListIter;
bool file_can_execute(const edelib::String& f) {
if(edelib::file_executable(f.c_str()))
return true;
/* find full path then */
edelib::String fp = edelib::file_path(f.c_str());
if(fp.empty())
return false;
return edelib::file_executable(fp.c_str());
}
2008-08-01 22:30:04 +04:00
class ControlButton : public Fl_Button {
private:
Fl_Box* tipbox;
edelib::String tipstr;
edelib::String exec;
public:
ControlButton(Fl_Box* t, const edelib::String& ts, const edelib::String& e, int x, int y, int w, int h, const char* l = 0) :
Fl_Button(x, y, w, h, l), tipbox(t), tipstr(ts), exec(e) {
2008-08-01 22:30:04 +04:00
box(FL_FLAT_BOX);
align(FL_ALIGN_WRAP);
color(FL_BACKGROUND2_COLOR);
}
2008-08-01 22:30:04 +04:00
~ControlButton() { }
int handle(int event);
};
2007-05-22 18:51:00 +04:00
int ControlButton::handle(int event) {
switch(event) {
2007-06-15 15:05:44 +04:00
case FL_ENTER:
2008-08-01 22:30:04 +04:00
tipbox->label(tipstr.c_str());
2007-05-22 18:51:00 +04:00
return 1;
2007-06-15 15:05:44 +04:00
case FL_LEAVE:
2008-08-01 22:30:04 +04:00
tipbox->label("");
2007-05-22 18:51:00 +04:00
return 1;
2007-06-15 15:05:44 +04:00
case FL_PUSH:
2008-08-01 22:30:04 +04:00
box(FL_DOWN_BOX);
redraw();
if(Fl::event_clicks()) {
if(exec.empty())
edelib::alert(_("Unable to execute command for '%s'. Command value is not set"), label());
else if(!file_can_execute(exec.c_str()))
edelib::alert(_("Unable to run program '%s'. Program not found"), exec.c_str());
else
edelib::run_program(exec.c_str(), false);
}
2008-08-01 22:30:04 +04:00
return 0;
2007-06-15 15:05:44 +04:00
case FL_RELEASE:
2008-08-01 22:30:04 +04:00
box(FL_FLAT_BOX);
redraw();
return 0;
2007-05-22 18:51:00 +04:00
default:
2007-06-15 15:05:44 +04:00
return Fl_Button::handle(event);
2007-05-22 18:51:00 +04:00
}
2007-06-15 15:05:44 +04:00
return Fl_Button::handle(event);
}
2007-06-15 15:05:44 +04:00
void close_cb(Fl_Widget*, void* w) {
2008-08-01 22:30:04 +04:00
Fl_Window* win = (Fl_Window*)w;
win->hide();
}
2008-08-01 22:30:04 +04:00
void load_buttons(Fl_Group* g, Fl_Box* tipbox) {
edelib::Config c;
2008-09-22 17:31:20 +04:00
if(!c.load("ede-conf.conf")) {
2007-05-22 18:51:00 +04:00
EWARNING("Can't load config\n");
return;
}
2007-05-22 18:51:00 +04:00
char buff[1024];
if(!c.get("Control Panel", "Items", buff, sizeof(buff))) {
EWARNING("Can't find Items key\n");
return;
}
2008-08-01 22:30:04 +04:00
StrList spl;
// get sections
edelib::stringtok(spl, buff, ",");
2007-08-21 16:17:35 +04:00
if(spl.empty())
return;
2008-08-01 22:30:04 +04:00
bool abspath;
const char* section;
//ControlIcon cicon;
StrListIter it = spl.begin(), it_end = spl.end();
edelib::String name, tip, exec;
2007-08-21 16:17:35 +04:00
for(; it != it_end; ++it) {
2008-08-01 22:30:04 +04:00
section = (*it).c_str();
edelib::str_trim((char*)section);
2008-08-01 22:30:04 +04:00
if(c.get(section, "Name", buff, sizeof(buff)))
name = buff;
2007-05-22 18:51:00 +04:00
else {
2008-08-01 22:30:04 +04:00
EWARNING("No %s, skipping...\n", section);
2007-05-22 18:51:00 +04:00
continue;
}
2008-08-01 22:30:04 +04:00
if(c.get(section, "Tip", buff, sizeof(buff)))
tip = buff;
if(c.get(section, "Exec", buff, sizeof(buff)))
exec = buff;
ControlButton* cb = new ControlButton(tipbox, tip, exec, 0, 0, 80, 100);
2008-08-01 22:30:04 +04:00
cb->copy_label(name.c_str());
c.get(section, "IconPathAbsolute", abspath, false);
if(c.get(section, "Icon", buff, sizeof(buff))) {
if(abspath) {
Fl_Image* img = Fl_Shared_Image::get(buff);
cb->image(img);
} else {
edelib::String iconpath = edelib::IconTheme::get(buff, edelib::ICON_SIZE_LARGE);
if(!iconpath.empty())
cb->image(Fl_Shared_Image::get(iconpath.c_str()));
else
EWARNING("Can't find %s icon\n", buff);
}
2007-05-22 18:51:00 +04:00
}
2008-08-01 22:30:04 +04:00
g->add(cb);
}
}
2007-05-22 18:51:00 +04:00
2008-08-01 22:30:04 +04:00
int main() {
edelib::Window* win = new edelib::Window(455, 330, _("EDE Control Panel"));
win->init();
win->begin();
Fl_Group* titlegrp = new Fl_Group(0, 0, 455, 50);
2007-06-15 15:05:44 +04:00
titlegrp->box(FL_FLAT_BOX);
titlegrp->color(138);
2007-05-22 18:51:00 +04:00
titlegrp->begin();
2008-08-01 22:30:04 +04:00
Fl_Box* title = new Fl_Box(10, 10, 435, 30, win->label());
2007-06-15 15:05:44 +04:00
title->color(138);
2008-08-01 22:30:04 +04:00
title->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
2007-06-15 15:05:44 +04:00
title->labelcolor(23);
title->labelfont(FL_HELVETICA_BOLD);
2007-05-22 18:51:00 +04:00
title->labelsize(16);
titlegrp->end();
titlegrp->resizable(title);
2008-08-01 22:30:04 +04:00
edelib::ExpandableGroup* icons = new edelib::ExpandableGroup(10, 60, 435, 225);
2007-06-15 15:05:44 +04:00
icons->box(FL_DOWN_BOX);
icons->color(FL_BACKGROUND2_COLOR);
icons->end();
2007-05-22 18:51:00 +04:00
2008-08-01 22:30:04 +04:00
Fl_Box* tipbox = new Fl_Box(10, 295, 240, 25, _("Double click on desired item"));
2007-06-15 15:05:44 +04:00
tipbox->box(FL_FLAT_BOX);
2008-08-01 22:30:04 +04:00
tipbox->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE | FL_ALIGN_CLIP);
2007-05-22 18:51:00 +04:00
2008-08-01 22:30:04 +04:00
load_buttons(icons, tipbox);
2008-08-01 22:30:04 +04:00
// Fl_Button* options = new Fl_Button(260, 295, 90, 25, _("&Options"));
Fl_Button* close = new Fl_Button(355, 295, 90, 25, _("&Close"));
close->callback(close_cb, win);
Fl::focus(close);
2006-09-11 16:48:40 +04:00
2007-05-22 18:51:00 +04:00
// resizable invisible box
2008-08-01 22:30:04 +04:00
Fl_Box* rbox = new Fl_Box(10, 220, 120, 65);
win->resizable(rbox);
win->end();
win->show();
2007-06-15 15:05:44 +04:00
return Fl::run();
}