5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
11 #include "ygtktextview.h"
18 YGTextView (YWidget *ywidget, YWidget *parent,
const std::string &label,
bool editable)
20 YGTK_TYPE_TEXT_VIEW,
"wrap-mode", GTK_WRAP_WORD_CHAR,
21 "editable", editable, NULL)
23 setPolicy (GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
25 connect (getBuffer(),
"changed", G_CALLBACK (text_changed_cb),
this);
28 GtkTextBuffer* getBuffer()
29 {
return gtk_text_view_get_buffer (GTK_TEXT_VIEW (getWidget())); }
32 {
return gtk_text_buffer_get_char_count (getBuffer()); }
34 void setCharsNb (
int max_chars)
37 if (maxChars != -1 && getCharsNb() > maxChars)
38 truncateText (maxChars);
41 void truncateText(
int pos)
44 GtkTextIter start_it, end_it;
45 gtk_text_buffer_get_iter_at_offset (getBuffer(), &start_it, pos);
46 gtk_text_buffer_get_end_iter (getBuffer(), &end_it);
47 gtk_text_buffer_delete (getBuffer(), &start_it, &end_it);
50 void setText (
const std::string &text)
53 gtk_text_buffer_set_text (getBuffer(), text.c_str(), -1);
56 void appendText (
const std::string &text)
59 gtk_text_buffer_get_end_iter (getBuffer(), &end_it);
60 gtk_text_buffer_insert (getBuffer(), &end_it, text.c_str(), -1);
65 GtkTextIter start_it, end_it;
66 gtk_text_buffer_get_bounds (getBuffer(), &start_it, &end_it);
68 gchar* text = gtk_text_buffer_get_text (getBuffer(), &start_it, &end_it, FALSE);
69 std::string str (text);
77 YGUtils::scrollWidget (gtk_scrollable_get_vadjustment(GTK_SCROLLABLE (getWidget())),
false);
79 GtkTextBuffer *buffer = getBuffer();
81 gtk_text_buffer_get_end_iter (buffer, &iter);
82 gtk_text_iter_set_line_offset (&iter, 0);
84 GtkTextMark *mark = gtk_text_buffer_get_mark (buffer,
"scroll");
86 gtk_text_buffer_move_mark (buffer, mark, &iter);
88 mark = gtk_text_buffer_create_mark (buffer,
"scroll", &iter, TRUE);
90 GtkTextView *view = GTK_TEXT_VIEW (getWidget());
91 gtk_text_view_scroll_mark_onscreen (view, mark);
96 static void text_changed_cb (GtkTextBuffer *buffer,
YGTextView *pThis)
98 if (pThis->maxChars != -1 && pThis->getCharsNb() > pThis->maxChars) {
99 pThis->truncateText (pThis->maxChars);
100 gtk_widget_error_bell (pThis->getWidget());
102 pThis->emitEvent (YEvent::ValueChanged);
106 #include "YMultiLineEdit.h"
112 : YMultiLineEdit (NULL, label)
117 virtual void setValue (
const std::string &text)
118 { YGTextView::setText (text); }
120 virtual std::string value()
121 {
return YGTextView::getText(); }
123 virtual void setInputMaxLength (
int nb)
125 YGTextView::setCharsNb (nb);
126 YMultiLineEdit::setInputMaxLength (nb);
131 virtual unsigned int getMinSize (YUIDimension dim)
133 if (dim == YD_VERT) {
134 int height = YGUtils::getCharsHeight (getWidget(), defaultVisibleLines());
135 return MAX (10, height);
140 YGLABEL_WIDGET_IMPL (YMultiLineEdit)
143 YMultiLineEdit *YGWidgetFactory::createMultiLineEdit (YWidget *parent,
const std::string &label)
148 #include "YLogView.h"
155 YGLogView (YWidget *parent,
const std::string &label,
int visibleLines,
int maxLines)
156 : YLogView (NULL, label, visibleLines, maxLines)
161 virtual void displayLogText (
const std::string &text)
164 if (text.empty())
return;
166 if (text.compare (0, m_text.size(), m_text) == 0) {
167 if (text.size() == m_text.size())
return;
170 GtkAdjustment *vadj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE (getWidget()));
171 bool autoScroll = gtk_adjustment_get_value(vadj) >= gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
173 std::string diff (text.substr (m_text.size()));
174 YGTextView::appendText (diff);
176 YGTextView::scrollToBottom();
180 YGTextView::setText (text);
181 YGTextView::scrollToBottom();
187 virtual unsigned int getMinSize (YUIDimension dim)
189 if (dim == YD_VERT) {
190 int height = YGUtils::getCharsHeight (getWidget(), visibleLines());
191 return MAX (80, height);
196 YGLABEL_WIDGET_IMPL (YLogView)
199 YLogView *YGWidgetFactory::createLogView (YWidget *parent,
const std::string &label,
200 int visibleLines,
int maxLines)
202 return new YGLogView (parent, label, visibleLines, maxLines);
205 #include "YRichText.h"
206 #include "ygtkhtmlwrap.h"
211 YGRichText (YWidget *parent,
const std::string &text,
bool plainText)
212 : YRichText (NULL, text, plainText)
215 ygtk_html_wrap_init (getWidget());
216 ygtk_html_wrap_connect_link_clicked (getWidget(), link_clicked_cb,
this);
217 setText (text, plainText);
220 void setPlainText (
const std::string &text)
222 ygtk_html_wrap_set_text (getWidget(), text.c_str(), TRUE);
225 void setRichText (
const std::string &text)
227 #if 0 // current done at the XHTML treatment level, we may want to enable
229 std::string text (_text);
230 std::string productName = YUI::app()->productName();
231 YGUtils::replace (text,
"&product;", 9, productName.c_str());
233 ygtk_html_wrap_set_text (getWidget(), text.c_str(), FALSE);
236 void scrollToBottom()
238 ygtk_html_wrap_scroll (getWidget(), FALSE);
241 void setText (
const std::string &text,
bool plain_mode)
243 plain_mode ? setPlainText (text) : setRichText (text);
244 if (autoScrollDown())
249 virtual void setValue (
const std::string &text)
251 YRichText::setValue (text);
252 setText (text, plainTextMode());
255 virtual void setAutoScrollDown (
bool on)
257 YRichText::setAutoScrollDown (on);
258 if (on) scrollToBottom();
261 virtual void setPlainTextMode (
bool plain_mode)
263 YRichText::setPlainTextMode (plain_mode);
264 if (plain_mode != plainTextMode())
265 setText (value(), plain_mode);
269 static void link_clicked_cb (GtkWidget *widget,
const char *url, gpointer data)
270 { YGUI::ui()->sendEvent (
new YMenuEvent (url)); }
273 virtual unsigned int getMinSize (YUIDimension dim)
274 {
return shrinkable() ? 10 : 100; }
277 void activateLink(
const std::string & url )
279 YGUI::ui()->sendEvent(
new YMenuEvent( url ) );
283 YGWIDGET_IMPL_COMMON (YRichText)
287 YRichText *YGWidgetFactory::createRichText (YWidget *parent,
const std::string &text,
290 return new YGRichText (parent, text, plainTextMode);