void FileList::update_layout() { hspace = rect.get_width()/2; vspace = 20; num_pages = static_cast<int>(directory.size()) / items_per_page(); if ((static_cast<int>(directory.size()) % items_per_page()) != 0 || num_pages == 0) num_pages += 1; if (page >= num_pages) page = num_pages-1; }
void FileList::draw(DrawingContext& gc) { GUIStyle::draw_lowered_box(gc, rect, Color(255, 255, 255)); int end = (page+1) * items_per_page(); if (end > int(directory.size())) end = static_cast<int>(directory.size()); int x = rect.left; int y = rect.top; for(System::Directory::iterator i = directory.begin() + page * items_per_page(); i != directory.begin() + end; ++i) { if (i->type == System::DE_DIRECTORY) gc.draw(directory_icon, Vector2i(x, y)); else if (i->type == System::DE_FILE) gc.draw(file_icon, Vector2i(x, y)); if ((click_item == -1 && (i - directory.begin()) == current_item) || (i - directory.begin()) == click_item) { if (click_item == current_item) gc.draw_fillrect(Rect(x, y, x + hspace, y + vspace), Color(0, 0, 255)); else gc.draw_rect(Rect(x, y, x + hspace, y + vspace), Color(0, 0, 255)); } gc.print_left(Fonts::verdana11, Vector2i(x + 4, y + 3), ((i->type == System::DE_DIRECTORY) ? "[DIR] " : "[FILE] ") + i->name); y += 20; if (y > rect.bottom - vspace) { y = rect.top; x += hspace; } } }
void FileList::on_pointer_move (int x, int y) { x = x - rect.left; y = y - rect.top; current_item = Math::clamp(0, y / vspace, rect.get_height() / vspace - 1) + Math::clamp(0, x / hspace, rect.get_width() / hspace - 1) * (rect.get_height()/vspace); current_item += page * items_per_page(); if (current_item < 0 || current_item >= int(directory.size())) current_item = -1; }
void FileList::set_directory(const std::string& pathname, const std::string& pattern) { m_direction = pathname; try { directory = System::opendir(pathname, pattern); } catch(const std::exception& err) { log_error("%1%", err.what()); directory.clear(); } directory.push_back(System::DirectoryEntry("..", System::DE_DIRECTORY)); std::sort(directory.begin(), directory.end(), DirectorySorter()); num_pages = static_cast<int>(directory.size()) / items_per_page(); if ((static_cast<int>(directory.size()) % items_per_page()) != 0 || num_pages == 0) num_pages += 1; page = 0; }
void set_items_per_page(unsigned n) { items_per_page_ = n; page_count_ = (count() / items_per_page()) + (count() % items_per_page() ? 1 : 0); }
//! Offset of the current selection relative to the start of the //! current page. unsigned page_offset() const { return current_index_ - page() * items_per_page(); }
//! Get the index of the end of the current page. unsigned page_end_index() const { return std::min(count(), page_begin_index() + items_per_page()); }
//! Take ownership of the strings (move). void set_items(string_container&& strings) { items_ = std::move(strings); page_count_ = (count() / items_per_page()) + (count() % items_per_page() ? 1 : 0); }