NetSurf
ctxmenu.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 Ole Loots <ole@monochrom.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "utils/log.h"
20#include "utils/messages.h"
21#include "utils/nsoption.h"
22#include "utils/nsurl.h"
24#include "netsurf/content.h"
25#include "netsurf/keypress.h"
26
27#include "atari/gui.h"
28#include "atari/misc.h"
29#include "atari/rootwin.h"
30#include "atari/clipboard.h"
31#include "atari/gemtk/gemtk.h"
32#include "atari/res/netsurf.rsh"
33#include "atari/ctxmenu.h"
34
35
36#define CNT_INVALID 0
37#define CNT_BROWSER 64
38#define CNT_HREF 128
39#define CNT_SELECTION 256
40#define CNT_INTERACTIVE 512
41#define CNT_IMG 1024
42
43bool gui_window_get_scroll(struct gui_window *w, int *sx, int *sy);
44
46 unsigned long flags;
48};
49
51
52static struct s_context_info * get_context_info( struct gui_window * gw, short mx, short my )
53{
54 struct hlcache_handle *h;
55 GRECT area;
56 struct browser_window * bw = gw->browser->bw;
57 int sx, sy;
58
60 ctxinfo.flags = 0;
61
63 if (POINT_WITHIN(mx, my, area)) {
64
65 mx -= area.g_x;
66 my -= area.g_y;
67
68 if (browser_window_has_content(bw) == false ||
70 return(&ctxinfo);
71 }
72
74
75 gui_window_get_scroll(gw, &sx, &sy);
76
77 browser_window_get_features( gw->browser->bw, mx+sx, my+sy,
79
80 if( ctxinfo.ccdata.link ){
82 }
83 if( ctxinfo.ccdata.object) {
86 }
87 }
88 if ( ctxinfo.ccdata.form_features == CTX_FORM_TEXT )
90 }
91
92 return(&ctxinfo);
93
94
95}
96
97/***
98 * Generates an unique filename for temp. files
99 * The generated filename includes the path.
100 * If TMPDIR environmenrt vairable is set, this will be used as path,
101 * otherwise U:\tmp\
102 * \param prefix
103 * \param sufffix
104 * \return pointer to static buffer owned by get_tmpfilename()
105 */
106static char * get_tmpfilename(const char * prefix, const char * suffix)
107{
108 int i=0;
109 static char tmpfilename[PATH_MAX];
110 char * tmpdir;
111 const char * tmp_path_suffix = "";
112
113 // TODO: make function public?
114 tmpdir = getenv("TMPDIR");
115 if(tmpdir == NULL){
116 tmpdir = (char*)"u:\\tmp\\";
117 }
118
119 if(tmpdir[strlen(tmpdir)-1] != '\\'){
120 tmp_path_suffix = "\\";
121 }
122
123 do{
124 /* generate a new filename: */
125 snprintf(tmpfilename, PATH_MAX, "%s%s%s%d%s", tmpdir,
126 tmp_path_suffix, prefix, i++, suffix);
127 /* check with cflib: */
128 } while(file_exists(tmpfilename));
129
130 return(tmpfilename);
131}
132
133//TODO: do not open popup for gui_window, but for a rootwin?
134void context_popup(struct gui_window * gw, short x, short y)
135{
136
137#define POP_FIRST_ITEM POP_CTX_CUT_SEL
138#define POP_LAST_ITEM POP_CTX_SAVE_LINK_AS
139
140 OBJECT * pop;
141 int choice;
142 struct s_context_info * ctx;
143 unsigned long size;
144 const char * data;
145 FILE * fp_tmpfile;
146 char cmdline[128];
147 /* skip first byte, which must hold length of commandline: */
148 char * tempfile = &cmdline[1];
149 char * editor;
150 MENU pop_menu, me_data;
151
152 pop = gemtk_obj_get_tree( POP_CTX );
153 if (pop == NULL)
154 return;
155 ctx = get_context_info(gw, x, y);
156
157 /*
158 Disable all items by default:
159 */
160 for( choice = POP_FIRST_ITEM; choice<=POP_LAST_ITEM; choice++ ){
161 SET_BIT(pop[ choice ].ob_state, OS_DISABLED, 1);
162 }
163
164 if( ctx->flags & CNT_INTERACTIVE ){
165 SET_BIT(pop[ POP_CTX_PASTE_SEL ].ob_state, OS_DISABLED, 0);
166 }
167
168 if( (ctx->flags & CNT_BROWSER) ){
169 SET_BIT(pop[ POP_CTX_SELECT_ALL ].ob_state, OS_DISABLED, 0);
170 SET_BIT(pop[ POP_CTX_COPY_SEL ].ob_state, OS_DISABLED, 0);
171 SET_BIT(pop[ POP_CTX_VIEW_SOURCE ].ob_state, OS_DISABLED, 0);
172 }
173
174 if( ctx->flags & CNT_HREF ){
175 SET_BIT(pop[ POP_CTX_COPY_LINK ].ob_state, OS_DISABLED, 0);
176 SET_BIT(pop[ POP_CTX_OPEN_NEW ].ob_state, OS_DISABLED, 0);
177 SET_BIT(pop[ POP_CTX_SAVE_LINK_AS ].ob_state, OS_DISABLED, 0);
178 }
179
180 if( ctx->flags & CNT_IMG ){
181 SET_BIT(pop[ POP_CTX_SAVE_AS ].ob_state, OS_DISABLED, 0);
182 SET_BIT(pop[ POP_CTX_COPY_URL ].ob_state, OS_DISABLED, 0);
183 SET_BIT(pop[ POP_CTX_OPEN_NEW ].ob_state, OS_DISABLED, 0);
184 }
185
186 // point mn_tree tree to states popup:
187 pop_menu.mn_tree = gemtk_obj_get_tree(POP_CTX);
188 pop_menu.mn_menu = 0;
189 pop_menu.mn_item = POP_CTX_CUT_SEL;
190 pop_menu.mn_scroll = SCROLL_NO;
191 pop_menu.mn_keystate = 0;
192
193 menu_popup(&pop_menu, x, y, &me_data);
194 choice = me_data.mn_item;
195
196 switch( choice ){
197 case POP_CTX_COPY_SEL:
199 break;
200
201 case POP_CTX_CUT_SEL:
203 break;
204
205 case POP_CTX_PASTE_SEL:
207 break;
208
209 case POP_CTX_SELECT_ALL:
211 break;
212
213 case POP_CTX_SAVE_AS:
214 if (ctx->ccdata.object != NULL) {
215 if( hlcache_handle_get_url(ctx->ccdata.object) != NULL ) {
217 gw->browser->bw,
221 NULL,
222 NULL,
223 NULL
224 );
225 }
226 }
227
228 case POP_CTX_SAVE_LINK_AS:
229 if (ctx->ccdata.link != NULL) {
230 nserror error;
231
233 gw->browser->bw,
234 ctx->ccdata.link,
237 NULL,
238 NULL,
239 NULL);
240
241 if (error != NSERROR_OK) {
243 }
244 }
245
246 break;
247
248 case POP_CTX_COPY_URL:
249 if ((ctx->flags & CNT_IMG) && (ctx->ccdata.object != NULL)) {
250 if( hlcache_handle_get_url(ctx->ccdata.object) != NULL ){
253 }
254 }
255 break;
256
257 case POP_CTX_COPY_LINK:
258 if ((ctx->flags & CNT_HREF) &&
259 (ctx->ccdata.link != NULL)) {
261 }
262 break;
263
264 case POP_CTX_OPEN_NEW:
265 if ((ctx->flags & CNT_HREF) &&
266 (ctx->ccdata.link != NULL)) {
267 nserror error;
268
269 error = browser_window_create(
271 ctx->ccdata.link,
273 gw->browser->bw,
274 NULL);
275 if (error != NSERROR_OK) {
277 }
278 }
279 break;
280
281 case POP_CTX_VIEW_SOURCE:
282 editor = nsoption_charp(atari_editor);
283 if (editor != NULL && strlen(editor)>0) {
285 &size);
286 if (size > 0 && data != NULL){
287 snprintf(tempfile, 127, "%s", get_tmpfilename("ns-", ".html"));
288 /* the GEMDOS cmdline contains the length of the commandline
289 in the first byte: */
290 cmdline[0] = (unsigned char)strlen(tempfile);
291 NSLOG(netsurf, INFO,
292 "Creating temporay source file: %s\n",
293 tempfile);
294 fp_tmpfile = fopen(tempfile, "w");
295 if (fp_tmpfile != NULL){
296 fwrite(data, size, 1, fp_tmpfile);
297 fclose(fp_tmpfile);
298
299 // Send SH_WDRAW to notify files changed:
300 gemtk_send_msg(SH_WDRAW, 0, -1, 0, 0, 0, 0);
301
302 // start application:
303 if(strlen(tempfile)<=125){
304 shel_write(1, 1, 1, editor, cmdline);
305 }
306 } else {
307 printf("Could not open temp file: %s!\n", tempfile );
308 }
309
310 } else {
311 NSLOG(netsurf, INFO,
312 "Invalid content!");
313 }
314 } else {
315 form_alert(0, "[1][Set option \"atari_editor\".][OK]");
316 }
317 break;
318
319 default: break;
320 }
321
322#undef POP_FIRST_ITEM
323#undef POP_LAST_ITEM
324
325}
int scrap_txt_write(char *str)
Definition: clipboard.c:50
#define CNT_IMG
Definition: ctxmenu.c:41
#define POP_FIRST_ITEM
#define CNT_INTERACTIVE
Definition: ctxmenu.c:40
#define CNT_HREF
Definition: ctxmenu.c:38
#define POP_LAST_ITEM
struct s_context_info ctxinfo
Definition: ctxmenu.c:50
#define CNT_SELECTION
Definition: ctxmenu.c:39
static char * get_tmpfilename(const char *prefix, const char *suffix)
Definition: ctxmenu.c:106
bool gui_window_get_scroll(struct gui_window *w, int *sx, int *sy)
Definition: gui.c:413
void context_popup(struct gui_window *gw, short x, short y)
Definition: ctxmenu.c:134
#define CNT_BROWSER
Definition: ctxmenu.c:37
static struct s_context_info * get_context_info(struct gui_window *gw, short mx, short my)
Definition: ctxmenu.c:52
#define PATH_MAX
Definition: gui.h:31
nserror atari_warn_user(const char *warning, const char *detail)
Warn the user of an event.
Definition: misc.c:56
Browser window creation and manipulation interface.
nserror browser_window_get_features(struct browser_window *bw, int x, int y, struct browser_window_features *data)
Get access to any page features at the given coordinates.
nserror browser_window_navigate(struct browser_window *bw, struct nsurl *url, struct nsurl *referrer, enum browser_window_nav_flags flags, char *post_urlenc, struct fetch_multipart_data *post_multipart, struct hlcache_handle *parent)
Start fetching a page in a browser window.
bool browser_window_has_content(struct browser_window *bw)
Find out if a browser window is currently showing a content.
struct nsurl * browser_window_access_url(const struct browser_window *bw)
Access a browser window's URL.
nserror browser_window_create(enum browser_window_create_flags flags, struct nsurl *url, struct nsurl *referrer, struct browser_window *existing, struct browser_window **bw)
Create and open a new root browser window with the given page.
struct hlcache_handle * browser_window_get_content(struct browser_window *bw)
Get a cache handle for the content within a browser window.
@ BW_CREATE_HISTORY
this will form a new history node (don't set for back/reload/etc)
@ BW_CREATE_CLONE
New gui_window to be clone of "existing" gui_window.
@ BW_NAVIGATE_DOWNLOAD
download rather than render the uri
@ CONTENT_IMAGE
All images.
Definition: content_type.h:67
@ CONTENT_HTML
content is HTML
Definition: content_type.h:58
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
Public content interface.
struct nsurl * hlcache_handle_get_url(const struct hlcache_handle *handle)
Retrieve the URL associated with a high level cache handle.
const uint8_t * content_get_source_data(struct hlcache_handle *h, size_t *size)
Retrieve source of content.
Definition: content.c:1209
content_type content_get_type(struct hlcache_handle *h)
Retrieve computed type of content.
Definition: content.c:1061
Interface to key press operations.
@ NS_KEY_SELECT_ALL
Definition: keypress.h:32
@ NS_KEY_PASTE
Definition: keypress.h:43
@ NS_KEY_COPY_SELECTION
Definition: keypress.h:33
@ NS_KEY_CUT_SELECTION
Definition: keypress.h:44
bool browser_window_key_press(struct browser_window *bw, uint32_t key)
Handle key presses in a browser window.
Definition: textinput.c:107
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
const char * messages_get_errorcode(nserror code)
lookup of a message by errorcode from the standard Messages hash.
Definition: messages.c:248
Localised message support (interface).
NetSurf URL handling (interface).
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
void window_get_grect(ROOTWIN *rootwin, enum browser_area_e which, GRECT *d)
Definition: rootwin.c:601
@ BROWSER_AREA_CONTENT
Definition: rootwin.h:34
Page features at a specific spatial location.
struct hlcache_handle * object
Object at position or NULL.
enum browser_window_features::@56 form_features
type of form feature.
struct nsurl * link
URL of a link or NULL.
Browser window data.
int x
Window dimensions.
struct browser_window * bw
first entry in window list
Definition: gui.c:296
struct s_gui_win_root * root
Definition: gui.h:148
struct s_browser * browser
Definition: gui.h:149
High-level cache handle.
Definition: hlcache.c:66
struct browser_window * bw
Definition: gui.h:137
struct browser_window_features ccdata
Definition: ctxmenu.c:47
unsigned long flags
Definition: ctxmenu.c:46
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:297