You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TXButton.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. //
  19. // TXButton.h
  20. //
  21. // A TXButton is a clickable button with some text in it. The button must be
  22. // big enough to contain the text - if not then it will be resized
  23. // appropriately.
  24. //
  25. #ifndef __TXBUTTON_H__
  26. #define __TXBUTTON_H__
  27. #include "TXWindow.h"
  28. #include <rfb/util.h>
  29. // TXButtonCallback's buttonActivate() method is called when a button is
  30. // activated.
  31. class TXButton;
  32. class TXButtonCallback {
  33. public:
  34. virtual void buttonActivate(TXButton* button)=0;
  35. };
  36. class TXButton : public TXWindow, public TXEventHandler {
  37. public:
  38. TXButton(Display* dpy_, const char* text_, TXButtonCallback* cb_=0,
  39. TXWindow* parent_=0, int w=1, int h=1)
  40. : TXWindow(dpy_, w, h, parent_), cb(cb_), down(false),
  41. disabled_(false)
  42. {
  43. setEventHandler(this);
  44. setText(text_);
  45. gc = XCreateGC(dpy, win(), 0, 0);
  46. XSetFont(dpy, gc, defaultFont);
  47. addEventMask(ExposureMask | ButtonPressMask | ButtonReleaseMask);
  48. }
  49. virtual ~TXButton() {
  50. XFreeGC(dpy, gc);
  51. }
  52. // setText() changes the text in the button.
  53. void setText(const char* text_) {
  54. text = text_;
  55. int textWidth = XTextWidth(defaultFS, text.data(), text.size());
  56. int textHeight = (defaultFS->ascent + defaultFS->descent);
  57. int newWidth = __rfbmax(width(), textWidth + xPad*2 + bevel*2);
  58. int newHeight = __rfbmax(height(), textHeight + yPad*2 + bevel*2);
  59. if (width() < newWidth || height() < newHeight) {
  60. resize(newWidth, newHeight);
  61. }
  62. }
  63. // disabled() sets or queries the disabled state of the checkbox. A disabled
  64. // checkbox cannot be changed via the user interface.
  65. void disabled(bool b) { disabled_ = b; paint(); }
  66. bool disabled() { return disabled_; }
  67. private:
  68. void paint() {
  69. int tw = XTextWidth(defaultFS, text.data(), text.size());
  70. int startx = (width() - tw) / 2;
  71. int starty = (height() + defaultFS->ascent - defaultFS->descent) / 2;
  72. if (down || disabled_) {
  73. drawBevel(gc, 0, 0, width(), height(), bevel, defaultBg, darkBg,lightBg);
  74. startx++; starty++;
  75. } else {
  76. drawBevel(gc, 0, 0, width(), height(), bevel, defaultBg, lightBg,darkBg);
  77. }
  78. XSetForeground(dpy, gc, disabled_ ? disabledFg : defaultFg);
  79. XDrawString(dpy, win(), gc, startx, starty, text.data(), text.size());
  80. }
  81. virtual void handleEvent(TXWindow* /*w*/, XEvent* ev) {
  82. switch (ev->type) {
  83. case Expose:
  84. paint();
  85. break;
  86. case ButtonPress:
  87. if (!disabled_) {
  88. down = true;
  89. paint();
  90. }
  91. break;
  92. case ButtonRelease:
  93. if (!down) break;
  94. down = false;
  95. paint();
  96. if (ev->xbutton.x >= 0 && ev->xbutton.x < width() &&
  97. ev->xbutton.y >= 0 && ev->xbutton.y < height()) {
  98. if (cb) cb->buttonActivate(this);
  99. }
  100. break;
  101. }
  102. }
  103. GC gc;
  104. std::string text;
  105. TXButtonCallback* cb;
  106. bool down;
  107. bool disabled_;
  108. };
  109. #endif