<commit_before>#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "holidays.h"
#include "calendar.h"

struct Configuration {
  gboolean attach_to_tray;
  gchar *holiday_file;
  gchar *config_file;
} config;

CalendarWidget *calendar = NULL;
HolidayContext *holiday_context = NULL;
GtkStatusIcon *status_icon = NULL;
gboolean tray_window_visible = FALSE;

void main_tray_position_window(GtkWidget *window)
{
  GdkRectangle area;
  GtkOrientation orientation;
  GdkScreen *screen;
  gint x;
  gint y;
  gint monitor_num;
  GdkRectangle monitor;
  GtkRequisition win_req;

  g_return_if_fail(GTK_IS_WINDOW(window));

  if (!status_icon) {
    return;
  }

  if (!gtk_status_icon_get_geometry(status_icon,
                                    &screen,
                                    &area,
                                    &orientation)) {
    g_warning("Unable to determine geometry of status icon\n");
    return;
  }

  gtk_window_set_screen(GTK_WINDOW(window), screen);

  monitor_num = gdk_screen_get_monitor_at_point(screen, area.x, area.y);
  gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
  
  gtk_container_foreach(GTK_CONTAINER(window), (GtkCallback)gtk_widget_show_all, NULL);
  gtk_widget_size_request(window, &win_req);

  if (orientation == GTK_ORIENTATION_VERTICAL) {
    if (area.x + area.width + win_req.width <= monitor.x + monitor.width) {
      x = area.x + area.width;
    }
    else {
      x = area.x - win_req.width;
    }
    if (area.y + win_req.height <= monitor.y + monitor.height) {
      y = area.y;
    }
    else {
      y = monitor.y + monitor.height - win_req.height;
    }
  }
  else {
    if (area.y + area.height + win_req.height <= monitor.y + monitor.height) {
      y = area.y + area.height;
    }
    else {
      y = area.y - win_req.height;
    }
    if (area.x + win_req.width <= monitor.x + monitor.width) {
      x = area.x;
    }
    else {
      x = monitor.x + monitor.width - win_req.width;
    }
  }

  gtk_window_move(GTK_WINDOW(window), x, y);
}

void main_tray_icon_set_visible(GtkWidget *tray_widget,
                                gboolean visible)
{
  tray_window_visible = visible;

  if (visible) {
    main_tray_position_window(tray_widget);
    gtk_widget_show_all(tray_widget);
  }
  else {
    gtk_widget_hide(tray_widget);
  }
}

void main_tray_icon_toggle_display(GtkWidget *tray_widget)
{
  main_tray_icon_set_visible(tray_widget, !tray_window_visible);
}

void main_tray_icon_clicked(GtkStatusIcon *icon,
                            GtkWidget *tray_widget)
{
  main_tray_icon_toggle_display(tray_widget);
}

GtkWidget *main_create_main_menu(GtkAccelGroup *accel)
{
  GtkWidget *item; 
  GtkWidget *menu;
 
  menu = gtk_menu_new();
  
/*  item = gtk_menu_item_new_with_label("Quit");*/
  item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
  g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gtk_main_quit), NULL);
  gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);

  if (accel) {
    gtk_widget_add_accelerator(item, "activate", accel, GDK_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
  }

  return menu;
}

void main_tray_icon_menu(GtkStatusIcon *icon,
                         guint button,
                         guint activate_time,
                         gpointer user_data)
{
  GtkWidget *popup_menu = main_create_main_menu(NULL);
  gtk_widget_show_all(popup_menu);

  gtk_menu_popup(GTK_MENU(popup_menu),
                 NULL,
                 NULL, 
                 (GtkMenuPositionFunc)gtk_status_icon_position_menu,
                 (gpointer)icon,
                 button,
                 activate_time);
}

gboolean main_read_configuration_file(void)<commit_msg>New menu items to show/hide holidays and tasks.<commit_after>#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "holidays.h"
#include "calendar.h"

#define MAIN_MENU_ITEM_TOGGLE_DISPLAY_HOLIDAYS     1
#define MAIN_MENU_ITEM_TOGGLE_DISPLAY_TASKS        2

struct Configuration {
  gboolean attach_to_tray;
  gchar *holiday_file;
  gchar *config_file;
  gboolean show_holidays;
  gboolean show_tasks;
} config;

CalendarWidget *calendar = NULL;
HolidayContext *holiday_context = NULL;
GtkStatusIcon *status_icon = NULL;
gboolean tray_window_visible = FALSE;

void main_tray_position_window(GtkWidget *window)
{
  GdkRectangle area;
  GtkOrientation orientation;
  GdkScreen *screen;
  gint x;
  gint y;
  gint monitor_num;
  GdkRectangle monitor;
  GtkRequisition win_req;

  g_return_if_fail(GTK_IS_WINDOW(window));

  if (!status_icon) {
    return;
  }

  if (!gtk_status_icon_get_geometry(status_icon,
                                    &screen,
                                    &area,
                                    &orientation)) {
    g_warning("Unable to determine geometry of status icon\n");
    return;
  }

  gtk_window_set_screen(GTK_WINDOW(window), screen);

  monitor_num = gdk_screen_get_monitor_at_point(screen, area.x, area.y);
  gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
  
  gtk_container_foreach(GTK_CONTAINER(window), (GtkCallback)gtk_widget_show_all, NULL);
  gtk_widget_size_request(window, &win_req);

  if (orientation == GTK_ORIENTATION_VERTICAL) {
    if (area.x + area.width + win_req.width <= monitor.x + monitor.width) {
      x = area.x + area.width;
    }
    else {
      x = area.x - win_req.width;
    }
    if (area.y + win_req.height <= monitor.y + monitor.height) {
      y = area.y;
    }
    else {
      y = monitor.y + monitor.height - win_req.height;
    }
  }
  else {
    if (area.y + area.height + win_req.height <= monitor.y + monitor.height) {
      y = area.y + area.height;
    }
    else {
      y = area.y - win_req.height;
    }
    if (area.x + win_req.width <= monitor.x + monitor.width) {
      x = area.x;
    }
    else {
      x = monitor.x + monitor.width - win_req.width;
    }
  }

  gtk_window_move(GTK_WINDOW(window), x, y);
}

void main_tray_icon_set_visible(GtkWidget *tray_widget,
                                gboolean visible)
{
  tray_window_visible = visible;

  if (visible) {
    main_tray_position_window(tray_widget);
    gtk_widget_show_all(tray_widget);
  }
  else {
    gtk_widget_hide(tray_widget);
  }
}

void main_tray_icon_toggle_display(GtkWidget *tray_widget)
{
  main_tray_icon_set_visible(tray_widget, !tray_window_visible);
}

void main_tray_icon_clicked(GtkStatusIcon *icon,
                            GtkWidget *tray_widget)
{
  main_tray_icon_toggle_display(tray_widget);
}

void main_menu_toggle_display(GtkCheckMenuItem *item,
                              gpointer user_data)
{
  gint id = GPOINTER_TO_INT(user_data);
  if (id == MAIN_MENU_ITEM_TOGGLE_DISPLAY_HOLIDAYS) {
    if (item->active) {
      g_print("holidays active\n");
    }
    else {
      g_print("holidays not active\n");
    }
  }
}

GtkWidget *main_create_main_menu(GtkAccelGroup *accel)
{
  GtkWidget *item; 
  GtkWidget *menu;
 
  menu = gtk_menu_new();

  item = gtk_check_menu_item_new_with_label("Show holidays");
  gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), config.show_holidays);
  g_signal_connect(G_OBJECT(item),
                   "toggled",
                   G_CALLBACK(main_menu_toggle_display),
                   GINT_TO_POINTER(MAIN_MENU_ITEM_TOGGLE_DISPLAY_HOLIDAYS));
  gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
  if (accel) {
    gtk_widget_add_accelerator(item, "activate", accel, GDK_h, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
  }
  
  item = gtk_check_menu_item_new_with_label("Show tasks");
  gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), config.show_tasks);
  g_signal_connect(G_OBJECT(item),
                   "toggled",
                   G_CALLBACK(main_menu_toggle_display),
                   GINT_TO_POINTER(MAIN_MENU_ITEM_TOGGLE_DISPLAY_TASKS));
  gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
  if (accel) {
    gtk_widget_add_accelerator(item, "activate", accel, GDK_t, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
  }

  gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());

  item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
  g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(gtk_main_quit), NULL);
  gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
  if (accel) {
    gtk_widget_add_accelerator(item, "activate", accel, GDK_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
  }

  return menu;
}

void main_tray_icon_menu(GtkStatusIcon *icon,
                         guint button,
                         guint activate_time,
                         gpointer user_data)
{
  GtkWidget *popup_menu = main_create_main_menu(NULL);
  gtk_widget_show_all(popup_menu);

  gtk_menu_popup(GTK_MENU(popup_menu),
                 NULL,
                 NULL, 
                 (GtkMenuPositionFunc)gtk_status_icon_position_menu,
                 (gpointer)icon,
                 button,
                 activate_time);
}

gboolean main_read_configuration_file(void)
{'fraction_non_alphanumeric': 0.09796920330283419, 'fraction_numerical': 0.0002231644722160232, 'mean_word_length': 3.0228904847396767, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5026055', 'n_tokens_mistral': 3354, 'n_tokens_neox': 3223, 'n_words': 497}