Skip to content

Commit

Permalink
Seperated tooltip setting code into its own function.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Mar 22, 2013
1 parent 780c606 commit 9565195
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
68 changes: 31 additions & 37 deletions aporia.nim
Expand Up @@ -193,8 +193,6 @@ proc Exit() =

# GTK Events
# -- w(PWindow)
proc destroy(widget: PWidget, data: pgpointer) {.cdecl.} =
aporia.Exit()

proc confirmUnsaved(win: var MainWin, t: Tab): int =
var askSave = win.w.messageDialogNew(0, MessageWarning, BUTTONS_NONE, nil)
Expand Down Expand Up @@ -386,7 +384,7 @@ proc cursorMoved(buffer: PTextBuffer, location: PTextIter,
proc onCloseTab(btn: PButton, child: PWidget)
proc tab_buttonRelease(widg: PWidget, ev: PEventButton,
userDat: pwidget): bool
proc createTabLabel(name: string, t_child: PWidget): tuple[box: PWidget,
proc createTabLabel(name: string, t_child: PWidget, filename: string): tuple[box: PWidget,
label: PLabel, closeBtn: PButton] =
var eventBox = eventBoxNew()
eventBox.setVisibleWindow(false)
Expand All @@ -395,6 +393,10 @@ proc createTabLabel(name: string, t_child: PWidget): tuple[box: PWidget,

var box = hboxNew(False, 0)
var label = labelNew(name)
if filename.startsWith(getTempDir()):
# If this is a temporary tab, mark it as such.
label.setMarkup(name & "<span color=\"#CC0E0E\"> *</span>")

var closebtn = buttonNew()
closeBtn.setLabel(nil)
var iconSize = iconSizeFromName("tabIconSize")
Expand All @@ -411,6 +413,20 @@ proc createTabLabel(name: string, t_child: PWidget): tuple[box: PWidget,
eventBox.add(box)
return (eventBox, label, closeBtn)


proc setTabTooltip(t: Tab) =
## (Re)sets the tab tooltip text.
if t.filename != "":
var tooltip = "<b>Path: </b> " & t.filename & "\n" &
"<b>Language: </b> " & getLanguageName(win, t.buffer)
if t.filename.startsWith(getTempDir()):
tooltip.add("\n<i>File is saved in temporary files and may be deleted.</i>")
t.label.setTooltipMarkup(tooltip)
else:
var tooltip = "<i>Tab is not saved.</i>\n" &
"<b>Language: </b> " & getLanguageName(win, t.buffer)
t.label.setTooltipMarkup(tooltip)

proc onModifiedChanged(buffer: PTextBuffer, theTab: gpointer) =
## This signal is called when the modification state of ``buffer`` is changed.
# *Warning* we assume here that the currently selected tab was modified.
Expand All @@ -430,16 +446,7 @@ proc onModifiedChanged(buffer: PTextBuffer, theTab: gpointer) =
else:
cTab.saved = true

var name = extractFilename(cTab.filename)
var tooltip = "<b>Path: </b> " & cTab.filename & "\n" &
"<b>Language: </b> " & getLanguageName(win, ctab.buffer)

if cTab.isTemporary:
cTab.label.setMarkup(name & "<span color=\"#CC0E0E\"> *</span>")
tooltip.add("\n<i>File is saved in temporary files and may be lost.</i>")
else:
cTab.label.setText(name)
cTab.label.setTooltipMarkup(tooltip)
setTabTooltip(cTab)

proc onChanged(buffer: PTextBuffer, sv: PSourceView) =
## This function is connected to the "changed" event on `buffer`.
Expand Down Expand Up @@ -748,20 +755,7 @@ proc addTab(name, filename: string, setCurrent: bool = True, encoding = "utf-8")
# Get the name.ext of the filename, for the tabs title
nam = extractFilename(filename)

var (TabLabel, labelText, closeBtn) = createTabLabel(nam, scrollWindow)

# Set the tooltip.
if filename != "":
var tooltip = "<b>Path: </b> " & filename & "\n" &
"<b>Language: </b> " & getLanguageName(win, buffer)
if filename.startsWith(getTempDir()):
labelText.setMarkup(nam & "<span color=\"#CC0E0E\"> *</span>")
tooltip.add("\n<i>File is saved in temporary files and may be deleted.</i>")
TabLabel.setTooltipMarkup(tooltip)
else:
var tooltip = "<i>Tab is not saved.</i>\n" &
"<b>Language: </b> " & getLanguageName(win, buffer)
TabLabel.setTooltipMarkup(tooltip)
var (TabLabel, labelText, closeBtn) = createTabLabel(nam, scrollWindow, filename)

# Add a tab
var nTab: Tab
Expand All @@ -777,6 +771,9 @@ proc addTab(name, filename: string, setCurrent: bool = True, encoding = "utf-8")
nTab.closeBtn.hide()
win.Tabs.add(nTab)

# Set the tooltip
setTabTooltip(win.Tabs[win.tabs.len-1])

# Add the tab to the GtkNotebook
let res = win.SourceViewTabs.appendPage(scrollWindow, TabLabel)
assert res != -1
Expand Down Expand Up @@ -1103,14 +1100,7 @@ proc pl_Toggled(menuitem: PCheckMenuItem, id: cstring) =
win.setHighlightSyntax(currentTab, True)
win.setLanguage(currentTab, langMan.getLanguage(id))

# Set tooltip
var name = extractFilename(win.Tabs[currentTab].filename)
var tooltip = "<b>Path: </b> " & name & "\n" &
"<b>Language: </b> " & getLanguageName(win, win.Tabs[currentTab].buffer)
if win.Tabs[currentTab].isTemporary:
win.Tabs[currentTab].label.setMarkup(name & "<span color=\"#CC0E0E\"> *</span>")
tooltip.add("\n<i>File is saved in temporary files and may be lost.</i>")
win.Tabs[currentTab].label.setTooltipMarkup(tooltip)
setTabTooltip(win.tabs[currentTab])

plCheckUpdate(currentTab)

Expand Down Expand Up @@ -2208,8 +2198,12 @@ proc initControls() =
win.w.setDefaultSize(win.autoSettings.winWidth, win.autoSettings.winHeight)
win.w.setTitle("Aporia")
if win.autoSettings.winMaximized: win.w.maximize()

discard win.w.signalConnect("destroy", SIGNAL_FUNC(aporia.destroy), nil)

let winDestroy =
proc (widget: PWidget, data: pgpointer) {.cdecl.} =
aporia.Exit()

discard win.w.signalConnect("destroy", SIGNAL_FUNC(winDestroy), nil)
discard win.w.signalConnect("delete_event",
SIGNAL_FUNC(aporia.delete_event), nil)
discard win.w.signalConnect("window-state-event",
Expand Down
1 change: 1 addition & 0 deletions todo.markdown
Expand Up @@ -92,3 +92,4 @@
* Being able to go to column with go to line feature
* Force focus the selected tab after initialisation.
* When Raw Preferences are edited, need to reapply some settings to sourceview (color scheme, indentation width etc) among other settings.
* When wrap around is disabled, if term searched for is above the cursor a "Match not found" error is shown. More informative message should be shown instead.

0 comments on commit 9565195

Please sign in to comment.