Grafische Oberfläche
-
Hallo wie kann man in Linux eine Grafische Oberfläche Programmieren ?
Ohne QT oder so.
In Windows gibt es ja z.b. die WIN API gibt es so etwas ähnliches in Linux auch ?
-
Du kannst natürlich auch direkt X11 programmieren (Siehe XLib/XCB). Aber X11 enthält keine Widgets! Die werden erst durch die diversen Libs (Qt, GTK+ etc.) zur Verfügung gestellt (deswegen sehen GTK+ und Qt-Programme idr. anders aus).
-
also, nur Xlib zu verwenden macht absolut keinen Spaß
hab nach Paar Wochen aufgegeben, weil es einfach zu umständlich ist, vor allem, wenn man (quasi) standarisiert mit QT, GTK, vxWidgets GUIs schnell und einfach schreiben kann. Die 12 xlib Bücher sind außerdem alles anders als billig.
edit: ich war am Anfang auch der Meinung, dass ich nur Xlib verwenden sollte. Aber seit den ersten Versuchen merkte ich, dass es keinen Sinn macht, nur Xlib zu verwenden. Mit GTK+, QT und wie sie alle heißen hast du nicht nur eine Schnittstelle zum X Server (und zu anderen GDIs wie die von Windows) sondern hast du gleich ein ganzes Framework, mit dem du sehr bequem (quasi) plattform unabhängig arbeiten kannst. Also freunde dich mit Qt oder GTK+ an.
-
Hab den Kramm aus meiner alten xlib Zeit gefunden:
hier ein minimales Bsp für ein Hello World Programm mit nur xlib (c) Xlib programming manual ISBN 1-56592-002-3
Xlib Programming Manual | ISBN: 1565920023und das ganze nur dafür http://www.informatik.uni-freiburg.de/~yanezp/sc_bw.jpg
/* basicwin.c */ /* This program creates a simple window using X11 libraries for UNIX / Linux / SOLARIS */ /* Baisc X11 Libraries */ #include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xos.h> // It makes the program some portable within X Servers #include <X11/Xatom.h> /* Standard C Libraries */ #include <stdio.h> /* custom resourcen */ #include "bitmaps/app1" /* Some constats */ #define BITMAPDEPTH 1 #define TOO_SMALL 0 #define BIG_ENOUGH 1 Display* display; int screen_num; static char* progname; void getGC(Window, GC*, XFontStruct*); void load_font(XFontStruct**); void place_text(Window, GC, XFontStruct*, unsigned int, unsigned int); void place_graphics(Window, GC, unsigned int, unsigned int); void TooSmall(Window, GC, XFontStruct*); int main(int argc, char* argv[]) { Window win; unsigned int width, height; // Window size int x, y; // Window position unsigned int border_width = 4; unsigned int display_width, display_height; unsigned int icon_width, icon_height; char* window_name = "Basic X11 Window Program"; char* icon_name = "basicwin"; Pixmap icon_pixmap; XSizeHints* size_hints; XIconSize* size_list; XWMHints* wm_hints; XClassHint* class_hints; XTextProperty windowName, iconName; int count; XEvent report; GC gc; XFontStruct* font_info; char* display_name = NULL; int window_size = 0; progname = argv[0]; if (!(size_hints = XAllocSizeHints())) { fprintf(stderr, "%s: failure allocating memory\n", progname); exit(0); } if (!(wm_hints = XAllocWMHints())) { fprintf(stderr, "%s: failure allocating memory\n", progname); exit(0); } if (!(class_hints = XAllocClassHint())) { fprintf(stderr, "%s: failure allocating memory\n", progname); exit(0); } /* Connect to the X Server */ if ((display=XOpenDisplay(display_name)) == NULL) { fprintf(stderr, "%s: cannot connect to X server %s\n", progname, XDisplayName(display_name)); exit(-1); } // Get screen size from display structure macro screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); // x=y=0; (Original to program) // Size window with enough room for text width = display_width/3; width +=100; height = display_height/4; // Das ist meine Änderung (this is my implementation) // The window shall appear right in the middle of the screen // Das Fenster wird in der Mitte geladen. x = (display_width - width) / 2; y = (display_height - height) / 2; // Create opaque window win = XCreateSimpleWindow(display, RootWindow(display, screen_num), x, y, width, height, border_width, BlackPixel(display, screen_num), WhitePixel(display, screen_num)); // Get available icon size from Window Manager if (XGetIconSizes(display, RootWindow(display, screen_num), &size_list, &count) == 0) fprintf(stderr, "%s: Window manager didn't set icon sizes - using default.\n", progname); /* A real application would search throught size_list here to find an acceptable icon size */ // Create pixmap of depth 1 (bitmap) for icon icon_pixmap = XCreateBitmapFromData(display, win, app1_bits, app1_width, app1_height); size_hints->flags = PPosition | PSize | PMinSize; size_hints->min_width = 300; size_hints->min_height = 200; if (XStringListToTextProperty(&window_name, 1, &windowName) == 0) { fprintf(stderr, "%s: structure allocation for windowName failed.\n", progname); exit(-1); } if (XStringListToTextProperty(&icon_name, 1, &iconName) == 0) { fprintf(stderr, "%s: structure allocation for iconName failed.\n", progname); exit(-1); } wm_hints->initial_state = NormalState; wm_hints->input = True; wm_hints->icon_pixmap = icon_pixmap; wm_hints->flags = StateHint | IconPixmapHint | InputHint; class_hints->res_name = progname; class_hints->res_class = "Basicwin"; XSetWMProperties(display, win, &windowName, &iconName, argv, argc, size_hints, wm_hints, class_hints); // Select event types wanted XSelectInput(display, win, ExposureMask | KeyPress | ButtonPressMask | StructureNotifyMask); load_font(&font_info); // Create GC for text and drawing getGC(win, &gc, font_info); // Display the window XMapWindow(display, win); // Get eventsuse first to display text and graphics while (1) { XNextEvent(display, &report); switch (report.type) { case Expose: /* Unless this is the last contiguos expose, don't draw the window */ if (report.xexpose.count != 0) break; /* if window to small to use */ if (window_size == TOO_SMALL) TooSmall(win, gc, font_info); else { // Place text in window place_text(win, gc, font_info, width, height); // Place graphics place_graphics(win, gc, width, height); } break; case ConfigureNotify: width = report.xconfigure.width; height = report.xconfigure.height; if((width < size_hints->min_width) || (height < size_hints->min_height)) window_size = TOO_SMALL; else window_size = BIG_ENOUGH; break; case ButtonPress: case KeyPress: XUnloadFont(display, font_info->fid); XFreeGC(display, gc); XCloseDisplay(display); exit(1); default: break; } // end of switch } // end of while } void getGC(Window win, GC* gc, XFontStruct* font_info) { unsigned long valuemask = 0; // Ignore XGCvalues and use defaults XGCValues values; unsigned int line_width = 6; int line_style = LineOnOffDash; int cap_style = CapRound; int join_style = JoinRound; int dash_offset = 0; static char dash_list[] = {12, 24}; int list_length = 2; // Create default Graphics Context *gc = XCreateGC(display, win, valuemask, &values); // Specify font XSetFont(display, *gc, font_info->fid); XSetForeground(display, *gc, BlackPixel(display, screen_num)); // Line attributes XSetLineAttributes(display, *gc, line_width, line_style, cap_style, join_style); // Set Dashes XSetDashes(display, *gc, dash_offset, dash_list, list_length); } void load_font(XFontStruct** font_info) { char* fontname = "9x15"; if ((*font_info = XLoadQueryFont(display, fontname)) == NULL) { fprintf(stderr, "%s: Cannot open 9x15 font\n", progname); exit(-1); } } void place_text(Window win, GC gc, XFontStruct* font_info, unsigned int win_width, unsigned int win_height) { char* string1 = "Hi! I'm a X11 window, who are you?"; char* string2 = "To terminate program; Press any key"; char* string3 = "or button while in this window"; char* string4 = "Screen Dimensions:"; char* copyright = "Done by Pablo. pabloy@pcpool00.mathematik.uni-freiburg.de"; int len1, len2, len3, len4, lenC; int width1, width2, width3, widthC; char cd_height[50], cd_width[50], cd_depth[50]; int font_height; int initial_y_offset, x_offset; len1 = strlen(string1); len2 = strlen(string2); len3 = strlen(string3); lenC = strlen(copyright); // get strings width for centering width1 = XTextWidth(font_info, string1, len1); width2 = XTextWidth(font_info, string2, len2); width3 = XTextWidth(font_info, string3, len3); widthC = XTextWidth(font_info, copyright, lenC); font_height = font_info->ascent + font_info->descent; XDrawString(display, win, gc, (win_width - width1)/2, font_height, string1, len1); XDrawString(display, win, gc, (win_width - widthC)/2, 2 * font_height, copyright, lenC); XDrawString(display, win, gc, (win_width - width2)/2, (int)(win_height - (2*font_height)), string2, len2); XDrawString(display, win, gc, (win_width - width3)/2, (int)(win_height - font_height), string3, len3); // Copy numbers into string variables sprintf(cd_height, " Height - %d pixels", DisplayHeight(display, screen_num)); sprintf(cd_width, " Width - %d pixels", DisplayWidth(display, screen_num)); sprintf(cd_depth, " Depth - %d plane(s)", DefaultDepth(display, screen_num)); // Reuse these for the same purpose len4 = strlen(string4); len1 = strlen(cd_height); len2 = strlen(cd_width); len3 = strlen(cd_depth); initial_y_offset = win_height/2 - font_height - font_info->descent; x_offset = (int) win_width/3; XDrawString(display, win, gc, x_offset, (int) initial_y_offset, string4, len4); XDrawString(display, win, gc, x_offset, (int) (initial_y_offset + font_height), cd_height, len1); XDrawString(display, win, gc, x_offset, (int) (initial_y_offset + 2 * font_height), cd_width, len2); XDrawString(display, win, gc, x_offset, (int) (initial_y_offset + 3 * font_height), cd_depth, len3); } void place_graphics(Window win, GC gc, unsigned int window_width, unsigned int window_height) { int x, y; int width, height; height = window_height / 2; width = 3*window_width/4; x = window_width/2 - width/2; y = window_height/2 - height/2; XDrawRectangle(display, win, gc, x, y, width, height); } void TooSmall(Window win, GC gc, XFontStruct* font_info) { char* string1 = "Too Small"; int y_offset, x_offset; y_offset = font_info->ascent + 2; x_offset = 2; XDrawString(display, win, gc, x_offset, y_offset, string1, strlen(string1)); }
bitmaps/app1
#define app1_width 32 #define app1_height 32 static unsigned char app1_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x07, 0x00, 0x80, 0xff, 0x07, 0x00, 0x80, 0xff, 0x07, 0x00, 0x80, 0xff, 0x07, 0x00, 0x80, 0xff, 0x07, 0x00, 0x80, 0xff, 0x07, 0x00, 0x80, 0xe7, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07, 0x00, 0xf0, 0xff, 0xff, 0x0f, 0xf0, 0xff, 0xff, 0x0f, 0xf0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00};
Meine Güte, damit war ich mehr als überfordert, weiter als das bin ich damals nicht gekommen.
-
Über 300 Zeilen für Hello World
Ich glaub ich guck mir mal GTK+ und QT an^^
-
grafik schrieb:
Über 300 Zeilen für Hello World
Ich glaub ich guck mir mal GTK+ und QT an^^
das sagte ich auch vorhin, du hast es aber nicht geglaubt
-
Schau dir mal das an - verwende ich persoenlich. Macht richtig Laune und
ist relativ einfach zu programmieren..http://enchantia.com/software/graphapp/
About GraphApp
What Is GraphApp?
GraphApp is a cross-platform graphics library written in the C programming language. It works with a variety of operating system and windowing system combinations, including Microsoft Windows, X-Windows, Linux, Macintosh OSX, and other Unixes. It gives the programmer access to buttons, text boxes, windows, fonts, colours, and so on, in a platform independent and easy to learn manner, so that graphical programs can be easily created and ported to different operating systems.
About the Author
The author of GraphApp is L. Patrick, who has been employed by the University of Sydney, Australia, as a lecturer and tutor in the Basser Department of Computer Science, in the areas of user interface design, computer graphics and software engineering. GraphApp has been used in teaching courses at the University.Use and Distribution of GraphApp
GraphApp is distributed under the App Software Licence. A copy of this licence can be found in the file LICENCE.TXT. GraphApp is free software and is distributed in the hope it will be of use, but the software has no warranty.