Esempio n. 1
0
static char* get_caret(Ihandle *n)
{
   if (type(n) == TEXT_ || (type(n) == LIST_ && iupCheck(n, "EDITBOX")==YES))
   {
     static char val[10];
     XmTextPosition caret;
     Widget w = (Widget)handle(n);
     if (type(n) == LIST_)
       w = (Widget)IupGetAttribute(n, "_IUPMOT_EDITBOX");

     caret = XmTextGetInsertionPosition(w);
     
     sprintf(val,"%ld",(long int)caret+1);
     return val;
   }
   else if (type(n) == MULTILINE_)
   {
        char *val = getBuffer();
        long int linText, colText;
        XmTextPosition caret = XmTextGetInsertionPosition((Widget)handle(n));
        char *str = XmTextGetString((Widget)handle(n));
        iupmotLincol( str, caret, &linText, &colText );
        sprintf(val, "%ld,%ld", linText, colText);
        XtFree(str);
        return (val);
   }
   else return NULL;
}
Esempio n. 2
0
void Browser::clearMark()
{
    unsigned long pos;

    if(!this->marker_char[0]) return;
    //
    // If the marker is on this page...
    //
    if( (this->marker_pos >= this->page_start) &&
	(this->marker_pos < this->page_start + this->page_size) )
    {
	pos = this->marker_pos - this->page_start;

	this->ignore_cb = True;
	XmTextReplace(this->text, pos, pos+1, this->marker_char);
	this->ignore_cb = False;
    }
    else
    {
	pos = XmTextGetInsertionPosition(this->text) - 1;
    }
    this->marker_pos = 0;
    this->marker_char[0] = '\0';
    this->updateOffsets(pos+1);
}
Esempio n. 3
0
File: CEdit.C Progetto: dhaley/dcp
void CEdit::ReplaceSel(LPCSTR lpszNewText)
{
    // Inserts the given text to replace the current selection
    // or at the insert cursor position if there is no selection.
    Boolean        has_selection = False;
    XmTextPosition left          = (XmTextPosition) 0 ;
    XmTextPosition right         = (XmTextPosition) 0 ;

    if (_xd_textwidget!=NULL)
    {
	if (XmIsText(_xd_textwidget))
	{
	    has_selection = XmTextGetSelectionPosition(_xd_textwidget, &left, &right);

	    if (!has_selection)
		left = right = XmTextGetInsertionPosition(_xd_textwidget);

	    XmTextReplace(_xd_textwidget, left, right, (char *)lpszNewText);
	}
	else if (XmIsTextField(_xd_textwidget))
	{
	    has_selection = XmTextFieldGetSelectionPosition(_xd_textwidget, &left, &right);

	    if (!has_selection)
		left = right = XmTextFieldGetInsertionPosition(_xd_textwidget);

	    XmTextFieldReplace(_xd_textwidget, left, right, (char *)lpszNewText);
	}
    }

}
Esempio n. 4
0
static char* motTextGetCaretPosAttrib(Ihandle* ih)
{
  XmTextPosition pos = XmTextGetInsertionPosition(ih->handle);
  char* str = iupStrGetMemory(50);
  sprintf(str, "%d", (int)pos);
  return str;
}
Esempio n. 5
0
void Browser::gotoByteStartOffsetCB(Widget w, XtPointer client, XtPointer )
{
    Browser *browser = (Browser *)client;
    XmTextPosition pos;

    theIBMApplication->setBusyCursor(TRUE);
    char *str = XmTextGetString(w);
    if(STRLEN(str) > 0)
    {
	pos = atoi(str);
    }
    else
    {
	theIBMApplication->setBusyCursor(FALSE);
	return;
    }
    char *text_str = XmTextGetString(browser->text);
    XmTextPosition current_pos = XmTextGetInsertionPosition(browser->text);
    long line_start = current_pos;

    //
    // Search backwards for the start of the line
    //
    while (line_start >= 0)
    {
	if(text_str[line_start] == '\n') break;
	line_start--;
    }
    line_start++;
	
    XmTextShowPosition(browser->text, line_start + pos);
    XmTextSetInsertionPosition(browser->text, line_start + pos);

    theIBMApplication->setBusyCursor(FALSE);
}
Esempio n. 6
0
static void templates_cb(
	Widget		w,
	XtPointer	cbd,
	XtPointer	cbs)
{
	TemplateCallbackData *cbdata = cbd;
	XmTextPosition start, end, after;

	if(!text_w) {
		char m[80];
		sprintf(m, "unexpected argument %p", cbdata);
		msg("template handler", m);
		return;
	}

	if(get_map_state(text_w) != IsViewable) {
		beep();
		return;
	}

	if(	XmTextGetSelectionPosition(text_w, &start, &end)
	&&	start < end) {
		XmTextReplace(text_w, start, end, *cbdata);
		XmTextClearSelection(text_w, CurrentTime);
	} else {
		start = XmTextGetInsertionPosition(text_w);
		XmTextInsert(text_w, start, *cbdata);
	}

	after = start + strlen (*cbdata);
	XmTextSetInsertionPosition(text_w, after);
	XmTextShowPosition(text_w, after);
	set_input_focus(text_w);
}
Esempio n. 7
0
void backward_characterAct(Widget, XEvent*, String*, Cardinal*)
{
    clear_isearch();
    XmTextPosition pos = XmTextGetInsertionPosition(gdb_w);
    if (pos > promptPosition)
	XmTextSetInsertionPosition(gdb_w, pos - 1);
}
Esempio n. 8
0
bool highlight_unbalanced_paren(void)
{
  /* if cursor is positioned at close paren, try to find reason for unbalanced expr and highlight it */
  int pos;
  bool success = true;
  pos = XmTextGetInsertionPosition(listener_text);
  if (pos > 2)
    {
      char *str;
      str = XmTextGetString(listener_text);
      if ((str[pos - 1] == ')') &&
	  ((str[pos - 2] != '\\') || (str[pos - 3] != '#')))
	{
	  int parens;
	  parens = find_matching_paren(str, 2, pos - 1, &paren_pos);
	  if (parens == 0)
	    {
	      XmTextSetHighlight(listener_text, paren_pos, paren_pos + 1, XmHIGHLIGHT_SELECTED);
	      flashes = 4;
	      XtAppAddTimeOut(MAIN_APP(ss),
			      (unsigned long)FLASH_TIME,
			      (XtTimerCallbackProc)flash_unbalanced_paren,
			      NULL);
	    }
	  else success = false;
	}
      if (str) XtFree(str);
    }
  return(success);
}
Esempio n. 9
0
void timestampCB (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs) {
    char buf[128];
    XmTextPosition curpos;
    Widget textw = (Widget) clientData;
    curpos = XmTextGetInsertionPosition (textw);
    sprintf (buf, "%s", theTime());
    XmTextInsert (textw, curpos, buf);
}
Esempio n. 10
0
/* Appends str to the command text */
static void AppendComText(String str)
{
  XmTextPosition old_pos;

  old_pos = XmTextGetInsertionPosition(appData->com_text);
  XmTextInsert(appData->com_text, old_pos, str);
  XmTextSetInsertionPosition(appData->com_text, old_pos + strlen(str));
}
Esempio n. 11
0
static int motTextSetClipboardAttrib(Ihandle *ih, const char *value)
{
  if (iupStrEqualNoCase(value, "COPY"))
  {
    char *str = XmTextGetSelection(ih->handle);
    if (!str) return 0;

    XmTextCopy(ih->handle, CurrentTime);

    /* do it also for the X clipboard */
    XStoreBytes(iupmot_display, str, strlen(str)+1);
    XtFree(str);
  }
  else if (iupStrEqualNoCase(value, "CUT"))
  {
    char *str = XmTextGetSelection(ih->handle);
    if (!str) return 0;

    /* disable callbacks */
    iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", "1");

    XmTextCut(ih->handle, CurrentTime);

    /* do it also for the X clipboard */
    XStoreBytes(iupmot_display, str, strlen(str)+1);
    XtFree(str);
    XmTextRemove(ih->handle);

    iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", NULL);
  }
  else if (iupStrEqualNoCase(value, "PASTE"))
  {
    int size;
    char* str = XFetchBytes(iupmot_display, &size);
    if (!str) return 0;

    /* disable callbacks */
    iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", "1");

    XmTextPaste(ih->handle); /* TODO: this could force 2 pastes, check in CDE */

    /* do it also for the X clipboard */
    XmTextRemove(ih->handle);
    XmTextInsert(ih->handle, XmTextGetInsertionPosition(ih->handle), str);
    XFree(str);

    iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", NULL);
  }
  else if (iupStrEqualNoCase(value, "CLEAR"))
  {
    /* disable callbacks */
    iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", "1");
    XmTextRemove(ih->handle);
    iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", NULL);
  }
  return 0;
}
Esempio n. 12
0
static int motTextSetInsertAttrib(Ihandle* ih, const char* value)
{
  if (!ih->handle)  /* do not do the action before map */
    return 0;
  if (!value)
    return 0;

  /* disable callbacks */
  ih->data->disable_callbacks = 1;
  XmTextRemove(ih->handle);
  XmTextInsert(ih->handle, XmTextGetInsertionPosition(ih->handle), (char*)value);
  ih->data->disable_callbacks = 0;

  return 0;
}
Esempio n. 13
0
static int motTextSetInsertAttrib(Ihandle* ih, const char* value)
{
  if (!ih->handle)  /* do not store the action before map */
    return 0;
  if (!value)
    return 0;

  /* disable callbacks */
  iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", "1");
  XmTextRemove(ih->handle);
  XmTextInsert(ih->handle, XmTextGetInsertionPosition(ih->handle), (char*)value);
  iupAttribSetStr(ih, "_IUPMOT_DISABLE_TEXT_CB", NULL);

  return 0;
}
Esempio n. 14
0
static void motTextKeyPressEvent(Widget w, Ihandle *ih, XKeyEvent *evt, Boolean *cont)
{
  Widget spinbox;

  *cont = True;
  iupmotKeyPressEvent(w, ih, (XEvent*)evt, cont);
  if (*cont == False)
    return;

  if (evt->state & ControlMask && !(state & Mod1Mask || state & Mod5Mask))   /* Ctrl but NOT Alt */
  {
    KeySym motcode = iupmotKeycodeToKeysym(evt);
    if (motcode == XK_c || motcode == XK_x || motcode == XK_v || motcode == XK_a)
    {
      ih->data->disable_callbacks = -1; /* let callbacks be processed in motTextSetClipboardAttrib */

      if (motcode == XK_c)
        motTextSetClipboardAttrib(ih, "COPY");
      else if (motcode == XK_x)
        motTextSetClipboardAttrib(ih, "CUT");
      else if (motcode == XK_v)
        motTextSetClipboardAttrib(ih, "PASTE");
      else if (motcode == XK_a)
        XmTextSetSelection(ih->handle, 0, XmTextGetLastPosition(ih->handle), CurrentTime);

      ih->data->disable_callbacks = 0;

      *cont = False; 
      return;
    }
  }

  spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (spinbox && XmIsSpinBox(spinbox))
  {
    KeySym motcode = iupmotKeycodeToKeysym(evt);
    if (motcode == XK_Left || motcode == XK_Right)
    {
      /* avoid spin increment using Left/Right arrows, 
         but must manually handle the new cursor position */
      XmTextPosition caret_pos = XmTextGetInsertionPosition(ih->handle);
      XmTextSetInsertionPosition(ih->handle, (motcode == XK_Left)? caret_pos-1: caret_pos+1);
      *cont = False; 
    }
  }
}
Esempio n. 15
0
void insert_source_argAct(Widget w, XEvent*, String*, Cardinal*)
{
    clear_isearch();

    string arg = source_arg->get_string();
    if (XmIsText(w)) {
	if (XmTextGetEditable(w)) {
	    XmTextPosition pos = XmTextGetInsertionPosition(w);
	    XmTextReplace(w, pos, pos, XMST(arg.chars()));
	}
    }
    else if (XmIsTextField(w)) {
	if (XmTextFieldGetEditable(w)) {
	    XmTextPosition pos = XmTextFieldGetInsertionPosition(w);
	    XmTextFieldReplace(w, pos, pos, XMST(arg.chars()));
	}
    }
}
Esempio n. 16
0
static char* motTextGetLineValueAttrib(Ihandle* ih)
{
  if (ih->data->is_multiline)
  {
    int lin, col, start, end;
    char* str = iupStrGetMemory(200);
    char *value = XmTextGetString(ih->handle);
    XmTextPosition pos = XmTextGetInsertionPosition(ih->handle);
    motTextGetLinColFromPosition(value, pos, &lin, &col);
    start = motTextSetLinColToPosition(value, lin, 1);
    end = motTextSetLinColToPosition(value, lin, 20000);
    XtFree(value);
    XmTextGetSubstring(ih->handle, start, end-start, 200, str);  /* do not include the EOL */
    return str;
  }
  else
    return motTextGetValueAttrib(ih);
}
Esempio n. 17
0
void Browser::placeMark()
{
    int i;
    int line_no;
    char mark_str[2];

    mark_str[0] = '\001';
    mark_str[1] = '\0';

    theIBMApplication->setBusyCursor(TRUE);
    char *str = XmTextGetString(this->text);

    this->clearMark();

    //
    // Calc where it should go
    //
    unsigned long pos = XmTextGetInsertionPosition(this->text);
    this->marker_pos = this->page_start + pos;

    line_no = 0;
    for(i = 0; i < pos; i++)
    {
	if(str[i] == '\n') line_no++;
    }
    this->marker_line_no = this->page_start_line_no + line_no;
    this->updateOffsets(pos);
    
    //
    // Save the marker character
    //
    this->marker_char[0] = str[pos];

    this->ignore_cb = True;
    XmTextReplace(this->text, pos, pos+1, mark_str);
    this->ignore_cb = False;

    XmTextSetInsertionPosition(this->text, pos);

    XtFree(str);
    theIBMApplication->setBusyCursor(FALSE);
}
Esempio n. 18
0
static char* motTextGetCaretAttrib(Ihandle* ih)
{
  XmTextPosition pos = XmTextGetInsertionPosition(ih->handle);

  if (ih->data->is_multiline)
  {
    int col, lin;

    char *value = XmTextGetString(ih->handle);
    motTextGetLinColFromPosition(value, pos, &lin, &col);
    XtFree(value);

    return iupStrReturnIntInt(lin, col, ',');
  }
  else
  {
    pos++; /* IUP starts at 1 */
    return iupStrReturnInt((int)pos);
  }
}
Esempio n. 19
0
void
includeFileCB( Widget w, XtPointer client_data, 
 		XtPointer call_data)
{
	char *file_data, *c_path;
	Widget widget;
	XmFileSelectionBoxCallbackStruct *res;
	XmTextPosition pos;
	
	res = (XmFileSelectionBoxCallbackStruct *) call_data; 
	c_path = XmStringUnparse(res->value, 
	    NULL, XmCHARSET_TEXT, XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL); 
	file_data = get_file_contents(w, "fileOpenCB", c_path);
	widget = get_document_text(w, "fileOpenCB");
	pos = XmTextGetInsertionPosition(widget);
	XmTextInsert(widget, pos, file_data);
	
	update_document_linecount(w, "includeFileCB");
	update_message(w, "fileOpenCB", "Included file.");
	
	XtFree(c_path);
	XtFree(file_data);
	XtUnmanageChild(w);
}
Esempio n. 20
0
static void motTextKeyPressEvent(Widget w, Ihandle *ih, XKeyEvent *evt, Boolean *cont)
{
  Widget spinbox;

  *cont = True;
  iupmotKeyPressEvent(w, ih, (XEvent*)evt, cont);
  if (*cont == False)
    return;

  if (evt->state & ControlMask)   /* Ctrl */
  {
    KeySym motcode = XKeycodeToKeysym(iupmot_display, evt->keycode, 0);
    if (motcode == XK_c)
      motTextSetClipboardAttrib(ih, "COPY");
    else if (motcode == XK_x)
      motTextSetClipboardAttrib(ih, "CUT");
    else if (motcode == XK_v)
      motTextSetClipboardAttrib(ih, "PASTE");
    else if (motcode == XK_a)
      XmTextSetSelection(ih->handle, 0, XmTextGetLastPosition(ih->handle), CurrentTime);
  }

  spinbox = (Widget)iupAttribGet(ih, "_IUP_EXTRAPARENT");
  if (spinbox && XmIsSpinBox(spinbox))
  {
    KeySym motcode = XKeycodeToKeysym(iupmot_display, evt->keycode, 0);
    if (motcode == XK_Left || motcode == XK_Right)
    {
      /* avoid spin increment using Left/Right arrows, 
         but must manually handle the new cursor position */
      XmTextPosition caret_pos = XmTextGetInsertionPosition(ih->handle);
      XmTextSetInsertionPosition(ih->handle, (motcode == XK_Left)? caret_pos-1: caret_pos+1);
      *cont = False; 
    }
  }
}
Esempio n. 21
0
static char* motTextGetCaretAttrib(Ihandle* ih)
{
  char* str = iupStrGetMemory(50);

  XmTextPosition pos = XmTextGetInsertionPosition(ih->handle);

  if (ih->data->is_multiline)
  {
    int col, lin;

    char *value = XmTextGetString(ih->handle);
    motTextGetLinColFromPosition(value, pos, &lin, &col);
    XtFree(value);

    sprintf(str, "%d,%d", lin, col);
  }
  else
  {
    pos++; /* IUP starts at 1 */
    sprintf(str, "%d", (int)pos);
  }

  return str;
}
Esempio n. 22
0
static int motTextSetClipboardAttrib(Ihandle *ih, const char *value)
{
  Boolean editable;
  XtVaGetValues(ih->handle, XmNeditable, &editable, NULL);
  
  /* NOTE: the functions XmTextCopy, XmTextPaste and XmTextCut did not work as expected.
    But using IupClipboard does not catch selections made in a terminal. */

  if (iupStrEqualNoCase(value, "COPY"))
  {
    Ihandle* clipboard;
    char *str = XmTextGetSelection(ih->handle);
    if (!str) 
      return 0;

    clipboard = IupClipboard();
    IupSetAttribute(clipboard, "TEXT", str);
    IupDestroy(clipboard);

    XtFree(str);
  }
  else if (iupStrEqualNoCase(value, "CUT"))
  {
    Ihandle* clipboard;
    char *str;

    if (!editable)
      return 0;

    str = XmTextGetSelection(ih->handle);
    if (!str) 
      return 0;

    clipboard = IupClipboard();
    IupSetAttribute(clipboard, "TEXT", str);
    IupDestroy(clipboard);

    XtFree(str);

    /* disable callbacks if not interactive */
    if (ih->data->disable_callbacks == -1)
      ih->data->disable_callbacks = 0;
    else
      ih->data->disable_callbacks = 1;
    XmTextRemove(ih->handle);
    ih->data->disable_callbacks = 0;
  }
  else if (iupStrEqualNoCase(value, "PASTE"))
  {
    Ihandle* clipboard;
    char *str;

    if (!editable)
      return 0;

    clipboard = IupClipboard();
    str = IupGetAttribute(clipboard, "TEXT");

    /* disable callbacks */
    /* disable callbacks if not interactive */
    if (ih->data->disable_callbacks == -1)
      ih->data->disable_callbacks = 0;
    else
      ih->data->disable_callbacks = 1;
    XmTextRemove(ih->handle);
    XmTextInsert(ih->handle, XmTextGetInsertionPosition(ih->handle), str);
    ih->data->disable_callbacks = 0;
  }
  else if (iupStrEqualNoCase(value, "CLEAR"))
  {
    if (!editable)
      return 0;

    /* disable callbacks if not interactive */
    if (ih->data->disable_callbacks == -1)
      ih->data->disable_callbacks = 0;
    else
      ih->data->disable_callbacks = 1;
    XmTextRemove(ih->handle);
    ih->data->disable_callbacks = 0;
  }
  return 0;
}
Esempio n. 23
0
long wxTextEntry::GetInsertionPoint() const
{
    return XmTextGetInsertionPosition(GetText());
}
Esempio n. 24
0
void Browser::searchForward(char *text)
{

    boolean wrap = FALSE;
    boolean found = FALSE;
    char *buf = (char *)CALLOC(this->page_size+1, sizeof(char));
    int i;

    long start_pos = 
	1 + XmTextGetInsertionPosition(this->text) + this->page_start;
    if(start_pos >= this->file_size) start_pos = 0;
    long pos = start_pos;
 

    theIBMApplication->setBusyCursor(TRUE);

#if defined(HAVE_REGCOMP) && defined(HAVE_REGEX_H)

    regex_t search_for;
    ASSERT(regcomp(&search_for, text, REG_NOSUB) == 0);

#elif defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)

    char *search_for = (char *)regcomp(text);
    ASSERT(search_for != NULL);

#elif defined(HAVE_REGCMP)

    char *search_for = regcmp(text, NULL);
    ASSERT(search_for != NULL);

#elif defined(HAVE_RE_COMP)

    char *error_string = re_comp(text);
    if(error_string)
	WarningMessage("Couldn't compile regular expression! %s", error_string);

#endif

    while(!found && !wrap || pos < start_pos)
    {
	//
	// Read in a buffer
	//
	this->from->seekg((std::streampos)pos);
	if(!this->from)
	{
	    std::cerr << "Seekg failed in Browser::searchForward()" << std::endl;
	}
	this->from->read(buf, this->page_size);
	if(!this->from)
	{
	    std::cerr << "Read failed in Browser::searchForward()" << std::endl;
	}
	if(this->from->fail()) this->from->clear();

	//
	// Get the actual number of chars read in
	//
	int nread = this->from->gcount();

	//
	// And NULL terminate the string
	//
	buf[nread] = '\0';
       
	//
	// Convert non-printing chars to '.'
	//
	for(i = 0; i < nread; i++)
	{
	    if(((!isprint(buf[i])) && (buf[i] != '\n') && (buf[i] !='\t') &&
		     (buf[i] != ' ')) || (!isascii(buf[i])))
		buf[i] = '.';
	}

	int offset;

#if defined(HAVE_REGCOMP) && defined(HAVE_REGEX_H)

	int i;
	for (i = 0; i < STRLEN(buf); i++)
	    if (regexec(&search_for, buf + i, 0, NULL, 0) != 0)
		break;
	
	if (i)
	{
	    offset = i - 1;
	    found = 1;
	}	    

#elif defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)

	int i;
	for (i = 0; i < STRLEN(buf); i++)
	    if (! regexec((regexp *)search_for, buf + i ))
	    	break;

	if (i)
	{
	    offset = i - 1;
	    found = 1;
	}

#elif defined(HAVE_REGCMP)

	extern char *__loc1;
	if (regex(search_for, buf))
	{
	    offset = __loc1 - buf;
	    found = 1;
	}

#elif defined(HAVE_RE_COMP)

	int i;
	for (i = 0; i < STRLEN(buf); i++)
	    if (! re_exec(buf + i))
	    	break;

	if (i)
	{
	    offset = i - 1;
	    found = 1;
	}

#else

	char *s;
	s = strstr(text,  buf);
	if (s)
	{
	    offset = s - buf;
	    found = 1;
	}
        
#endif

	if(found)
	{
	    this->gotoByte(offset);
	    found = TRUE;
	    break;
	}
	else
	{
	    int nread = this->from->gcount();
	    //
	    // "Loop" around on the search
	    //
	    if(nread == this->page_size)
	    {
		pos += (int)(0.95*this->page_size);
	    }
	    else
	    {
		// If we have already wrapped, break out.
		if(wrap)
		    break;
		else
		    pos = 0;
		wrap = TRUE;
	    }
	}
    }
    if(!found)
	WarningMessage("Pattern not found");


#if defined(HAVE_REGCOMP) && defined(HAVE_REGEX_H)
    regfree(&search_for);
#elif (defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)) || defined(HAVE_REGCMP)
    free(search_for);
#endif

    FREE(buf);
    theIBMApplication->setBusyCursor(FALSE);
}
Esempio n. 25
0
void Browser::searchBackward(char *text)
{

    boolean wrap = FALSE;
    boolean last_buf = FALSE;
    boolean found = FALSE;
    char *buf = (char *)CALLOC(this->page_size+1, sizeof(char));
    long start_pos = XmTextGetInsertionPosition(this->text) + this->page_start;
    if(start_pos < 0) start_pos = this->file_size;
    long pos = start_pos;
    long prev_pos;
    long bufstart;

    theIBMApplication->setBusyCursor(TRUE);

#if defined(HAVE_REGCOMP) && defined(HAVE_REGEX_H)

    regex_t search_for;
    ASSERT(regcomp(&search_for, text, REG_NOSUB) == 0);
    
#elif defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)

    char *search_for = (char *)regcomp(text);
    ASSERT(search_for != NULL);

#elif defined(HAVE_REGCMP)

    char *search_for = regcmp(text, NULL);
    ASSERT(search_for != NULL);

#elif defined(HAVE_RE_COMP)

    char *error_string = re_comp(text);
    if(error_string)
	WarningMessage("Couldn't compile regular expression! %s", error_string);

#endif


    bufstart = start_pos - this->page_size;
    if(bufstart < 0) bufstart = 0;
    
    while(!wrap || pos >= start_pos)
    {
	//
	// Read in a buffer
	//
	this->from->seekg((std::streampos)bufstart);
	if(!this->from)
	{
	    std::cerr << "Seekg failed in Browser::searchBackward()" << std::endl;
	}
	this->from->read(buf, this->page_size);
	if(!this->from)
	{
	    std::cerr << "Read failed in Browser::searchBackward()" << std::endl;
	}
	if(this->from->fail()) this->from->clear();

        //
        // Get the actual number of chars read in
        //
        int nread = this->from->gcount();

        //
        // And NULL terminate the string
        //
        buf[nread] = '\0';

        //
        // Convert non-printing chars to '.'
        //
        int i;
        for(i = 0; i < nread; i++)
        {
	    if(((!isprint(buf[i])) && (buf[i] != '\n') && (buf[i] !='\t') &&
		     (buf[i] != ' ')) || (!isascii(buf[i])))
		buf[i] = '.';
        }

	int offset;

#if defined(HAVE_REGCOMP) && defined(HAVE_REGEX_H)

	if (regexec(&search_for, buf, 0, NULL, 0) == 0)
	{
	    found = 1;
	    
	    for (i = STRLEN(buf)-1; i >= 0; i--)
		if (regexec(&search_for, buf + i, 0, NULL, 0) != 0)
		    break;
	    
	    offset = i + 1;
	}
	
#elif defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)

	if (regexec((regexp *)search_for, buf))
	{
	    found = 1;

	    for (i = STRLEN(buf)-1; i >= 0; i--)
		if (! regexec((regexp *)search_for, buf + i ))
		    break;

	    offset = i + 1;
	}

#elif defined(HAVE_REGCMP)

	if (regex(search_for, buf))
	{
	    found = 1;

	    for (i = STRLEN(buf)-1; i >= 0; i--)
		if (! regex(search_for, buf + i ))
		    break;

	    offset = i + 1;
	}

#elif defined(HAVE_RE_COMP)

	if (re_exec(buf))
	{
	    found = 1;

	    for (i = STRLEN(buf)-1; i >= 0; i--)
		if (! re_exec(buf + i))
		    break;

	    offset = i + 1;
	}

#else

	if (strstr(text,  buf))
	{
	    found = 1;

	    for (i = STRLEN(buf)-1; i >= 0; i--)
		if (! strstr(text,  buf + i ))
		    break;

	    offset = i + 1;
	}
#endif

	pos = bufstart + offset;
	if(!wrap)
	{
	    if(pos >= start_pos)
	    {
		break;
	    }
	    else
	    {
		prev_pos = pos;
		found = TRUE;
	    }
	}
	else
	{
	    prev_pos = pos;
	    found = TRUE;
	}
    
	if(!found)
	{
	    //
	    // "Loop" around on the search
	    //
	    if(bufstart != 0)
	    {
		bufstart -= (int)(0.95*this->page_size);
		if(bufstart < 0) bufstart = 0;
		if(wrap)
		{
		    if(bufstart < start_pos) 
		    {
			if(last_buf) break;
			bufstart = start_pos;
			last_buf = TRUE;
		    }
		}
	    }
	    else
	    {
		bufstart = this->file_size - this->page_size;
		if(bufstart < 0) bufstart = 0;
		pos = this->file_size;
		if(wrap) break;
		wrap = TRUE;
	    }
	}
	else
	{
	    this->gotoByte(prev_pos);
	    break;
	}
    }
    if(!found)
	WarningMessage("Pattern not found");

#if defined(HAVE_REGCOMP) && defined(HAVE_REGEX_H)
    regfree(&search_for);
#elif (defined(HAVE_REGCOMP) && defined(HAVE_REGEXP_H)) || defined(HAVE_REGCMP)
    free(search_for);
#endif

    FREE(buf);
    theIBMApplication->setBusyCursor(FALSE);
}
Esempio n. 26
0
static char* motTextGetCaretPosAttrib(Ihandle* ih)
{
  XmTextPosition pos = XmTextGetInsertionPosition(ih->handle);
  return iupStrReturnInt((int)pos);
}