GTK+ 3.0 and C programm - c

I'm a beginner programmer in C/C++. ;) Recently on the Internet I found information about GTK and so on. Also I found this site: (http://wingtk.sourceforge.net/ishan/sliders.html). I wanted to run this code, but my compiler says something like this in 231 line (in sliders.c): "GtkWidget has no member named 'parent'", I don't understand, what's wrong with this program. I tried to fix this, but I failed.
Piece of code:
void on_new_activate (GtkMenuItem *menuitem, gpointer user_data)
{
int rand, x;
gtk_statusbar_push (GTK_STATUSBAR(statusbar1), 0, "Welcome to sliders");
move_no=0;
if (GTK_IS_WIDGET(image) && GTK_IS_WIDGET(image->parent)) //**error here**
{
gtk_object_ref (GTK_OBJECT(image));
gtk_container_remove (GTK_CONTAINER(alignment1), image);
gtk_container_add (GTK_CONTAINER(alignment1), table1);
gtk_widget_show (table1);
}
for (x=0; x<=14; x++)
{
rand = abs(g_rand_int ( g_rand_new () )) % 15;
if (x!=rand)
swap_buttons (x, rand);
}
}

The parent container is no longer exposed as a member. Instead of accessing image->parent, call gtk_widget_get_parent(image).
In the long term, as pointed out by #andlabs, you are best off finding a tutorial that covers GTK+ version 3, which you are using.

Related

An Error with SDL_CeateWindow appearing when program started

The code below initializes an SDL window. Because of bug I can't find SDL_CreateWindow() returns NULL.
int xwin_init(int w, int h)
{
int r;
r = SDL_Init(SDL_INIT_VIDEO);
assert(win == NULL);
win = SDL_CreateWindow("PRG Semester Project",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED, w, h,SDL_WINDOW_SHOWN);
SDL_SetWindowTitle(win, "PRG SEM");
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(icon_32x32_bits,32, 32, 24, 32*3, 0xff, 0xff00,0xff0000, 0x0000);
SDL_SetWindowIcon(win, surface);
SDL_FreeSurface(surface);
return r;
}
The error message looks like this:
prgsem: xwin_sdl.c:58: xwin_init: Assertion win != NULL' failed`
While I can't tell you what your problem is, at the very least it is worth checking for errors after initializing video and creating the window. You can use SDL_GetError to do this. A message about an error reported through the SDL library will be accessible through this function, and you can tell when to call it based on the return codes of other SDL functions such as SDL_Init in your example.

switching to a specific window

I'm writing a program using XLIB and gtk, I'm supposed to implement a function to switch a specific window(using any human identifyable reference, window name, process name or anything that is going to be static everytime that application is run)
I'm completely lost and I cannot find anything to guide me through the beginning of the application
thanks
You can do this with X libraries but it'll be a bit of a pain: you'll have to understand X IPC and properties. Fortunately there's an easier way, assuming you can use other libraries: use Libwnck. Here's a minimal example to list windows (adapted from the "Getting started" page):
WnckScreen *screen = wnck_screen_get_default ();
WnckWindow *active_window = wnck_screen_get_active_window (screen);
GList *l;
for (l = wnck_screen_get_windows (screen); l != NULL; l = window_l->next) {
WnckWindow *window = WNCK_WINDOW (window_l->data);
g_print ("%s%s\n", wnck_window_get_name (window),
window == active_window ? " (active)" : "");
}
Switching to a specific window should be just:
wnck_window_activate (window, 0);
The only gotcha is that wnck is designed to be used with a GLib mainloop. If you are not running one (as would be the case for a simple command line app), you'll need to use wnck_screen_force_update () to fetch the current data from X (see the "Getting started" example).

SDL window close because of SDL_Flip with an image surface array

I am in the beginning of a game of brick breaker type and I'm stuck in the SDL_Flip step. My CodeBlocks compiler says nothing and the console doesn't crash, but yet the SDL window shutdown and the console process returned code 3. When I ran the debugger it says:
SDL_Flip()
Display(Bricks=0x28f69c, screen=0x0)
and the Display type error was said located at the line of my SDL_Flip(screen);
Here's a glimpse of my code. My Brick_Coordinates and Brick_Surface struct are already initialize (my coordinates for Brick_Coordinates and NULL for Brick_Surface) by another function before that one:
void Display(BrickStruct Bricks[12][10],SDL_Surface *screen)
{
int i=0,j=0;
for(j=0;j<10;j++)
{
if( (j+1)%2==0 ) // If we are on even lines, display only 11 bricks
{
for(i=0;i<11;i++)
{
Bricks[i][j].Brick_Surface = IMG_Load("BrickTest1.png");
SDL_BlitSurface(Bricks[i][j].Brick_Surface, NULL, screen, &Bricks[i][j].Brick_Coordinates);
SDL_Flip(screen);
}
}
else // If we are on odd lines, display the 12 bricks
{
for(i=0;i<12;i++)
{
}
}
}
}
My Structure looks like this:
typedef struct BrickStruct
{
int type;
SDL_Rect Brick_Coordinates;
SDL_Surface *Brick_Surface;
}BrickStruct;
In my main, my code is like this:
SDL_Surface *screen= NULL;
BrickStruct Bricks[12][10]; // I create my 2D array of struct named Bricks
Display(Bricks,screen);
I've already tested with a fprintf the values of my coordinates initialized. These are good. And apparently my SDL_Blit is working. But The Flip isn't. My screen surface is big enough for all my images (480x540 and my images are 40x20). I was wondering if that problem has to do with an impossibility for Blit to place an image on top of another but the Flip doesn't even work when I try with only one image (without my loops).
Can somebody please have the kindness to indicate me where is located my problem ?
Thanks in advance
There reason was that that you didn't save screen into the global variable.
You probably had a line in your SDL_Initialisation similar to this:
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
This creates a new local variable called screen. Since you wanted to save this into the global one, you should change it to:
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
According to your debugger and your example code your screen structure is null. So your call to SDL_BlitSurface will fail. The reason it probably works for you when you do your Display call inside your Initialize is that you've just initialized your screen and used it right after.
You need to store the surface you are writing to and use it again when you're blitting.
Also, as others have recommended, you should take a look at a tutorial for SDL and perhaps some more C tutorials to reinforce some concepts.

Stuck in programming with winBGIm with C

I recently discovered winBGIm libraries, and I find it very useful to learn to code, so I started creating something to get some practice but I got stuck with my program.
The program should show a little ball and two rectangles approaching to the ball itself, and the player can move the ball up and down simply by pressing a button on the keyboard. Initially, I wrote everything in the main to make it fast, but since this is terrible I divided the program into functions.
First of all, even when everything was together, the getch function seemed to not work, because, while it should wipe the input buffer for the kbhit function, it made the ball not move at all, while the mere kbhit function worked, but obvioulsy the ball continued going up even when you stopped pressing a key. I used the same procedure in another text-only program and it worked very well, so I don't know where the problem is.
The second and most important problem is that, after splitting the program into functions, it became static, since the main loop which would make the graphic move stops at the end of the first iteration. It only restarts working when I delete the cleardevice function at the end of the loop and I disable the duble buffering in the initwindow function, but I can't understand the relationship between these things.
Finally, when I set a new background color, if it is not 0 (black), the window remains completely black.
I hope someone can help me.
Best regards, Giacomo.
#include <graphics.h>
#include <time.h>
#include <stdio.h>
int rettangoli(int b);
void bird(int x_default, int y_default);
void bird();
int main() {
int a=640;
int b=480;
int x_default=150;
int y_default=400;
int verifica=0;
srand(time(NULL));
initwindow(a, b, "BGI", 0, 0, true, true);
setbkcolor(0);
while(1) {
bird(x_default, y_default);
verifica=rettangoli(b);
if(verifica==1) {
outtextxy(0, 0, "HAI PERSO");
outtextxy(0,20, "PREMERE UN TASTO PER CONTINUARE");
break;
}
delay(50);
while(kbhit()) {
getch();
}
swapbuffers();
cleardevice();
}
delay(350);
getch();
closegraph();
return 0;
}
void bird(int x_default, int y_default) {
static int x_pos=x_default;
static int y_pos=y_default;
if(kbhit()) {
if(y_pos-16>0)
y_pos=y_pos-5;
circle(x_pos, y_pos, 16);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(x_pos, y_pos, 15);
}
else {
while(y_pos<400)
y_pos=y_pos+15;
circle(x_pos, y_pos, 16);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(x_pos, y_pos, 15);
}
return;
}
int rettangoli(int b) {
static int x_rett=615;
int verifica=0;
int contatore;
int altezza;
if(x_rett==615)
altezza=rand()%(b-60)+1;
rectangle(x_rett, 0, x_rett+25, altezza);
rectangle(x_rett, altezza+60, x_rett+25, 480);
setfillstyle(SOLID_FILL, GREEN);
floodfill(x_rett+1, 1, 15);
floodfill(x_rett+1, 479, 15);
for(contatore=0;contatore<altezza && x_rett>100;contatore++) {
if(getpixel(x_rett-1, contatore)!=getbkcolor())
verifica=1;
}
for(contatore=altezza+60;contatore<b && x_rett>100;contatore++) {
if(getpixel(x_rett-1, contatore)!=getbkcolor())
verifica=1;
}
for(contatore=0;contatore<15;contatore++) {
if(getpixel(contatore, altezza)!=getbkcolor())
verifica=1;
}
for(contatore=0;contatore<15;contatore++) {
if(getpixel(contatore, altezza+60)!=getbkcolor())
verifica=1;
}
x_rett=x_rett-5;
printf("%i", x_rett);
return verifica;
}
Nevermind, I solved the problem on my own. There was an incompatibility issue between Windows 8 x64 and Codeblocks-EP, and I think there is a general incompatibility between winBGIm and x64 systems.
I created a virtual machine using Vmware Player and Windows XP x86 and now everything is fine.

Is there a Linux equivalent of SetWindowPos?

A while ago I wrote a script in C that used the Windows API functions EnumWindows, SetWindowPos and SetForegroundWindow to automatically arrange windows (by title) in a particular layout that I commonly wanted.
Are there Linux equivalents for these functions? I will be using Kubuntu, so KDE-specific and/or Ubuntu-specific solutions are fine.
The best way to do this is either in the window manager itself (if yours supports extensions) or with the protocols and hints designed to support "pagers" (pager = any non-window-manager process that does window organization or navigation things).
The EWMH spec includes a _NET_MOVERESIZE_WINDOW designed for use by pagers. http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html#id2731465
Raw Xlib or Xcb is pretty rough but there's a library called libwnck specifically designed to do the kind of thing you're talking about. (I wrote the original library long ago but it's been maintained by others forever.) Even if you don't use it, read the code to see how to do stuff. KDE may have an equivalent with KDE-style APIs I'm not sure.
There should be no need to use anything KDE or GNOME or distribution specific since the needed stuff is all spelled out in EWMH. That said, for certain window managers doing this as an extension may be easier than writing a separate app.
Using old school X calls directly can certainly be made to work but there are lots of details to handle there that require significant expertise if you want to iron out all the bugs and corner cases, in my opinion, so using a WM extension API or pager library would be my advice.
#andrewdotn has a fine answer there but you can do this old school as well fairly simply by walking the tree starting at the root window of the display using XQueryTree and fetching the window name with XFetchName then moving it with XMoveWindow. Here is an example that will list all the windows and if any are called 'xeyes' they get moved to the top left. Like most X programs, there is more to it and this should probably be calling XGetWindowProperty to fetch the _NET_WM_NAME extended window manager property but the example works ok as a starter. Compile with gcc -Wall -g -o demo demo.c -lX11
#include <X11/Xlib.h>
#include <stdio.h>
#include <string.h>
static int
EnumWindows(Display *display, Window window, int depth)
{
Window parent, *children;
unsigned int count = 0;
int r = 1, n = 0;
char *name = NULL;
XFetchName(display, window, &name);
for (n = 0; n < depth; ++n) putchar(' ');
printf("%08x %s\n", (int)window, name?name:"(null)");
if (name && strcmp("xeyes", name) == 0) {
XMoveWindow(display, window, 5, 5);
}
if (name) XFree(name);
if (XQueryTree(display, window, &window, &parent, &children, &count) == 0) {
fprintf(stderr, "error: XQueryTree error\n");
return 0;
}
for (n = 0; r && n < count; ++n) {
r = EnumWindows(display, children[n], depth+1);
}
XFree(children);
return r;
}
int
main(int argc, char *const argv[])
{
Display *display = NULL;
if ((display = XOpenDisplay(NULL)) == NULL) {
fprintf(stderr, "error: cannot connect to X server\n");
return 1;
}
EnumWindows(display, DefaultRootWindow(display), 0);
XCloseDisplay(display);
return 0;
}
Yes, you can do this using the X Windows protocol. It’s a very low-level protocol so it will take some work. You can use xcb_query_tree to find the window to operate on, and then move it with xcb_configure_window. This page gives some details on how to do it. There’s a basic tutorial on using the library those functions come from, but you’ll probably want to Google for a better one.
It may seem daunting, but it’s not too bad. Here’s a 50-line C program that will move all your xterms 10px to the right:
#include <stdio.h>
#include <string.h>
#include <xcb/xcb.h>
void handle(xcb_connection_t* connection, xcb_window_t window) {
xcb_query_tree_reply_t *tree = xcb_query_tree_reply(connection,
xcb_query_tree(connection, window), NULL);
xcb_window_t *children = xcb_query_tree_children(tree);
for (int i = 0; i < xcb_query_tree_children_length(tree); i++) {
xcb_get_property_reply_t *class_reply = xcb_get_property_reply(
connection,
xcb_get_property(connection, 0, children[i], XCB_ATOM_WM_CLASS,
XCB_ATOM_STRING, 0, 512), NULL);
char* class = (char*)xcb_get_property_value(class_reply);
class[xcb_get_property_value_length(class_reply)] = '\0';
if (!strcmp(class, "xterm")) {
/* Get geometry relative to parent window */
xcb_get_geometry_reply_t* geom = xcb_get_geometry_reply(
connection,
xcb_get_geometry(connection, window),
NULL);
/* Move 10 pixels right */
uint32_t values[] = {geom->x + 10};
xcb_configure_window(connection, children[i],
XCB_CONFIG_WINDOW_X, values);
}
/* Recurse down window tree */
handle(connection, children[i]);
}
}
int main() {
xcb_connection_t *connection;
const xcb_setup_t *setup;
connection = xcb_connect(NULL, NULL);
setup = xcb_get_setup(connection);
xcb_screen_iterator_t screen = xcb_setup_roots_iterator(setup);
handle(connection, screen.data->root);
return 0;
}
There’s no error-checking or memory management, and what it can do is pretty limited. But it should be straightforward to update into a program that does what you want, or to turn it into a general-purpose helper program by adding command-line options to specify which windows to operate on and which operations to perform on them.
As it seems you are not looking specifically for a solution in code, but rather in a desktop environment, you need to take a look at one of the window managers that handle the window placement in such a desktop environment.
KDE's KWin's Window Attributes
Compiz (GNOME) has "Window Rules" and "Place Windows" in the CompizConfig Settings Manager application. See e.g. here
Openbox seems a lot harder to get right, although they link to a GUI tool at the bottom of this page.
The problem with using X directly is that X in itself knows nothing about your desktop environment (panels, shortcuts, etc.) and you'll have to compensate manually.
After googling for this, I'm surprised KDE is the only one that has a simple way to do this.

Resources