FreeBASIC  0.91.0
io_width.c
Go to the documentation of this file.
1 /* console width() for Windows */
2 /* code based on PDCurses, Win32 port by Chris Szurgot (szurgot@itribe.net) */
3 
4 #include "../fb.h"
5 #include "fb_private_console.h"
6 
7 int fb_ConsoleWidth( int cols, int rows )
8 {
9  COORD size, max;
10  int cur, do_change = FALSE;
11  int ncols, nrows;
12 
14 
16  return 0;
17 
18  max = GetLargestConsoleWindowSize( __fb_out_handle );
19  fb_hConsoleGetWindow( NULL, NULL, &ncols, &nrows );
20 
21  if( cols > 0 )
22  {
23  size.X = cols;
24  do_change = TRUE;
25  }
26  else
27  {
28  size.X = (SHORT) ncols;
29  }
30 
31  if( rows > 0 )
32  {
33  size.Y = rows;
34  do_change = TRUE;
35  }
36  else
37  {
38  size.Y = (SHORT) nrows;
39  }
40 
41  cur = size.X | (size.Y << 16);
42 
43  if( do_change == FALSE )
44  return cur;
45 
46  SMALL_RECT rect;
47  rect.Left = rect.Top = 0;
48  rect.Right = size.X - 1;
49  if( rect.Right > max.X )
50  rect.Right = max.X;
51 
52  rect.Bottom = rect.Top + size.Y - 1;
53  if( rect.Bottom > max.Y )
54  rect.Bottom = max.Y;
55 
56  /* Ensure that the window isn't larger than the destination screen
57  * buffer size */
58  int do_resize = FALSE;
59  SMALL_RECT rectRes;
60  if( rect.Bottom < (nrows-1) )
61  {
62  do_resize = TRUE;
63  memcpy( &rectRes, &rect, sizeof(SMALL_RECT) );
64  if( rectRes.Right >= ncols )
65  rectRes.Right = ncols - 1;
66  }
67  else if( rect.Right < (ncols-1) )
68  {
69  do_resize = TRUE;
70  memcpy( &rectRes, &rect, sizeof(SMALL_RECT) );
71  if( rectRes.Bottom >= nrows )
72  rectRes.Bottom = nrows - 1;
73  }
74 
75  if( do_resize )
76  {
77  int i;
78  for( i = 0; i < FB_CONSOLE_MAXPAGES; i++ )
79  {
80  if( __fb_con.pgHandleTb[i] != NULL )
81  SetConsoleWindowInfo( __fb_con.pgHandleTb[i], TRUE, &rectRes );
82  }
83  }
84 
85  /* Now set the screen buffer size and ensure that the window is
86  * large enough to show the whole buffer */
87  int i;
88  for( i = 0; i < FB_CONSOLE_MAXPAGES; i++ )
89  {
90  if( __fb_con.pgHandleTb[i] != NULL )
91  {
92  SetConsoleScreenBufferSize( __fb_con.pgHandleTb[i], size );
93  SetConsoleWindowInfo( __fb_con.pgHandleTb[i], TRUE, &rect );
94  }
95  }
96 
97  /* Re-enable updating */
101 
102  return cur;
103 }