~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to src/FbTk/TextButton.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// TextButton.cc for Fluxbox Window Manager
2
 
// Copyright (c) 2003 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot 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 3961 2005-04-27 15:15:36Z simonb $
23
 
 
24
 
#include "TextButton.hh"
25
 
#include "Font.hh"
26
 
#include "GContext.hh"
27
 
 
28
 
namespace FbTk {
29
 
 
30
 
TextButton::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_left_padding(0),
38
 
    m_right_padding(0) {
39
 
    setRenderer(*this);
40
 
 
41
 
}
42
 
 
43
 
void TextButton::resize(unsigned int width, unsigned int height) {
44
 
    if (this->width() == width && height == this->height())
45
 
        return;
46
 
 
47
 
    Button::resize(width, height);
48
 
}
49
 
 
50
 
void TextButton::moveResize(int x, int y,
51
 
                            unsigned int width, unsigned int height) {
52
 
    if (this->width() == width && height == this->height() &&
53
 
        x == this->x() && y == this->y())
54
 
        return;
55
 
 
56
 
    Button::moveResize(x, y, width, height);
57
 
}
58
 
 
59
 
void TextButton::setJustify(FbTk::Justify just) {
60
 
    m_justify = just;
61
 
}
62
 
 
63
 
void TextButton::setText(const std::string &text) {
64
 
    if (m_text != text) {
65
 
        m_text = text;
66
 
        updateBackground(false);
67
 
        clear();
68
 
    }
69
 
}
70
 
 
71
 
void TextButton::setFont(const FbTk::Font &font) {
72
 
    // no need to set new font if it's the same
73
 
    if (&font == m_font)
74
 
        return;
75
 
    m_font = &font;
76
 
}
77
 
 
78
 
/// set bevel and redraw text
79
 
void TextButton::setBevel(int bevel) {
80
 
    if (m_bevel == bevel)
81
 
        return;
82
 
    m_bevel = bevel;
83
 
}
84
 
 
85
 
void TextButton::setTextPaddingLeft(unsigned int leftpadding) {
86
 
    m_left_padding = leftpadding;
87
 
}
88
 
 
89
 
void TextButton::setTextPaddingRight(unsigned int rightpadding) {
90
 
    m_right_padding = rightpadding;
91
 
}
92
 
 
93
 
void TextButton::setTextPadding(unsigned int padding) {
94
 
    setTextPaddingLeft(padding/2);
95
 
    setTextPaddingRight(padding/2);
96
 
}
97
 
 
98
 
/// clear window and redraw text
99
 
void TextButton::clear() {
100
 
    TextButton::clearArea(0, 0,
101
 
                          width(), height());
102
 
}
103
 
 
104
 
void TextButton::clearArea(int x, int y,
105
 
                           unsigned int width, unsigned int height,
106
 
                           bool exposure) {
107
 
    Button::clearArea(x, y, width, height, exposure);
108
 
    if (backgroundPixmap() == ParentRelative) 
109
 
        drawText(0, 0, this);
110
 
}
111
 
 
112
 
unsigned int TextButton::textWidth() const {
113
 
    return font().textWidth(text().c_str(), text().size());
114
 
}
115
 
 
116
 
void TextButton::renderForeground(FbWindow &win, FbDrawable &drawable) {
117
 
    // (win should always be *this, no need to check)
118
 
    drawText(0, 0, &drawable);
119
 
}
120
 
 
121
 
void TextButton::drawText(int x_offset, int y_offset, FbDrawable *drawable) {
122
 
    unsigned int textlen = text().size();
123
 
    // do text alignment
124
 
 
125
 
    int align_x = FbTk::doAlignment(width() - x_offset - m_left_padding - m_right_padding,
126
 
                                    bevel(),
127
 
                                    justify(),
128
 
                                    font(),
129
 
                                    text().c_str(), text().size(),
130
 
                                    textlen); // return new text lne
131
 
 
132
 
    // center text by default
133
 
    int center_pos = height()/2 + font().ascent()/2 - 1;
134
 
 
135
 
    if (drawable == 0)
136
 
        drawable = this;
137
 
 
138
 
    font().drawText(*drawable,
139
 
                    screenNumber(),
140
 
                    gc(), // graphic context
141
 
                    text().c_str(), textlen, // string and string size
142
 
                    align_x + x_offset + m_left_padding, center_pos + y_offset); // position
143
 
    
144
 
}
145
 
 
146
 
void TextButton::exposeEvent(XExposeEvent &event) {
147
 
    clearArea(event.x, event.y, event.width, event.height, false);
148
 
}
149
 
 
150
 
}; // end namespace FbTk