1 /*
2  * Copyright © 2004-2008 Jens Oknelid, paskharen@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * In addition, as a special exception, compiling, linking, and/or
19  * using OpenSSL with this program is allowed.
20  */
21 
22 #include "bookentry.hh"
23 #include "wulformanager.hh"
24 
25 using namespace std;
26 
27 GSList* BookEntry::group = NULL;
28 
BookEntry(const EntryType type,const string & text,const string & glade,const string & id)29 BookEntry::BookEntry(const EntryType type, const string &text, const string &glade, const string &id):
30 	Entry(type, glade, id),
31 	bold(FALSE),
32 	urgent(FALSE)
33 {
34 	labelBox = gtk_hbox_new(FALSE, 5);
35 
36 	eventBox = gtk_event_box_new();
37 	gtk_event_box_set_above_child(GTK_EVENT_BOX(eventBox), TRUE);
38 	gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventBox), FALSE);
39 
40 	// Add an icon to the tab label
41 	icon = gtk_image_new();
42 	gtk_box_pack_start(GTK_BOX(labelBox), icon, FALSE, FALSE, 0);
43 
44 	// Make the eventbox fill to all left-over space.
45 	gtk_box_pack_start(GTK_BOX(labelBox), GTK_WIDGET(eventBox), TRUE, TRUE, 0);
46 
47 	label = GTK_LABEL(gtk_label_new(text.c_str()));
48 	gtk_container_add(GTK_CONTAINER(eventBox), GTK_WIDGET(label));
49 
50 	// Align text to the left (x = 0) and in the vertical center (0.5)
51 	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
52 
53 	closeButton = gtk_button_new();
54 	gtk_button_set_relief(GTK_BUTTON(closeButton), GTK_RELIEF_NONE);
55 	gtk_button_set_focus_on_click(GTK_BUTTON(closeButton), FALSE);
56 
57 	// Shrink the padding around the close button
58 	GtkRcStyle *rcstyle = gtk_rc_style_new();
59 	rcstyle->xthickness = rcstyle->ythickness = 0;
60 	gtk_widget_modify_style(closeButton, rcstyle);
61 	gtk_rc_style_unref(rcstyle);
62 
63 	// Add the stock icon to the close button
64 	GtkWidget *image = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
65 	gtk_container_add(GTK_CONTAINER(closeButton), image);
66 	gtk_box_pack_start(GTK_BOX(labelBox), closeButton, FALSE, FALSE, 0);
67 
68 	tips = gtk_tooltips_new();
69 	gtk_tooltips_enable(tips);
70 	gtk_tooltips_set_tip(tips, closeButton, _("Close tab"), NULL);
71 
72 	gtk_widget_show_all(labelBox);
73 
74 	tabMenuItem = gtk_radio_menu_item_new_with_label(group, text.c_str());
75 	group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(tabMenuItem));
76 
77 	setLabel_gui(text);
78 	setIcon_gui();
79 
80 	// Associates entry to the widget for later retrieval in MainWindow::switchPage_gui()
81 	g_object_set_data(G_OBJECT(getContainer()), "entry", (gpointer)this);
82 }
83 
getContainer()84 GtkWidget* BookEntry::getContainer()
85 {
86 	return getWidget("mainBox");
87 }
88 
setLabel_gui(string text)89 void BookEntry::setLabel_gui(string text)
90 {
91 	// Update the tab menu item label
92 	GtkWidget *child = gtk_bin_get_child(GTK_BIN(tabMenuItem));
93 	if (child && GTK_IS_LABEL(child))
94 		gtk_label_set_text(GTK_LABEL(child), text.c_str());
95 
96 	// Update the notebook tab label
97 	gtk_tooltips_set_tip(tips, eventBox, text.c_str(), text.c_str());
98 	glong len = g_utf8_strlen(text.c_str(), -1);
99 
100 	// Truncate the label text
101 	if (len > labelSize)
102 	{
103 		gchar truncatedText[text.size()];
104 		// TRANSLATORS: Tab label ellipsis to indicate text is longer than tab width
105 		const string clipText = _("...");
106 		len = labelSize - g_utf8_strlen(clipText.c_str(), -1);
107 		g_utf8_strncpy(truncatedText, text.c_str(), len);
108 		truncatedLabelText = truncatedText + clipText;
109 	}
110 	else
111 	{
112 		truncatedLabelText = text;
113 	}
114 
115 	labelText = text;
116 	updateLabel_gui();
117 
118 	// Update the main window title if the current tab is selected.
119 	if (isSelected_gui())
120 		WulforManager::get()->getMainWindow()->setTitle(getLabelText());
121 }
122 
setIcon_gui(const string & customIconName)123 void BookEntry::setIcon_gui(const string &customIconName)
124 {
125 	string iconName = customIconName;
126 
127 	if (iconName.empty())
128 	{
129 		switch (getType())
130 		{
131 			case Entry::DOWNLOAD_QUEUE:
132 				iconName = "linuxdcpp-queue";
133 				break;
134 			case Entry::FAVORITE_HUBS:
135 				iconName = "linuxdcpp-favorite-hubs";
136 				break;
137 			case Entry::FAVORITE_USERS:
138 				iconName = "linuxdcpp-favorite-users";
139 				break;
140 			case Entry::FINISHED_DOWNLOADS:
141 				iconName = "linuxdcpp-finished-downloads";
142 				break;
143 			case Entry::FINISHED_UPLOADS:
144 				iconName = "linuxdcpp-finished-uploads";
145 				break;
146 			case Entry::HUB:
147 				iconName = "network-server";
148 				break;
149 			case Entry::PRIVATE_MESSAGE:
150 				iconName = "user-available";
151 				break;
152 			case Entry::PUBLIC_HUBS:
153 				iconName = "linuxdcpp-public-hubs";
154 				break;
155 			case Entry::SEARCH:
156 				iconName = "linuxdcpp-search";
157 				break;
158 			case Entry::SHARE_BROWSER:
159 				iconName = "folder";
160 				break;
161 			default:
162 				; // Default to empty string to indicate no icon should be shown below
163 		}
164 	}
165 
166 	// If user doesn't have the icon in their theme, default to showing no icon instead
167 	// of showing some generic missing icon. This may occur if the user's system
168 	// doesn't implement the full freedesktop.org Icon Naming Specification.
169 	GtkIconTheme *iconTheme = gtk_icon_theme_get_default();
170 	if (!iconName.empty() && gtk_icon_theme_has_icon(iconTheme, iconName.c_str()))
171 		gtk_image_set_from_icon_name(GTK_IMAGE(icon), iconName.c_str(), GTK_ICON_SIZE_MENU);
172 }
173 
setBold_gui()174 void BookEntry::setBold_gui()
175 {
176 	if (!bold && !isActive_gui())
177 	{
178 		bold = TRUE;
179 		updateLabel_gui();
180 	}
181 }
182 
setUrgent_gui()183 void BookEntry::setUrgent_gui()
184 {
185 	if (!isActive_gui())
186 	{
187 		MainWindow *mw = WulforManager::get()->getMainWindow();
188 
189 		if (!urgent)
190 		{
191 			bold = TRUE;
192 			urgent = TRUE;
193 			updateLabel_gui();
194 		}
195 
196 		if (!mw->isActive_gui())
197 			mw->setUrgent_gui();
198 	}
199 }
200 
setActive_gui()201 void BookEntry::setActive_gui()
202 {
203 	if (bold || urgent)
204 	{
205 		bold = FALSE;
206 		urgent = FALSE;
207 		updateLabel_gui();
208 	}
209 }
210 
isActive_gui()211 bool BookEntry::isActive_gui()
212 {
213 	MainWindow *mw = WulforManager::get()->getMainWindow();
214 
215 	return mw->isActive_gui() && mw->currentPage_gui() == getContainer();
216 }
217 
isSelected_gui()218 bool BookEntry::isSelected_gui()
219 {
220 	MainWindow *mw = WulforManager::get()->getMainWindow();
221 
222 	return mw->currentPage_gui() == getContainer();
223 }
224 
updateLabel_gui()225 void BookEntry::updateLabel_gui()
226 {
227 	const char *format = "%s";
228 
229 	if (urgent)
230 		format = "<i><b>%s</b></i>";
231 	else if (bold)
232 		format = "<b>%s</b>";
233 
234 	char *markup = g_markup_printf_escaped(format, truncatedLabelText.c_str());
235 	gtk_label_set_markup(label, markup);
236 	g_free(markup);
237 }
238 
getLabelText()239 const string& BookEntry::getLabelText()
240 {
241 	return labelText;
242 }
243 
244