<commit_before>// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.

#include "appshell/browser/root_window_gtk.h"

#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include <X11/Xlib.h>
#undef Success     // Definition conflicts with cef_message_router.h
#undef RootWindow  // Definition conflicts with root_window.h

#include "include/base/cef_bind.h"
#include "include/cef_app.h"
#include "appshell/browser/browser_window_std_gtk.h"
#include "appshell/browser/main_context.h"
#include "appshell/browser/main_message_loop.h"
#include "appshell/browser/resource.h"
#include "appshell/browser/temp_window.h"
#include "appshell/browser/window_test.h"
#include "appshell/common/client_switches.h"

// Brackets specific changes.
#include "appshell/native_menu_model.h"
#include "appshell/command_callbacks.h"
#include "appshell/appshell_helpers.h"

#define DEFAULT_WINDOW_WIDTH    800
#define DEFAULT_WINDOW_HEIGHT  600
// End of Brackets specific changes.

namespace client {

namespace {

const char kMenuIdKey[] = "menu_id";

// Brackets specific changes.
gboolean IsWindowMaximized(GtkWindow* window) {

  if (window) {
    GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
    if (gdk_window) {
      gint state = gdk_window_get_state(gdk_window);
      return (state & GDK_WINDOW_STATE_MAXIMIZED) ? TRUE : FALSE;
    }
  } else {
    return FALSE;
  }

}

void MinimizeWindow(GtkWindow* window) {

  if (window) {
    // Unmaximize the window before minimizing so restore behaves correctly.
    if (IsWindowMaximized(window))
      gtk_window_unmaximize(window);

    gtk_window_iconify(window);
  }
}

void MaximizeWindow(GtkWindow* window) {
  if (window) {
    gtk_window_maximize(window);
  }
}

void SaveWindowState(GtkWindow* window) {

  if (!window)
    return;

  gint left   = 1;
  gint top    = 1;
  gint width  = DEFAULT_WINDOW_WIDTH;
  gint height = DEFAULT_WINDOW_HEIGHT;

  GKeyFile* key_file = g_key_file_new();
  GError* err = NULL;
  gchar* filePath = NULL;

  if (key_file) {
    filePath = g_strdup_printf("%s/%s", appshell::AppGetSupportDirectory().ToString().c_str(), "window.ini");
    gboolean maximized  = IsWindowMaximized(window);

    // If window is not maximized, save current size and position
    
    if (!maximized) {
      gtk_window_get_position(window, &left, &top);
      gtk_window_get_size(window, &width, &height);
    } else if (g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, &err)) {

      // If maximized, load size and position from file
      // to preserve last saved values
      left = g_key_file_get_integer(key_file, "position", "left", &err);
      if (!err)
        top = g_key_file_get_integer(key_file, "position", "top", &err);

      if (!err)
        width = g_key_file_get_integer(key_file, "size", "width", &err);

      if (!err)
        height = g_key_file_get_integer(key_file, "size", "height", &err);
      
      // If any value can not be read, restore defaults
      if (err) {
        left   = 1;
        top    = 1;
        width  = DEFAULT_WINDOW_WIDTH;
        height = DEFAULT_WINDOW_HEIGHT;
        g_error_free(err);
      }
    }

    // The values would always be written to file.
    g_key_file_set_integer(key_file, "position", "left", left);
    g_key_file_set_integer(key_file, "position", "top", top);
    g_key_file_set_integer(key_file, "size", "width", width - 1);   // DelayedResize() 1 pixel compensation
    g_key_file_set_integer(key_file, "size", "height", height - 1); // DelayedResize() 1 pixel compensation
    g_key_file_set_boolean(key_file, "state", "maximized", maximized);
  
    err = NULL;
    g_key_file_save_to_file(key_file, filePath, &err);

    if (err) {
      fprintf(stderr, "Err -> SaveWindowState(): could not write to `window.ini`. Error Description: %s\n", err->message);
    }
  } else {
    fprintf(stderr, "Err -> SaveWindowState(): could not write to `window.ini`\n");
  }
}

void LoadWindowState(GtkWindow* window) {

  if (!window) {
    return;
  }

  // Default values for the window state.
  gint left   = 1;
  gint top    = 1;
  gint width  = DEFAULT_WINDOW_WIDTH;
  gint height = DEFAULT_WINDOW_HEIGHT;

  // Try to center the window.
  GdkScreen* screen = gdk_screen_get_default();
  if (screen) {
    left = (gdk_screen_get_width(screen)  - DEFAULT_WINDOW_WIDTH) / 2 ;
    top  = (gdk_screen_get_height(screen) - DEFAULT_WINDOW_HEIGHT) / 2 ;
  }

  gboolean maximized = false;

  GKeyFile* key_file = g_key_file_new();
  bool any_error = false;
  GError* err = NULL;
  gchar* filePath = g_strdup_printf("%s/%s", appshell::AppGetSupportDirectory().ToString().c_str(), "window.ini");

  if (key_file && g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, &err)) {

    left = g_key_file_get_integer(key_file, "position", "left", &err);
    if (!err)
      top = g_key_file_get_integer(key_file, "position", "top", &err);

    if (!err)
      width = g_key_file_get_integer(key_file, "size", "width", &err);

    if (!err)
      height = g_key_file_get_integer(key_file, "size", "height", &err);

    if (!err)
      maximized = g_key_file_get_boolean(key_file, "state", "maximized", &err);

    // If any value can not be readed, set defaults again
    if (err) {
      left      = 1;
      top       = 1;
      width     = DEFAULT_WINDOW_WIDTH;
      height    = DEFAULT_WINDOW_HEIGHT;
      maximized = TRUE;
    }

    gtk_window_move(GTK_WINDOW(window), left, top);
    gtk_window_set_default_size(GTK_WINDOW(window), width, height);

    if (maximized)
      MaximizeWindow(window);

  } else {
    any_error = true;
  }

  if (err || any_error) {

    // The failure could be because the file may not have been present,
    // or the file read itself failed.
    // In either of the cases default to maximizing the window.
    MaximizeWindow(window);

    if (err) {
      if (err->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND){
        fprintf(stderr, "LoadWindowState(): Could not read %s. Error Description:%s.\n", filePath, err->message);
      }
      g_error_free(err);
    } else {
      fprintf(stderr, "LoadWindowState(): Could not read %s.\n", filePath);
    }
  }
}
// End of Brackets specific changes.

}  // namespace

RootWindowGtk::RootWindowGtk()
    : delegate_(NULL),
      with_controls_(false),
      with_osr_(false),
      is_popup_(false),
      initialized_(false),
      window_(NULL),
      back_button_(NULL),<commit_msg>Center window on first save. Avoid the size of window too small when demaximizing (in the first execution).<commit_after>// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.

#include "appshell/browser/root_window_gtk.h"

#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include <X11/Xlib.h>
#undef Success     // Definition conflicts with cef_message_router.h
#undef RootWindow  // Definition conflicts with root_window.h

#include "include/base/cef_bind.h"
#include "include/cef_app.h"
#include "appshell/browser/browser_window_std_gtk.h"
#include "appshell/browser/main_context.h"
#include "appshell/browser/main_message_loop.h"
#include "appshell/browser/resource.h"
#include "appshell/browser/temp_window.h"
#include "appshell/browser/window_test.h"
#include "appshell/common/client_switches.h"

// Brackets specific changes.
#include "appshell/native_menu_model.h"
#include "appshell/command_callbacks.h"
#include "appshell/appshell_helpers.h"

#define DEFAULT_WINDOW_WIDTH    800
#define DEFAULT_WINDOW_HEIGHT  600
// End of Brackets specific changes.

namespace client {

namespace {

const char kMenuIdKey[] = "menu_id";

// Brackets specific changes.
gboolean IsWindowMaximized(GtkWindow* window) {

  if (window) {
    GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
    if (gdk_window) {
      gint state = gdk_window_get_state(gdk_window);
      return (state & GDK_WINDOW_STATE_MAXIMIZED) ? TRUE : FALSE;
    }
  } else {
    return FALSE;
  }

}

void MinimizeWindow(GtkWindow* window) {

  if (window) {
    // Unmaximize the window before minimizing so restore behaves correctly.
    if (IsWindowMaximized(window))
      gtk_window_unmaximize(window);

    gtk_window_iconify(window);
  }
}

void MaximizeWindow(GtkWindow* window) {
  if (window) {
    gtk_window_maximize(window);
  }
}

void SaveWindowState(GtkWindow* window) {

  if (!window)
    return;

  gint left   = 1;
  gint top    = 1;
  gint width  = DEFAULT_WINDOW_WIDTH;
  gint height = DEFAULT_WINDOW_HEIGHT;

  // Try to center the window.
  GdkScreen* screen = gdk_screen_get_default();
  if (screen) {
    left = (gdk_screen_get_width(screen)  - DEFAULT_WINDOW_WIDTH) / 2 ;
    top  = (gdk_screen_get_height(screen) - DEFAULT_WINDOW_HEIGHT) / 2 ;
  }

  GKeyFile* key_file = g_key_file_new();
  GError* err = NULL;
  gchar* filePath = NULL;

  if (key_file) {
    filePath = g_strdup_printf("%s/%s", appshell::AppGetSupportDirectory().ToString().c_str(), "window.ini");
    gboolean maximized  = IsWindowMaximized(window);

    // If window is not maximized, save current size and position
    
    if (!maximized) {
      gtk_window_get_position(window, &left, &top);
      gtk_window_get_size(window, &width, &height);
    } else if (g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, &err)) {

      // If maximized, load size and position from file
      // to preserve last saved values
      left = g_key_file_get_integer(key_file, "position", "left", &err);
      if (!err)
        top = g_key_file_get_integer(key_file, "position", "top", &err);

      if (!err)
        width = g_key_file_get_integer(key_file, "size", "width", &err);

      if (!err)
        height = g_key_file_get_integer(key_file, "size", "height", &err);
      
      // If any value can not be read, restore defaults
      if (err) {
        left   = 1;
        top    = 1;
        width  = DEFAULT_WINDOW_WIDTH;
        height = DEFAULT_WINDOW_HEIGHT;
        g_error_free(err);
      }
    }

    // The values would always be written to file.
    g_key_file_set_integer(key_file, "position", "left", left);
    g_key_file_set_integer(key_file, "position", "top", top);
    g_key_file_set_integer(key_file, "size", "width", width - 1);   // DelayedResize() 1 pixel compensation
    g_key_file_set_integer(key_file, "size", "height", height - 1); // DelayedResize() 1 pixel compensation
    g_key_file_set_boolean(key_file, "state", "maximized", maximized);
  
    err = NULL;
    g_key_file_save_to_file(key_file, filePath, &err);

    if (err) {
      fprintf(stderr, "Err -> SaveWindowState(): could not write to `window.ini`. Error Description: %s\n", err->message);
    }
  } else {
    fprintf(stderr, "Err -> SaveWindowState(): could not write to `window.ini`\n");
  }
}

void LoadWindowState(GtkWindow* window) {

  if (!window) {
    return;
  }

  // Default values for the window state.
  gint left   = 1;
  gint top    = 1;
  gint width  = DEFAULT_WINDOW_WIDTH;
  gint height = DEFAULT_WINDOW_HEIGHT;

  // Try to center the window.
  GdkScreen* screen = gdk_screen_get_default();
  if (screen) {
    left = (gdk_screen_get_width(screen)  - DEFAULT_WINDOW_WIDTH) / 2 ;
    top  = (gdk_screen_get_height(screen) - DEFAULT_WINDOW_HEIGHT) / 2 ;
  }

  gboolean maximized = false;

  GKeyFile* key_file = g_key_file_new();
  bool any_error = false;
  GError* err = NULL;
  gchar* filePath = g_strdup_printf("%s/%s", appshell::AppGetSupportDirectory().ToString().c_str(), "window.ini");

  if (key_file && g_key_file_load_from_file(key_file, filePath, G_KEY_FILE_NONE, &err)) {

    left = g_key_file_get_integer(key_file, "position", "left", &err);
    if (!err)
      top = g_key_file_get_integer(key_file, "position", "top", &err);

    if (!err)
      width = g_key_file_get_integer(key_file, "size", "width", &err);

    if (!err)
      height = g_key_file_get_integer(key_file, "size", "height", &err);

    if (!err)
      maximized = g_key_file_get_boolean(key_file, "state", "maximized", &err);

    // If any value can not be readed, set defaults again
    if (err) {
      left      = 1;
      top       = 1;
      width     = DEFAULT_WINDOW_WIDTH;
      height    = DEFAULT_WINDOW_HEIGHT;
      maximized = TRUE;
    }
  } else {
    any_error = true;
  }

  gtk_window_move(GTK_WINDOW(window), left, top);
  gtk_window_set_default_size(GTK_WINDOW(window), width, height);

  if (maximized)
    MaximizeWindow(window);

  if (err || any_error) {

    // The failure could be because the file may not have been present,
    // or the file read itself failed.
    // In either of the cases default to maximizing the window.
    MaximizeWindow(window);

    if (err) {
      if (err->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND){
        fprintf(stderr, "LoadWindowState(): Could not read %s. Error Description:%s.\n", filePath, err->message);
      }
      g_error_free(err);
    } else {
      fprintf(stderr, "LoadWindowState(): Could not read %s.\n", filePath);
    }
  }
}
// End of Brackets specific changes.

}  // namespace

RootWindowGtk::RootWindowGtk()
    : delegate_(NULL),
      with_controls_(false),
      with_osr_(false),
      is_popup_(false),
      initialized_(false),
      window_(NULL),
      back_button_(NULL),
{'fraction_non_alphanumeric': 0.1204944891271969, 'fraction_numerical': 0.004021447721179625, 'mean_word_length': 3.468885191347754, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '6182915', 'n_tokens_mistral': 4769, 'n_tokens_neox': 4634, 'n_words': 1096}