diff options
-rw-r--r-- | src/FbTk/TextButton.cc | 96 | ||||
-rw-r--r-- | src/FbTk/TextButton.hh | 67 |
2 files changed, 163 insertions, 0 deletions
diff --git a/src/FbTk/TextButton.cc b/src/FbTk/TextButton.cc new file mode 100644 index 0000000..9518627 --- /dev/null +++ b/src/FbTk/TextButton.cc | |||
@@ -0,0 +1,96 @@ | |||
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.1 2003/08/18 12:15:39 fluxgen Exp $ | ||
23 | |||
24 | #include "TextButton.hh" | ||
25 | #include "Font.hh" | ||
26 | |||
27 | #include <iostream> | ||
28 | using namespace std; | ||
29 | |||
30 | namespace FbTk { | ||
31 | |||
32 | TextButton::TextButton(const FbTk::FbWindow &parent, | ||
33 | const FbTk::Font &font, | ||
34 | const std::string &text): | ||
35 | FbTk::Button(parent, 0, 0, 10, 10), | ||
36 | m_font(&font), | ||
37 | m_text(text), | ||
38 | m_justify(FbTk::LEFT), m_bevel(1) { | ||
39 | |||
40 | } | ||
41 | |||
42 | void TextButton::setJustify(FbTk::Justify just) { | ||
43 | m_justify = just; | ||
44 | } | ||
45 | |||
46 | void TextButton::setText(const std::string &text) { | ||
47 | m_text = text; | ||
48 | } | ||
49 | |||
50 | void TextButton::setFont(const FbTk::Font &font) { | ||
51 | // no need to set new font if it's the same | ||
52 | if (&font == m_font) | ||
53 | return; | ||
54 | m_font = &font; | ||
55 | clear(); // redraw text with new font | ||
56 | } | ||
57 | |||
58 | /// set bevel and redraw text | ||
59 | void TextButton::setBevel(int bevel) { | ||
60 | if (m_bevel == bevel) | ||
61 | return; | ||
62 | m_bevel = bevel; | ||
63 | } | ||
64 | |||
65 | /// clear window and redraw text | ||
66 | void TextButton::clear() { | ||
67 | FbTk::Button::clear(); | ||
68 | drawText(); | ||
69 | } | ||
70 | |||
71 | unsigned int TextButton::textWidth() const { | ||
72 | return font().textWidth(text().c_str(), text().size()); | ||
73 | } | ||
74 | |||
75 | void TextButton::drawText(int x_offset, int y_offset) { | ||
76 | unsigned int textlen = text().size(); | ||
77 | // do text alignment | ||
78 | int align_x = FbTk::doAlignment(width() - x_offset, | ||
79 | bevel(), | ||
80 | justify(), | ||
81 | font(), | ||
82 | text().c_str(), text().size(), | ||
83 | textlen // return new text len | ||
84 | ); | ||
85 | |||
86 | // center text by default | ||
87 | int center_pos = height()/2 + font().ascent()/2; | ||
88 | |||
89 | font().drawText(window(), // drawable | ||
90 | screenNumber(), | ||
91 | gc(), // graphic context | ||
92 | text().c_str(), textlen, // string and string size | ||
93 | align_x + x_offset, center_pos + y_offset); // position | ||
94 | } | ||
95 | |||
96 | }; // end namespace FbTk | ||
diff --git a/src/FbTk/TextButton.hh b/src/FbTk/TextButton.hh new file mode 100644 index 0000000..fa1149d --- /dev/null +++ b/src/FbTk/TextButton.hh | |||
@@ -0,0 +1,67 @@ | |||
1 | // TextButton.hh 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.hh,v 1.1 2003/08/18 12:15:38 fluxgen Exp $ | ||
23 | |||
24 | #ifndef FBTK_TEXTBUTTON_HH | ||
25 | #define FBTK_TEXTBUTTON_HH | ||
26 | |||
27 | #include "Button.hh" | ||
28 | #include "Text.hh" | ||
29 | |||
30 | #include <string> | ||
31 | |||
32 | namespace FbTk { | ||
33 | |||
34 | class Font; | ||
35 | |||
36 | /// Displays a text on a button | ||
37 | class TextButton: public FbTk::Button { | ||
38 | public: | ||
39 | TextButton(const FbTk::FbWindow &parent, | ||
40 | const FbTk::Font &font, const std::string &text); | ||
41 | |||
42 | void setJustify(FbTk::Justify just); | ||
43 | void setText(const std::string &text); | ||
44 | void setFont(const FbTk::Font &font); | ||
45 | void setBevel(int bevel); | ||
46 | /// clears window and redraw text | ||
47 | void clear(); | ||
48 | |||
49 | inline FbTk::Justify justify() const { return m_justify; } | ||
50 | inline const std::string &text() const { return m_text; } | ||
51 | inline const FbTk::Font &font() const { return *m_font; } | ||
52 | unsigned int textWidth() const; | ||
53 | int bevel() const { return m_bevel; } | ||
54 | |||
55 | protected: | ||
56 | void drawText(int x_offset = 0, int y_offset = 0); | ||
57 | |||
58 | private: | ||
59 | const FbTk::Font *m_font; | ||
60 | std::string m_text; | ||
61 | FbTk::Justify m_justify; | ||
62 | int m_bevel; | ||
63 | }; | ||
64 | |||
65 | }; // end namespace FbTk | ||
66 | |||
67 | #endif // FBTK_TEXTBUTTON_HH | ||