#include <wx/wxprec.h>
#include <wx/wx.h>
#include "../include/wxdragon.h"
#include "wxd_utils.h"
#include <wx/choicdlg.h>
#include <wx/listbox.h>
#include <wx/checklst.h>
WXD_EXPORTED wxd_MultiChoiceDialog_t*
wxd_MultiChoiceDialog_Create(const wxd_Window_t* parent, const char* message, const char* caption,
const wxd_ArrayString_t* choices, wxd_Style_t style, int x, int y,
int width, int height)
{
wxWindow* parent_wx = (wxWindow*)parent;
const wxArrayString* wxChoices = reinterpret_cast<const wxArrayString*>(choices);
wxPoint pos = (x == -1 && y == -1) ? wxDefaultPosition : wxPoint(x, y);
wxMultiChoiceDialog* dialog =
new wxMultiChoiceDialog(parent_wx, WXD_STR_TO_WX_STRING_UTF8_NULL_OK(message),
WXD_STR_TO_WX_STRING_UTF8_NULL_OK(caption), *wxChoices, style);
if (x != -1 && y != -1) {
dialog->SetPosition(pos);
}
if (width != -1 && height != -1) {
dialog->SetSize(width, height);
}
return reinterpret_cast<wxd_MultiChoiceDialog_t*>(dialog);
}
WXD_EXPORTED void
wxd_MultiChoiceDialog_GetSelections(const wxd_MultiChoiceDialog_t* self, int* selections,
int* count)
{
if (!self || !selections || !count)
return;
const wxMultiChoiceDialog* dialog = reinterpret_cast<const wxMultiChoiceDialog*>(self);
wxArrayInt selectedItems = dialog->GetSelections();
if (selectedItems.IsEmpty()) {
*count = 0;
return;
}
*count = selectedItems.GetCount();
for (size_t i = 0; i < selectedItems.GetCount(); i++) {
selections[i] = selectedItems[i];
}
}
WXD_EXPORTED void
wxd_MultiChoiceDialog_SetSelections(wxd_MultiChoiceDialog_t* self, const int* selections, int count)
{
if (!self || !selections || count <= 0)
return;
wxMultiChoiceDialog* dialog = (wxMultiChoiceDialog*)self;
wxArrayInt selectionArray;
for (int i = 0; i < count; i++) {
selectionArray.Add(selections[i]);
}
dialog->SetSelections(selectionArray);
}
WXD_EXPORTED void
wxd_MultiChoiceDialog_GetStringSelections(const wxd_MultiChoiceDialog_t* self,
wxd_ArrayString_t* selections)
{
if (!self || !selections)
return;
wxMultiChoiceDialog* dialog = (wxMultiChoiceDialog*)self;
wxArrayInt selectedIndices = dialog->GetSelections();
wxArrayString* wxSelections = reinterpret_cast<wxArrayString*>(selections);
wxSelections->Clear();
wxWindow* contentWin = wxDynamicCast(dialog->GetContentWindow(), wxWindow);
if (!contentWin) {
contentWin = dialog;
}
wxCheckListBox* checkListBox = NULL;
wxWindowList& children = contentWin->GetChildren();
for (wxWindowList::iterator it = children.begin(); it != children.end(); ++it) {
wxWindow* child = *it;
checkListBox = wxDynamicCast(child, wxCheckListBox);
if (checkListBox) {
break;
}
wxWindowList& grandchildren = child->GetChildren();
for (wxWindowList::iterator git = grandchildren.begin(); git != grandchildren.end();
++git) {
wxWindow* grandchild = *git;
checkListBox = wxDynamicCast(grandchild, wxCheckListBox);
if (checkListBox) {
break;
}
}
if (checkListBox) {
break;
}
}
if (checkListBox) {
for (size_t i = 0; i < selectedIndices.GetCount(); i++) {
int index = selectedIndices[i];
if (index >= 0 && index < checkListBox->GetCount()) {
wxSelections->Add(checkListBox->GetString(index));
}
}
}
else {
wxArrayString allChoices;
bool foundChoices = false;
wxListBox* listBox = NULL;
for (wxWindowList::iterator it = children.begin(); it != children.end(); ++it) {
wxWindow* child = *it;
listBox = wxDynamicCast(child, wxListBox);
if (listBox) {
break;
}
wxWindowList& grandchildren = child->GetChildren();
for (wxWindowList::iterator git = grandchildren.begin(); git != grandchildren.end();
++git) {
wxWindow* grandchild = *git;
listBox = wxDynamicCast(grandchild, wxListBox);
if (listBox) {
break;
}
}
if (listBox) {
break;
}
}
if (listBox) {
for (unsigned int i = 0; i < listBox->GetCount(); i++) {
allChoices.Add(listBox->GetString(i));
}
foundChoices = true;
}
if (foundChoices) {
for (size_t i = 0; i < selectedIndices.GetCount(); i++) {
int index = selectedIndices[i];
if (index >= 0 && index < (int)allChoices.GetCount()) {
wxSelections->Add(allChoices[index]);
}
}
}
else {
for (size_t i = 0; i < selectedIndices.GetCount(); i++) {
wxSelections->Add(wxString::Format("Item #%d", selectedIndices[i]));
}
}
}
}