Frederick2 189 Posting Whiz

[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.

Frederick2 189 Posting Whiz

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$ …
Frederick2 189 Posting Whiz

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 …

Frederick2 189 Posting Whiz

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.

Frederick2 189 Posting Whiz

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.

Frederick2 189 Posting Whiz

if its not in a class look at memset(). That will set any range of bytes (if they are in your process) to whatever you want. For example, if you've got a thousand doubles and you want that memory all set to nulls, you would call the function with the starting address and the number of bytes you want filled - here 8000.

Frederick2 189 Posting Whiz

Narue's advice is good. That was Petzold's last book on the Window's Api (he now writes about .NET and other technology topics), and he is considered quite the expert on it - especially in graphics. The Windows Api doesn't really change all that much.

Another book on the Api that some prefer is the one by Newcomen (sp? Newcomb? and Rector), but I prefer Petzold. Its how I learned. Class frameworks come and go, but the Api is always there.

edited

Its Newcomer and Rector

http://www.amazon.com/Win32-Programming-Addison-Wesley-Advanced-Windows/dp/0201634929/ref=pd_sbs_b_njs_3

but I prefer Petzold!

Frederick2 189 Posting Whiz

I use two different compilers/development environments, i.e., Microsoft's Visual C++ 6.0 and Dev C++, the latter of which is a free and relatively small download of about 10 MB. For just starting out I'd highly recommend Dev C++ as a development environment. You can compile console or GUI Windows programs with either. Here is a link to download Dev C++/Bloodshed...

http://www.bloodshed.net

Of course, Microsoft is and has been for some time giving away various free compilers/development tools, but I personally tend to shy away from these as the download size and volume of documentation is beyond what I'm prepared to handle.

Frederick2 189 Posting Whiz

Petzold's books are how I learned Windows Api coding, and the material is still timely and applicable, IMHO. I primarily use Win 2000 and XP and everything in Petzold works fine. Do you have the Windows 95 or 98 book? Actually, there have been some updates to the functions. For example, instead of WNDCLASS you should be using WNDCLASSEX, instead of CreateWindow(), use CreateWindowEx(), etc. That sort of thing. I don't have Vista but people who do told me there isn't much difference regarding the Win32 Api.