Tue, 29 Oct 2024 11:51:25 +0100
fix crash when closing the preferences window (GTK3)
51
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2017 Olaf Wintermann. All rights reserved. |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include <stdio.h> |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include <stdlib.h> |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <string.h> |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include <cx/map.h> |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | #include "toolkit.h" |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | #include "icon.h" |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #include "../common/properties.h" |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | static CxMap *image_map; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | static GtkIconTheme *icon_theme; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | #if GTK_MAJOR_VERSION >= 4 |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | #define ICONTHEME_GET_DEFAULT() gtk_icon_theme_get_for_display(gdk_display_get_default()) |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | #else |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | #define ICONTHEME_GET_DEFAULT() gtk_icon_theme_get_default() |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | #endif |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | void ui_image_init(void) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | image_map = cxHashMapCreateSimple(CX_STORE_POINTERS); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | icon_theme = ICONTHEME_GET_DEFAULT(); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | // **** deprecated functions **** |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | GdkPixbuf* ui_get_image(const char *name) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | UiImage *img = cxMapGet(image_map, name); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | if(img) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | return img->pixbuf; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | } else { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | //ui_add_image(name, name); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | //return ucx_map_cstr_get(image_map, name); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | // TODO |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | return NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | // **** deprecated2**** |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | static UiIcon* get_icon(const char *name, int size, int scale) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | #if GTK_MAJOR_VERSION >= 4 |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | GtkIconPaintable *info = gtk_icon_theme_lookup_icon(icon_theme, name, NULL, size, scale, GTK_TEXT_DIR_LTR, GTK_ICON_LOOKUP_FORCE_REGULAR); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | #elif defined(UI_SUPPORTS_SCALE) |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | GtkIconInfo *info = gtk_icon_theme_lookup_icon_for_scale(icon_theme, name, size, scale, 0); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | #else |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | GtkIconInfo *info = gtk_icon_theme_lookup_icon(icon_theme, name, size, 0); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | #endif |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | if(info) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | UiIcon *icon = malloc(sizeof(UiIcon)); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | icon->info = info; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | icon->pixbuf = NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | return icon; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | return NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | UiIcon* ui_icon(const char* name, size_t size) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | return get_icon(name, size, ui_get_scalefactor()); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | UiIcon* ui_imageicon(const char* file) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | GError *error = NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(file, &error); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | if(!pixbuf) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | fprintf(stderr, "UiError: Cannot load image: %s\n", file); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | return NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | UiIcon *icon = malloc(sizeof(UiIcon)); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | icon->info = NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | icon->pixbuf = pixbuf; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | return icon; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | void ui_icon_free(UiIcon* icon) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | if(icon->info) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | g_object_unref(icon->info); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109 | if(icon->pixbuf) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | g_object_unref(icon->pixbuf); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | free(icon); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | UiIcon* ui_foldericon(size_t size) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | return ui_icon("folder", size); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | UiIcon* ui_fileicon(size_t size) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | UiIcon *icon = ui_icon("file", size); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | #if GTK_MAJOR_VERSION >= 4 |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | GFile *file = gtk_icon_paintable_get_file(icon->info); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | char *path = g_file_get_path(file); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | if(!path) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | icon = ui_icon("application-x-generic", size); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126 | } |
70
94144a948aff
fix generic file icon on gtk3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
127 | #else |
94144a948aff
fix generic file icon on gtk3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
128 | if(!icon) { |
94144a948aff
fix generic file icon on gtk3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
129 | icon = ui_icon("application-x-generic", size); |
94144a948aff
fix generic file icon on gtk3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
51
diff
changeset
|
130 | } |
51
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | #endif |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | return icon; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | UiIcon* ui_icon_unscaled(const char *name, int size) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | return get_icon(name, size, 1); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | #if GTK_MAJOR_VERSION >= 4 |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | GdkPixbuf* ui_icon_pixbuf(UiIcon *icon) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | if(!icon->pixbuf) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | GFile *file = gtk_icon_paintable_get_file(icon->info); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | GError *error = NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | char *path = g_file_get_path(file); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | icon->pixbuf = gdk_pixbuf_new_from_file(path, &error); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | return icon->pixbuf; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | #else |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | GdkPixbuf* ui_icon_pixbuf(UiIcon *icon) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | if(!icon->pixbuf) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152 | GError *error = NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
153 | icon->pixbuf = gtk_icon_info_load_icon(icon->info, &error); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | return icon->pixbuf; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | #endif |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | /* |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | UiImage* ui_icon_image(UiIcon *icon) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | GError *error = NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | if(pixbuf) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | UiImage *img = malloc(sizeof(UiImage)); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | img->pixbuf = pixbuf; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | return img; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | return NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | */ |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | /* |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | UiImage* ui_image(const char *filename) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | return ui_named_image(filename, NULL); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | UiImage* ui_named_image(const char *filename, const char *name) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | char *path = uic_get_image_path(filename); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | if(!path) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | fprintf(stderr, "UiError: pixmaps directory not set\n"); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | return NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | UiImage *img = ui_load_image_from_path(path, name); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | free(path); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185 | return img; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | UiImage* ui_load_image_from_path(const char *path, const char *name) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | GError *error = NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | if(!pixbuf) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | fprintf(stderr, "UiError: Cannot load image: %s\n", path); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | return NULL; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | UiImage *img = malloc(sizeof(UiImage)); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | img->pixbuf = pixbuf; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
198 | if(name) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | cxMapPut(image_map, name, img); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | return img; |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | } |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | */ |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | void ui_free_image(UiImage *img) { |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | g_object_unref(img->pixbuf); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | free(img); |
e324291ca9f8
add resource preview window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | } |