lw_common.ui.note_ctrl.set_current_line_impl C# (CSharp) Method

set_current_line_impl() private method

private set_current_line_impl ( line l ) : void
l line
return void
        private void set_current_line_impl(line l) {
            if (cur_line.idx == l.idx && cur_line.view_name == l.view_name)
                return; // we're already there

            if (l.idx < 0) {
                ++ignore_change_;
                for (int idx = 0; idx < notesCtrl.GetItemCount(); ++idx) {
                    var i = notesCtrl.GetItem(idx).RowObject as note_item; 
                    if ( i.is_cur_line)
                        notesCtrl.RemoveObject(i);
                }
                --ignore_change_;
            }

            cur_line.copy_from(l);

            if (l.idx < 0) {
                ++ignore_change_;
                notesCtrl.SelectedIndex = -1;
                --ignore_change_;
                update_cur_note_controls();
                return;
            }

            /* Possibilities:
            
            - case 1 - there are already notes on this line

                1a - there are already notes on this line, and the selection is already one of them
                1b - there are already notes on this line, and the selection is on another note (or nowhere)

            - case 2 - there are no notes on this line
            */
            note_item header = null;
            for (int idx = 0; idx < notesCtrl.GetItemCount() && header == null ; ++idx) {
                var i = notesCtrl.GetItem(idx).RowObject as note_item;
                if (i.is_note_header && lines_[i.line_id].idx == l.idx && !i.is_cur_line) 
                    header = i;
            }

            bool last_note_is_cur_line = notesCtrl.GetItemCount() > 0 && (notesCtrl.GetItem(notesCtrl.GetItemCount() - 1).RowObject as note_item).is_cur_line;
            if (header != null) {
                // already a note on this line, not needed now
                if (last_note_is_cur_line)
                    notesCtrl.RemoveObject(notesCtrl.GetItem(notesCtrl.GetItemCount() - 1).RowObject);

                int sel = notesCtrl.SelectedIndex;
                var sel_item = sel >= 0 ? notesCtrl.GetItem(sel).RowObject as note_item : null;
                if (sel_item != null && sel_item.line_id == header.line_id)
                    // 1a - on a note from this line
                    return;

                // 1b - on a note from another line
                // I will select the last note from this user
                var last = ui_find_last_note_from_cur_user(header.line_id);
                notesCtrl.SelectObject( last);
                notesCtrl.EnsureModelVisible(last);
            } else {
                // 2 - no notes on this line
                if (!last_note_is_cur_line) {
                    var new_ = new note_item(this, next_guid, cur_line_id_, false, DateTime.UtcNow);
                    notesCtrl.AddObject(new_);
                    refresh_note( new_);
                }
                var last = notesCtrl.GetItem(notesCtrl.GetItemCount() - 1).RowObject as note_item;
                refresh_note(last);

                bool focus_on_notes = win32.focused_ctrl() == notesCtrl;
                if (notesCtrl.SelectedIndex < 0 || !focus_on_notes) {
                    // select the "last line"
                    notesCtrl.SelectedIndex = notesCtrl.GetItemCount() - 1;
                    notesCtrl.EnsureVisible( notesCtrl.GetItemCount() - 1);
                }
            }

            update_cur_note_controls();
        }