xtd 0.2.0
Loading...
Searching...
No Matches
test_forms.cpp

tests forms control

Windows

macOS

Gnome

#include <xtd/diagnostics/process>
#include <xtd/forms/application>
#include <xtd/forms/form>
#include <xtd/forms/text_box>
#include <xtd/environment>
#include <xtd/startup>
class virtual_console : public xtd::forms::text_box {
public:
virtual_console() {
multiline(true);
text(prompt);
select(text().size() - 1, 0);
}
protected:
xtd::drawing::size default_size() const noexcept override {return {300, 200};}
switch (e.key_code()) {
case xtd::forms::keys::down: key_down_pressed(e); break;
case xtd::forms::keys::up: key_up_pressed(e); break;
case xtd::forms::keys::enter: key_enter_pressed(e); break;
default: break;
}
}
private:
xtd::ustring get_command() const {
auto pos = text().last_index_of(prompt);
if (pos != xtd::ustring::npos) {
pos += prompt.size();
return text().substring(pos);
}
return "";
}
void key_down_pressed(xtd::forms::key_event_args& e) {
e.handled(true);
}
void key_up_pressed(xtd::forms::key_event_args& e) {
e.handled(true);
}
void key_enter_pressed(xtd::forms::key_event_args& e) {
auto command_line = get_command();
if (!command_line.empty()) {
try {
auto args = command_line.split({' '});
start_info.file_name(args[0]);
if (args.size() > 1)
start_info.arguments(xtd::ustring::join(" ", xtd::collections::specialized::string_collection(args.begin() + 1, args.end())));
start_info.use_shell_execute(false);
start_info.redirect_standard_error(true);
start_info.redirect_standard_output(true);
if (start_info.file_name() == "cd" && !start_info.arguments().empty()) {
} else {
append_text(xtd::ustring::format("cd: no such file or directory: {}", start_info.arguments()));
}
} else {
process.start_info(start_info);
process.start();
std::istream& standard_error = process.standard_error();
std::istream& standard_output = process.standard_output();
xtd::io::stream_reader error_reader(standard_error);
while (!error_reader.end_of_stream()) {
append_text(error_reader.read_line());
}
xtd::io::stream_reader output_reader(standard_output);
while (!output_reader.end_of_stream()) {
append_text(output_reader.read_line());
}
}
} catch (...) {
append_text(xtd::ustring::format("command not found: {}", start_info.file_name()));
}
}
append_text(prompt);
e.handled(true);
}
};
using namespace xtd;
using namespace xtd::windows::forms;
class form_main : public form {
public:
static auto main() {
application::run(form_main());
}
form_main() {
text("Virtual console");
client_size({600, 600});
virtual_console.dock(dock_style::fill);
virtual_console.parent(*this);
}
private:
class virtual_console virtual_console;
};
startup_(form_main::main);
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:33
Specifies a set of values that are used when you start a process.
Definition process_start_info.h:39
bool redirect_standard_output() const noexcept
Gets a value that indicates whether the textual output of an application is written to the xtd::diagn...
const xtd::ustring & file_name() const noexcept
Gets the application or document to start.
bool redirect_standard_error() const noexcept
Gets a value that indicates whether the error output of an application is written to the xtd::diagnos...
const xtd::ustring & arguments() const noexcept
Gets the set of command-line arguments to use when starting the application.
bool use_shell_execute() const noexcept
Gets a value indicating whether to use the operating system shell to start the process.
Provides access to local and remote processes and enables you to start and stop local system processe...
Definition process.h:49
Represents an ARGB (alpha, red, green, blue) color.
Definition color.h:49
static const xtd::drawing::color lime
Gets a system-defined color that has an ARGB value of 0xFF00FF00. This field is constant.
Definition color.h:290
static const xtd::drawing::color black
Gets a system-defined color that has an ARGB value of 0xFF000000. This field is constant.
Definition color.h:83
static font_family generic_monospace() noexcept
Gets a generic monospace font_family.
Defines a particular format for text, including font face, size, and style attributes....
Definition font.h:45
Stores an ordered pair of integers, which specify a height and width.
Definition size.h:31
static xtd::ustring get_folder_path(environment::special_folder folder)
Gets the path to the system special folder that is identified by the specified enumeration.
@ home
The file system directory that contains home folder.
static xtd::ustring new_line() noexcept
Gets the newline string defined for this environment.
static xtd::ustring current_directory()
Gets the fully qualified path of the current working directory.
virtual drawing::color fore_color() const noexcept
Gets the foreground color of the control.
virtual void on_key_down(key_event_args &e)
Raises the xtd::forms::control::key_down event.
virtual drawing::font default_font() const noexcept
Gets the default font of the control.
virtual drawing::font font() const noexcept
Gets the font of the text displayed by the control.
virtual drawing::color back_color() const noexcept
Gets the background color for the control.
virtual drawing::size default_size() const noexcept
Gets the default size of the control.
virtual drawing::size size() const noexcept
Gets the height and width of the control.
Provides data for the xtd::forms::control::key_down or xtd::forms::control::key_up event.
Definition key_event_args.h:25
virtual bool multiline() const noexcept
Gets a value indicating whether this is a multiline text box control.
Represents a standard Windows text box.
Definition text_box.h:31
drawing::color default_back_color() const noexcept override
Gets the default background color of the control.
drawing::color default_fore_color() const noexcept override
Gets the default foreground color of the control.
void append_text(const xtd::ustring &value) override
Appends text to the current text of a text box.
const xtd::ustring & text() const noexcept override
Gets the text associated with this control.
static bool exists(const xtd::ustring &path)
Determines whether the given path refers to an existing directory on disk.
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.h:28
bool end_of_stream() const
Gets a value that indicates whether the current stream position is at the end of the stream.
virtual xtd::ustring read_line()
Reads a line of characters from the current stream and returns the data as a string.
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:52
size_t last_index_of(value_type value) const noexcept
Reports the index of the last occurrence of the specified character in this tring.
static ustring join(const ustring separator, const collection_t &values) noexcept
Concatenates a specified separator string between each element of a specified object array,...
Definition ustring.h:1176
ustring substring(size_t start_index) const noexcept
Retrieves a substring from this instance. The substring starts at a specified character position and ...
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition ustring.h:1068
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
@ process
The IME PROCESS key.
@ up
The UP key.
@ down
The DOWN key.
@ select
The SELECT key.
@ enter
The ENTER key.
@ text
The xtd::forms::status_bar_panel displays text in the standard font.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10