ByteVector String::data(Type t) const { switch(t) { case Latin1: { ByteVector v(size(), 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) *p++ = static_cast<char>(*it); return v; } case UTF8: if(!d->data.empty()) { ByteVector v(size() * 4 + 1, 0); UTF16toUTF8(d->data.c_str(), d->data.size(), v.data(), v.size()); v.resize(::strlen(v.data())); return v; } else { return ByteVector::null; } case UTF16: { ByteVector v(2 + size() * 2, 0); char *p = v.data(); // Assume that if we're doing UTF16 and not UTF16BE that we want little // endian encoding. (Byte Order Mark) *p++ = '\xff'; *p++ = '\xfe'; for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it & 0xff); *p++ = static_cast<char>(*it >> 8); } return v; } case UTF16BE: { ByteVector v(size() * 2, 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it >> 8); *p++ = static_cast<char>(*it & 0xff); } return v; } case UTF16LE: { ByteVector v(size() * 2, 0); char *p = v.data(); for(wstring::const_iterator it = d->data.begin(); it != d->data.end(); it++) { *p++ = static_cast<char>(*it & 0xff); *p++ = static_cast<char>(*it >> 8); } return v; } default: { debug("String::data() - Invalid Type value."); return ByteVector::null; } } }
void widgetHandleSDLEvent(const SDL_Event *sdlEvt) { // Last known location of the mouse static point previousMouseLoc = { -1, -1 }; switch (sdlEvt->type) { case SDL_MOUSEMOTION: { eventMouse evtMouse; evtMouse.event = widgetCreateEvent(EVT_MOUSE_MOVE); // Location evtMouse.loc.x = sdlEvt->motion.x; evtMouse.loc.y = sdlEvt->motion.y; // Previous location evtMouse.previousLoc = previousMouseLoc; // Update the previous location previousMouseLoc = evtMouse.loc; windowHandleEventForWindowVector((event *) &evtMouse); break; } case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { eventMouseBtn evtMouseBtn; evtMouseBtn.event = widgetCreateEvent(sdlEvt->button.state == SDL_PRESSED ? EVT_MOUSE_DOWN : EVT_MOUSE_UP); // Location evtMouseBtn.loc.x = sdlEvt->button.x; evtMouseBtn.loc.y = sdlEvt->button.y; // Update the previous location previousMouseLoc = evtMouseBtn.loc; // Button pressed/released evtMouseBtn.button = SDLButtonToMouseButton(sdlEvt->button.button); windowHandleEventForWindowVector((event *) &evtMouseBtn); break; } case SDL_KEYDOWN: case SDL_KEYUP: { eventKey evtKey; evtKey.event = widgetCreateEvent(sdlEvt->key.type == SDL_KEYDOWN ? EVT_KEY_DOWN : EVT_KEY_UP); // Key pressed/released evtKey.keycode = SDLKeyToEventKeycode(sdlEvt->key.keysym.sym); // Modifier keys if (sdlEvt->key.keysym.mod & KMOD_CTRL) { evtKey.ctrl = true; } if (sdlEvt->key.keysym.mod & KMOD_ALT) { evtKey.alt = true; } if (sdlEvt->key.keysym.mod & KMOD_SHIFT) { evtKey.shift = true; } // Only needed/works on SDL 1.2! if (sdlEvt->key.keysym.unicode) { const uint16_t utf16[2] = { sdlEvt->key.keysym.unicode, 0 }; char *utf8; // Create the text event eventText evtText; evtText.event = widgetCreateEvent(EVT_TEXT); // Convert the UTF-16 string to UTF-8 utf8 = UTF16toUTF8(utf16, NULL); // Set the text of the event to UTF-8 string evtText.utf8 = utf8; // Dispatch the event windowHandleEventForWindowVector((event *) &evtText); // Free the memory allocated by UTF16toUTF8 free(utf8); } windowHandleEventForWindowVector((event *) &evtKey); break; } } }