Developing a simple windowed app for Linux - c

Okay, I'd like to write a simple C app for Linux (say Ubuntu with Gnome) that would do the following:
Open a Window
Draw something in that window using a main loop, e.g. the current loop number. I don't want to use controls, but to draw directly on the window surface
Close the window & the app
I can do that in Windows, but I've no idea how I could do that in Linux.
Thanks!

Unless you want a full-blown GUI (in which case I'd recommend Qt or GTK), then SDL is a very popular and extremely simple free cross-platform library that gives you a drawing surface and some simple IO facilities. It's natively C, but has bindings to a large number of other languages.

There are various "Hello World" examples for X11 programming.
Using GTK+:
http://library.gnome.org/devel/gtk-tutorial/2.13/c39.html
Using Qt:
http://doc.qt.nokia.com/latest/tutorials-widgets-toplevel.html
Using wxWidgets:
http://www.wxwidgets.org/docs/tutorials/hello.htm
There are a lot more toolkits: Fox, FLTK, Tk, EFL ...
So far these have all been cross-platform, so let's have a look at X11-specific exampls:
This is using Xlib:
http://en.literateprograms.org/Special:Downloadcode/Hello_World_(C,_Xlib)
And this is using Xcb:
http://xcb.freedesktop.org/tutorial/basicwindowsanddrawing/

If you only want to draw something, why not just use OpenGL and GLUT. The latter provides simple methods to create a window with an OpenGL context.
Setting up a GLUT application is very straighforward and there are lots of tutorials out there , e.g. Lighthouse3d.com. This tutorial works with visual studio, but it's not hard to translate this to compiling an application on Linux.
Alternatively, you could also work with Qt, which is a more advanced and easy to use GUI toolkit, and which would not necessarily require you to write OpenGL code.

Since you mentioned C, there is Glade if you want to make use of GTK+ for a nice little editor that allows you to draw controls onto a window.
Alternatively if you have access to a C++ compiler you can have a look at Qt which provides similar functionality.

Well, if you're familiar with making gui apps in windows I'm going to take a guess that you've done it with .net or something similar. An easy transition would be to use mono. A cross platform .NET development platform - http://mono-project.com/Main_Page
There's also has a variety gui toolkits to use: http://www.mono-project.com/Gui_Toolkits

If you want to draw directly onto the window, have you considered X11?
It's not going to be as nice as working with a toolkit like GTK or Qt, but it's about as low level as you can get in the windowing system.
I don't have any experience with programming straight X11, so I can't recommend any starting material.

Related

How can I create a GUI for a text based program?

I'm quite in a basic project for now which is recoding a unix shell in C. It is of course intended to work in a tty which means without a GUI. Though I'd like to add an option to use it with a graphical mode. As it's a text based program, when I launch it with Gnome (using alacarte) it launches itself in a Gnome Terminal. What I want to add is a sort of graphical interface like the Gnome Terminal app. Is there a simple way to put a text based program in a sort of frame with graphical elements in it ? I want to get rid of any other application to launch my shell so when I'm in graphical mode (when not using tty) I want my own frame and not another application that launches it.
Can anyone help me ? =)
You cannot create a GUI using the standard C library. You can make one with an external library though.
With OpenCV, it is possible to make a GUI, and there is a large community behind the project. You can find lots of code examples and tutorials on their website. In addition, you can use OpenCV for lots of other computer vision related things.
You could also use GTK+ to write a GUI for your program. It is very simple, and easy to use for beginners. It's more focused on graphics though, and if you want another functionality required by an external library, you might not be able to use GTK+.

Simple 2D graphics C library for windows with the following requirements?

I have come across a project where it is required to draw some 2D graphics on a form under Windows and to be able to perform the following tasks:
read image formats jpg, GIF, png, with transparency
monitor mouse and keyboard input to this form
draw simple 2D shapes, eg. line, ellipse, rectangle, pixel set/clear, polygons, ...
draw text with true font types and sizes and measure text sizes for different strings
written in C and can be integrated into Visual Studio 2008 C project
I've tried OpenCV but it lacks the transperency, font types, and GIF decoding and other things
I need your help please, It would be good if the library is easy to learn and use
thanks all
Sounds like a perfect job for SDL
And why not use the GDI built into Windows?
Traditional GDI documentation - http://msdn.microsoft.com/en-us/library/dd145203%28v=VS.85%29.aspx
Newer GDI+ documentation - http://msdn.microsoft.com/en-us/library/ms533798%28VS.85%29.aspx
Some more info added:
I guess I don't understand your requirements. Is cross platform support required? If yes, then use C and some of the cross platform libraries people have mentioned. If this is Windows only, then why not use the APIs that come with Windows?
Since your already using Visual Studio and you want ease of learning and development, why don't you use C# and the.NET libraries? In 2010, that is really the easiest way to build Windows only programs. Windows Forms will likely do everything you need. If you really must write in C and you are Windows only, then use GDI+ (or GDI if you need to support older versions of Windows). If you want to learn classic Windows C programming, go to the source - http://www.charlespetzold.com/pw5/.
As daddz said, you can use SDL for inputs, and image reading. In order to render your 2D primitives, OpenGL will do the work (be sure to create an OpenGL compatible window while calling SDL_CreateWindow). Concerning font rendering, it is not directly implemented in SDL but a couple of libraries can be used (see Survey Of OpenGL Font Technology).

What are some choices to port existing Windows GUI app written in C to Linux?

I've been tasked with porting an existing Windows GUI app to Linux. Ideally, I'd like to do this so the same code base can be used to build either the Windows version or the Linux version. I'll be doing my work on Ubuntu 9.04. After searching around, it's unclear to me what tools are best suited to help me with this.
A list of loose requirements would be:
The code is in C, not C++, and should compile to build both Windows and Linux versions. Since it's existing code, and fairly large, converting to a managed language like .NET is out of the question for now.
I would prefer if I can use the same dialogs in both systems. In Windows, putting up a dialog is pretty simple. You build the dialog in the Resource Editor in Visual Studio, then call DialogBox() API, and handle the event messages. I would really like to find something that can do the equivalent on the Linux side.
It would also be nice to have a good IDE similar to Visual Studio.
Any helps or hints would be appreciated.
Thanks,
Winelib should let you compile Win32 code under Linux with only a few modifications.
Since your code base is in C, I'd suggest using GTK+. It's a cross platform GUI toolkit. For instance, Pidgin instant messenger GUI is created with GTK+. Glade user interface designer can be used to graphically design UIs.
If you're on a tight budget, and don't mind taking time to work around a fair number of limitations, Winlib is an option. If you're shorter on time, and have a larger budget, you might want to look into Mainsoft instead. It's not exactly perfect, but I believe it supports a considerably larger part of the Win32 API (at a correspondingly higher price).

GUI for linux c

is it possible to develop a GUI in linux c??
how can be do that??
If you want to develop GUI applications for Linux with pure C you can use GTK+. IF C++ is an option you also have Qt.
There are many graphical toolkits for linux such as GTK, Qt, wxWidgets, and FLTK. They have bindings for many languages such as C and Python. I suggest you google around to see what you like. If you want a RAD you may want to check out things like glade and qt creator.
Yes. Use a GUI toolkit such as GTK+ that uses C, or find a wrapper for one of the various C++ toolkits.
XForms is a graphical user interface toolkit for X based on the X11 Xlib library. I.e., it allows you to create windows, containing all kinds of widgets (buttons, sliders, browsers, menus etc.) with a few lines of code and then attach actions to the widgets, i.e., have some function called when a button is pressed. To make this even easier XForms comes with a program called fdesign that allows you to design a GUI for a program directly on the screen and which then writes out the necessary C code for it.
XForms is written in C and has a C API, i.e., you can use it directly from a C program. It should work with X11 R4, R5, R6 & R7 and under all kinds of operating systems of the UNIX family (including MacOS X) as well as at least OpenVMS, OS/2 und Windows NT 4.0. In addition, the library is extensible and new objects can easily be created and added to the library.
http://xforms-toolkit.org
I would recommend FLTK. It may be difficult to write complex interface with it. But FLTK, as its name implies, is very small and fairly fast. What is more important, it is cross-platform, working nicely on the three major OS: linux, windows and mac. In my view, GTK/Qt/wxWidgets are far too heavy. If you statically link to these library, you will end up with a huge executable which eat up the memory; if you dynamically link to them, users have to install the library before hand, which is always troublesome.
EDIT: I just realize that this is a "C" question. Then the best choice should be GTK. If you need graphics but not interface/widgets (e.g. menu, scrollbar and so on), opengl is also nice.

How do I take C programming beyond the console?

I'm trying to learn some graphics programming using C. What would be the best way for a beginner to start? I'd like to how to make programs that use graphics and images that can be run directly from a command line prompt, and don't rely on a windowing system like X to execute.
Thanks,
Mike
Look into libsdl - Simple DirectMedia Layer. Although on Linux it can use X11 for displaying output, it can also directly use a framebuffer device. It's designed to be simple for pixel-bashing game-type programming, and supports a wide variety of platforms.
There's also Allegro if you're not a fan of SDL. It's somewhat more fully-featured for simple vector graphics; SDL is mainly a cross-platform framebuffer until you add extension libraries.
Learn some GUI toolkit like Qt or GTK, this way you will make modern GUI applications.
Check out the FLTK GUI toolkit. It is small and easy to learn.

Resources