GTK+ Code Demos - compile Color Selector - gcc

I found out that Color Selector from the GTK+ Code Demos is a quite nice color chooser. Thus, I would like to use it as a standalone application without having to run gtk-demos every time.
I tried to compile the example code which is:
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
static GtkWidget *da;
static GdkColor color;
static GtkWidget *frame;
/* Expose callback for the drawing area
*/
static gboolean
expose_event_callback (GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
GdkWindow *window = gtk_widget_get_window (widget);
if (window)
{
GtkStyle *style;
cairo_t *cr;
style = gtk_widget_get_style (widget);
cr = gdk_cairo_create (window);
gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_NORMAL]);
gdk_cairo_rectangle (cr, &event->area);
cairo_fill (cr);
cairo_destroy (cr);
}
return TRUE;
}
static void
change_color_callback (GtkWidget *button,
gpointer data)
{
GtkWidget *dialog;
GtkColorSelection *colorsel;
gint response;
dialog = gtk_color_selection_dialog_new ("Changing color");
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));
colorsel =
GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (GTK_COLOR_SELECTION_DIALOG (dialog)));
gtk_color_selection_set_previous_color (colorsel, &color);
gtk_color_selection_set_current_color (colorsel, &color);
gtk_color_selection_set_has_palette (colorsel, TRUE);
response = gtk_dialog_run (GTK_DIALOG (dialog));
if (response == GTK_RESPONSE_OK)
{
gtk_color_selection_get_current_color (colorsel,
&color);
gtk_widget_modify_bg (da, GTK_STATE_NORMAL, &color);
}
gtk_widget_destroy (dialog);
}
GtkWidget *
do_colorsel (GtkWidget *do_widget)
{
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *alignment;
if (!window)
{
color.red = 0;
color.blue = 65535;
color.green = 0;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Color Selection");
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
vbox = gtk_vbox_new (FALSE, 8);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
gtk_container_add (GTK_CONTAINER (window), vbox);
/*
* Create the color swatch area
*/
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
da = gtk_drawing_area_new ();
g_signal_connect (da, "expose_event",
G_CALLBACK (expose_event_callback), NULL);
/* set a minimum size */
gtk_widget_set_size_request (da, 200, 200);
/* set the color */
gtk_widget_modify_bg (da, GTK_STATE_NORMAL, &color);
gtk_container_add (GTK_CONTAINER (frame), da);
alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0);
button = gtk_button_new_with_mnemonic ("_Change the above color");
gtk_container_add (GTK_CONTAINER (alignment), button);
gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
g_signal_connect (button, "clicked",
G_CALLBACK (change_color_callback), NULL);
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
else
{
gtk_widget_destroy (window);
window = NULL;
}
return window;
}
The error is:
[orschiro#thinkpad ~]$ gcc `pkg-config --cflags --libs gtk+-3.0` -o colorSelector colorSelector.ccolorSelector.c: In function ‘expose_event_callback’:
colorSelector.c:26:7: warning: ‘gdk_cairo_set_source_color’ is deprecated (declared at /usr/include/gtk-3.0/gdk/gdkcairo.h:58): Use 'gdk_cairo_set_source_rgba' instead [-Wdeprecated-declarations]
colorSelector.c: In function ‘change_color_callback’:
colorSelector.c:44:3: warning: ‘gtk_color_selection_dialog_new’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorseldialog.h:73): Use 'gtk_color_chooser_dialog_new' instead [-Wdeprecated-declarations]
colorSelector.c:49:5: warning: ‘gtk_color_selection_dialog_get_color_selection’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorseldialog.h:75): Use 'GtkColorChooser' instead [-Wdeprecated-declarations]
colorSelector.c:51:3: warning: ‘gtk_color_selection_set_previous_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:148) [-Wdeprecated-declarations]
colorSelector.c:52:3: warning: ‘gtk_color_selection_set_current_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:142): Use 'gtk_color_chooser_set_rgba' instead [-Wdeprecated-declarations]
colorSelector.c:53:3: warning: ‘gtk_color_selection_set_has_palette’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:100) [-Wdeprecated-declarations]
colorSelector.c:59:7: warning: ‘gtk_color_selection_get_current_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:145): Use 'gtk_color_chooser_get_rgba' instead [-Wdeprecated-declarations]
colorSelector.c: In function ‘do_colorsel’:
colorSelector.c:91:7: warning: ‘gtk_vbox_new’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkvbox.h:60): Use 'gtk_box_new' instead [-Wdeprecated-declarations]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Installed packages:
gtk2 2.24.14-1
gtk3 3.6.2-1
gcc-libs-multilib 4.7.2-2
gcc-multilib 4.7.2-2
Could someone please explain me the error?

Your source code is incomplete, the main function is missing. gtk-demo is organised so it has a single main that calls some submodules and processes them the same way, and you're missing that main.
The other warning come from the fact that this example code you're trying to compile was done for GTK 2, and you're trying to build it against GTK 3.
You'll find all the missing parts in the gtk-demo directory of the GTK+ repository.

Related

Failed to load keyboard layout DLL for layout A0000409: C:\WINDOWS\system32\kbdPrlUS.dll

My ultimate goal is to compile and build a GTK application for Windows using only the C programming language
I'm using a Macbook Pro with the M1 chip. So instead of developing on Mac and cross compiling for windows, I decided to try and develop on Windows for windows.
I'm using parallels to run a Windows 10 virtual machine and after a lot of trial and error I was finally able to compile and execute a GTK program using msys2.
There is just one problem: No keyboard functionality in the GTK application. With the error message in the command prompt: "Failed to load keyboard layout DLL for layout A0000409: C:\WINDOWS\system32\KbdPrlUS.dll"
I solved my previous DLL issue by adding a path to the system variables. How might I approach addressing this keyboard layout DLL issue? The file definitely already exists at that location.
EDIT Here is the code that compiles (and works) but doesn't register and letter keypress from the keyboard. The same code works on Mac OS just fine.
#include <gtk/gtk.h>
static GtkTextBuffer *tb = NULL;
static GtkTextBuffer *tb2 = NULL;
static void
quit_activated(GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GApplication *app = G_APPLICATION (user_data);
g_application_quit (app);
}
static void
click1_cb (GtkButton *btn,
gpointer user_data)
{
GtkTextIter start, end;
gtk_text_buffer_get_bounds(tb, &start, &end);
const GtkTextIter* start2 = &start;
const GtkTextIter* end2 = &end;
char* text = gtk_text_buffer_get_text(tb,start2,end2,false);
gtk_text_buffer_set_text (tb2, text, -1);
}
static void
app_activate (GApplication *app,
gpointer user_data)
{
GtkWidget *win;
GtkWidget *tv;
GtkWidget *tv2;
GtkWidget *box;
GtkWidget *btn;
GtkWidget *btn2;
gchar *text;
text =
"Input some text "
"As many lines as you want\n"
"One day, he went into a mountain and found a shining bamboo. "
"\"What a mysterious bamboo it is!,\" he said. "
"He cut it, then there was a small cute baby girl in it. "
"The girl was shining faintly. "
"He thought this baby girl is a gift from Heaven and took her home.\n"
"His wife was surprized at his tale. "
"They were very happy because they had no children. "
;
win = gtk_application_window_new (GTK_APPLICATION (app));
gtk_window_set_title (GTK_WINDOW (win), "Taketori");
gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
gtk_window_set_child (GTK_WINDOW (win), box);
btn = gtk_button_new_with_label ("Execute.");
g_signal_connect (btn, "clicked", G_CALLBACK (click1_cb), NULL);
tv = gtk_text_view_new ();
tv2 = gtk_text_view_new ();
tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
tb2 = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv2));
gtk_text_buffer_set_text (tb2, "", -1);
gtk_text_buffer_set_text (tb, text, -1);
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv2), GTK_WRAP_WORD_CHAR);
gtk_box_append (GTK_BOX (box),tv);
gtk_box_append (GTK_BOX (box), tv2);
gtk_box_append (GTK_BOX (box), btn);
/*
GSimpleAction *act_quit = g_simple_action_new ("quit", NULL);
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION(act_quit));
g_signal_connect (act_quit,"activate",G_CALLBACK (quit_activated),app);
GMenu *menubar = g_menu_new();
GMenuItem *menu_item_menu = g_menu_item_new ("Menu",NULL);
GMenu *menu = g_menu_new();
GMenuItem *menu_item_quit = g_menu_item_new ("Quit","app.quit");
g_menu_append_item (menu, menu_item_quit);
g_object_unref (menu_item_quit);
g_menu_set_submenu (menu_item_menu,G_MENU_MODEL(menu));
g_menu_append_item (menubar,menu_item_menu);
g_object_unref(menu_item_menu);*/
//gtk_application_set_menubar (GTK_APPLICATION (app), G_MENU_MODEL (menubar));
//gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (win), TRUE);
gtk_widget_show (win);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int stat;
app = gtk_application_new ("com.gitbut.fff", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL);
stat = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return stat;
}

GTK 2.0 Linker Error in Dev C++

So I found some tutorials on how to configure gtk to dev c++. I followed that tutorial very closely but when i tried to run this:
#include <gtk/gtk.h>
static void hello( GtkWidget *widget,
gpointer data )
{
g_print ("Hello World\n");
}
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");
return TRUE;
}
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked",
G_CALLBACK (hello), NULL);
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy),
window);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
It produces linker error like this:
[Linker error] undefined reference to g_print
And many more of this type.
How can I fix this?

how to compile gtk2.0 program with gtk3?

i have a simple program which is compiled with gtk2.0 in ubuntu.In ubuntu11.04 i installed gtk3.then i compile the same code,i got an error in the following line
/* Add a timer callback to update the value of the progress bar */
timer = gtk_timeout_add (100, progress_timeout, pdata);
i just comment the line and recompile it.then i got output file.but it is not properly working without the commented line.
in gtk2.0 i compiled by the following command
gcc progressbar.c `pkg-config --cflags --libs gtk+-2.0`
and in gtk3
gcc progressbar.c `pkg-config --cflags --libs gtk+-3.0`
I have a doubt that,is there any deprecation in that method in gtk3.please give me a link to an easy documentation with examples.what are the main difference between 2 and 3.
The full source code is as shown below
#include <gtk/gtk.h>
typedef struct _ProgressData {
GtkWidget *pbar;
} ProgressData;
gint progress_timeout( gpointer data )
{
ProgressData *pdata = (ProgressData *)data;
gdouble new_val;
new_val = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (pdata->pbar)) + 0.01;
if (new_val > 1.0)
new_val = 0.0;
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata->pbar), new_val);
return TRUE;
}
int main( int argc,
char *argv[])
{
ProgressData *pdata;
GtkWidget *align;
GtkWidget *window;
int timer;
GtkWidget *vbox;
gtk_init (&argc, &argv);
/* Allocate memory for the data that is passed to the callbacks */
pdata = g_malloc (sizeof (ProgressData));
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
g_signal_connect ( window, "destroy", gtk_main_quit, NULL ) ;
gtk_window_set_title (GTK_WINDOW (window), "GtkProgressBar");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
vbox = gtk_vbox_new (FALSE, 5);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
/* Create a centering alignment object */
align = gtk_alignment_new (0.5, 0.5, 0, 0);
gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 5);
gtk_widget_show (align);
/* Create the GtkProgressBar */
pdata->pbar = gtk_progress_bar_new ();
gtk_container_add (GTK_CONTAINER (align), pdata->pbar);
gtk_widget_show (pdata->pbar);
/* Add a timer callback to update the value of the progress bar */
timer = gtk_timeout_add (100, progress_timeout, pdata);
gtk_widget_show (window);
gtk_main ();
return 0;
}
You need to change the gtk_timeout_add call to g_timeout_add.
gtk_timeout_add ()
guint gtk_timeout_add (guint32 interval,
GtkFunction function,
gpointer data);
Warning
gtk_timeout_add has been deprecated since version 2.4 and should not be used in
newly-written code. Use g_timeout_add() instead.
Google "gtk_timeout_add g_timeout_add" will get you examples, e.g. this one, http://gna.org/patch/?2563.
As jesse told you, you're using gtk_timeout_add, which has been deprecated in GTK2. All the symbols deprecated in GTK2 were removed in GTK3.
To have your program work in GTK3, you need to make sure you don't use any GTK2-deprecated symbols. For this, use symbols like G_DISABLE_DEPRECATED (for GLib), GTK_DISABLE_DEPRECATED, etc. that can help you make sure when you compile with GTK2 that you're not using a symbol that was removed in GTK3.
There is also a GTK2 to GTK3 migration guide that you can use, as well as some GNOME Goals that link to the patches that were used in GNOME to accomplish that same task, for GLib and GTK symbols.

Detect click on Gtk::Image?

I've been trying to detect click on a Gtk::Image with gtkmm for over 2 hours, but I couldn't get it to work. It does compile and execute fine, but the event is never triggered.
Some stuff I tried, that compiles, does not crash, but doesn't work:
m_image = manage(new Gtk::Image(Gtk::Stock::APPLY, Gtk::ICON_SIZE_BUTTON));
m_image->add_events(Gdk::ALL_EVENTS_MASK);
m_hbox->pack_start(*m_image, Gtk::PACK_SHRINK);
m_image->signal_button_release_event()
.connect(sigc::hide(sigc::mem_fun(*this, &Todo::switchStatus)));
m_image->show();
or
#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/stock.h>
#include <gtkmm/image.h>
#include <iostream>
using namespace std;
class Win : public Gtk::Window
{
public:
Win();
bool link(GdkEventButton* e);
private:
Gtk::Image image;
};
Win::Win()
: image(Gtk::Stock::APPLY, Gtk::ICON_SIZE_BUTTON)
{
cerr << "created" << endl;
image.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
image.signal_button_release_event().connect(sigc::mem_fun(*this, &Win::link));
image.show();
add(image);
}
bool Win::link(GdkEventButton* e)
{
cerr << "kuh" << endl;
}
int main(int argc, char *argv[])
{
Gtk::Main app(argc, argv);
Gtk::Window window;
window.resize(300, 500);
Win win;
Gtk::Main::run(win);
return 0;
}
From http://developer.gnome.org/gtkmm/unstable/classGtk_1_1Image.html:
Gtk::Image is a "no window" widget (has no Gdk::Window of its own), so by default does not receive events. If you want to receive events on the image, such as button clicks, place the image inside a Gtk::EventBox, then connect to the event signals on the event box
So I guess try to put a signal on an eventbox after wrapping the image with the EventBox.
I like to put a button overtop of the image with an opacity of 0. Then you can use the click event from it instead.
This thread is quite old but still popular. I came across the same problem and I coded this code, maybe it can help someone save tim. My code detects the click ONLY if the pointer is hovering the image.
#include <gtk/gtk.h>
//compile with cc `pkg-config --cflags gtk+-3.0` text.c `pkg-config --libs gtk+-3.0` -o text
static void click_callback(GtkWidget *widget, GdkEventButton *event, gpointer );
static gboolean inRange(gint value, gint min, gint max);
static gboolean pointInRect(gint mouseX, gint mouseY, gint wx, gint wy, gint width, gint height);
GtkWidget *image;
GtkWidget *image2;
int main (int argc, char *argv[])
{
GtkWidget *event_box;
GtkWidget *window;
GtkWidget *box;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
/* GTK_ALIGN_CENTER is necessary otherwise
* the callback triggers from the space on
* the top and the bottom of the image */
gtk_widget_set_valign(box, GTK_ALIGN_CENTER);
image = gtk_image_new_from_file("image1.png");
image2 = gtk_image_new_from_file("image2.png");
gtk_box_pack_start(GTK_BOX(box), image, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), image2, TRUE, FALSE, 0);
event_box = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER (event_box), box);
gtk_container_add (GTK_CONTAINER (window), event_box);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect( event_box, "button_press_event", G_CALLBACK( click_callback ), image);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "Image click event");
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void click_callback(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
GtkAllocation alloc;
GtkAllocation alloc2;
gtk_widget_get_allocation(image, &alloc);
gtk_widget_get_allocation(image2, &alloc2);
if (pointInRect(event->x, event->y, alloc.x, alloc.y, alloc.width, alloc.height))
g_print("You clicked from image\n");
if (pointInRect(event->x, event->y, alloc2.x, alloc2.y, alloc2.width, alloc2.height))
g_print("You clicked from image2\n");
}
static gboolean pointInRect(gint mouseX, gint mouseY, gint wx, gint wy, gint width, gint height)
{
return inRange(mouseX, wx, wx + width) &&
inRange(mouseY, wy, wy + height);
}
static gboolean inRange(gint value, gint min, gint max)
{
return (value >= min && value <= max);
}

OpenGL on Mac operation

This is really architecture question or 'how does it work' question than a problem to solve.
Apple documentation claims that CGL is lowest level api for managing OpenGL contexts, yet is lacks functionality that allows to connect a context to a window.
AGL and Cocoa can bind a context to a window without a problem though, so the question is - how do they do that if they are built upon CGL?
The obvious way appears to be that they use CGL to render to offscreen memory and are then capable of compositing this somehow. If this is so, how does that happen?
There is a private function CGLSetSurface that connects a surface that is part of a window to a GL context created with CGLCreateContext. Both AGL and Cocoa use this function internally.
Complete example:
/*
mkdir -p build/test.app/Contents/MacOS
clang++ --std=c++11
-fno-exceptions
-fno-rtti
-mmacosx-version-min=10.9
-Wno-writable-strings
-Wno-deprecated-declarations
-framework OpenGL
-framework Carbon
-g gui8.cpp
-o build/test.app/Contents/MacOS/test
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
#include <OpenGL/CGLTypes.h>
#include <OpenGL/CGLCurrent.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
typedef int CGSConnectionID;
typedef int CGSWindowID;
typedef int CGSSurfaceID;
typedef uint32_t _CGWindowID;
extern "C" {
typedef int CGSConnection;
typedef int CGSWindow;
typedef int CGSValue;
typedef enum _CGSWindowOrderingMode {
kCGSOrderAbove = 1, // Window is ordered above target.
kCGSOrderBelow = -1, // Window is ordered below target.
kCGSOrderOut = 0 // Window is removed from the on-screen window list.
} CGSWindowOrderingMode;
typedef void *CGSRegion;
typedef CGSRegion *CGSRegionRef;
typedef CGSWindow *CGSWindowRef;
extern CGError CGSNewWindow( CGSConnection cid, int, float, float, const CGSRegion, CGSWindowRef);
extern CGError CGSNewRegionWithRect( const CGRect * rect, CGSRegionRef newRegion );
extern OSStatus CGSOrderWindow(CGSConnection cid, CGSWindow win, CGSWindowOrderingMode place, CGSWindow relativeToWindow /* nullable */);
extern OSStatus CGSSetWindowProperty(const CGSConnection cid, CGSWindow wid, CGSValue key, CGSValue value);
extern CGSConnectionID CGSMainConnectionID(void);
extern CGError CGSAddSurface(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID *sid);
extern CGError CGSSetSurfaceBounds(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID sid, CGRect rect);
extern CGError CGSOrderSurface(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID sid, int a, int b);
extern OSStatus CGSMoveWindow(const CGSConnection cid, const CGSWindow wid, CGPoint *point);
extern CGLError CGLSetSurface(CGLContextObj gl, CGSConnectionID cid, CGSWindowID wid, CGSSurfaceID sid);
}
#define kCGSBufferedBackingType 2
int main () {
CGLContextObj cgl_context = NULL;
CGSWindow window = 0;
int width = 500, height = 500;
CGPoint window_pos = { .x = 200, .y = 200 };
bool quit = false;
CGSConnectionID connection_id = CGSMainConnectionID();
assert(connection_id);
{
CGSRegion region = NULL;
CGRect r = CGRectMake(0,0, width, height);
auto err1 = CGSNewRegionWithRect(&r, &region);
assert(region);
auto err2 = CGSNewWindow(connection_id, kCGSBufferedBackingType, window_pos.x, window_pos.y, region, &window);
assert(window);
auto err3 = CGSOrderWindow(connection_id, window, kCGSOrderAbove, 0);
assert (err3 == kCGErrorSuccess);
CGLPixelFormatAttribute attributes[] = {
kCGLPFADoubleBuffer,
kCGLPFAAccelerated, // Hardware rendering
// kCGLPFARendererID, (CGLPixelFormatAttribute) kCGLRendererGenericFloatID, // Software rendering
(CGLPixelFormatAttribute)0
};
CGLPixelFormatObj pix;
GLint num;
auto err4 = CGLChoosePixelFormat(attributes, &pix, &num);
assert(err4 == kCGLNoError); // CGLErrorString(err1)
assert(pix);
CGLCreateContext(pix, NULL, &cgl_context);
assert(cgl_context);
CGLDestroyPixelFormat(pix);
CGLSetCurrentContext(cgl_context);
GLint v_sync_enabled = 1;
CGLSetParameter(cgl_context, kCGLCPSwapInterval, &v_sync_enabled);
CGSSurfaceID surface_id = 0;
auto err5 = CGSAddSurface(connection_id, window, &surface_id);
assert(err5 == kCGErrorSuccess);
auto err6 = CGSSetSurfaceBounds(connection_id, window, surface_id, CGRectMake(0, 0, width, height));
assert(err6 == kCGErrorSuccess);
auto err7 = CGSOrderSurface(connection_id, window, surface_id, 1, 0);
assert(err7 == kCGErrorSuccess);
auto err8 = CGLSetSurface(cgl_context, connection_id, window, surface_id);
assert(err8 == kCGLNoError);
GLint drawable = 0;
CGLGetParameter(cgl_context, kCGLCPHasDrawable, &drawable);
assert(drawable == 1);
}
assert(glGetError() == GL_NO_ERROR);
CGPoint drag_starting_position;
bool drag_started = false;
while (!quit) {
glClearColor(1,1,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
static float a = 0;
glRotatef(a * 1000, 0, 0, 1);
// printf("a: %f\n", a);
a= a + .001;
glBegin(GL_QUADS);
if (a>1.5) a=0;
glColor4f(0,a,1,1);
glVertex2f(0.25, 0.25);
glVertex2f(0.75, 0.25);
glVertex2f(0.75, 0.75);
glVertex2f(0.25, 0.75);
glEnd();
auto err1 = CGLFlushDrawable(cgl_context);
assert(err1 == kCGLNoError);
assert(glGetError() == GL_NO_ERROR);
}
CGLSetCurrentContext(NULL);
CGLDestroyContext(cgl_context);
}
I didn't revisit the question ever since, this is what I was able to make out of it:
The undocumented api's that float around the net appear to be a missing block from all that - I was able to CGLSetSurface without it returning an error, however it didn't do all that much in the end. Apparently there is some other stuff that needs to be done to make everything work on such a low level.
In all, there doesn't appear to be a sane way to control everything through CGL. The way to handle everything is like everyone else is doing apparently - through Cocoa classes (using CGL for all the stuff other then attaching to window is fine though after that point).

Resources