1 #pragma once 2 3 #include <gtk/gtkwindow.h> 4 #include "math/Vector2.h" 5 #include <string> 6 7 /* greebo: A WindowPosition object keeps track of the window's size and position. 8 * 9 * Use the connect() method to connect a GtkWindow to this object. 10 * 11 * Use the loadFromNode() and saveToNode() methods to save the internal 12 * size info into the given xml::Node 13 * 14 * This is used by the XYWnd classes to save/restore the window state upon restart. 15 */ 16 namespace gtkutil { 17 18 namespace { 19 typedef BasicVector2<int> PositionVector; 20 typedef BasicVector2<int> SizeVector; 21 } 22 23 class WindowPosition { 24 // The size and position of this object 25 PositionVector _position; 26 SizeVector _size; 27 28 // The connected window 29 GtkWindow* _window; 30 31 public: 32 WindowPosition(); 33 34 // Connect the passed window to this object 35 void connect(GtkWindow* window); 36 37 const PositionVector& getPosition() const; 38 const SizeVector& getSize() const; 39 40 void setPosition(int x, int y); 41 void setSize(int width, int height); 42 43 // Loads/saves the window position to the given Registry path 44 void saveToPath(const std::string& path); 45 void loadFromPath(const std::string& path); 46 47 // Applies the internally stored size/position info to the GtkWindow 48 // The algorithm was adapted from original GtkRadiant code (window.h) 49 void applyPosition(); 50 51 // Reads the position from the GtkWindow 52 void readPosition(); 53 54 /** 55 * Fits the current position/dimension to the screen of the connected 56 * window. This object has to be connected to a GtkWindow before 57 * the method can function properly. 58 * 59 * @param xfraction,yfraction: the fraction of the screen which the window 60 * should occupy. (e.g. Pass 0.5/0.66 to let the window half of the 61 * monitor width and two thirds of the monitor height. 62 * 63 * Note: applyPosition() has to be called for the changes to take effect. 64 */ 65 void fitToScreen(float xfraction = 1, float yfraction = 1); 66 67 // Adjusts the position/dimensions to fit on the given screen (GdkRectangle) 68 void fitToScreen(GdkRectangle screen, float xfraction = 1, float yfraction = 1); 69 70 private: 71 72 // The static GTK callback that gets invoked on window size/position changes 73 static gboolean onConfigure(GtkWidget* widget, GdkEventConfigure *event, WindowPosition* self); 74 75 }; // class WindowPosition 76 77 } // namespace gtkutil 78