Commit b0534ef40082259cf324a148937ad71efe8118c5

Authored by Perry Werneck
1 parent 9ad7fc96

Improving cut&paste methods.

@@ -220,6 +220,9 @@ @@ -220,6 +220,9 @@
220 <Unit filename="src/lib3270/selection/actions.c"> 220 <Unit filename="src/lib3270/selection/actions.c">
221 <Option compilerVar="CC" /> 221 <Option compilerVar="CC" />
222 </Unit> 222 </Unit>
  223 + <Unit filename="src/lib3270/selection/get.c">
  224 + <Option compilerVar="CC" />
  225 + </Unit>
223 <Unit filename="src/lib3270/selection/selection.c"> 226 <Unit filename="src/lib3270/selection/selection.c">
224 <Option compilerVar="CC" /> 227 <Option compilerVar="CC" />
225 </Unit> 228 </Unit>
src/include/lib3270.h
@@ -1040,7 +1040,7 @@ @@ -1040,7 +1040,7 @@
1040 * @return selected text if available, or NULL. Release it with free() 1040 * @return selected text if available, or NULL. Release it with free()
1041 * 1041 *
1042 */ 1042 */
1043 - LIB3270_EXPORT char * lib3270_get_selected(H3270 *h); 1043 + LIB3270_EXPORT char * lib3270_get_selected(H3270 *hSession);
1044 1044
1045 LIB3270_EXPORT char * lib3270_cut_selected(H3270 *hSession); 1045 LIB3270_EXPORT char * lib3270_cut_selected(H3270 *hSession);
1046 1046
src/include/lib3270/selection.h
@@ -24,11 +24,16 @@ @@ -24,11 +24,16 @@
24 * 24 *
25 * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) 25 * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
26 * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) 26 * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
27 - * licinio@bb.com.br (Licínio Luis Branco)  
28 - * kraucer@bb.com.br (Kraucer Fernandes Mazuco)  
29 * 27 *
30 */ 28 */
31 29
  30 + /**
  31 + * @brief LIB3270 calls for managing selected area.
  32 + *
  33 + * @author perry.werneck@gmail.com
  34 + *
  35 + */
  36 +
32 #ifndef LIB3270_SELECTION_H_INCLUDED 37 #ifndef LIB3270_SELECTION_H_INCLUDED
33 38
34 #define LIB3270_SELECTION_H_INCLUDED 1 39 #define LIB3270_SELECTION_H_INCLUDED 1
@@ -41,7 +46,21 @@ @@ -41,7 +46,21 @@
41 LIB3270_EXPORT int lib3270_select_all(H3270 *session); 46 LIB3270_EXPORT int lib3270_select_all(H3270 *session);
42 47
43 /** 48 /**
44 - * "Paste" supplied string. 49 + * @brief Get selection options.
  50 + *
  51 + * @see lib3270_get_selection
  52 + *
  53 + */
  54 + typedef enum _LIB3270_SELECTION_OPTIONS {
  55 +
  56 + LIB3270_SELECTION_CUT = 0x0001, ///< @brief Cut selected data (if available).
  57 + LIB3270_SELECTION_ALL = 0x0002, ///< @brief Get all data (the default is get only selected data).
  58 + LIB3270_SELECTION_UNPROTECTED_ONLY = 0x0004, ///< @brief Get only unprotected contents.
  59 +
  60 + } LIB3270_SELECTION_OPTIONS;
  61 +
  62 + /**
  63 + * @brief "Paste" supplied string.
45 * 64 *
46 * @param h Session handle. 65 * @param h Session handle.
47 * @param str String to paste. 66 * @param str String to paste.
@@ -54,7 +73,7 @@ @@ -54,7 +73,7 @@
54 LIB3270_EXPORT int lib3270_paste(H3270 *h, const unsigned char *str); 73 LIB3270_EXPORT int lib3270_paste(H3270 *h, const unsigned char *str);
55 74
56 /** 75 /**
57 - * Paste remaining string. 76 + * @brief Paste remaining string.
58 * 77 *
59 * @param h Session handle. 78 * @param h Session handle.
60 * 79 *
@@ -66,7 +85,7 @@ @@ -66,7 +85,7 @@
66 LIB3270_EXPORT int lib3270_pastenext(H3270 *h); 85 LIB3270_EXPORT int lib3270_pastenext(H3270 *h);
67 86
68 /** 87 /**
69 - * Move selected box 1 char in the selected direction. 88 + * @brief Move selected box 1 char in the selected direction.
70 * 89 *
71 * @param h Session handle. 90 * @param h Session handle.
72 * @param dir Direction to move 91 * @param dir Direction to move
@@ -76,7 +95,7 @@ @@ -76,7 +95,7 @@
76 LIB3270_EXPORT int lib3270_move_selection(H3270 *h, LIB3270_DIRECTION dir); 95 LIB3270_EXPORT int lib3270_move_selection(H3270 *h, LIB3270_DIRECTION dir);
77 96
78 /** 97 /**
79 - * Move selected box. 98 + * @brief Move selected box.
80 * 99 *
81 * @param h Session handle. 100 * @param h Session handle.
82 * @param from Address of origin position inside the selected buffer. 101 * @param from Address of origin position inside the selected buffer.
@@ -88,7 +107,7 @@ @@ -88,7 +107,7 @@
88 LIB3270_EXPORT int lib3270_move_selected_area(H3270 *h, int from, int to); 107 LIB3270_EXPORT int lib3270_move_selected_area(H3270 *h, int from, int to);
89 108
90 /** 109 /**
91 - * Drag selected region. 110 + * @brief Drag selected region.
92 * 111 *
93 * Move or resize selected box according to the selection flags. 112 * Move or resize selected box according to the selection flags.
94 * 113 *
@@ -103,7 +122,7 @@ @@ -103,7 +122,7 @@
103 LIB3270_EXPORT int lib3270_drag_selection(H3270 *h, unsigned char flag, int origin, int baddr); 122 LIB3270_EXPORT int lib3270_drag_selection(H3270 *h, unsigned char flag, int origin, int baddr);
104 123
105 /** 124 /**
106 - * Gets the selected range of characters in the screen 125 + * @brief Gets the selected range of characters in the screen
107 * 126 *
108 * @param h Session handle. 127 * @param h Session handle.
109 * @param start return location for start of selection, as a character offset. 128 * @param start return location for start of selection, as a character offset.
@@ -115,7 +134,7 @@ @@ -115,7 +134,7 @@
115 LIB3270_EXPORT int lib3270_get_selection_bounds(H3270 *hSession, int *start, int *end); 134 LIB3270_EXPORT int lib3270_get_selection_bounds(H3270 *hSession, int *start, int *end);
116 135
117 /** 136 /**
118 - * Get bitmasked flag for the current selection. 137 + * @brief Get bitmasked flag for the current selection.
119 * 138 *
120 * Calculate flags to help drawing of the correct mouse pointer over a selection. 139 * Calculate flags to help drawing of the correct mouse pointer over a selection.
121 * 140 *
@@ -127,7 +146,7 @@ @@ -127,7 +146,7 @@
127 LIB3270_EXPORT unsigned char lib3270_get_selection_flags(H3270 *h, int baddr); 146 LIB3270_EXPORT unsigned char lib3270_get_selection_flags(H3270 *h, int baddr);
128 147
129 /** 148 /**
130 - * Get a string from required region. 149 + * @brief Get a string from required region.
131 * 150 *
132 * @param h Session handle. 151 * @param h Session handle.
133 * @param start_pos First char to get. 152 * @param start_pos First char to get.
@@ -141,7 +160,7 @@ @@ -141,7 +160,7 @@
141 160
142 161
143 /** 162 /**
144 - * Selects a range of characters in the screen. 163 + * @brief Selects a range of characters in the screen.
145 * 164 *
146 * @param h Session handle. 165 * @param h Session handle.
147 * @param start_offset Start offset. 166 * @param start_offset Start offset.
src/lib3270/private.h
@@ -670,7 +670,7 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession); @@ -670,7 +670,7 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession);
670 670
671 LIB3270_INTERNAL int lib3270_default_event_dispatcher(H3270 *hSession, int block); 671 LIB3270_INTERNAL int lib3270_default_event_dispatcher(H3270 *hSession, int block);
672 672
673 -LIB3270_INTERNAL void do_select(H3270 *h, int start, int end, int rect); 673 +LIB3270_INTERNAL void do_select(H3270 *h, unsigned int start, unsigned int end, unsigned int rect);
674 674
675 675
676 /** 676 /**
src/lib3270/selection/get.c 0 → 100644
@@ -0,0 +1,144 @@ @@ -0,0 +1,144 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como - e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + */
  29 +
  30 + #include "../private.h"
  31 + #include <lib3270.h>
  32 + #include <lib3270/session.h>
  33 + #include <lib3270/selection.h>
  34 + #include "3270ds.h"
  35 +
  36 + /*--[ Implement ]------------------------------------------------------------------------------------*/
  37 +
  38 +static void clear_chr(H3270 *hSession, int baddr)
  39 +{
  40 + hSession->text[baddr].chr = ' ';
  41 +
  42 + hSession->ea_buf[baddr].cc = EBC_null;
  43 + hSession->ea_buf[baddr].cs = 0;
  44 +
  45 + hSession->cbk.update( hSession,
  46 + baddr,
  47 + hSession->text[baddr].chr,
  48 + hSession->text[baddr].attr,
  49 + baddr == hSession->cursor_addr );
  50 +}
  51 +
  52 +LIB3270_EXPORT char * lib3270_get_selection(H3270 *hSession, char tok, LIB3270_SELECTION_OPTIONS options)
  53 +{
  54 + int row, col, baddr;
  55 + char * ret;
  56 + size_t buflen = (hSession->rows * (hSession->cols+1))+1;
  57 + size_t sz = 0;
  58 + unsigned short attr = 0xFFFF;
  59 + char cut = (options & LIB3270_SELECTION_CUT) != 0;
  60 + char all = (options & LIB3270_SELECTION_ALL) != 0;
  61 +
  62 + if(check_online_session(hSession))
  63 + return NULL;
  64 +
  65 + if(!hSession->selected || hSession->select.start == hSession->select.end)
  66 + return NULL;
  67 +
  68 + ret = lib3270_malloc(buflen);
  69 +
  70 + baddr = 0;
  71 + unsigned char fa = 0;
  72 +
  73 + for(row=0;row < ((int) hSession->rows);row++)
  74 + {
  75 + int cr = 0;
  76 +
  77 + for(col = 0; col < ((int) hSession->cols);col++)
  78 + {
  79 + if(hSession->ea_buf[baddr].fa) {
  80 + fa = hSession->ea_buf[baddr].fa;
  81 + }
  82 +
  83 + if(all || hSession->text[baddr].attr & LIB3270_ATTR_SELECTED)
  84 + {
  85 + if(tok && attr != hSession->text[baddr].attr)
  86 + {
  87 + attr = hSession->text[baddr].attr;
  88 + ret[sz++] = tok;
  89 + ret[sz++] = (attr & 0x0F);
  90 + ret[sz++] = ((attr & 0xF0) >> 4);
  91 +
  92 + }
  93 +
  94 + cr++;
  95 + ret[sz++] = hSession->text[baddr].chr;
  96 +
  97 + if(cut && !FA_IS_PROTECTED(fa)) {
  98 + clear_chr(hSession,baddr);
  99 + }
  100 +
  101 + }
  102 + baddr++;
  103 + }
  104 +
  105 + if(cr)
  106 + ret[sz++] = '\n';
  107 +
  108 + if((sz+10) > buflen)
  109 + {
  110 + buflen += 100;
  111 + ret = lib3270_realloc(ret,buflen);
  112 + }
  113 + }
  114 +
  115 + if(!sz)
  116 + {
  117 + lib3270_free(ret);
  118 + errno = ENOENT;
  119 + return NULL;
  120 + }
  121 + else if(sz > 1 && ret[sz-1] == '\n') // Remove ending \n
  122 + {
  123 + ret[sz-1] = 0;
  124 + }
  125 +
  126 + ret[sz++] = 0;
  127 +
  128 + if(sz != buflen)
  129 + ret = lib3270_realloc(ret,sz);
  130 +
  131 + return ret;
  132 +}
  133 +
  134 +LIB3270_EXPORT char * lib3270_get_selected(H3270 *hSession)
  135 +{
  136 + return lib3270_get_selection(hSession,0,0);
  137 +}
  138 +
  139 +LIB3270_EXPORT char * lib3270_cut_selected(H3270 *hSession)
  140 +{
  141 + return lib3270_get_selection(hSession,0,LIB3270_SELECTION_CUT);
  142 +}
  143 +
  144 +
src/lib3270/selection/selection.c
@@ -84,9 +84,9 @@ static void update_selected_rectangle(H3270 *session) @@ -84,9 +84,9 @@ static void update_selected_rectangle(H3270 *session)
84 84
85 // First remove unselected areas 85 // First remove unselected areas
86 baddr = 0; 86 baddr = 0;
87 - for(row=0;row < session->rows;row++) 87 + for(row=0;row < ((int) session->rows);row++)
88 { 88 {
89 - for(col = 0; col < session->cols;col++) 89 + for(col = 0; col < ((int) session->cols);col++)
90 { 90 {
91 if(!(row >= p[0].row && row <= p[1].row && col >= p[0].col && col <= p[1].col) && (session->text[baddr].attr & LIB3270_ATTR_SELECTED)) 91 if(!(row >= p[0].row && row <= p[1].row && col >= p[0].col && col <= p[1].col) && (session->text[baddr].attr & LIB3270_ATTR_SELECTED))
92 { 92 {
@@ -99,9 +99,9 @@ static void update_selected_rectangle(H3270 *session) @@ -99,9 +99,9 @@ static void update_selected_rectangle(H3270 *session)
99 99
100 // Then, draw selected ones 100 // Then, draw selected ones
101 baddr = 0; 101 baddr = 0;
102 - for(row=0;row < session->rows;row++) 102 + for(row=0;row < ((int) session->rows);row++)
103 { 103 {
104 - for(col = 0; col < session->cols;col++) 104 + for(col = 0; col < ((int) session->cols);col++)
105 { 105 {
106 if((row >= p[0].row && row <= p[1].row && col >= p[0].col && col <= p[1].col) && !(session->text[baddr].attr & LIB3270_ATTR_SELECTED)) 106 if((row >= p[0].row && row <= p[1].row && col >= p[0].col && col <= p[1].col) && !(session->text[baddr].attr & LIB3270_ATTR_SELECTED))
107 { 107 {
@@ -163,9 +163,9 @@ void toggle_rectselect(H3270 *session, struct lib3270_toggle GNUC_UNUSED(*t), LI @@ -163,9 +163,9 @@ void toggle_rectselect(H3270 *session, struct lib3270_toggle GNUC_UNUSED(*t), LI
163 update_selected_region(session); 163 update_selected_region(session);
164 } 164 }
165 165
166 -void do_select(H3270 *h, int start, int end, int rect) 166 +void do_select(H3270 *h, unsigned int start, unsigned int end, unsigned int rect)
167 { 167 {
168 - if(start < 0 || end > (h->rows * h->cols)) 168 + if(end > (h->rows * h->cols))
169 return; 169 return;
170 170
171 // Do we really need to change selection? 171 // Do we really need to change selection?
@@ -240,6 +240,7 @@ LIB3270_EXPORT unsigned char lib3270_get_selection_flags(H3270 *hSession, int ba @@ -240,6 +240,7 @@ LIB3270_EXPORT unsigned char lib3270_get_selection_flags(H3270 *hSession, int ba
240 return rc; 240 return rc;
241 } 241 }
242 242
  243 +/*
243 static void clear_chr(H3270 *hSession, int baddr) 244 static void clear_chr(H3270 *hSession, int baddr)
244 { 245 {
245 hSession->text[baddr].chr = ' '; 246 hSession->text[baddr].chr = ' ';
@@ -254,7 +255,7 @@ static void clear_chr(H3270 *hSession, int baddr) @@ -254,7 +255,7 @@ static void clear_chr(H3270 *hSession, int baddr)
254 baddr == hSession->cursor_addr ); 255 baddr == hSession->cursor_addr );
255 } 256 }
256 257
257 -static char * get_text(H3270 *hSession,unsigned char all, unsigned char tok, Boolean cut) 258 +static char * get_text(H3270 *hSession, unsigned char all, char tok, unsigned char cut)
258 { 259 {
259 int row, col, baddr; 260 int row, col, baddr;
260 char * ret; 261 char * ret;
@@ -333,6 +334,7 @@ static char * get_text(H3270 *hSession,unsigned char all, unsigned char tok, Boo @@ -333,6 +334,7 @@ static char * get_text(H3270 *hSession,unsigned char all, unsigned char tok, Boo
333 334
334 return ret; 335 return ret;
335 } 336 }
  337 +*/
336 338
337 LIB3270_EXPORT char * lib3270_get_region(H3270 *h, int start_pos, int end_pos, unsigned char all) 339 LIB3270_EXPORT char * lib3270_get_region(H3270 *h, int start_pos, int end_pos, unsigned char all)
338 { 340 {
@@ -341,9 +343,7 @@ LIB3270_EXPORT char * lib3270_get_region(H3270 *h, int start_pos, int end_pos, u @@ -341,9 +343,7 @@ LIB3270_EXPORT char * lib3270_get_region(H3270 *h, int start_pos, int end_pos, u
341 int sz = 0; 343 int sz = 0;
342 int baddr; 344 int baddr;
343 345
344 - CHECK_SESSION_HANDLE(h);  
345 -  
346 - if(!lib3270_connected(h)) 346 + if(check_online_session(h))
347 return NULL; 347 return NULL;
348 348
349 maxlen = h->rows * (h->cols+1); 349 maxlen = h->rows * (h->cols+1);
@@ -470,14 +470,3 @@ LIB3270_EXPORT int lib3270_has_selection(H3270 *hSession) @@ -470,14 +470,3 @@ LIB3270_EXPORT int lib3270_has_selection(H3270 *hSession)
470 470
471 return hSession->selected != 0; 471 return hSession->selected != 0;
472 } 472 }
473 -  
474 -LIB3270_EXPORT char * lib3270_get_selected(H3270 *hSession)  
475 -{  
476 - return get_text(hSession,0,0,0);  
477 -}  
478 -  
479 -LIB3270_EXPORT char * lib3270_cut_selected(H3270 *hSession)  
480 -{  
481 - return get_text(hSession,0,0,1);  
482 -}  
483 -