overview.html 22 KB

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