File: example_stockbrowser.cc

package info (click to toggle)
gtkmm2.4 1%3A2.20.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 73,860 kB
  • ctags: 20,553
  • sloc: xml: 79,575; sh: 10,120; cpp: 8,347; makefile: 290
file content (277 lines) | stat: -rw-r--r-- 8,823 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* Stock Item and Icon Browser
 *
 * The source code for this demo is only really useful
 * for the use of Gtk::TreeView, as it demonstrates a few
 * things not seen in other demos, such as packing multiple
 * renderers into one column and using Gtk::CellRendererPixbuf
 * successfully. The code for retrieving and displaying all
 * stock items probably won't be useful for the vast majority
 * of applications
 */

#include <gtkmm.h>

class Example_StockBrowser: public Gtk::Window
{
public:
  Example_StockBrowser();
  virtual ~Example_StockBrowser();

protected:

  //signal handlers:
  virtual void on_selection_changed();

  virtual Glib::RefPtr<Gtk::TreeModel> create_model();
  static Glib::ustring id_to_symbol(const Gtk::StockID& stockid); // convert StockID to Gtk::Stock symbol

  //Child widgets:
  Gtk::Frame m_Frame;
  Gtk::VBox m_VBox;
  Gtk::HBox m_HBox;
  Gtk::ScrolledWindow m_ScrolledWindow;
  Glib::RefPtr<Gtk::TreeModel> m_refTreeModel;
  Gtk::TreeView m_TreeView;
  Gtk::Alignment m_Alignment;
  Glib::RefPtr<Gtk::TreeSelection> m_refTreeSelection;

  Gtk::Label m_Label_Type;
  Gtk::Label m_Label_Symbol;
  Gtk::Label m_Label_ID;
  Gtk::Label m_Label_Accel;
  Gtk::Image m_Image;

  struct ModelColumns : public Gtk::TreeModel::ColumnRecord
  {
    Gtk::TreeModelColumn< Glib::RefPtr<Gdk::Pixbuf> >  icon;
    Gtk::TreeModelColumn<Glib::ustring>                symbol;
    Gtk::TreeModelColumn<Glib::ustring>                label;
    Gtk::TreeModelColumn<Glib::ustring>                accel;
    Gtk::TreeModelColumn<Gtk::StockID>                 id;

    ModelColumns() { add(icon); add(symbol); add(label); add(accel); add(id); }
  };

  const ModelColumns m_columns;
};


Gtk::Window* do_stock_browser()
{
  return new Example_StockBrowser();
}

Example_StockBrowser::Example_StockBrowser()
:
  m_Frame("Selected Item"),
  m_VBox(false, 8),
  m_HBox(false, 8),
  m_Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_TOP, 0.0, 0.0)
{
  set_title("Stock Icons and Items");
  set_default_size(-1, 500);
  set_border_width(8);

  add(m_HBox);
  m_ScrolledWindow.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
  m_HBox.pack_start(m_ScrolledWindow, Gtk::PACK_SHRINK);

  // create the TreeModel with all the stock item data in it
  m_refTreeModel = create_model();

  // create the TreeView, associate the TreeModel with it
  m_TreeView .set_model(m_refTreeModel);
  m_ScrolledWindow.add(m_TreeView);

  // add columns to the TreeView, mapping model Columns to TreeView m_columns::

  { //The pixbuf/text column:
    Gtk::TreeView::Column* pColumn = Gtk::manage( new Gtk::TreeView::Column("Symbol") );  //We specify the renderer(s) after construction.

    // The icon/symbol column uses two CellRenderers: Here we see why Gtk::TreeView is so good
    // m_columns.icon and m_columns.symbol are columns in the model. pColumn is the column in the TreeView:
    pColumn->pack_start(m_columns.icon, false);
    pColumn->pack_start(m_columns.symbol);

    m_TreeView.append_column(*pColumn);
  }

  // the label column
  m_TreeView.append_column("Label", m_columns.label); //Defaults to CellRendererText for a string model column.

  // the accelerator column
  m_TreeView.append_column("Accelerator", m_columns.accel); //Defaults to CellRendererText for a string model column.

  // the ID column
  m_TreeView.append_column("ID", m_columns.id); //Defaults to CellRendererText for a string model column.

  m_HBox.pack_end(m_Alignment);
  m_Alignment.add(m_Frame);

  m_VBox.set_border_width(4);
  m_Frame.add(m_VBox);

  m_VBox.pack_start(m_Label_Type, Gtk::PACK_SHRINK);
  m_VBox.pack_start(m_Image, Gtk::PACK_SHRINK);
  m_VBox.pack_start(m_Label_Accel, Gtk::PACK_SHRINK);
  m_VBox.pack_start(m_Label_Symbol, Gtk::PACK_SHRINK);
  m_VBox.pack_start(m_Label_ID, Gtk::PACK_SHRINK);

  m_refTreeSelection = m_TreeView.get_selection();
  m_refTreeSelection->set_mode(Gtk::SELECTION_SINGLE);
  m_refTreeSelection->signal_changed().connect(
      sigc::mem_fun(*this, &Example_StockBrowser::on_selection_changed));

  show_all();
}

Example_StockBrowser::~Example_StockBrowser()
{
}

Glib::RefPtr<Gtk::TreeModel> Example_StockBrowser::create_model()
{
  Glib::RefPtr<Gtk::ListStore> refStore = Gtk::ListStore::create(m_columns);
  refStore->set_sort_column(m_columns.id, Gtk::SORT_ASCENDING);

  // obtain the ids of all stock items
  typedef std::vector<Gtk::StockID> type_vecIDs;
  type_vecIDs vecIDs = Gtk::Stock::get_ids();

  // iterate through them, populating the ListStore as appropriate
  for(type_vecIDs::const_iterator iterIDs = vecIDs.begin(); iterIDs != vecIDs.end(); ++iterIDs)
  {
    const Gtk::StockID& stockid = *iterIDs;

    // Add a new empty row to the ListStore:
    Gtk::TreeModel::Row row = *(refStore->append());

    // Populate ID and symbol columns:
    row[m_columns.id]     = stockid;
    row[m_columns.symbol] = id_to_symbol(stockid);

    // get a Gtk::StockItem object for use:
    Gtk::StockItem item;
    if(Gtk::Stock::lookup(stockid, item))
    {
      // Populate Accel column, if the stock item has an accelerator key.
      if(const unsigned int keyval = item.get_keyval())
        row[m_columns.accel] = Gtk::AccelGroup::name(keyval, item.get_modifier());

      row[m_columns.label] = item.get_label();
    }

    // Populate icon column:
    row[m_columns.icon] = render_icon(stockid, Gtk::ICON_SIZE_MENU);
  }

  return refStore;
}

/* gtk-foo-bar -> Gtk::Stock::FOO_BAR
 */
Glib::ustring Example_StockBrowser::id_to_symbol(const Gtk::StockID& stockid)
{
  static const char prefix[] = "gtk-";
  const size_t prefix_length = sizeof(prefix) - 1;

  const Glib::ustring id = stockid.get_string();
  Glib::ustring::const_iterator id_pos = id.begin();

  Glib::ustring scope;

  if((id.length() >= prefix_length) && (id.compare(0, prefix_length, prefix) == 0))
  {
    scope = "Gtk::Stock::";

    // This does not work with the SUN Forte compiler:
    // "/opt/SUNWspro/WS6U2/include/CC/Cstd/rw/iterator", 
    // line 331: Error: Could not find a match for 
    // std::__iterator_category<std::T>(Glib::ustring_Iterator<const char*>).
    // "example_stockbrowser.cc", line 186:     Where: While instantiating 
    // "std::advance<Glib::ustring_Iterator<const char*>, unsigned>(Glib::ustring_Iterator<const char*>&, unsigned)".
    // "example_stockbrowser.cc", line 186:     Where: Instantiated from non-template code.
    //
    // std::advance(id_pos, prefix_length);
    //
    // So we do this instead:
    for(Glib::ustring::size_type i = 0; i < prefix_length; ++i)
      ++id_pos;
  }

  Glib::ustring symbol;
  std::replace_copy(id_pos, id.end(), std::back_inserter(symbol), gunichar('-'), gunichar('_'));

  return scope + symbol.uppercase();
}

// triggered when TreeView selection changes
void Example_StockBrowser::on_selection_changed()
{
  // put all the nice data stuff in the box on the right

  // Access the model "row" corresponding to the selected TreeView "row".
  if(const Gtk::TreeModel::iterator iter = m_refTreeSelection->get_selected())
  {
    const Gtk::TreeModel::Row row = *iter;

    // get the data out of the model
    const Gtk::StockID stockid = row[m_columns.id];
    const Glib::ustring symbol = row[m_columns.symbol];
    const Glib::ustring accel  = row[m_columns.accel];
    const Glib::ustring label  = row[m_columns.label];
    const Glib::RefPtr<Gdk::Pixbuf> icon = row[m_columns.icon];

    // Show what data was available:
    const char* type_text = "???????";

    if(!label.empty() && icon)  type_text = "Icon and Item";
    else if(icon)               type_text = "Icon Only";
    else if(!label.empty())     type_text = "Item Only";

    m_Label_Type.set_text(type_text);
    m_Label_Symbol.set_text(symbol);
    m_Label_ID.set_text(stockid.get_string());
    m_Label_Accel.set_text_with_mnemonic(label + ' ' + accel);

    if(icon) //If there's a stock icon:
    {
      // find the largest size the icon comes in:
      const Gtk::IconSet iconset = Gtk::IconSet::lookup_default(stockid);

      typedef std::vector<Gtk::IconSize> type_vecSizes;
      type_vecSizes sizes = iconset.get_sizes();

      Gtk::IconSize best_size = Gtk::ICON_SIZE_INVALID;
      int biggest_pixelcount = 0;

      for(type_vecSizes::const_iterator psize = sizes.begin(); psize != sizes.end(); ++psize)
      {
        int width  = 0;
        int height = 0;
        Gtk::IconSize::lookup(*psize, width, height);

        if(width * height > biggest_pixelcount)
        {
          biggest_pixelcount = width * height;
          best_size = *psize;
        }
      }

      m_Image.set(stockid, best_size);
    }
    else
    {
      m_Image.clear();
    }
  }
  else
  {
    m_Label_Type.set_text("No selected item");
    m_Label_Symbol.set_text("");
    m_Label_ID.set_text("");
    m_Label_Accel.set_text("");
    m_Image.clear();
  }
}