GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Pango.H
Go to the documentation of this file.
1 #ifndef PANGO_H_
2 #define PANGO_H_
3 
4 #include "ColourLineSpec.H"
5 #include <sstream>
6 
11 class Font : public string {
12  PangoFontDescription *pangoFontDescription;
13 
16  void init(){pangoFontDescription=NULL;}
17  public:
18  Font() : string(){init();}
19  Font(char *str) : string(str){init();}
20  Font(string str) : string(str){init();}
21 
25  ~Font(void){
27  pango_font_description_free(pangoFontDescription);
29  }
30 
33  PangoFontDescription *getPangoFontDescription(void){
35  pango_font_description_free(pangoFontDescription);
37  return pangoFontDescription=pango_font_description_from_string(c_str());
38  }
39 };
40 
44 class PangoIOS {
45  public:
51 
52 // bool operator==(PangoIOS &pios, int psf){
53 // return
54 // }
55 };
56 
86 class Pango {
87  /*
88  The internal workings of the class are such that each string is associated with a list of markup/variable pairs.
89  In this manner, many markups may be applied to a single string.
90 
91  Thoughts for future expansion to handle <b> <i> and similar tags are to firstly add extra PangoIOS variables, such as PangoIOS::Bold, PangoIOS::Italic and so on.
92  How to structure this is confounded by the following two concepts of which is dominant, a ColourLineSpec/Font tag OR a PangoIOS tag ? ...
93  Pango pango;
94  // want to carry the ColourLineSpec tag along, past the PangoIOS::Italic tag, suggesting that ColourLineSpec is superior.
95  pango<<ColourLineSpec("r10000")<<"red 10 non italic"<<Pango::Italic<<"red 10 italic text";
96  // want green to be italic as well which suggests that PangoIOS tags are superior
97  pango<<Pango::Italic<<ColourLineSpec("r10000")<<"red 10 italic text"<<ColourLineSpec("g10000")<<"green 10 italic";
98  */
99 
102 
105 
107 
112  void nextTag(void){
113  markups.add(new LinkList<pair<string*, string*> *>); // add a new markups link
114  strings.add(NULL); // add a new strings link
115  }
116 public:
117 
121  Pango(void){
122  pangoMarkups.add(new string("foreground")); // these must be in the same order of pangoMarkupsIDs
123  pangoMarkups.add(new string("size"));
124  pangoMarkups.add(new string("font"));
125  }
126 
130  ~Pango(void){
131  reset();
132  while (pangoMarkups.getCount())
133  delete pangoMarkups.remove();
134  }
135 
139  string &getMarkup(void){
140  formattedMarkup=""; // clear the markup string
141  if (markups.getCount()){ // if markups and strings exist
142  markups.grab(1); markups.prev(); // set both to the first element in their lists
143  strings.grab(1); strings.prev();
144  for (int i=1; i<=markups.getCount(); i++){ // cycle through each element
145  markups.next(); strings.next();
146  LinkList<pair<string*, string*> *> *currentMarkups= markups.current(); // get the list of markups strings and variables
147  for (int j=1;j<=currentMarkups->getCount();j++){ // for each markup instruction, concat the markup and variable into formattedString
148  if (j==1) // if this is the last markup instruction, then complete the group of markups
149  formattedMarkup+="<span ";
150  currentMarkups->grab(j);
151  formattedMarkup+=(*currentMarkups->current()->first)+"='"+(*currentMarkups->current()->second)+"'";
152  if (j==currentMarkups->getCount()) // if this is the last markup instruction, then complete the group of markups
153  formattedMarkup+=">";
154  else // more to come, add a space
155  formattedMarkup+=" ";
156  }
157  // Now if the string is specified, add the string which is receiving the markup
158  if (strings.current())
160  if (currentMarkups->getCount()) // finally, if a markup was specified, then end the markup session
161  formattedMarkup+="</span>";
162  }
163  }
164  return formattedMarkup;
165  }
166 
169  void setLabelText(GtkWidget *l){
170  gtk_label_set_markup(GTK_LABEL(l), getMarkup().c_str());
171  }
172 
178  if (!strings.getCount() | (strings.current()!=NULL)) // we have completed the last string, start a new markup and string
179  nextTag();
180  markups.current()->add(new pair<string*, string*>(pangoMarkups.grab(FONT), new string(f))); // add the font markup and the associated font string
181  return *this;
182  }
183 
192  if (!strings.getCount() | (strings.current()!=NULL)) // we have completed the last string, start a new markup and string
193  nextTag();
194  if (cls.wasColourSpecified()){
195  gchar *c=cls.getColourString();
196  markups.current()->add(new pair<string*, string*>(pangoMarkups.grab(COLOUR), new string(c))); // add the size markup and the associated size string
197  g_free(c);
198  }
199  if (cls.wasSizeSpecified()){
200  ostringstream oss; oss<<cls.getSize(); // turn the size into a string
201  markups.current()->add(new pair<string*, string*>(pangoMarkups.grab(SIZE), new string(oss.str()))); // add the size markup and the associated size string
202  }
203  return *this;
204  }
205 
209  Pango &operator<<(string &s){
210  if (!strings.getCount()) nextTag(); // incase there is no markup just text
211  if (strings.current()==NULL)
212  strings.change(new string(s));
213  else
214  (*strings.current())+=s;
215  return *this;
216  }
217 
221  Pango &operator<<(char *s){
222  string str(s);
223  return operator<<(str);
224  }
225 
229  if (sf==PangoIOS::Next) // handle a nextTag request
230  nextTag();
231  if (sf==PangoIOS::Reset) // handle a reset request
232  reset();
233  return *this;
234  }
235 
239  void reset(void){
240  // remove any previous strings and markups
241  while (markups.getCount()){ // remove the markup pair linked lists and remove/delete each pair/string
243  while (llp->getCount()){
244  pair<string*, string*> *p=llp->remove();
245  delete p->second;
246  delete p;
247  }
248  delete llp;
249  }
250  while (strings.getCount()) // remove and delete the markup strings
251  delete strings.remove();
252  }
253 };
254 #endif // PANGO_H_