00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "Button.hh"
00025
00026 #include "Command.hh"
00027 #include "EventManager.hh"
00028 #include "App.hh"
00029
00030 namespace FbTk {
00031
00032 Button::Button(int screen_num, int x, int y,
00033 unsigned int width, unsigned int height):
00034 FbWindow(screen_num, x, y, width, height,
00035 ExposureMask | ButtonPressMask | ButtonReleaseMask),
00036 m_foreground_pm(0),
00037 m_background_pm(0),
00038 m_pressed_pm(0),
00039 m_pressed_color("black", screen_num),
00040 m_gc(DefaultGC(FbTk::App::instance()->display(), screen_num)),
00041 m_pressed(false) {
00042
00043
00044 FbTk::EventManager::instance()->add(*this, *this);
00045 }
00046
00047 Button::Button(const FbWindow &parent, int x, int y,
00048 unsigned int width, unsigned int height):
00049 FbWindow(parent, x, y, width, height,
00050 ExposureMask | ButtonPressMask | ButtonReleaseMask),
00051 m_foreground_pm(0),
00052 m_background_pm(0),
00053 m_pressed_pm(0),
00054 m_pressed_color("black", parent.screenNumber()),
00055 m_gc(DefaultGC(FbTk::App::instance()->display(), screenNumber())),
00056 m_pressed(false) {
00057
00058 FbTk::EventManager::instance()->add(*this, *this);
00059 }
00060
00061 Button::~Button() {
00062
00063 }
00064
00065 void Button::setOnClick(RefCount<Command> &cmd, int button) {
00066
00067 if (button > 5 || button == 0)
00068 return;
00069
00070 m_onclick[button - 1] = cmd;
00071 }
00072
00073 void Button::setPixmap(Pixmap pm) {
00074 m_foreground_pm = pm;
00075 }
00076
00077 void Button::setPressedPixmap(Pixmap pm) {
00078 m_pressed_pm = pm;
00079 }
00080
00081 void Button::setPressedColor(const FbTk::Color &color) {
00082 m_pressed_color = color;
00083 }
00084
00085 void Button::setBackgroundColor(const Color &color) {
00086 m_background_pm = 0;
00087 m_background_color = color;
00088 FbTk::FbWindow::setBackgroundColor(color);
00089 }
00090
00091 void Button::setBackgroundPixmap(Pixmap pm) {
00092 m_background_pm = pm;
00093 FbTk::FbWindow::setBackgroundPixmap(pm);
00094 }
00095
00096 void Button::buttonPressEvent(XButtonEvent &event) {
00097 if (m_pressed_pm != 0)
00098 FbWindow::setBackgroundPixmap(m_pressed_pm);
00099 else if (m_pressed_color.isAllocated())
00100 FbWindow::setBackgroundColor(m_pressed_color);
00101
00102 m_pressed = true;
00103 clear();
00104 updateTransparent();
00105 }
00106
00107 void Button::buttonReleaseEvent(XButtonEvent &event) {
00108 m_pressed = false;
00109 if (m_background_pm)
00110 setBackgroundPixmap(m_background_pm);
00111 else
00112 setBackgroundColor(m_background_color);
00113
00114 clear();
00115
00116 if (m_foreground_pm) {
00117 Display *disp = App::instance()->display();
00118
00119 if (m_gc == 0)
00120 m_gc = DefaultGC(disp, screenNumber());
00121
00122 XCopyArea(disp, m_foreground_pm, window(), m_gc, 0, 0, width(), height(), 0, 0);
00123 }
00124
00125 updateTransparent();
00126
00127
00128 if (event.button > 0 && event.button <= 5 &&
00129 event.x > 0 && event.x < static_cast<signed>(width()) &&
00130 event.y > 0 && event.y < static_cast<signed>(height()) &&
00131 m_onclick[event.button -1].get() != 0)
00132 m_onclick[event.button - 1]->execute();
00133
00134
00135 }
00136
00137 void Button::exposeEvent(XExposeEvent &event) {
00138 if (m_background_pm)
00139 setBackgroundPixmap(m_background_pm);
00140 else
00141 setBackgroundColor(m_background_color);
00142
00143 clearArea(event.x, event.y, event.width, event.height);
00144 updateTransparent(event.x, event.y, event.width, event.height);
00145 }
00146
00147 };