window.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_GTKUTIL_WINDOW_H)
00023 #define INCLUDED_GTKUTIL_WINDOW_H
00024 
00025 #include <gtk/gtkwindow.h>
00026 #include <string>
00027 
00028 #include "debugging/debugging.h"
00029 #include "generic/callback.h"
00030 #include "widget.h"
00031 
00032 inline gboolean window_focus_in_clear_focus_widget (GtkWidget* widget, GdkEventKey* event, gpointer data)
00033 {
00034     gtk_window_set_focus(GTK_WINDOW(widget), NULL);
00035     return FALSE;
00036 }
00037 
00038 inline guint window_connect_focus_in_clear_focus_widget (GtkWindow* window)
00039 {
00040     return g_signal_connect(G_OBJECT(window), "focus_in_event", G_CALLBACK(window_focus_in_clear_focus_widget), NULL);
00041 }
00042 
00043 unsigned int connect_floating (GtkWindow* main_window, GtkWindow* floating);
00044 GtkWindow* create_floating_window (const std::string& title, GtkWindow* parent);
00045 void destroy_floating_window (GtkWindow* window);
00046 
00047 GtkWindow* create_persistent_floating_window (const std::string& title, GtkWindow* main_window);
00048 gboolean persistent_floating_window_delete (GtkWindow* floating, GdkEvent *event, GtkWindow* main_window);
00049 
00050 void window_remove_minmax (GtkWindow* window);
00051 
00052 typedef struct _GtkScrolledWindow GtkScrolledWindow;
00053 GtkScrolledWindow* create_scrolled_window (GtkPolicyType hscrollbar_policy, GtkPolicyType vscrollbar_policy,
00054         int border = 0);
00055 
00056 struct WindowPosition
00057 {
00058         int x, y, w, h;
00059 
00060         WindowPosition ()
00061         {
00062         }
00063         WindowPosition (int _x, int _y, int _w, int _h) :
00064             x(_x), y(_y), w(_w), h(_h)
00065         {
00066         }
00067 };
00068 
00069 static const WindowPosition c_default_window_pos(50, 25, 800, 600);
00070 
00071 inline void window_get_position (GtkWindow* window, WindowPosition& position)
00072 {
00073     ASSERT_MESSAGE(window != 0, "error saving window position");
00074 
00075     gtk_window_get_position(window, &position.x, &position.y);
00076     gtk_window_get_size(window, &position.w, &position.h);
00077 }
00078 
00079 inline void window_set_position (GtkWindow* window, const WindowPosition& position)
00080 {
00081     gtk_window_set_gravity(window, GDK_GRAVITY_STATIC);
00082 
00083     GdkScreen* screen = gdk_screen_get_default();
00084     if (position.x < 0 || position.y < 0 || position.x > gdk_screen_get_width(screen) || position.y
00085             > gdk_screen_get_height(screen)) {
00086         gtk_window_set_position(window, GTK_WIN_POS_CENTER_ON_PARENT);
00087     } else {
00088         gtk_window_move(window, position.x, position.y);
00089     }
00090 
00091     gtk_window_set_default_size(window, position.w, position.h);
00092 }
00093 
00094 inline void WindowPosition_Parse (WindowPosition& position, const char* value)
00095 {
00096     if (sscanf(value, "%d %d %d %d", &position.x, &position.y, &position.w, &position.h) != 4) {
00097         position = WindowPosition(c_default_window_pos); // ensure sane default value for window position
00098     }
00099 }
00100 typedef ReferenceCaller1<WindowPosition, const char*, WindowPosition_Parse> WindowPositionImportStringCaller;
00101 
00102 inline void WindowPosition_Write (const WindowPosition& position, const StringImportCallback& importCallback)
00103 {
00104     char buffer[64];
00105     sprintf(buffer, "%d %d %d %d", position.x, position.y, position.w, position.h);
00106     importCallback(buffer);
00107 }
00108 typedef ConstReferenceCaller1<WindowPosition, const StringImportCallback&, WindowPosition_Write>
00109         WindowPositionExportStringCaller;
00110 
00111 class WindowPositionTracker
00112 {
00113         WindowPosition m_position;
00114 
00115         static gboolean configure (GtkWidget* widget, GdkEventConfigure *event, WindowPositionTracker* self)
00116         {
00117             self->m_position = WindowPosition(event->x, event->y, event->width, event->height);
00118             return FALSE;
00119         }
00120 
00121     public:
00122         WindowPositionTracker () :
00123             m_position(c_default_window_pos)
00124         {
00125         }
00126 
00127         void sync (GtkWindow* window)
00128         {
00129             window_set_position(window, m_position);
00130         }
00131 
00132         void connect (GtkWindow* window)
00133         {
00134             sync(window);
00135             g_signal_connect(G_OBJECT(window), "configure_event", G_CALLBACK(configure), this);
00136         }
00137 
00138         const WindowPosition& getPosition () const
00139         {
00140             return m_position;
00141         }
00142 
00143         //hack
00144         void setPosition (const WindowPosition& position)
00145         {
00146             m_position = position;
00147         }
00148 };
00149 
00150 inline void WindowPositionTracker_importString (WindowPositionTracker& self, const char* value)
00151 {
00152     WindowPosition position;
00153     WindowPosition_Parse(position, value);
00154     self.setPosition(position);
00155 }
00156 typedef ReferenceCaller1<WindowPositionTracker, const char*, WindowPositionTracker_importString>
00157         WindowPositionTrackerImportStringCaller;
00158 
00159 inline void WindowPositionTracker_exportString (const WindowPositionTracker& self, const StringImportCallback& importer)
00160 {
00161     WindowPosition_Write(self.getPosition(), importer);
00162 }
00163 typedef ConstReferenceCaller1<WindowPositionTracker, const StringImportCallback&, WindowPositionTracker_exportString>
00164         WindowPositionTrackerExportStringCaller;
00165 
00166 #endif

Generated by  doxygen 1.6.2