main.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Optional.h>
  27. #include <AK/String.h>
  28. #include <AK/StringBuilder.h>
  29. #include <AK/Vector.h>
  30. #include <LibCore/File.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/BoxLayout.h>
  33. #include <LibGUI/Button.h>
  34. #include <LibGUI/Desktop.h>
  35. #include <LibGUI/Label.h>
  36. #include <LibGUI/MessageBox.h>
  37. #include <LibGUI/StackWidget.h>
  38. #include <LibGUI/Window.h>
  39. #include <LibGfx/Bitmap.h>
  40. #include <LibGfx/Font.h>
  41. #include <stdio.h>
  42. #include <unistd.h>
  43. #include "BackgroundWidget.h"
  44. #include "TextWidget.h"
  45. #include "UnuncheckableButton.h"
  46. struct ContentPage {
  47. String menu_name;
  48. String title;
  49. String icon = String::empty();
  50. Vector<String> content;
  51. };
  52. Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
  53. {
  54. const auto error = Optional<Vector<ContentPage>>();
  55. auto file = Core::File::construct(path);
  56. if (!file->open(Core::IODevice::ReadOnly))
  57. return error;
  58. Vector<ContentPage> pages;
  59. StringBuilder current_output_line;
  60. bool started = false;
  61. ContentPage current;
  62. while (true) {
  63. auto buffer = file->read_line(4096);
  64. if (buffer.is_null()) {
  65. if (file->error()) {
  66. file->close();
  67. return error;
  68. }
  69. break;
  70. }
  71. auto line = String((char*)buffer.data());
  72. if (line.length() > 1)
  73. line = line.substring(0, line.length() - 1); // remove newline
  74. switch (line[0]) {
  75. case '*':
  76. dbg() << "menu_item line:\t" << line;
  77. if (started)
  78. pages.append(current);
  79. else
  80. started = true;
  81. current = {};
  82. current.menu_name = line.substring(2, line.length() - 2);
  83. break;
  84. case '$':
  85. dbg() << "icon line: \t" << line;
  86. current.icon = line.substring(2, line.length() - 2);
  87. break;
  88. case '>':
  89. dbg() << "title line:\t" << line;
  90. current.title = line.substring(2, line.length() - 2);
  91. break;
  92. case '\n':
  93. dbg() << "newline";
  94. if (!current_output_line.to_string().is_empty())
  95. current.content.append(current_output_line.to_string());
  96. current_output_line.clear();
  97. break;
  98. case '#':
  99. dbg() << "comment line:\t" << line;
  100. break;
  101. default:
  102. dbg() << "content line:\t" << line;
  103. if (current_output_line.length() != 0)
  104. current_output_line.append(' ');
  105. current_output_line.append(line);
  106. break;
  107. }
  108. }
  109. if (started) {
  110. current.content.append(current_output_line.to_string());
  111. pages.append(current);
  112. }
  113. file->close();
  114. return pages;
  115. }
  116. int main(int argc, char** argv)
  117. {
  118. if (pledge("stdio shared_buffer rpath unix cpath fattr", nullptr) < 0) {
  119. perror("pledge");
  120. return 1;
  121. }
  122. GUI::Application app(argc, argv);
  123. if (pledge("stdio shared_buffer rpath", nullptr) < 0) {
  124. perror("pledge");
  125. return 1;
  126. }
  127. if (unveil("/res", "r") < 0) {
  128. perror("unveil");
  129. return 1;
  130. }
  131. unveil(nullptr, nullptr);
  132. Optional<Vector<ContentPage>> _pages = parse_welcome_file("/res/welcome.txt");
  133. if (!_pages.has_value()) {
  134. GUI::MessageBox::show("Could not open Welcome file.", "Welcome", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, nullptr);
  135. return 1;
  136. }
  137. auto pages = _pages.value();
  138. auto window = GUI::Window::construct();
  139. window->set_title("Welcome");
  140. Gfx::Rect window_rect { 0, 0, 640, 360 };
  141. window_rect.center_within(GUI::Desktop::the().rect());
  142. window->set_resizable(true);
  143. window->set_rect(window_rect);
  144. auto& background = window->set_main_widget<BackgroundWidget>();
  145. background.set_fill_with_background_color(false);
  146. background.set_layout<GUI::VerticalBoxLayout>();
  147. background.layout()->set_margins({ 16, 8, 16, 8 });
  148. background.layout()->set_spacing(8);
  149. background.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  150. //
  151. // header
  152. //
  153. auto header = background.add<GUI::Label>();
  154. header->set_font(Gfx::Font::load_from_file("/res/fonts/PebbletonBold11.font"));
  155. header->set_text("Welcome to SerenityOS!");
  156. header->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  157. header->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  158. header->set_preferred_size(0, 30);
  159. //
  160. // main section
  161. //
  162. auto main_section = background.add<GUI::Widget>();
  163. main_section->set_layout<GUI::HorizontalBoxLayout>();
  164. main_section->layout()->set_margins({ 0, 0, 0, 0 });
  165. main_section->layout()->set_spacing(8);
  166. main_section->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  167. auto menu = main_section->add<GUI::Widget>();
  168. menu->set_layout<GUI::VerticalBoxLayout>();
  169. menu->layout()->set_margins({ 0, 0, 0, 0 });
  170. menu->layout()->set_spacing(4);
  171. menu->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  172. menu->set_preferred_size(100, 0);
  173. auto stack = main_section->add<GUI::StackWidget>();
  174. stack->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  175. bool first = true;
  176. for (auto& page : pages) {
  177. auto content = stack->add<GUI::Widget>();
  178. content->set_layout<GUI::VerticalBoxLayout>();
  179. content->layout()->set_margins({ 0, 0, 0, 0 });
  180. content->layout()->set_spacing(8);
  181. content->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  182. auto title_box = content->add<GUI::Widget>();
  183. title_box->set_layout<GUI::HorizontalBoxLayout>();
  184. title_box->layout()->set_spacing(4);
  185. title_box->set_preferred_size(0, 16);
  186. title_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  187. if (!page.icon.is_empty()) {
  188. auto icon = title_box->add<GUI::Label>();
  189. icon->set_icon(Gfx::Bitmap::load_from_file(page.icon));
  190. icon->set_preferred_size(16, 16);
  191. icon->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  192. }
  193. auto content_title = title_box->add<GUI::Label>();
  194. content_title->set_font(Gfx::Font::default_bold_font());
  195. content_title->set_text(page.title);
  196. content_title->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  197. content_title->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  198. content_title->set_preferred_size(0, 10);
  199. for (auto& paragraph : page.content) {
  200. auto content_text = content->add<TextWidget>();
  201. content_text->set_font(Gfx::Font::default_font());
  202. content_text->set_text(paragraph);
  203. content_text->set_text_alignment(Gfx::TextAlignment::TopLeft);
  204. content_text->set_line_height(12);
  205. content_text->wrap_and_set_height();
  206. }
  207. auto menu_option = menu->add<UnuncheckableButton>();
  208. menu_option->set_font(Gfx::Font::default_font());
  209. menu_option->set_text(page.menu_name);
  210. menu_option->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  211. menu_option->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  212. menu_option->set_preferred_size(0, 20);
  213. menu_option->set_checkable(true);
  214. menu_option->set_exclusive(true);
  215. if (first)
  216. menu_option->set_checked(true);
  217. menu_option->on_click = [content = content.ptr(), &stack] {
  218. stack->set_active_widget(content);
  219. content->invalidate_layout();
  220. };
  221. first = false;
  222. }
  223. window->show();
  224. return app.exec();
  225. }