def __init__(self, parent=None, screenshot=None):
        '''
        init toolbar
        @param parent: the transient parent for this window
        @param screenshot: a Screenshot object
        '''
        self.screenshot = screenshot
        self.win = screenshot.window
        self.__config = OperateConfig()
        save_op = self.__config.get("save", "save_op")
        if save_op:
            self.screenshot.save_op_index = int(save_op)
        else:
            self.screenshot.save_op_index = SAVE_OP_AUTO

        #toolbar_padding_x = 15
        #toolbar_padding_y = 5
        #toolbar_icon_width = toolbar_icon_height = 28
        #toolbar_icon_num = 10
        #self.height = toolbar_icon_height + toolbar_padding_y * 2
        self.height = 30
        self.width = 279
        #self.width = 240

        self.window = Window(window_type=gtk.WINDOW_POPUP, shadow_visible=False)
        self.window.set_keep_above(True)
        self.window.set_decorated(False)
        self.window.set_transient_for(parent)

        self.toolbox = gtk.HBox(False, 6)
        toolbox_align = gtk.Alignment()
        toolbox_align.set(0, 0.5, 0, 0)
        toolbox_align.set_padding(2, 2, 11, 11)
        toolbox_align.add(self.toolbox)
        self.window.window_frame.pack_start(toolbox_align, True, True)
        #self.window.set_size_request(self.width, self.height)
        self.window.set_size_request(-1, self.height)

        self._toggle_button_list = []
        self.__button_accelerator_dict = {}
        self._toggle_button_group = ToggleButtonGroup([], 6)
        self.toolbox.pack_start(self._toggle_button_group)
        self.create_toggle_button("rect", ACTION_RECTANGLE, 0, _("Draw Rectangle"), "<Alt>1")
        self.create_toggle_button("ellipse", ACTION_ELLIPSE, 1, _("Draw Ellipse"), "<Alt>2")
        self.create_toggle_button("arrow",ACTION_ARROW, 2, _("Draw Arrow"), "<Alt>3")
        self.create_toggle_button("line",ACTION_LINE, 3, _("Draw Line"), "<Alt>4")
        self.create_toggle_button("text",ACTION_TEXT, 4, _("Draw Text"), "<Alt>5")

        self.create_button("undo", _("Undo"), "<Alt>6")

        if self.screenshot.is_subprocess:
            self.create_button("save", _("Save"), "<Alt>7")
        else:
            # pack save and list button
            save_combo_button = ComboButton(
                app_theme.get_pixbuf("action/save_normal.png"),
                app_theme.get_pixbuf("action/save_hover.png"),
                app_theme.get_pixbuf("action/save_press.png"),
                app_theme.get_pixbuf("action/save_normal.png"),
                app_theme.get_pixbuf("action/list_normal.png"),
                app_theme.get_pixbuf("action/list_hover.png"),
                app_theme.get_pixbuf("action/list_press.png"),
                app_theme.get_pixbuf("action/list_normal.png"),)
            save_combo_button.set_name("save")
            save_combo_button.connect("button-clicked", self._button_clicked, "save")
            save_combo_button.connect("arrow-clicked", self._list_menu_show)
            save_tip_text_list = ["Save automatically", "Save as", "Save to clipboard", "Save automatically to file and clipboard"]
            tip_text = save_tip_text_list[self.screenshot.save_op_index]
            save_combo_button.connect("enter-notify-event", self._show_tooltip, _(tip_text))
            self.toolbox.pack_start(save_combo_button)

        self.create_button("cancel", _("Cancel"), "<Alt>8")

        if not self.screenshot.is_subprocess:
            self.create_button("share", _("Share"), "<Alt>9")

        if self.screenshot:
            self._button_clicked_cb = {
                'undo': self.screenshot.undo,
                'save': self.save_operate,
                'cancel': self.win.quit,
                'share': self.share_picture}
Beispiel #2
0
    def __init__(self, parent=None, screenshot=None):
        '''
        init toolbar
        @param parent: the transient parent for this window
        @param screenshot: a Screenshot object
        '''
        self.screenshot = screenshot
        self.win = screenshot.window
        self.__config = OperateConfig()
        save_op = self.__config.get("save", "save_op")
        if save_op:
            self.screenshot.save_op_index = int(save_op)
        else:
            self.screenshot.save_op_index = SAVE_OP_AUTO

        #toolbar_padding_x = 15
        #toolbar_padding_y = 5
        #toolbar_icon_width = toolbar_icon_height = 28
        #toolbar_icon_num = 10
        #self.height = toolbar_icon_height + toolbar_padding_y * 2
        self.height = 30
        self.width = 279
        #self.width = 240

        self.window = Window(window_type=gtk.WINDOW_POPUP,
                             shadow_visible=False)
        self.window.set_keep_above(True)
        self.window.set_decorated(False)
        self.window.set_transient_for(parent)

        self.toolbox = gtk.HBox(False, 6)
        toolbox_align = gtk.Alignment()
        toolbox_align.set(0, 0.5, 0, 0)
        toolbox_align.set_padding(2, 2, 11, 11)
        toolbox_align.add(self.toolbox)
        self.window.window_frame.pack_start(toolbox_align, True, True)
        #self.window.set_size_request(self.width, self.height)
        self.window.set_size_request(-1, self.height)

        self._toggle_button_list = []
        self._toggle_button_group = ToggleButtonGroup([], 6)
        self.toolbox.pack_start(self._toggle_button_group)
        self.create_toggle_button("rect", ACTION_RECTANGLE, 0,
                                  _("draw rectangle"))
        self.create_toggle_button("ellipse", ACTION_ELLIPSE, 1,
                                  _("draw ellipse"))
        self.create_toggle_button("arrow", ACTION_ARROW, 2, _("draw arrow"))
        self.create_toggle_button("line", ACTION_LINE, 3, _("draw line"))
        self.create_toggle_button("text", ACTION_TEXT, 4, _("draw Text"))

        self.create_button("undo", _("undo"))
        # pack save and list button
        save_combo_button = ComboButton(
            app_theme.get_pixbuf("action/save_normal.png"),
            app_theme.get_pixbuf("action/save_hover.png"),
            app_theme.get_pixbuf("action/save_press.png"),
            app_theme.get_pixbuf("action/save_normal.png"),
            app_theme.get_pixbuf("action/list_normal.png"),
            app_theme.get_pixbuf("action/list_hover.png"),
            app_theme.get_pixbuf("action/list_press.png"),
            app_theme.get_pixbuf("action/list_normal.png"),
        )
        save_combo_button.set_name("save")
        save_combo_button.connect("button-clicked", self._button_clicked,
                                  "save")
        save_combo_button.connect("arrow-clicked", self._list_menu_show)
        save_tip_text_list = [
            "save automatically", "save as", "save to clipboard",
            "save automatically to file and clipboard"
        ]
        tip_text = save_tip_text_list[self.screenshot.save_op_index]
        save_combo_button.connect("enter-notify-event", self._show_tooltip,
                                  _(tip_text))
        self.toolbox.pack_start(save_combo_button)

        self.create_button("cancel", _("cancel"))
        self.create_button("share", _("share"))

        if self.screenshot:
            self._button_clicked_cb = {
                'undo': self.screenshot.undo,
                'save': self.save_operate,
                'cancel': self.win.quit,
                'share': self.share_picture
            }
class Toolbar(object):
    ''' Toolbar window'''
    def __init__(self, parent=None, screenshot=None):
        '''
        init toolbar
        @param parent: the transient parent for this window
        @param screenshot: a Screenshot object
        '''
        self.screenshot = screenshot
        self.win = screenshot.window
        self.__config = OperateConfig()
        save_op = self.__config.get("save", "save_op")
        if save_op:
            self.screenshot.save_op_index = int(save_op)
        else:
            self.screenshot.save_op_index = SAVE_OP_AUTO

        #toolbar_padding_x = 15
        #toolbar_padding_y = 5
        #toolbar_icon_width = toolbar_icon_height = 28
        #toolbar_icon_num = 10
        #self.height = toolbar_icon_height + toolbar_padding_y * 2
        self.height = 30
        self.width = 279
        #self.width = 240

        self.window = Window(window_type=gtk.WINDOW_POPUP, shadow_visible=False)
        self.window.set_keep_above(True)
        self.window.set_decorated(False)
        self.window.set_transient_for(parent)

        self.toolbox = gtk.HBox(False, 6)
        toolbox_align = gtk.Alignment()
        toolbox_align.set(0, 0.5, 0, 0)
        toolbox_align.set_padding(2, 2, 11, 11)
        toolbox_align.add(self.toolbox)
        self.window.window_frame.pack_start(toolbox_align, True, True)
        #self.window.set_size_request(self.width, self.height)
        self.window.set_size_request(-1, self.height)

        self._toggle_button_list = []
        self.__button_accelerator_dict = {}
        self._toggle_button_group = ToggleButtonGroup([], 6)
        self.toolbox.pack_start(self._toggle_button_group)
        self.create_toggle_button("rect", ACTION_RECTANGLE, 0, _("Draw Rectangle"), "<Alt>1")
        self.create_toggle_button("ellipse", ACTION_ELLIPSE, 1, _("Draw Ellipse"), "<Alt>2")
        self.create_toggle_button("arrow",ACTION_ARROW, 2, _("Draw Arrow"), "<Alt>3")
        self.create_toggle_button("line",ACTION_LINE, 3, _("Draw Line"), "<Alt>4")
        self.create_toggle_button("text",ACTION_TEXT, 4, _("Draw Text"), "<Alt>5")

        self.create_button("undo", _("Undo"), "<Alt>6")

        if self.screenshot.is_subprocess:
            self.create_button("save", _("Save"), "<Alt>7")
        else:
            # pack save and list button
            save_combo_button = ComboButton(
                app_theme.get_pixbuf("action/save_normal.png"),
                app_theme.get_pixbuf("action/save_hover.png"),
                app_theme.get_pixbuf("action/save_press.png"),
                app_theme.get_pixbuf("action/save_normal.png"),
                app_theme.get_pixbuf("action/list_normal.png"),
                app_theme.get_pixbuf("action/list_hover.png"),
                app_theme.get_pixbuf("action/list_press.png"),
                app_theme.get_pixbuf("action/list_normal.png"),)
            save_combo_button.set_name("save")
            save_combo_button.connect("button-clicked", self._button_clicked, "save")
            save_combo_button.connect("arrow-clicked", self._list_menu_show)
            save_tip_text_list = ["Save automatically", "Save as", "Save to clipboard", "Save automatically to file and clipboard"]
            tip_text = save_tip_text_list[self.screenshot.save_op_index]
            save_combo_button.connect("enter-notify-event", self._show_tooltip, _(tip_text))
            self.toolbox.pack_start(save_combo_button)

        self.create_button("cancel", _("Cancel"), "<Alt>8")

        if not self.screenshot.is_subprocess:
            self.create_button("share", _("Share"), "<Alt>9")

        if self.screenshot:
            self._button_clicked_cb = {
                'undo': self.screenshot.undo,
                'save': self.save_operate,
                'cancel': self.win.quit,
                'share': self.share_picture}

    def create_toggle_button(self, name, action, index, text='', accel_key=None):
        '''
        create a togglebutton
        @param name: the button's name, a string
        @param action: one of ACTION Type Constants 
        @param index: the button's index in button list, an int num
        @param text: the button's tooltip text, a string
        '''
        button = ToggleButtonItem(
            (app_theme.get_pixbuf("action/" + name + "_normal.png"),
             app_theme.get_pixbuf("action/" + name + "_press.png"),
             app_theme.get_pixbuf("action/" + name + "_hover.png"),
             app_theme.get_pixbuf("action/" + name + "_press.png"), None),
            index, self._toggle_button_group.set_index, self._toggle_button_group.get_index)
        button.connect("pressed", self._toggle_button_pressed)
        button.connect("toggled", self._toggle_button_toggled, action)
        button.connect("enter-notify-event", self._show_tooltip, text)
        button.set_name(name)
        #self.toolbox.pack_start(button)
        self._toggle_button_group.pack_start(button)
        self._toggle_button_list.append(button)
        if accel_key:
            self.__button_accelerator_dict[gtk.accelerator_name(
                                           *gtk.accelerator_parse(accel_key))] = button

    def create_button(self, name, text='', accel_key=None):
        '''
        make a button
        @param name: the button's name, a string
        @param text: the button's tooltip text, a string
        '''
        button = ImageButton(
            app_theme.get_pixbuf("action/" + name + "_normal.png"),
            app_theme.get_pixbuf("action/" + name + "_hover.png"),
            app_theme.get_pixbuf("action/" + name + "_press.png"))
        button.connect("enter-notify-event", self._show_tooltip, text)
        button.connect("clicked", self._button_clicked, name)
        button.set_name(name)
        #button.set_size_request(28, 28)
        self.toolbox.pack_start(button)
        if accel_key:
            self.__button_accelerator_dict[gtk.accelerator_name(
                                           *gtk.accelerator_parse(accel_key))] = button
        return button

    def _show_tooltip(self, widget, event, text):
        '''the button enter-notify-event callback. Create help tooltip.'''
        #widget.set_has_tooltip(True)
        #widget.set_tooltip_text(text)
        #widget.trigger_tooltip_query()
        Tooltip.text(widget, text)

    def _list_menu_show(self, button, x, y, offset_x, offset_y):
        '''the combo button clicked callback. show combo_buton list menu'''
        menu_item = [
            (None, _("Save automatically"), self._list_menu_click, SAVE_OP_AUTO, button),
            (None, _("Save as"), self._list_menu_click, SAVE_OP_AS, button),
            (None, _("Save to clipboard"), self._list_menu_click, SAVE_OP_CLIP, button),
            (None, _("Save automatically to file and clipboard"), self._list_menu_click, SAVE_OP_AUTO_AND_CLIP, button)]
        # set current operate icon
        current_item = menu_item[self.screenshot.save_op_index] 
        menu_pixbuf = (
            app_theme.get_pixbuf("action/selected.png"),
            app_theme.get_pixbuf("action/selected_hover.png"),
            app_theme.get_pixbuf("action/selected.png"))
        menu_item[self.screenshot.save_op_index] = (menu_pixbuf, 
            current_item[1], current_item[2], current_item[3])
        self.combo_menu = Menu(menu_item, is_root_menu=True, 
            menu_item_select_color=app_theme.get_shadow_color("menu_item_select").get_color_info())
        self.set_all_inactive()
        self.combo_menu.show((x, y), (offset_x, offset_y))
    
    def _list_menu_click(self, save_op_index, button=None):
        '''list menu clicked callback'''
        self.screenshot.save_op_index = save_op_index
        self.__config.set("save", save_op=str(save_op_index))

        # reset the save button's tooltip
        if button:
            item = self.combo_menu.get_menu_items()[save_op_index]
            button.disconnect_by_func(self._show_tooltip)
            button.connect("enter-notify-event", self._show_tooltip, item.item[1])
            self.combo_menu.destroy()
        self.save_operate()

    def _button_clicked(self, widget, name):
        ''' button clicked callback '''
        if self.screenshot is None:
            return
        # save current input text
        if self.screenshot.show_text_window_flag:
            self.win.save_text_window()
        if name in self._button_clicked_cb:
            self._button_clicked_cb[name](widget)

    def _toggle_button_pressed(self, widget):
        ''' toggle button pressed callback '''
        # save current input text
        if self.screenshot.show_text_window_flag:
            self.win.save_text_window()
        widget.released()

    def _toggle_button_toggled(self, widget, action):
        ''' toggle button toggled callback'''
        if self.screenshot is None:
            return
        if widget.get_active():
            self.screenshot.set_action_type(action)
            self.win.set_cursor(action)
            self.win.show_colorbar()
            self.win.adjust_colorbar()
        elif widget.index == widget.get_index():
            self.win.set_cursor(None)
            self.win.hide_colorbar()
            if not self.screenshot.action_list and not self.screenshot.text_action_list and self.screenshot.show_toolbar_flag and not self.screenshot.window_flag:
                self.screenshot.set_action_type(ACTION_SELECT)
            elif self.screenshot.action_list:
                self.screenshot.set_action_type(None)
    
    def set_button_active(self, name, state):
        '''
        set button active
        @param name: the button's name which will set, a string type
        @param state: the state to set, True or False
        '''
        if self._toggle_button_group.is_active():
            button = self._toggle_button_list[self._toggle_button_group.get_index()]
            # if the button has been in this state, ignore
            if button.name == name:
                if button.get_active() == state:
                    return
            else:
                button.set_active(False)
                self._toggle_button_group.set_index(-1)
        i = 0
        for each in self._toggle_button_list:
            if name == each.get_name():
                each.set_active(state)
                self._toggle_button_group.set_index(i)
                break
            i += 1
    
    def has_button_active(self):
        '''
        is has one toggle button active
        @return: True if has one togglebutton active, otherwise False
        '''
        return self._toggle_button_group.is_active()
    
    def save_operate(self, widget=None):
        '''do save operate'''
        screenshot = self.screenshot
        if screenshot.is_subprocess:
            screenshot.save_to_tmp_file()
        else:
            # auto save
            if screenshot.save_op_index == SAVE_OP_AUTO:
                folder = utils.get_pictures_dir()
                filename = "%s%s.%s" % (_(DEFAULT_FILENAME), utils.get_format_time(), "png")
                screenshot.save_snapshot("%s/%s" % (folder, filename))
            # save as
            elif screenshot.save_op_index == SAVE_OP_AS:
                self.save_to_file()
            # copy to clip
            elif screenshot.save_op_index == SAVE_OP_CLIP:
                screenshot.save_snapshot()
            # auto save and copy to clip
            else:
                folder = utils.get_pictures_dir()
                filename = "%s%s.%s" % (_(DEFAULT_FILENAME), utils.get_format_time(), "png")
                screenshot.save_snapshot("%s/%s" % (folder, filename), clip_flag=True)
    
    def share_picture(self, widget):
        '''share picture. share button clicked callback'''
        self.screenshot.share_to_flag = True
        self.screenshot.save_op_index = SAVE_OP_AUTO
        self.save_operate()

    def save_to_file(self):
        ''' save to file '''
        self.win.hide_colorbar()
        self.win.hide_toolbar()
        #dialog = SaveFileDialog('', self.screenshot.window.window,
            #ok_callback=self._save_to_file_cb, cancel_callback=self._save_to_file_cancel)
        dialog = gtk.FileChooserDialog(
            "Save..",
            self.win.window,
            gtk.FILE_CHOOSER_ACTION_SAVE,
            (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
             gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT))
        dialog.set_action(gtk.FILE_CHOOSER_ACTION_SAVE)
        dialog.set_default_response(gtk.RESPONSE_ACCEPT)
        dialog.set_position(gtk.WIN_POS_MOUSE)
        dialog.set_local_only(True)
        last_folder = self.__config.get("save", "folder")
        if last_folder:
            dialog.set_current_folder(last_folder)
        else:
            dialog.set_current_folder(utils.get_pictures_dir())
        if config.OPTION_FILE:
            dialog.set_current_name(config.OPTION_FILE)
        else:
            dialog.set_current_name("%s%s.%s" % (_(DEFAULT_FILENAME), utils.get_format_time(), "png"))
        response = dialog.run()
        filename = dialog.get_filename()
        if response == gtk.RESPONSE_ACCEPT:
            self.__config.set("save", folder=dialog.get_current_folder())
            self._save_to_file_cb(filename)
        else:
            self._save_to_file_cancel(filename)
        dialog.destroy()

    def _save_to_file_cancel(self, filename):
        ''' save file dialog cancel_callback'''
        self.screenshot.share_to_flag = False
        self.win.adjust_toolbar()
        self.win.show_toolbar()
        if self.has_button_active():
            self.win.show_colorbar()
        
    def _save_to_file_cb(self, filename):
        ''' save file dialog ok_callback'''
        print "save", filename
        self.screenshot.save_snapshot(filename=filename)
    
    def set_all_inactive(self):
        '''set all toggle button inactive'''
        #index = self._toggle_button_group.get_index()
        #if index != -1:
            #self._toggle_button_list[index].set_active(False)
        #for each in self._toggle_button_list:
            #each.set_active(False)
        if self._toggle_button_group.is_active():
            self._toggle_button_group.set_index(-1)
    
    def accel_group_callback(self, group, acceleratable, keyval, modifier):
        accel_name = gtk.accelerator_name(keyval, modifier)
        if accel_name in self.__button_accelerator_dict:
            button = self.__button_accelerator_dict[accel_name]
            button.pressed()
            button.released()
            #button.clicked()
            self.set_button_active(button.get_name(), True)
        
    def show(self):
        ''' show the toolbar '''
        if not self.window.get_visible():
            self.window.show_window()
        #print "toolbox:", self.toolbox.allocation, self.window.allocation

    def hide(self):
        '''hide the toolbar'''
        if self.window.get_visible():
            self.window.hide_all()
Beispiel #4
0
class Toolbar():
    ''' Toolbar window'''
    def __init__(self, parent=None, screenshot=None):
        '''
        init toolbar
        @param parent: the transient parent for this window
        @param screenshot: a Screenshot object
        '''
        self.screenshot = screenshot
        self.win = screenshot.window
        self.__config = OperateConfig()
        save_op = self.__config.get("save", "save_op")
        if save_op:
            self.screenshot.save_op_index = int(save_op)
        else:
            self.screenshot.save_op_index = SAVE_OP_AUTO

        #toolbar_padding_x = 15
        #toolbar_padding_y = 5
        #toolbar_icon_width = toolbar_icon_height = 28
        #toolbar_icon_num = 10
        #self.height = toolbar_icon_height + toolbar_padding_y * 2
        self.height = 30
        self.width = 279
        #self.width = 240

        self.window = Window(window_type=gtk.WINDOW_POPUP,
                             shadow_visible=False)
        self.window.set_keep_above(True)
        self.window.set_decorated(False)
        self.window.set_transient_for(parent)

        self.toolbox = gtk.HBox(False, 6)
        toolbox_align = gtk.Alignment()
        toolbox_align.set(0, 0.5, 0, 0)
        toolbox_align.set_padding(2, 2, 11, 11)
        toolbox_align.add(self.toolbox)
        self.window.window_frame.pack_start(toolbox_align, True, True)
        #self.window.set_size_request(self.width, self.height)
        self.window.set_size_request(-1, self.height)

        self._toggle_button_list = []
        self._toggle_button_group = ToggleButtonGroup([], 6)
        self.toolbox.pack_start(self._toggle_button_group)
        self.create_toggle_button("rect", ACTION_RECTANGLE, 0,
                                  _("draw rectangle"))
        self.create_toggle_button("ellipse", ACTION_ELLIPSE, 1,
                                  _("draw ellipse"))
        self.create_toggle_button("arrow", ACTION_ARROW, 2, _("draw arrow"))
        self.create_toggle_button("line", ACTION_LINE, 3, _("draw line"))
        self.create_toggle_button("text", ACTION_TEXT, 4, _("draw Text"))

        self.create_button("undo", _("undo"))
        # pack save and list button
        save_combo_button = ComboButton(
            app_theme.get_pixbuf("action/save_normal.png"),
            app_theme.get_pixbuf("action/save_hover.png"),
            app_theme.get_pixbuf("action/save_press.png"),
            app_theme.get_pixbuf("action/save_normal.png"),
            app_theme.get_pixbuf("action/list_normal.png"),
            app_theme.get_pixbuf("action/list_hover.png"),
            app_theme.get_pixbuf("action/list_press.png"),
            app_theme.get_pixbuf("action/list_normal.png"),
        )
        save_combo_button.set_name("save")
        save_combo_button.connect("button-clicked", self._button_clicked,
                                  "save")
        save_combo_button.connect("arrow-clicked", self._list_menu_show)
        save_tip_text_list = [
            "save automatically", "save as", "save to clipboard",
            "save automatically to file and clipboard"
        ]
        tip_text = save_tip_text_list[self.screenshot.save_op_index]
        save_combo_button.connect("enter-notify-event", self._show_tooltip,
                                  _(tip_text))
        self.toolbox.pack_start(save_combo_button)

        self.create_button("cancel", _("cancel"))
        self.create_button("share", _("share"))

        if self.screenshot:
            self._button_clicked_cb = {
                'undo': self.screenshot.undo,
                'save': self.save_operate,
                'cancel': self.win.quit,
                'share': self.share_picture
            }

    def create_toggle_button(self, name, action, index, text=''):
        '''
        create a togglebutton
        @param name: the button's name, a string
        @param action: one of ACTION Type Constants 
        @param index: the button's index in button list, an int num
        @param text: the button's tooltip text, a string
        '''
        button = ToggleButtonItem(
            (app_theme.get_pixbuf("action/" + name + "_normal.png"),
             app_theme.get_pixbuf("action/" + name + "_press.png"),
             app_theme.get_pixbuf("action/" + name + "_hover.png"),
             app_theme.get_pixbuf("action/" + name + "_press.png"), None),
            index, self._toggle_button_group.set_index,
            self._toggle_button_group.get_index)
        button.connect("pressed", self._toggle_button_pressed)
        button.connect("toggled", self._toggle_button_toggled, action)
        button.connect("enter-notify-event", self._show_tooltip, text)
        button.set_name(name)
        #self.toolbox.pack_start(button)
        self._toggle_button_group.pack_start(button)
        self._toggle_button_list.append(button)

    def create_button(self, name, text=''):
        '''
        make a button
        @param name: the button's name, a string
        @param text: the button's tooltip text, a string
        '''
        button = ImageButton(
            app_theme.get_pixbuf("action/" + name + "_normal.png"),
            app_theme.get_pixbuf("action/" + name + "_hover.png"),
            app_theme.get_pixbuf("action/" + name + "_press.png"))
        button.connect("enter-notify-event", self._show_tooltip, text)
        button.connect("clicked", self._button_clicked, name)
        button.set_name(name)
        #button.set_size_request(28, 28)
        self.toolbox.pack_start(button)
        return button

    def _show_tooltip(self, widget, event, text):
        '''the button enter-notify-event callback. Create help tooltip.'''
        #widget.set_has_tooltip(True)
        #widget.set_tooltip_text(text)
        #widget.trigger_tooltip_query()
        Tooltip.text(widget, text)

    def _list_menu_show(self, button, x, y, offset_x, offset_y):
        '''the combo button clicked callback. show combo_buton list menu'''
        menu_item = [(None, _("save automatically"), self._list_menu_click,
                      SAVE_OP_AUTO, button),
                     (None, _("save as"), self._list_menu_click, SAVE_OP_AS,
                      button),
                     (None, _("save to clipboard"), self._list_menu_click,
                      SAVE_OP_CLIP, button),
                     (None, _("save automatically to file and clipboard"),
                      self._list_menu_click, SAVE_OP_AUTO_AND_CLIP, button)]
        # set current operate icon
        current_item = menu_item[self.screenshot.save_op_index]
        menu_pixbuf = (app_theme.get_pixbuf("action/selected.png"),
                       app_theme.get_pixbuf("action/selected_hover.png"),
                       app_theme.get_pixbuf("action/selected.png"))
        menu_item[self.screenshot.save_op_index] = (menu_pixbuf,
                                                    current_item[1],
                                                    current_item[2],
                                                    current_item[3])
        self.combo_menu = Menu(
            menu_item,
            is_root_menu=True,
            menu_item_select_color=app_theme.get_shadow_color(
                "menu_item_select").get_color_info())
        self.set_all_inactive()
        self.combo_menu.show((x, y), (offset_x, offset_y))

    def _list_menu_click(self, save_op_index, button=None):
        '''list menu clicked callback'''
        self.screenshot.save_op_index = save_op_index
        self.__config.set("save", save_op=str(save_op_index))

        # reset the save button's tooltip
        if button:
            item = self.combo_menu.get_menu_items()[save_op_index]
            button.disconnect_by_func(self._show_tooltip)
            button.connect("enter-notify-event", self._show_tooltip,
                           item.item[1])
            self.combo_menu.destroy()
        self.save_operate()

    def _button_clicked(self, widget, name):
        ''' button clicked callback '''
        if self.screenshot is None:
            return
        # save current input text
        if self.screenshot.show_text_window_flag:
            self.win.save_text_window()
        if name in self._button_clicked_cb:
            self._button_clicked_cb[name](widget)

    def _toggle_button_pressed(self, widget):
        ''' toggle button pressed callback '''
        # save current input text
        if self.screenshot.show_text_window_flag:
            self.win.save_text_window()

    def _toggle_button_toggled(self, widget, action):
        ''' toggle button toggled callback'''
        if self.screenshot is None:
            return
        if widget.get_active():
            self.screenshot.set_action_type(action)
            self.win.set_cursor(action)
            self.win.show_colorbar()
            self.win.adjust_colorbar()
        else:
            self.win.set_cursor(None)
            self.win.hide_colorbar()
            if not self.screenshot.action_list and not self.screenshot.text_action_list and self.screenshot.show_toolbar_flag and not self.screenshot.window_flag:
                self.screenshot.set_action_type(ACTION_SELECT)
            elif self.screenshot.action_list:
                self.screenshot.set_action_type(None)

    def set_button_active(self, name, state):
        '''
        set button active
        @param name: the button's name which will set, a string type
        @param state: the state to set, True or False
        '''
        if self._toggle_button_group.is_active():
            button = self._toggle_button_list[
                self._toggle_button_group.get_index()]
            # if the button has been in this state, ignore
            if button.name == name:
                if button.get_active() == state:
                    return
            else:
                button.set_active(False)
                self._toggle_button_group.set_index(-1)
        i = 0
        for each in self._toggle_button_list:
            if name == each.get_name():
                each.set_active(state)
                self._toggle_button_group.set_index(i)
                break
            i += 1

    def has_button_active(self):
        '''
        is has one toggle button active
        @return: True if has one togglebutton active, otherwise False
        '''
        return self._toggle_button_group.is_active()

    def save_operate(self, widget=None):
        '''do save operate'''
        screenshot = self.screenshot
        #print "operate:", screenshot.save_op_index
        # auto save
        if screenshot.save_op_index == SAVE_OP_AUTO:
            folder = utils.get_pictures_dir()
            filename = "%s%s.%s" % (_(DEFAULT_FILENAME),
                                    utils.get_format_time(), "png")
            screenshot.save_snapshot("%s/%s" % (folder, filename))
        # save as
        elif screenshot.save_op_index == SAVE_OP_AS:
            self.save_to_file()
        # copy to clip
        elif screenshot.save_op_index == SAVE_OP_CLIP:
            screenshot.save_snapshot()
        # auto save and copy to clip
        else:
            folder = utils.get_pictures_dir()
            filename = "%s%s.%s" % (_(DEFAULT_FILENAME),
                                    utils.get_format_time(), "png")
            screenshot.save_snapshot("%s/%s" % (folder, filename),
                                     clip_flag=True)

    def share_picture(self, widget):
        '''share picture. share button clicked callback'''
        self.screenshot.share_to_flag = True
        self.screenshot.save_op_index = SAVE_OP_AUTO
        self.save_operate()

    def save_to_file(self):
        ''' save to file '''
        self.win.hide_colorbar()
        self.win.hide_toolbar()
        #dialog = SaveFileDialog('', self.screenshot.window.window,
        #ok_callback=self._save_to_file_cb, cancel_callback=self._save_to_file_cancel)
        dialog = gtk.FileChooserDialog("Save..", self.win.window,
                                       gtk.FILE_CHOOSER_ACTION_SAVE,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                                        gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT))
        dialog.set_action(gtk.FILE_CHOOSER_ACTION_SAVE)
        dialog.set_default_response(gtk.RESPONSE_ACCEPT)
        dialog.set_position(gtk.WIN_POS_MOUSE)
        dialog.set_local_only(True)
        last_folder = self.__config.get("save", "folder")
        if last_folder:
            dialog.set_current_folder(last_folder)
        else:
            dialog.set_current_folder(utils.get_pictures_dir())
        dialog.set_current_name(
            "%s%s.%s" % (_(DEFAULT_FILENAME), utils.get_format_time(), "png"))
        response = dialog.run()
        filename = dialog.get_filename()
        if response == gtk.RESPONSE_ACCEPT:
            self.__config.set("save", folder=dialog.get_current_folder())
            self._save_to_file_cb(filename)
        else:
            self._save_to_file_cancel(filename)
        dialog.destroy()

    def _save_to_file_cancel(self, filename):
        ''' save file dialog cancel_callback'''
        self.screenshot.share_to_flag = False
        self.win.adjust_toolbar()
        self.win.show_toolbar()
        if self.has_button_active():
            self.win.show_colorbar()

    def _save_to_file_cb(self, filename):
        ''' save file dialog ok_callback'''
        print "save", filename
        self.screenshot.save_snapshot(filename=filename)

    def set_all_inactive(self):
        '''set all toggle button inactive'''
        #index = self._toggle_button_group.get_index()
        #if index != -1:
        #self._toggle_button_list[index].set_active(False)
        #for each in self._toggle_button_list:
        #each.set_active(False)
        if self._toggle_button_group.is_active():
            self._toggle_button_group.set_index(-1)

    def show(self):
        ''' show the toolbar '''
        if not self.window.get_visible():
            self.window.show_window()
        #print "toolbox:", self.toolbox.allocation, self.window.allocation

    def hide(self):
        '''hide the toolbar'''
        if self.window.get_visible():
            self.window.hide_all()