Skip to content

Commit

Permalink
Settings are now savable and loadable. Fixed the 'changed open' bug i…
Browse files Browse the repository at this point in the history
…n a different way :P
  • Loading branch information
dom96 committed Mar 28, 2010
1 parent c6fcdab commit b02183f
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 41 deletions.
76 changes: 45 additions & 31 deletions aporia.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@
#

import glib2, gtk2, gdk2, gtksourceview, dialogs, os, pango
import settings, types
import settings, types, cfg
{.push callConv:cdecl.}

var win: types.MainWin
win.Tabs = @[]
# Some default settings - These will be loaded from a cfg file in the future
win.settings.search = "caseinsens"
win.settings.font = "monospace 9"
win.settings.colorSchemeID = "cobalt"

# Load the settings
try:
win.settings = cfg.load()
except EIO:
dialogs.error(win.w, "Could not load configuration file.")
quit(QuitFailure)
except ECFGParse:
# Ask araq how to get the msg of the error
dialogs.error(win.w, "Error parsing the configuration file.")
quit(QuitFailure)

# GTK Events
# -- w(PWindow)
proc destroy(widget: PWidget, data: pgpointer){.cdecl.} =
proc destroy(widget: PWidget, data: pgpointer){.cdecl.} =
# First save the settings
win.settings.save()
main_quit()

# -- SourceView(PSourceView) & SourceBuffer
Expand Down Expand Up @@ -67,27 +76,24 @@ proc createTabLabel(name: string, t_child: PWidget): PWidget =
box.showAll()
return box

var repelChanged: bool = False # When a file is opened, the text changes
# Repel the "changed" event, when opening files
proc changed(buffer: PTextBuffer, user_data: pgpointer){.cdecl.} =
# Update the 'Line & Column'
updateStatusBar(buffer)

if repelChanged == False:
# Change the tabs state to 'unsaved'
# and add '*' to the Tab Name
var current = win.SourceViewTabs.getCurrentPage()
var name = ""
if win.Tabs[current].filename == "":
win.Tabs[current].saved = False
name = "Untitled *"
else:
win.Tabs[current].saved = False
name = splitFile(win.Tabs[current].filename).name &
splitFile(win.Tabs[current].filename).ext & " *"

var cTab = win.sourceViewTabs.getNthPage(current)
win.sourceViewTabs.setTabLabel(cTab, createTabLabel(name, cTab))
# Change the tabs state to 'unsaved'
# and add '*' to the Tab Name
var current = win.SourceViewTabs.getCurrentPage()
var name = ""
if win.Tabs[current].filename == "":
win.Tabs[current].saved = False
name = "Untitled *"
else:
win.Tabs[current].saved = False
name = splitFile(win.Tabs[current].filename).name &
splitFile(win.Tabs[current].filename).ext & " *"

var cTab = win.sourceViewTabs.getNthPage(current)
win.sourceViewTabs.setTabLabel(cTab, createTabLabel(name, cTab))

# Other(Helper) functions

Expand All @@ -102,8 +108,8 @@ proc initSourceView(SourceView: var PWidget, scrollWindow: var PScrolledWindow,
# SourceView(gtkSourceView)
SourceView = sourceViewNew()
PSourceView(SourceView).setInsertSpacesInsteadOfTabs(True)
PSourceView(SourceView).setIndentWidth(2)
PSourceView(SourceView).setShowLineNumbers(True)
PSourceView(SourceView).setIndentWidth(win.settings.indentWidth)
PSourceView(SourceView).setShowLineNumbers(win.settings.showLineNumbers)

var font = font_description_from_string(win.settings.font)
SourceView.modifyFont(font)
Expand All @@ -112,6 +118,8 @@ proc initSourceView(SourceView: var PWidget, scrollWindow: var PScrolledWindow,
SourceView.show()
# -- Set the syntax highlighter language
buffer = PSourceBuffer(PTextView(SourceView).getBuffer())
buffer.setHighlightMatchingBrackets(
win.settings.highlightMatchingBrackets)

# UGLY workaround for yet another compiler bug:
discard gsignalConnect(buffer, "mark-set",
Expand Down Expand Up @@ -162,14 +170,20 @@ proc openFile(menuItem: PMenuItem, user_data: pgpointer) =
var file: string = readFile(path)
if file != nil:
addTab("", path)
# Repel the 'changed' event
repelChanged = True
# Set the TextBuffer's text.
win.Tabs[win.Tabs.len()-1].buffer.set_text(file, len(file))
# Switch to the newly created tab
win.sourceViewTabs.setCurrentPage(win.Tabs.len()-1)
# Set the TextBuffer's text.
var newTab = win.Tabs[win.Tabs.len()-1]
newTab.buffer.set_text(file, len(file))
# Change the saved state to True,
# set_text(changed event) will change it to False
# We need to reset it, we also need to change
# the tab label because it got changed.
newTab.saved = True
var name = splitFile(newTab.filename).name & splitFile(newTab.filename).ext
var cTab = win.sourceViewTabs.getNthPage(win.Tabs.len()-1)
win.sourceViewTabs.setTabLabel(cTab, createTabLabel(name, cTab))

repelChanged = False # Change it back to default
else:
error(win.w, "Unable to read from file")

Expand Down Expand Up @@ -198,7 +212,7 @@ proc saveFile(menuItem: PMenuItem, user_data: pgpointer) =
f.write(text)
f.close()

# Change the tab name, .Tabs.filename etc.
# Change the tab name and .Tabs.filename etc.
win.Tabs[current].filename = path
win.Tabs[current].saved = True
var name = splitFile(path).name & splitFile(path).ext
Expand Down
75 changes: 75 additions & 0 deletions cfg.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#
#
# Aporia - Nimrod IDE
# (c) Copyright 2010 Dominik Picheta
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
import types, times, streams, parsecfg, strutils

type
ECFGParse* = object of E_Base

proc save*(settings: TSettings) =
var f: TFile
if open(f, "config.conf", fmWrite):
var confInfo = "; Aporia configuration file - Created on "
confInfo.add($getTime())
f.write(confInfo & "\n")

f.write("[editor]\n")
f.write("font = \"" & settings.font & "\"\n")
f.write("scheme = \"" & settings.colorSchemeID & "\"\n")
f.write("indentWidth = " & $settings.indentWidth & "\n")
f.write("showLineNumbers = " & $settings.showLineNumbers & "\n")
f.write("highlightMatchingBrackets = " &
$settings.highlightMatchingBrackets & "\n")

f.write("[other]\n")
f.write("searchMethod = \"" & settings.search & "\"\n")

f.close()

proc load*(): TSettings =
var f = newFileStream("config.conf", fmRead)
if f != nil:
var p: TCfgParser
open(p, f, "config.conf")
while True:
var e = next(p)
case e.kind
of cfgEof:
break
of cfgKeyValuePair:
case e.key
of "font":
result.font = e.value
of "scheme":
result.colorSchemeID = e.value
of "indentWidth":
result.indentWidth = e.value.parseInt()
of "showLineNumbers":
result.showLineNumbers = e.value == "true"
of "highlightMatchingBrackets":
result.highlightMatchingBrackets = e.value == "true"
of "searchMethod":
result.search = e.value
of cfgError:
raise newException(ECFGParse, e.msg)
of cfgSectionStart, cfgOption:
#
else:
raise newException(EIO, "Could not open configuration file.")

when isMainModule:

echo(load().showLineNumbers)

var s: TSettings
s.search = "caseinsens"
s.font = "monospace 9"
s.colorSchemeID = "cobalt"

save(s)

9 changes: 9 additions & 0 deletions config.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; Aporia configuration file - Created on Sun Mar 28 17:35:55 2010
[editor]
font = "Monospace 9"
scheme = "oblivion"
indentWidth = 2
showLineNumbers = true
highlightMatchingBrackets = true
[other]
searchMethod = "caseinsens"
5 changes: 4 additions & 1 deletion gtksourceview.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ proc source_buffer_new*(table: PTextTagTable): PSourceBuffer {.cdecl, dynlib: li

proc source_buffer_new_with_language*(language: PSourceLanguage): PSourceBuffer {.cdecl, dynlib: lib,
importc: "gtk_source_buffer_new_with_language".}


proc set_highlight_matching_brackets*(view: PSourceBuffer, show: gboolean) {.cdecl, dynlib: lib,
importc: "gtk_source_buffer_set_highlight_matching_brackets".}

proc undo*(buffer: PSourceBuffer) {.cdecl, dynlib: lib,
importc: "gtk_source_buffer_undo".}

Expand Down
20 changes: 13 additions & 7 deletions settings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
# distribution, for details about the copyright.
#

import gtk2, gdk2, glib2
import gtk2, gdk2, glib2, pango
import gtksourceview, types
{.push callConv:cdecl.}

var win: types.MainWin
var win: ptr types.MainWin

# -- Fonts and Colors --

Expand Down Expand Up @@ -40,6 +40,7 @@ proc schemesTreeView_onChanged(selection: PGObject, user_data: pgpointer) =
if getSelected(PTreeSelection(selection), addr(model), addr(iter)):
model.get(addr(iter), 0, addr(value), -1)
win.settings.colorSchemeID = $value
echo(win.settings.colorSchemeID)
var schemeMan = schemeManagerGetDefault()
var scheme = schemeMan.getScheme(value)
# Loop through each tab, and set the scheme
Expand All @@ -52,6 +53,7 @@ proc fontDialog_Canc(widget: PWidget, user_data: PFontSelectionDialog) =
PDialog(userData).response(RESPONSE_CANCEL)

proc fontChangeBtn_Clicked(widget: PWidget, user_data: PEntry) =
# Initialize the FontDialog
var fontDialog = fontSelectionDialogNew("Select font")
fontDialog.setTransientFor(win.w)
discard fontDialog.dialogSetFontName(win.settings.font)
Expand All @@ -67,6 +69,11 @@ proc fontChangeBtn_Clicked(widget: PWidget, user_data: PEntry) =
if result == RESPONSE_OK:
win.settings.font = $fontDialog.dialogGetFontName()
userData.setText(fontDialog.dialogGetFontName())
# Loop through each tab, and change the font
for i in items(win.Tabs):
var font = fontDescriptionFromString(win.settings.font)
i.sourceView.modifyFont(font)

gtk2.POBject(fontDialog).destroy()


Expand Down Expand Up @@ -146,14 +153,13 @@ proc initFontsColors(settingsTabs: PNotebook) =
var dialog: gtk2.PWindow
proc closeDialog(widget: pWidget, user_data: pgpointer) =
gtk2.PObject(dialog).destroy()
proc dialog_destroy(widget: PWidget, user_data: pgpointer) =
# save the settings to file, win.settings should be set by the use of events

proc showSettings*(aWin: types.MainWin) =
win = aWin
proc showSettings*(aWin: var types.MainWin) =
win = addr(aWin) # This has to be a pointer
# Because i need the settings to be changed
# in aporia.nim not in here.

dialog = windowNew(gtk2.WINDOW_TOPLEVEL)
discard dialog.signalConnect("destroy", SIGNAL_FUNC(dialog_destroy), nil)
dialog.setDefaultSize(300, 400)
dialog.setSizeRequest(300, 400)
dialog.setTransientFor(win.w)
Expand Down
9 changes: 7 additions & 2 deletions types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import gtk2, gtksourceview
type

TSettings* = object

search*: string
font*: string
colorSchemeID*: string

font*: string # font used by the sourceview
colorSchemeID*: string # color scheme used by the sourceview
indentWidth*: int # how many spaces used for indenting code(in the sourceview)
showLineNumbers*: bool # whether to show line numbers in the sourceview
highlightMatchingBrackets*: bool # whether to highlight matching brackets

MainWin* = object
# Widgets
Expand Down

0 comments on commit b02183f

Please sign in to comment.