/[svn]/gigedit/trunk/src/gigedit/scripteditor.cpp
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/scripteditor.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3286 by schoenebeck, Thu Jun 22 10:54:10 2017 UTC revision 3314 by schoenebeck, Tue Jul 18 22:00:56 2017 UTC
# Line 7  Line 7 
7    
8  #include "scripteditor.h"  #include "scripteditor.h"
9  #include "global.h"  #include "global.h"
10    #include <gtk/gtkwidget.h> // for gtk_widget_modify_*()
11    #if defined(__APPLE__)
12    # include "MacHelper.h"
13    #endif
14    #include <math.h> // for log10()
15    
16  #if !USE_LS_SCRIPTVM  #if !USE_LS_SCRIPTVM
17    
# Line 151  ScriptEditor::ScriptEditor() : Line 156  ScriptEditor::ScriptEditor() :
156      m_warningTag->property_background() = "#fffd7c"; // yellow      m_warningTag->property_background() = "#fffd7c"; // yellow
157      m_tagTable->add(m_warningTag);      m_tagTable->add(m_warningTag);
158    
159        m_lineNrTag = Gtk::TextBuffer::Tag::create();
160        m_lineNrTag->property_foreground() = "#CCCCCC";
161        m_tagTable->add(m_lineNrTag);
162    
163      // create menu      // create menu
164      m_actionGroup = Gtk::ActionGroup::create();      m_actionGroup = Gtk::ActionGroup::create();
165      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));      m_actionGroup->add(Gtk::Action::create("MenuScript", _("_Script")));
# Line 181  ScriptEditor::ScriptEditor() : Line 190  ScriptEditor::ScriptEditor() :
190          "</ui>"          "</ui>"
191      );      );
192    
193        m_lineNrBuffer = Gtk::TextBuffer::create(m_tagTable);
194        m_lineNrView.set_buffer(m_lineNrBuffer);
195        m_lineNrView.set_left_margin(3);
196        m_lineNrView.set_right_margin(3);
197        m_lineNrTextViewSpacer.set_size_request(5);
198        {
199            Gdk::Color color;
200            color.set("#F5F5F5");
201            GtkWidget* widget = (GtkWidget*) m_lineNrView.gobj();
202            gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
203            gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
204        }
205        {
206            Gdk::Color color;
207            color.set("#EEEEEE");
208            GtkWidget* widget = (GtkWidget*) m_lineNrTextViewSpacer.gobj();
209            gtk_widget_modify_base(widget, GTK_STATE_NORMAL, color.gobj());
210            gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, color.gobj());
211        }
212      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);      m_textBuffer = Gtk::TextBuffer::create(m_tagTable);
213      m_textView.set_buffer(m_textBuffer);      m_textView.set_buffer(m_textBuffer);
214        m_textView.set_left_margin(5);
215      setFontSize(currentFontSize(), false);      setFontSize(currentFontSize(), false);
216      m_scrolledWindow.add(m_textView);      m_textViewHBox.pack_start(m_lineNrView, Gtk::PACK_SHRINK);
217        m_textViewHBox.pack_start(m_lineNrTextViewSpacer, Gtk::PACK_SHRINK);
218        m_textViewHBox.add(m_textView);
219        m_scrolledWindow.add(m_textViewHBox);
220      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);      m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
221    
222      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");      Gtk::Widget* menuBar = m_uiManager->get_widget("/MenuBar");
# Line 254  ScriptEditor::~ScriptEditor() { Line 286  ScriptEditor::~ScriptEditor() {
286    
287  int ScriptEditor::currentFontSize() const {  int ScriptEditor::currentFontSize() const {
288  #if defined(__APPLE__)  #if defined(__APPLE__)
289      const int defaultFontSize = 13;      const int defaultFontSize = 11;
290  #else  #else
291      const int defaultFontSize = 10;      const int defaultFontSize = 10;
292  #endif  #endif
# Line 267  void ScriptEditor::setFontSize(int size, Line 299  void ScriptEditor::setFontSize(int size,
299      //printf("setFontSize(%d,%d)\n", size, save);      //printf("setFontSize(%d,%d)\n", size, save);
300      Pango::FontDescription fdesc;      Pango::FontDescription fdesc;
301      fdesc.set_family("monospace");      fdesc.set_family("monospace");
302    #if defined(__APPLE__)
303        // fixes poor readability of default monospace font on Macs
304        if (macIsMinMac10_6())
305            fdesc.set_family("Menlo");
306    #endif
307      fdesc.set_size(size * PANGO_SCALE);      fdesc.set_size(size * PANGO_SCALE);
308        /*Glib::RefPtr<Pango::Context> context = m_textView.get_pango_context();
309        Cairo::FontOptions options;
310        options.set_antialias(Cairo::ANTIALIAS_NONE);
311        context->set_cairo_font_options(options);*/
312  #if GTKMM_MAJOR_VERSION < 3  #if GTKMM_MAJOR_VERSION < 3
313        m_lineNrView.modify_font(fdesc);
314      m_textView.modify_font(fdesc);      m_textView.modify_font(fdesc);
315  #else  #else
316        m_lineNrView.override_font(fdesc);
317      m_textView.override_font(fdesc);      m_textView.override_font(fdesc);
318  #endif  #endif
319      if (save) Settings::singleton()->scriptEditorFontSize = size;      if (save) Settings::singleton()->scriptEditorFontSize = size;
# Line 291  void ScriptEditor::setScript(gig::Script Line 334  void ScriptEditor::setScript(gig::Script
334      m_textBuffer->set_modified(false);      m_textBuffer->set_modified(false);
335  }  }
336    
337    void ScriptEditor::updateLineNumbers() {
338        const int n = m_textBuffer->get_line_count();
339        const int old = m_lineNrBuffer->get_line_count();
340        if (n == old) return;
341        const int digits = log10(n) + 1;
342        const int bufSz = digits + 2;
343        char* buf = new char[bufSz];
344        std::string sFmt1 =   "%" + ToString(digits) + "d";
345        std::string sFmt2 = "\n%" + ToString(digits) + "d";
346        Glib::ustring s;
347        for (int i = 0; i < n; ++i) {
348            snprintf(buf, bufSz, i ? sFmt2.c_str() : sFmt1.c_str(), i+1);
349            s += buf;
350        }
351        m_lineNrBuffer->remove_all_tags(m_lineNrBuffer->begin(), m_lineNrBuffer->end());
352        m_lineNrBuffer->set_text(s);
353        m_lineNrBuffer->apply_tag(m_lineNrTag, m_lineNrBuffer->begin(), m_lineNrBuffer->end());
354        if (buf) delete[] buf;
355    }
356    
357  void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {  void ScriptEditor::onTextInserted(const Gtk::TextBuffer::iterator& itEnd, const Glib::ustring& txt, int length) {
358      //printf("onTextInserted()\n");      //printf("onTextInserted()\n");
359  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
# Line 341  void ScriptEditor::onTextInserted(const Line 404  void ScriptEditor::onTextInserted(const
404      ;      ;
405            
406  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
407        updateLineNumbers();
408  }  }
409    
410  #if USE_LS_SCRIPTVM  #if USE_LS_SCRIPTVM
# Line 566  void ScriptEditor::onTextErased(const Gt Line 630  void ScriptEditor::onTextErased(const Gt
630    
631      m_textBuffer->remove_all_tags(itStart2, itEnd2);      m_textBuffer->remove_all_tags(itStart2, itEnd2);
632  #endif // USE_LS_SCRIPTVM  #endif // USE_LS_SCRIPTVM
633        updateLineNumbers();
634  }  }
635    
636  bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {  bool ScriptEditor::on_motion_notify_event(GdkEventMotion* e) {

Legend:
Removed from v.3286  
changed lines
  Added in v.3314

  ViewVC Help
Powered by ViewVC