1
/* Metacity theme preview widget */
4
* Copyright (C) 2002 Havoc Pennington
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License as
8
* published by the Free Software Foundation; either version 2 of the
9
* License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22
#include "preview-widget.h"
24
static void meta_preview_class_init (MetaPreviewClass *klass);
25
static void meta_preview_init (MetaPreview *preview);
26
static void meta_preview_size_request (GtkWidget *widget,
28
static void meta_preview_size_allocate (GtkWidget *widget,
29
GtkAllocation *allocation);
30
static gboolean meta_preview_expose (GtkWidget *widget,
31
GdkEventExpose *event);
32
static void meta_preview_finalize (GObject *object);
34
static GtkWidgetClass *parent_class;
37
meta_preview_get_type (void)
39
static GtkType preview_type = 0;
43
static const GtkTypeInfo preview_info =
47
sizeof (MetaPreviewClass),
48
(GtkClassInitFunc) meta_preview_class_init,
49
(GtkObjectInitFunc) meta_preview_init,
50
/* reserved_1 */ NULL,
51
/* reserved_2 */ NULL,
52
(GtkClassInitFunc) NULL,
55
preview_type = gtk_type_unique (GTK_TYPE_BIN, &preview_info);
62
meta_preview_class_init (MetaPreviewClass *class)
64
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
65
GtkObjectClass *object_class;
66
GtkWidgetClass *widget_class;
68
object_class = (GtkObjectClass*) class;
69
widget_class = (GtkWidgetClass*) class;
70
parent_class = gtk_type_class (GTK_TYPE_BIN);
72
gobject_class->finalize = meta_preview_finalize;
74
widget_class->expose_event = meta_preview_expose;
75
widget_class->size_request = meta_preview_size_request;
76
widget_class->size_allocate = meta_preview_size_allocate;
80
meta_preview_init (MetaPreview *preview)
84
GTK_WIDGET_SET_FLAGS (preview, GTK_NO_WINDOW);
87
while (i < MAX_BUTTONS_PER_CORNER)
89
preview->button_layout.left_buttons[i] = META_BUTTON_FUNCTION_LAST;
90
preview->button_layout.right_buttons[i] = META_BUTTON_FUNCTION_LAST;
94
preview->button_layout.left_buttons[0] = META_BUTTON_FUNCTION_MENU;
96
preview->button_layout.right_buttons[0] = META_BUTTON_FUNCTION_MINIMIZE;
97
preview->button_layout.right_buttons[1] = META_BUTTON_FUNCTION_MAXIMIZE;
98
preview->button_layout.right_buttons[2] = META_BUTTON_FUNCTION_CLOSE;
100
preview->type = META_FRAME_TYPE_NORMAL;
102
META_FRAME_ALLOWS_DELETE |
103
META_FRAME_ALLOWS_MENU |
104
META_FRAME_ALLOWS_MINIMIZE |
105
META_FRAME_ALLOWS_MAXIMIZE |
106
META_FRAME_ALLOWS_VERTICAL_RESIZE |
107
META_FRAME_ALLOWS_HORIZONTAL_RESIZE |
108
META_FRAME_HAS_FOCUS |
109
META_FRAME_ALLOWS_SHADE |
110
META_FRAME_ALLOWS_MOVE;
112
preview->left_width = -1;
113
preview->right_width = -1;
114
preview->top_height = -1;
115
preview->bottom_height = -1;
119
meta_preview_new (void)
121
MetaPreview *preview;
123
preview = gtk_type_new (META_TYPE_PREVIEW);
125
return GTK_WIDGET (preview);
129
meta_preview_finalize (GObject *object)
131
MetaPreview *preview;
133
preview = META_PREVIEW (object);
135
G_OBJECT_CLASS (parent_class)->finalize (object);
139
ensure_info (MetaPreview *preview)
143
widget = GTK_WIDGET (preview);
145
if (preview->layout == NULL)
147
PangoFontDescription *font_desc;
149
PangoAttrList *attrs;
150
PangoAttribute *attr;
153
scale = meta_theme_get_title_scale (preview->theme,
159
preview->layout = gtk_widget_create_pango_layout (widget,
162
font_desc = meta_gtk_widget_get_font_desc (widget, scale, NULL);
164
preview->text_height =
165
meta_pango_font_desc_get_text_height (font_desc,
166
gtk_widget_get_pango_context (widget));
168
attrs = pango_attr_list_new ();
170
attr = pango_attr_size_new (pango_font_description_get_size (font_desc));
171
attr->start_index = 0;
172
attr->end_index = G_MAXINT;
174
pango_attr_list_insert (attrs, attr);
176
pango_layout_set_attributes (preview->layout, attrs);
178
pango_attr_list_unref (attrs);
180
pango_font_description_free (font_desc);
183
if (preview->top_height < 0)
187
meta_theme_get_frame_borders (preview->theme,
189
preview->text_height,
191
&preview->top_height,
192
&preview->bottom_height,
193
&preview->left_width,
194
&preview->right_width);
198
preview->top_height = 0;
199
preview->bottom_height = 0;
200
preview->left_width = 0;
201
preview->right_width = 0;
207
meta_preview_expose (GtkWidget *widget,
208
GdkEventExpose *event)
210
MetaPreview *preview;
214
MetaButtonState button_states[META_BUTTON_TYPE_LAST] =
216
META_BUTTON_STATE_NORMAL,
217
META_BUTTON_STATE_NORMAL,
218
META_BUTTON_STATE_NORMAL,
219
META_BUTTON_STATE_NORMAL
222
g_return_val_if_fail (META_IS_PREVIEW (widget), FALSE);
223
g_return_val_if_fail (event != NULL, FALSE);
225
preview = META_PREVIEW (widget);
227
ensure_info (preview);
229
border_width = GTK_CONTAINER (widget)->border_width;
231
client_width = widget->allocation.width - preview->left_width - preview->right_width - border_width * 2;
232
client_height = widget->allocation.height - preview->top_height - preview->bottom_height - border_width * 2;
234
if (client_width < 0)
236
if (client_height < 0)
241
border_width = GTK_CONTAINER (widget)->border_width;
243
meta_theme_draw_frame (preview->theme,
247
widget->allocation.x + border_width,
248
widget->allocation.y + border_width,
251
client_width, client_height,
253
preview->text_height,
254
&preview->button_layout,
256
meta_preview_get_mini_icon (),
257
meta_preview_get_icon ());
261
return GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
265
meta_preview_size_request (GtkWidget *widget,
268
MetaPreview *preview;
270
preview = META_PREVIEW (widget);
272
ensure_info (preview);
274
req->width = preview->left_width + preview->right_width;
275
req->height = preview->top_height + preview->bottom_height;
277
if (GTK_BIN (preview)->child &&
278
GTK_WIDGET_VISIBLE (GTK_BIN (preview)->child))
280
GtkRequisition child_requisition;
282
gtk_widget_size_request (GTK_BIN (preview)->child, &child_requisition);
284
req->width += child_requisition.width;
285
req->height += child_requisition.height;
289
#define NO_CHILD_WIDTH 80
290
#define NO_CHILD_HEIGHT 20
291
req->width += NO_CHILD_WIDTH;
292
req->height += NO_CHILD_HEIGHT;
295
req->width += GTK_CONTAINER (widget)->border_width * 2;
296
req->height += GTK_CONTAINER (widget)->border_width * 2;
300
meta_preview_size_allocate (GtkWidget *widget,
301
GtkAllocation *allocation)
303
MetaPreview *preview;
305
GtkAllocation child_allocation;
307
preview = META_PREVIEW (widget);
309
ensure_info (preview);
311
widget->allocation = *allocation;
313
border_width = GTK_CONTAINER (widget)->border_width;
315
if (GTK_BIN (widget)->child &&
316
GTK_WIDGET_VISIBLE (GTK_BIN (widget)->child))
318
child_allocation.x = widget->allocation.x + border_width + preview->left_width;
319
child_allocation.y = widget->allocation.y + border_width + preview->top_height;
321
child_allocation.width = MAX (1, widget->allocation.width - border_width * 2 - preview->left_width - preview->right_width);
322
child_allocation.height = MAX (1, widget->allocation.height - border_width * 2 - preview->top_height - preview->bottom_height);
324
gtk_widget_size_allocate (GTK_BIN (widget)->child, &child_allocation);
329
clear_cache (MetaPreview *preview)
333
g_object_unref (G_OBJECT (preview->layout));
334
preview->layout = NULL;
337
preview->left_width = -1;
338
preview->right_width = -1;
339
preview->top_height = -1;
340
preview->bottom_height = -1;
344
meta_preview_set_theme (MetaPreview *preview,
347
g_return_if_fail (META_IS_PREVIEW (preview));
349
preview->theme = theme;
351
clear_cache (preview);
353
gtk_widget_queue_resize (GTK_WIDGET (preview));
357
meta_preview_set_title (MetaPreview *preview,
360
g_return_if_fail (META_IS_PREVIEW (preview));
362
g_free (preview->title);
363
preview->title = g_strdup (title);
365
clear_cache (preview);
367
gtk_widget_queue_resize (GTK_WIDGET (preview));
371
meta_preview_set_frame_type (MetaPreview *preview,
374
g_return_if_fail (META_IS_PREVIEW (preview));
376
preview->type = type;
378
clear_cache (preview);
380
gtk_widget_queue_resize (GTK_WIDGET (preview));
384
meta_preview_set_frame_flags (MetaPreview *preview,
385
MetaFrameFlags flags)
387
g_return_if_fail (META_IS_PREVIEW (preview));
389
preview->flags = flags;
391
clear_cache (preview);
393
gtk_widget_queue_resize (GTK_WIDGET (preview));
397
meta_preview_set_button_layout (MetaPreview *preview,
398
const MetaButtonLayout *button_layout)
400
g_return_if_fail (META_IS_PREVIEW (preview));
402
preview->button_layout = *button_layout;
404
gtk_widget_queue_draw (GTK_WIDGET (preview));
407
#include "inlinepixbufs.h"
410
meta_preview_get_icon (void)
412
static GdkPixbuf *default_icon = NULL;
414
if (default_icon == NULL)
418
base = gdk_pixbuf_new_from_inline (-1, default_icon_data,
424
default_icon = gdk_pixbuf_scale_simple (base,
427
GDK_INTERP_BILINEAR);
429
g_object_unref (G_OBJECT (base));
436
meta_preview_get_mini_icon (void)
438
static GdkPixbuf *default_icon = NULL;
440
if (default_icon == NULL)
444
base = gdk_pixbuf_new_from_inline (-1, default_icon_data,
450
default_icon = gdk_pixbuf_scale_simple (base,
451
META_MINI_ICON_WIDTH,
452
META_MINI_ICON_HEIGHT,
453
GDK_INTERP_BILINEAR);
455
g_object_unref (G_OBJECT (base));