home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / gtk / joypadconfig.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-11  |  7.4 KB  |  277 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. #include "joypadconfig.h"
  20.  
  21. #include <string.h>
  22.  
  23. #include "intl.h"
  24.  
  25. namespace VBA
  26. {
  27.  
  28. guint * JoypadConfig::puiAt(int _iIndex)
  29. {
  30.   guint * puiMember;
  31.  
  32.   switch (_iIndex)
  33.   {
  34.   case 0:
  35.     puiMember = &m_uiUp;
  36.     break;
  37.   case 1:
  38.     puiMember = &m_uiDown;
  39.     break;
  40.   case 2:
  41.     puiMember = &m_uiLeft;
  42.     break;
  43.   case 3:
  44.     puiMember = &m_uiRight;
  45.     break;
  46.   case 4:
  47.     puiMember = &m_uiA;
  48.     break;
  49.   case 5:
  50.     puiMember = &m_uiB;
  51.     break;
  52.   case 6:
  53.     puiMember = &m_uiL;
  54.     break;
  55.   case 7:
  56.     puiMember = &m_uiR;
  57.     break;
  58.   case 8:
  59.     puiMember = &m_uiSelect;
  60.     break;
  61.   case 9:
  62.     puiMember = &m_uiStart;
  63.     break;
  64.   case 10:
  65.     puiMember = &m_uiSpeed;
  66.     break;
  67.   case 11:
  68.     puiMember = &m_uiCapture;
  69.     break;
  70.   default:
  71.     puiMember = NULL;
  72.   }
  73.  
  74.   return puiMember;
  75. }
  76.  
  77. int JoypadConfig::iFind(guint _uiKeycode)
  78. {
  79.   for (guint i = 0; i < 12; i++)
  80.   {
  81.     if (*puiAt(i) == _uiKeycode)
  82.     {
  83.       return i;
  84.     }
  85.   }
  86.  
  87.   return -1;
  88. }
  89.  
  90. void JoypadConfig::vSetDefault()
  91. {
  92.   guint auiKeyval[] =
  93.   {
  94.     GDK_Up, GDK_Down, GDK_Left, GDK_Right,
  95.     GDK_z, GDK_x, GDK_a, GDK_s,
  96.     GDK_BackSpace, GDK_Return,
  97.     GDK_space, GDK_F12
  98.   };
  99.  
  100.   for (guint i = 0; i < G_N_ELEMENTS(auiKeyval); i++)
  101.   {
  102.     GdkKeymapKey * pstKeys;
  103.     int iKeys;
  104.  
  105.     if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(),
  106.                                           auiKeyval[i],
  107.                                           &pstKeys,
  108.                                           &iKeys))
  109.     {
  110.       *puiAt(i) = pstKeys[0].keycode;
  111.       g_free(pstKeys);
  112.     }
  113.     else
  114.     {
  115.       *puiAt(i) = 0;
  116.     }
  117.   }
  118. }
  119.  
  120. Keymap * JoypadConfig::poCreateKeymap() const
  121. {
  122.   Keymap * poKeymap = new Keymap();
  123.  
  124.   poKeymap->vRegister(m_uiUp,      KeyUp      );
  125.   poKeymap->vRegister(m_uiDown,    KeyDown    );
  126.   poKeymap->vRegister(m_uiLeft,    KeyLeft    );
  127.   poKeymap->vRegister(m_uiRight,   KeyRight   );
  128.   poKeymap->vRegister(m_uiA,       KeyA       );
  129.   poKeymap->vRegister(m_uiB,       KeyB       );
  130.   poKeymap->vRegister(m_uiL,       KeyL       );
  131.   poKeymap->vRegister(m_uiR,       KeyR       );
  132.   poKeymap->vRegister(m_uiSelect,  KeySelect  );
  133.   poKeymap->vRegister(m_uiStart,   KeyStart   );
  134.   poKeymap->vRegister(m_uiSpeed,   KeySpeed   );
  135.   poKeymap->vRegister(m_uiCapture, KeyCapture );
  136.  
  137.   return poKeymap;
  138. }
  139.  
  140. JoypadConfigDialog::JoypadConfigDialog(GtkDialog * _pstDialog,
  141.                                        const Glib::RefPtr<Gnome::Glade::Xml> & _poXml) :
  142.   Gtk::Dialog(_pstDialog)
  143. {
  144.   m_puiCurrentKeyCode = NULL;
  145.  
  146.   memset(&m_oConfig, 0, sizeof(m_oConfig));
  147.  
  148.   m_poOkButton = dynamic_cast<Gtk::Button *>(_poXml->get_widget("JoypadOkButton"));
  149.  
  150.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadUpEntry")));
  151.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadDownEntry")));
  152.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadLeftEntry")));
  153.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadRightEntry")));
  154.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadAEntry")));
  155.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadBEntry")));
  156.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadLEntry")));
  157.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadREntry")));
  158.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadSelectEntry")));
  159.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadStartEntry")));
  160.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadSpeedEntry")));
  161.   m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadCaptureEntry")));
  162.  
  163.   for (guint i = 0; i < m_oEntries.size(); i++)
  164.   {
  165.     Gtk::Entry * poEntry = m_oEntries[i];
  166.  
  167.     poEntry->signal_focus_in_event().connect(SigC::bind<guint>(
  168.                                                SigC::slot(*this, &JoypadConfigDialog::bOnEntryFocusIn),
  169.                                                i));
  170.     poEntry->signal_focus_out_event().connect(SigC::slot(*this, &JoypadConfigDialog::bOnEntryFocusOut));
  171.   }
  172.  
  173.   vUpdateEntries();
  174. }
  175.  
  176. JoypadConfigDialog::~JoypadConfigDialog()
  177. {
  178. }
  179.  
  180. void JoypadConfigDialog::vSetConfig(const JoypadConfig & _roConfig)
  181. {
  182.   m_oConfig = _roConfig;
  183.   vUpdateEntries();
  184. }
  185.  
  186. void JoypadConfigDialog::vUpdateEntries()
  187. {
  188.   for (guint i = 0; i < m_oEntries.size(); i++)
  189.   {
  190.     guint uiKeyval = 0;
  191.     gdk_keymap_translate_keyboard_state(gdk_keymap_get_default(),
  192.                                         *m_oConfig.puiAt(i),
  193.                                         (GdkModifierType)0,
  194.                                         0,
  195.                                         &uiKeyval,
  196.                                         NULL,
  197.                                         NULL,
  198.                                         NULL);
  199.     const char * csName = gdk_keyval_name(uiKeyval);
  200.     if (csName == NULL)
  201.     {
  202.       m_oEntries[i]->set_text(_("<Undefined>"));
  203.     }
  204.     else
  205.     {
  206.       m_oEntries[i]->set_text(csName);
  207.     }
  208.   }
  209. }
  210.  
  211. bool JoypadConfigDialog::bOnEntryFocusIn(GdkEventFocus * _pstEvent,
  212.                                          guint           _uiEntry)
  213. {
  214.   m_uiCurrentEntry    = _uiEntry;
  215.   m_puiCurrentKeyCode = m_oConfig.puiAt(_uiEntry);
  216.  
  217.   return false;
  218. }
  219.  
  220. bool JoypadConfigDialog::bOnEntryFocusOut(GdkEventFocus * _pstEvent)
  221. {
  222.   m_puiCurrentKeyCode = NULL;
  223.  
  224.   return false;
  225. }
  226.  
  227. bool JoypadConfigDialog::on_key_press_event(GdkEventKey * _pstEvent)
  228. {
  229.   if (m_puiCurrentKeyCode == NULL)
  230.   {
  231.     return Gtk::Dialog::on_key_press_event(_pstEvent);
  232.   }
  233.  
  234.   *m_puiCurrentKeyCode = 0;
  235.   int iFound = m_oConfig.iFind(_pstEvent->hardware_keycode);
  236.   if (iFound >= 0)
  237.   {
  238.     *m_oConfig.puiAt(iFound) = 0;
  239.     m_oEntries[iFound]->set_text(_("<Undefined>"));
  240.   }
  241.  
  242.   *m_puiCurrentKeyCode = _pstEvent->hardware_keycode;
  243.  
  244.   guint uiKeyval = 0;
  245.   gdk_keymap_translate_keyboard_state(gdk_keymap_get_default(),
  246.                                       _pstEvent->hardware_keycode,
  247.                                       (GdkModifierType)0,
  248.                                       0,
  249.                                       &uiKeyval,
  250.                                       NULL,
  251.                                       NULL,
  252.                                       NULL);
  253.  
  254.   const char * csName = gdk_keyval_name(uiKeyval);
  255.   if (csName == NULL)
  256.   {
  257.     m_oEntries[m_uiCurrentEntry]->set_text(_("<Undefined>"));
  258.   }
  259.   else
  260.   {
  261.     m_oEntries[m_uiCurrentEntry]->set_text(csName);
  262.   }
  263.  
  264.   if (m_uiCurrentEntry + 1 < m_oEntries.size())
  265.   {
  266.     m_oEntries[m_uiCurrentEntry + 1]->grab_focus();
  267.   }
  268.   else
  269.   {
  270.     m_poOkButton->grab_focus();
  271.   }
  272.  
  273.   return true;
  274. }
  275.  
  276. } // namespace VBA
  277.