Пример #1
0
  def __init__(self, ctx, paths, stumped_txt):
    Gedit.Tab.__init__(self)

    self.ctx = ctx
    self._srm = ctx.srm
    self._paths = paths
    self._bg_color = None
    self._fg_color = None
    self._docs = DocumentPack(self, paths, 0)
    self._quick_tab_ext = ctypes.CDLL(LIBSRM_TRAINER_TAB_PATH)

    hbox = Gtk.HBox()

    header = Gtk.HBox()
    header.connect('draw', self.draw_bgnd)

    self._nav = ProblemNavigator([p.type_name for p in self._srm.problems])
    self._nav.connect('problem-selected', self._set_location)
    header.pack_start(make_vbox(self._nav, True, True, 10), False, False, 10)

    stumped_widget = SrmTrainerImStumped(stumped_txt)
    stumped_widget_dropdown = SrmTrainerDropDownBtn('I\'m stumped!', stumped_widget)
    align = Gtk.Alignment()
    align.set(0.0, 0.4, 0.0, 0.0)
    align.add(stumped_widget_dropdown)
    header.pack_end(align, False, False, 10)

    action_menu = Gtk.VBox()
    self._add_actions(action_menu)
    self._action_menu_dropdown = SrmTrainerDropDownBtn('Actions', action_menu)
    align = Gtk.Alignment()
    align.set(0.0, 0.4, 0.0, 0.0)
    align.add(self._action_menu_dropdown)
    header.pack_end(align, False, False, 10)

    hbox.pack_start(header, True, True, 0)

    self.pack_start(hbox, False, True, 0)
    self.reorder_child(hbox, 0)

    self.set_colors()

    hbox.show_all()

    self._set_location(None, 0)
Пример #2
0
class SrmTab(Gedit.Tab):

  __gtypename__ = 'SrmTab'

  __g_signals__ = {
    'menu-item-clicked': (GObject.SIGNAL_RUN_FIRST, GObject.TYPE_NONE, [object])
  }

  def __init__(self, ctx, paths, stumped_txt):
    Gedit.Tab.__init__(self)

    self.ctx = ctx
    self._srm = ctx.srm
    self._paths = paths
    self._bg_color = None
    self._fg_color = None
    self._docs = DocumentPack(self, paths, 0)
    self._quick_tab_ext = ctypes.CDLL(LIBSRM_TRAINER_TAB_PATH)

    hbox = Gtk.HBox()

    header = Gtk.HBox()
    header.connect('draw', self.draw_bgnd)

    self._nav = ProblemNavigator([p.type_name for p in self._srm.problems])
    self._nav.connect('problem-selected', self._set_location)
    header.pack_start(make_vbox(self._nav, True, True, 10), False, False, 10)

    stumped_widget = SrmTrainerImStumped(stumped_txt)
    stumped_widget_dropdown = SrmTrainerDropDownBtn('I\'m stumped!', stumped_widget)
    align = Gtk.Alignment()
    align.set(0.0, 0.4, 0.0, 0.0)
    align.add(stumped_widget_dropdown)
    header.pack_end(align, False, False, 10)

    action_menu = Gtk.VBox()
    self._add_actions(action_menu)
    self._action_menu_dropdown = SrmTrainerDropDownBtn('Actions', action_menu)
    align = Gtk.Alignment()
    align.set(0.0, 0.4, 0.0, 0.0)
    align.add(self._action_menu_dropdown)
    header.pack_end(align, False, False, 10)

    hbox.pack_start(header, True, True, 0)

    self.pack_start(hbox, False, True, 0)
    self.reorder_child(hbox, 0)

    self.set_colors()

    hbox.show_all()

    self._set_location(None, 0)

  def _set_location(self, widget, problem_idx):
    self._docs.set_location(problem_idx)

  @property
  def problem_idx(self):
    return self._docs.get_location()

  def _add_actions(self, menu):
    class OnClick:
    
      def __init__(self, tab, item):
        self._tab = tab
        self._item = item

      def __call__(self, widget, data):
        self._tab._action_menu_dropdown.hide_popup()
        self._item['action'](self._tab)

    for item in self.ctx.menu_items:
      menu_item = SrmTrainerMenuItem(item['label'])
      menu_item.connect('clicked', OnClick(self, item))
      menu.pack_start(menu_item, False, True, 0)

  def _menu_item_clicked(self, item):
    self.emit('menu-item-clicked', item)

  def set_colors(self):
    # TODO: right now this only works w/ Cobalt. Since each style scheme can name
    #       individual styles whatever they want, there's no way to reliably get
    #       the 'main' text style. I'll have to map the style name w/ the style
    #       scheme in the config file. (e.g., 'cobalt' would map w/ 'text')
    txt_style = self.get_view()                 \
                    .get_buffer()               \
                    .get_style_scheme()         \
                    .get_style('text')

    if not txt_style:
      return

    bg_color_txt = txt_style.get_property('background')
    self._bg_color = Gdk.color_parse(bg_color_txt)[1]

    fg_color_txt = txt_style.get_property('foreground')
    self._fg_color = Gdk.color_parse(fg_color_txt)[1]

    self._nav.text_color = self._fg_color
    self._nav.bg_color = self._bg_color

  def draw_bgnd(self, widget, ctx):
    alloc = widget.get_allocation()

    self.set_colors()

    ctx.set_source_rgb( \
      self._bg_color.red / COLOR_MAX, \
      self._bg_color.green / COLOR_MAX, \
      self._bg_color.blue / COLOR_MAX)
    ctx.rectangle(0, 0, alloc.width, alloc.height)
    ctx.fill()

  def add_to_window(self, window, jump_to=True):
    self._quick_tab_ext.srm_trainer_tab_add_to_window(hash(self), hash(window), jump_to)