File: combo-button.cpp

package info (click to toggle)
libretro-bsnes-mercury 094%2Bgit20160126-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,636 kB
  • sloc: cpp: 109,056; ansic: 3,097; makefile: 638; xml: 11; sh: 1
file content (77 lines) | stat: -rw-r--r-- 2,364 bytes parent folder | download | duplicates (5)
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
namespace phoenix {

void pComboButton::append(string text) {
  SendMessage(hwnd, CB_ADDSTRING, 0, (LPARAM)(wchar_t*)utf16_t(text));
  if(SendMessage(hwnd, CB_GETCOUNT, 0, 0) == 1) setSelection(0);
}

Size pComboButton::minimumSize() {
  unsigned maximumWidth = 0;
  for(auto& text : comboButton.state.text) maximumWidth = max(maximumWidth, pFont::size(hfont, text).width);
  return {maximumWidth + 24, pFont::size(hfont, " ").height + 10};
}

void pComboButton::remove(unsigned selection) {
  locked = true;
  SendMessage(hwnd, CB_DELETESTRING, selection, 0);
  locked = false;

  if(selection == comboButton.state.selection) comboButton.setSelection(0);
}

void pComboButton::reset() {
  SendMessage(hwnd, CB_RESETCONTENT, 0, 0);
}

void pComboButton::setGeometry(Geometry geometry) {
  //height = minimum drop-down list height; use CB_SETITEMHEIGHT to control actual widget height
  pWidget::setGeometry({geometry.x, geometry.y, geometry.width, 1});
  RECT rc;
  GetWindowRect(hwnd, &rc);
  unsigned adjustedHeight = geometry.height - ((rc.bottom - rc.top) - SendMessage(hwnd, CB_GETITEMHEIGHT, (WPARAM)-1, 0));
  SendMessage(hwnd, CB_SETITEMHEIGHT, (WPARAM)-1, adjustedHeight);
}

void pComboButton::setSelection(unsigned selection) {
  SendMessage(hwnd, CB_SETCURSEL, selection, 0);
}

void pComboButton::setText(unsigned selection, string text) {
  locked = true;
  SendMessage(hwnd, CB_DELETESTRING, selection, 0);
  SendMessage(hwnd, CB_INSERTSTRING, selection, (LPARAM)(wchar_t*)utf16_t(text));
  setSelection(comboButton.state.selection);
  locked = false;
}

void pComboButton::constructor() {
  hwnd = CreateWindow(
    L"COMBOBOX", L"",
    WS_CHILD | WS_TABSTOP | CBS_DROPDOWNLIST | CBS_HASSTRINGS,
    0, 0, 0, 0,
    parentHwnd, (HMENU)id, GetModuleHandle(0), 0
  );
  SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&comboButton);
  setDefaultFont();
  for(auto& text : comboButton.state.text) append(text);
  setSelection(comboButton.state.selection);
  synchronize();
}

void pComboButton::destructor() {
  DestroyWindow(hwnd);
}

void pComboButton::orphan() {
  destructor();
  constructor();
}

void pComboButton::onChange() {
  unsigned selection = SendMessage(hwnd, CB_GETCURSEL, 0, 0);
  if(selection == comboButton.state.selection) return;
  comboButton.state.selection = selection;
  if(comboButton.onChange) comboButton.onChange();
}

}