1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
|
/*
Copyright (C) 2002 Paul Wilkins
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* lcd.c by Paul Wilkins 3/22/97 */
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "lcd.h"
#include "mode.h"
#include "number.h"
#include "stack.h"
#include "process_input.h"
#include "editor.h"
#include "error.h"
#include "undo.h"
#define FONT1 "12x24"
#define FONT2 "-*-courier-medium-r-*-*-24-*"
#define FONT3 "-misc-fixed-medium-r-normal-*-20-*"
#define FONT4 "10x20"
#define FONT5 "-*-courier-medium-r-*-*-12-*"
#define FONT6 "fixed"
/* the amount of spacing we want around the display */
#define BORDER 2
GtkWidget *lcdDA = NULL;
char **lcdText = NULL;
GdkGC *lcdGC = 0;
GdkGC *lcdHighlightGC = 0;
/* the last area highlighted */
int dataSelected = 0;
int hiX0, hiX1, hiY1;
int hiY0, hiX2, hiY2;
/* font stuff */
GdkFont *lcdFont;
int fontW = 0;
int fontH = 0;
int fontD = 0;
int lcdWidth = 0;
int lcdHeight = 0;
int lcdDisplayMode = LONG_DISPLAY;
int lcdInitalized = 0;
/* some callbacks */
static gint lcdExposeCB(GtkWidget *widget, GdkEventExpose *event);
static gint lcdResizeCB(GtkWidget *widget, GdkEventConfigure *event);
/* local functions */
void calcStackLCD();
void drawStackLCD();
/* this stuff for cut & paste */
#define START_SELECTING 0x1
#define NONE_SELECTED 0x2
#define DATA_SELECTED 0x4
static gint lcdButtonPressEvnt(GtkWidget *widget, GdkEventButton *event);
static gint lcdButtonReleaseEvnt(GtkWidget *widget, GdkEventButton *event);
static gint lcdMotionEvnt(GtkWidget *widget, GdkEventMotion *event);
static gint lcdKeyPressEvnt(GtkWidget *widget, GdkEventKey *event);
static gint lcdKeyReleaseEvnt(GtkWidget *widget, GdkEventKey *event);
void lcdPasteCB(GtkWidget *, GtkSelectionData *, gpointer);
gint loseSelection(GtkWidget *, GdkEventSelection *);
#ifdef GTK_VER_1_1
void convertSelection(GtkWidget *, GtkSelectionData *,
guint, guint, gpointer);
#else
void convertSelection(GtkWidget *, GtkSelectionData *,
gpointer);
#endif
void lcdGetSelection(int time);
void lcdStartHighlight(int x, int y, int time);
void lcdStopHighlight(int x, int y, int time);
void lcdContinueHighlight(int x, int y);
#define TARGET_STRING 0x1
#define TARGET_TEXT 0x2
#define TARGET_COMPOUND_TEXT 0x4
GtkWidget *setupLCD(GtkWidget *parent, int rows, int cols, char *font){
int i;
int width;
int wid, hgt;
#ifdef GTK_VER_1_1
static GtkTargetEntry targetlist[] = {
/* Target Flags Info */
{ "STRING", 0, TARGET_STRING },
{ "TEXT", 0, TARGET_TEXT },
{ "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT }
};
static gint ntargets = sizeof(targetlist) / sizeof(targetlist[0]);
#endif
/* store arguments */
lcdWidth = cols;
lcdHeight = rows;
/* get a font for the main window */
if(font != NULL){
if(NULL == (lcdFont = gdk_font_load(font))){
fprintf(stderr, "Unable to load font %s.\n", font);
exit(0);
}
} else if(NULL == (lcdFont = gdk_font_load(FONT1)) &&
NULL == (lcdFont = gdk_font_load(FONT2)) &&
NULL == (lcdFont = gdk_font_load(FONT3)) &&
NULL == (lcdFont = gdk_font_load(FONT4)) &&
NULL == (lcdFont = gdk_font_load(FONT5)) &&
NULL == (lcdFont = gdk_font_load(FONT6))){
fprintf(stderr, "Unable to load a font.\n");
exit(0);
}
/* find max font width */
for(i=0; i<256; i++){
width = gdk_char_width(lcdFont, (gchar)i);
if(width < 50 && width > fontW) fontW = width;
}
/* globals we use all over the place */
fontH = lcdFont->ascent + lcdFont->descent;
fontD = lcdFont->descent;
if(fontW == 0 || fontH == 0){
fprintf(stderr, "Error: can not determine font dimentions.\n");
exit(0);
}
wid = (2 * BORDER) + (lcdWidth * fontW);
hgt = (2 * BORDER) + (lcdHeight * fontH);
lcdDA = gtk_drawing_area_new();
gtk_drawing_area_size(GTK_DRAWING_AREA(lcdDA), wid, hgt);
gtk_box_pack_start(GTK_BOX(parent), lcdDA, TRUE, TRUE, 0);
gtk_widget_show(lcdDA);
/* Signals used to handle window ops */
gtk_signal_connect(GTK_OBJECT(lcdDA), "expose_event",
(GtkSignalFunc)lcdExposeCB, NULL);
gtk_signal_connect(GTK_OBJECT(lcdDA),"configure_event",
(GtkSignalFunc)lcdResizeCB, NULL);
/* Event signals (Selection) */
gtk_signal_connect(GTK_OBJECT(lcdDA), "selection_received",
(GtkSignalFunc)lcdPasteCB, NULL);
gtk_signal_connect(GTK_OBJECT(lcdDA), "selection_clear_event",
(GtkSignalFunc)loseSelection, NULL);
/*
gtk_signal_connect(GTK_OBJECT(lcdDA), "selection_request_event",
(GtkSignalFunc)convertSelection, NULL);
*/
#ifdef GTK_VER_1_1
gtk_selection_add_targets(lcdDA,
GDK_SELECTION_PRIMARY, targetlist, ntargets);
gtk_signal_connect(GTK_OBJECT(lcdDA), "selection_get",
(GtkSignalFunc)convertSelection, NULL);
#else
gtk_selection_add_handler(lcdDA, GDK_SELECTION_PRIMARY,
GDK_SELECTION_TYPE_STRING, convertSelection, NULL);
#endif
/* Event signals (Input) */
gtk_signal_connect(GTK_OBJECT(lcdDA), "motion_notify_event",
(GtkSignalFunc)lcdMotionEvnt, NULL);
gtk_signal_connect(GTK_OBJECT(lcdDA), "button_press_event",
(GtkSignalFunc)lcdButtonPressEvnt, NULL);
gtk_signal_connect(GTK_OBJECT(lcdDA), "button_release_event",
(GtkSignalFunc)lcdButtonReleaseEvnt, NULL);
gtk_signal_connect_after(GTK_OBJECT(lcdDA), "key_press_event",
(GtkSignalFunc)lcdKeyPressEvnt, NULL);
gtk_signal_connect_after(GTK_OBJECT(lcdDA), "key_release_event",
(GtkSignalFunc)lcdKeyReleaseEvnt, NULL);
gtk_widget_set_events(lcdDA,
GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK |
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
GTK_WIDGET_SET_FLAGS(lcdDA, GTK_CAN_FOCUS);
gtk_widget_grab_focus(lcdDA);
return lcdDA;
}
void clearLCD(){
/* clear the stack */
clearStack();
/* reset any error string */
resetError();
/* clear the entry buffer */
cancelEditor();
/* re-initialize (and clear) the undo system */
initUndoSystem();
/* redraw the window */
calcStackLCD();
drawStackLCD();
}
void clearLCDwindow(){
gdk_window_clear(lcdDA->window);
}
/* Recalculate the contents of the lcd display.
* note: we don't redraw the lcd here, that is done by drawStackLCD().
*/
void calcStackLCD(){
int i, j;
int strLen, pLen, curPos;
char *c, *p;
char *txt; /* the number */
char label[16]; /* the stack number label */
int height;
int indx;
int row; /* the row to start print the current number */
int txtPos; /* which col do we start printing in */
int lines; /* the number of lines this number has */
int top; /* which row is the top for drawing the stack */
int bottom; /* which row is the current bottom for drawing the stack */
int labelLen; /* the stack number label length */
Number *num;
if(isEditingEditor()){
height = lcdHeight - 1;
} else {
height = lcdHeight;
}
if(height <= 0) return;
/* clear the old display */
for(i=0; i<lcdHeight; i++){
for(j=0; j<lcdWidth; j++){
lcdText[i][j] = ' ';
}
}
/* check for an error */
if(isError()){
top = 1;
c = getStringError();
strLen = strlen(c);
if(strLen > lcdWidth) strLen = lcdWidth;
strncpy(lcdText[0], c, strLen);
} else {
top = 0;
}
bottom = height;
/* print all of the numbers */
for(indx=0; indx<stackLen(); indx++){
/* get the number to print */
if(NULL == (num = getStackEle(indx))) break;
/* get the text of the number */
if(indx == 0 || lcdDisplayMode == LONG_DISPLAY){
if(NULL == (txt = printNumber(num))) break;
} else {
if(NULL == (txt = printNumberShort(num))) break;
}
/* how many lines is this number? */
lines = 1;
for(p=txt; *p!='\0'; p++) if(*p=='\n'){ lines++; *p='\0'; }
/* find the starting line of the number */
row = bottom - lines;
if(row < top) row = top;
/* print the stack number label */
sprintf(label, "%d: ", indx+1);
labelLen = strlen(label);
if(labelLen > lcdWidth) labelLen = lcdWidth;
strncpy(lcdText[row], label, labelLen);
/* print the number */
p = txt;
for(j=0, i=row; i<bottom && j<lines; i++, j++){
pLen = strlen(p);
txtPos = lcdWidth - pLen;
if(txtPos < labelLen) txtPos = labelLen;
if(txtPos > lcdWidth) break;
strncpy(&(lcdText[i][txtPos]), p, lcdWidth-txtPos);
p = p + pLen + 1;
}
/* free the mem used to print the number */
free(txt);
bottom = bottom - j;
if(bottom == top) break;
}
/* fill in the rest of the indexes */
for(i=indx, j=bottom; j>top; i++, j--){
sprintf(label, "%d: ", i+1);
labelLen = strlen(label);
if(labelLen > lcdWidth) labelLen = lcdWidth;
strncpy(lcdText[j-1], label, labelLen);
}
if(isEditingEditor()){
txt = getLineEditor();
curPos = cursorPosEditor();
if(curPos > lcdWidth){
txt += (curPos - lcdWidth);
}
strLen = strlen(txt);
if(strLen > lcdWidth) strLen = lcdWidth;
strncpy(lcdText[lcdHeight-1], txt, strLen);
}
}
/* Redraw the contents of the lcd display.
* note: start BORDER pixels down and to the right.
* TODO: we could be smart and only redraw what has changed.
* This does this work correctly with varriable sized fonts.
*/
void drawStackLCD(){
int i;
int curPos;
int strt, stop;
GdkGC *drawgc;
/* draw the stack */
for(i=0; i<lcdHeight; i++){
/* draw the first unhighlighted section of the line */
strt = 0;
if(dataSelected == DATA_SELECTED && i>=hiY1 && i<=hiY2){
stop = hiX1;
if(stop > lcdWidth) stop = lcdWidth;
} else {
stop = lcdWidth;
}
if(stop-strt){
gdk_window_clear_area(lcdDA->window,
strt*fontW + BORDER, i*fontH + BORDER,
fontW*(stop-strt), fontH);
gdk_draw_text(lcdDA->window, lcdFont, lcdGC,
strt*fontW + BORDER, (i+1)*fontH - fontD + BORDER,
&lcdText[i][strt], stop-strt);
}
/* draw the highlighted section of the line */
if(dataSelected == DATA_SELECTED && i>=hiY1 && i<=hiY2){
strt = hiX1;
if(strt > lcdWidth) strt = lcdWidth;
stop = hiX2+1;
if(stop > lcdWidth) stop = lcdWidth;
if(stop-strt){
gdk_draw_rectangle(lcdDA->window, lcdGC, TRUE,
strt*fontW + BORDER, i*fontH + BORDER,
fontW*(stop-strt), fontH);
gdk_draw_text(lcdDA->window, lcdFont, lcdHighlightGC,
strt*fontW + BORDER, (i+1)*fontH - fontD + BORDER,
&lcdText[i][strt], stop-strt);
}
}
/* draw the last unhighlighted section of the line */
stop = lcdWidth;
if(dataSelected == DATA_SELECTED && i>=hiY1 && i<=hiY2){
strt = hiX2+1;
if(strt > lcdWidth) strt = lcdWidth;
} else {
strt = lcdWidth;
}
if(stop-strt){
gdk_window_clear_area(lcdDA->window,
strt*fontW + BORDER, i*fontH + BORDER,
fontW*(stop-strt), fontH);
gdk_draw_text(lcdDA->window, lcdFont, lcdGC,
strt*fontW + BORDER, (i+1)*fontH - fontD + BORDER,
&lcdText[i][strt], stop-strt);
}
}
/* draw the cursor */
if(isEditingEditor()){
curPos = cursorPosEditor();
if(curPos > lcdWidth){
curPos = lcdWidth;
}
if(dataSelected == DATA_SELECTED &&
curPos>=hiX1 && curPos<=hiX2 &&
lcdHeight-1>=hiY1 && lcdHeight-1<=hiY2)
{
drawgc = lcdHighlightGC;
} else {
drawgc = lcdGC;
}
/* hack: draw a white line to erase any old cursor
in position 0. XDrawImageString dosen't cover this
up like it does with an old cursor in any other position */
if(!(dataSelected == DATA_SELECTED &&
0==hiX1 && lcdHeight-1>=hiY1 && lcdHeight-1<=hiY2))
{
gdk_draw_line(lcdDA->window, lcdHighlightGC,
BORDER - 1, (lcdHeight-1) * fontH + BORDER,
BORDER - 1, lcdHeight * fontH - 1 + BORDER);
}
/* draw the cursor */
gdk_draw_line(lcdDA->window, drawgc,
fontW * curPos - 1 + BORDER, (lcdHeight-1) * fontH + BORDER,
fontW * curPos - 1 + BORDER, lcdHeight * fontH - 1 + BORDER);
}
}
/* redraw what we need to */
void redrawLCD(){
calcStackLCD();
drawStackLCD();
}
void lcdResize(){
int i;
char *buf;
int width, height;
width = lcdDA->allocation.width;
height = lcdDA->allocation.height;
lcdWidth = (width - 2*BORDER) / fontW;
if(lcdWidth < 0) lcdWidth = 0;
lcdHeight = (height - 2*BORDER) / fontH;
if(lcdHeight < 0) lcdHeight = 0;
/* free the old mem */
if(lcdText){
free(lcdText[0]);
free(lcdText);
}
/* get the new mem. note: add 1 bec size could otherwize be 0. */
if(NULL == (lcdText=(char**)malloc((1+lcdHeight)*sizeof(char *)))){
perror("malloc");
exit(0);
}
if(NULL == (buf=(char*)malloc(1+lcdHeight*lcdWidth*sizeof(char)))){
perror("malloc");
exit(0);
}
lcdText[0] = buf; /* in case lcdHeight == 0 */
for(i=0; i<lcdHeight; i++){
lcdText[i] = &buf[lcdWidth * i];
}
/* we don't get an expose if the window shrinks */
clearLCDwindow();
calcStackLCD();
drawStackLCD();
}
void setLCDDispMode(int val){
if(val == LONG_DISPLAY) lcdDisplayMode = LONG_DISPLAY;
else if(val == SHORT_DISPLAY) lcdDisplayMode = SHORT_DISPLAY;
else printf("setShortLCD invalid mode\n");
}
int getLCDDispMode(){
return lcdDisplayMode;
}
static gint lcdExposeCB(GtkWidget *widget, GdkEventExpose *event){
GtkStyle *style;
if(lcdInitalized == 0){
style = gtk_widget_get_style(widget);
/* Set the default Graphics Contexts */
/* highlight swaps foreground and background */
lcdGC = style->fg_gc[GTK_STATE_NORMAL];
lcdHighlightGC = style->bg_gc[GTK_STATE_NORMAL];
lcdResize();
lcdInitalized = 1;
}
clearLCDwindow();
drawStackLCD();
return FALSE;
}
static gint lcdResizeCB(GtkWidget *widget, GdkEventConfigure *event){
if(lcdInitalized != 0) lcdResize();
return TRUE;
}
static gint lcdKeyPressEvnt(GtkWidget *widget, GdkEventKey *event){
char c;
if(event->string) c = *(event->string);
else c = 'd'; /* just pick something */
processInput(
event->keyval,
event->state & GDK_CONTROL_MASK ? 1 : 0,
c,
'd');
return TRUE;
}
static gint lcdKeyReleaseEvnt(GtkWidget *widget, GdkEventKey *event){
return TRUE;
}
static gint lcdButtonPressEvnt(GtkWidget *widget, GdkEventButton *event) {
gtk_widget_grab_focus(lcdDA);
if(event->button == 1){
lcdStartHighlight(event->x, event->y, event->time);
}
}
static gint lcdButtonReleaseEvnt(GtkWidget *widget, GdkEventButton *event) {
switch(event->button){
case 1: /* stop highlighting an area */
lcdStopHighlight(event->x, event->y, event->time);
break;
case 2: /* paste data */
lcdGetSelection(event->time);
break;
case 3: /* might be used to continue highlighting */
if(dataSelected == DATA_SELECTED){
lcdContinueHighlight(event->x, event->y);
lcdStopHighlight(event->x, event->y, event->time);
}
break;
}
}
static gint lcdMotionEvnt(GtkWidget *widget, GdkEventMotion *event)
{
int x, y;
GdkModifierType state;
if(event->is_hint){
gdk_window_get_pointer(event->window, &x, &y, &state);
} else {
x = event->x;
y = event->y;
state = event->state;
}
if(state & GDK_BUTTON1_MASK){
/* highlighting with button 1 */
lcdContinueHighlight(x, y);
} else if(state & GDK_BUTTON3_MASK &&
dataSelected == DATA_SELECTED){
/* continuing a highlight with button 3 */
lcdContinueHighlight(x, y);
}
return TRUE;
}
#ifdef GTK_VER_1_1
void convertSelection(
GtkWidget *widget,
GtkSelectionData *selection,
guint info,
guint time,
gpointer data)
#else
void convertSelection(
GtkWidget *widget,
GtkSelectionData *selection,
gpointer data)
#endif
{
int i;
int width, height;
char *str;
guchar *text;
gint len;
gint format;
GdkAtom type;
/* no highlight */
if(dataSelected != DATA_SELECTED){
printf("convertSelection called, but no selection.\n");
} else {
width = hiX2 - hiX1 + 1;
height = hiY2 - hiY1 + 1;
len = (width + 1) * height;
str = (char *)malloc(len * sizeof(char));
for(i=0; i<height; i++){
strncpy(str+(i*(width+1)), &(lcdText[i+hiY1][hiX1]), width);
*(str+(i*(width+1))+width) = '\n';
}
str[len-1] = '\0';
#ifdef GTK_VER_1_1
switch(info){
#else
switch(selection->target){
#endif
case GDK_TARGET_STRING:
case TARGET_STRING:
gtk_selection_data_set(
selection,
GDK_SELECTION_TYPE_STRING,
8,
str,
len-1);
break;
case TARGET_TEXT:
case TARGET_COMPOUND_TEXT:
gdk_string_to_compound_text(str,
&type, &format, &text, &len);
gtk_selection_data_set(
selection,
type,
format,
text,
len);
gdk_free_compound_text(text);
break;
}
}
}
gint loseSelection(
GtkWidget *widget,
GdkEventSelection *event)
{
/* if there is a current (old) selection, then clear it */
if(dataSelected == DATA_SELECTED){
dataSelected = NONE_SELECTED;
drawStackLCD();
}
return TRUE;
}
void lcdStartHighlight(int x, int y, int time){
int xx, yy;
/* if there is a current (old) selection, then clear it */
if(dataSelected == DATA_SELECTED){
dataSelected = NONE_SELECTED;
drawStackLCD();
/* ... and tell GDK */
if(gdk_selection_owner_get(GDK_SELECTION_PRIMARY) == lcdDA->window){
gtk_selection_owner_set(NULL,
GDK_SELECTION_PRIMARY,
GDK_CURRENT_TIME);
}
}
xx = (x - BORDER) / fontW;
if(xx < 0) xx = 0;
if(xx >= lcdWidth) xx = lcdWidth - 1;
yy = (y - BORDER) / fontH;
if(yy < 0) yy = 0;
if(yy >= lcdHeight) yy = lcdHeight - 1;
hiX0 = hiX1 = hiX2 = xx;
hiY0 = hiY1 = hiY2 = yy;
dataSelected = START_SELECTING;
}
void lcdStopHighlight(int x, int y, int time){
int reCalc, curPos;
int xx, yy;
/* if the user started to select something, but didn't actually
select anything, then reset stuff */
if(dataSelected == START_SELECTING){
dataSelected = NONE_SELECTED;
/* user may have been positioning the edit cursor */
if(isEditingEditor()){
xx = (x - BORDER) / fontW;
if(xx < 0) xx = 0;
if(xx >= lcdWidth) xx = lcdWidth - 1;
yy = (y - BORDER) / fontH;
if(yy == lcdHeight-1){
curPos = cursorPosEditor();
if(curPos > lcdWidth){
reCalc = 1;
xx += (curPos - lcdWidth);
} else {
reCalc = 0;
}
setCursorPosEditor(xx);
/* need to recalc since cursor may have moved out of the
visable part of the editor */
if(reCalc) calcStackLCD();
}
}
/* clear any old highlights */
drawStackLCD();
}
/* if data is now selected */
if(dataSelected == DATA_SELECTED){
if(0 == gtk_selection_owner_set(
lcdDA,
GDK_SELECTION_PRIMARY,
GDK_CURRENT_TIME))
{
fprintf(stderr, "failed to gain selection\n");
dataSelected = NONE_SELECTED;
drawStackLCD();
}
}
}
void lcdContinueHighlight(int x, int y){
int xx, yy;
int lastx1, lastx2, lasty1, lasty2;
xx = (x - BORDER) / fontW;
if(xx < 0) xx = 0;
if(xx >= lcdWidth) xx = lcdWidth - 1;
yy = (y - BORDER) / fontH;
if(yy < 0) yy = 0;
if(yy >= lcdHeight) yy = lcdHeight - 1;
lastx1 = hiX1;
lastx2 = hiX2;
lasty1 = hiY1;
lasty2 = hiY2;
if(xx < hiX0){
hiX1 = xx;
hiX2 = hiX0 + 0;
} else {
hiX1 = hiX0;
hiX2 = xx + 0;
}
if(yy < hiY0){
hiY1 = yy;
hiY2 = hiY0 + 0;
} else {
hiY1 = hiY0;
hiY2 = yy + 0;
}
/* if the highlight area changed */
if(lastx1 != hiX1 || lastx2 != hiX2 || lasty1 != hiY1 || lasty2 != hiY2){
dataSelected = DATA_SELECTED;
/* repaint the display */
drawStackLCD();
}
}
/* ask for data */
void lcdGetSelection(int time){
/* And request the primary selection */
gtk_selection_convert(lcdDA,
GDK_SELECTION_PRIMARY, /* GdkAtom selection */
GDK_TARGET_STRING, /* GdkAtom target */
GDK_CURRENT_TIME); /* guint32 time */
}
void lcdPasteCB(
GtkWidget *widget,
GtkSelectionData *selection,
gpointer data)
{
int i;
char *str;
static GdkAtom clipboard_atom = GDK_NONE;
if(selection->length <= 0 ||
selection->type != GDK_SELECTION_TYPE_STRING){
/* printf("length is zero or did not get a string.\n"); */
if(selection->selection != GDK_SELECTION_PRIMARY) return;
/* selection failed. Try the clipboard now... */
if(clipboard_atom == GDK_NONE){
clipboard_atom = gdk_atom_intern("CLIPBOARD", FALSE);
}
/* ask for another selection, but this time from the clipboard */
gtk_selection_convert(lcdDA,
clipboard_atom, /* GdkAtom selection */
GDK_TARGET_STRING, /* GdkAtom target */
GDK_CURRENT_TIME); /* guint32 time */
return;
}
str = (char*)selection->data;
for(i=0; i<selection->length; i++){
/* TODO maybe do something like isalpha() here? */
processInput(0, 0, str[i], str[i]);
}
}
|