summary refs log tree commit diff stats
path: root/tools/mapedit/src/mapselect_combo.cpp
blob: c8315b8e8511f1e6ee48980d6b9429f176e59007 (plain) (blame)
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
#include "mapselect_combo.h"
#include "map.h"

wxBEGIN_EVENT_TABLE(MapSelectComboPopup, wxTreeCtrl)
  EVT_LEFT_UP(MapSelectComboPopup::OnMouseClick)
wxEND_EVENT_TABLE()

MapSelectComboPopup::MapSelectComboPopup(wxTreeCtrl* tree, int initial)
{
  mapTree = tree;
  lastSelected = initial;

  Init();
}

void MapSelectComboPopup::Init()
{

}

bool MapSelectComboPopup::Create(wxWindow* parent)
{
  return wxTreeCtrl::Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT);
}

wxWindow* MapSelectComboPopup::GetControl()
{
  return this;
}

wxString MapSelectComboPopup::GetStringValue() const
{
  return ((MapPtrCtr*) GetItemData(GetFocusedItem()))->map->getTitle();
}

void MapSelectComboPopup::OnPopup()
{
  DeleteAllItems();
  
  wxTreeItemId newRoot = AddRoot("root");
  wxTreeItemId oldRoot = mapTree->GetRootItem();
  wxTreeItemIdValue cookie;
  for (wxTreeItemId it = mapTree->GetFirstChild(oldRoot, cookie); it.IsOk(); it = mapTree->GetNextChild(newRoot, cookie))
  {
    CopyNodes(it, newRoot);
  }
}

void MapSelectComboPopup::CopyNodes(wxTreeItemId from, wxTreeItemId to)
{
  MapPtrCtr* ctl1 = (MapPtrCtr*) mapTree->GetItemData(from);
  MapPtrCtr* ctl2 = nullptr;
  if (ctl1 != nullptr)
  {
    ctl2 = new MapPtrCtr(ctl1->map);
  }
  
  wxTreeItemId copied = mapTree->AppendItem(to, mapTree->GetItemText(from), -1, -1, ctl2);
  if (mapTree->ItemHasChildren(from))
  {
    wxTreeItemIdValue cookie;
    for (wxTreeItemId it = mapTree->GetFirstChild(from, cookie); it.IsOk(); it = mapTree->GetNextChild(from, cookie))
    {
      CopyNodes(it, copied);
    }
  }
  
  if (ctl1->map->getID() == lastSelected)
  {
    SelectItem(copied);
  }
  
  Expand(copied);
}

void MapSelectComboPopup::OnMouseClick(wxMouseEvent&)
{
  lastSelected = ((MapPtrCtr*) GetItemData(GetFocusedItem()))->map->getID();
    
  Dismiss();
}

int MapSelectComboPopup::GetSelectedMapID() const
{
  return lastSelected;
}

void MapSelectComboPopup::SetSelectedMapID(int id)
{
  lastSelected = id;
}