| 1 | //============================================================================ | 
|---|
| 2 | // | 
|---|
| 3 | //   SSSS    tt          lll  lll | 
|---|
| 4 | //  SS  SS   tt           ll   ll | 
|---|
| 5 | //  SS     tttttt  eeee   ll   ll   aaaa | 
|---|
| 6 | //   SSSS    tt   ee  ee  ll   ll      aa | 
|---|
| 7 | //      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator" | 
|---|
| 8 | //  SS  SS   tt   ee      ll   ll  aa  aa | 
|---|
| 9 | //   SSSS     ttt  eeeee llll llll  aaaaa | 
|---|
| 10 | // | 
|---|
| 11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony | 
|---|
| 12 | // and the Stella Team | 
|---|
| 13 | // | 
|---|
| 14 | // See the file "License.txt" for information on usage and redistribution of | 
|---|
| 15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
|---|
| 16 | //============================================================================ | 
|---|
| 17 |  | 
|---|
| 18 | #include "bspf.hxx" | 
|---|
| 19 | #include "Control.hxx" | 
|---|
| 20 | #include "Dialog.hxx" | 
|---|
| 21 | #include "EventHandler.hxx" | 
|---|
| 22 | #include "OSystem.hxx" | 
|---|
| 23 | #include "EditTextWidget.hxx" | 
|---|
| 24 | #include "PopUpWidget.hxx" | 
|---|
| 25 | #include "Widget.hxx" | 
|---|
| 26 | #include "Font.hxx" | 
|---|
| 27 | #include "ComboDialog.hxx" | 
|---|
| 28 |  | 
|---|
| 29 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 30 | ComboDialog::ComboDialog(GuiObject* boss, const GUI::Font& font, | 
|---|
| 31 | const VariantList& combolist) | 
|---|
| 32 | : Dialog(boss->instance(), boss->parent(), font, "Add..."), | 
|---|
| 33 | myComboEvent(Event::NoType) | 
|---|
| 34 | { | 
|---|
| 35 | const int lineHeight   = font.getLineHeight(), | 
|---|
| 36 | fontWidth    = font.getMaxCharWidth(); | 
|---|
| 37 | int xpos, ypos; | 
|---|
| 38 | WidgetArray wid; | 
|---|
| 39 |  | 
|---|
| 40 | // Get maximum width of popupwidget | 
|---|
| 41 | int pwidth = 0; | 
|---|
| 42 | for (const auto& s : combolist) | 
|---|
| 43 | pwidth = std::max(font.getStringWidth(s.first), pwidth); | 
|---|
| 44 |  | 
|---|
| 45 | // Set real dimensions | 
|---|
| 46 | _w = 11 * fontWidth + pwidth-4 + 10 * 2; | 
|---|
| 47 | _h = 10 * (lineHeight + 4) + 10 + _th; | 
|---|
| 48 | xpos = 10; | 
|---|
| 49 | ypos = 10 + _th; | 
|---|
| 50 |  | 
|---|
| 51 | // Add event popup for 8 events | 
|---|
| 52 | auto  = [&](int idx, const string& label) | 
|---|
| 53 | { | 
|---|
| 54 | myEvents[idx] = new PopUpWidget(this, font, xpos, ypos, | 
|---|
| 55 | pwidth, lineHeight, combolist, label); | 
|---|
| 56 | wid.push_back(myEvents[idx]); | 
|---|
| 57 | ypos += lineHeight + 4; | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | myEvents[0] = nullptr;  ADD_EVENT_POPUP(0, "Event 1 "); | 
|---|
| 61 | myEvents[1] = nullptr;  ADD_EVENT_POPUP(1, "Event 2 "); | 
|---|
| 62 | myEvents[2] = nullptr;  ADD_EVENT_POPUP(2, "Event 3 "); | 
|---|
| 63 | myEvents[3] = nullptr;  ADD_EVENT_POPUP(3, "Event 4 "); | 
|---|
| 64 | myEvents[4] = nullptr;  ADD_EVENT_POPUP(4, "Event 5 "); | 
|---|
| 65 | myEvents[5] = nullptr;  ADD_EVENT_POPUP(5, "Event 6 "); | 
|---|
| 66 | myEvents[6] = nullptr;  ADD_EVENT_POPUP(6, "Event 7 "); | 
|---|
| 67 | myEvents[7] = nullptr;  ADD_EVENT_POPUP(7, "Event 8 "); | 
|---|
| 68 |  | 
|---|
| 69 | // Add Defaults, OK and Cancel buttons | 
|---|
| 70 | addDefaultsOKCancelBGroup(wid, font); | 
|---|
| 71 |  | 
|---|
| 72 | addToFocusList(wid); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 76 | void ComboDialog::show(Event::Type event, const string& name) | 
|---|
| 77 | { | 
|---|
| 78 | // Make sure the event is allowed | 
|---|
| 79 | if(event >= Event::Combo1 && event <= Event::Combo16) | 
|---|
| 80 | { | 
|---|
| 81 | myComboEvent = event; | 
|---|
| 82 | setTitle( "Add events for "+ name); | 
|---|
| 83 | open(); | 
|---|
| 84 | } | 
|---|
| 85 | else | 
|---|
| 86 | myComboEvent = Event::NoType; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 90 | void ComboDialog::loadConfig() | 
|---|
| 91 | { | 
|---|
| 92 | StringList events = instance().eventHandler().getComboListForEvent(myComboEvent); | 
|---|
| 93 |  | 
|---|
| 94 | uInt32 size = std::min(uInt32(events.size()), 8u); | 
|---|
| 95 | for(uInt32 i = 0; i < size; ++i) | 
|---|
| 96 | myEvents[i]->setSelected( "", events[i]); | 
|---|
| 97 |  | 
|---|
| 98 | // Fill any remaining items to 'None' | 
|---|
| 99 | if(size < 8) | 
|---|
| 100 | for(uInt32 i = size; i < 8; ++i) | 
|---|
| 101 | myEvents[i]->setSelected( "None", "-1"); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 105 | void ComboDialog::saveConfig() | 
|---|
| 106 | { | 
|---|
| 107 | StringList events; | 
|---|
| 108 | for(int i = 0; i < 8; ++i) | 
|---|
| 109 | events.push_back(myEvents[i]->getSelectedTag().toString()); | 
|---|
| 110 |  | 
|---|
| 111 | instance().eventHandler().setComboListForEvent(myComboEvent, events); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 115 | void ComboDialog::setDefaults() | 
|---|
| 116 | { | 
|---|
| 117 | for(int i = 0; i < 8; ++i) | 
|---|
| 118 | myEvents[i]->setSelected( "None", "-1"); | 
|---|
| 119 |  | 
|---|
| 120 | setDirty(); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
|---|
| 124 | void ComboDialog::handleCommand(CommandSender* sender, int cmd, | 
|---|
| 125 | int data, int id) | 
|---|
| 126 | { | 
|---|
| 127 | switch(cmd) | 
|---|
| 128 | { | 
|---|
| 129 | case GuiObject::kOKCmd: | 
|---|
| 130 | saveConfig(); | 
|---|
| 131 | close(); | 
|---|
| 132 | break; | 
|---|
| 133 |  | 
|---|
| 134 | case GuiObject::kDefaultsCmd: | 
|---|
| 135 | setDefaults(); | 
|---|
| 136 | break; | 
|---|
| 137 |  | 
|---|
| 138 | default: | 
|---|
| 139 | Dialog::handleCommand(sender, cmd, data, 0); | 
|---|
| 140 | break; | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|