GTKMainWindow.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // Filename:  GTKMainWindow.cpp
00003 // Author:    Florian Hecht
00004 // Date:      2009
00005 // ****************************************************************************
00006 
00007 
00008 // ****************************************************************************
00009 // Includes
00010 // ****************************************************************************
00011 
00012 #include "GTKMainWindow.h"
00013 #include "GTKMainWindowWidget.h"
00014 #include "GTKApplicationHandler.h"
00015 #include "Interfaces/MainWindowEventInterface.h"
00016 
00017 #include "Image/ByteImage.h"
00018 #include "Image/ImageProcessor.h"
00019 
00020 #ifdef USE_OPENGL
00021 #include "GTKGLContext.h"
00022 #endif
00023 
00024 #include <string.h>
00025 
00026 
00027 
00028 static unsigned int last_modifier_key_state = 0;
00029 
00030 static void button_pressed_callback(GtkWidget *widget, gpointer data)
00031 {
00032         CGTKMainWindowWidget *ptr = (CGTKMainWindowWidget*)data;
00033         if (ptr)
00034                 ptr->Clicked();
00035 }
00036 
00037 static void checkbox_toggled_callback(GtkWidget *widget, gpointer data)
00038 {
00039         int state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
00040         CGTKMainWindowWidget *ptr = (CGTKMainWindowWidget*)data;
00041         if (ptr)
00042                 ptr->Toggled(state > 0);
00043 }
00044 
00045 static void slider_changed_callback(GtkWidget *widget, gpointer data)
00046 {
00047         CGTKMainWindowWidget *ptr = (CGTKMainWindowWidget*)data;
00048         
00049         if (ptr)
00050         {
00051                 int value = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(ptr->m_obj));
00052                 ptr->ValueChanged(value);
00053         }
00054 }
00055 
00056 static void combo_box_changed_callback(GtkWidget *widget, gpointer data)
00057 {
00058         CGTKMainWindowWidget *ptr = (CGTKMainWindowWidget*)data;
00059         
00060         if (ptr)
00061         {
00062                 int value = gtk_combo_box_get_active(GTK_COMBO_BOX(ptr->m_widget));
00063                 ptr->ValueChanged(value);
00064         }
00065 }
00066 
00067 static void text_edit_changed_callback(GtkWidget *widget, gpointer data)
00068 {
00069         CGTKMainWindowWidget *ptr = (CGTKMainWindowWidget*)data;
00070         
00071         if (ptr)
00072         {
00073                 ptr->TextChanged(NULL);
00074         }
00075 }
00076 
00077 static gboolean ivtimage_configure_event(GtkWidget *widget, GdkEventConfigure *event)
00078 {
00079         IVTImage *ptr = (IVTImage *)widget;
00080 
00081         if (ptr->pixmap)
00082                 g_object_unref(ptr->pixmap);
00083 
00084         ptr->pixmap = gdk_pixmap_new(widget->window,
00085                           widget->allocation.width,
00086                           widget->allocation.height,
00087                           -1);
00088         gdk_draw_rectangle(ptr->pixmap,
00089                       widget->style->black_gc,
00090                       TRUE,
00091                       0, 0,
00092                       widget->allocation.width,
00093                       widget->allocation.height);
00094 
00095         return TRUE;
00096 }
00097 
00098 static gboolean ivtimage_expose_event(GtkWidget *widget, GdkEventExpose *event)
00099 {
00100         IVTImage *ptr = (IVTImage *)widget;
00101         
00102         GdkPixmap *pixmap = ptr->pixmap;
00103         
00104         if (ptr->image == 0)
00105         {
00106                 gdk_draw_drawable(widget->window,
00107                             widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
00108                             pixmap,
00109                             event->area.x, event->area.y,
00110                             event->area.x, event->area.y,
00111                             event->area.width, event->area.height);
00112         }
00113         else
00114         {
00115                 gdk_draw_rgb_image(widget->window,
00116                             widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
00117                             0, 0, ptr->image->width, ptr->image->height,
00118                             GDK_RGB_DITHER_NONE, ptr->image->pixels, ptr->image->width*3);
00119         }
00120         
00121         if (ptr->mouse_down == 1)
00122         {
00123                 int x0 = ptr->mouse_start_x;
00124                 int y0 = ptr->mouse_start_y;
00125                 int x1 = ptr->mouse_x;
00126                 int y1 = ptr->mouse_y;
00127                 
00128                 if (x0 > x1)
00129                 {
00130                         int tmp = x0;
00131                         x0 = x1;
00132                         x1 = tmp;
00133                 }
00134                 if (y0 > y1)
00135                 {
00136                         int tmp = y0;
00137                         y0 = y1;
00138                         y1 = tmp;
00139                 }
00140                 
00141                 int dx = x1 - x0;
00142                 int dy = y1 - y0;
00143                 
00144                 GdkGC *gc = widget->style->white_gc;
00145                 gdk_gc_set_function(gc, GDK_XOR);
00146                 gdk_draw_rectangle(widget->window,
00147                               gc,
00148                               FALSE,
00149                               x0, y0,
00150                               dx, dy);
00151                 gdk_gc_set_function(gc, GDK_COPY);
00152         }
00153 
00154         return FALSE;
00155 }
00156 
00157 static gboolean ivtimage_button_press_event(GtkWidget *widget, GdkEventButton *event)
00158 {
00159         IVTImage *ptr = (IVTImage *)widget;
00160         
00161         guint modifiers = gtk_accelerator_get_default_mod_mask();
00162         last_modifier_key_state = event->state & modifiers;
00163 
00164         gtk_widget_grab_focus(widget);
00165 
00166         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00167         if (callback)
00168         {
00169                 int btn = 0;
00170                 if (event->button == 1) btn = IVT_LEFT_BUTTON;
00171                 if (event->button == 3) btn = IVT_RIGHT_BUTTON;
00172                 if (event->button == 2) btn = IVT_MIDDLE_BUTTON;
00173                 
00174                 callback->MouseDown((WIDGET_HANDLE)ptr->main_window_widget, btn, (int)event->x, (int)event->y);
00175         }
00176                 
00177         if (event->button == 1)
00178         {
00179                 ptr->mouse_down = 1;
00180                 ptr->mouse_start_x = event->x;
00181                 ptr->mouse_start_y = event->y;
00182                 ptr->mouse_x = event->x;
00183                 ptr->mouse_y = event->y;
00184         }
00185 
00186         return TRUE;
00187 }
00188 
00189 static gboolean ivtimage_button_release_event(GtkWidget *widget, GdkEventButton *event)
00190 {
00191         IVTImage *ptr = (IVTImage *)widget;
00192         
00193         guint modifiers = gtk_accelerator_get_default_mod_mask();
00194         last_modifier_key_state = event->state & modifiers;
00195         
00196         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00197         if (callback)
00198         {
00199                 int btn = 0;
00200                 if (event->button == 1) btn = IVT_LEFT_BUTTON;
00201                 if (event->button == 3) btn = IVT_RIGHT_BUTTON;
00202                 if (event->button == 2) btn = IVT_MIDDLE_BUTTON;
00203                 
00204                 callback->MouseUp((WIDGET_HANDLE)ptr->main_window_widget, btn, (int)event->x, (int)event->y);
00205         }
00206         
00207         if (ptr->mouse_down == 1 && event->button == 1)
00208         {
00209                 ptr->mouse_down = 0;
00210                 
00211                 int x0 = ptr->mouse_start_x;
00212                 int y0 = ptr->mouse_start_y;
00213                 int x1 = ptr->mouse_x;
00214                 int y1 = ptr->mouse_y;
00215                 
00216                 if (x0 > x1)
00217                 {
00218                         int tmp = x0;
00219                         x0 = x1;
00220                         x1 = tmp;
00221                 }
00222                 if (y0 > y1)
00223                 {
00224                         int tmp = y0;
00225                         y0 = y1;
00226                         y1 = tmp;
00227                 }
00228                 
00229                 int dx = x1 - x0;
00230                 int dy = y1 - y0;
00231                 
00232                 CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00233                 if (callback)
00234                 {
00235                         if (dx < 5 && dy < 5)
00236                         {
00237                                 callback->PointClicked((WIDGET_HANDLE)ptr->main_window_widget, (x0 + x1) / 2, (y0 + y1) / 2);
00238                         }
00239                         else
00240                         {
00241                                 callback->RectSelected((WIDGET_HANDLE)ptr->main_window_widget, x0, y0, x1, y1);
00242                         }
00243                 }
00244                 
00245                 gtk_widget_queue_draw_area(widget, x0, y0, dx, dy);
00246                 
00247         }
00248 
00249         return TRUE;
00250 }
00251 
00252 static gboolean ivtimage_motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
00253 {
00254         IVTImage *ptr = (IVTImage *)widget;
00255         
00256         guint modifiers = gtk_accelerator_get_default_mod_mask();
00257         last_modifier_key_state = event->state & modifiers;
00258         
00259         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00260         if (callback)
00261         {
00262                 int x, y;
00263                 GdkModifierType state;
00264 
00265                 if (event->is_hint)
00266                         gdk_window_get_pointer(event->window, &x, &y, &state);
00267                 else
00268                 {
00269                         x = (int)event->x;
00270                         y = (int)event->y;
00271                         state = (GdkModifierType)event->state;
00272                 }
00273                 
00274                 callback->MouseMove((WIDGET_HANDLE)ptr->main_window_widget, x, y);
00275         }
00276         
00277         if (ptr->mouse_down != 1)
00278                 return FALSE;
00279                 
00280         int x, y;
00281         GdkModifierType state;
00282 
00283         if (event->is_hint)
00284                 gdk_window_get_pointer(event->window, &x, &y, &state);
00285         else
00286         {
00287                 x = event->x;
00288                 y = event->y;
00289                 state = (GdkModifierType)event->state;
00290         }
00291 
00292         if (state & GDK_BUTTON1_MASK)
00293         {
00294                 ptr->mouse_x = x;
00295                 ptr->mouse_y = y;
00296                 
00297                 gtk_widget_queue_draw_area(widget, 0, 0, ptr->image->width, ptr->image->height);
00298         }
00299 
00300         return TRUE;
00301 }
00302 
00303 static gboolean ivtimage_key_press_event(GtkWidget *widget, GdkEventKey *event)
00304 {
00305         IVTImage *ptr = (IVTImage *)widget;
00306         
00307         guint modifiers = gtk_accelerator_get_default_mod_mask();
00308         last_modifier_key_state = event->state & modifiers;
00309         
00310         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00311         if (callback)
00312         {
00313                 if (event->length != 1)
00314                 {
00315                         callback->KeyDown((WIDGET_HANDLE)ptr->main_window_widget, -1);
00316                 }
00317                 else
00318                 {
00319                         callback->KeyDown((WIDGET_HANDLE)ptr->main_window_widget, event->string[0]);
00320                 }
00321         }
00322 
00323         return TRUE;
00324 }
00325 
00326 static gboolean ivtimage_key_release_event(GtkWidget *widget, GdkEventKey *event)
00327 {
00328         IVTImage *ptr = (IVTImage *)widget;
00329         
00330         guint modifiers = gtk_accelerator_get_default_mod_mask();
00331         last_modifier_key_state = event->state & modifiers;
00332         
00333         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00334         if (callback)
00335         {
00336                 if (event->length != 1)
00337                 {
00338                         callback->KeyUp((WIDGET_HANDLE)ptr->main_window_widget, -1);
00339                 }
00340                 else
00341                 {
00342                         callback->KeyUp((WIDGET_HANDLE)ptr->main_window_widget, event->string[0]);
00343                 }
00344         }
00345 
00346         return TRUE;
00347 }
00348 
00349 
00350 #ifdef USE_OPENGL
00351 
00352 
00353 static gboolean ivtglwidget_configure_event(GtkWidget *widget, GdkEventConfigure *event)
00354 {
00355         IVTGLWidget *ptr = (IVTGLWidget *)widget;
00356 
00357         if (ptr->glcontext)
00358         {
00359                 // the first time the widget becomes visible we make it the current gl context
00360                 ptr->glcontext->MakeCurrent(widget->window);
00361         }
00362 
00363         return TRUE;
00364 }
00365 
00366 static gboolean ivtglwidget_button_press_event(GtkWidget *widget, GdkEventButton *event)
00367 {
00368         IVTGLWidget *ptr = (IVTGLWidget *)widget;
00369         
00370         guint modifiers = gtk_accelerator_get_default_mod_mask();
00371         last_modifier_key_state = event->state & modifiers;
00372         
00373         gtk_widget_grab_focus(widget);
00374 
00375         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00376         if (callback)
00377         {
00378                 int btn = 0;
00379                 if (event->button == 1) btn = IVT_LEFT_BUTTON;
00380                 if (event->button == 3) btn = IVT_RIGHT_BUTTON;
00381                 if (event->button == 2) btn = IVT_MIDDLE_BUTTON;
00382                 
00383                 callback->MouseDown((WIDGET_HANDLE)ptr->main_window_widget, btn, (int)event->x, (int)event->y);
00384         }
00385 
00386         return TRUE;
00387 }
00388 
00389 static gboolean ivtglwidget_button_release_event(GtkWidget *widget, GdkEventButton *event)
00390 {
00391         IVTGLWidget *ptr = (IVTGLWidget *)widget;
00392         
00393         guint modifiers = gtk_accelerator_get_default_mod_mask();
00394         last_modifier_key_state = event->state & modifiers;
00395         
00396         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00397         if (callback)
00398         {
00399                 int btn = 0;
00400                 if (event->button == 1) btn = IVT_LEFT_BUTTON;
00401                 if (event->button == 3) btn = IVT_RIGHT_BUTTON;
00402                 if (event->button == 2) btn = IVT_MIDDLE_BUTTON;
00403                 
00404                 callback->MouseUp((WIDGET_HANDLE)ptr->main_window_widget, btn, (int)event->x, (int)event->y);
00405         }
00406 
00407         return TRUE;
00408 }
00409 
00410 static gboolean ivtglwidget_motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
00411 {
00412         IVTGLWidget *ptr = (IVTGLWidget *)widget;
00413         
00414         guint modifiers = gtk_accelerator_get_default_mod_mask();
00415         last_modifier_key_state = event->state & modifiers;
00416         
00417         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00418         if (callback)
00419         {
00420                 int x, y;
00421                 GdkModifierType state;
00422 
00423                 if (event->is_hint)
00424                         gdk_window_get_pointer(event->window, &x, &y, &state);
00425                 else
00426                 {
00427                         x = (int)event->x;
00428                         y = (int)event->y;
00429                         state = (GdkModifierType)event->state;
00430                 }
00431                 
00432                 callback->MouseMove((WIDGET_HANDLE)ptr->main_window_widget, x, y);
00433         }
00434 
00435         return TRUE;
00436 }
00437 
00438 static gboolean ivtglwidget_key_press_event(GtkWidget *widget, GdkEventKey *event)
00439 {
00440         IVTGLWidget *ptr = (IVTGLWidget *)widget;
00441         
00442         guint modifiers = gtk_accelerator_get_default_mod_mask();
00443         last_modifier_key_state = event->state & modifiers;
00444         
00445         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00446         if (callback)
00447         {
00448                 if (event->length != 1)
00449                 {
00450                         callback->KeyDown((WIDGET_HANDLE)ptr->main_window_widget, -1);
00451                 }
00452                 else
00453                 {
00454                         callback->KeyDown((WIDGET_HANDLE)ptr->main_window_widget, event->string[0]);
00455                 }
00456         }
00457 
00458         return TRUE;
00459 }
00460 
00461 static gboolean ivtglwidget_key_release_event(GtkWidget *widget, GdkEventKey *event)
00462 {
00463         IVTGLWidget *ptr = (IVTGLWidget *)widget;
00464         
00465         guint modifiers = gtk_accelerator_get_default_mod_mask();
00466         last_modifier_key_state = event->state & modifiers;
00467         
00468         CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
00469         if (callback)
00470         {
00471                 if (event->length != 1)
00472                 {
00473                         callback->KeyUp((WIDGET_HANDLE)ptr->main_window_widget, -1);
00474                 }
00475                 else
00476                 {
00477                         callback->KeyUp((WIDGET_HANDLE)ptr->main_window_widget, event->string[0]);
00478                 }
00479         }
00480 
00481         return TRUE;
00482 }
00483 
00484 #endif /* USE_OPENGL */
00485 
00486 
00487 static int destroy_window_callback(GtkWidget *widget, gpointer data)
00488 {
00489         // process internal things (reference counter, exit flag)
00490         CGTKMainWindow *pMainWindow = (CGTKMainWindow *) data;
00491         pMainWindow->Destroy(widget);
00492         
00493         // destroy widget
00494         gtk_widget_destroy(widget);
00495         
00496         return 0;
00497 }
00498 
00499 
00500 int CGTKMainWindow::m_ref_count = 0;
00501 
00502 void CGTKMainWindow::Destroy(GtkWidget *widget)
00503 {
00504         // set flag, so that the destructor knows it doesn't have to destroy m_main_window again
00505         if (widget == m_main_window)
00506                 m_bDestroyed = true;
00507         
00508         if (--m_ref_count == 0)
00509         {
00510                 CGTKApplicationHandler *pApplicationHandler = CGTKApplicationHandler::GetApplicationHandler();
00511                 if (pApplicationHandler)
00512                         pApplicationHandler->SetExit(true);
00513         }
00514 }
00515 
00516 
00517 CGTKMainWindow::CGTKMainWindow(int x, int y, int width, int height, const char *title)
00518 {
00519         m_main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00520         gtk_window_set_title(GTK_WINDOW(m_main_window), title);
00521         gtk_window_set_default_size(GTK_WINDOW(m_main_window), width, height);
00522         gtk_window_set_position(GTK_WINDOW(m_main_window), GTK_WIN_POS_NONE);
00523         gtk_window_move(GTK_WINDOW(m_main_window), x, y);
00524 
00525         g_signal_connect(G_OBJECT(m_main_window), "destroy", G_CALLBACK(destroy_window_callback), this);
00526         
00527         m_fixed_container = gtk_fixed_new();
00528         gtk_container_add(GTK_CONTAINER(m_main_window), m_fixed_container);
00529         gtk_widget_show(m_fixed_container);
00530         
00531         m_event_callback = NULL;
00532         m_ref_count++;
00533         
00534         m_bDestroyed = false;
00535 }
00536 
00537 CGTKMainWindow::~CGTKMainWindow()
00538 {
00539         // delete widgets in inverse order
00540         const int nWidgets = m_widgets.size();
00541         for (int i = nWidgets - 1; i >= 0; i--)
00542                 delete m_widgets[i];
00543         
00544         // clear list
00545         m_widgets.clear();
00546         
00547         // check if m_main_window has already been destroyed (by closing the window)
00548         if (!m_bDestroyed)
00549         {
00550                 // m_main_window has not been destroyed yet, destroy it now
00551                 destroy_window_callback(m_main_window, this); // has same effect
00552         
00553                 // process pending events (otherwise window the won't close)
00554                 while (gtk_events_pending())
00555                         gtk_main_iteration();
00556         }
00557 }
00558 
00559 // create widgets
00560 WIDGET_HANDLE CGTKMainWindow::AddImage(int x, int y, int width, int height, WIDGET_HANDLE parent)
00561 {
00562         CGTKMainWindowWidget *w = new CGTKMainWindowWidget(this, eImage);
00563         
00564         GtkWidget *image;
00565         image = ivtimage_new();
00566         
00567         gtk_signal_connect(GTK_OBJECT(image), "expose_event", (GtkSignalFunc)ivtimage_expose_event, NULL);
00568         gtk_signal_connect(GTK_OBJECT(image), "configure_event", (GtkSignalFunc)ivtimage_configure_event, NULL);
00569         gtk_signal_connect(GTK_OBJECT(image), "motion_notify_event", (GtkSignalFunc)ivtimage_motion_notify_event, NULL);
00570         gtk_signal_connect(GTK_OBJECT(image), "button_press_event", (GtkSignalFunc)ivtimage_button_press_event, NULL);
00571         gtk_signal_connect(GTK_OBJECT(image), "button_release_event", (GtkSignalFunc)ivtimage_button_release_event, NULL);
00572         gtk_signal_connect(GTK_OBJECT(image), "key_press_event", (GtkSignalFunc)ivtimage_key_press_event, NULL);
00573         gtk_signal_connect(GTK_OBJECT(image), "key_release_event", (GtkSignalFunc)ivtimage_key_release_event, NULL);
00574 
00575         gtk_widget_set_events(image, GDK_EXPOSURE_MASK
00576                          | GDK_LEAVE_NOTIFY_MASK
00577                          | GDK_BUTTON_PRESS_MASK
00578                          | GDK_BUTTON_RELEASE_MASK
00579                          | GDK_POINTER_MOTION_MASK
00580                          | GDK_POINTER_MOTION_HINT_MASK
00581                          | GDK_KEY_PRESS_MASK
00582                          | GDK_KEY_RELEASE_MASK);
00583 
00584         
00585         gtk_fixed_put(GTK_FIXED(m_fixed_container), image, x, y);
00586         gtk_drawing_area_size(GTK_DRAWING_AREA(image), width, height);
00587         
00588         ((IVTImage*)image)->image = new CByteImage(width, height, CByteImage::eRGB24);
00589         ((IVTImage*)image)->main_window_widget = w;
00590         
00591         gtk_widget_show(image);
00592         
00593         w->m_widget = image;
00594         
00595         m_widgets.push_back(w);
00596         
00597         return (WIDGET_HANDLE)w;
00598 }
00599 
00600 WIDGET_HANDLE CGTKMainWindow::AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00601 {
00602         CGTKMainWindowWidget *w = new CGTKMainWindowWidget(this, eButton);
00603         
00604         GtkWidget *button;
00605         button = gtk_button_new_with_label(text);
00606         g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(button_pressed_callback), w);
00607         gtk_fixed_put(GTK_FIXED(m_fixed_container), button, x, y);
00608         gtk_widget_set_size_request(button, width, height);
00609         gtk_widget_show(button);
00610         
00611         w->m_widget = button;
00612         
00613         m_widgets.push_back(w);
00614         
00615         return (WIDGET_HANDLE)w;
00616 }
00617 
00618 WIDGET_HANDLE CGTKMainWindow::AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00619 {
00620         CGTKMainWindowWidget *w = new CGTKMainWindowWidget(this, eLabel);
00621         
00622         GtkWidget *label;
00623         label = gtk_label_new(text);
00624         gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
00625         gtk_fixed_put(GTK_FIXED(m_fixed_container), label, x, y);
00626         gtk_widget_set_size_request(label, width, height);
00627         gtk_widget_show(label);
00628         
00629         w->m_widget = label;
00630         
00631         m_widgets.push_back(w);
00632         
00633         return (WIDGET_HANDLE)w;
00634 }
00635 
00636 WIDGET_HANDLE CGTKMainWindow::AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent)
00637 {
00638         CGTKMainWindowWidget *w = new CGTKMainWindowWidget(this, eCheckBox);
00639         
00640         GtkWidget *check_button;
00641         check_button = gtk_check_button_new_with_label(text);
00642         g_signal_connect(G_OBJECT(check_button), "toggled", G_CALLBACK(checkbox_toggled_callback), w);
00643         gtk_fixed_put(GTK_FIXED(m_fixed_container), check_button, x, y);
00644         gtk_widget_set_size_request(check_button, width, height);
00645         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button), checked);
00646         gtk_widget_show(check_button);
00647         
00648         w->m_widget = check_button;
00649         
00650         m_widgets.push_back(w);
00651         
00652         return (WIDGET_HANDLE)w;
00653 }
00654 
00655 WIDGET_HANDLE CGTKMainWindow::AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
00656 {
00657         CGTKMainWindowWidget *w = new CGTKMainWindowWidget(this, eTextEdit);
00658         
00659         GtkWidget *edit;
00660         edit = gtk_entry_new();
00661         
00662         gtk_entry_set_text(GTK_ENTRY(edit), text);
00663         
00664         g_signal_connect(G_OBJECT(edit), "changed", G_CALLBACK(text_edit_changed_callback), w);
00665         
00666         gtk_fixed_put(GTK_FIXED(m_fixed_container), edit, x, y);
00667         gtk_widget_set_size_request(edit, width, height);
00668         gtk_widget_show(edit);
00669         
00670         w->m_widget = edit;
00671         
00672         m_widgets.push_back(w);
00673         
00674         return (WIDGET_HANDLE)w;
00675 }
00676 
00677 WIDGET_HANDLE CGTKMainWindow::AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent)
00678 {
00679         CGTKMainWindowWidget *w = new CGTKMainWindowWidget(this, eSlider);
00680         
00681         GtkObject *adjustment;
00682         adjustment = gtk_adjustment_new(value, min_value, max_value, 1.0, step, 1.0);
00683         g_signal_connect(G_OBJECT(adjustment), "value_changed", G_CALLBACK(slider_changed_callback), w);
00684 
00685         GtkWidget *slider;
00686         slider = gtk_hscale_new(GTK_ADJUSTMENT(adjustment));
00687         gtk_scale_set_draw_value(GTK_SCALE(slider), FALSE);
00688         
00689         gtk_fixed_put(GTK_FIXED(m_fixed_container), slider, x, y);
00690         gtk_widget_set_size_request(slider, width, height);
00691         gtk_widget_show(slider);
00692         
00693         w->m_widget = slider;
00694         w->m_obj = adjustment;
00695         
00696         m_widgets.push_back(w);
00697         
00698         return (WIDGET_HANDLE)w;
00699 }
00700 
00701 WIDGET_HANDLE CGTKMainWindow::AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent)
00702 {
00703         CGTKMainWindowWidget *w = new CGTKMainWindowWidget(this, eComboBox);
00704         
00705         GtkWidget *cbox;
00706         cbox = gtk_combo_box_new_text();
00707         
00708         for (int i = 0; i < num_entries; i++)
00709         {
00710                 gtk_combo_box_append_text(GTK_COMBO_BOX(cbox), entries[i]);
00711         }
00712         
00713         gtk_combo_box_set_active(GTK_COMBO_BOX(cbox), current_entry);
00714         
00715         g_signal_connect(G_OBJECT(cbox), "changed", G_CALLBACK(combo_box_changed_callback), w);
00716         
00717         gtk_fixed_put(GTK_FIXED(m_fixed_container), cbox, x, y);
00718         gtk_widget_set_size_request(cbox, width, height);
00719         gtk_widget_show(cbox);
00720         
00721         w->m_widget = cbox;
00722         
00723         m_widgets.push_back(w);
00724         
00725         return (WIDGET_HANDLE)w;
00726 }
00727 
00728 WIDGET_HANDLE CGTKMainWindow::AddGLWidget(int x, int y, int width, int height, WIDGET_HANDLE parent)
00729 {
00730 #ifdef USE_OPENGL
00731         CGTKMainWindowWidget *w = new CGTKMainWindowWidget(this, eGLWidget);
00732         
00733         CGTKGLContext *glcontext = new CGTKGLContext();
00734         if (!glcontext->IsInitialized())
00735         {
00736                 printf("CGTKMainWindow::AddGLWidget: creation of GLWidget failed\n");
00737                 return NULL;
00738         }
00739         
00740         // get the visual for the OpenGL context
00741         GdkVisual *visual = glcontext->GetVisual();
00742         
00743         // use a colormap and the visual that is used for the OpenGL context
00744         gtk_widget_push_colormap(gdk_colormap_new(visual, TRUE));
00745         gtk_widget_push_visual(visual);
00746 
00747         GtkWidget *glw;
00748         glw = ivtglwidget_new();
00749         
00750         // pop back the default visual settings
00751         gtk_widget_pop_visual();
00752         gtk_widget_pop_colormap();
00753         
00754         gtk_signal_connect(GTK_OBJECT(glw), "configure_event", (GtkSignalFunc)ivtglwidget_configure_event, NULL);
00755         gtk_signal_connect(GTK_OBJECT(glw), "motion_notify_event", (GtkSignalFunc)ivtglwidget_motion_notify_event, NULL);
00756         gtk_signal_connect(GTK_OBJECT(glw), "button_press_event", (GtkSignalFunc)ivtglwidget_button_press_event, NULL);
00757         gtk_signal_connect(GTK_OBJECT(glw), "button_release_event", (GtkSignalFunc)ivtglwidget_button_release_event, NULL);
00758         gtk_signal_connect(GTK_OBJECT(glw), "key_press_event", (GtkSignalFunc)ivtglwidget_key_press_event, NULL);
00759         gtk_signal_connect(GTK_OBJECT(glw), "key_release_event", (GtkSignalFunc)ivtglwidget_key_release_event, NULL);
00760 
00761         gtk_widget_set_events(glw, GDK_LEAVE_NOTIFY_MASK
00762                          | GDK_BUTTON_PRESS_MASK
00763                          | GDK_BUTTON_RELEASE_MASK
00764                          | GDK_POINTER_MOTION_MASK
00765                          | GDK_POINTER_MOTION_HINT_MASK
00766                          | GDK_KEY_PRESS_MASK
00767                          | GDK_KEY_RELEASE_MASK);
00768 
00769         
00770         gtk_fixed_put(GTK_FIXED(m_fixed_container), glw, x, y);
00771         gtk_drawing_area_size(GTK_DRAWING_AREA(glw), width, height);
00772         
00773         ((IVTGLWidget*)glw)->main_window_widget = w;
00774         ((IVTGLWidget*)glw)->glcontext = glcontext;
00775         
00776         gtk_widget_show(glw);
00777         
00778         // this forces the window to be created, so we can call MakeCurrent
00779         gtk_widget_realize(glw);
00780         ((IVTGLWidget*)glw)->glcontext->MakeCurrent(glw->window);
00781         
00782         w->m_widget = glw;
00783         
00784         m_widgets.push_back(w);
00785         
00786         return (WIDGET_HANDLE)w;
00787 #else
00788         printf("WARNING: CGTKMainWindow::AddGLWidget has been called, but USE_OPENGL = 0 in Makefile.base\n");
00789         return (WIDGET_HANDLE)NULL;
00790 
00791 #endif
00792 }
00793 
00794 
00795 // access to widget attributes
00796 bool CGTKMainWindow::GetText(WIDGET_HANDLE widget, char *text, int len)
00797 {
00798         CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
00799         
00800         if (!w)
00801                 return false;
00802                 
00803         if (w->m_type == eLabel)
00804         {
00805                 const char *ptr = gtk_label_get_text(GTK_LABEL(w->m_widget));
00806                 strncpy(text, ptr, len);
00807                 
00808                 return true;
00809         }
00810         else if (w->m_type == eButton || w->m_type == eCheckBox)
00811         {
00812                 const char *ptr = gtk_button_get_label(GTK_BUTTON(w->m_widget));
00813                 strncpy(text, ptr, len);
00814                 
00815                 return true;
00816         }
00817         else if (w->m_type == eTextEdit)
00818         {
00819                 const char *ptr = gtk_entry_get_text(GTK_ENTRY(w->m_widget));
00820                 strncpy(text, ptr, len);
00821                 
00822                 return true;
00823         }
00824         
00825         return false;
00826 }
00827 bool CGTKMainWindow::SetText(WIDGET_HANDLE widget, const char *text)
00828 {
00829         CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
00830         
00831         if (!w)
00832                 return false;
00833                 
00834         if (w->m_type == eLabel)
00835         {
00836                 gtk_label_set_text(GTK_LABEL(w->m_widget), text);
00837                 
00838                 return true;
00839         }
00840         else if (w->m_type == eButton || w->m_type == eCheckBox)
00841         {
00842                 gtk_button_set_label(GTK_BUTTON(w->m_widget), text);
00843                 
00844                 return true;
00845         }
00846         else if (w->m_type == eTextEdit)
00847         {
00848                 gtk_entry_set_text(GTK_ENTRY(w->m_widget), text);
00849                 
00850                 return true;
00851         }
00852         
00853         return false;
00854 }
00855 
00856 bool CGTKMainWindow::SetImage(WIDGET_HANDLE widget, const CByteImage *pImage)
00857 {
00858         CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
00859         
00860         if (!w)
00861                 return false;
00862                 
00863         if (w->m_type == eImage)
00864         {
00865                 CByteImage *bi = ((IVTImage*)w->m_widget)->image;
00866                 
00867                 if (bi->width != pImage->width || bi->height != pImage->height)
00868                 {
00869                         printf("error: SetImage: image size does not match size of image widget\n");
00870                         return false;
00871                 }
00872                 
00873                 if (bi->type != pImage->type)
00874                 {
00875                         ImageProcessor::ConvertImage(pImage, bi);
00876                 }
00877                 else
00878                 {
00879                         ImageProcessor::CopyImage(pImage, bi);
00880                 }
00881                 
00882                 // init redraw
00883                 gtk_widget_queue_draw_area(w->m_widget, 0, 0, bi->width, bi->height);
00884 
00885         }
00886         
00887         return false;
00888 }
00889 
00890 bool CGTKMainWindow::GetValue(WIDGET_HANDLE widget, int &value)
00891 {
00892         CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
00893         
00894         if (!w)
00895                 return false;
00896                 
00897         if (w->m_type == eCheckBox)
00898         {
00899                 value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->m_widget));
00900                 return true;
00901         }
00902         else if (w->m_type == eSlider)
00903         {
00904                 value = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(w->m_obj));
00905                 return true;
00906         }
00907         else if (w->m_type == eComboBox)
00908         {
00909                 value = gtk_combo_box_get_active(GTK_COMBO_BOX(w->m_widget));
00910                 return true;
00911         }
00912         
00913         return false;
00914 }
00915 
00916 bool CGTKMainWindow::SetValue(WIDGET_HANDLE widget, int value)
00917 {
00918         CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
00919         
00920         if (!w)
00921                 return false;
00922                 
00923         if (w->m_type == eCheckBox)
00924         {
00925                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->m_widget), value);
00926                 return true;
00927         }
00928         else if (w->m_type == eSlider)
00929         {
00930                 gtk_adjustment_set_value(GTK_ADJUSTMENT(w->m_obj), (double)value);
00931                 return true;
00932         }
00933         else if (w->m_type == eComboBox)
00934         {
00935                 gtk_combo_box_set_active(GTK_COMBO_BOX(w->m_widget), value);
00936                 return true;
00937         }
00938         
00939         return false;
00940 }
00941 
00942 bool CGTKMainWindow::SwapBuffersGLWidget(WIDGET_HANDLE widget)
00943 {
00944 #ifdef USE_OPENGL
00945         CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
00946         
00947         if (!w)
00948                 return false;
00949                 
00950         if (w->m_type == eGLWidget)
00951         {
00952                 IVTGLWidget* glw = (IVTGLWidget*)w->m_widget;
00953                 
00954                 if (glw->glcontext)
00955                         glw->glcontext->SwapBuffers(GTK_WIDGET(glw)->window);
00956                 
00957                 return true;
00958         }
00959 #endif
00960         
00961         return false;
00962 }
00963 
00964 bool CGTKMainWindow::MakeCurrentGLWidget(WIDGET_HANDLE widget)
00965 {
00966 #ifdef USE_OPENGL
00967         CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
00968         
00969         if (!w)
00970                 return false;
00971                 
00972         if (w->m_type == eGLWidget)
00973         {
00974                 IVTGLWidget* glw = (IVTGLWidget*)w->m_widget;
00975                 
00976                 if (glw->glcontext)
00977                         glw->glcontext->MakeCurrent(GTK_WIDGET(glw)->window);
00978                 
00979                 return true;
00980         }
00981 #endif
00982         
00983         return false;
00984 }
00985 
00986 
00987 
00988 // window control
00989 void CGTKMainWindow::Show(WIDGET_HANDLE widget)
00990 {
00991         if (widget == NULL)
00992         {
00993                 gtk_widget_show(m_main_window);
00994         }
00995         else
00996         {
00997                 CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
00998                 gtk_widget_show(w->m_widget);
00999         }
01000 }
01001 void CGTKMainWindow::Hide(WIDGET_HANDLE widget)
01002 {
01003         if (widget == NULL)
01004         {
01005                 gtk_widget_hide(m_main_window);
01006         }
01007         else
01008         {
01009                 CGTKMainWindowWidget *w = (CGTKMainWindowWidget*)widget;
01010                 gtk_widget_hide(w->m_widget);
01011         }
01012 }
01013 
01014 
01015 int CGTKMainWindow::GetModifierKeyState()
01016 {
01017         int state = 0;
01018         
01019         if (last_modifier_key_state & GDK_SHIFT_MASK) state |= IVT_SHIFT_KEY;
01020         if (last_modifier_key_state & GDK_CONTROL_MASK) state |= IVT_CONTROL_KEY;
01021         if (last_modifier_key_state & GDK_MOD1_MASK) state |= IVT_ALT_KEY;
01022         
01023         return state;
01024 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57