| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2019-2022 Igara Studio S.A. |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "app/ui/filename_field.h" |
| 12 | |
| 13 | #include "app/i18n/strings.h" |
| 14 | #include "app/ui/skin/skin_theme.h" |
| 15 | #include "base/fs.h" |
| 16 | #include "ui/box.h" |
| 17 | #include "ui/button.h" |
| 18 | #include "ui/entry.h" |
| 19 | #include "ui/message.h" |
| 20 | |
| 21 | namespace app { |
| 22 | |
| 23 | using namespace ui; |
| 24 | |
| 25 | FilenameField::FilenameField(const Type type, |
| 26 | const std::string& pathAndFilename) |
| 27 | : m_entry(type == EntryAndButton ? new ui::Entry(1024, ""): nullptr) |
| 28 | , m_button(type == EntryAndButton ? Strings::select_file_browse(): |
| 29 | Strings::select_file_text()) |
| 30 | { |
| 31 | setFocusStop(true); |
| 32 | if (m_entry) { |
| 33 | m_entry->setExpansive(true); |
| 34 | addChild(m_entry); |
| 35 | } |
| 36 | else { |
| 37 | m_button.setExpansive(true); |
| 38 | } |
| 39 | addChild(&m_button); |
| 40 | |
| 41 | setFilename(pathAndFilename); |
| 42 | |
| 43 | if (m_entry) { |
| 44 | m_entry->Change.connect( |
| 45 | [this]{ |
| 46 | m_file = m_entry->text(); |
| 47 | Change(); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | m_button.Click.connect( |
| 52 | [this]{ |
| 53 | std::string fn = SelectFile(); |
| 54 | if (!fn.empty()) { |
| 55 | setFilename(fn); |
| 56 | } |
| 57 | }); |
| 58 | |
| 59 | initTheme(); |
| 60 | } |
| 61 | |
| 62 | std::string FilenameField::filename() const |
| 63 | { |
| 64 | return base::join_path(m_path, m_file); |
| 65 | } |
| 66 | |
| 67 | void FilenameField::setFilename(const std::string& fn) |
| 68 | { |
| 69 | if (filename() == fn) |
| 70 | return; |
| 71 | |
| 72 | m_path = base::get_file_path(fn); |
| 73 | m_file = base::get_file_name(fn); |
| 74 | updateWidgets(); |
| 75 | } |
| 76 | |
| 77 | bool FilenameField::onProcessMessage(ui::Message* msg) |
| 78 | { |
| 79 | switch (msg->type()) { |
| 80 | case ui::kFocusEnterMessage: |
| 81 | if (manager()->getFocus() == this) { |
| 82 | if (m_entry) |
| 83 | m_entry->requestFocus(); |
| 84 | else |
| 85 | m_button.requestFocus(); |
| 86 | } |
| 87 | break; |
| 88 | } |
| 89 | return HBox::onProcessMessage(msg); |
| 90 | } |
| 91 | |
| 92 | void FilenameField::onInitTheme(ui::InitThemeEvent& ev) |
| 93 | { |
| 94 | HBox::onInitTheme(ev); |
| 95 | setChildSpacing(0); |
| 96 | |
| 97 | auto theme = skin::SkinTheme::get(this); |
| 98 | ui::Style* style = theme->styles.miniButton(); |
| 99 | if (style) |
| 100 | m_button.setStyle(style); |
| 101 | } |
| 102 | |
| 103 | void FilenameField::updateWidgets() |
| 104 | { |
| 105 | if (m_entry) |
| 106 | m_entry->setText(m_file); |
| 107 | else if (m_file.empty()) |
| 108 | m_button.setText(Strings::select_file_text()); |
| 109 | else |
| 110 | m_button.setText(m_file); |
| 111 | |
| 112 | Change(); |
| 113 | } |
| 114 | |
| 115 | } // namespace app |
| 116 |