1 /* AbiSource Application Framework
2  * Copyright (C) 1998-2000 AbiSource, Inc.
3  * Copyright (C) 2005 INdT
4  * Author: Renato Araujo <renato.filho@indt.org.br>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301 USA.
20  */
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <gtk/gtk.h>
26 #include <hildon/hildon-font-selection-dialog.h>
27 
28 #include "ut_assert.h"
29 #include "ut_debugmsg.h"
30 #include "ut_string.h"
31 #include "ut_misc.h"
32 #include "ut_hash.h"
33 #include "ut_units.h"
34 #include "xap_UnixDialogHelper.h"
35 #include "xap_UnixHildonDlg_FontChooser.h"
36 #include "xap_UnixApp.h"
37 #include "xap_Frame.h"
38 #include "xap_UnixFrameImpl.h"
39 #include "xap_EncodingManager.h"
40 
41 //
42 // For Screen color picker
43 enum
44 {
45 		RED,
46 		GREEN,
47 		BLUE,
48 		OPACITY
49 };
50 
51 
52 /*****************************************************************/
static_constructor(XAP_DialogFactory * pFactory,XAP_Dialog_Id id)53 XAP_Dialog * XAP_UnixHildonDialog_FontChooser::static_constructor(XAP_DialogFactory * pFactory,
54 														 XAP_Dialog_Id id)
55 {
56 	XAP_UnixHildonDialog_FontChooser * p = new XAP_UnixHildonDialog_FontChooser(pFactory,id);
57 	return p;
58 }
59 
XAP_UnixHildonDialog_FontChooser(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id id)60 XAP_UnixHildonDialog_FontChooser::XAP_UnixHildonDialog_FontChooser(XAP_DialogFactory * pDlgFactory,
61 												   XAP_Dialog_Id id)
62 	: XAP_Dialog_FontChooser(pDlgFactory,id)
63 {
64 }
65 
~XAP_UnixHildonDialog_FontChooser(void)66 XAP_UnixHildonDialog_FontChooser::~XAP_UnixHildonDialog_FontChooser(void)
67 {
68 }
69 
70 
fillFontInfo()71 void XAP_UnixHildonDialog_FontChooser::fillFontInfo()
72 {
73 	GdkColor color;
74 	UT_RGBColor c;
75 	gchar* pszFontName;
76 	PangoFontDescription *ptrFontDescNew;
77 
78 	UT_parseColor (getVal("color").c_str(), c);
79 	color.red = static_cast<guint16> ((c.m_red / 255.0) * 65535.0);
80 	color.green = static_cast<guint16> ((c.m_grn / 255.0) * 65535.0);
81 	color.blue = static_cast<guint16> ((c.m_blu / 255.0) * 65535.0);
82 
83 
84        	pszFontName = g_strdup_printf ("%s,%d", getVal("font-family").c_str(),
85 						atoi(std_size_string(UT_convertToPoints(getVal("font-size").c_str()))));
86 
87 	PangoFontDescription* ptrFontDesc = pango_font_description_from_string (pszFontName);
88 
89 	PangoFont *fnt = pango_context_load_font (gtk_widget_get_pango_context (GTK_WIDGET(m_Widget)),
90 						  ptrFontDesc);
91 
92 	ptrFontDescNew = pango_font_describe  (fnt);
93 
94 	g_object_set (G_OBJECT (m_Widget), "family", pango_font_description_get_family (ptrFontDescNew),
95 					   "underline", m_bUnderline,
96 					   "strikethrough", m_bStrikeout,
97 					   "color", &color,
98 					   "size", atoi(std_size_string(UT_convertToPoints(getVal("font-size").c_str()))),
99 					   NULL);
100 
101 	pango_font_description_free (ptrFontDesc);
102 	pango_font_description_free (ptrFontDescNew);
103 	g_free (pszFontName);
104 
105 	//font style
106 	listStyle st = LIST_STYLE_NORMAL;
107 	if (getVal("font-style").empty() || getVal("font-weight").empty())
108 			st = LIST_STYLE_NONE;
109 	else if (!g_ascii_strcasecmp(getVal("font-style").c_str(), "normal") &&
110 					 !g_ascii_strcasecmp(getVal("font-weight").c_str(), "normal"))
111 			st = LIST_STYLE_NORMAL;
112 	else if (!g_ascii_strcasecmp(getVal("font-style").c_str(), "normal") &&
113 					 !g_ascii_strcasecmp(getVal("font-weight").c_str(), "bold"))
114 			st = LIST_STYLE_BOLD;
115 	else if (!g_ascii_strcasecmp(getVal("font-style").c_str(), "italic") &&
116 					 !g_ascii_strcasecmp(getVal("font-weight").c_str(), "normal"))
117 			st = LIST_STYLE_ITALIC;
118 	else if (!g_ascii_strcasecmp(getVal("font-style").c_str(), "italic") &&
119 					 !g_ascii_strcasecmp(getVal("font-weight").c_str(), "bold"))
120 			st = LIST_STYLE_BOLD_ITALIC;
121 	else
122 	{
123 			UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
124 	}
125 	switch (st)
126 	{
127 	case LIST_STYLE_NORMAL:
128 		g_object_set (G_OBJECT (m_Widget),
129 					"bold", FALSE,
130 					"italic", FALSE,
131 					NULL);
132 		break;
133 	case LIST_STYLE_BOLD:
134 		g_object_set (G_OBJECT (m_Widget),
135 					"bold", TRUE,
136 					"italic", FALSE,
137 					NULL);
138 		break;
139 	case LIST_STYLE_ITALIC:
140 		g_object_set (G_OBJECT (m_Widget),
141 					"bold", FALSE,
142 					"italic", TRUE,
143 					NULL);
144 		break;
145 	case LIST_STYLE_BOLD_ITALIC:
146 		g_object_set (G_OBJECT (m_Widget),
147 					"bold", TRUE,
148 					"italic", TRUE,
149 					NULL);
150 		break;
151 	default:
152 		break;
153 	}
154 }
155 
156 /* Gets PangoFontDescription from iterator */
loadFontInfo()157 void XAP_UnixHildonDialog_FontChooser::loadFontInfo()
158 {
159 	const gchar* cszFontFamily;
160 	GdkColor *ptrFontColor;
161 	gboolean bFontBold;
162 	gboolean bFontItalic;
163 	gboolean bFontUnderline;
164 	gboolean bFontStrikethrough;
165 	gint iFontSize;
166 
167  	m_bStrikeout = m_bUnderline = false;
168 	m_bChangedUnderline = !m_bChangedUnderline;
169 	m_bChangedStrikeOut = !m_bChangedStrikeOut;
170 
171 	/* Search needed desc from list's attributes */
172 	g_object_get (G_OBJECT (m_Widget),
173 		      "color", &ptrFontColor,
174 		      "family", &cszFontFamily,
175 		      "bold", &bFontBold,
176 		      "italic", &bFontItalic,
177 		      "underline", &bFontUnderline,
178 		      "strikethrough", &bFontStrikethrough,
179 		      "size", &iFontSize,
180 		      NULL);
181 
182 	//font color
183 	gchar *buf_color = (gchar *) g_new(gchar, 8);
184 
185 	m_currentFGColor[RED]   = static_cast<double> (ptrFontColor->red) / 65535.0;
186 	m_currentFGColor[GREEN] = static_cast<double> (ptrFontColor->green) / 65535.0;
187 	m_currentFGColor[BLUE]  = static_cast<double> (ptrFontColor->blue) /  65535.0;
188 
189 	sprintf(buf_color, "%02x%02x%02x",
190 		static_cast<unsigned int>(m_currentFGColor[RED] * static_cast<gdouble>(255.0)),
191 		static_cast<unsigned int>(m_currentFGColor[GREEN] * static_cast<gdouble>(255.0)),
192 		static_cast<unsigned int>(m_currentFGColor[BLUE] * static_cast<gdouble>(255.0)));
193 
194 	addOrReplaceVecProp("color",static_cast<gchar *>(buf_color));
195 
196 
197 	//font family
198 	char *szFontFamily = new char[strlen(cszFontFamily)  + 1 ];
199 	sprintf(szFontFamily, cszFontFamily);
200 	addOrReplaceVecProp("font-family",static_cast<gchar *> (szFontFamily) );
201 
202 	//font size
203 	char *szFontSize = new char[50];
204 	memset(szFontSize, '\0', 50);
205 	g_snprintf(szFontSize, 50, "%dpt", iFontSize);
206 	addOrReplaceVecProp("font-size", static_cast<gchar *> (szFontSize) );
207 
208 	//font style
209 	if (bFontItalic)
210 		addOrReplaceVecProp("font-style","italic");
211 	else
212 		addOrReplaceVecProp("font-style","normal");
213 
214 	if (bFontBold)
215 		addOrReplaceVecProp("font-weight","bold");
216 	else
217 		addOrReplaceVecProp("font-weight","normal");
218 
219 
220 	//bgcolor
221 	//TODO
222 
223 	//font underline
224 	m_bUnderline = bFontUnderline;
225 
226 	//font srtrikethrough
227 	m_bStrikeout = bFontStrikethrough;
228 
229 	setFontDecoration(m_bUnderline,m_bOverline,m_bStrikeout,m_bTopline,m_bBottomline);
230 }
231 
232 
233 
runModal(XAP_Frame * pFrame)234 void XAP_UnixHildonDialog_FontChooser::runModal(XAP_Frame * pFrame)
235 {
236 	GtkWidget *pTopLevel = (static_cast<XAP_UnixFrameImpl*> (pFrame->getFrameImpl()))->getTopLevelWindow();
237 	//PangoAttrList *default_list = pango_attr_list_new();
238 
239         m_Widget = GTK_WIDGET(hildon_font_selection_dialog_new(GTK_WINDOW(gtk_widget_get_parent(GTK_WIDGET(pTopLevel))),
240 		                                               NULL));
241 
242 	gtk_widget_show_all(GTK_WIDGET(m_Widget));
243 
244 	m_answer = a_CANCEL;
245 
246 	m_doneFirstFont = true;
247 
248 	fillFontInfo();
249 
250 	if (gtk_dialog_run(GTK_DIALOG(m_Widget)) == GTK_RESPONSE_OK)
251 	{
252 
253 		//default_list = hildon_font_selection_dialog_get_font ( HILDON_FONT_SELECTION_DIALOG ( m_Widget ) );
254 		loadFontInfo();
255 		m_answer = a_OK;
256 
257 		//TODO
258 		//addOrReplaceVecProp("bgcolor",static_cast<gchar *>(buf_color));
259 	}
260 	gtk_widget_destroy(GTK_WIDGET(m_Widget));
261 	m_doneFirstFont = false;
262 
263 	UT_DEBUGMSG(("FontChooserEnd: Family[%s%s] Size[%s%s] Weight[%s%s] Style[%s%s] Color[%s%s] Underline[%d%s] StrikeOut[%d%s]\n",
264 							 getVal("font-family").c_str(),        ((m_bChangedFontFamily) ? "(chg)" : ""),
265 							 getVal("font-size").c_str(),            ((m_bChangedFontSize) ? "(chg)" : ""),
266 							 getVal("font-weight").c_str(),        ((m_bChangedFontWeight) ? "(chg)" : ""),
267 							 getVal("font-style").c_str(),          ((m_bChangedFontStyle) ? "(chg)" : ""),
268 							 getVal("color").c_str(),                           ((m_bChangedColor) ? "(chg)" : ""),
269 							 m_bUnderline,                                                        ((m_bChangedUnderline) ? "(chg)" : ""),
270 							 m_bStrikeout,                                                        ((m_bChangedStrikeOut) ? "(chg)" : "")));
271 
272 }
273