gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
Pango.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream 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  GTK+ IOStream 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 have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef PANGO_H_
18 #define PANGO_H_
19 
20 #include "ColourLineSpec.H"
21 #include <sstream>
22 
27 class Font : public string {
28  PangoFontDescription *pangoFontDescription;
29 
32  void init(){pangoFontDescription=NULL;}
33  public:
34  Font() : string(){init();}
35  Font(const char *str) : string(str){init();}
36  Font(string str) : string(str){init();}
37 
41  ~Font(void){
42  if (pangoFontDescription)
43  pango_font_description_free(pangoFontDescription);
44  pangoFontDescription=NULL;
45  }
46 
49  PangoFontDescription *getPangoFontDescription(void){
50  if (pangoFontDescription)
51  pango_font_description_free(pangoFontDescription);
52  pangoFontDescription=NULL;
53  return pangoFontDescription=pango_font_description_from_string(c_str());
54  }
55 };
56 
60 class PangoIOS {
61  public:
66  enum specialFlags {Next, Reset};
67 
68 // bool operator==(PangoIOS &pios, int psf){
69 // return
70 // }
71 };
72 
102 class Pango {
103  /*
104  The internal workings of the class are such that each string is associated with a list of markup/variable pairs.
105  In this manner, many markups may be applied to a single string.
106 
107  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.
108  How to structure this is confounded by the following two concepts of which is dominant, a ColourLineSpec/Font tag OR a PangoIOS tag ? ...
109  Pango pango;
110  // want to carry the ColourLineSpec tag along, past the PangoIOS::Italic tag, suggesting that ColourLineSpec is superior.
111  pango<<ColourLineSpec("r10000")<<"red 10 non italic"<<Pango::Italic<<"red 10 italic text";
112  // want green to be italic as well which suggests that PangoIOS tags are superior
113  pango<<Pango::Italic<<ColourLineSpec("r10000")<<"red 10 italic text"<<ColourLineSpec("g10000")<<"green 10 italic";
114  */
115 
118 
119  enum pangoMarkupsIDs {COLOUR=1, SIZE, FONT};
121 
123 
128  void nextTag(void){
129  markups.add(new LinkList<pair<string*, string*> *>); // add a new markups link
130  strings.add(NULL); // add a new strings link
131  }
132 public:
133 
137  Pango(void){
138  pangoMarkups.add(new string("foreground")); // these must be in the same order of pangoMarkupsIDs
139  pangoMarkups.add(new string("size"));
140  pangoMarkups.add(new string("font"));
141  }
142 
146  ~Pango(void){
147  reset();
148  while (pangoMarkups.getCount())
149  delete pangoMarkups.remove();
150  }
151 
155  string &getMarkup(void){
156  formattedMarkup=""; // clear the markup string
157  if (markups.getCount()){ // if markups and strings exist
158  markups.grab(1); markups.prev(); // set both to the first element in their lists
159  strings.grab(1); strings.prev();
160  for (int i=1; i<=markups.getCount(); i++){ // cycle through each element
161  markups.next(); strings.next();
162  LinkList<pair<string*, string*> *> *currentMarkups= markups.current(); // get the list of markups strings and variables
163  for (int j=1;j<=currentMarkups->getCount();j++){ // for each markup instruction, concat the markup and variable into formattedString
164  if (j==1) // if this is the last markup instruction, then complete the group of markups
165  formattedMarkup+="<span ";
166  currentMarkups->grab(j);
167  formattedMarkup+=(*currentMarkups->current()->first)+"='"+(*currentMarkups->current()->second)+"'";
168  if (j==currentMarkups->getCount()) // if this is the last markup instruction, then complete the group of markups
169  formattedMarkup+=">";
170  else // more to come, add a space
171  formattedMarkup+=" ";
172  }
173  // Now if the string is specified, add the string which is receiving the markup
174  if (strings.current())
175  formattedMarkup+=(*strings.current());
176  if (currentMarkups->getCount()) // finally, if a markup was specified, then end the markup session
177  formattedMarkup+="</span>";
178  }
179  }
180  return formattedMarkup;
181  }
182 
185  void setLabelText(GtkWidget *l){
186  gtk_label_set_markup(GTK_LABEL(l), getMarkup().c_str());
187  }
188 
194  if (!strings.getCount() | (strings.current()!=NULL)) // we have completed the last string, start a new markup and string
195  nextTag();
196  markups.current()->add(new pair<string*, string*>(pangoMarkups.grab(FONT), new string(f))); // add the font markup and the associated font string
197  return *this;
198  }
199 
208  if (!strings.getCount() | (strings.current()!=NULL)) // we have completed the last string, start a new markup and string
209  nextTag();
210  if (cls.wasColourSpecified()){
211  gchar *c=cls.getColourString();
212  markups.current()->add(new pair<string*, string*>(pangoMarkups.grab(COLOUR), new string(c))); // add the size markup and the associated size string
213  g_free(c);
214  }
215  if (cls.wasSizeSpecified()){
216  ostringstream oss; oss<<cls.getSize(); // turn the size into a string
217  markups.current()->add(new pair<string*, string*>(pangoMarkups.grab(SIZE), new string(oss.str()))); // add the size markup and the associated size string
218  }
219  return *this;
220  }
221 
225  Pango &operator<<(string &s){
226  if (!strings.getCount()) nextTag(); // incase there is no markup just text
227  if (strings.current()==NULL)
228  strings.change(new string(s));
229  else
230  (*strings.current())+=s;
231  return *this;
232  }
233 
237  Pango &operator<<(const char *s){
238  string str(s);
239  return operator<<(str);
240  }
241 
245  if (sf==PangoIOS::Next) // handle a nextTag request
246  nextTag();
247  if (sf==PangoIOS::Reset) // handle a reset request
248  reset();
249  return *this;
250  }
251 
255  void reset(void){
256  // remove any previous strings and markups
257  while (markups.getCount()){ // remove the markup pair linked lists and remove/delete each pair/string
258  LinkList<pair<string*, string*> *> *llp=markups.remove();
259  while (llp->getCount()){
260  pair<string*, string*> *p=llp->remove();
261  delete p->second;
262  delete p;
263  }
264  delete llp;
265  }
266  while (strings.getCount()) // remove and delete the markup strings
267  delete strings.remove();
268  }
269 };
270 #endif // PANGO_H_
bool wasColourSpecified(void)
gchar * getColourString(void)
specialFlags
Definition: Pango.H:66
Definition: Pango.H:27
Definition: Pango.H:60
PangoFontDescription * getPangoFontDescription(void)
Definition: Pango.H:49
float getSize(void) const
PangoFontDescription * pangoFontDescription
Definition: Pango.H:28
void setLabelText(GtkWidget *l)
Definition: Pango.H:185
Pango & operator<<(ColourLineSpec cls)
Definition: Pango.H:207
string & getMarkup(void)
Definition: Pango.H:155
bool wasSizeSpecified(void)
Pango(void)
Definition: Pango.H:137
float p
LinkList< string * > pangoMarkups
The pango markup strings which are pointed to by markups.
Definition: Pango.H:120
void init()
Definition: Pango.H:32
~Pango(void)
Deconstructor Destructor, delete allocated memory and empty the linked lists.
Definition: Pango.H:146
Font(const char *str)
Construct a font description from a character array.
Definition: Pango.H:35
Definition: Pango.H:102
Pango & operator<<(Font f)
Definition: Pango.H:193
void reset(void)
Definition: Pango.H:255
void nextTag(void)
Definition: Pango.H:128
LinkList< string * > strings
The strings to apply the markups to.
Definition: Pango.H:117
std::ostream & operator<<(std::ostream &stream, const BitStream &bitStream)
Definition: BitStream.C:204
~Font(void)
Definition: Pango.H:41
string formattedMarkup
The complete marked up string, set using getMarkup()
Definition: Pango.H:122
Pango & operator<<(PangoIOS::specialFlags sf)
Definition: Pango.H:244
Font()
Empty constructor.
Definition: Pango.H:34
pangoMarkupsIDs
Definition: Pango.H:119
LinkList< LinkList< pair< string *, string * > * > * > markups
Each of the strings is associated with a set of markup and markup variable strings.
Definition: Pango.H:116
Font(string str)
Construct a font description from a string.
Definition: Pango.H:36
Pango & operator<<(string &s)
Definition: Pango.H:225
encapsulates a method to specify colour and line or point plotting This class tries to encapsulate oc...
Pango & operator<<(const char *s)
Definition: Pango.H:237
gtkIOStream: /tmp/gtkiostream/include/Pango.H Source File
GTK+ IOStream  Beta