Logo Search packages:      
Sourcecode: cadabra version File versions

bool XCadabra::receive ( modglue::ipipe &  p  ) 

Data coming from cadabra arrives here. This handles adding boxes to the document and propagating this to the notebooks.

Definition at line 1889 of file window.cc.

References add_cell(), add_property_help(), NotebookCanvas::cell_grab_focus(), id_to_datacell, and NotebookCanvas::scroll_into_view().

      {
      static std::string str;
      static std::string comment;
      static std::string error;
      static bool error_occurred=false;
      static bool last_was_prompt=true; // avoid repeated empty cells
      static bool in_cell=false; // prompts only get honored outside cells
      static Glib::RefPtr<DataCell> cp, origcell;

      have_received=true;

      while(std::getline(p,str)) {
#ifdef DEBUG
            std::cerr << "rec: " << str << std::endl;
#endif
            if(str.substr(0,10)=="#cellstart") {
                  std::istringstream ss(str.substr(11));
                  int help;
                  ss >> help;
                  assert(id_to_datacell.find(help)!=id_to_datacell.end());
                  cp=id_to_datacell[help];
                  remove_noninput_below(cp);
                  origcell=cp;
                  in_cell=true;
                  comment="";
                  continue;
                  }
            else if(str=="#cellend") {
                  in_cell=false;
                  last_was_prompt=false;
                  if(trim(comment).size()!=0 && trim(comment)!=">") {
                        Glib::RefPtr<DataCell> newcell(new DataCell(DataCell::c_comment, trim(comment)));
                        cp=add_cell(newcell, cp, false);
                        show_cell(newcell);
                        }
                  comment="";
                  continue;
                  }
            else if(str.substr(0,7)=="Cadabra") {
                  size_t spacepos=str.find_first_of(' ', 8);
                  b_kernelversion.set_label("Kernel: "+str.substr(8, spacepos-8)+".");
                  }
            else if(str=="<comment>") {
                  parse_mode.push_back(m_comment);
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="</comment>") {
                  parse_mode.pop_back();
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="<status>") {
                  parse_mode.push_back(m_status);
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="</status>") {
                  parse_mode.pop_back();
                  continue;
                  }
            else if(str=="<plain>") {
                  parse_mode.push_back(m_plain);
                  plain="";
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="</plain>") {
                  parse_mode.pop_back();
                  last_was_prompt=false;              
                  continue;
                  }
            else if(str=="<progress>") {
                  progress="";
                  progress_todo=-1;
                  progress_done=-1;
                  progress_count=-1;
                  parse_mode.push_back(m_progress);
                  continue;
                  }
            else if(str=="</progress>") {
                  std::ostringstream ss;
                  if(progress_todo>0) 
                        ss << progress << "..." << " (" << progress_done << " of " << progress_todo << ")";
                  else
                        ss << progress << "...";
                  if(progress_count==1) {
                        progressbar1.set_text(ss.str());
                        if(progress_todo==0) progressbar1.set_fraction(0);
                        else                 progressbar1.set_fraction(std::min(1.0,progress_done/(1.0*progress_todo)));
                        }
                  else {
                        progressbar2.set_text(ss.str());
                        if(progress_todo==0) progressbar2.set_fraction(0);
                        else                 progressbar2.set_fraction(std::min(1.0,progress_done/(1.0*progress_todo)));
                        }
                  parse_mode.pop_back();
                  continue;
                  }
            else if(str=="<error>") {
                  parse_mode.push_back(m_error);
                  last_was_prompt=false;
                  error_occurred=true;
                  continue;
                  }
            else if(str=="</error>") {
                  parse_mode.pop_back();
                  if(trim(error).size()!=0) {
                        Glib::RefPtr<DataCell> newcell(new DataCell(DataCell::c_error, trim(error)));
                        kernel_idle();
                        cp=add_cell(newcell, cp, false);
                        show_cell(newcell);
//                      // make previous input cell active
//                      DataCells_t::iterator dit=datacells.begin();
//                      while(dit!=datacells.end()) {
//                             if((*dit)==newcell) {
//                                    while(dit!=datacells.begin()) {
//                                              
//                                              }
//                                    }
//                             prevcell=(*dit);
//                             ++dit;
//                             }
                        }
                  error="";
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="<eqno>") {
                  parse_mode.push_back(m_eqno);
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="</eqno>") {
                  parse_mode.pop_back();
                  continue;
                  }
            else if(str=="<eq>") {
                  parse_mode.push_back(m_eq);
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="</eq>") {
                  parse_mode.pop_back();
                  if(eq.size()!=0) {
                        Glib::RefPtr<DataCell> newcell(new DataCell(DataCell::c_output, eqno+"\\specialcolon{}= "+eq));
                        newcell->cdbbuf=plain;
                        cp=add_cell(newcell, cp, false);
                        show_cell(newcell);
                        }
                  eq="";
                  eqno="";
                  plain="";
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="<property>") {
                  parse_mode.push_back(m_property);
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="</property>") {
                   add_property_help(property);
                   property="";
                  parse_mode.pop_back();
                  continue;
                  }
            else if(str=="<algorithm>") {
                  parse_mode.push_back(m_algorithm);
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="</algorithm>") {
                   add_algorithm_help(algorithm);
                   algorithm="";
                  parse_mode.pop_back();
                  continue;
                  }
            else if(str=="<reserved>") {
                  parse_mode.push_back(m_reserved);
                  last_was_prompt=false;
                  continue;
                  }
            else if(str=="</reserved>") {
                   add_reserved_help(reserved);
                   reserved="";
                  parse_mode.pop_back();
                  continue;
                  }
            else if(trim(str).substr(0,1)==">") {
#ifdef DEBUG
                   std::cerr << "received empty prompt" << std::endl;
#endif
                  progressbar2.set_fraction(0);
                  progressbar1.set_fraction(0);
                  progressbar2.set_text(" ");
                  progressbar1.set_text(" ");
                  if(!last_was_prompt && !in_cell) {
                        if(!running) {
                               kernel_idle();
                              }
                        last_was_prompt=true;
                        if(error_occurred) {
                              error_occurred=false;
                              // re-enable original cell
                              if(origcell) {
                                    origcell->running=false;
#if (GLIBMM_VER == 216)
                                    origcell.reset();
#else
                                    origcell.clear();
#endif
                                    }
                              }
                        else { // everything hunky dorey
                              if(datacells.back()==cp || cp==0 ) { // we are at the last cell of the notebook
                                    if(datacells.size()>0 && datacells.back()->cell_type==DataCell::c_input &&
                                          trim(datacells.back()->textbuf->get_text()).size()==0 ) {
                                           // we still have an empty cell below
                                           if(restarting_kernel) {
                                                  restarting_kernel=false;
                                                  active_canvas->cell_grab_focus(active_cell);
                                                  }
                                           else {
                                                  active_canvas->cell_grab_focus(datacells.back());
                                                  }
                                           while (gtk_events_pending ())
                                                  gtk_main_iteration ();
                                           active_canvas->scroll_into_view(active_cell);
                                           }
                                    else { // this last cell is not an input cell; add a new input cell
                                          Glib::RefPtr<DataCell> newcell(new DataCell(DataCell::c_input, ""));
                                          action_add(Glib::RefPtr<ActionBase>(new ActionAddCell(newcell, cp, false)));
                                          cp = newcell;
//                                        cp=add_cell(newcell, cp, false); // HERE
                                          show_cell(newcell);
                                          active_canvas->cell_grab_focus(cp);
                                          }
                                    if(restarting_kernel) {
                                          restarting_kernel=false;
                                          active_canvas->cell_grab_focus(active_cell);
                                          }
                                    while (gtk_events_pending ())
                                          gtk_main_iteration ();
                                    active_canvas->scroll_into_view(active_cell);
                                    // re-enable original cell (mark it non-running)
                                    if(origcell) {
                                          origcell->running=false;
#if (GLIBMM_VER == 216)
                                          origcell.reset();
#else
                                          origcell.clear();
#endif
                                          }
                                    }
                              else { // still more cells below
                                    if(restarting_kernel) {
                                          restarting_kernel=false;
                                          active_canvas->cell_grab_focus(active_cell);
                                          }
                                    else { 
                     // put cursor in the next input cell
                                          DataCells_t::iterator it=datacells.begin();
                                          while(it!=datacells.end()) {
                                                if(*it==cp) {
                                                      ++it;
                                                      while(it!=datacells.end() && (*it)->cell_type!=DataCell::c_input)
                                                            ++it;
                                                      if(it==datacells.end()) {
                                                            Glib::RefPtr<DataCell> newcell(new DataCell(DataCell::c_input, ""));
                                                            action_add(Glib::RefPtr<ActionBase>(new ActionAddCell(newcell, cp, false)));
                                                            cp = newcell;
//                                                          cp=add_cell(newcell, cp, false); // HERE
                                                            show_cell(newcell);
                                                            active_canvas->cell_grab_focus(cp);
                                                            }
                                                      else {
                                                             active_canvas->cell_grab_focus(*it);
                                                             }
                                                      // re-enable original cell
                                                      if(origcell) {
                                                            origcell->running=false;
#if (GLIBMM_VER == 216)
                                                            origcell.reset();
#else
                                                            origcell.clear();
#endif
                                                            }
                                                      break;
                                                      }
                                                ++it;
                                                }
                                          if(!running) { // put cell in view
                                                 while (gtk_events_pending ())
                                                        gtk_main_iteration ();
                                                 active_canvas->scroll_into_view(active_cell);
                                                 }
                                          }
                                    }
                              }
                        }
                  else str="\n";
                  }
            switch(parse_mode.back()) {
                  case m_eq:
                        eq+=str;
                        if(str[str.size()-1]=='%')
                              eq+="\n";
                        break;
                  case m_eqno:
                        eqno+=str;
                        break;
                  case m_status:
                  case m_discard:
                        break;
                  case m_property:
                        property+=str;
                        break;
                  case m_algorithm:
                        algorithm+=str; 
                        break;
                  case m_reserved:
                        reserved+=str; 
                        break;
                  case m_comment:
                        if(trim(str).size()>0)
                              comment+=str+"\n";
                        break;
                  case m_error:
                        error+=str+"\n";
                        break;
                  case m_plain:
                        plain+=str;
                        break;
                  case m_progress:
                        if(progress=="")            progress=str;
                        else if(progress_todo==-1)  progress_todo=atoi(str.c_str());
                        else if(progress_done==-1)  progress_done=atoi(str.c_str());
                        else                        progress_count=atoi(str.c_str());
                        break;
                  }
            }
      p.clear();

      if(load_file) {
            load_file=false;
#ifdef DEBUG
            std::cerr << "Loading file..." << std::endl;
#endif            
            std::string tmp=name;
            std::string res=load(tmp, true);
            if(res.size()>0) {
                   Gtk::MessageDialog md("Error loading document "+tmp);
                   md.set_secondary_text(res);
                   md.set_type_hint(Gdk::WINDOW_TYPE_HINT_DIALOG);
                   md.run();
                   name="";
                   }
            else {
                  name=tmp;
                  Glib::RefPtr<DataCell> newcell(new DataCell(DataCell::c_input));
                  add_cell(newcell, Glib::RefPtr<DataCell>());
                  show_cell(newcell);
                  while (gtk_events_pending ())
                        gtk_main_iteration ();
//                active_canvas->cell_grab_focus(newcell);
                  }
            modified=false;
            update_title();
            }

      return true;
      }


Generated by  Doxygen 1.6.0   Back to index