overview.html 21 KB

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