overview.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <!DOCTYPE html>
  2. <!--[if IE]><![endif]-->
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  7. <title>Overview </title>
  8. <meta name="viewport" content="width=device-width">
  9. <meta name="title" content="Overview ">
  10. <meta name="generator" content="docfx 2.18.2.0">
  11. <link rel="shortcut icon" href="../favicon.ico">
  12. <link rel="stylesheet" href="../styles/docfx.vendor.css">
  13. <link rel="stylesheet" href="../styles/docfx.css">
  14. <link rel="stylesheet" href="../styles/main.css">
  15. <meta property="docfx:navrel" content="">
  16. <meta property="docfx:tocrel" content="">
  17. </head>
  18. <body data-spy="scroll" data-target="#affix">
  19. <div id="wrapper">
  20. <header>
  21. <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
  22. <div class="container">
  23. <div class="navbar-header">
  24. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
  25. <span class="sr-only">Toggle navigation</span>
  26. <span class="icon-bar"></span>
  27. <span class="icon-bar"></span>
  28. <span class="icon-bar"></span>
  29. </button>
  30. <a class="navbar-brand" href="../index.html">
  31. <img id="logo" class="svg" src="../logo.svg" alt="">
  32. </a>
  33. </div>
  34. <div class="collapse navbar-collapse" id="navbar">
  35. <form class="navbar-form navbar-right" role="search" id="search">
  36. <div class="form-group">
  37. <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
  38. </div>
  39. </form>
  40. </div>
  41. </div>
  42. </nav>
  43. <div class="subnav navbar navbar-default">
  44. <div class="container hide-when-search" id="breadcrumb">
  45. <ul class="breadcrumb">
  46. <li></li>
  47. </ul>
  48. </div>
  49. </div>
  50. </header>
  51. <div role="main" class="container body-content hide-when-search">
  52. <div class="article row grid">
  53. <div class="col-md-10">
  54. <article class="content wrap" id="_content" data-uid="">
  55. <h1 id="overview">Overview</h1>
  56. <p><code>Terminal.Gui</code> is a library intended to create console-based
  57. applications using C#. The framework has been designed to make it
  58. easy to write applications that will work on monochrome terminals, as
  59. well as modern color terminals with mouse support.</p>
  60. <p>This library works across Windows, Linux and MacOS.</p>
  61. <p>This library provides a text-based toolkit as works in a way similar
  62. to graphic toolkits. There are many controls that can be used to
  63. create your applications and it is event based, meaning that you
  64. create the user interface, hook up various events and then let the
  65. a processing loop run your application, and your code is invoked via
  66. one or more callbacks.</p>
  67. <p>The simplest application looks like this:</p>
  68. <pre><code class="lang-csharp">using Terminal.Gui;
  69. class Demo {
  70. static int Main ()
  71. {
  72. Application.Init ();
  73. var n = MessageBox.Query (50, 7,
  74. &quot;Question&quot;, &quot;Do you like console apps?&quot;, &quot;Yes&quot;, &quot;No&quot;);
  75. return n;
  76. }
  77. }
  78. </code></pre><p>This example shows a prompt and returns an integer value depending on
  79. which value was selected by the user (Yes, No, or if they use chose
  80. not to make a decision and instead pressed the ESC key).</p>
  81. <p>More interesting user interfaces can be created by composing some of
  82. the various views that are included. In the following sections, you
  83. will see how applications are put together.</p>
  84. <p>In the example above, you can see that we have initialized the runtime by calling the
  85. <a href="../api/Terminal.Gui/Terminal.Gui.Application.html#Terminal_Gui_Application_Init"><code>Init</code></a> method in the Application class - this sets up the environment, initializes the color
  86. schemes available for your application and clears the screen to start your application.</p>
  87. <p>The <a href="../api/Terminal.Gui/Terminal.Gui.Application.html"><code>Application</code></a> class, additionally creates an instance of the [Toplevel]((../api/Terminal.Gui/Terminal.Gui.Toplevel.html) class that is ready to be consumed,
  88. this instance is available in the <code>Application.Top</code> property, and can be used like this:</p>
  89. <pre><code class="lang-csharp">using Terminal.Gui;
  90. class Demo {
  91. static int Main ()
  92. {
  93. Application.Init ();
  94. var label = new Label (&quot;Hello World&quot;) {
  95. X = Pos.Center (),
  96. Y = Pos.Center (),
  97. Height = 1,
  98. };
  99. Application.Top.Add (label);
  100. Application.Run ();
  101. }
  102. }
  103. </code></pre><p>Typically, you will want your application to have more than a label, you might
  104. want a menu, and a region for your application to live in, the following code
  105. does this:</p>
  106. <pre><code class="lang-csharp">using Terminal.Gui;
  107. class Demo {
  108. static int Main ()
  109. {
  110. Application.Init ();
  111. var menu = new MenuBar (new MenuBarItem [] {
  112. new MenuBarItem (&quot;_File&quot;, new MenuItem [] {
  113. new MenuItem (&quot;_Quit&quot;, &quot;&quot;, () =&gt; {
  114. Application.RequestStop ();
  115. })
  116. }),
  117. });
  118. var win = new Window (&quot;Hello&quot;) {
  119. X = 0,
  120. Y = 1,
  121. Width = Dim.Fill (),
  122. Height = Dim.Fill () - 1
  123. };
  124. // Add both menu and win in a single call
  125. Application.Top.Add (menu, win);
  126. Application.Run ();
  127. }
  128. }
  129. </code></pre><h1 id="views">Views</h1>
  130. <p>All visible elements on a Terminal.Gui application are implemented as
  131. <a href="../api/Terminal.Gui/Terminal.Gui.View.html">Views</a>. Views are self-contained
  132. objects that take care of displaying themselves, can receive keyboard and mouse
  133. input and participate in the focus mechanism.</p>
  134. <p>Every view can contain an arbitrary number of children views. These are called
  135. the Subviews. You can add a view to an existing view, by calling the
  136. <a href="../api/Terminal.Gui/Terminal.Gui.View.html#Terminal_Gui_View_Add_Terminal_Gui_View_"><code>Add</code></a> method, for example, to add a couple of buttons to a UI, you can do this:</p>
  137. <pre><code class="lang-csharp">void SetupMyView (View myView)
  138. {
  139. var label = new Label (&quot;Username: &quot;) {
  140. X = 1,
  141. Y = 1,
  142. Width = 20,
  143. Height = 1
  144. };
  145. myView.Add (label);
  146. var username = new TextField (&quot;&quot;) {
  147. X = 1,
  148. Y = 2,
  149. Width = 30,
  150. Height = 1
  151. }
  152. myView.Add (username);
  153. }
  154. </code></pre><p>The container of a given view is called the <code>SuperView</code> and it is a property of every
  155. View.</p>
  156. <p>There are many views that you can use to spice up your application:</p>
  157. <p><a href="../api/Terminal.Gui/Terminal.Gui.Button.html">Buttons</a>, <a href="../api/Terminal.Gui/Terminal.Gui.Label.html">Labels</a>, <a href="../api/Terminal.Gui/Terminal.Gui.TextField.html">Text entry</a>, <a href="../api/Terminal.Gui/Terminal.Gui.TextView.html">Text view</a>, <a href="../api/Terminal.Gui/Terminal.Gui.RadioGroup.html">Radio buttons</a>, <a href="../api/Terminal.Gui/Terminal.Gui.CheckBox.html">Checkboxes</a>, <a href="../api/Terminal.Gui/Terminal.Gui.Dialog.html">Dialog boxes</a>, <a href="../api/Terminal.Gui/Terminal.Gui.MessageBox.html">Message boxes</a>, <a href="../api/Terminal.Gui/Terminal.Gui.Window.html">Windows</a>, <a href="../api/Terminal.Gui/Terminal.Gui.MenuBar.html">Menus</a>, <a href="../api/Terminal.Gui/Terminal.Gui.ListView.html">ListViews</a>, <a href="../api/Terminal.Gui/Terminal.Gui.FrameView.html">Frames</a>, <a href="../api/Terminal.Gui/Terminal.Gui.ProgressBar.html">ProgressBars</a>, <a href="../api/Terminal.Gui/Terminal.Gui.ScrollView.html">Scroll views</a> and <a href="../api/Terminal.Gui/Terminal.Gui.ScrollBarView.html">Scrollbars</a>.</p>
  158. <h2 id="layout">Layout</h2>
  159. <p><code>Terminal.Gui</code> supports two different layout systems, absolute and computed \
  160. (controlled by the <a href="../api/Terminal.Gui/Terminal.Gui.LayoutStyle.html"><code>LayoutStyle</code></a>
  161. property on the view.</p>
  162. <p>The absolute system is used when you want the view to be positioned exactly in
  163. one location and want to manually control where the view is. This is done
  164. by invoking your View constructor with an argument of type <a href="../api/Terminal.Gui/Terminal.Gui.Rect.html"><code>Rect</code></a>. When you do this, to change the
  165. position of the View, you can change the <code>Frame</code> property on the View.</p>
  166. <p>The computed layout system offers a few additional capabilities, like automatic
  167. centering, expanding of dimensions and a handful of other features. To use
  168. this you construct your object without an initial <code>Frame</code>, but set the
  169. <code>X</code>, <code>Y</code>, <code>Width</code> and <code>Height</code> properties after the object has been created.</p>
  170. <p>Examples:</p>
  171. <pre><code class="lang-csharp">
  172. // Dynamically computed
  173. var label = new Label (&quot;Hello&quot;) {
  174. X = 1,
  175. Y = Pos.Center (),
  176. Width = Dim.Fill (),
  177. Height = 1
  178. };
  179. // Absolute position using the provided rectangle
  180. var label2 = new Label (new Rect (1, 2, 20, 1), &quot;World&quot;)
  181. </code></pre><p>The computed layout system does not take integers, instead the <code>X</code> and <code>Y</code> properties are of type <a href="../api/Terminal.Gui/Terminal.Gui.Pos.html"><code>Pos</code></a> and the <code>Width</code> and <code>Height</code> properties are of type <a href="../api/Terminal.Gui/Terminal.Gui.Dim.html"><code>Dim</code></a> both which can be created implicitly from integer values.</p>
  182. <h3 id="the-pos-type">The <code>Pos</code> Type</h3>
  183. <p>The <code>Pos</code> type on <code>X</code> and <code>Y</code> offers a few options:</p>
  184. <ul>
  185. <li>Absolute position, by passing an integer</li>
  186. <li>Percentage of the parent&#39;s view size - <code>Pos.Percent(n)</code></li>
  187. <li>Anchored from the end of the dimension - <code>AnchorEnd(int margin=0)</code></li>
  188. <li>Centered, using <code>Center()</code></li>
  189. </ul>
  190. <p>The <code>Pos</code> values can be added or subtracted, like this:</p>
  191. <pre><code class="lang-csharp">// Set the X coordinate to 10 characters left from the center
  192. view.X = Pos.Center () - 10;
  193. view.Y = Pos.Percent (20);
  194. anotherView.X = AnchorEnd (10);
  195. anotherView.Width = 9;
  196. </code></pre><h3 id="the-dim-type">The <code>Dim</code> Type</h3>
  197. <p>The <code>Dim</code> type is used for the <code>Width</code> and <code>Height</code> properties on the View and offers
  198. the following options:</p>
  199. <ul>
  200. <li>Absolute size, by passing an integer</li>
  201. <li>Percentage of the parent&#39;s view size - <code>Dim.Percent(n)</code></li>
  202. <li>Fill to the end - <code>Dim.Fill ()</code></li>
  203. </ul>
  204. <p>Like, <code>Pos</code>, objects of type <code>Dim</code> can be added an subtracted, like this:</p>
  205. <pre><code class="lang-csharp">// Set the Width to be 10 characters less than filling
  206. // the remaining portion of the screen
  207. view.Width = Dim.Fill () - 10;
  208. view.Height = Dim.Percent(20) - 1;
  209. </code></pre><h1 id="toplevels-windows-and-dialogs">TopLevels, Windows and Dialogs.</h1>
  210. <p>Among the many kinds of views, you typically will create a <a href="../api/Terminal.Gui/Terminal.Gui.Toplevel.html">Toplevel</a> view (or any of its subclasses,
  211. like <a href="../api/Terminal.Gui/Terminal.Gui.Window.html">Window</a> or <a href="../api/Terminal.Gui/Terminal.Gui.Dialog.html">Dialog</a> which is special kind of views
  212. that can be executed modally - that is, the view can take over all input and returns
  213. only when the user chooses to complete their work there. </p>
  214. <p>The following sections cover the differences.</p>
  215. <h2 id="toplevel-views">TopLevel Views</h2>
  216. <p><a href="../api/Terminal.Gui/Terminal.Gui.Toplevel.html">Toplevel</a> views have no visible user interface elements and occupy an arbitrary portion of the screen.</p>
  217. <p>You would use a toplevel Modal view for example to launch an entire new experience in your application, one where you would have a new top-level menu for example. You
  218. typically would add a Menu and a Window to your Toplevel, it would look like this:</p>
  219. <pre><code class="lang-csharp">using Terminal.Gui;
  220. class Demo {
  221. static void Edit (string filename)
  222. {
  223. var top = new Toplevel () {
  224. X = 0,
  225. Y = 0,
  226. Width = Dim.Fill (),
  227. Height = Dim.Fill ()
  228. };
  229. var menu = new MenuBar (new MenuBarItem [] {
  230. new MenuBarItem (&quot;_File&quot;, new MenuItem [] {
  231. new MenuItem (&quot;_Close&quot;, &quot;&quot;, () =&gt; {
  232. Application.RequestStop ();
  233. })
  234. }),
  235. });
  236. // nest a window for the editor
  237. var win = new Window (filename) {
  238. X = 0,
  239. Y = 1,
  240. Width = Dim.Fill (),
  241. Height = Dim.Fill () - 1
  242. };
  243. var editor = new TextView () {
  244. X = 0,
  245. Y = 0,
  246. Width = Dim.Fill (),
  247. Height = Dim.Fill ()
  248. };
  249. editor.Text = System.IO.File.ReadAllText (filename);
  250. win.Add (editor);
  251. // Add both menu and win in a single call
  252. top.Add (win, menu);
  253. Application.Run (top);
  254. }
  255. }
  256. </code></pre><h2 id="window-views">Window Views</h2>
  257. <p><a href="../api/Terminal.Gui/Terminal.Gui.Window.html">Window</a> views extend the Toplevel view by providing a frame and a title around the toplevel - and can be moved on the screen with the mouse (caveat: code is currently disabled)</p>
  258. <p>From a user interface perspective, you might have more than one Window on the screen at a given time.</p>
  259. <h2 id="dialogs">Dialogs</h2>
  260. <p><a href="../api/Terminal.Gui/Terminal.Gui.Dialog.html">Dialog</a> are <a href="../api/Terminal.Gui/Terminal.Gui.Window.html">Window</a> objects that happen to be centered in the middle of the screen.</p>
  261. <p>Dialogs are instances of a Window that are centered in the screen, and are intended
  262. to be used modally - that is, they run, and they are expected to return a result
  263. before resuming execution of your application.</p>
  264. <p>Dialogs are a subclass of <code>Window</code> and additionally expose the
  265. <a href="https://migueldeicaza.github.io/gui.cs/api/Terminal.Gui/Terminal.Gui.Dialog.html#Terminal_Gui_Dialog_AddButton_Terminal_Gui_Button_"><code>AddButton</code></a> API which manages the layout
  266. of any button passed to it, ensuring that the buttons are at the bottom of the dialog.</p>
  267. <p>Example:</p>
  268. <pre><code class="lang-csharp">bool okpressed = false;
  269. var ok = new Button(&quot;Ok&quot;);
  270. var cancel = new Button(&quot;Cancel&quot;);
  271. var dialog = new Dialog (&quot;Quit&quot;, 60, 7, ok, cancel);
  272. </code></pre><p>Which will show something like this:</p>
  273. <pre><code>+- Quit -----------------------------------------------+
  274. | |
  275. | |
  276. | [ Ok ] [ Cancel ] |
  277. +------------------------------------------------------+
  278. </code></pre><h2 id="running-modally">Running Modally</h2>
  279. <p>To run your Dialog, Window or Toplevel modally, you will invoke the <code>Application.Run</code>
  280. method on the toplevel. It is up to your code and event handlers to invoke the <code>Application.RequestStop()</code> method to terminate the modal execution.</p>
  281. <pre><code class="lang-csharp">bool okpressed = false;
  282. var ok = new Button(3, 14, &quot;Ok&quot;) {
  283. Clicked = () =&gt; { Application.RequestStop (); okpressed = true; }
  284. };
  285. var cancel = new Button(10, 14, &quot;Cancel&quot;) {
  286. Clicked = () =&gt; Application.RequestStop ()
  287. };
  288. var dialog = new Dialog (&quot;Login&quot;, 60, 18, ok, cancel);
  289. var entry = new TextField () {
  290. X = 1,
  291. Y = 1,
  292. Width = Dim.Fill (),
  293. Height = 1
  294. };
  295. dialog.Add (entry);
  296. Application.Run (dialog);
  297. if (okpressed)
  298. Console.WriteLine (&quot;The user entered: &quot; + entry.Text);
  299. </code></pre><p>There is no return value from running modally, so your code will need to have a mechanism
  300. of indicating the reason that the execution of the modal dialog was completed, in the
  301. case above, the <code>okpressed</code> value is set to true if the user pressed or selected the Ok button.</p>
  302. <h1 id="input-handling">Input Handling</h1>
  303. <p>Every view has a focused view, and if that view has nested views, one of those is
  304. the focused view. This is called the focus chain, and at any given time, only one
  305. View has the focus. </p>
  306. <p>The library binds the key Tab to focus the next logical view,
  307. and the Shift-Tab combination to focus the previous logical view. </p>
  308. <p>Keyboard processing is divided in three stages: HotKey processing, regular processing and
  309. cold key processing. </p>
  310. <ul>
  311. <li><p>Hot key processing happens first, and it gives all the views in the current
  312. toplevel a chance to monitor whether the key needs to be treated specially. This
  313. for example handles the scenarios where the user pressed Alt-o, and a view with a
  314. highlighted &quot;o&quot; is being displayed.</p>
  315. </li>
  316. <li><p>If no view processed the hotkey, then the key is sent to the currently focused
  317. view.</p>
  318. </li>
  319. <li><p>If the key was not processed by the normal processing, all views are given
  320. a chance to process the keystroke in their cold processing stage. Examples
  321. include the processing of the &quot;return&quot; key in a dialog when a button in the
  322. dialog has been flagged as the &quot;default&quot; action.</p>
  323. </li>
  324. </ul>
  325. <p>The most common case is the normal processing, which sends the keystrokes to the
  326. currently focused view.</p>
  327. <p>Mouse events are processed in visual order, and the event will be sent to the
  328. view on the screen. The only exception is that no mouse events are delivered
  329. to background views when a modal view is running. </p>
  330. <h1 id="color-schemes">Color Schemes</h1>
  331. <p>All views have been configured with a color scheme that will work both in color
  332. terminals as well as the more limited black and white terminals. </p>
  333. <p>The various styles are captured in the <a href="../api/Terminal.Gui/Terminal.Gui.Colors.html"><code>Colors</code></a> class which defined color schemes for
  334. the normal views, the menu bar, popup dialog boxes and error dialog boxes.</p>
  335. <p>The <a href="../api/Terminal.Gui/Terminal.Gui.ColorScheme.html"><code>ColorScheme</code></a> represents
  336. four values, the color used for Normal text, the color used for normal text when
  337. a view is focused an the colors for the hot-keys both in focused and unfocused modes.</p>
  338. <p>By using <code>ColorSchemes</code> you ensure that your application will work correctbly both
  339. in color and black and white terminals.</p>
  340. </article>
  341. </div>
  342. <div class="hidden-sm col-md-2" role="complementary">
  343. <div class="sideaffix">
  344. <div class="contribution">
  345. <ul class="nav">
  346. <li>
  347. <a href="https://github.com/migueldeicaza/gui.cs/blob/master/docfx/articles/overview.md/#L1" class="contribution-link">Improve this Doc</a>
  348. </li>
  349. </ul>
  350. </div>
  351. <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
  352. <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
  353. </nav>
  354. </div>
  355. </div>
  356. </div>
  357. </div>
  358. <footer>
  359. <div class="grad-bottom"></div>
  360. <div class="footer">
  361. <div class="container">
  362. <span class="pull-right">
  363. <a href="#top">Back to top</a>
  364. </span>
  365. <span>Copyright © 2015-2017 Microsoft<br>Generated by <strong>DocFX</strong></span>
  366. </div>
  367. </div>
  368. </footer>
  369. </div>
  370. <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
  371. <script type="text/javascript" src="../styles/docfx.js"></script>
  372. <script type="text/javascript" src="../styles/main.js"></script>
  373. </body>
  374. </html>