aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/TextButton.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/TextButton.cc')
-rw-r--r--src/FbTk/TextButton.cc154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/FbTk/TextButton.cc b/src/FbTk/TextButton.cc
new file mode 100644
index 0000000..de8fad0
--- /dev/null
+++ b/src/FbTk/TextButton.cc
@@ -0,0 +1,154 @@
1// TextButton.cc for Fluxbox Window Manager
2// Copyright (c) 2003 Henrik Kinnunen (fluxgen[at]fluxbox.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: TextButton.cc,v 1.4 2003/10/31 20:02:49 rathnor Exp $
23
24#include "TextButton.hh"
25#include "Font.hh"
26#include "GContext.hh"
27
28namespace FbTk {
29
30TextButton::TextButton(const FbTk::FbWindow &parent,
31 const FbTk::Font &font,
32 const std::string &text):
33 FbTk::Button(parent, 0, 0, 10, 10),
34 m_font(&font),
35 m_text(text),
36 m_justify(FbTk::LEFT), m_bevel(1),
37 m_buffer(drawable(), width(), height(), depth()) {
38
39}
40
41void TextButton::resize(unsigned int width, unsigned int height) {
42 m_buffer.resize(width, height);
43
44 if (backgroundPixmap() != ParentRelative)
45 FbWindow::setBackgroundPixmap(m_buffer.drawable());
46 Button::resize(width, height);
47}
48
49void TextButton::moveResize(int x, int y,
50 unsigned int width, unsigned int height) {
51 m_buffer.resize(width, height);
52
53 if (backgroundPixmap() != ParentRelative)
54 FbWindow::setBackgroundPixmap(m_buffer.drawable());
55 Button::moveResize(x, y, width, height);
56}
57
58void TextButton::setJustify(FbTk::Justify just) {
59 m_justify = just;
60}
61
62void TextButton::setText(const std::string &text) {
63 m_text = text;
64}
65
66void TextButton::setFont(const FbTk::Font &font) {
67 // no need to set new font if it's the same
68 if (&font == m_font)
69 return;
70 m_font = &font;
71}
72
73/// set bevel and redraw text
74void TextButton::setBevel(int bevel) {
75 if (m_bevel == bevel)
76 return;
77 m_bevel = bevel;
78}
79
80/// clear window and redraw text
81void TextButton::clear() {
82 TextButton::clearArea(0, 0,
83 width(), height());
84}
85
86void TextButton::clearArea(int x, int y,
87 unsigned int width, unsigned int height,
88 bool exposure) {
89 if (backgroundPixmap() != ParentRelative) {
90
91 if (backgroundPixmap()) {
92 m_buffer.copyArea(backgroundPixmap(),
93 gc(),
94 x, y,
95 x, y,
96 width, height);
97
98 } else { // fill with background color
99 FbTk::GContext gc(drawable());
100 gc.setForeground(backgroundColor());
101 m_buffer.fillRectangle(gc.gc(),
102 x, y,
103 width, height);
104 }
105
106 drawText();
107
108 setBufferPixmap(m_buffer.drawable());
109 FbWindow::setBackgroundPixmap(m_buffer.drawable());
110 updateTransparent(x, y, width, height);
111
112 FbWindow::clearArea(x, y, width, height, exposure);
113
114 } else { // parent relative
115 FbWindow::setBufferPixmap(0);
116 FbWindow::setBackgroundPixmap(backgroundPixmap());
117 Button::clearArea(x, y, width, height, exposure);
118 updateTransparent(x, y, width, height);
119 drawText();
120 }
121
122
123
124}
125
126unsigned int TextButton::textWidth() const {
127 return font().textWidth(text().c_str(), text().size());
128}
129
130void TextButton::drawText(int x_offset, int y_offset) {
131 unsigned int textlen = text().size();
132 // do text alignment
133 int align_x = FbTk::doAlignment(width() - x_offset,
134 bevel(),
135 justify(),
136 font(),
137 text().c_str(), text().size(),
138 textlen); // return new text len
139
140 // center text by default
141 int center_pos = height()/2 + font().ascent()/2 - 1;
142
143 font().drawText(backgroundPixmap() == ParentRelative ? window() : m_buffer.drawable(),
144 screenNumber(),
145 gc(), // graphic context
146 text().c_str(), textlen, // string and string size
147 align_x + x_offset, center_pos + y_offset); // position
148}
149
150void TextButton::exposeEvent(XExposeEvent &event) {
151 clearArea(event.x, event.y, event.width, event.height, false);
152}
153
154}; // end namespace FbTk