1 | /* rggladewindow.cc |
---|
2 | * |
---|
3 | * Copyright (c) 2003 Michael Vogt |
---|
4 | * |
---|
5 | * Author: Michael Vogt <mvo@debian.irg> |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License as |
---|
9 | * published by the Free Software Foundation; either version 2 of the |
---|
10 | * License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
20 | * USA |
---|
21 | */ |
---|
22 | |
---|
23 | |
---|
24 | #include <apt-pkg/fileutl.h> |
---|
25 | #include <cassert> |
---|
26 | #include "config.h" |
---|
27 | #include "i18n.h" |
---|
28 | #include "rggladewindow.h" |
---|
29 | |
---|
30 | |
---|
31 | /* the convention is that the top window is named: |
---|
32 | "window_$windowname" in your glade-source |
---|
33 | */ |
---|
34 | RGGladeWindow::RGGladeWindow(RGWindow *parent, string name, string mainName) |
---|
35 | { |
---|
36 | //std::cout << "RGGladeWindow::RGGladeWindow(parent,name)" << endl; |
---|
37 | |
---|
38 | _busyCursor = gdk_cursor_new(GDK_WATCH); |
---|
39 | |
---|
40 | // for development |
---|
41 | gchar *filename = NULL; |
---|
42 | gchar *main_widget = NULL; |
---|
43 | |
---|
44 | filename = g_strdup_printf("window_%s.glade", name.c_str()); |
---|
45 | if (mainName.empty()) |
---|
46 | main_widget = g_strdup_printf("window_%s", name.c_str()); |
---|
47 | else |
---|
48 | main_widget = g_strdup_printf("window_%s", mainName.c_str()); |
---|
49 | if (FileExists(filename)) { |
---|
50 | _gladeXML = glade_xml_new(filename, main_widget, NULL); |
---|
51 | } else { |
---|
52 | g_free(filename); |
---|
53 | filename = |
---|
54 | g_strdup_printf(SYNAPTIC_GLADEDIR "window_%s.glade", name.c_str()); |
---|
55 | _gladeXML = glade_xml_new(filename, main_widget, NULL); |
---|
56 | } |
---|
57 | assert(_gladeXML); |
---|
58 | _win = glade_xml_get_widget(_gladeXML, main_widget); |
---|
59 | |
---|
60 | gtk_window_set_position(GTK_WINDOW(_win), |
---|
61 | GTK_WIN_POS_CENTER_ON_PARENT); |
---|
62 | if(parent != NULL) |
---|
63 | gtk_window_set_transient_for(GTK_WINDOW(_win), |
---|
64 | GTK_WINDOW(parent->window())); |
---|
65 | |
---|
66 | assert(_win); |
---|
67 | g_free(filename); |
---|
68 | g_free(main_widget); |
---|
69 | |
---|
70 | //gtk_window_set_title(GTK_WINDOW(_win), (char *)name.c_str()); |
---|
71 | |
---|
72 | gtk_object_set_data(GTK_OBJECT(_win), "me", this); |
---|
73 | gtk_signal_connect(GTK_OBJECT(_win), "delete-event", |
---|
74 | (GtkSignalFunc) windowCloseCallback, this); |
---|
75 | _topBox = NULL; |
---|
76 | //gtk_widget_realize(_win); |
---|
77 | // if we have no parent, don't skip the taskbar hint |
---|
78 | int id = _config->FindI("Volatile::ParentWindowId", -1); |
---|
79 | if(_config->FindB("Volatile::HideMainwindow",false) && id < 0) |
---|
80 | { |
---|
81 | gtk_window_set_skip_taskbar_hint(GTK_WINDOW(_win), FALSE); |
---|
82 | gtk_window_set_urgency_hint(GTK_WINDOW(_win), TRUE); |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | bool RGGladeWindow::setLabel(const char *widget_name, const char *value) |
---|
88 | { |
---|
89 | GtkWidget *widget = glade_xml_get_widget(_gladeXML, widget_name); |
---|
90 | if (widget == NULL) { |
---|
91 | cout << "widget == NULL with: " << widget_name << endl; |
---|
92 | return false; |
---|
93 | } |
---|
94 | |
---|
95 | if (!value) |
---|
96 | value = _("N/A"); |
---|
97 | gtk_label_set_label(GTK_LABEL(widget), utf8(value)); |
---|
98 | return true; |
---|
99 | } |
---|
100 | |
---|
101 | bool RGGladeWindow::setLabel(const char *widget_name, const long value) |
---|
102 | { |
---|
103 | string strVal; |
---|
104 | GtkWidget *widget = glade_xml_get_widget(_gladeXML, widget_name); |
---|
105 | if (widget == NULL) { |
---|
106 | cout << "widget == NULL with: " << widget_name << endl; |
---|
107 | return false; |
---|
108 | } |
---|
109 | // we can never have values of zero or less |
---|
110 | if (value <= 0) |
---|
111 | // TRANSLATORS: this is a abbreviation for "not applicable" (on forms) |
---|
112 | // happens when e.g. a package has no installed version (or no |
---|
113 | // downloadable version) |
---|
114 | strVal = _("N/A"); |
---|
115 | else |
---|
116 | strVal = SizeToStr(value); |
---|
117 | gtk_label_set_label(GTK_LABEL(widget), utf8(strVal.c_str())); |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | bool RGGladeWindow::setTreeList(const char *widget_name, vector<string> values, |
---|
122 | bool use_markup) |
---|
123 | { |
---|
124 | char *type; |
---|
125 | string strVal; |
---|
126 | GtkWidget *widget = glade_xml_get_widget(_gladeXML, widget_name); |
---|
127 | if (widget == NULL) { |
---|
128 | cout << "widget == NULL with: " << widget_name << endl; |
---|
129 | return false; |
---|
130 | } |
---|
131 | // create column (if needed) |
---|
132 | if(gtk_tree_view_get_column(GTK_TREE_VIEW(widget), 0) == NULL) { |
---|
133 | |
---|
134 | // cell renderer |
---|
135 | GtkCellRenderer *renderer; |
---|
136 | renderer = gtk_cell_renderer_text_new(); |
---|
137 | |
---|
138 | if(use_markup) |
---|
139 | type = "markup"; |
---|
140 | else |
---|
141 | type = "text"; |
---|
142 | GtkTreeViewColumn *column; |
---|
143 | column = gtk_tree_view_column_new_with_attributes("SubView", |
---|
144 | renderer, |
---|
145 | type, 0, |
---|
146 | NULL); |
---|
147 | gtk_tree_view_append_column(GTK_TREE_VIEW(widget), column); |
---|
148 | } |
---|
149 | |
---|
150 | // store stuff |
---|
151 | GtkListStore *store = gtk_list_store_new(1, G_TYPE_STRING); |
---|
152 | GtkTreeIter iter; |
---|
153 | for(unsigned int i=0;i<values.size();i++) { |
---|
154 | gtk_list_store_append(store, &iter); |
---|
155 | gtk_list_store_set(store, &iter, 0, utf8(values[i].c_str()), -1); |
---|
156 | } |
---|
157 | gtk_tree_view_set_model(GTK_TREE_VIEW(widget), GTK_TREE_MODEL(store)); |
---|
158 | return true; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | bool RGGladeWindow::setTextView(const char *widget_name, |
---|
163 | const char* value, |
---|
164 | bool use_headline) |
---|
165 | { |
---|
166 | GtkTextIter start,end; |
---|
167 | |
---|
168 | GtkWidget *view = glade_xml_get_widget(_gladeXML, widget_name); |
---|
169 | if (view == NULL) { |
---|
170 | cout << "textview == NULL with: " << widget_name << endl; |
---|
171 | return false; |
---|
172 | } |
---|
173 | |
---|
174 | GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); |
---|
175 | gtk_text_buffer_set_text (buffer, utf8(value), -1); |
---|
176 | |
---|
177 | if(use_headline) { |
---|
178 | GtkTextTagTable *tag_table = gtk_text_buffer_get_tag_table(buffer); |
---|
179 | if(gtk_text_tag_table_lookup(tag_table, "bold") == NULL) { |
---|
180 | gtk_text_buffer_create_tag(buffer, "bold", |
---|
181 | "weight", PANGO_WEIGHT_BOLD, |
---|
182 | "scale", 1.1, |
---|
183 | NULL); |
---|
184 | } |
---|
185 | gtk_text_buffer_get_iter_at_offset(buffer, &start, 0); |
---|
186 | |
---|
187 | gtk_text_buffer_get_iter_at_offset(buffer, &end, 0); |
---|
188 | gtk_text_iter_forward_line(&end); |
---|
189 | |
---|
190 | gtk_text_buffer_apply_tag_by_name(buffer, "bold", &start, &end); |
---|
191 | } |
---|
192 | |
---|
193 | gtk_text_buffer_get_iter_at_offset(buffer, &start, 0); |
---|
194 | gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(view), &start,0,FALSE,0,0); |
---|
195 | return true; |
---|
196 | } |
---|
197 | |
---|
198 | bool RGGladeWindow::setPixmap(const char *widget_name, GdkPixbuf *value) |
---|
199 | { |
---|
200 | GtkWidget *pix = glade_xml_get_widget(_gladeXML, widget_name); |
---|
201 | if (pix == NULL) { |
---|
202 | cout << "textview == NULL with: " << widget_name << endl; |
---|
203 | return false; |
---|
204 | } |
---|
205 | gtk_image_set_from_pixbuf(GTK_IMAGE(pix), value); |
---|
206 | |
---|
207 | return true; |
---|
208 | } |
---|
209 | |
---|
210 | void RGGladeWindow::setBusyCursor(bool flag) |
---|
211 | { |
---|
212 | if(flag) { |
---|
213 | if(GTK_WIDGET_VISIBLE(_win)) |
---|
214 | gdk_window_set_cursor(window()->window, _busyCursor); |
---|
215 | #if GTK_CHECK_VERSION(2,4,0) |
---|
216 | // if we don't iterate here, the busy cursor is not shown |
---|
217 | while (gtk_events_pending()) |
---|
218 | gtk_main_iteration(); |
---|
219 | #endif |
---|
220 | } else { |
---|
221 | if(GTK_WIDGET_VISIBLE(_win)) |
---|
222 | gdk_window_set_cursor(window()->window, NULL); |
---|
223 | } |
---|
224 | } |
---|