<commit_before>/* GG is a GUI for SDL and OpenGL.
   Copyright (C) 2003 <NAME>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1
   of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
    
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA

   If you do not wish to comply with the terms of the LGPL please
   contact the author as other terms are available for a fee.
    
   <NAME>
   <EMAIL> */

#include <GG/GUI.h>

#include <GG/BrowseInfoWnd.h>
#include <GG/Control.h>
#include <GG/EventPump.h>
#include <GG/Layout.h>
#include <GG/PluginInterface.h>
#include <GG/StyleFactory.h>
#include <GG/Timer.h>
#include <GG/WndEvent.h>
#include <GG/ZList.h>

#include <boost/format.hpp>

#include <cassert>
#include <fstream>
#include <list>


using namespace GG;

namespace {
    /* returns the storage value of key_mods that should be used with keyboard accelerators the accelerators don't care
       which side of the keyboard you use for CTRL, SHIFT, etc., and whether or not the numlock or capslock are
       engaged.*/
    Uint32 MassagedAccelKeyMods(Uint32 key_mods)
    {
        key_mods &= ~(GGKMOD_NUM | GGKMOD_CAPS);
        if (key_mods & GGKMOD_CTRL)
            key_mods |= GGKMOD_CTRL;
        if (key_mods & GGKMOD_SHIFT)
            key_mods |= GGKMOD_SHIFT;
        if (key_mods & GGKMOD_ALT)
            key_mods |= GGKMOD_ALT;
        if (key_mods & GGKMOD_META)
            key_mods |= GGKMOD_META;
        return key_mods;
    }
}

// implementation data types
struct GG::GUIImplData
{
    GUIImplData() :
        focus_wnd(0),
        mouse_pos(0,0),
        mouse_rel(0,0),
        key_mods(0),
        button_down_repeat_delay(250),
        button_down_repeat_interval(66),
        last_button_down_repeat_time(0),
        double_click_interval(500),
        min_drag_time(250),
        min_drag_distance(5),
        prev_button_press_time(-1),
        prev_wnd_under_cursor(0),
        prev_wnd_under_cursor_time(-1),
        curr_wnd_under_cursor(0),
        drag_wnds(),
        curr_drag_wnd_dragged(false),
        curr_drag_drop_here_wnd(0),
        wnd_region(WR_NONE),
        browse_target(0),
        drag_drop_originating_wnd(0),
        delta_t(0),
        FPS(-1.0),
        calc_FPS(false),
        max_FPS(0.0),
        double_click_wnd(0),
        double_click_start_time(-1),
        double_click_time(-1),
        style_factory(new StyleFactory()),
        save_wnd_fn(0),
        load_wnd_fn(0)
    {
        button_state[0] = button_state[1] = button_state[2] = false;
        drag_wnds[0] = drag_wnds[1] = drag_wnds[2] = 0;
    }

    std::string  app_name;              // the user-defined name of the apllication

    ZList        zlist;                 // object that keeps the GUI windows in the correct depth ordering
    Wnd*         focus_wnd;             // GUI window that currently has the input focus (this is the base level focus window, used when no modal windows are active)
    std::list<std::pair<Wnd*, Wnd*> >
                 modal_wnds;            // modal GUI windows, and the window with focus for that modality (only the one in back is active, simulating a stack but allowing traversal of the list)

    bool         button_state[3];       // the up/down states of the three buttons on the mouse are kept here
    Pt           mouse_pos;             // absolute position of mouse, based on last MOUSEMOVE event
    Pt           mouse_rel;             // relative position of mouse, based on last MOUSEMOVE event
    Uint32       key_mods;              // currently-depressed modifier keys, based on last KEYPRESS event

    int          button_down_repeat_delay;     // see note above GUI class definition
    int          button_down_repeat_interval;
    int          last_button_down_repeat_time; // last time of a simulated button-down message

    int          double_click_interval; // the maximum interval allowed between clicks that is still considered a double-click, in ms
    int          min_drag_time;         // the minimum amount of time that a drag must be in progress before it is considered a drag, in ms
    int          min_drag_distance;     // the minimum distance that a drag must cover before it is considered a drag

    int          prev_button_press_time;// the time of the most recent mouse button press
    Pt           prev_button_press_pos; // the location of the most recent mouse button press
    Wnd*         prev_wnd_under_cursor; // GUI window most recently under the input cursor; may be 0
    int          prev_wnd_under_cursor_time; // the time at which prev_wnd_under_cursor was initially set to its current value
    Wnd*         curr_wnd_under_cursor; // GUI window currently under the input cursor; may be 0
    Wnd*         drag_wnds[3];          // GUI window currently being clicked or dragged by each mouse button
    Pt           prev_wnd_drag_position;// the upper-left corner of the dragged window when the last *Drag message was generated
    Pt           wnd_drag_offset;       // the offset from the upper left corner of the dragged window to the cursor for the current drag
    bool         curr_drag_wnd_dragged; // true iff the currently-pressed window (drag_wnd[N]) has actually been dragged some distance (in which case releasing the mouse button is not a click)
    Wnd*         curr_drag_drop_here_wnd;// the Wnd that most recently received a DragDropEnter or DragDropHere message (0 if DragDropLeave was sent as well, or if none)
    Pt           wnd_resize_offset;     // offset from the cursor of either the upper-left or lower-right corner of the GUI window currently being resized
    WndRegion    wnd_region;            // window region currently being dragged or clicked; for non-frame windows, this will always be WR_NONE

    boost::shared_ptr<BrowseInfoWnd>
                 browse_info_wnd;       // the current browse info window, if any
    int          browse_info_mode;      // the current browse info mode (only valid if browse_info_wnd is non-null)
    Wnd*         browse_target;         // the current browse info target

    Wnd*         drag_drop_originating_wnd; // the window that originally owned the Wnds in drag_drop_wnds
    std::map<Wnd*, Pt>
                 drag_drop_wnds;        // the Wnds (and their offsets) that are being dragged and dropped between Wnds

    std::set<std::pair<Key, Uint32> >
                 accelerators;          // the keyboard accelerators

    std::map<std::pair<Key, Uint32>, boost::shared_ptr<GUI::AcceleratorSignalType> >
                 accelerator_sigs;      // the signals emitted by the keyboard accelerators

    int          delta_t;               // the number of ms since the last frame
    double       FPS;                   // the most recent calculation of the frames per second rendering speed (-1.0 if calcs are disabled)
    bool         calc_FPS;              // true iff FPS calcs are to be done
    double       max_FPS;               // the maximum allowed frames per second rendering speed

    Wnd*         double_click_wnd;      // GUI window most recently clicked
    int          double_click_button;   // the index of the mouse button used in the last click
    int          double_click_start_time;// the time from which we started measuring double_click_time, in ms
    int          double_click_time;     // time elapsed since last click, in ms

    boost::shared_ptr<StyleFactory> style_factory;

    std::set<Timer*>  timers;

    GUI::SaveWndFn    save_wnd_fn;
    GUI::LoadWndFn    load_wnd_fn;
};

// static member(s)
GUI*                           GUI::s_gui = 0;
boost::shared_ptr<GUIImplData> GUI::s_impl;

// member functions
GUI::GUI(const std::string& app_name)
{
    assert(!s_gui);
    s_gui = this;
    assert(!s_impl);
    s_impl.reset(new GUIImplData());
    s_impl->app_name = app_name;
}

GUI::~GUI()
{
}

Wnd* GUI::FocusWnd() const
{
    return s_impl->modal_wnds.empty() ? s_impl->focus_wnd : s_impl->modal_wnds.back().second;
}
<commit_msg>Added code to ~GUI() to ensure that Wnd::s_default_browse_info_wnd does not outlive the GUI.<commit_after>/* GG is a GUI for SDL and OpenGL.
   Copyright (C) 2003 <NAME>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1
   of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
    
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA

   If you do not wish to comply with the terms of the LGPL please
   contact the author as other terms are available for a fee.
    
   <NAME>
   <EMAIL> */

#include <GG/GUI.h>

#include <GG/BrowseInfoWnd.h>
#include <GG/Control.h>
#include <GG/EventPump.h>
#include <GG/Layout.h>
#include <GG/PluginInterface.h>
#include <GG/StyleFactory.h>
#include <GG/Timer.h>
#include <GG/WndEvent.h>
#include <GG/ZList.h>

#include <boost/format.hpp>

#include <cassert>
#include <fstream>
#include <list>


using namespace GG;

namespace {
    /* returns the storage value of key_mods that should be used with keyboard accelerators the accelerators don't care
       which side of the keyboard you use for CTRL, SHIFT, etc., and whether or not the numlock or capslock are
       engaged.*/
    Uint32 MassagedAccelKeyMods(Uint32 key_mods)
    {
        key_mods &= ~(GGKMOD_NUM | GGKMOD_CAPS);
        if (key_mods & GGKMOD_CTRL)
            key_mods |= GGKMOD_CTRL;
        if (key_mods & GGKMOD_SHIFT)
            key_mods |= GGKMOD_SHIFT;
        if (key_mods & GGKMOD_ALT)
            key_mods |= GGKMOD_ALT;
        if (key_mods & GGKMOD_META)
            key_mods |= GGKMOD_META;
        return key_mods;
    }
}

// implementation data types
struct GG::GUIImplData
{
    GUIImplData() :
        focus_wnd(0),
        mouse_pos(0,0),
        mouse_rel(0,0),
        key_mods(0),
        button_down_repeat_delay(250),
        button_down_repeat_interval(66),
        last_button_down_repeat_time(0),
        double_click_interval(500),
        min_drag_time(250),
        min_drag_distance(5),
        prev_button_press_time(-1),
        prev_wnd_under_cursor(0),
        prev_wnd_under_cursor_time(-1),
        curr_wnd_under_cursor(0),
        drag_wnds(),
        curr_drag_wnd_dragged(false),
        curr_drag_drop_here_wnd(0),
        wnd_region(WR_NONE),
        browse_target(0),
        drag_drop_originating_wnd(0),
        delta_t(0),
        FPS(-1.0),
        calc_FPS(false),
        max_FPS(0.0),
        double_click_wnd(0),
        double_click_start_time(-1),
        double_click_time(-1),
        style_factory(new StyleFactory()),
        save_wnd_fn(0),
        load_wnd_fn(0)
    {
        button_state[0] = button_state[1] = button_state[2] = false;
        drag_wnds[0] = drag_wnds[1] = drag_wnds[2] = 0;
    }

    std::string  app_name;              // the user-defined name of the apllication

    ZList        zlist;                 // object that keeps the GUI windows in the correct depth ordering
    Wnd*         focus_wnd;             // GUI window that currently has the input focus (this is the base level focus window, used when no modal windows are active)
    std::list<std::pair<Wnd*, Wnd*> >
                 modal_wnds;            // modal GUI windows, and the window with focus for that modality (only the one in back is active, simulating a stack but allowing traversal of the list)

    bool         button_state[3];       // the up/down states of the three buttons on the mouse are kept here
    Pt           mouse_pos;             // absolute position of mouse, based on last MOUSEMOVE event
    Pt           mouse_rel;             // relative position of mouse, based on last MOUSEMOVE event
    Uint32       key_mods;              // currently-depressed modifier keys, based on last KEYPRESS event

    int          button_down_repeat_delay;     // see note above GUI class definition
    int          button_down_repeat_interval;
    int          last_button_down_repeat_time; // last time of a simulated button-down message

    int          double_click_interval; // the maximum interval allowed between clicks that is still considered a double-click, in ms
    int          min_drag_time;         // the minimum amount of time that a drag must be in progress before it is considered a drag, in ms
    int          min_drag_distance;     // the minimum distance that a drag must cover before it is considered a drag

    int          prev_button_press_time;// the time of the most recent mouse button press
    Pt           prev_button_press_pos; // the location of the most recent mouse button press
    Wnd*         prev_wnd_under_cursor; // GUI window most recently under the input cursor; may be 0
    int          prev_wnd_under_cursor_time; // the time at which prev_wnd_under_cursor was initially set to its current value
    Wnd*         curr_wnd_under_cursor; // GUI window currently under the input cursor; may be 0
    Wnd*         drag_wnds[3];          // GUI window currently being clicked or dragged by each mouse button
    Pt           prev_wnd_drag_position;// the upper-left corner of the dragged window when the last *Drag message was generated
    Pt           wnd_drag_offset;       // the offset from the upper left corner of the dragged window to the cursor for the current drag
    bool         curr_drag_wnd_dragged; // true iff the currently-pressed window (drag_wnd[N]) has actually been dragged some distance (in which case releasing the mouse button is not a click)
    Wnd*         curr_drag_drop_here_wnd;// the Wnd that most recently received a DragDropEnter or DragDropHere message (0 if DragDropLeave was sent as well, or if none)
    Pt           wnd_resize_offset;     // offset from the cursor of either the upper-left or lower-right corner of the GUI window currently being resized
    WndRegion    wnd_region;            // window region currently being dragged or clicked; for non-frame windows, this will always be WR_NONE

    boost::shared_ptr<BrowseInfoWnd>
                 browse_info_wnd;       // the current browse info window, if any
    int          browse_info_mode;      // the current browse info mode (only valid if browse_info_wnd is non-null)
    Wnd*         browse_target;         // the current browse info target

    Wnd*         drag_drop_originating_wnd; // the window that originally owned the Wnds in drag_drop_wnds
    std::map<Wnd*, Pt>
                 drag_drop_wnds;        // the Wnds (and their offsets) that are being dragged and dropped between Wnds

    std::set<std::pair<Key, Uint32> >
                 accelerators;          // the keyboard accelerators

    std::map<std::pair<Key, Uint32>, boost::shared_ptr<GUI::AcceleratorSignalType> >
                 accelerator_sigs;      // the signals emitted by the keyboard accelerators

    int          delta_t;               // the number of ms since the last frame
    double       FPS;                   // the most recent calculation of the frames per second rendering speed (-1.0 if calcs are disabled)
    bool         calc_FPS;              // true iff FPS calcs are to be done
    double       max_FPS;               // the maximum allowed frames per second rendering speed

    Wnd*         double_click_wnd;      // GUI window most recently clicked
    int          double_click_button;   // the index of the mouse button used in the last click
    int          double_click_start_time;// the time from which we started measuring double_click_time, in ms
    int          double_click_time;     // time elapsed since last click, in ms

    boost::shared_ptr<StyleFactory> style_factory;

    std::set<Timer*>  timers;

    GUI::SaveWndFn    save_wnd_fn;
    GUI::LoadWndFn    load_wnd_fn;
};

// static member(s)
GUI*                           GUI::s_gui = 0;
boost::shared_ptr<GUIImplData> GUI::s_impl;

// member functions
GUI::GUI(const std::string& app_name)
{
    assert(!s_gui);
    s_gui = this;
    assert(!s_impl);
    s_impl.reset(new GUIImplData());
    s_impl->app_name = app_name;
}

GUI::~GUI()
{ Wnd::s_default_browse_info_wnd.reset(); }

Wnd* GUI::FocusWnd() const
{
    return s_impl->modal_wnds.empty() ? s_impl->focus_wnd : s_impl->modal_wnds.back().second;
}

{'fraction_non_alphanumeric': 0.07101827676240209, 'fraction_numerical': 0.009399477806788513, 'mean_word_length': 2.4721998388396456, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '703868', 'n_tokens_mistral': 5212, 'n_tokens_neox': 4996, 'n_words': 1787}