Debian Codegraph @

src/af/xap/gtk/xap_UnixWidget.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 /* AbiSource Application Framework
3 * Copyright (C) 2005,2011 Hubert Figuiere
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA.
19 */
20
21 #include <gtk/gtk.h>
22
23 #include "ut_assert.h"
24 #include "ut_debugmsg.h"
25 #include "ut_string.h"
26 #include "ut_std_string.h"
27 #include "ut_string_class.h"
28 #include "xap_UnixWidget.h"
29 #include "xap_Gtk2Compat.h"
30
31 XAP_UnixWidget::XAP_UnixWidget(GtkWidget * w)
32 : XAP_Widget()
33 , m_widget(w)
34 {
35 }
36
37 /** set the widget enabled/disabled state */
38 void XAP_UnixWidget::setState(bool enabled)
39 {
40 gtk_widget_set_sensitive(m_widget, enabled);
41 }
42
43
44 /** set the widget enabled/disabled state */
45 bool XAP_UnixWidget::getState(void)
46 {
47 return gtk_widget_get_sensitive(m_widget);
48 }
49
50 /** set the widget visible state */
51 void XAP_UnixWidget::setVisible(bool visible)
52 {
53 if (visible) {
54 gtk_widget_show(m_widget);
55 }
56 else {
57 gtk_widget_hide(m_widget);
58 }
59 }
60 /** get the widget visible state */
61 bool XAP_UnixWidget::getVisible(void)
62 {
63 return gtk_widget_get_visible(m_widget);
64 }
65
66 /** set the widget int value */
67 void XAP_UnixWidget::setValueInt(int val)
68 {
69 if (GTK_IS_TOGGLE_BUTTON(m_widget)) {
70 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), (val?TRUE:FALSE));
71 }
72 else if (GTK_IS_LABEL(m_widget)) {
73 std::string str = UT_std_string_sprintf("%d",val);
74 gtk_label_set_text(GTK_LABEL(m_widget), str.c_str());
75 }
76 else if (GTK_IS_ENTRY(m_widget)) {
77 std::string str = UT_std_string_sprintf("%d", val);
78 gtk_entry_set_text(GTK_ENTRY(m_widget), str.c_str());
79 }
80 else {
81 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
82 }
83 }
84
85 /** get the widget int value */
86 int XAP_UnixWidget::getValueInt(void)
87 {
88 if (GTK_IS_TOGGLE_BUTTON(m_widget)) {
89 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget));
90 }
91 else if (GTK_IS_ENTRY(m_widget)) {
92 return atoi(gtk_entry_get_text(GTK_ENTRY(m_widget)));
93 }
94 else {
95 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
96 }
97 return 0;
98 }
99
100
101 /** set the widget value as string */
102 void XAP_UnixWidget::setValueString(const UT_UTF8String &val)
103 {
104 if (GTK_IS_ENTRY(m_widget)) {
105 gtk_entry_set_text(GTK_ENTRY(m_widget), val.utf8_str());
106 }
107 else if (GTK_IS_LABEL(m_widget)) {
108 gtk_label_set_text(GTK_LABEL(m_widget), val.utf8_str());
109 }
110 else {
111 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
112 }
113 }
114
115 /** get the widget value as string */
116 void XAP_UnixWidget::getValueString(UT_UTF8String &val)
117 {
118 if (GTK_IS_ENTRY(m_widget)) {
119 val.assign(gtk_entry_get_text(GTK_ENTRY(m_widget)));
120 }
121 else if (GTK_IS_LABEL(m_widget)) {
122 val.assign(gtk_label_get_text(GTK_LABEL(m_widget)));
123 }
124 else {
125 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
126 }
127 }
128
129 /** set the widget value as float */
130 void XAP_UnixWidget::setValueFloat(float val)
131 {
132 if (GTK_IS_ENTRY(m_widget)) {
133 std::string str = UT_std_string_sprintf("%f", val);
134 gtk_entry_set_text(GTK_ENTRY(m_widget), str.c_str());
135 }
136 }
137
138 /** get the widget value as float */
139 float XAP_UnixWidget::getValueFloat(void)
140 {
141 UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED);
142 return 0;
143 }
144
145
146 /** set the widget label */
147 void XAP_UnixWidget::setLabel(const UT_UTF8String &val)
148 {
149 setLabelCStr(val.utf8_str());
150 }
151
152 void XAP_UnixWidget::setLabel(const std::string &val)
153 {
154 setLabelCStr(val.c_str());
155 }
156
157 void XAP_UnixWidget::setLabelCStr(const char* val)
158 {
159 if (GTK_IS_BUTTON(m_widget)) {
160 gtk_button_set_label(GTK_BUTTON(m_widget), val);
161 }
162 else if (GTK_IS_LABEL(m_widget)) {
163 if(!gtk_label_get_use_markup(GTK_LABEL(m_widget))) {
164 gtk_label_set_text(GTK_LABEL(m_widget), val);
165 }
166 else {
167 std::string markup = UT_std_string_sprintf(m_data.c_str(),
168 val);
169 gtk_label_set_label(GTK_LABEL(m_widget), markup.c_str());
170 }
171 }
172 else if (GTK_IS_WINDOW(m_widget)) {
173 gtk_window_set_title(GTK_WINDOW(m_widget), val);
174 }
175 else {
176 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
177 }
178 }