~muktupavels/metacity/adwaita-icon-theme-lp-1414613

« back to all changes in this revision

Viewing changes to src/preview-widget.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-10-03 22:44:28 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20051003224428-ft31gkmz12qpzohj
Tags: 1:2.12.1-0ubuntu1
* New upstream release:
  - Thanks to Ray Strode, Havoc Pennington, and Elijah Newren for
    improvements in this release.
  - Truncate ridiculously long titles to avoid crashing or letting the
    pager crash (Ray, Havoc, Elijah) [#315070] (Ubuntu: #15995)
  - Get the tabbing window outline to work with gtk+ 2.8.4 again
    (Elijah) [#317528] (Ubuntu: #16589)
  - Translations: Mahay Alam Khan (bn), Francisco Javier F. Serrador (es), 
    Ivar Smolin (et), I\uffffaki Larra\uffffaga Murgoitio (eu), Luca 
    Ferretti (it), Christian Rose (sv), Clytie Siddall (vi), Funda 
    Wang (zh_CN)
* debian/control.in:
  - Bumped Standards-Version.
* debian/patches/003_bordersdrawingfix.patch:
  - dropped, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Metacity theme preview widget */
 
2
 
 
3
/* 
 
4
 * Copyright (C) 2002 Havoc Pennington
 
5
 * 
 
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.
 
10
 *
 
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.
 
15
 * 
 
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
 
19
 * 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "preview-widget.h"
 
23
 
 
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,
 
27
                                            GtkRequisition   *req);
 
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);
 
33
 
 
34
static GtkWidgetClass *parent_class;
 
35
 
 
36
GtkType
 
37
meta_preview_get_type (void)
 
38
{
 
39
  static GtkType preview_type = 0;
 
40
 
 
41
  if (!preview_type)
 
42
    {
 
43
      static const GtkTypeInfo preview_info =
 
44
      {
 
45
        "MetaPreview",
 
46
        sizeof (MetaPreview),
 
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,
 
53
      };
 
54
 
 
55
      preview_type = gtk_type_unique (GTK_TYPE_BIN, &preview_info);
 
56
    }
 
57
 
 
58
  return preview_type;
 
59
}
 
60
 
 
61
static void
 
62
meta_preview_class_init (MetaPreviewClass *class)
 
63
{
 
64
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
 
65
  GtkObjectClass *object_class;
 
66
  GtkWidgetClass *widget_class;
 
67
 
 
68
  object_class = (GtkObjectClass*) class;
 
69
  widget_class = (GtkWidgetClass*) class;
 
70
  parent_class = gtk_type_class (GTK_TYPE_BIN);
 
71
 
 
72
  gobject_class->finalize = meta_preview_finalize;
 
73
 
 
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;
 
77
}
 
78
 
 
79
static void
 
80
meta_preview_init (MetaPreview *preview)
 
81
{
 
82
  int i;
 
83
  
 
84
  GTK_WIDGET_SET_FLAGS (preview, GTK_NO_WINDOW);
 
85
 
 
86
  i = 0;
 
87
  while (i < MAX_BUTTONS_PER_CORNER)
 
88
    {
 
89
      preview->button_layout.left_buttons[i] = META_BUTTON_FUNCTION_LAST;
 
90
      preview->button_layout.right_buttons[i] = META_BUTTON_FUNCTION_LAST;
 
91
      ++i;
 
92
    }
 
93
  
 
94
  preview->button_layout.left_buttons[0] = META_BUTTON_FUNCTION_MENU;
 
95
 
 
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;
 
99
  
 
100
  preview->type = META_FRAME_TYPE_NORMAL;
 
101
  preview->flags =
 
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;
 
111
  
 
112
  preview->left_width = -1;
 
113
  preview->right_width = -1;
 
114
  preview->top_height = -1;
 
115
  preview->bottom_height = -1;
 
116
}
 
117
 
 
118
GtkWidget*
 
119
meta_preview_new (void)
 
120
{
 
121
  MetaPreview *preview;
 
122
  
 
123
  preview = gtk_type_new (META_TYPE_PREVIEW);
 
124
  
 
125
  return GTK_WIDGET (preview);
 
126
}
 
127
 
 
128
static void
 
129
meta_preview_finalize (GObject *object)
 
130
{
 
131
  MetaPreview *preview;
 
132
 
 
133
  preview = META_PREVIEW (object);
 
134
  
 
135
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
136
}
 
137
 
 
138
static void
 
139
ensure_info (MetaPreview *preview)
 
140
{
 
141
  GtkWidget *widget;
 
142
 
 
143
  widget = GTK_WIDGET (preview);
 
144
  
 
145
  if (preview->layout == NULL)
 
146
    {
 
147
      PangoFontDescription *font_desc;
 
148
      double scale;
 
149
      PangoAttrList *attrs;
 
150
      PangoAttribute *attr;
 
151
 
 
152
      if (preview->theme)        
 
153
        scale = meta_theme_get_title_scale (preview->theme,
 
154
                                            preview->type,
 
155
                                            preview->flags);
 
156
      else
 
157
        scale = 1.0;
 
158
      
 
159
      preview->layout = gtk_widget_create_pango_layout (widget,
 
160
                                                        preview->title);
 
161
      
 
162
      font_desc = meta_gtk_widget_get_font_desc (widget, scale, NULL);
 
163
      
 
164
      preview->text_height =
 
165
        meta_pango_font_desc_get_text_height (font_desc,
 
166
                                              gtk_widget_get_pango_context (widget));
 
167
          
 
168
      attrs = pango_attr_list_new ();
 
169
      
 
170
      attr = pango_attr_size_new (pango_font_description_get_size (font_desc));
 
171
      attr->start_index = 0;
 
172
      attr->end_index = G_MAXINT;
 
173
      
 
174
      pango_attr_list_insert (attrs, attr);
 
175
      
 
176
      pango_layout_set_attributes (preview->layout, attrs);
 
177
      
 
178
      pango_attr_list_unref (attrs);      
 
179
  
 
180
      pango_font_description_free (font_desc);
 
181
    }
 
182
 
 
183
  if (preview->top_height < 0)
 
184
    {
 
185
      if (preview->theme)
 
186
        {
 
187
          meta_theme_get_frame_borders (preview->theme,
 
188
                                        preview->type,
 
189
                                        preview->text_height,
 
190
                                        preview->flags,
 
191
                                        &preview->top_height,
 
192
                                        &preview->bottom_height,
 
193
                                        &preview->left_width,
 
194
                                        &preview->right_width);
 
195
        }
 
196
      else
 
197
        {
 
198
          preview->top_height = 0;
 
199
          preview->bottom_height = 0;
 
200
          preview->left_width = 0;
 
201
          preview->right_width = 0;
 
202
        }
 
203
    }
 
204
}
 
205
 
 
206
static gboolean
 
207
meta_preview_expose (GtkWidget      *widget,
 
208
                     GdkEventExpose *event)
 
209
{
 
210
  MetaPreview *preview;
 
211
  int border_width;
 
212
  int client_width;
 
213
  int client_height;
 
214
  MetaButtonState button_states[META_BUTTON_TYPE_LAST] =
 
215
  {
 
216
    META_BUTTON_STATE_NORMAL,
 
217
    META_BUTTON_STATE_NORMAL,
 
218
    META_BUTTON_STATE_NORMAL,
 
219
    META_BUTTON_STATE_NORMAL
 
220
  };
 
221
  
 
222
  g_return_val_if_fail (META_IS_PREVIEW (widget), FALSE);
 
223
  g_return_val_if_fail (event != NULL, FALSE);
 
224
 
 
225
  preview = META_PREVIEW (widget);
 
226
 
 
227
  ensure_info (preview);
 
228
 
 
229
  border_width = GTK_CONTAINER (widget)->border_width;
 
230
  
 
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;
 
233
 
 
234
  if (client_width < 0)
 
235
    client_width = 1;
 
236
  if (client_height < 0)
 
237
    client_height = 1;  
 
238
  
 
239
  if (preview->theme)
 
240
    {
 
241
      border_width = GTK_CONTAINER (widget)->border_width;
 
242
      
 
243
      meta_theme_draw_frame (preview->theme,
 
244
                             widget,
 
245
                             widget->window,
 
246
                             &event->area,
 
247
                             widget->allocation.x + border_width,
 
248
                             widget->allocation.y + border_width,
 
249
                             preview->type,
 
250
                             preview->flags,
 
251
                             client_width, client_height,
 
252
                             preview->layout,
 
253
                             preview->text_height,
 
254
                             &preview->button_layout,
 
255
                             button_states,
 
256
                             meta_preview_get_mini_icon (),
 
257
                             meta_preview_get_icon ());
 
258
    }
 
259
 
 
260
  /* draw child */
 
261
  return GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
 
262
}
 
263
 
 
264
static void
 
265
meta_preview_size_request (GtkWidget      *widget,
 
266
                           GtkRequisition *req)
 
267
{
 
268
  MetaPreview *preview;
 
269
 
 
270
  preview = META_PREVIEW (widget);
 
271
 
 
272
  ensure_info (preview);
 
273
 
 
274
  req->width = preview->left_width + preview->right_width;
 
275
  req->height = preview->top_height + preview->bottom_height;
 
276
  
 
277
  if (GTK_BIN (preview)->child &&
 
278
      GTK_WIDGET_VISIBLE (GTK_BIN (preview)->child))
 
279
    {
 
280
      GtkRequisition child_requisition;
 
281
 
 
282
      gtk_widget_size_request (GTK_BIN (preview)->child, &child_requisition);
 
283
 
 
284
      req->width += child_requisition.width;
 
285
      req->height += child_requisition.height;
 
286
    }
 
287
  else
 
288
    {
 
289
#define NO_CHILD_WIDTH 80
 
290
#define NO_CHILD_HEIGHT 20
 
291
      req->width += NO_CHILD_WIDTH;
 
292
      req->height += NO_CHILD_HEIGHT;
 
293
    }
 
294
 
 
295
  req->width += GTK_CONTAINER (widget)->border_width * 2;
 
296
  req->height += GTK_CONTAINER (widget)->border_width * 2;
 
297
}
 
298
 
 
299
static void
 
300
meta_preview_size_allocate (GtkWidget         *widget,
 
301
                            GtkAllocation     *allocation)
 
302
{
 
303
  MetaPreview *preview;
 
304
  int border_width;
 
305
  GtkAllocation child_allocation;
 
306
  
 
307
  preview = META_PREVIEW (widget);
 
308
 
 
309
  ensure_info (preview);
 
310
  
 
311
  widget->allocation = *allocation;
 
312
 
 
313
  border_width = GTK_CONTAINER (widget)->border_width;
 
314
  
 
315
  if (GTK_BIN (widget)->child &&
 
316
      GTK_WIDGET_VISIBLE (GTK_BIN (widget)->child))
 
317
    {
 
318
      child_allocation.x = widget->allocation.x + border_width + preview->left_width;
 
319
      child_allocation.y = widget->allocation.y + border_width + preview->top_height;
 
320
      
 
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);
 
323
 
 
324
      gtk_widget_size_allocate (GTK_BIN (widget)->child, &child_allocation);
 
325
    }
 
326
}
 
327
 
 
328
static void
 
329
clear_cache (MetaPreview *preview)
 
330
{
 
331
  if (preview->layout)
 
332
    {
 
333
      g_object_unref (G_OBJECT (preview->layout));
 
334
      preview->layout = NULL;
 
335
    }
 
336
 
 
337
  preview->left_width = -1;
 
338
  preview->right_width = -1;
 
339
  preview->top_height = -1;
 
340
  preview->bottom_height = -1;
 
341
}
 
342
 
 
343
void
 
344
meta_preview_set_theme (MetaPreview    *preview,
 
345
                        MetaTheme      *theme)
 
346
{
 
347
  g_return_if_fail (META_IS_PREVIEW (preview));
 
348
 
 
349
  preview->theme = theme;
 
350
  
 
351
  clear_cache (preview);
 
352
 
 
353
  gtk_widget_queue_resize (GTK_WIDGET (preview));
 
354
}
 
355
 
 
356
void
 
357
meta_preview_set_title (MetaPreview    *preview,
 
358
                        const char     *title)
 
359
{
 
360
  g_return_if_fail (META_IS_PREVIEW (preview));
 
361
 
 
362
  g_free (preview->title);
 
363
  preview->title = g_strdup (title);
 
364
  
 
365
  clear_cache (preview);
 
366
 
 
367
  gtk_widget_queue_resize (GTK_WIDGET (preview));
 
368
}
 
369
 
 
370
void
 
371
meta_preview_set_frame_type (MetaPreview    *preview,
 
372
                             MetaFrameType   type)
 
373
{
 
374
  g_return_if_fail (META_IS_PREVIEW (preview));
 
375
 
 
376
  preview->type = type;
 
377
 
 
378
  clear_cache (preview);
 
379
 
 
380
  gtk_widget_queue_resize (GTK_WIDGET (preview));
 
381
}
 
382
 
 
383
void
 
384
meta_preview_set_frame_flags (MetaPreview    *preview,
 
385
                              MetaFrameFlags  flags)
 
386
{
 
387
  g_return_if_fail (META_IS_PREVIEW (preview));
 
388
 
 
389
  preview->flags = flags;
 
390
 
 
391
  clear_cache (preview);
 
392
 
 
393
  gtk_widget_queue_resize (GTK_WIDGET (preview));
 
394
}
 
395
 
 
396
void
 
397
meta_preview_set_button_layout (MetaPreview            *preview,
 
398
                                const MetaButtonLayout *button_layout)
 
399
{
 
400
  g_return_if_fail (META_IS_PREVIEW (preview));
 
401
  
 
402
  preview->button_layout = *button_layout;  
 
403
  
 
404
  gtk_widget_queue_draw (GTK_WIDGET (preview));
 
405
}
 
406
 
 
407
#include "inlinepixbufs.h"
 
408
 
 
409
GdkPixbuf*
 
410
meta_preview_get_icon (void)
 
411
{
 
412
  static GdkPixbuf *default_icon = NULL;
 
413
 
 
414
  if (default_icon == NULL)
 
415
    {
 
416
      GdkPixbuf *base;
 
417
 
 
418
      base = gdk_pixbuf_new_from_inline (-1, default_icon_data,
 
419
                                         FALSE,
 
420
                                         NULL);
 
421
 
 
422
      g_assert (base);
 
423
 
 
424
      default_icon = gdk_pixbuf_scale_simple (base,
 
425
                                              META_ICON_WIDTH,
 
426
                                              META_ICON_HEIGHT,
 
427
                                              GDK_INTERP_BILINEAR);
 
428
 
 
429
      g_object_unref (G_OBJECT (base));
 
430
    }
 
431
  
 
432
  return default_icon;
 
433
}
 
434
 
 
435
GdkPixbuf*
 
436
meta_preview_get_mini_icon (void)
 
437
{
 
438
  static GdkPixbuf *default_icon = NULL;
 
439
 
 
440
  if (default_icon == NULL)
 
441
    {
 
442
      GdkPixbuf *base;
 
443
 
 
444
      base = gdk_pixbuf_new_from_inline (-1, default_icon_data,
 
445
                                         FALSE,
 
446
                                         NULL);
 
447
 
 
448
      g_assert (base);
 
449
 
 
450
      default_icon = gdk_pixbuf_scale_simple (base,
 
451
                                              META_MINI_ICON_WIDTH,
 
452
                                              META_MINI_ICON_HEIGHT,
 
453
                                              GDK_INTERP_BILINEAR);
 
454
 
 
455
      g_object_unref (G_OBJECT (base));
 
456
    }
 
457
  
 
458
  return default_icon;
 
459
}