aboutsummaryrefslogtreecommitdiff
path: root/mate-applet/topmenu-mate-panel-applet.c
blob: 32841b731e435fbd3352a0c162b2cee6baa60352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright 2014 Javier S. Pedro <maemo@javispedro.com>
 *
 * This file is part of TopMenu.
 *
 * TopMenu 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 3 of the License, or
 * (at your option) any later version.
 *
 * TopMenu 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 TopMenu.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "topmenu-mate-panel-applet.h"

#include <glib/gi18n.h>
#include <gdk/gdkx.h>
#include <cairo-xlib.h>

G_DEFINE_TYPE(TopMenuMatePanelApplet, topmenu_mate_panel_applet, PANEL_TYPE_APPLET)

static void display_preferences_dialog(GtkAction *action, TopMenuMatePanelApplet *self)
{
	// No preferences yet
}

static void display_about_dialog(GtkAction *action, TopMenuMatePanelApplet *self)
{
	GtkWindow *parent = NULL;
	GtkWidget *parent_widget = gtk_widget_get_toplevel(GTK_WIDGET(self));
	if (GTK_IS_WINDOW(parent_widget)) {
		parent = GTK_WINDOW(parent_widget);
	}

	gtk_show_about_dialog(parent,
	                      "program-name", "TopMenu Mate Panel Applet",
	                      NULL);
}

static const GtkActionEntry menu_verbs[] = {
	{ "TopMenuPreferences", GTK_STOCK_PROPERTIES, N_("_Preferences"),
	  NULL, NULL,
	  G_CALLBACK (display_preferences_dialog) },
	{ "TopMenuAbout", GTK_STOCK_ABOUT, N_("_About"),
	  NULL, NULL,
	  G_CALLBACK (display_about_dialog) }
};

#if GTK_MAJOR_VERSION == 3
static void topmenu_mate_panel_applet_change_background(MatePanelApplet* applet, MatePanelAppletBackgroundType type, GdkRGBA* color, cairo_pattern_t* pattern)
{
	TopMenuMatePanelApplet *self = TOPMENU_MATE_PANEL_APPLET(applet);

	if (type == PANEL_PIXMAP_BACKGROUND) {
		topmenu_background_set_from_cairo_pattern(self->menu_widget->background, pattern);
	} else if (type == PANEL_COLOR_BACKGROUND) {
		topmenu_background_set_from_color(self->menu_widget->background,
		                                  color->red, color->green, color->blue,
		                                  color->alpha);
	} else {
		topmenu_background_clear(self->menu_widget->background);
	}
}
#elif GTK_MAJOR_VERSION == 2
static void topmenu_mate_panel_applet_change_background(MatePanelApplet* applet, MatePanelAppletBackgroundType type, GdkColor* color, GdkPixmap* pixmap)
{
	TopMenuMatePanelApplet *self = TOPMENU_MATE_PANEL_APPLET(applet);

	if (type == PANEL_PIXMAP_BACKGROUND) {
		topmenu_background_set_from_drawable(self->menu_widget->background,
		                                     GDK_PIXMAP_XID(pixmap));
	} else if (type == PANEL_COLOR_BACKGROUND) {
		topmenu_background_set_from_color(self->menu_widget->background,
		                                  color->red / 65535.0,
		                                  color->green / 65535.0,
                                          color->blue / 65535.0,
		                                  1.0);
	} else {
		topmenu_background_clear(self->menu_widget->background);
	}
}
#endif

static void topmenu_mate_panel_applet_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
	TopMenuMatePanelApplet *self = TOPMENU_MATE_PANEL_APPLET(widget);
	if (self->menu_widget) {
		gtk_widget_size_allocate(GTK_WIDGET(self->menu_widget), allocation);
	}
	GTK_WIDGET_CLASS(topmenu_mate_panel_applet_parent_class)->size_allocate(widget, allocation);
}

static void topmenu_mate_panel_applet_size_request(GtkWidget *widget, GtkRequisition *requisition)
{
	TopMenuMatePanelApplet *self = TOPMENU_MATE_PANEL_APPLET(widget);
	if (self->menu_widget) {
		gtk_widget_size_request(GTK_WIDGET(self->menu_widget), requisition);
	}
}

static void topmenu_mate_panel_applet_class_init(TopMenuMatePanelAppletClass *klass)
{
	MatePanelAppletClass *applet_class = MATE_PANEL_APPLET_CLASS(klass);
	applet_class->change_background = topmenu_mate_panel_applet_change_background;

	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
	widget_class->size_allocate = topmenu_mate_panel_applet_size_allocate;
	widget_class->size_request = topmenu_mate_panel_applet_size_request;
}

static void topmenu_mate_panel_applet_init(TopMenuMatePanelApplet *self)
{
	self->menu_widget = TOPMENU_WIDGET(topmenu_widget_new());
	gtk_widget_set_can_focus(GTK_WIDGET(self->menu_widget), TRUE);
	gtk_container_add(GTK_CONTAINER(self), GTK_WIDGET(self->menu_widget));

	GtkActionGroup *action_group = gtk_action_group_new("TopMenu Mate Panel Applet Actions");
	gtk_action_group_add_actions(action_group,
	                             menu_verbs, G_N_ELEMENTS(menu_verbs), self);

	mate_panel_applet_set_flags(MATE_PANEL_APPLET(self),
	                            MATE_PANEL_APPLET_EXPAND_MINOR);
	mate_panel_applet_setup_menu(MATE_PANEL_APPLET(self),
	                             "<menuitem name=\"TopMenu Preferences Item\" action=\"TopMenuPreferences\"/>"
	                             "<menuitem name=\"TopMenu About Item\" action=\"TopMenuAbout\"/>",
	                             action_group);

	g_object_unref(action_group);
}

MatePanelApplet *topmenu_mate_panel_applet_new(void)
{
	return MATE_PANEL_APPLET(g_object_new(TOPMENU_TYPE_MATE_PANEL_APPLET, NULL));
}