overview.html 21 KB

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