GInputBox.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <LibGUI/GInputBox.h>
  2. #include <LibGUI/GBoxLayout.h>
  3. #include <LibGUI/GLabel.h>
  4. #include <LibGUI/GButton.h>
  5. #include <LibGUI/GTextEditor.h>
  6. #include <stdio.h>
  7. GInputBox::GInputBox(const String& prompt, const String& title, GObject* parent)
  8. : GDialog(parent)
  9. , m_prompt(prompt)
  10. {
  11. set_title(title);
  12. build();
  13. }
  14. GInputBox::~GInputBox()
  15. {
  16. }
  17. void GInputBox::build()
  18. {
  19. auto* widget = new GWidget;
  20. set_main_widget(widget);
  21. int text_width = widget->font().width(m_prompt);
  22. set_rect(x(), y(), text_width + 80, 80);
  23. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  24. widget->set_fill_with_background_color(true);
  25. widget->layout()->set_margins({ 8, 8, 8, 8 });
  26. widget->layout()->set_spacing(8);
  27. auto* label = new GLabel(m_prompt, widget);
  28. label->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  29. label->set_preferred_size({ text_width, 16 });
  30. m_text_editor = new GTextEditor(GTextEditor::SingleLine, widget);
  31. m_text_editor->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  32. m_text_editor->set_preferred_size({ 0, 16 });
  33. auto* button_container_outer = new GWidget(widget);
  34. button_container_outer->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  35. button_container_outer->set_preferred_size({ 0, 16 });
  36. button_container_outer->set_layout(make<GBoxLayout>(Orientation::Vertical));
  37. auto* button_container_inner = new GWidget(button_container_outer);
  38. button_container_inner->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  39. button_container_inner->layout()->set_spacing(8);
  40. m_cancel_button = new GButton(button_container_inner);
  41. m_cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  42. m_cancel_button->set_preferred_size({ 0, 16 });
  43. m_cancel_button->set_caption("Cancel");
  44. m_cancel_button->on_click = [this] (auto&) {
  45. dbgprintf("GInputBox: Cancel button clicked\n");
  46. done(ExecCancel);
  47. };
  48. m_ok_button = new GButton(button_container_inner);
  49. m_ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  50. m_ok_button->set_preferred_size({ 0, 16 });
  51. m_ok_button->set_caption("OK");
  52. m_ok_button->on_click = [this] (auto&) {
  53. dbgprintf("GInputBox: OK button clicked\n");
  54. m_text_value = m_text_editor->text();
  55. done(ExecOK);
  56. };
  57. m_text_editor->on_return_pressed = [this] (auto&) {
  58. m_ok_button->click();
  59. };
  60. m_text_editor->on_escape_pressed = [this] (auto&) {
  61. m_cancel_button->click();
  62. };
  63. m_text_editor->set_focus(true);
  64. }