| 1 | /* Copyright (C) 2001 The gtkmm Development Team |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public |
| 5 | * License as published by the Free Software Foundation; either |
| 6 | * version 2.1 of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #ifdef HAVE_CONFIG_H |
| 18 | #include "config.h" |
| 19 | #endif |
| 20 | |
| 21 | #include "demowindow.h" |
| 22 | |
| 23 | #include "demos.h" |
| 24 | #include "textwidget.h" |
| 25 | |
| 26 | #include <glibmm/bytes.h> |
| 27 | #include <glibmm/convert.h> |
| 28 | #include <giomm/resource.h> |
| 29 | #include <gtkmm/cellrenderertext.h> |
| 30 | #include <gtkmm/treeviewcolumn.h> |
| 31 | |
| 32 | #include <cstddef> |
| 33 | #include <cstring> |
| 34 | #include <iostream> |
| 35 | |
| 36 | using std::isspace; |
| 37 | using std::strlen; |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | |
| 42 | struct DemoColumns : public Gtk::TreeModelColumnRecord |
| 43 | { |
| 44 | Gtk::TreeModelColumn<Glib::ustring> title; |
| 45 | Gtk::TreeModelColumn<Glib::ustring> filename; |
| 46 | Gtk::TreeModelColumn<type_slotDo> slot; |
| 47 | Gtk::TreeModelColumn<bool> italic; |
| 48 | |
| 49 | DemoColumns() { add(title); add(filename); add(slot); add(italic); } |
| 50 | }; |
| 51 | |
| 52 | // Singleton accessor function. |
| 53 | const DemoColumns& demo_columns() |
| 54 | { |
| 55 | static DemoColumns column_record; |
| 56 | return column_record; |
| 57 | } |
| 58 | |
| 59 | } // anonymous namespace |
| 60 | |
| 61 | |
| 62 | DemoWindow::DemoWindow() |
| 63 | : m_RunButton("Run"), |
| 64 | m_HBox(Gtk::ORIENTATION_HORIZONTAL), |
| 65 | m_TextWidget_Info(false), |
| 66 | m_TextWidget_Source(true) |
| 67 | { |
| 68 | m_pWindow_Example = nullptr; |
| 69 | |
| 70 | configure_header_bar(); |
| 71 | |
| 72 | add(m_HBox); |
| 73 | |
| 74 | //Tree: |
| 75 | m_refTreeStore = Gtk::TreeStore::create(demo_columns()); |
| 76 | m_TreeView.set_model(m_refTreeStore); |
| 77 | |
| 78 | m_refTreeSelection = m_TreeView.get_selection(); |
| 79 | m_refTreeSelection->set_mode(Gtk::SELECTION_BROWSE); |
| 80 | m_refTreeSelection->set_select_function( sigc::ptr_fun(&DemoWindow::select_function) ); |
| 81 | |
| 82 | m_TreeView.set_size_request(200, -1); |
| 83 | |
| 84 | fill_tree(); |
| 85 | |
| 86 | //SideBar |
| 87 | m_SideBar.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); |
| 88 | m_SideBar.get_style_context()->add_class("sidebar"); |
| 89 | m_SideBar.add(m_TreeView); |
| 90 | m_HBox.pack_start(m_SideBar, Gtk::PACK_SHRINK); |
| 91 | |
| 92 | //Notebook: |
| 93 | m_Notebook.popup_enable(); |
| 94 | m_Notebook.set_scrollable(); |
| 95 | m_Notebook.append_page(m_TextWidget_Info, "_Info", true); //true = use mnemonic. |
| 96 | m_Notebook.append_page(m_TextWidget_Source, "_Source", true); //true = use mnemonic. |
| 97 | m_Notebook.child_property_tab_expand(m_TextWidget_Info) = true; |
| 98 | m_Notebook.child_property_tab_expand(m_TextWidget_Source) = true; |
| 99 | m_HBox.pack_start(m_Notebook); |
| 100 | |
| 101 | set_default_size (800, 600); |
| 102 | |
| 103 | load_file (testgtk_demos[0].filename); |
| 104 | show_all(); |
| 105 | } |
| 106 | |
| 107 | void DemoWindow::configure_header_bar() |
| 108 | { |
| 109 | m_HeaderBar.set_show_close_button(); |
| 110 | m_HeaderBar.pack_start(m_RunButton); |
| 111 | |
| 112 | m_RunButton.get_style_context()->add_class("suggested-action"); |
| 113 | m_RunButton.signal_clicked().connect(sigc::mem_fun(*this, &DemoWindow::on_run_button_clicked)); |
| 114 | |
| 115 | set_titlebar(m_HeaderBar); |
| 116 | } |
| 117 | |
| 118 | void DemoWindow::fill_tree() |
| 119 | { |
| 120 | const auto& columns = demo_columns(); |
| 121 | |
| 122 | /* this code only supports 1 level of children. If we |
| 123 | * want more we probably have to use a recursing function. |
| 124 | */ |
| 125 | for (auto d = testgtk_demos; d->title; ++d) |
| 126 | { |
| 127 | Gtk::TreeRow row = *(m_refTreeStore->append()); |
| 128 | |
| 129 | row[columns.title] = d->title; |
| 130 | row[columns.filename] = d->filename; |
| 131 | row[columns.slot] = d->slot; |
| 132 | row[columns.italic] = false; |
| 133 | |
| 134 | for (auto child = d->children; child && child->title; ++child) |
| 135 | { |
| 136 | Gtk::TreeRow child_row = *(m_refTreeStore->append(row.children())); |
| 137 | |
| 138 | child_row[columns.title] = child->title; |
| 139 | child_row[columns.filename] = child->filename; |
| 140 | child_row[columns.slot] = child->slot; |
| 141 | child_row[columns.italic] = false; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | Gtk::CellRendererText* pCell = Gtk::make_managed<Gtk::CellRendererText>(); |
| 146 | pCell->property_style() = Pango::STYLE_ITALIC; |
| 147 | |
| 148 | Gtk::TreeViewColumn* pColumn = new Gtk::TreeViewColumn("Widget (double click for demo)", *pCell); |
| 149 | pColumn->add_attribute(pCell->property_text(), columns.title); |
| 150 | pColumn->add_attribute(pCell->property_style_set(), columns.italic); |
| 151 | |
| 152 | m_TreeView.append_column(*pColumn); |
| 153 | |
| 154 | m_refTreeSelection->signal_changed().connect(sigc::mem_fun(*this, &DemoWindow::on_treeselection_changed)); |
| 155 | m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this, &DemoWindow::on_treeview_row_activated)); |
| 156 | |
| 157 | m_TreeView.expand_all(); |
| 158 | } |
| 159 | |
| 160 | DemoWindow::~DemoWindow() |
| 161 | { |
| 162 | on_example_window_hide(); //delete the example window if there is one. |
| 163 | } |
| 164 | |
| 165 | void DemoWindow::on_run_button_clicked() |
| 166 | { |
| 167 | if(m_pWindow_Example == nullptr) //Don't open a second window. |
| 168 | { |
| 169 | if(const Gtk::TreeModel::iterator iter = m_refTreeSelection->get_selected()) |
| 170 | { |
| 171 | m_TreePath = m_refTreeStore->get_path(iter); |
| 172 | |
| 173 | run_example(*iter); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void DemoWindow::on_treeview_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* /* model */) |
| 179 | { |
| 180 | m_TreePath = path; |
| 181 | |
| 182 | if(m_pWindow_Example == nullptr) //Don't open a second window. |
| 183 | { |
| 184 | if(const Gtk::TreeModel::iterator iter = m_TreeView.get_model()->get_iter(m_TreePath)) |
| 185 | { |
| 186 | run_example(*iter); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void DemoWindow::run_example(const Gtk::TreeModel::Row& row) |
| 192 | { |
| 193 | const auto& columns = demo_columns(); |
| 194 | |
| 195 | const type_slotDo& slot = row[columns.slot]; |
| 196 | |
| 197 | if(slot && (m_pWindow_Example = slot())) |
| 198 | { |
| 199 | row[columns.italic] = true; |
| 200 | |
| 201 | m_pWindow_Example->signal_hide().connect(sigc::mem_fun(*this, &DemoWindow::on_example_window_hide)); |
| 202 | m_pWindow_Example->show(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | bool DemoWindow::select_function(const Glib::RefPtr<Gtk::TreeModel>& model, |
| 207 | const Gtk::TreeModel::Path& path, bool) |
| 208 | { |
| 209 | const Gtk::TreeModel::iterator iter = model->get_iter(path); |
| 210 | return iter->children().empty(); // only allow leaf nodes to be selected |
| 211 | } |
| 212 | |
| 213 | void DemoWindow::on_treeselection_changed() |
| 214 | { |
| 215 | if(const Gtk::TreeModel::iterator iter = m_refTreeSelection->get_selected()) |
| 216 | { |
| 217 | const Glib::ustring filename = (*iter)[demo_columns().filename]; |
| 218 | const Glib::ustring title = (*iter)[demo_columns().title]; |
| 219 | |
| 220 | load_file(Glib::filename_from_utf8(filename)); |
| 221 | m_HeaderBar.set_title(title); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void DemoWindow::load_file(const std::string& filename) |
| 226 | { |
| 227 | if ( m_current_filename == filename ) |
| 228 | { |
| 229 | return; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | // Show extra data files for this demo, if any. |
| 234 | remove_data_tabs(); |
| 235 | add_data_tabs(filename); |
| 236 | |
| 237 | m_current_filename = filename; |
| 238 | |
| 239 | m_TextWidget_Info.wipe(); |
| 240 | m_TextWidget_Source.wipe(); |
| 241 | |
| 242 | Glib::RefPtr<Gtk::TextBuffer> refBufferInfo = m_TextWidget_Info.get_buffer(); |
| 243 | Glib::RefPtr<Gtk::TextBuffer> refBufferSource = m_TextWidget_Source.get_buffer(); |
| 244 | |
| 245 | Glib::RefPtr<const Glib::Bytes> bytes; |
| 246 | try |
| 247 | { |
| 248 | bytes = Gio::Resource::lookup_data_global("/sources/" + filename); |
| 249 | } |
| 250 | catch (const Gio::ResourceError& ex) |
| 251 | { |
| 252 | std::cerr << "Cannot open source for " << filename << ": " << ex.what() << std::endl; |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | gsize data_size = 0; |
| 257 | gchar** lines = g_strsplit(static_cast<const gchar*>(bytes->get_data(data_size)), "\n", -1); |
| 258 | bytes.reset(); |
| 259 | |
| 260 | int state = 0; |
| 261 | bool in_para = false; |
| 262 | auto start = refBufferInfo->get_iter_at_offset(0); |
| 263 | for (std::size_t i = 0; lines[i] != NULL; i++) |
| 264 | { |
| 265 | /* Make sure \r is stripped at the end for the poor windows people */ |
| 266 | lines[i] = g_strchomp(lines[i]); |
| 267 | |
| 268 | gchar *p = lines[i]; |
| 269 | gchar *q = nullptr; |
| 270 | gchar *r = nullptr; |
| 271 | |
| 272 | switch (state) |
| 273 | { |
| 274 | case 0: |
| 275 | /* Reading title */ |
| 276 | while (*p == '/' || *p == '*' || isspace (*p)) |
| 277 | p++; |
| 278 | |
| 279 | r = p; |
| 280 | |
| 281 | while (*r != '/' && strlen (r)) |
| 282 | r++; |
| 283 | if (strlen (r) > 0) |
| 284 | p = r + 1; |
| 285 | |
| 286 | q = p + strlen (p); |
| 287 | |
| 288 | while (q > p && isspace (*(q - 1))) |
| 289 | q--; |
| 290 | |
| 291 | if (q > p) |
| 292 | { |
| 293 | Gtk::TextBuffer::iterator end = start; |
| 294 | |
| 295 | const Glib::ustring strTemp (p, q); |
| 296 | end = refBufferInfo->insert(end, strTemp); |
| 297 | start = end; |
| 298 | |
| 299 | start.backward_chars(strTemp.length()); |
| 300 | refBufferInfo->apply_tag_by_name("title", start, end); |
| 301 | |
| 302 | start = end; |
| 303 | |
| 304 | state++; |
| 305 | } |
| 306 | break; |
| 307 | |
| 308 | case 1: |
| 309 | /* Reading body of info section */ |
| 310 | while (isspace (*p)) |
| 311 | p++; |
| 312 | |
| 313 | if (*p == '*' && *(p + 1) == '/') |
| 314 | { |
| 315 | start = refBufferSource->get_iter_at_offset(0); |
| 316 | state++; |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | int len; |
| 321 | |
| 322 | while (*p == '*' || isspace (*p)) |
| 323 | p++; |
| 324 | |
| 325 | len = strlen (p); |
| 326 | while (isspace (*(p + len - 1))) |
| 327 | len--; |
| 328 | |
| 329 | if (len > 0) |
| 330 | { |
| 331 | if (in_para) |
| 332 | { |
| 333 | start = refBufferInfo->insert(start, " "); |
| 334 | } |
| 335 | |
| 336 | start = refBufferInfo->insert(start, Glib::ustring(p, p + len)); |
| 337 | in_para = 1; |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | start = refBufferInfo->insert(start, "\n"); |
| 342 | in_para = 0; |
| 343 | } |
| 344 | } |
| 345 | break; |
| 346 | |
| 347 | case 2: |
| 348 | /* Skipping blank lines */ |
| 349 | while (isspace (*p)) |
| 350 | p++; |
| 351 | |
| 352 | if (!*p) |
| 353 | break; |
| 354 | |
| 355 | p = lines[i]; |
| 356 | state++; |
| 357 | /* Fall through */ |
| 358 | |
| 359 | case 3: |
| 360 | /* Reading program body */ |
| 361 | start = refBufferSource->insert(start, p); |
| 362 | start = refBufferSource->insert(start, "\n"); |
| 363 | break; |
| 364 | } // end switch state |
| 365 | } // end for i |
| 366 | |
| 367 | g_strfreev (lines); |
| 368 | |
| 369 | m_TextWidget_Source.fontify(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void DemoWindow::add_data_tabs(const std::string& filename) |
| 374 | { |
| 375 | // We can get the resource_dir from the filename by removing "example_" and ".cc". |
| 376 | const auto resource_dir = "/" + filename.substr(8, filename.size() - 11); |
| 377 | std::vector<std::string> resources; |
| 378 | try |
| 379 | { |
| 380 | resources = Gio::Resource::enumerate_children_global(resource_dir); |
| 381 | } |
| 382 | catch (const Gio::ResourceError& ex) |
| 383 | { |
| 384 | // Ignore this exception. It's no error, if resource_dir does not exist. |
| 385 | } |
| 386 | for (std::size_t i = 0; i < resources.size(); ++i) |
| 387 | { |
| 388 | const std::string resource_name = resource_dir + "/" + resources[i]; |
| 389 | Gtk::Widget* widget = nullptr; |
| 390 | Gtk::Image* image = new Gtk::Image(); |
| 391 | image->set_from_resource(resource_name); |
| 392 | if (image->get_pixbuf() || image->get_animation()) |
| 393 | { |
| 394 | widget = image; |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | // So we've used the best API available to figure out it's |
| 399 | // not an image. Let's try something else then. |
| 400 | delete image; |
| 401 | image = nullptr; |
| 402 | |
| 403 | Glib::RefPtr<const Glib::Bytes> bytes; |
| 404 | try |
| 405 | { |
| 406 | bytes = Gio::Resource::lookup_data_global(resource_name); |
| 407 | } |
| 408 | catch (const Gio::ResourceError& ex) |
| 409 | { |
| 410 | std::cerr << "Can't get data from resource '" << resource_name << "': " << ex.what() << std::endl; |
| 411 | continue; |
| 412 | } |
| 413 | gsize data_size = 0; |
| 414 | auto data = static_cast<const char*>(bytes->get_data(data_size)); |
| 415 | if (g_utf8_validate(data, data_size, nullptr)) |
| 416 | { |
| 417 | // Looks like it parses as text. Dump it into a TextWidget then! |
| 418 | auto textwidget = new TextWidget(false); |
| 419 | auto refBuffer = textwidget->get_buffer(); |
| 420 | refBuffer->set_text(data, data + data_size); |
| 421 | widget = textwidget; |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | std::cerr << "Don't know how to display resource '" << resource_name << "'" << std::endl; |
| 426 | continue; |
| 427 | } |
| 428 | } |
| 429 | widget->show_all(); |
| 430 | m_Notebook.append_page(*Gtk::manage(widget), Glib::ustring(resources[i])); |
| 431 | m_Notebook.child_property_tab_expand(*widget) = true; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void DemoWindow::remove_data_tabs() |
| 436 | { |
| 437 | // Remove all tabs except Info and Source. |
| 438 | for (auto i = m_Notebook.get_n_pages(); i-- > 2;) |
| 439 | { |
| 440 | m_Notebook.remove_page(i); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void DemoWindow::on_example_window_hide() |
| 445 | { |
| 446 | if(m_pWindow_Example) |
| 447 | { |
| 448 | if(const Gtk::TreeModel::iterator iter = m_refTreeStore->get_iter(m_TreePath)) |
| 449 | { |
| 450 | (*iter)[demo_columns().italic] = false; |
| 451 | |
| 452 | delete m_pWindow_Example; |
| 453 | m_pWindow_Example = nullptr; |
| 454 | } |
| 455 | } |
| 456 | } |