Callbacks

Now that we've defined the GUI components of our editor, we need to define our callback functions.

changed_cb()

This function will be called whenever the user changes any text in the input widget:

void changed_cb(void) {
  set_changed(1);
}
        

The set_changed() function is one that we will write to set the changed status on the current file. We're doing it this way because some of the other callbacks will set the changed status to 0, and also because we want to show the changed status in the window's title bar.

copy_cb()

This callback function will call input->copy() to copy the currently selected text to the clipboard:

void copy_cb(void) {
  input->copy();
}
        

cut_cb()

This callback function will call input->copy() to copy the currently selected text to the clipboard and then input->cut() to delete it:

void cut_cb(void) {
  input->copy();
  input->cut();
}
        

delete_cb()

This callback function will call input->cut() to delete the selected text:

void delete_cb(void) {
  input->cut();
}
        

find_cb()

This callback function asks for a search string using the fl_input() convenience function and then calls the find2_cb() function to find the string:

void find_cb(void) {
  const char *val;

  val = fl_input("Search String:", search);
  if (val != NULL) {
    // User entered a string - go find it!
    strcpy(search, val);
    find2_cb();
  }
}
        

find2_cb()

This function will find the next occurrence of the search string. If the search string is blank then we want to pop up the search dialog:

void find2_cb(void) {
  const char *val, *found;
  int pos;

  if (search[0] == '\0') {
    // Search string is blank; get a new one...
    find_cb();
    return;
  }

  val   = input->value() + input->mark();
  found = strstr(val, search);

  if (found != NULL) {
    // Found a match; update the position and mark...
    pos = input->mark() + found - val;
    input->position(pos, pos + strlen(search));
  }
  else fl_alert("No occurrences of \'%s\' found!", search);
}
        

If the search string cannot be found we use the fl_alert() convenience function to display a message to that effect.

new_cb()

This callback function will clear the input widget and current filename. It also calls the check_save() function to give the user the opportunity to save the current file first as needed:

void new_cb(void) {
  if (changed)
    if (!check_save()) return;

  filename[0] = '\0';
  input->value("");
  set_changed(0);
}
        

open_cb()

This callback function will ask the user for a filename and then load the specified file into the input widget and current filename. It also calls the check_save() function to give the user the opportunity to save the current file first as needed:

void open_cb(void) {
  char *newfile;

  if (changed)
    if (!check_save()) return;

  newfile = fl_file_chooser("Open File?", "*", filename);
  if (newfile != NULL) load_file(newfile);
}
       

We call the load_file() function to actually load the file.

paste_cb()

This callback function will send a FL_PASTE message to the input widget using the Fl::paste() method:

void paste_cb(void) {
  Fl::paste(*input);
}
        

quit_cb()

The quit callback will first see if the current file has been modified, and if so give the user a chance to save it. It then hides the main window:

void quit_cb(void) {
  if (changed)
    if (!check_save())
      return;

  window->hide();
}
        

replace_cb()

The replace callback just shows the replace dialog:

void replace_cb(void) {
  replace_dlg->show();
}
        

replace2_cb()

This callback will replace the next occurence of the replacement string. If nothing has been entered for the replacement string, then the replace dialog is displayed instead:

void replace2_cb() {
  const char *find, *val, *found;
  int pos;

  find = replace_find->value();
  if (find[0] == '\0') {
    // Search string is blank; get a new one...
    replace_dlg->show();
    return;
  }

  val   = input->value() + input->position();
  found = strstr(val, find);

  if (found != NULL) {
    // Found a match; update the position and replace text...
    pos = input->position() + found - val;
    input->replace(pos, pos + strlen(find), replace_with->value());
    input->position(pos + strlen(replace_with->value()));
  }
  else fl_alert("No occurrences of \'%s\' found!", find);
}
        

replall_cb()

This callback will replace all occurences of the search string in the file:

void replall_cb() {
  const char *find, *val, *found;
  int pos;
  int times;

  find = replace_find->value();
  if (find[0] == '\0') {
    // Search string is blank; get a new one...
    replace_dlg->show();
    return;
  }

  input->position(0);
  times = 0;

  // Loop through the whole string
  do {
    val   = input->value() + input->position();
    found = strstr(val, find);

    if (found != NULL) {
      // Found a match; update the position and replace text...
      times ++;
      pos = input->position() + found - val;
      input->replace(pos, pos + strlen(find), replace_with->value());
      input->position(pos + strlen(replace_with->value()));
    }
  } while (found != NULL);

  if (times > 0) fl_message("Replaced %d occurrences.", times);
  else fl_alert("No occurrences of \'%s\' found!", find);
}
        

replcan_cb()

This callback just hides the replace dialog:

void replcan_cb() {
  replace_dlg->hide();
}
        

save_cb()

This callback saves the current file. If the current filename is blank it calls the "save as" callback:

void save_cb(void) {
  if (filename[0] == '\0') {
    // No filename - get one!
    saveas_cb();
    return;
  }
  else save_file(filename);
}
       

The save_file() function saves the current file to the specified filename.

saveas_cb()

This callback asks the user for a filename and saves the current file:

void saveas_cb(void) {
  char *newfile;

  newfile = fl_file_chooser("Save File As?", "*", filename);
  if (newfile != NULL) save_file(newfile);
}
        

The save_file() function saves the current file to the specified filename.

undo_cb()

The undo callback just calls the undo() method:

void undo_cb(void) {
  input->undo();
}