int *select_sort_by(int *data, const size_t length, int (*sort_by)(const int, const int)) { size_t sort_range; size_t i; size_t j; size_t head; size_t head_index; sort_range = length - 1; for (i = 0; i < sort_range; ++i) { head_index = i; head = data[head_index]; for (j = i + 1; j < length; ++j) { if (sort_by(data[j], head)) { head_index = j; head = data[head_index]; } } data[head_index] = data[i]; data[i] = head; } return data; }
/************************************************************************************ * TOP LEVEL FUNCTIONS * *▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼*/ int *insert_sort_by(int *data, const size_t length, int (*sort_by)(const int, const int)) { int temp; int j; for (int i = 1; i < length; ++i) { temp = data[i]; j = i - 1; /* if current el should be ordered ahead of next previous el */ while ((j > -1) && sort_by(temp, data[j])) { /* shift next previous el forward, making room for current el */ data[j + 1] = data[j]; --j; } /* drop current el into its proper position */ data[j + 1] = temp; } return data; }
bool all_unique_by_less(Compare comp, const Container& xs) { internal::check_compare_for_container<Compare, Container>(); if (size_of_cont(xs) < 2) return true; return size_of_cont(unique(sort_by(comp, xs))) == size_of_cont(xs); }
void menu::set_sorter(sorter *s) { if(sortby_ >= 0) { //clear an existing sort sort_by(-1); } sorter_ = s; sortreversed_ = false; sortby_ = -1; }
int *merge_sort_by(int *data, const size_t length, int (*sort_by)(const int, const int)) { /* determine sentinel value ("least" value according to sort_by) */ const int sentinel = sort_by(INT_MIN, INT_MAX) ? INT_MAX : INT_MIN; do_split(data, 0, length - 1, sort_by, sentinel); return data; }
void menu::handle_event(const SDL_Event& event) { scrollarea::handle_event(event); if (height()==0 || hidden()) return; if(event.type == SDL_KEYDOWN) { // Only pass key events if we have the focus if (focus(&event)) key_press(event.key.keysym.sym); } else if(!mouse_locked() && ((event.type == SDL_MOUSEBUTTONDOWN && (event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT)) || event.type == DOUBLE_CLICK_EVENT)) { int x = 0; int y = 0; if(event.type == SDL_MOUSEBUTTONDOWN) { x = event.button.x; y = event.button.y; } else { x = reinterpret_cast<long>(event.user.data1); y = reinterpret_cast<long>(event.user.data2); } const int item = hit(x,y); if(item != -1) { set_focus(true); move_selection_to(item); if(click_selects_) { show_result_ = true; } if(event.type == DOUBLE_CLICK_EVENT) { if (ignore_next_doubleclick_) { ignore_next_doubleclick_ = false; } else { double_clicked_ = true; last_was_doubleclick_ = true; if(!silent_) { sound::play_UI_sound(game_config::sounds::button_press); } } } else if (last_was_doubleclick_) { // If we have a double click as the next event, it means // this double click was generated from a click that // already has helped in generating a double click. SDL_Event ev; SDL_PeepEvents(&ev, 1, SDL_PEEKEVENT, SDL_EVENTMASK(DOUBLE_CLICK_EVENT)); if (ev.type == DOUBLE_CLICK_EVENT) { ignore_next_doubleclick_ = true; } last_was_doubleclick_ = false; } } if(sorter_ != NULL) { const int heading = hit_heading(x,y); if(heading >= 0 && sorter_->column_sortable(heading)) { sort_by(heading); } } } else if(!mouse_locked() && event.type == SDL_MOUSEMOTION) { if(click_selects_) { const int item = hit(event.motion.x,event.motion.y); const bool out = (item == -1); if (out_ != out) { out_ = out; invalidate_row_pos(selected_); } if (item != -1) { move_selection_to(item); } } const int heading_item = hit_heading(event.motion.x,event.motion.y); if(heading_item != highlight_heading_) { highlight_heading_ = heading_item; invalidate_heading(); } } }
select_scanned_file_dlg::select_scanned_file_dlg(wxWindow *parent, std::vector<playlist_file_cptr> const &playlists, wxString const &orig_file_name) : wxDialog{parent, wxID_ANY, Z("Select file to add"), wxDefaultPosition, wxSize{1000, 650}, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxMINIMIZE_BOX | wxMAXIMIZE_BOX} , m_playlists{playlists} , m_selected_playlist_idx{0} , m_geometry_saver{this, "select_scanned_file_dlg"} , m_sorted_by_column{} , m_sorted_ascending{} { // controls left column auto st_scanned_files = new wxStaticText(this, wxID_ANY, Z("Scanned files")); auto sl_left = new wxStaticLine(this); m_lc_files = new wxListCtrl( this, ID_LC_PLAYLIST_FILE, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL); // controls right column auto st_details = new wxStaticText(this, wxID_ANY, Z("Details")); auto sl_right = new wxStaticLine(this); auto st_duration = new wxStaticText(this, wxID_ANY, Z("Duration:")); m_st_duration = new wxStaticText(this, wxID_ANY, wxEmptyString); auto st_size = new wxStaticText(this, wxID_ANY, Z("Size:")); m_st_size = new wxStaticText(this, wxID_ANY, wxEmptyString); auto st_chapters = new wxStaticText(this, wxID_ANY, Z("Number of chapters:")); m_st_chapters = new wxStaticText(this, wxID_ANY, wxEmptyString); auto st_tracks = new wxStaticText(this, wxID_ANY, Z("Tracks:")); m_lc_tracks = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL); auto st_playlist_items = new wxStaticText(this, wxID_ANY, Z("Playlist items:")); m_lc_items = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL); // button controls auto b_add = new wxButton(this, wxID_OK, Z("&Add")); auto b_cancel = new wxButton(this, wxID_CANCEL, Z("&Cancel")); b_add->SetDefault(); // list controls m_lc_files->InsertColumn(0, Z("File name")); m_lc_files->InsertColumn(1, Z("Duration"), wxLIST_FORMAT_RIGHT); m_lc_files->InsertColumn(2, Z("Size"), wxLIST_FORMAT_RIGHT); m_lc_tracks->InsertColumn(0, Z("Type")); m_lc_tracks->InsertColumn(1, Z("Codec")); m_lc_tracks->InsertColumn(2, Z("Language")); m_lc_items->InsertColumn(0, Z("File name")); m_lc_items->InsertColumn(1, Z("Directory")); m_sort_arrows.Add(wx_get_png(sort_ascending)); m_sort_arrows.Add(wx_get_png(sort_descending)); m_lc_files->SetImageList(&m_sort_arrows, wxIMAGE_LIST_SMALL); // fill "files" with data m_lc_files->Hide(); long idx = 0; for (auto &playlist : m_playlists) { auto id = m_lc_files->InsertItem(idx, wxFileName{playlist->file_name}.GetFullName()); m_lc_files->SetItemData(id, idx); auto item = wxListItem{}; item.SetId(id); item.SetColumn(1); item.SetText(wxU(format_timecode(playlist->duration, 0))); item.SetAlign(wxLIST_FORMAT_RIGHT); item.SetMask(wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT); m_lc_files->SetItem(item); item.SetColumn(2); item.SetText(wxU(format_file_size(playlist->size))); m_lc_files->SetItem(item); if (orig_file_name == playlist->file_name) { m_lc_files->SetItemState(id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); m_selected_playlist_idx = idx; } ++idx; } sort_by(0, true); m_lc_files->SetColumnWidth(0, wxLIST_AUTOSIZE); m_lc_files->SetColumnWidth(1, wxLIST_AUTOSIZE); m_lc_files->SetColumnWidth(2, wxLIST_AUTOSIZE); m_lc_files->Show(); // layout auto siz_left_column = new wxBoxSizer(wxVERTICAL); siz_left_column->Add(st_scanned_files, 0, wxALL, 5); siz_left_column->Add(sl_left, 0, wxALL | wxEXPAND, 5); siz_left_column->Add(m_lc_files, 1, wxALL | wxEXPAND, 5); auto siz_info = new wxGridSizer(3, 2, 0, 0); siz_info->Add(st_duration, 0, wxALL, 5); siz_info->Add(m_st_duration, 0, wxALL, 5); siz_info->Add(st_size, 0, wxALL, 5); siz_info->Add(m_st_size, 0, wxALL, 5); siz_info->Add(st_chapters, 0, wxALL, 5); siz_info->Add(m_st_chapters, 0, wxALL, 5); auto siz_right_column = new wxBoxSizer(wxVERTICAL); siz_right_column->Add(st_details, 0, wxALL, 5); siz_right_column->Add(sl_right, 0, wxEXPAND | wxALL, 5); siz_right_column->Add(siz_info, 0, wxEXPAND, 5); siz_right_column->Add(st_tracks, 0, wxALL, 5); siz_right_column->Add(m_lc_tracks, 1, wxALL | wxEXPAND, 5); siz_right_column->Add(st_playlist_items, 0, wxALL, 5); siz_right_column->Add(m_lc_items, 1, wxALL | wxEXPAND, 5); auto siz_columns = new wxBoxSizer(wxHORIZONTAL); siz_columns->Add(siz_left_column, 1, wxEXPAND, 5); siz_columns->Add(siz_right_column, 1, wxEXPAND, 5); auto siz_buttons = new wxBoxSizer(wxHORIZONTAL); siz_buttons->AddStretchSpacer(); siz_buttons->Add(b_add, 0, wxALL, 5); siz_buttons->AddStretchSpacer(); siz_buttons->Add(b_cancel, 0, wxALL, 5); siz_buttons->AddStretchSpacer(); auto siz_all = new wxBoxSizer(wxVERTICAL); siz_all->Add(siz_columns, 1, wxEXPAND, 5); siz_all->Add(siz_buttons, 0, wxEXPAND, 5); SetSizerAndFit(siz_all); Layout(); Centre(wxBOTH); m_geometry_saver.set_default_size(1000, 650, true).restore(); update_info(); }
void select_scanned_file_dlg::on_column_clicked(wxListEvent &evt) { size_t column = evt.GetColumn(); sort_by(column, column == m_sorted_by_column ? !m_sorted_ascending : m_sorted_ascending); }