There was no forum for just general programming, so I decided to put this here.

Just out of curiosity, does anyone know how you get from your programs running in terminal or command prompt to having them opening in their own window, with their own GUI? If anyone could shed some light on the subject, it would be much appreciated, seeing as this topic, now that I think of it, is really bugging me. Thanks.

These days the console is a window with specific, default restrictions. That being said, to port code written for a console to a window requires you to know functions that are specific for any given GUI. Simply put, there is no simple tansition that I know of. Sure, the concept of variables translates pretty straight forward, functions are used in both console and GUIs, and loops are really important in both, etc. However, most GUIs are event driven. That means the program begins on a certain course but the user can adjust the course of the course of the program by creating an event. An event can be many different things. For example, click in one place and something happens. Click someplace else and something else happens. Or for that matter, click somewhere else and maybe nothing happens at all. The downstroke of a keypress is different from the upstroke of keypress. A mouse button is different from a key. If the pointer is over one part of the screen it has one appearance and if over another it has another, etc. In addition to menus you have buttons and checkboxes that can act like menus. You have to repaint the window, or a part thereof, when certain things happen, etc. So, if you feel pretty comfortable with console code, then find a tutorial or pick up a book about your favorite, or desired, GUI and dive in.

GUI existed as of almost 30 years ago.
Consoles were invented before that time to replace a teletypewritermachine (tty) which was a machine producing a papertape with the output(text) of the computer. Such a terminal screen was quite an improvement!
Nowadays such consoles are simulated in a gui environment.
Vista, XP, IS gui. There are several tools out there to start up your own gui programming. Since you post here I suggest Visual Studio C++.

What platform are you working with Orwell84? Windows, Linux, ??? If Windows, then you either need to learn the Windows Api, or adopt some Windows Application Framework such as MFC, .NET, etc. For myself, I just about strictly use the Windows Api directly for Windows programming.

For Linux I dabble in lots of different stuff GTK, xlib, motif, etc.

There's lots of choices. It seems just about everybody and all books start out with console mode programs because a beginner has to learn about variable types, decision structures, etc., first. Once you get that stuff down you can think about learning GUI programming. That's a C++ prospective.

If you start with vb.net or c# some of the books start pretty much directly with GUI coding. Hope this info helps.

And now, naturally, I have another question. You have to know the operating system before making a GUI application, right? When writing your own operating system, wouldn't you have to incorporate your own way to read GUI?

>>When writing your own operating system, wouldn't you have to incorporate your own way to read GUI?

Not necessarily, if you make it compatable with X11 then you can use most standard *nix GUIs. The GUI is only a method for humans to interact with the monitor and keyboard. It has nothing to do with the operating system itsef.

Linux it is then! If you would like I'll post some XLib & GTK programs that show what it looks like to get A GUI window up and running. But to understand it you really need a decent knowledge of C / C++. It gives me a good deal of satisfaction that C is taken pretty seriously in Linux development. The Linux toolkit (application framework) I use is GTK. I'd look into installing that. Here is a link to a good GTK forum...

http://www.gtkforums.com/

Not sure what is here at daniweb in terms of Linux C++ development. Perhaps there is a forum here on that too.

To get an x windows gui program running in Ubuntu 8.1 or any other Linux/Unix you need to have the xlib development libraries installed. You can search the UBuntu forums for how to do that. Here is a command line (Terminal) description of getting an xlib program running. 1st here is a program that produces a blank window that can be correctly closed with the [x] button in the title bar...

/* 
   Form1.c
   Compiler        = gcc
   Program Name    = Form1.c
   Executble Name  = Form1
   Link Path       = /usr/X11R6/lib
   Link            = X11
 
   Command Line:   gcc Form1.c  -o Form1  -L/usr/X11R6/lib  -lX11  
*/

#include <X11/Xlib.h>

int main(int argc, char* argv[])
{
 char* display_name;
 Display* display;
 int screen_num;
 Window hWnd;

 display_name=(char*)getenv("DISPLAY");
 display=XOpenDisplay(display_name);
 if(!display)
    return (1);
 screen_num=DefaultScreen(display);
 hWnd=
 XCreateSimpleWindow
 (
    display,                         //pointer to Display struct, kind of GDI object?
    RootWindow(display,screen_num),  //parent of hWnd
    50, 50, 500, 450,                //x, y, width, height
    2,                               //window border width
    BlackPixel(display,screen_num),  //foreground 'brush'
    WhitePixel(display,screen_num)   //background 'brush'
 );
 XMapWindow(display,hWnd);           //ShowWindow(hWnd);
 XSync(display,False);               //UpdateWindow()  ???
 sleep(30);                          //prevent from ending???
 XCloseDisplay(display);             //Shut down connection to X

 return (0);
}

The command line compiling info is at top. It will produce a program named Form1. To run it put a shortcut to it on your desktop or do this in the Terminal...

fred@CodeWarrior:~$ cd code/xlib
fred@CodeWarrior:~/code/xlib$ gcc Form1.c -o Form1 -L/usr/X11R6/lib -lX11
fred@CodeWarrior:~/code/xlib$ ./Form1

My name is fred and my computer's name is CodeWarrior. I had the source code in a subdirectory of my /home folder /code/xlib/Form1.c. To run the program you need the ./Form1 stuff in Linux.

Here is a program that does a bunch of string minipulations on our English alphabet and prints it out to the window. Note it has part of my C++ string class in it at top...

/*  
   g++ strOut.cpp -o strOut -L/usr/X11R6/lib -lX11  
*/
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class String
{
 public:
   String() //Uninitialized Constructor - Does nothing
   {
    pStrBuffer=NULL;
   }


   ~String() //Destructor
   {
    if(pStrBuffer) //Don't try to [] delete a NULL pointer!
       delete [] pStrBuffer;
   }


   String &operator=(const char* pStr)
   {
    if(this->pStrBuffer)
       delete [] pStrBuffer;
    pStrBuffer=new char[strlen(pStr)+1];
    strcpy(pStrBuffer,pStr);

    return *this;
   }


   String &operator=(const String& strRight) //Overloaded operator = for assigning
   {                                         //another String to a String
    if(this==&strRight)
       return *this;
    delete [] this->pStrBuffer;
    pStrBuffer=new char[strlen(strRight.pStrBuffer)+1];
    strcpy(pStrBuffer,strRight.pStrBuffer);

    return *this;
   }


   String &operator=(const char c)   //Overloaded operator = for assigning a
   {                                 //character to a String
    if(this->pStrBuffer)
       delete [] this->pStrBuffer;
    pStrBuffer=new char[2];
    this->pStrBuffer[0]=c;
    this->pStrBuffer[1]='\0';

    return *this;
   }

   
   String &operator+(const char* pChar) //Overloaded operator + (Adds char literals)
   {
    char* pNew;

    if(this->pStrBuffer) //1st term (pStrBuffer) isn't empty (NULL)
    {
       pNew=new char[strlen(this->pStrBuffer)+strlen(pChar)+1];
       strcpy(pNew,this->pStrBuffer);
       delete [] this->pStrBuffer;
       this->pStrBuffer=pNew;
       strcat(this->pStrBuffer,pChar);
    }
    else                 //1st string (this->pStrBuffer) is empty (NULL)
    {
       pNew=new char[strlen(pChar)+1];
       this->pStrBuffer=pNew;
       strcpy(this->pStrBuffer,pChar);
    }

    return *this;
   }


   String &operator+(const char ch) //Overloaded operator + (Adds char in String)
   {
    unsigned int iLen;
    char* pNew;

    if(this->pStrBuffer) //1st term (pStrBuffer) isn't empty (NULL)
    {
       iLen=strlen(this->pStrBuffer);
       pNew=new char[iLen+2];
       strcpy(pNew,this->pStrBuffer);
       delete [] this->pStrBuffer;
       this->pStrBuffer=pNew;
       this->pStrBuffer[iLen]=ch;
       this->pStrBuffer[iLen+1]='\0';
    }
    else                 //1st string (this->pStrBuffer) is empty (NULL)
    {
       pStrBuffer=new char[2];
       this->pStrBuffer[0]=ch;
       this->pStrBuffer[1]='\0';
    }

    return *this;
   }


   unsigned int LenStr(void)
   {
    return strlen(this->pStrBuffer);
   }
   

   char* lpStr()
   {
    return pStrBuffer;
   }
 
 private:
   char* pStrBuffer;
};


int main(void)
{
 Atom ProtocolAtom,KillAtom;
 int bkColor,wtColor;
 Window hWnd,hRoot; 
 char* pszDisplay=0;
 Display* disp;
 int iScreen;
 XEvent ev;

 pszDisplay=(char*)getenv("DISPLAY");
 disp=XOpenDisplay(pszDisplay);
 iScreen=DefaultScreen(disp);
 bkColor=BlackPixel(disp,iScreen);
 wtColor=WhitePixel(disp,iScreen);
 hRoot=RootWindow(disp,iScreen);
 hWnd=XCreateSimpleWindow(disp,hRoot,400,200,550,450,1,bkColor,wtColor);
 ProtocolAtom=XInternAtom(disp,"WM_PROTOCOLS",False);
 KillAtom=XInternAtom(disp,"WM_DELETE_WINDOW",False);
 XSetWMProtocols(disp,hWnd,&KillAtom,1);
 XSelectInput(disp,hRoot,SubstructureNotifyMask);
 XSelectInput(disp,hWnd,ExposureMask|KeyPressMask);
 XMapWindow(disp,hWnd);
 while(True)
 {
  XNextEvent(disp,&ev);
  switch(ev.type)
  {
   case Expose:
     {
        unsigned int i,j=1;
        String s1;
        for(i=97;i<=122;i++)
        {
            s1=s1+i;
            XDrawString(disp,hWnd,DefaultGC(disp,iScreen),25,16*j++,s1.lpStr(),s1.LenStr());       
        }
     }
     break;
   case ReparentNotify:
     {
        unsigned int iRet=0;
        Window hRootReturn,hParentWnd;
        Window* hWndChildren=0;
        unsigned int i;
        printf("Entering ReparentNotify Processing...\n");
        XQueryTree(ev.xreparent.display,ev.xreparent.window,&hRootReturn,&hParentWnd,&hWndChildren,&iRet);
        printf("\tev.xreparent.window=%u\n",ev.xreparent.window);
        printf("\tev.xreparent.parent=%u\n",ev.xreparent.parent);
        if(hWndChildren)
        {
           XFree(&hWndChildren);
           hWndChildren=0;
        }
        puts("Leaving ReparentNotify Processing.()\n");
     }
     break;
   case ClientMessage:
     printf("Entering ClientMessage Processing\n");
     if(ev.xclient.message_type==ProtocolAtom && ev.xclient.data.l[0]==KillAtom)
     {
        printf("\te.xclient.window=%u\n",ev.xclient.window);
        XCloseDisplay(disp);
        puts("Leaving ClientMessage Processing.\n");
        exit(0);
     }
     break;
  }
 }
 
 return 0;
}

If my memory serves me the Linux package you need to install to program using xlib is xorg.dev.

For Gtk its libgtk or something like that. Again, check the Linux forums. I personally only dabble in Linux development, as I'm mainly an sdk style api coder. I'd like to do more with Linux, but have a hard time finding the time. But anyway, here are a couple more gui Linux programs, 1st a HelloWorld using xlib, then a gtk+ program that puts a button in a window...

/*
   gcc HelloWorld.c  -o HelloWorld  -L/usr/X11R6/lib  -lX11
*/

#include <X11/Xlib.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
 Display* display;
 Atom ProtocolAtom,KillAtom;
 int blackColor,whiteColor,iScreen;
 Window hRoot,hMain;
 char* pDisp;
 XEvent e;  

 pDisp=getenv("DISPLAY");
 display=XOpenDisplay(pDisp);
 if(!display)
    return 1;
 hRoot=DefaultRootWindow(display);
 blackColor=BlackPixel(display,DefaultScreen(display));
 whiteColor=WhitePixel(display,DefaultScreen(display));
 iScreen=DefaultScreen(display);
 hMain=XCreateSimpleWindow(display,hRoot,200,200,350,300,0,blackColor,whiteColor);
 ProtocolAtom=XInternAtom(display,"WM_PROTOCOLS",False);
 KillAtom=XInternAtom(display,"WM_DELETE_WINDOW",False);
 XSetWMProtocols(display,hMain,&KillAtom,1);
 XSelectInput(display,hRoot,SubstructureNotifyMask);
 XSelectInput(display,hMain,ExposureMask);
 XMapWindow(display,hMain);
 while(1)
 {
  XNextEvent(display,&e);
  switch(e.type)
  {
   case Expose:
     XDrawString(display,hMain,DefaultGC(display,iScreen),130,100,"Hello, World!",strlen("Hello, World!"));
     break;
   case ClientMessage:
     if(e.xclient.message_type==ProtocolAtom && e.xclient.data.l[0]==KillAtom)
     {
        XCloseDisplay(display);
        exit(0);
     }
     break;
   default:
     break;
  }
 }

 return 0;
}

The above program, although short, has some real, real complicated stuff in it involving tapping into the low level x messaging protacols to catch a click of the [x] button to close the window. In any demonstration programs you will find any where on the net programs will be ended by a keystroke. It took me weeks to figure this out.

Here's the gtk program...

/*
fred@CodeWarrior:~$ cd code/gtk
fred@CodeWarrior:~/code/gtk$ gcc buttons.c -o buttons `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
fred@CodeWarrior:~/code/gtk$ ./buttons
fred@CodeWarrior:~/code/gtk$ 
*/

/*
  gcc buttons.c -o buttons `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
*/
#include <gtk/gtk.h>

static void destroy(GtkWidget *window,gpointer data)
{
  gtk_main_quit();
}


int main (int argc, char *argv[])
{
  GtkWidget *window, *button;

  gtk_init (&argc, &argv);
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Buttons");
  gtk_container_set_border_width (GTK_CONTAINER (window), 25);
  gtk_widget_set_size_request (window, 200, 100);
  g_signal_connect(G_OBJECT (window),"destroy",G_CALLBACK(destroy),NULL);
  button = gtk_button_new_with_mnemonic ("_Close");
  gtk_button_set_relief(GTK_BUTTON (button),GTK_RELIEF_NORMAL);
  g_signal_connect_swapped(G_OBJECT (button),"clicked",G_CALLBACK(gtk_widget_destroy),(gpointer) window);
  gtk_container_add(GTK_CONTAINER (window), button);
  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}

Ok, just keep in mind that I'm not looking to start making GUI applications, I'm just wondering how they work...

[I'm just wondering how they work...]

Well, that's not exactly something that can be explained in 'just' five minutes - you know, kind of out of like say - 'mild curiousity'.

How GUI aplications work was the culmination of the lifetimes of study/theorizing on the part of computer scientists on the cutting edge of their discipline back in the late 1970's. Their work came to fruition at the Xerox Palo Alto labs in the late seventies and early eighties. Apple picked up on it first. Then Bill Gates and Microsoft.

Highly renowned writer Charles Petzold introduced Windows GUI programming to C programmers back in the late 1980s with his famous 'Programming Windows' books. That is how most Windows developers learned GUI programming. Petzold's books were aimed at professional C programmers, that is, people not just learning to program, but rather folks who were already very competant professional programers who were masters of the C programming language. The nature of the subject was so difficult & complicated that Petzold stated it would take such a aspiring professional C programmer six months of hard study to become somewhat competant at Windows programming.

The situation with Linux/Unix GUI development is the same - just different names involved - mostly from MIT I believe.

So, if you are just mildly curious, I'd just forget it.

I wouldn't say mildly curious...more like enthusiastically curious as to how it works. I'm just obviously not ready to code stuff like this yet. That's all.

This question has already been answered. Start a new discussion instead.