axmol/core/ui/UIEditBox/UIEditBoxImpl-linux.cpp

116 lines
3.5 KiB
C++
Raw Normal View History

2015-06-06 14:20:16 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2015 hanxi
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
2015-06-06 14:20:16 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2015-06-06 14:20:16 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-06-06 14:20:16 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "ui/UIEditBox/UIEditBoxImpl-linux.h"
2015-06-06 14:20:16 +08:00
2022-07-15 19:17:01 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_LINUX)
2015-06-06 14:20:16 +08:00
2021-12-25 10:04:45 +08:00
# include "ui/UIEditBox/UIEditBox.h"
# include "2d/CCLabel.h"
# include "base/ccUTF8.h"
# include <gtk/gtk.h>
2015-06-06 14:20:16 +08:00
// destroy dialog when lost focus
2015-06-06 14:20:16 +08:00
static void dialogFocusOutCallback(GtkWidget* widget, gpointer user_data)
{
gtk_widget_destroy(widget);
}
2021-12-25 10:04:45 +08:00
bool LinuxInputBox(std::string& entryLine)
2015-06-06 14:20:16 +08:00
{
bool didChange = false;
2021-12-25 10:04:45 +08:00
GtkWidget* dialog;
GtkWidget* entry;
GtkWidget* contentArea;
2015-06-06 14:20:16 +08:00
gtk_init(0, NULL);
2021-12-25 10:04:45 +08:00
dialog = gtk_dialog_new();
entry = gtk_entry_new();
2015-06-06 14:20:16 +08:00
contentArea = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
gtk_container_add(GTK_CONTAINER(contentArea), entry);
gtk_dialog_add_button(GTK_DIALOG(dialog), "OK", 0);
gtk_entry_set_text(GTK_ENTRY(entry), entryLine.c_str());
g_signal_connect(dialog, "focus-out-event", G_CALLBACK(dialogFocusOutCallback), NULL);
gtk_window_set_keep_above(GTK_WINDOW(dialog), true);
gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_MENU);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
gtk_widget_show_all(dialog);
gint result = gtk_dialog_run(GTK_DIALOG(dialog));
2021-12-25 10:04:45 +08:00
switch (result)
2015-06-06 14:20:16 +08:00
{
case 0:
entryLine = gtk_entry_get_text(GTK_ENTRY(entry));
didChange = true;
break;
default:
2022-07-16 10:43:05 +08:00
// AXLOG("Undefined. Perhaps dialog was closed");
2015-06-06 14:20:16 +08:00
break;
}
gtk_widget_destroy(dialog);
2021-12-25 10:04:45 +08:00
while (g_main_context_iteration(NULL, false))
;
2015-06-06 14:20:16 +08:00
return didChange;
}
NS_AX_BEGIN
2015-06-06 14:20:16 +08:00
2021-12-25 10:04:45 +08:00
namespace ui
{
2015-06-06 14:20:16 +08:00
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
{
return new EditBoxImplLinux(pEditBox);
}
2021-12-25 10:04:45 +08:00
EditBoxImplLinux::EditBoxImplLinux(EditBox* pEditText) : EditBoxImplCommon(pEditText) {}
2015-06-06 14:20:16 +08:00
2021-12-25 10:04:45 +08:00
EditBoxImplLinux::~EditBoxImplLinux() {}
2015-06-06 14:20:16 +08:00
bool EditBoxImplLinux::isEditing()
{
return false;
}
void EditBoxImplLinux::nativeOpenKeyboard()
{
std::string text = this->getText();
2021-12-25 10:04:45 +08:00
bool didChange = LinuxInputBox(text);
2015-06-06 14:20:16 +08:00
if (didChange)
{
this->editBoxEditingDidEnd(text);
}
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2015-06-06 14:20:16 +08:00
NS_AX_END
2015-06-06 14:20:16 +08:00
2022-07-15 19:17:01 +08:00
#endif /* #if (AX_TARGET_PLATFORM == AX_PLATFORM_LINUX) */